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

1"""Berendsen NVT dynamics class.""" 

2 

3import numpy as np 

4 

5from ase import Atoms 

6from ase.md.md import MolecularDynamics 

7 

8 

9class NVTBerendsen(MolecularDynamics): 

10 """Berendsen (constant N, V, T) molecular dynamics. 

11 

12 Berendsen *et al.*, J. Chem. Phys. 81, 3684–3690 (1984). 

13 https://doi.org/10.1063/1.448118 

14 """ 

15 

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. 

32 

33 timestep: float 

34 The time step in ASE time units. 

35 

36 temperature: float 

37 The desired temperature, in Kelvin. 

38 

39 temperature_K: float 

40 Alias for *temperature* 

41 

42 taut: float 

43 Time constant for Berendsen temperature coupling in ASE 

44 time units. 

45 

46 fixcm: bool (optional) 

47 If True, the position and momentum of the center of mass is 

48 kept unperturbed. Default: True. 

49 

50 **kwargs : dict, optional 

51 Additional arguments passed to :class:~ase.md.md.MolecularDynamics 

52 base class. 

53 

54 Physical background and limitations 

55 ----------------------------------- 

56 

57 In Berendsen NVT simulations the velocities are scaled to achieve 

58 the desired temperature. 

59 

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. 

63 

64 Example 

65 -------- 

66 Room temperature simulation (300 K, 0.1 fs time step): 

67 

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 

80 

81 """ 

82 MolecularDynamics.__init__(self, atoms, timestep, **kwargs) 

83 

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 ) 

90 

91 self.fix_com = fixcm # will the center of mass be held fixed? 

92 

93 def set_taut(self, taut): 

94 self.taut = taut 

95 

96 def get_taut(self): 

97 return self.taut 

98 

99 def set_temperature(self, temperature=None, *, temperature_K=None): 

100 self.temperature = self._process_temperature( 

101 temperature, temperature_K, 'K' 

102 ) 

103 

104 def get_temperature(self): 

105 return self.temperature 

106 

107 def set_timestep(self, timestep): 

108 self.dt = timestep 

109 

110 def get_timestep(self): 

111 return self.dt 

112 

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() 

117 

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 

126 

127 p = self.atoms.get_momenta() 

128 p = scl_temperature * p 

129 self.atoms.set_momenta(p) 

130 

131 def step(self, forces=None): 

132 """Move one timestep forward using Berenden NVT molecular dynamics.""" 

133 self.scale_velocities() 

134 

135 # one step velocity verlet 

136 atoms = self.atoms 

137 

138 if forces is None: 

139 forces = atoms.get_forces(md=True) 

140 

141 p = self.atoms.get_momenta() 

142 p += 0.5 * self.dt * forces 

143 

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 

149 

150 self.atoms.set_positions( 

151 self.atoms.get_positions() 

152 + self.dt * p / self.atoms.get_masses()[:, np.newaxis] 

153 ) 

154 

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. 

160 

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) 

164 

165 return forces