Coverage for /builds/ase/ase/ase/md/fix.py: 100.00%
22 statements
« prev ^ index » next coverage.py v7.5.3, created at 2025-08-02 00:12 +0000
« prev ^ index » next coverage.py v7.5.3, created at 2025-08-02 00:12 +0000
1# fmt: off
3import numpy as np
6class FixRotation:
7 """Remove rotation from an atoms object.
9 This class is intended as an observer on an atoms class during
10 a molecular dynamics simulation. When it is called, it removes
11 any rotation around the center of mass.
13 It assumes that the system is a (nano)particle with free boundary
14 conditions.
16 Bugs:
17 Should check that the boundary conditions make sense.
18 """
20 def __init__(self, atoms):
21 self.atoms = atoms
23 def __call__(self):
24 atoms = self.atoms
26 r = atoms.get_positions() - atoms.get_center_of_mass()
27 v = atoms.get_velocities()
28 p = atoms.get_momenta()
29 m = atoms.get_masses()
31 x = r[:, 0]
32 y = r[:, 1]
33 z = r[:, 2]
35 I11 = np.sum(m * (y**2 + z**2))
36 I22 = np.sum(m * (x**2 + z**2))
37 I33 = np.sum(m * (x**2 + y**2))
38 I12 = np.sum(-m * x * y)
39 I13 = np.sum(-m * x * z)
40 I23 = np.sum(-m * y * z)
42 I = np.array([[I11, I12, I13],
43 [I12, I22, I23],
44 [I13, I23, I33]])
46 w = np.dot(np.linalg.inv(I), np.sum(np.cross(r, p), axis=0))
48 self.atoms.set_velocities(v - np.cross(w, r))