xnn.ffnn.models.charmm.Charmm#
- class xnn.ffnn.models.charmm.Charmm(ffield, topology, *, cutoff=12.0, switch_width=0.0, charges=None, fudge_qq=None, coulomb_constant=None, trainable=(), keep_intermediates=False, auto_impropers=True, constrain_charge=True, use_fast='auto', atomic_energies=None)[source]#
Bases:
ClassicalFastPath,InteratomicPotentialThe CHARMM fixed-topology force field (MacKerell et al. 1998 form).
An instance binds a parameter library to one molecular topology; the bonded terms, impropers, exclusions and 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 (CharmmLibrary, CharmmForceField, str or Path) – The parameter library – a parsed
CharmmLibrary, a set shipped with xnn ("charmm22"), a.frcor native JSON path, or aCharmmForceFieldto 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; itsresiduesselect the impropers of the residue templates.cutoff (float, optional) – Nonbonded cutoff in Angstrom (the neighbor-list cutoff); by default 12.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_qq (float, optional) – Override the library’s 1,4 electrostatic scaling (1.0, CHARMM’s
e14fac).coulomb_constant (float, optional) – Override the library’s Coulomb constant (kcal Angstrom / mol / e^2; 332.0716 in the CHARMM program, 332.0636 in NAMD, 332.06371 in LAMMPS
realunits).trainable (sequence of str or "all", optional) – Force-field groups (see
CharmmForceField) 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, take them from the residue templates of the matched residues (
place_impropers()). DefaultTrue. Listed impropers are used as given, central atom first, resolved by their atom types (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.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. 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_urey_bradley,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 (central atom first).
- Parameters:
ffield (Union[CharmmLibrary, CharmmForceField, str, Path])
topology (Union[MolecularTopology, str, Path])
cutoff (float)
switch_width (float)
fudge_qq (Optional[float])
coulomb_constant (Optional[float])
keep_intermediates (bool)
auto_impropers (bool)
- classmethod from_atoms(structure, ffield='charmm22', *, charge=0, bonds=None, charges=None, orders=True, **kwargs)[source]#
Build a CHARMM model for a structure, typing it from the library.
Residue templates (the
#fragmentsof the parameter file) assign atom types, partial charges and impropers 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 (CharmmLibrary, CharmmForceField, str or Path, optional) – The parameter library (must carry templates); default
"charmm22".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’.
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 CHARMM 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 a
CharmmLibrary.The per-atom charges of this model are written into the residue templates of the library wherever the bound topology carries residue positions, so a refit of
chargeround-trips through the.frcfile.- Returns:
- Return type:
- classmethod from_config(cfg)[source]#
Construct a
Charmmmodel from a core model config.Core field:
cfg.cutoff. Fromcfg.extra:library(defaultcharmm22) and eithertopology(a topology JSON path) ortypes+bonds(+ optionalimpropers/improper_keys/charges/residues/residue_names) inline. Optional:switch_width,fudge_qq(CHARMM’se14fac),coulomb_constant,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.