Coverage for cvpack/distance.py: 100%

10 statements  

« prev     ^ index     » next       coverage.py v7.5.1, created at 2024-05-09 16:14 +0000

1""" 

2.. class:: Distance 

3 :platform: Linux, MacOS, Windows 

4 :synopsis: The distance between two atoms 

5 

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

7 

8""" 

9 

10import openmm 

11from openmm import unit as mmunit 

12 

13from .collective_variable import CollectiveVariable 

14 

15 

16class Distance(CollectiveVariable, openmm.CustomBondForce): 

17 r""" 

18 The distance between two atoms: 

19 

20 .. math:: 

21 

22 d({\bf r}) = \| {\bf r}_2 - {\bf r}_1 \|. 

23 

24 Parameters 

25 ---------- 

26 atom1 

27 The index of the first atom. 

28 atom2 

29 The index of the second atom. 

30 pbc 

31 Whether to use periodic boundary conditions. 

32 name 

33 The name of the collective variable. 

34 

35 Example 

36 ------- 

37 >>> import cvpack 

38 >>> import openmm 

39 >>> system = openmm.System() 

40 >>> [system.addParticle(1) for i in range(2)] 

41 [0, 1] 

42 >>> distance = cvpack.Distance(0, 1) 

43 >>> system.addForce(distance) 

44 0 

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

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

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

48 >>> context.setPositions([openmm.Vec3(0, 0, 0),openmm.Vec3(1, 1, 1)]) 

49 >>> distance.getValue(context) 

50 1.7320... nm 

51 

52 """ 

53 

54 def __init__( 

55 self, atom1: int, atom2: int, pbc: bool = False, name: str = "distance" 

56 ) -> None: 

57 super().__init__("r") 

58 self.addBond(atom1, atom2, []) 

59 self.setUsesPeriodicBoundaryConditions(pbc) 

60 self._registerCV(name, mmunit.nanometers, atom1=atom1, atom2=atom2, pbc=pbc) 

61 

62 

63Distance.registerTag("!cvpack.Distance")