Coverage for cvpack/rmsd.py: 100%

23 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-10-21 18:13 +0000

1""" 

2.. class:: RMSD 

3 :platform: Linux, MacOS, Windows 

4 :synopsis: Root-mean-square deviation of a group of atoms 

5 

6.. classauthor:: Charlles Abreu <craabreu@gmail.com> 

7 

8""" 

9 

10from __future__ import annotations 

11 

12import typing as t 

13 

14import openmm 

15from openmm import unit as mmunit 

16 

17from .base_rmsd import BaseRMSD 

18from .units import MatrixQuantity, VectorQuantity 

19 

20 

21class RMSD(BaseRMSD, openmm.RMSDForce): 

22 r""" 

23 The root-mean-square deviation (RMSD) between the current and reference 

24 coordinates of a group of `n` atoms: 

25 

26 .. math:: 

27 

28 d_{\rm rms}({\bf r}) = \sqrt{ 

29 \frac{1}{n} \min_{ 

30 \bf q \in \mathbb{R}^4 \atop \|{\bf q}\| = 1 

31 } \sum_{i=1}^n \left\| 

32 {\bf A}({\bf q}) \hat{\bf r}_i - \hat{\bf r}_i^{\rm ref} 

33 \right\|^2 

34 } 

35 

36 where :math:`\hat{\bf r}_i` is the position of the :math:`i`-th atom in the group 

37 relative to the group's center of geometry (centroid), the superscript 

38 :math:`\rm ref` denotes the reference structure, :math:`{\bf q}` is a unit 

39 quaternion, and :math:`{\bf A}({\bf q})` is the rotation matrix corresponding to 

40 :math:`{\bf q}`. 

41 

42 .. warning:: 

43 

44 Periodic boundary conditions are `not supported 

45 <https://github.com/openmm/openmm/issues/2913>`_. This is not a problem if all 

46 atoms in the group belong to the same molecule. If they belong to distinct 

47 molecules, it is possible to circumvent the issue by calling the method 

48 :func:`getNullBondForce` and adding the resulting force to the system. 

49 

50 Parameters 

51 ---------- 

52 referencePositions 

53 The reference coordinates, which can be either a coordinate matrix or a mapping 

54 from atom indices to coordinate vectors. It must contain all atoms in ``group``, 

55 and does not need to contain all atoms in the system. See ``numAtoms`` below. 

56 group 

57 A sequence of atom indices. 

58 numAtoms 

59 The total number of atoms in the system, including those that are not in 

60 ``group``. This argument is necessary only if ``referencePositions`` does not 

61 contain all atoms in the system. 

62 name 

63 The name of the collective variable. 

64 

65 Raises 

66 ------ 

67 ValueError 

68 If referencePositions is a dict and numAtoms is not given, or if numAtoms 

69 is not positive. 

70 

71 Example 

72 ------- 

73 >>> import cvpack 

74 >>> import openmm 

75 >>> from openmm import app, unit 

76 >>> from openmmtools import testsystems 

77 >>> model = testsystems.AlanineDipeptideImplicit() 

78 >>> num_atoms = model.topology.getNumAtoms() 

79 >>> group = list(range(num_atoms)) 

80 >>> rmsd = cvpack.RMSD(model.positions, group, num_atoms) 

81 >>> rmsd.addToSystem(model.system) 

82 >>> platform = openmm.Platform.getPlatformByName('Reference') 

83 >>> integrator = openmm.VerletIntegrator(2*unit.femtoseconds) 

84 >>> context = openmm.Context(model.system, integrator, platform) 

85 >>> context.setPositions(model.positions) 

86 >>> integrator.step(1000) 

87 >>> rmsd.getValue(context) 

88 0.123138... nm 

89 

90 """ 

91 

92 def __init__( 

93 self, 

94 referencePositions: MatrixQuantity | dict[int, VectorQuantity], 

95 group: t.Iterable[int], 

96 numAtoms: int | None = None, 

97 name: str = "rmsd", 

98 ) -> None: 

99 group = list(map(int, group)) 

100 if not group: 

101 # OpenMM's RMSDForce would interpret an empty group as all atoms 

102 raise ValueError("The group of atoms must not be empty") 

103 num_atoms = self._getNumAtoms(referencePositions, numAtoms) 

104 defined_coords = self._getDefinedCoords(referencePositions, group) 

105 all_coords = self._getAllCoords(defined_coords, num_atoms) 

106 super().__init__(all_coords, group) 

107 self._registerCV( 

108 name, 

109 mmunit.nanometers, 

110 referencePositions=defined_coords, 

111 group=group, 

112 numAtoms=num_atoms, 

113 ) 

114 

115 def getNullBondForce(self) -> openmm.HarmonicBondForce: 

116 """ 

117 Get a null bond force that creates a connected graph with all the atoms in the 

118 group. 

119 

120 Returns 

121 ------- 

122 openmm.HarmonicBondForce 

123 The null bond force. 

124 

125 Example 

126 ------- 

127 >>> import cvpack 

128 >>> import openmm 

129 >>> from openmm import app, unit 

130 >>> from openmmtools import testsystems 

131 >>> model = testsystems.WaterBox( 

132 ... box_edge=10*unit.angstroms, 

133 ... cutoff=5*unit.angstroms, 

134 ... ) 

135 >>> group = [ 

136 ... atom.index 

137 ... for atom in model.topology.atoms() 

138 ... if atom.residue.index < 3 

139 ... ] 

140 >>> rmsd = cvpack.RMSD( 

141 ... model.positions, group, model.topology.getNumAtoms() 

142 ... ) 

143 >>> rmsd.addToSystem(model.system) 

144 >>> _ = model.system.addForce(rmsd.getNullBondForce()) 

145 >>> integrator = openmm.VerletIntegrator(2*unit.femtoseconds) 

146 >>> platform = openmm.Platform.getPlatformByName('Reference') 

147 >>> context = openmm.Context(model.system, integrator, platform) 

148 >>> context.setPositions(model.positions) 

149 >>> integrator.step(100) 

150 >>> rmsd.getValue(context) 

151 0.10436... nm 

152 

153 """ 

154 force = openmm.HarmonicBondForce() 

155 group = self._args["group"] 

156 for i, j in zip(group[:-1], group[1:]): 

157 force.addBond(i, j, 0.0, 0.0) 

158 return force 

159 

160 

161RMSD.registerTag("!cvpack.RMSD")