xnn.common.deploy.ase_calculator.XNNCalculator#
- class xnn.common.deploy.ase_calculator.XNNCalculator(model, cutoff, device='cpu', eeq_reuse=False, **kwargs)[source]#
Bases:
CalculatorASE Calculator wrapping a trained xnn model.
This is an ase.calculators.calculator.Calculator subclass that lets a trained model drive standard ASE workflows (energy, force and stress evaluation, dynamics, optimization, etc.). It converts an
Atomsobject into the model’s graph representation, runs the model and stores the results in the ASE-expected units and layout.Requires
aseto be installed (optional extra:pip install "xnn[ase]"); constructing an instance raisesImportErrorif ASE is not available.- Parameters:
model (torch.nn.Module) – A trained model. It is moved to
deviceand put inevalmode. Its forward is expected to accept anAtomicGraphand return a dict with an"energy"key and optionally"forces"and"stress".cutoff (float) – Neighbor-list cutoff radius (in ASE length units) used when building the graph from an
Atomsobject.device (str, optional) – Torch device the model runs on. Defaults to
"cpu".**kwargs – Additional keyword arguments forwarded to the base
Calculator.eeq_reuse (bool)
- Variables:
implemented_properties (list of str) – Properties this calculator can produce:
"energy","forces"and"stress".model (torch.nn.Module) – The wrapped model (on
device, in eval mode).cutoff (float) – The neighbor-list cutoff radius.
device (str) – The torch device string.
dtype (torch.dtype) – Floating-point dtype of the model; graph tensors are built in it.
eeq_reuse (bool, optional) – Carry the large-regime EEQ solve of a D4 term from one step to the next (
enable_eeq_reuse()), for molecular dynamics and geometry optimization; results agree with the fresh solve to the solver tolerance. DefaultFalse.
- Raises:
ImportError – If ASE is not installed.
- implemented_properties: list[str] = ['energy', 'forces', 'stress', 'charges', 'dipole']#
Properties calculator can handle (energy, forces, …)
- classmethod from_pretrained(source, device='cpu', eeq_reuse=False, calculator_kwargs=None, **kwargs)[source]#
Build a calculator for a pre-trained model.
Loads
sourcewithload_pretrained()(a registered name such as"mace-off23-small", a local checkpoint or model directory, a URL or a Zenodo DOI) with the stress head on, and takes the neighbor-list cutoff from the loaded model:atoms.calc = XNNCalculator.from_pretrained("mace-mp-0-medium")
- Parameters:
source (str or pathlib.Path) – The model to load.
device (str, optional) – Torch device. Defaults to
"cpu".eeq_reuse (bool, optional) – As for the constructor.
calculator_kwargs (dict, optional) – Keyword arguments for the base ASE
Calculator.**kwargs – Options of
load_pretrained()(cache_dir,head,filename,dtype,dispersion,local_files_only,use_fast, …).
- Returns:
The calculator.
- Return type:
- check_state(atoms, tol=1e-15)[source]#
ASE’s change detection, plus the net charge and spin multiplicity.
ASE compares positions, numbers, cell and periodicity only, so the same geometry with another
atoms.info["charge"]would otherwise be served the cached result.
- calculate(atoms=None, properties=('energy',), system_changes=['positions', 'numbers', 'cell', 'pbc', 'initial_charges', 'initial_magmoms'])[source]#
Compute requested properties for an
Atomsobject.Builds an
AtomicGraphfrom the atoms’ positions, atomic numbers, cell and periodic-boundary flags (the cell is passed only when any PBC direction is active), runs the model, and populatesself.results. The total energy is the sum of the model’s per-atom (node) energies. Forces, when produced, are stored as an(n_atoms, 3)NumPy array. Stress, when produced and the system is periodic, is converted from the model’s3 x 3tensor to ASE’s Voigt 6-vector ordering[xx, yy, zz, yz, xz, xy]. A model that predicts partial charges (PhysNet, BAMBOO, AIMNet2) also fills"charges"and"dipole"(in the model’s units); so does an LES model from itsles_chargesand, for molecules,les_dipole(e and e Angstrom). The net charge and spin multiplicity of the structure are read fromatoms.info(keys"charge"/"total_charge"and"spin_multiplicity"/"multiplicity"), asatoms_to_structure()does.- Parameters:
atoms (ase.Atoms or None, optional) – The atomic structure to evaluate. Passed through to the base
Calculator.properties (sequence of str, optional) – Names of the properties to compute. Defaults to
("energy",).system_changes (list of str, optional) – Which aspects of the system have changed since the last call. Defaults to ASE’s
all_changes.
- Returns:
Results are written into
self.results.- Return type:
None