ASE¶
Official website |
How is metatomic supported? |
|---|---|
Via the |
Supported model outputs¶
the energy, non-conservative forces and stress including their variants are supported and fully integrated with ASE calculator interface (i.e.
ase.Atoms.get_potential_energy(),ase.Atoms.get_forces(), …);arbitrary outputs can be computed for any
ase.AtomsusingMetatomicCalculator.run_model();
How to install the code¶
The code is available in the metatomic-ase package, which can be installed
using pip install metatomic-ase.
How to use the code¶
metatomic_ase.MetatomicCalculator supports all the features
described above, and is designed to be used as a drop-in replacement for any ASE
calculator in any ASE workflow. You can also check the corresponding
tutorial. To compute rotationally-averaged energies,
forces, and stresses with non-equivariant architectures, wrap the model with
metatomic.torch.o3.SymmetrizedModel.wrap() before passing it to the
calculator.
API documentation¶
- class metatomic_ase.MetatomicCalculator(model: str | bytes | PurePath | AtomisticModel, *, additional_outputs: Dict[str, ModelOutput] | None = None, extensions_directory=None, check_consistency=False, device=None, variants: Dict[str, str | None] | None = None, non_conservative: bool | Literal['forces', 'stress'] = False, do_gradients_with_energy=True, uncertainty_threshold=0.1, neighbor_skin=2.0)[source]¶
Bases:
CalculatorThe
MetatomicCalculatorclass implements ASE’sase.calculators.calculator.CalculatorAPI using metatomic models to compute energy, forces and any other supported property.This class can be initialized with any
metatomic.torch.AtomisticModel, and used to run simulations using ASE’s MD facilities.Neighbor lists are computed using the vesin neighbor list library, either on CPU or GPU depending on the device of the model. If nvalchemiops is installed, full neighbor lists on GPU will be computed with it instead.
The calculator also supports the computation of additional properties beyond energy, forces and stress, as long as they are supported by the underlying model. You can either use the
additional_outputsargument to the constructor (the additional outputs will be stored in theadditional_outputsattribute), or callrun_model()directly with the desired outputs.The calculator can provide the following additional standard quantities as inputs to the model:
per-system: charge (read from
atoms.info["charge"], defaults to 0), spin_multiplicity (read fromatoms.info["spin_multiplicity"]if present,atoms.info["spin"]otherwise, defaults to 1);
As well as the following ASE-specific quantities:
per-atom:
ase::initial_magmoms,ase::initial_charges, andase::tags; corresponding to the outputs ofase.Atoms.get_initial_magnetic_moments(),ase.Atoms.get_initial_charges(), andase.Atoms.get_tags()respectively. Model can also request data fromase.Atoms.arrayswith thease::arrays::prefix (e.g.ase::arrays::foowill request thefooarray fromase.Atoms.arrays).per-system: Model can request data from
ase.Atoms.infowith thease::info::prefix (e.g.ase::info::foowill request thefooentry fromase.Atoms.info).
- Parameters:
model (str | bytes | PurePath | AtomisticModel) – model to use for the calculation. This can be a file path, a Python instance of
metatomic.torch.AtomisticModel, or the output oftorch.jit.script()onmetatomic.torch.AtomisticModel.additional_outputs (Dict[str, ModelOutput] | None) – Dictionary of additional outputs to be computed by the model. These outputs will always be computed whenever the
calculate()function is called (e.g. byase.Atoms.get_potential_energy(),ase.optimize.optimize.Dynamics.run(), etc.) and stored in theadditional_outputsattribute. If you want more control over when and how to compute specific outputs, you should userun_model()instead.extensions_directory – if the model uses extensions, we will try to load them from this directory
check_consistency – should we check the model for consistency when running, defaults to False.
device – torch device to use for the calculation. If
None, we will try the options in the model’ssupported_devicein order.variants (Dict[str, str | None] | None) – dictionary mapping output names to a variant that should be used for the calculations (e.g.
{"energy": "PBE"}). If"energy"is set to a variant also the uncertainty and non conservative outputs will be taken from this variant. This behaviour can be overriden by setting the corresponding keys explicitly toNoneor to another value (e.g.{"energy_uncertainty": "r2scan"}).non_conservative (bool | Literal['forces', 'stress']) –
controls which outputs are obtained directly from the model rather than via autograd on the energy. Accepted values are:
False(default): conservative mode; forces and stress are both derived from the gradient of the energy.True: both forces and stress are read directly from the model’snon_conservative_forcesandnon_conservative_stressoutputs."forces": forces come from the model’snon_conservative_forcesoutput; stress is still obtained via autograd."stress": stress comes from the model’snon_conservative_stressoutput; forces are still obtained via autograd.
Using any value other than
Falsecan afford a speed-up, potentially at the expense of physical correctness (especially in molecular dynamics simulations).do_gradients_with_energy – if
True, this calculator will always compute the energy gradients (forces and stress) when the energy is requested (e.g. throughatoms.get_potential_energy()). Because the results of a calculation are cached by ASE, this means future calls toatom.get_forces()will return immediately, without needing to execute the model again. If you are mainly interested in the energy, you can set this toFalseand enjoy a faster model. Forces will still be calculated if requested withatoms.get_forces().uncertainty_threshold – threshold for the atomic energy uncertainty in eV. This will only be used if the model supports atomic uncertainty estimation (https://pubs.acs.org/doi/full/10.1021/acs.jctc.3c00704). Set this to
Noneto disable uncertainty quantification even if the model supports it.neighbor_skin – skin distance in Å for the Verlet list in the neighbor list calculation. By setting this parameter, you are extending the cutoff radius of the neighbor list calculation by neighbor_skin Å. The extended cutoff helps reduce the frequency of neighbor list updates, because when the maximun atomic displacement is smaller than \((r_{skin} / 2)\), the neighbor list can still ensure that all neighbors within \((r_{cutoff})\) are included. The default value is 2.0 Å.
- additional_outputs: Dict[str, TensorMap]¶
Additional outputs computed by
calculate()are stored in this dictionary.The keys will match the keys of the
additional_outputsparameters to the constructor; and the values will be the corresponding rawmetatensor.torch.TensorMapproduced by the model.
- model() AtomisticModel[source]¶
Get the underlying model used by this calculator
- Return type:
- metadata() ModelMetadata[source]¶
Get the metadata of the underlying model
- Return type:
- run_model(atoms: Atoms | List[Atoms], outputs: Dict[str, ModelOutput], selected_atoms: Labels | None = None) Dict[str, TensorMap][source]¶
Run the model on the given
atoms, computing the requestedoutputsand only these.The output of the model is returned directly, and as such the blocks’
valueswill betorch.Tensor.This is intended as an easy way to run metatensor models on
ase.Atomswhen the model can compute outputs not supported by the standard ASE’s calculator interface.All the parameters have the same meaning as the corresponding ones in
metatomic.torch.ModelInterface.forward().
- check_state(atoms: Atoms, tol: float = 1e-15) List[str][source]¶
Detect system changes, including
atoms.infokeys used as model inputs.ASE’s default
check_state()only tracks per-atom arrays, cell and pbc, so mutations toatoms.info(e.g.atoms.info["spin"]) would otherwise return a stale cached result. Any watched info key that differs from the last calculation is appended to the change list, forcing a fresh run.
- calculate(atoms: Atoms, properties: List[str], system_changes: List[str]) None[source]¶
Compute some
propertieswith this calculator, and return them in the format expected by ASE.This is not intended to be called directly by users, but to be an implementation detail of
atoms.get_energy()and related functions. Seease.calculators.calculator.Calculator.calculate()for more information.
- compute_energy(atoms: Atoms | List[Atoms], compute_forces_and_stresses: bool = False, *, per_atom: bool = False) Dict[str, float | ndarray | List[float | ndarray]][source]¶
Compute the energy of the given
atoms.Energies are computed in eV, forces in eV/Å, and stresses in 3x3 tensor format and in units of eV/Å^3.
- Parameters:
atoms (Atoms | List[Atoms]) –
ase.Atoms, or list ofase.Atoms, on which to run the modelcompute_forces_and_stresses (bool) – if
True, the model will also compute forces and stresses. IMPORTANT: stresses will only be computed if all provided systems have periodic boundary conditions in all directions.per_atom (bool) – if
True, the per-atom energies will also be computed.
- Returns:
A dictionary with the computed properties. The dictionary will contain the
energyas a float. Ifcompute_forces_and_stressesis True, theforcesandstresswill also be included as numpy arrays. Ifper_atomis True, theenergieskey will also be present, containing the per-atom energies as a numpy array. In case of a list ofase.Atoms, the dictionary values will instead be lists of the corresponding properties, in the same format.- Return type: