Coverage for cvpack/radius_of_gyration_sq.py: 100%

9 statements  

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

1""" 

2.. class:: RadiusOfGyrationSq 

3 :platform: Linux, MacOS, Windows 

4 :synopsis: The square of the radius of gyration of a group of atoms 

5 

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

7 

8""" 

9 

10import typing as t 

11 

12from openmm import unit as mmunit 

13 

14from .base_radius_of_gyration import BaseRadiusOfGyration 

15 

16 

17class RadiusOfGyrationSq(BaseRadiusOfGyration): 

18 r""" 

19 The square of the radius of gyration of a group of :math:`n` atoms: 

20 

21 .. math:: 

22 

23 r_g^2({\bf r}) = \frac{1}{n} \sum_{i=1}^n \left\| 

24 {\bf r}_i - {\bf r}_c({\bf r}) 

25 \right\|^2. 

26 

27 where :math:`{\bf r}_c({\bf r})` is the geometric center of the group: 

28 

29 .. math:: 

30 

31 {\bf r}_c({\bf r}) = \frac{1}{n} \sum_{j=1}^n {\bf r}_j 

32 

33 .. note:: 

34 

35 The computation is done by OpenMM's :OpenMM:`RGForce`. 

36 

37 Parameters 

38 ---------- 

39 group 

40 The indices of the atoms in the group. 

41 pbc 

42 Deprecated, with no effect. OpenMM does not wrap positions inside a 

43 :OpenMM:`Context`, so the group of atoms must not be split across periodic 

44 images, as was already required for its centroid. 

45 weighByMass 

46 No longer supported. It must be False, because ``True`` measured distances from 

47 the center of mass without weighting their average, which is not the usual 

48 mass-weighted radius of gyration. 

49 name 

50 The name of the collective variable. 

51 

52 Example 

53 ------- 

54 >>> import cvpack 

55 >>> import openmm 

56 >>> from openmmtools import testsystems 

57 >>> model = testsystems.AlanineDipeptideVacuum() 

58 >>> num_atoms = model.system.getNumParticles() 

59 >>> rgsq = cvpack.RadiusOfGyrationSq(list(range(num_atoms))) 

60 >>> rgsq.addToSystem(model.system) 

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

62 >>> integrator = openmm.VerletIntegrator(0) 

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

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

65 >>> print(rgsq.getValue(context)) # doctest: +ELLIPSIS 

66 0.0871... nm**2 

67 

68 """ 

69 

70 def __init__( 

71 self, 

72 group: t.Iterable[int], 

73 pbc: bool = False, 

74 weighByMass: bool = False, 

75 name: str = "radius_of_gyration_sq", 

76 ) -> None: 

77 group = list(group) 

78 super().__init__(True, group, pbc, weighByMass) 

79 self._registerCV( 

80 name, mmunit.nanometers**2, group=group, pbc=pbc, weighByMass=weighByMass 

81 ) 

82 

83 

84RadiusOfGyrationSq.registerTag("!cvpack.RadiusOfGyrationSq")