openmm
openmm
¶
OpenMM molecular mechanics backend.
Provides a full-featured MM engine using OpenMM for energy, minimization,
Hessian, and frequency calculations. Supports both harmonic and MM3
functional forms with runtime parameter updates via :class:OpenMMHandle.
OpenMMHandle
dataclass
¶
OpenMMHandle(molecule: Q2MMMolecule, system: object, integrator: object, context: object, bond_force: object | None, angle_force: object | None, torsion_force: object | None, vdw_force: object | None, ub_force: object | None, cmap_force: object | None, bond_terms: list[_BondTerm], angle_terms: list[_AngleTerm], torsion_terms: list[_TorsionTerm], vdw_terms: list[_VdwTerm], ub_terms: list[_UBTerm] = list(), cmap_terms: list[_CmapTerm] = list(), exceptions_14: list[_Exception14] = list(), functional_form: FunctionalForm = MM3)
Reusable OpenMM system/context pair for fast parameter updates.
Attributes:
| Name | Type | Description |
|---|---|---|
molecule |
Q2MMMolecule
|
Deep copy of the input molecule. |
system |
object
|
The |
integrator |
object
|
The |
context |
object
|
The |
bond_force |
object | None
|
The OpenMM bond force object, or |
angle_force |
object | None
|
The OpenMM angle force object, or |
torsion_force |
object | None
|
The OpenMM torsion force object, or |
vdw_force |
object | None
|
The OpenMM vdW force object, or |
ub_force |
object | None
|
The OpenMM HarmonicBondForce for Urey-Bradley terms, or |
cmap_force |
object | None
|
The OpenMM CMAPTorsionForce, or |
bond_terms |
list[_BondTerm]
|
Mapping of molecule bonds to force indices. |
angle_terms |
list[_AngleTerm]
|
Mapping of molecule angles to force indices. |
torsion_terms |
list[_TorsionTerm]
|
Mapping of molecule torsions to force indices. |
vdw_terms |
list[_VdwTerm]
|
Mapping of atoms to vdW particle indices. |
ub_terms |
list[_UBTerm]
|
Mapping of Urey-Bradley 1-3 pairs to force indices. |
cmap_terms |
list[_CmapTerm]
|
Mapping of CMAP corrections to force indices. |
exceptions_14 |
list[_Exception14]
|
1-4 nonbonded exceptions (harmonic form only). |
functional_form |
FunctionalForm
|
The functional form used when the handle was created. |
OpenMMEngine
¶
Bases: MMEngine
Molecular mechanics backend powered by OpenMM.
Supports both harmonic (AMBER-style) and MM3 functional forms.
Provides reusable :class:OpenMMHandle objects for fast parameter
updates during optimization loops.
Initialize the OpenMM engine.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
platform_name
|
str | None
|
OpenMM platform to use (e.g. |
None
|
precision
|
str | None
|
Floating-point precision for GPU platforms
( |
None
|
Raises:
| Type | Description |
|---|---|
ImportError
|
If OpenMM is not installed. |
Source code in q2mm/backends/mm/openmm.py
name
property
¶
Human-readable engine name including the active platform.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
e.g. |
supported_functional_forms
¶
Functional forms this engine can evaluate.
Returns:
| Type | Description |
|---|---|
frozenset[str]
|
frozenset[str]: |
is_available
¶
Check if OpenMM is installed.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
|
deps_available
classmethod
¶
supports_runtime_params
¶
Whether parameters can be updated without rebuilding the system.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
Always |
supports_analytical_gradients
¶
Whether this engine provides analytical parameter gradients.
Both HARMONIC and MM3 functional forms use CustomBondForce,
CustomAngleForce, and CustomTorsionForce with global
parameters, so addEnergyParameterDerivative() provides exact
dE/d(param) for bond, angle, and torsion parameters.
vdW parameters use per-particle values and are supplemented
with central finite differences inside energy_and_param_grad,
so the returned gradient is always complete.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
Always |
Source code in q2mm/backends/mm/openmm.py
create_context
¶
create_context(structure: Q2MMMolecule | str | Path, forcefield: ForceField | None = None, *, precision: str | None = None) -> OpenMMHandle
Build an OpenMM system and context for a molecule.
Creates force objects (bond, angle, vdW) matching the force field's functional form and assigns per-term parameters from forcefield.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
structure
|
Q2MMMolecule | str | Path
|
A
:class: |
required |
forcefield
|
ForceField | None
|
Force field to apply. Auto-generated from the
molecule if |
None
|
precision
|
str | None
|
Override GPU precision ( |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
OpenMMHandle |
OpenMMHandle
|
Reusable handle for energy evaluation and parameter updates. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If an atom's element has no defined mass. |
ValueError
|
If no OpenMM terms could be created from the force field, or if a vdW parameter is missing for an atom. |
Source code in q2mm/backends/mm/openmm.py
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 | |
update_forcefield
¶
update_forcefield(handle: OpenMMHandle, forcefield: ForceField) -> None
Update per-term parameters in an existing OpenMM Context.
Modifies bond, angle, and vdW parameters in-place, then pushes changes to the OpenMM context. Much faster than rebuilding the system from scratch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
handle
|
OpenMMHandle
|
An existing :class: |
required |
forcefield
|
ForceField
|
New force field parameters to apply. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the force field's functional form does not match the handle's form, or if a required parameter is missing. |
Source code in q2mm/backends/mm/openmm.py
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 | |
export_system_xml
¶
export_system_xml(path: str | Path, structure: Q2MMMolecule | str | Path | OpenMMHandle, forcefield: ForceField | None = None) -> Path
Serialize the OpenMM System to XML.
Produces a topology-specific XML file containing the force objects
(HarmonicBondForce/CustomBondForce, etc. depending on the
functional form) with all per-term parameters. The file can be
loaded back with openmm.XmlSerializer.deserialize().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Output file path. |
required |
structure
|
Q2MMMolecule | str | Path | OpenMMHandle
|
A
:class: |
required |
forcefield
|
ForceField | None
|
Force field to apply. When structure is not an
:class: |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Path |
Path
|
The resolved output path. |
Source code in q2mm/backends/mm/openmm.py
load_system_xml
staticmethod
¶
Deserialize an OpenMM System from XML.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path to the XML file. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
object |
object
|
An |
Source code in q2mm/backends/mm/openmm.py
energy_and_param_grad
¶
energy_and_param_grad(structure: Q2MMMolecule, forcefield: ForceField) -> tuple[float, ndarray]
Compute energy and analytical gradient w.r.t. FF parameters.
Uses OpenMM's addEnergyParameterDerivative() on CustomForce
objects to get exact dE/d(param) for bond, angle, and torsion
parameters. vdW parameters use per-particle values that cannot
be differentiated via global parameters, so their gradients are
computed via central finite differences automatically.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
structure
|
Q2MMMolecule
|
Molecular structure. |
required |
forcefield
|
ForceField
|
Force field parameters. |
required |
Returns:
| Type | Description |
|---|---|
tuple[float, ndarray]
|
tuple[float, np.ndarray]: |
Source code in q2mm/backends/mm/openmm.py
1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 | |
energy
¶
energy(structure: Q2MMMolecule | str | Path | OpenMMHandle, forcefield: ForceField | None = None) -> float
Calculate MM energy in kcal/mol.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
structure
|
Q2MMMolecule | str | Path | OpenMMHandle
|
Molecule,
XYZ path, or :class: |
required |
forcefield
|
ForceField | None
|
Force field to apply. Auto-generated if |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Potential energy in kcal/mol. |
Source code in q2mm/backends/mm/openmm.py
minimize
¶
minimize(structure: Q2MMMolecule | str | Path | OpenMMHandle, forcefield: ForceField | None = None, tolerance: float = 1.0, max_iterations: int = 200) -> tuple
Energy-minimize structure using L-BFGS.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
structure
|
Q2MMMolecule | str | Path | OpenMMHandle
|
Molecule,
XYZ path, or :class: |
required |
forcefield
|
ForceField | None
|
Force field to apply. Auto-generated if |
None
|
tolerance
|
float
|
Energy convergence tolerance in kJ/mol. |
1.0
|
max_iterations
|
int
|
Maximum minimization steps. |
200
|
Returns:
| Type | Description |
|---|---|
tuple
|
tuple[float, list[str], np.ndarray]: |
Source code in q2mm/backends/mm/openmm.py
hessian
¶
hessian(structure: Q2MMMolecule | str | Path | OpenMMHandle, forcefield: ForceField | None = None, step: float = 0.0001) -> ndarray
Finite-difference Hessian in canonical units (Hartree/Bohr²).
Internally computed in kJ/mol/nm² (OpenMM native) and converted
to Hartree/Bohr² before returning, matching the canonical unit
contract defined in :class:~q2mm.backends.base.MMEngine.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
structure
|
Q2MMMolecule | str | Path | OpenMMHandle
|
Molecule,
XYZ path, or :class: |
required |
forcefield
|
ForceField | None
|
Force field to apply. Auto-generated if |
None
|
step
|
float
|
Finite-difference displacement in nm. |
0.0001
|
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Shape |
Source code in q2mm/backends/mm/openmm.py
frequencies
¶
frequencies(structure: Q2MMMolecule | str | Path | OpenMMHandle, forcefield: ForceField | None = None, **kwargs: Any) -> list[float]
Approximate harmonic frequencies in cm⁻¹ from the numerical Hessian.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
structure
|
Q2MMMolecule | str | Path | OpenMMHandle
|
Molecule,
XYZ path, or :class: |
required |
forcefield
|
ForceField | None
|
Force field to apply. Auto-generated if |
None
|
**kwargs
|
Any
|
Forwarded to
:func: |
{}
|
Returns:
| Type | Description |
|---|---|
list[float]
|
list[float]: Vibrational frequencies in cm⁻¹. |
Source code in q2mm/backends/mm/openmm.py
detect_best_platform
¶
Return the name of the fastest available OpenMM platform.
If the OPENMM_DEFAULT_PLATFORM environment variable is set, its
value is returned directly (no validation against installed
platforms). This allows test harnesses to force CPU-only execution.
Otherwise, platform preference order: CUDA > OpenCL > CPU > Reference.
Logs a warning when CUDA is unavailable and the function falls back to OpenCL on a system with an NVIDIA GPU — OpenCL on modern NVIDIA GPUs gives very poor utilisation (~14%). The warning is suppressed on non-NVIDIA systems where OpenCL may be the intended backend.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Name of the best available platform. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If OpenMM is not installed. |