Coverage for ase/md/bussi.py: 100.00%
37 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"""Bussi NVT dynamics class."""
3import math
5import numpy as np
7from ase import units
8from ase.md.verlet import VelocityVerlet
11class Bussi(VelocityVerlet):
12 """Bussi stochastic velocity rescaling (NVT) molecular dynamics.
14 Based on the paper from Bussi et al., J. Chem. Phys. 126, 014101 (2007)
15 (also available from https://arxiv.org/abs/0803.4060).
16 """
18 def __init__(
19 self,
20 atoms,
21 timestep,
22 temperature_K,
23 taut,
24 rng=None,
25 **kwargs,
26 ):
27 """
28 Parameters
29 ----------
30 atoms : Atoms
31 The atoms object.
32 timestep : float
33 The time step in ASE time units.
34 temperature_K : float
35 The desired temperature, in Kelvin.
36 taut : float
37 Time constant for Bussi temperature coupling in ASE time units.
38 rng : RNG object, optional
39 Random number generator, by default numpy.random.
40 **kwargs : dict, optional
41 Additional arguments are passed to
42 :class:~ase.md.md.MolecularDynamics base class.
44 Physical background and limitations
45 -----------------------------------
47 The Bussi class implements the Bussi dynamics, where constant
48 temperature is imposed by a stochastic velocity rescaling algorithm.
49 The thermostat is conceptually very close to the Berendsen thermostat,
50 but does sample the canonical ensemble correctly. Given that the
51 thermostat is both correct and global, it is advised to use it for
52 production runs.
53 """
54 super().__init__(atoms, timestep, **kwargs)
56 self.temp = temperature_K * units.kB
57 self.taut = taut
58 if rng is None:
59 self.rng = np.random
60 else:
61 self.rng = rng
63 self.ndof = self.atoms.get_number_of_degrees_of_freedom()
65 self.target_kinetic_energy = 0.5 * self.temp * self.ndof
67 if np.isclose(self.atoms.get_kinetic_energy(), 0.0, rtol=0, atol=1e-12):
68 raise ValueError(
69 'Initial kinetic energy is zero. '
70 'Please set the initial velocities before running Bussi NVT.'
71 )
73 self._exp_term = math.exp(-self.dt / self.taut)
74 self._masses = self.atoms.get_masses()[:, np.newaxis]
76 self.transferred_energy = 0.0
78 def scale_velocities(self):
79 """Do the NVT Bussi stochastic velocity scaling."""
80 kinetic_energy = self.atoms.get_kinetic_energy()
81 alpha = self.calculate_alpha(kinetic_energy)
83 momenta = self.atoms.get_momenta()
84 self.atoms.set_momenta(alpha * momenta)
86 self.transferred_energy += (alpha**2 - 1.0) * kinetic_energy
88 def calculate_alpha(self, kinetic_energy):
89 """Calculate the scaling factor alpha using equation (A7)
90 from the Bussi paper."""
92 energy_scaling_term = (
93 (1 - self._exp_term)
94 * self.target_kinetic_energy
95 / kinetic_energy
96 / self.ndof
97 )
99 # ASE mpi interfaces can only broadcast arrays, not scalars
100 noisearray = np.zeros((2,))
101 # R1 in Eq. (A7)
102 noisearray[0] = self.rng.standard_normal()
103 # \sum_{i=2}^{Nf} R_i^2 in Eq. (A7)
104 # 2 * standard_gamma(n / 2) is equal to chisquare(n)
105 noisearray[1] = 2.0 * self.rng.standard_gamma(0.5 * (self.ndof - 1))
107 self.comm.broadcast(noisearray, 0)
108 normal_noise = noisearray[0]
109 sum_of_noises = noisearray[1]
111 return math.sqrt(
112 self._exp_term
113 + energy_scaling_term * (sum_of_noises + normal_noise**2)
114 + 2 * normal_noise * math.sqrt(self._exp_term * energy_scaling_term)
115 )
117 def step(self, forces=None):
118 """Move one timestep forward using Bussi NVT molecular dynamics."""
119 self.scale_velocities()
120 return super().step(forces)