xnn.ffnn.models.amoeba.Amoeba#

class xnn.ffnn.models.amoeba.Amoeba(ffield, topology, *, cutoff=10.0, trainable=(), polarization=None, keep_intermediates=False, atomic_energies=None, ewald=None, ewald_alpha=None, pewald_alpha=None, dewald_alpha=None, ewald_cutoff=None, pme_grid=None, pme_order=5, ppme_order=None, dpme_grid=None, dpme_order=4, vdw_cutoff=None, vdw_taper=None, taper=None, repulsion_cutoff=None, chgtrn_cutoff=None, dispersion_cutoff=None, polar_eps=1e-08, preconditioner='diagonal', precond_cutoff=4.5, mesh_self=False, solver='auto', max_iter=100, predictor=None, predictor_history=6, conserve_charge=True, symmetry_mask=True, use_fast='auto', mixed_precision=False)[source]#

Bases: InteratomicPotential

The AMOEBA polarizable multipole force field.

An instance binds a parameter library to one molecular topology; the valence terms, multipole frames, polarization groups and nonbonded scale factors are resolved once, and forward evaluates the energy on any conformation (or batch of conformations) of that system. Forces come from autograd via ForceStressOutput.

Parameters:
  • ffield (AmoebaLibrary, AmoebaForceField, str or Path) – The parameter library: a parsed AmoebaLibrary, a set shipped with xnn ("amoeba-water-2003", "amoebapro13"), a .frc / Tinker .prm / JSON path, or an AmoebaForceField to share parameters with other models.

  • topology (MolecularTopology, str or Path) – The system’s topology (or a topology JSON path).

  • cutoff (float, optional) – Neighbor-list cutoff in Angstrom, by default 10.0. For clusters use a value that encloses the whole system (every pair interacts in full); for cells it bounds the real-space cutoffs below.

  • trainable (sequence of str or "all", optional) – Force-field groups (see AmoebaForceField) plus the model group "atom_ref"; "all" unfreezes everything.

  • polarization (str, optional) – "mutual" solves the induced dipoles self-consistently, "direct" uses the permanent field only and "none" switches the induction off; by default the library’s polarization setting (MUTUAL unless the file says DIRECT).

  • keep_intermediates (bool, optional) – Stash the intermediate tensors of the last evaluation in self.intermediates.

  • atomic_energies (mapping, optional) – Per-element reference energies in eV loaded into atom_ref.

  • ewald (bool, optional) – Ewald sums for the electrostatics of periodic cells: None (default) switches them on for every structure that has a cell, False truncates at the cutoff instead (minimum image). A slab (two periodic axes) is summed on the full cell, which must hold a vacuum gap along the free axis, with the Yeh-Berkowitz correction.

  • ewald_alpha (float, optional) – Ewald coefficients (1/Angstrom) of the permanent multipoles, the induction and HIPPO’s dispersion; the defaults make the screened interaction at the real-space cutoff 1e-8, the induction one equal to the multipole one.

  • pewald_alpha (float, optional) – Ewald coefficients (1/Angstrom) of the permanent multipoles, the induction and HIPPO’s dispersion; the defaults make the screened interaction at the real-space cutoff 1e-8, the induction one equal to the multipole one.

  • dewald_alpha (float, optional) – Ewald coefficients (1/Angstrom) of the permanent multipoles, the induction and HIPPO’s dispersion; the defaults make the screened interaction at the real-space cutoff 1e-8, the induction one equal to the multipole one.

  • ewald_cutoff (float, optional) – Real-space cutoff of the Ewald sums, by default the smaller of 7 Angstrom and cutoff.

  • pme_grid (sequence of int, optional) – Mesh dimensions of the electrostatic and dispersion particle-mesh sums, by default 1.2 (0.8) points per Angstrom. As in Tinker, every size is rounded up to an even one with prime factors 2, 3 and 5, at least 16 (a requested 27 becomes 30).

  • dpme_grid (sequence of int, optional) – Mesh dimensions of the electrostatic and dispersion particle-mesh sums, by default 1.2 (0.8) points per Angstrom. As in Tinker, every size is rounded up to an even one with prime factors 2, 3 and 5, at least 16 (a requested 27 becomes 30).

  • pme_order (int, optional) – B-spline orders of the multipole, induction and dispersion meshes (5, the multipole one, 4).

  • ppme_order (int, optional) – B-spline orders of the multipole, induction and dispersion meshes (5, the multipole one, 4).

  • dpme_order (int, optional) – B-spline orders of the multipole, induction and dispersion meshes (5, the multipole one, 4).

  • vdw_cutoff (float, optional) – Cutoffs of the switched short-range terms, by default cutoff (the dispersion one is the Ewald real-space cutoff when its sum is on). The van der Waals term acts between the reduced sites, so a pair of sites within vdw_cutoff can belong to atoms just beyond it: with cutoff above vdw_cutoff by twice the largest site displacement (about 0.2 Angstrom for hydrogens reduced to 0.9 of their bond), the neighbor list holds every such pair, as Tinker’s list of the sites does.

  • repulsion_cutoff (float, optional) – Cutoffs of the switched short-range terms, by default cutoff (the dispersion one is the Ewald real-space cutoff when its sum is on). The van der Waals term acts between the reduced sites, so a pair of sites within vdw_cutoff can belong to atoms just beyond it: with cutoff above vdw_cutoff by twice the largest site displacement (about 0.2 Angstrom for hydrogens reduced to 0.9 of their bond), the neighbor list holds every such pair, as Tinker’s list of the sites does.

  • chgtrn_cutoff (float, optional) – Cutoffs of the switched short-range terms, by default cutoff (the dispersion one is the Ewald real-space cutoff when its sum is on). The van der Waals term acts between the reduced sites, so a pair of sites within vdw_cutoff can belong to atoms just beyond it: with cutoff above vdw_cutoff by twice the largest site displacement (about 0.2 Angstrom for hydrogens reduced to 0.9 of their bond), the neighbor list holds every such pair, as Tinker’s list of the sites does.

  • dispersion_cutoff (float, optional) – Cutoffs of the switched short-range terms, by default cutoff (the dispersion one is the Ewald real-space cutoff when its sum is on). The van der Waals term acts between the reduced sites, so a pair of sites within vdw_cutoff can belong to atoms just beyond it: with cutoff above vdw_cutoff by twice the largest site displacement (about 0.2 Angstrom for hydrogens reduced to 0.9 of their bond), the neighbor list holds every such pair, as Tinker’s list of the sites does.

  • vdw_taper (float, optional) – Start of the C2 switch of the van der Waals term, and of every switched term, in Angstrom; by default 0.9 of each cutoff for cells and no switching for molecules.

  • taper (float, optional) – Start of the C2 switch of the van der Waals term, and of every switched term, in Angstrom; by default 0.9 of each cutoff for cells and no switching for molecules.

  • mesh_self (bool, optional) – Remove the spurious, position-averaged mesh self-interaction of every multipole from the particle-mesh energy, from the field that polarizes it and from the dispersion sum. Off by default, as in Tinker. On, the mean discretisation error of the self-interaction leaves the energies and the stress.

  • polar_eps (float, optional) – Convergence of the iterative induced-dipole solve, the rms change per iteration in Debye (1e-8). The adjoint solve of the forces converges the dipoles of the polar field to the same tolerance.

  • preconditioner (str, optional) – Preconditioner of the iterative solve: "diagonal" (default, the polarizabilities), "block" (the exact inverse of every molecule’s real-space block, polarization groups for molecules of more than 32 atoms), "neumann" (the diagonal plus a first-order correction from the pairs within precond_cutoff) or "block-neumann" (both). The dipoles are converged to polar_eps either way; only the number of iterations changes.

  • precond_cutoff (float, optional) – Pair distance of the Neumann correction, in Angstrom (4.5).

  • solver (str, optional) – "auto" (dense for molecules up to a thousand atoms, iterative otherwise), "dense" or "iterative".

  • max_iter (int, optional) – Iteration limit of the solve (100).

  • predictor (str, optional) – Predict the starting dipoles of the iterative solve from the converged dipoles of the last evaluations (DipolePredictor: "aspc", "gear" or "previous"), for molecular dynamics where each structure follows the last. Used in evaluation mode only; by default None (the MDI engine turns ASPC on). The result agrees with a cold start to polar_eps.

  • predictor_history (int, optional) – Number of past dipole sets the predictor uses (6).

  • conserve_charge (bool, optional) – Hold the total charge of every molecule at its library value while charge trains (a minimum-norm projection of the row charges, registered on the force field), by default True.

  • symmetry_mask (bool, optional) – Keep the dipole and quadrupole components that the library leaves zero at zero while they train, by default True. The quadrupoles stay symmetric and traceless in any case, and export_library writes the multipoles the model uses.

  • mixed_precision (bool, optional) – How the fast implementation evaluates the radial ladders of the pair terms in a float32 model: False (default) in float64, cast afterwards, at the round-off of the float32 reference; True in float32 throughout, as Tinker9’s mixed precision does, faster, with the energies at the float32 round-off and the forces up to one digit off. No effect on a float64 model.

  • use_fast (bool or str, optional) – The fast path: False runs the reference implementation, the eager composition of the equations (any device, second derivatives); True runs the fast implementation, which computes the same numbers to round-off with the same derivatives: the fused GPU kernels of the pair terms, the fields and the induction’s products (amoeba_kernels) and the mesh spreading and interpolation kernels. The pair kernels are differentiable twice (their generated tangents and parameter adjoints, amoeba_tangents), so force losses and parameter gradients train through them; in training the mesh and the solver’s products run eager. In evaluation mode the mesh runs on kernels too, and a second derivative (a Hessian) takes the gradient of their backward pass through the eager stencils. When forces follow, AMOEBA’s fast implementation solves the dipoles of the direct and the polar field together, one set of iterations for both (Tinker’s scheme), so its dipoles and forces agree with the reference’s to the solver tolerance. On a CPU with a C++ compiler the same kernels run as C++ loops (amoeba_cpu). "auto" (default) takes the fast implementation above the measured crossover of the device and the mode (evaluation or training) and the reference otherwise.

  • mixed_precision – How the fast implementation of HIPPO evaluates the radial ladders of the pair terms in a float32 model: False (default) in float64, cast afterwards, which keeps the float32 numbers at the round-off of the float32 reference; True in float32 throughout, as Tinker9’s mixed precision does, which is faster, with the energies at the float32 round-off and the forces up to one digit off. No effect on a float64 model or on AMOEBA.

Notes

forward returns node_energy / energy / node_features, the per-atom charges (monopoles, charge flux included), the induced_dipoles (N, 3) in e Angstrom, the molecular dipole (B, 3) in e Angstrom, and one per-structure tensor per term: e_bond, e_angle, e_strbnd, e_urey, e_opbend, e_torsion, e_pitors, e_tortor, e_vdw, e_multipole, e_polar (and for HIPPO e_repulsion, e_dispersion, e_chgtrn instead of e_vdw), and e_ref.

classmethod from_atoms(structure, ffield='amoeba-water-2003', *, charge=0, bonds=None, orders=True, **kwargs)[source]#

Build a model for a structure, typing it from the library’s templates.

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 (AmoebaLibrary, AmoebaForceField, str or Path, optional) – The parameter library (must carry templates or fragments).

  • charge (int, optional) – Total charge (for bond-order perception).

  • bonds (sequence of (int, int), optional) – Known connectivity.

  • orders (bool, optional) – With bonds given, False skips bond-order perception.

  • **kwargs – Forwarded to the constructor.

Returns:

The model bound to the derived topology.

Return type:

Amoeba or Hippo

classmethod from_tinker_xyz(path, ffield, type_map=None, **kwargs)[source]#

Build a model from a Tinker .xyz file (types and connectivity).

Parameters:
  • path (str or Path) – The coordinate file.

  • ffield (AmoebaLibrary, AmoebaForceField, str or Path) – The parameter library; the file’s integer types must be the library’s type names, or be translated by type_map.

  • type_map (dict, optional) – {integer type: type name}.

  • **kwargs – Forwarded to the constructor.

Returns:

(model, structure) with structure a dict of pos, atomic_numbers (and cell when the file has one) ready for structure_to_graph().

Return type:

tuple

plan_pairs(data, plan)[source]#

Refresh the pair list of plan from the neighbor list of data in place, keeping the rest of the plan: a cheap rebuild while the atoms move continuously (an atom wrapped back into the cell changes the image shifts of its bonded vectors too and needs plan_step()).

Parameters:
  • data (AtomicGraph) – The batched graph at the current positions, its neighbor list built with cutoff + skin.

  • plan (StepPlan) – The plan to refresh.

Returns:

Whether the pairs fitted the plan’s capacity and were copied; the plan is unchanged otherwise.

Return type:

bool

refresh_pairs(data, plan, cutoff)[source]#

Refresh the pair list of plan in place at the positions and cell of data, one structure, from a half neighbor list of cutoff (the model’s cutoff plus the skin), without building a graph: the same pairs as plan_pairs(), found once each on the device.

Parameters:
  • data (AtomicGraph) – The plan’s graph, its positions and cell at the current step.

  • plan (StepPlan) – The plan to refresh.

  • cutoff (float) – The radius of the pair list.

Returns:

Whether the pairs fitted the plan’s capacity and were copied; the plan is unchanged otherwise.

Return type:

bool

stage_pairs(data, plan, pairs, single_image=False)[source]#

The unique pairs (i, k, shift) of data, one structure, sorted by i (unique_pairs()), recorded as the pair list of a new plan for plan.update(fresh, bonded=False). plan is not touched, so a step can still run on it meanwhile.

Parameters:
  • data (AtomicGraph) – The plan’s graph, its positions and cell where the pairs were found.

  • plan (StepPlan) – The plan whose capacity and skin the new one takes.

  • pairs (tuple of Tensor) – The pairs and their integer image shifts, or those in buffers of the plan’s capacity followed by their number on the device.

  • single_image (bool, optional) – Every pair has one image within the list’s cutoff (a cell more than twice the cutoff high on every axis), so a bonded pair’s scale factors need no minimum-image test.

Returns:

The new pair list, or None when the pairs exceed the capacity.

Return type:

StepPlan or None

forward(data, plan=None)[source]#

Evaluate the 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.

  • plan (StepPlan, optional) – The host decisions of this structure, from plan_step(): the evaluation reads them instead of deciding them again, and takes its pairs from the plan instead of the neighbor list. Its terms select the terms evaluated (all by default).

Returns:

See the class notes.

Return type:

dict of str to Tensor

Raises:

ValueError – If a structure in the batch does not match the bound topology.

forms_plan_forces(data, plan)[source]#

Whether an evaluation on plan forms its forces and strain derivative itself (forward_plan_direct()): an inner step (terms="valence") of one periodic structure on the valence kernels of a GPU.

Parameters:
Returns:

Whether forward_plan_direct() serves the evaluation.

Return type:

bool

forward_plan_direct(data, plan)[source]#

The valence terms of an inner step (see forms_plan_forces()) with their forces and strain derivative, formed directly: the kernels’ gradient with respect to the bonded vectors is scattered to the atoms, and the strain derivative is the sum of every vector times its gradient (a bonded vector strains with the cell and the positions).

Parameters:
Returns:

energy, node_energy, forces, virial (the strain derivative (1, 3, 3)) and the valence terms’ totals.

Return type:

dict of str to Tensor

plan_step(data, skin=0.0, capacity=None)[source]#

Record the host decisions of an evaluation of data for later evaluations of the same structure as it moves (see StepPlan); the recording evaluation itself runs without gradients and leaves the dipole predictor alone.

Parameters:
  • data (AtomicGraph) – The batched graph, its neighbor list built with cutoff + skin.

  • skin (float, optional) – The margin of the neighbor list beyond the model’s cutoff in Angstrom, by default 0 (the plan then holds at these positions only).

  • capacity (int, optional) – The number of pairs the plan holds, padded beyond the unique pairs of the neighbor list; by default exactly those.

Returns:

The plan, for forward(data, plan).

Return type:

StepPlan

Raises:

ValueError – If the neighbor list has more unique pairs than capacity.

set_use_fast(use_fast)[source]#

Set when the fast implementation runs (see use_fast of the class).

Parameters:

use_fast (bool or str) – True, False or "auto".

Returns:

The model.

Return type:

Amoeba

rewind_predictors()[source]#

Take the dipoles of the last evaluation back out of the predictors that stored them, before the step is evaluated again (see DipolePredictor.rewind()).

Return type:

None

refresh_cell(plan, cells, changed=False)[source]#

Bring the cell terms the step plan plan keeps across evaluations (the mesh influence functions, inverse cells and volumes of every mesh group) to the cells cells (B, 3, 3), in place, so that a recorded step follows a changed cell. Reads the cells on the host, unless the caller found them changed.

Parameters:
  • plan (StepPlan) – The plan.

  • cells (Tensor) – The lattice vectors of the batch as rows.

  • changed (bool, optional) – Remake every group’s terms without comparing its cells, by default False.

Returns:

Whether any kept term described other cells.

Return type:

bool

set_dipole_predictor(kind, history=6)[source]#

Switch the induced-dipole predictor on ("aspc", "gear", "previous") or off (None or "none"); see DipolePredictor.

Parameters:
  • kind (str or None) – The extrapolation scheme.

  • history (int, optional) – Number of past dipole sets it uses (6).

Return type:

None

molecular_polarizability(data)[source]#

Static dipole polarizability of every structure from the induction model.

A uniform field is applied to every polarizable site, the induced dipoles are solved with the mutual-induction matrix of the structure (Thole or density damping included) and summed, column by column, into the (B, 3, 3) tensor in Angstrom^3. Permanent multipoles and the polarization groups play no role: the induced dipoles see every other induced dipole as in a mutual induction run.

Parameters:

data (AtomicGraph) – The (batched) graph of conformations of the bound topology.

Returns:

Polarizability tensors (B, 3, 3).

Return type:

Tensor

export_library()[source]#

Export the current parameters as an AmoebaLibrary.

Return type:

AmoebaLibrary

property masses: Tensor#

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

Type:

Tensor

property polarization_groups: list[int]#

The polarization group index of every atom.

Type:

list of int

classmethod from_config(cfg)[source]#

Construct the model from a core model config.

Core field: cfg.cutoff. From cfg.extra: library and either topology (a topology JSON path), tinker_xyz (a Tinker coordinate file) or types + bonds inline; optional polarization, trainable, atomic_energies.

Parameters:

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

Returns:

The model.

Return type:

Amoeba