Coverage for cvpack/radius_of_gyration_sq.py: 100%
12 statements
« prev ^ index » next coverage.py v7.5.1, created at 2024-05-09 16:14 +0000
« prev ^ index » next coverage.py v7.5.1, created at 2024-05-09 16:14 +0000
1"""
2.. class:: RadiusOfGyrationSq
3 :platform: Linux, MacOS, Windows
5 :synopsis: The square of the radius of gyration of a group of atoms
7.. classauthor:: Charlles Abreu <craabreu@gmail.com>
9"""
11import typing as t
13from openmm import unit as mmunit
15from .base_radius_of_gyration import BaseRadiusOfGyration
18class RadiusOfGyrationSq(BaseRadiusOfGyration):
19 r"""
20 The square of the radius of gyration of a group of :math:`n` atoms:
22 .. math::
24 r_g^2({\bf r}) = \frac{1}{n} \sum_{i=1}^n \left\|
25 {\bf r}_i - {\bf r}_c({\bf r})
26 \right\|^2.
28 where :math:`{\bf r}_c({\bf r})` is the geometric center of the group:
30 .. math::
32 {\bf r}_c({\bf r}) = \frac{1}{n} \sum_{i=j}^n {\bf r}_j
34 Optionally, the radius of gyration can be computed with respect to the center of
35 mass of the group. In this case, the geometric center is replaced by:
37 .. math::
39 {\bf r}_m({\bf r}) = \frac{1}{M} \sum_{i=1}^n m_i {\bf r}_i
41 where :math:`M = \sum_{i=1}^n m_i` is the total mass of the group.
43 .. note::
45 This collective variable is better parallelized than :class:`RadiusOfGyration`
46 and might be preferred over :class:`RadiusOfGyration` when the group of atoms is
47 large.
49 Parameters
50 ----------
51 group
52 The indices of the atoms in the group.
53 pbc
54 Whether to use periodic boundary conditions.
55 weighByMass
56 Whether to use the center of mass of the group instead of its geometric center.
57 name
58 The name of the collective variable.
60 Example
61 -------
62 >>> import cvpack
63 >>> import openmm
64 >>> from openmmtools import testsystems
65 >>> model = testsystems.AlanineDipeptideVacuum()
66 >>> num_atoms = model.system.getNumParticles()
67 >>> rgsq = cvpack.RadiusOfGyrationSq(list(range(num_atoms)))
68 >>> rgsq.addToSystem(model.system)
69 >>> platform = openmm.Platform.getPlatformByName('Reference')
70 >>> integrator = openmm.VerletIntegrator(0)
71 >>> context = openmm.Context(model.system, integrator, platform)
72 >>> context.setPositions(model.positions)
73 >>> print(rgsq.getValue(context)) # doctest: +ELLIPSIS
74 0.0871... nm**2
76 """
78 def __init__(
79 self,
80 group: t.Iterable[int],
81 pbc: bool = False,
82 weighByMass: bool = False,
83 name: str = "radius_of_gyration_sq",
84 ) -> None:
85 group = list(group)
86 num_atoms = len(group)
87 super().__init__(2, f"distance(g1, g2)^2/{num_atoms}", group, pbc, weighByMass)
88 for atom in group:
89 self.addBond([atom, num_atoms])
90 self._registerCV(
91 name, mmunit.nanometers**2, group=group, pbc=pbc, weighByMass=weighByMass
92 )
95RadiusOfGyrationSq.registerTag("!cvpack.RadiusOfGyrationSq")