Coverage for /builds/debichem-team/python-ase/ase/md/fix.py: 100.00%
22 statements
« prev ^ index » next coverage.py v7.5.3, created at 2025-03-06 04:00 +0000
« prev ^ index » next coverage.py v7.5.3, created at 2025-03-06 04:00 +0000
1import numpy as np
4class FixRotation:
5 """Remove rotation from an atoms object.
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.
11 It assumes that the system is a (nano)particle with free boundary
12 conditions.
14 Bugs:
15 Should check that the boundary conditions make sense.
16 """
18 def __init__(self, atoms):
19 self.atoms = atoms
21 def __call__(self):
22 atoms = self.atoms
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()
29 x = r[:, 0]
30 y = r[:, 1]
31 z = r[:, 2]
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)
40 I = np.array([[I11, I12, I13],
41 [I12, I22, I23],
42 [I13, I23, I33]])
44 w = np.dot(np.linalg.inv(I), np.sum(np.cross(r, p), axis=0))
46 self.atoms.set_velocities(v - np.cross(w, r))