Coverage for /builds/kinetik161/ase/ase/md/fix.py: 100.00%

22 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-12-10 11:04 +0000

1import numpy as np 

2 

3 

4class FixRotation: 

5 """Remove rotation from an atoms object. 

6 

7 This class is intended as an observer on an atoms class during 

8 a molecular dynamics simulation. When it is called, it removes 

9 any rotation around the center of mass. 

10 

11 It assumes that the system is a (nano)particle with free boundary 

12 conditions. 

13 

14 Bugs: 

15 Should check that the boundary conditions make sense. 

16 """ 

17 

18 def __init__(self, atoms): 

19 self.atoms = atoms 

20 

21 def __call__(self): 

22 atoms = self.atoms 

23 

24 r = atoms.get_positions() - atoms.get_center_of_mass() 

25 v = atoms.get_velocities() 

26 p = atoms.get_momenta() 

27 m = atoms.get_masses() 

28 

29 x = r[:, 0] 

30 y = r[:, 1] 

31 z = r[:, 2] 

32 

33 I11 = np.sum(m * (y**2 + z**2)) 

34 I22 = np.sum(m * (x**2 + z**2)) 

35 I33 = np.sum(m * (x**2 + y**2)) 

36 I12 = np.sum(-m * x * y) 

37 I13 = np.sum(-m * x * z) 

38 I23 = np.sum(-m * y * z) 

39 

40 I = np.array([[I11, I12, I13], 

41 [I12, I22, I23], 

42 [I13, I23, I33]]) 

43 

44 w = np.dot(np.linalg.inv(I), np.sum(np.cross(r, p), axis=0)) 

45 

46 self.atoms.set_velocities(v - np.cross(w, r))