ForceField¶
forcefield
¶
Clean, format-agnostic force field representation for Q2MM.
Decouples Q2MM's optimization from specific file formats (MM3 .fld, Tinker .prm, AMBER .frcmod). Parameters are identified by element pairs/triples, not format-specific atom type strings or line numbers.
FunctionalForm
¶
Bases: str, Enum
Physical functional form used by a force field.
Determines which energy expressions an engine should build:
HARMONIC: Standard harmonic bonds/angles, periodic torsions, Lennard-Jones 12-6 vdW. Used by AMBER, OPLS, GAFF, etc.MM3: Allinger's MM3 cubic bond stretch, sextic angle bend, buffered 14-7 vdW.
The enum is orthogonal to source_format (file format) — an MM3
force field can be loaded from .fld or .prm files, while a
HARMONIC force field comes from .frcmod or programmatic
construction.
BondParam
dataclass
¶
BondParam(elements: tuple[str, str], equilibrium: float, force_constant: float, label: str = '', env_id: str = '', ff_row: int | None = None)
A bond force field parameter.
Units (canonical): force_constant in kcal/(mol·Å²),
equilibrium in Å. Energy convention: E = k·(r − r₀)².
AngleParam
dataclass
¶
AngleParam(elements: tuple[str, str, str], equilibrium: float, force_constant: float, label: str = '', env_id: str = '', ff_row: int | None = None)
An angle force field parameter.
Units (canonical): force_constant in kcal/(mol·rad²),
equilibrium in degrees. Energy convention: E = k·(θ − θ₀)².
TorsionParam
dataclass
¶
TorsionParam(elements: tuple[str, str, str, str], periodicity: int = 1, force_constant: float = 0.0, phase: float = 0.0, label: str = '', env_id: str = '', ff_row: int | None = None)
A torsion/dihedral force field parameter.
Each object represents a single Fourier component (V_n). An MM3
torsion line with V1, V2, V3 produces three TorsionParam
objects with periodicity 1, 2, 3 respectively.
VdwParam
dataclass
¶
VdwParam(atom_type: str, radius: float, epsilon: float, element: str = '', reduction: float = 0.0, label: str = '', ff_row: int | None = None)
An atom-type van der Waals parameter.
__post_init__
¶
Normalize atom_type and auto-extract element if not provided.
ForceField
dataclass
¶
ForceField(name: str = 'Q2MM Force Field', bonds: list[BondParam] = list(), angles: list[AngleParam] = list(), torsions: list[TorsionParam] = list(), vdws: list[VdwParam] = list(), source_path: Path | None = None, source_format: Literal['mm3_fld', 'tinker_prm', 'openmm_xml', 'amber_frcmod'] | None = None, functional_form: FunctionalForm | None = None)
Format-agnostic force field representation.
Parameters are identified by element tuples, not format-specific atom types or line numbers. This eliminates matching bugs between different parsers.
Usage
ff = ForceField.from_mm3_fld("mm3.fld") ff = ForceField(bonds=[BondParam(('C', 'F'), 1.38, 5.0)])
Export to MM3 .fld is planned but not yet implemented.¶
n_params
property
¶
Number of adjustable scalar parameters in get_param_vector().
Layout: 2 per bond (k, r0) + 2 per angle (k, theta0) + 1 per torsion (k) + 2 per vdw (radius, epsilon).
get_bond
¶
get_bond(elem1: str, elem2: str, env_id: str = '') -> BondParam | None
Find bond parameter by element pair and optional environment ID.
Source code in q2mm/models/forcefield.py
get_bonds
¶
get_bonds(elem1: str, elem2: str) -> list[BondParam]
Find ALL bond parameters matching an element pair.
get_angle
¶
get_angle(elem1: str, elem_center: str, elem2: str, env_id: str = '') -> AngleParam | None
Find angle parameter by element triple and optional environment ID.
Source code in q2mm/models/forcefield.py
get_vdw
¶
get_vdw(atom_type: str = '', element: str = '') -> VdwParam | None
Find vdW parameter by atom type or element.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
atom_type
|
str
|
Exact atom type string to match. |
''
|
element
|
str
|
Element symbol to match (returns only if unique). |
''
|
Returns:
| Type | Description |
|---|---|
VdwParam | None
|
VdwParam | None: Matching parameter, or None if not found. |
Source code in q2mm/models/forcefield.py
get_torsion
¶
get_torsion(elem1: str, elem2: str, elem3: str, elem4: str, periodicity: int | None = None, env_id: str = '') -> TorsionParam | None
Find torsion parameter by element quad and optional periodicity/env_id.
Source code in q2mm/models/forcefield.py
get_param_vector
¶
Get all adjustable parameters as a flat vector.
Order: bond (k, r0), angle (k, theta0), torsion (k), vdw (radius, epsilon).
Source code in q2mm/models/forcefield.py
match_bond
¶
match_bond(elements: tuple[str, str], env_id: str = '', ff_row: int | None = None) -> BondParam | None
Match a bond parameter using ff_row, then env_id, then elements.
Source code in q2mm/models/forcefield.py
match_angle
¶
match_angle(elements: tuple[str, str, str], env_id: str = '', ff_row: int | None = None) -> AngleParam | None
Match an angle parameter using ff_row, then env_id, then elements.
Source code in q2mm/models/forcefield.py
match_vdw
¶
match_vdw(atom_type: str = '', element: str = '', ff_row: int | None = None) -> VdwParam | None
Match a vdW parameter using ff_row, then atom_type/element lookup (with fallback).
Source code in q2mm/models/forcefield.py
set_param_vector
¶
Set parameters from a flat vector (inverse of get_param_vector).
Source code in q2mm/models/forcefield.py
get_param_indices_by_type
¶
Map parameter type names to their indices in the param vector.
Returns a dict with keys bond_k, bond_eq, angle_k,
angle_eq, torsion_k, vdw_radius, vdw_epsilon and
values that are lists of integer indices into
:meth:get_param_vector.
Source code in q2mm/models/forcefield.py
get_param_type_labels
¶
Return the type label for each element of the param vector.
Same length as :meth:get_param_vector, useful for mapping each
scalar to its per-type step size or bounds category.
Source code in q2mm/models/forcefield.py
get_step_sizes
¶
Per-element differentiation step sizes for the param vector.
Uses the legacy STEPS dictionary values from
:mod:q2mm.optimizers.defaults, mapped via
:attr:_PARAM_TYPE_TO_STEP_KEY.
Returns¶
np.ndarray
Array of step sizes, same length as :meth:get_param_vector.
Source code in q2mm/models/forcefield.py
get_bounds
¶
Get (min, max) bounds for each element of the param vector.
Matches the layout of :meth:get_param_vector:
bond (k, r0), angle (k, theta0), torsion (k), vdw (radius, epsilon).
Parameters¶
overrides : dict, optional
Override default bounds per type. Keys: bond_k,
bond_eq, angle_k, angle_eq, torsion_k,
vdw_radius, vdw_epsilon.
Source code in q2mm/models/forcefield.py
copy
¶
copy() -> ForceField
from_mm3_fld
classmethod
¶
from_mm3_fld(path: str | Path) -> ForceField
from_tinker_prm
classmethod
¶
from_tinker_prm(path: str | Path) -> ForceField
Load bond and angle parameters from a Tinker .prm file.
to_mm3_fld
¶
to_mm3_fld(path: str | Path, template_path: str | Path | None = None, *, substructure_name: str = 'OPT Generated', smiles: str = 'AUTO') -> Path
Export to MM3 .fld format.
Source code in q2mm/models/forcefield.py
to_tinker_prm
¶
to_tinker_prm(path: str | Path, template_path: str | Path | None = None, *, section_name: str = 'OPT Generated') -> Path
Export to Tinker .prm format.
Source code in q2mm/models/forcefield.py
to_openmm_xml
¶
Export to OpenMM ForceField XML format.
Produces a standalone <ForceField> XML file loadable by
openmm.app.ForceField(path). Uses custom force definitions
with MM3 functional forms (cubic bond, sextic angle, buffered
14-7 vdW).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Output file path. |
required |
molecule
|
Q2MMMolecule | list[Q2MMMolecule] | None
|
Optional molecule(s) for generating
|
None
|
Returns:
| Type | Description |
|---|---|
Path
|
The resolved output path. |
Source code in q2mm/models/forcefield.py
from_amber_frcmod
classmethod
¶
from_amber_frcmod(path: str | Path) -> ForceField
to_amber_frcmod
¶
to_amber_frcmod(path: str | Path, template_path: str | Path | None = None, *, remark: str = 'Q2MM generated frcmod') -> Path
Export to AMBER .frcmod format.
Source code in q2mm/models/forcefield.py
create_for_molecule
classmethod
¶
create_for_molecule(molecule: Q2MMMolecule, default_bond_k: float = 5.0, default_angle_k: float = 0.5, name: str = '') -> ForceField
Create a force field with default parameters for a molecule.
Auto-detects unique bond and angle types from the molecule's geometry and creates parameters with sensible defaults.