Coverage for /builds/debichem-team/python-ase/ase/md/verlet.py: 100.00%

17 statements  

« prev     ^ index     » next       coverage.py v7.5.3, created at 2025-03-06 04:00 +0000

1"""Velocity Verlet.""" 

2from ase.md.md import MolecularDynamics 

3 

4 

5class VelocityVerlet(MolecularDynamics): 

6 """MD with NVE ensemble and velocity Verlet time integration.""" 

7 

8 def step(self, forces=None): 

9 

10 atoms = self.atoms 

11 

12 if forces is None: 

13 forces = atoms.get_forces(md=True) 

14 

15 p = atoms.get_momenta() 

16 p += 0.5 * self.dt * forces 

17 masses = atoms.get_masses()[:, None] 

18 r = atoms.get_positions() 

19 

20 # if we have constraints then this will do the first part of the 

21 # RATTLE algorithm: 

22 atoms.set_positions(r + self.dt * p / masses) 

23 if atoms.constraints: 

24 p = (atoms.get_positions() - r) * masses / self.dt 

25 

26 # We need to store the momenta on the atoms before calculating 

27 # the forces, as in a parallel Asap calculation atoms may 

28 # migrate during force calculations, and the momenta need to 

29 # migrate along with the atoms. 

30 atoms.set_momenta(p, apply_constraint=False) 

31 

32 forces = atoms.get_forces(md=True) 

33 

34 # Second part of RATTLE will be done here: 

35 atoms.set_momenta(atoms.get_momenta() + 0.5 * self.dt * forces) 

36 return forces