Coverage for /builds/ase/ase/ase/md/verlet.py: 100.00%

17 statements  

« prev     ^ index     » next       coverage.py v7.5.3, created at 2025-08-02 00:12 +0000

1# fmt: off 

2 

3"""Velocity Verlet.""" 

4from ase.md.md import MolecularDynamics 

5 

6 

7class VelocityVerlet(MolecularDynamics): 

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

9 

10 def step(self, forces=None): 

11 

12 atoms = self.atoms 

13 

14 if forces is None: 

15 forces = atoms.get_forces(md=True) 

16 

17 p = atoms.get_momenta() 

18 p += 0.5 * self.dt * forces 

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

20 r = atoms.get_positions() 

21 

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

23 # RATTLE algorithm: 

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

25 if atoms.constraints: 

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

27 

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

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

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

31 # migrate along with the atoms. 

32 atoms.set_momenta(p, apply_constraint=False) 

33 

34 forces = atoms.get_forces(md=True) 

35 

36 # Second part of RATTLE will be done here: 

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

38 return forces