xnn.ffnn.models.amber.Amber#
- class xnn.ffnn.models.amber.Amber(ffield, topology, *, cutoff=10.0, switch_width=0.0, charges=None, fudge_lj=None, fudge_qq=None, trainable=(), keep_intermediates=False, auto_impropers=True, constrain_charge=True, use_fast='auto', atomic_energies=None)[source]#
Bases:
ClassicalFastPath,InteratomicPotentialThe AMBER fixed-topology force field (Cornell et al. 1995 form).
An instance binds a parameter library to one molecular topology; the bonded terms, impropers, exclusions and scaled 1,4 pairs are resolved once, and
forwardevaluates the energy on any conformation (or batch of conformations) of that system. Forces and stress come from autograd viaForceStressOutput.- Parameters:
ffield (AmberLibrary, AmberForceField, str or Path) – The parameter library – a parsed
AmberLibrary, a set shipped with xnn ("ff94"), a.frcor native JSON path, or anAmberForceFieldto share parameters with other models.topology (MolecularTopology, str or Path) – The system’s topology (or a topology JSON path). Its
chargesare the partial charges unlesschargesis given; itsresiduesorder the impropers (place_impropers()).cutoff (float, optional) – Nonbonded cutoff in Angstrom (the neighbor-list cutoff); by default 10.0. 1,4 pairs are independent of it.
switch_width (float, optional) – Width of a quintic switching function taking the nonbonded interactions smoothly to zero at the cutoff; 0.0 (plain truncation) by default.
charges (sequence of float, optional) – Per-atom partial charges in e, overriding the topology’s.
fudge_lj (float, optional) – Override the library’s 1,4 scaling factors (1/2 and 1/1.2).
fudge_qq (float, optional) – Override the library’s 1,4 scaling factors (1/2 and 1/1.2).
trainable (sequence of str or "all", optional) – Force-field groups (see
AmberForceField) plus the model groups"charge"(per-atom charges) and"atom_ref"(per-element reference energies);"all"unfreezes everything.keep_intermediates (bool, optional) – Stash the intermediate tensors of the last evaluation in
self.intermediates. DefaultFalse.auto_impropers (bool, optional) – When the topology lists no impropers, place them by the AMBER rules (one per three-connected atom with a matching pattern). Default
True. Listed impropers are used as given, resolved by their atom classes in the listed order (or byimproper_keys).constrain_charge (bool or float, optional) – Hold the bound system’s total charge when
chargetrains:True(default) keeps the sum of the initial charges, a number fixes that value in e,Falseregisters no constraint. The charges the model evaluates are the per-atom parameters shifted uniformly onto the constraint.use_fast (bool or "auto", optional) – Evaluate the terms with the fused GPU kernels of
classical_kernels(the same numbers to round-off)."auto"(default) andTruetake them on a CUDA device with Triton, in evaluation and in training;Falseevaluates every term eagerly. The kernels are differentiable twice (force training, Hessians), and once with a cell gradient (stress); training with a stress loss evaluates every term eagerly.atomic_energies (mapping, optional) – Per-element reference energies in eV,
{Z or symbol: E0}, loaded intoatom_ref; the offset between the labels’ zero and the force field’s. All zero by default.
Notes
forwardreturns, besidesnode_energy/energy/node_features, the partialcharges(N,)and one per-structure tensor per term:e_bond,e_angle,e_torsion,e_improper,e_vdw,e_coulomb,e_vdw14,e_coulomb14ande_ref. Every structure in a batch must have this topology’s atom count and element sequence.- Variables:
atom_ref (torch.nn.Embedding) – The per-element reference energies, row
Z.species (list of int) – The atomic numbers of the bound topology.
impropers (list of tuple) – The improper quadruples in evaluation order (center third).
- Parameters:
ffield (Union[AmberLibrary, AmberForceField, str, Path])
topology (Union[MolecularTopology, str, Path])
cutoff (float)
switch_width (float)
fudge_lj (Optional[float])
fudge_qq (Optional[float])
keep_intermediates (bool)
auto_impropers (bool)
- classmethod from_atoms(structure, ffield='ff94', *, charge=0, bonds=None, charges=None, orders=True, **kwargs)[source]#
Build an AMBER model for a structure, typing it from the library.
Residue templates (the
#fragmentsof the parameter file) assign atom types and partial charges to every atom they match; atoms outside any residue are typed by the per-type SMARTS templates and carry zero charge unlesschargesis given.- Parameters:
structure (object) – An
ase.Atoms, a(positions, atomic_numbers)pair, a dict with"pos"/"atomic_numbers", an RDKit molecule or a SMILES string.ffield (AmberLibrary, AmberForceField, str or Path, optional) – The parameter library (must carry templates); default
"ff94".charge (int, optional) – Total charge of the structure (for bond-order perception).
bonds (sequence of (int, int), optional) – Known connectivity; bond orders are then perceived, not bonds.
charges (sequence of float, optional) – Per-atom partial charges overriding the templates’ (e.g. RESP charges of a small molecule).
orders (bool, optional) – With
bondsgiven,Falseskips the perception of bond orders and formal charges (the residue templates need only the connectivity), which is robust for charged systems and ions.**kwargs – Forwarded to the constructor (
cutoff,trainable, …).
- Returns:
The model, bound to the derived topology.
- Return type:
- property charges: Tensor#
the
chargeparameters shifted uniformly so that they sum to the constrained total (the parameters themselves without a constraint).- Type:
Tensor
- Type:
The per-atom charges the model evaluates
- forward(data)[source]#
Evaluate the AMBER energy on a (batched) atomic graph.
- Parameters:
data (AtomicGraph) – The batched graph; every structure must be a conformation of the bound topology, with the neighbor list built with this model’s
cutoff.- Returns:
node_energy(N,),energy(B,),charges(N,),node_features(N, 1)and the per-structure energy decomposition (see the class docstring).- Return type:
dict of str to Tensor
- Raises:
ValueError – If a structure in the batch does not match the bound topology.
- export_library()[source]#
Export the current parameters as an
AmberLibrary.The per-atom charges of this model are written into the residue templates of the library wherever the bound topology carries residue positions (every atom of a matched residue), so a refit of
chargeround-trips through the.frcfile.- Returns:
- Return type:
- classmethod from_config(cfg)[source]#
Construct an
Ambermodel from a core model config.Core field:
cfg.cutoff. Fromcfg.extra:library(defaultff94) and eithertopology(a topology JSON path) ortypes+bonds(+ optionalimpropers/improper_keys/charges/residues) inline. Optional:switch_width,fudge_lj,fudge_qq(or the AMBER divisorsscnb/scee),trainable,constrain_chargeandatomic_energies. Alternative spellings are translated byxnn.common.config.translate.- Parameters:
cfg (xnn.common.config.schema.ModelConfig) – The core model config.
- Returns:
The model.
- Return type:
- Raises:
ValueError – If the topology specification is missing.