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

17 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-11-27 15:21 +0000

1"""Velocity Verlet.""" 

2 

3from ase.md.md import MolecularDynamics 

4 

5 

6class VelocityVerlet(MolecularDynamics): 

7 """ 

8 MD with NVE ensemble and velocity Verlet time integration. 

9 

10 Notes 

11 ----- 

12 The ``VelocityVerlet`` dynamics class implements the NVE ensemble. 

13 It requires two arguments, the atoms and the time step. Choosing 

14 too large a time step will immediately be obvious, as the energy will 

15 increase with time, often very rapidly. 

16 

17 A longer example is given in the tutorial :ref:`md_tutorial`. 

18 

19 It is also possible to use :class:`ase.md.langevinbaoab.LangevinBAOAB` 

20 for NVE or NPH (variable cell) by running without a target temperature; 

21 see :ref:`langevin-baoab-description`. 

22 

23 Example 

24 ------- 

25 >>> from ase import units 

26 >>> from ase.build import bulk 

27 >>> from ase.calculators.emt import EMT 

28 >>> from ase.md.verlet import VelocityVerlet 

29 >>> atoms = bulk('Cu', cubic=True) 

30 >>> atoms.calc = EMT() 

31 >>> dyn = VelocityVerlet(atoms, timestep=5.0 * units.fs, 

32 ... trajectory='md.traj', logfile='md.log') 

33 >>> dyn.run(10) # take 10 steps 

34 True 

35 """ 

36 

37 def step(self, forces=None): 

38 

39 atoms = self.atoms 

40 

41 if forces is None: 

42 forces = atoms.get_forces(md=True) 

43 

44 p = atoms.get_momenta() 

45 p += 0.5 * self.dt * forces 

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

47 r = atoms.get_positions() 

48 

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

50 # RATTLE algorithm: 

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

52 if atoms.constraints: 

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

54 

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

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

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

58 # migrate along with the atoms. 

59 atoms.set_momenta(p, apply_constraint=False) 

60 

61 forces = atoms.get_forces(md=True) 

62 

63 # Second part of RATTLE will be done here: 

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

65 return forces