Coverage for cvpack/base_radius_of_gyration.py: 100%
16 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-16 00:10 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-16 00:10 +0000
1"""
2.. class:: BaseRadiusOfGyration
3 :platform: Linux, MacOS, Windows
4 :synopsis: Base class for the radius of gyration of an atom group
6.. classauthor:: Charlles Abreu <craabreu@gmail.com>
8"""
10import typing as t
11import warnings
13import openmm
15from .collective_variable import CollectiveVariable
18class BaseRadiusOfGyration(CollectiveVariable, openmm.CustomCVForce):
19 """
20 Abstract class for the radius of gyration of a group of `n` atoms, computed by
21 OpenMM's RGForce.
23 Parameters
24 ----------
25 squared
26 Whether the collective variable is the squared radius of gyration.
27 group
28 The indices of the atoms in the group.
29 pbc
30 Deprecated. It has no effect.
31 weighByMass
32 No longer supported. It must be False.
33 """
35 def __init__(
36 self,
37 squared: bool,
38 group: t.Sequence[int],
39 pbc: bool,
40 weighByMass: bool,
41 ) -> None:
42 if not group:
43 # OpenMM's RGForce would interpret an empty group as all atoms
44 raise ValueError("The group of atoms must not be empty")
45 if len(set(group)) != len(group):
46 raise ValueError("The group of atoms must not have repeated indices")
47 if weighByMass:
48 raise ValueError(
49 "weighByMass=True is no longer supported. It measured distances from "
50 "the center of mass but averaged them without mass weights, which is "
51 "not the usual mass-weighted radius of gyration."
52 )
53 if pbc:
54 warnings.warn(
55 "The pbc argument of the radius of gyration is deprecated and has no "
56 "effect. The group of atoms must not be split across periodic images.",
57 DeprecationWarning,
58 stacklevel=3,
59 )
60 super().__init__("rg^2" if squared else "rg")
61 self.addCollectiveVariable("rg", openmm.RGForce(group))