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, InteratomicPotential

The 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 forward evaluates the energy on any conformation (or batch of conformations) of that system. Forces and stress come from autograd via ForceStressOutput.

Parameters:
  • ffield (CharmmLibrary, CharmmForceField, str or Path) – The parameter library – a parsed CharmmLibrary, a set shipped with xnn ("charmm22"), a .frc or native JSON path, or a CharmmForceField to share parameters with other models.

  • topology (MolecularTopology, str or Path) – The system’s topology (or a topology JSON path). Its charges are the partial charges unless charges is given; its residues select 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 real units).

  • 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. Default False.

  • auto_impropers (bool, optional) – When the topology lists no impropers, take them from the residue templates of the matched residues (place_impropers()). Default True. Listed impropers are used as given, central atom first, resolved by their atom types (or by improper_keys).

  • constrain_charge (bool or float, optional) – Hold the bound system’s total charge when charge trains: True (default) keeps the sum of the initial charges, a number fixes that value in e, False registers 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) and True take them on a CUDA device with Triton, in evaluation and in training; False evaluates 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 into atom_ref. All zero by default.

Notes

forward returns, besides node_energy / energy / node_features, the partial charges (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_coulomb14 and e_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:
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 #fragments of 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 unless charges is 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 bonds given, False skips 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:

Charmm

property charges: Tensor#

the charge parameters 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 charge round-trips through the .frc file.

Returns:

See CharmmForceField.export_library().

Return type:

CharmmLibrary

property masses: Tensor#

Per-atom masses (u) of the bound topology.

Type:

Tensor

classmethod from_config(cfg)[source]#

Construct a Charmm model from a core model config.

Core field: cfg.cutoff. From cfg.extra: library (default charmm22) and either topology (a topology JSON path) or types + bonds (+ optional impropers / improper_keys / charges / residues / residue_names) inline. Optional: switch_width, fudge_qq (CHARMM’s e14fac), coulomb_constant, trainable, constrain_charge and atomic_energies. Alternative spellings are translated by xnn.common.config.translate.

Parameters:

cfg (xnn.common.config.schema.ModelConfig) – The core model config.

Returns:

The model.

Return type:

Charmm

Raises:

ValueError – If the topology specification is missing.