Coverage for ase/md/switch_langevin.py: 91.67%

48 statements  

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

1from typing import Any 

2 

3import numpy as np 

4from scipy.integrate import trapezoid 

5 

6from ase import Atoms 

7from ase.calculators.mixing import MixedCalculator 

8from ase.md.langevin import Langevin 

9 

10 

11class SwitchLangevin(Langevin): 

12 """ 

13 MD class for carrying out thermodynamic integration between two 

14 hamiltonians 

15 

16 Read more at https://en.wikipedia.org/wiki/Thermodynamic_integration 

17 

18 The integration path is lambda 0 ---> 1 , with the switching hamiltonian 

19 H(lambda) = (1-lambda) * H1 + lambda * H2 

20 

21 Attributes 

22 ---------- 

23 path_data : numpy array 

24 col 1 (md-step), col 2 (lambda), col 3 (energy H1), col 4 (energy H2) 

25 

26 Parameters 

27 ---------- 

28 atoms : ASE Atoms object 

29 Atoms object for which MD will be run 

30 calc1 : ASE calculator object 

31 Calculator corresponding to first Hamiltonian 

32 calc2 : ASE calculator object 

33 Calculator corresponding to second Hamiltonian 

34 dt : float 

35 Timestep for MD simulation 

36 T : float 

37 Temperature in eV (deprecated) 

38 friction : float 

39 Friction for langevin dynamics 

40 n_eq : int 

41 Number of equilibration steps 

42 n_switch : int 

43 Number of switching steps 

44 """ 

45 

46 def __init__( 

47 self, 

48 atoms: Atoms, 

49 calc1, 

50 calc2, 

51 dt: float, 

52 T: float | None = None, 

53 friction: float | None = None, 

54 n_eq: int | None = None, 

55 n_switch: int | None = None, 

56 temperature_K: float | None = None, 

57 **langevin_kwargs, 

58 ): 

59 super().__init__( 

60 atoms, 

61 dt, 

62 temperature=T, 

63 temperature_K=temperature_K, 

64 friction=friction, 

65 **langevin_kwargs, 

66 ) 

67 if friction is None: 

68 raise TypeError("Missing 'friction' argument.") 

69 if n_eq is None: 

70 raise TypeError("Missing 'n_eq' argument.") 

71 if n_switch is None: 

72 raise TypeError("Missing 'n_switch' argument.") 

73 self.n_eq = n_eq 

74 self.n_switch = n_switch 

75 self.lam = 0.0 

76 calc = MixedCalculator(calc1, calc2, weight1=1.0, weight2=0.0) 

77 self.atoms.calc = calc 

78 

79 self.path_data: list[Any] = [] 

80 

81 def run(self): 

82 """Run the MD switching simulation""" 

83 forces = self.atoms.get_forces(md=True) 

84 

85 # run equilibration with calc1 

86 for _ in range(self.n_eq): 

87 forces = self.step(forces) 

88 self.nsteps += 1 

89 self.call_observers() 

90 

91 # run switch from calc1 to calc2 

92 self.path_data.append( 

93 [0, self.lam, *self.atoms.calc.get_energy_contributions(self.atoms)] 

94 ) 

95 for step in range(1, self.n_switch): 

96 # update calculator 

97 self.lam = get_lambda(step, self.n_switch) 

98 self.atoms.calc.set_weights(1 - self.lam, self.lam) 

99 

100 # carry out md step 

101 forces = self.step(forces) 

102 self.nsteps += 1 

103 

104 # collect data 

105 self.call_observers() 

106 self.path_data.append( 

107 [ 

108 step, 

109 self.lam, 

110 *self.atoms.calc.get_energy_contributions(self.atoms), 

111 ] 

112 ) 

113 

114 self.path_data = np.array(self.path_data) 

115 

116 def get_free_energy_difference(self): 

117 """Return the free energy difference between calc2 and calc1, by 

118 integrating dH/dlam along the switching path 

119 

120 Returns 

121 ------- 

122 float 

123 Free energy difference, F2 - F1 

124 """ 

125 if len(self.path_data) == 0: 

126 raise ValueError('No free energy data found.') 

127 

128 lambdas = self.path_data[:, 1] 

129 U1 = self.path_data[:, 2] 

130 U2 = self.path_data[:, 3] 

131 delta_F = trapezoid(U2 - U1, lambdas) 

132 return delta_F 

133 

134 

135def get_lambda(step, n_switch): 

136 """Return lambda value along the switching path""" 

137 assert step >= 0 and step <= n_switch 

138 t = step / (n_switch - 1) 

139 return t**5 * (70 * t**4 - 315 * t**3 + 540 * t**2 - 420 * t + 126)