Coverage for ase/md/nvtberendsen.py: 83.67%
49 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-27 15:21 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-27 15:21 +0000
1"""Berendsen NVT dynamics class."""
3import numpy as np
5from ase import Atoms
6from ase.md.md import MolecularDynamics
9class NVTBerendsen(MolecularDynamics):
10 """Berendsen (constant N, V, T) molecular dynamics.
12 Berendsen *et al.*, J. Chem. Phys. 81, 3684–3690 (1984).
13 https://doi.org/10.1063/1.448118
14 """
16 def __init__(
17 self,
18 atoms: Atoms,
19 timestep: float,
20 temperature: float | None = None,
21 taut: float | None = None,
22 fixcm: bool = True,
23 *,
24 temperature_K: float | None = None,
25 **kwargs,
26 ):
27 """
28 Parameters
29 ----------
30 atoms: Atoms object
31 The list of atoms.
33 timestep: float
34 The time step in ASE time units.
36 temperature: float
37 The desired temperature, in Kelvin.
39 temperature_K: float
40 Alias for *temperature*
42 taut: float
43 Time constant for Berendsen temperature coupling in ASE
44 time units.
46 fixcm: bool (optional)
47 If True, the position and momentum of the center of mass is
48 kept unperturbed. Default: True.
50 **kwargs : dict, optional
51 Additional arguments passed to :class:~ase.md.md.MolecularDynamics
52 base class.
54 Physical background and limitations
55 -----------------------------------
57 In Berendsen NVT simulations the velocities are scaled to achieve
58 the desired temperature.
60 This method does not result in proper NVT sampling but it usually is
61 sufficiently good in practice (with large `taut`). For discussion see
62 the gromacs manual at https://www.gromacs.org.
64 Example
65 --------
66 Room temperature simulation (300 K, 0.1 fs time step):
68 >>> from ase import units
69 >>> from ase.build import bulk
70 >>> from ase.calculators.emt import EMT
71 >>> from ase.md import thermalize_momenta
72 >>> from ase.md.nvtberendsen import NVTBerendsen
73 >>> atoms = bulk('Cu', cubic=True)
74 >>> atoms.calc = EMT()
75 >>> thermalize_momenta(atoms, temperature_K=300)
76 >>> dyn = NVTBerendsen(atoms, 0.1 * units.fs, temperature_K=300,
77 ... taut=0.5 * 1000 * units.fs)
78 >>> dyn.run(10)
79 True
81 """
82 MolecularDynamics.__init__(self, atoms, timestep, **kwargs)
84 if taut is None:
85 raise TypeError("Missing 'taut' argument.")
86 self.taut = taut
87 self.temperature = self._process_temperature(
88 temperature, temperature_K, 'K'
89 )
91 self.fix_com = fixcm # will the center of mass be held fixed?
93 def set_taut(self, taut):
94 self.taut = taut
96 def get_taut(self):
97 return self.taut
99 def set_temperature(self, temperature=None, *, temperature_K=None):
100 self.temperature = self._process_temperature(
101 temperature, temperature_K, 'K'
102 )
104 def get_temperature(self):
105 return self.temperature
107 def set_timestep(self, timestep):
108 self.dt = timestep
110 def get_timestep(self):
111 return self.dt
113 def scale_velocities(self):
114 """Do the NVT Berendsen velocity scaling"""
115 tautscl = self.dt / self.taut
116 old_temperature = self.atoms.get_temperature()
118 scl_temperature = np.sqrt(
119 1.0 + (self.temperature / old_temperature - 1.0) * tautscl
120 )
121 # Limit the velocity scaling to reasonable values
122 if scl_temperature > 1.1:
123 scl_temperature = 1.1
124 if scl_temperature < 0.9:
125 scl_temperature = 0.9
127 p = self.atoms.get_momenta()
128 p = scl_temperature * p
129 self.atoms.set_momenta(p)
131 def step(self, forces=None):
132 """Move one timestep forward using Berenden NVT molecular dynamics."""
133 self.scale_velocities()
135 # one step velocity verlet
136 atoms = self.atoms
138 if forces is None:
139 forces = atoms.get_forces(md=True)
141 p = self.atoms.get_momenta()
142 p += 0.5 * self.dt * forces
144 if self.fix_com:
145 # calculate the center of mass
146 # momentum and subtract it
147 psum = p.sum(axis=0) / float(len(p))
148 p = p - psum
150 self.atoms.set_positions(
151 self.atoms.get_positions()
152 + self.dt * p / self.atoms.get_masses()[:, np.newaxis]
153 )
155 # We need to store the momenta on the atoms before calculating
156 # the forces, as in a parallel Asap calculation atoms may
157 # migrate during force calculations, and the momenta need to
158 # migrate along with the atoms. For the same reason, we
159 # cannot use self.masses in the line above.
161 self.atoms.set_momenta(p)
162 forces = self.atoms.get_forces(md=True)
163 atoms.set_momenta(self.atoms.get_momenta() + 0.5 * self.dt * forces)
165 return forces