Coverage for ase/calculators/tip4p.py: 96.40%

139 statements  

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

1import numpy as np 

2 

3from ase import units 

4from ase.calculators.calculator import Calculator, all_changes 

5from ase.calculators.tip3p import TIP3P, angleHOH, rOH 

6 

7__all__ = ['rOH', 'angleHOH', 'TIP4P', 'sigma0', 'epsilon0'] 

8 

9# Electrostatic constant and parameters: 

10k_c = units.Hartree * units.Bohr 

11qH = 0.52 

12A = 600e3 * units.kcal / units.mol 

13B = 610 * units.kcal / units.mol 

14sigma0 = (A / B) ** (1 / 6.0) 

15epsilon0 = B**2 / (4 * A) 

16# https://doi.org/10.1063/1.445869 

17 

18 

19class TIP4P(TIP3P): 

20 def __init__(self, rc=7.0, width=1.0): 

21 """TIP4P potential for water. 

22 

23 :doi:`10.1063/1.445869` 

24 

25 Requires an atoms object of OHH,OHH, ... sequence 

26 Correct TIP4P charges and LJ parameters set automatically. 

27 

28 Virtual interaction sites implemented in the following scheme: 

29 Original atoms object has no virtual sites. 

30 When energy/forces are requested: 

31 

32 * virtual sites added to temporary xatoms object 

33 * energy / forces calculated 

34 * forces redistributed from virtual sites to actual atoms object 

35 

36 This means you do not get into trouble when propagating your system 

37 with MD while having to skip / account for massless virtual sites. 

38 

39 This also means that if using for QM/MM MD with GPAW, the EmbedTIP4P 

40 class must be used. 

41 """ 

42 

43 TIP3P.__init__(self, rc, width) 

44 self.atoms_per_mol = 3 

45 self.sites_per_mol = 4 

46 self.energy = None 

47 self.forces = None 

48 

49 def calculate( 

50 self, 

51 atoms=None, 

52 properties=None, 

53 system_changes=all_changes, 

54 ): 

55 if properties is None: 

56 properties = ['energy', 'forces'] 

57 Calculator.calculate(self, atoms, properties, system_changes) 

58 

59 assert (atoms.numbers[::3] == 8).all() 

60 assert (atoms.numbers[1::3] == 1).all() 

61 assert (atoms.numbers[2::3] == 1).all() 

62 

63 xpos = self.add_virtual_sites(atoms.positions) 

64 xcharges = self.get_virtual_charges(atoms) 

65 

66 cell = atoms.cell 

67 pbc = atoms.pbc 

68 

69 natoms = len(atoms) 

70 nmol = natoms // 3 

71 

72 self.energy = 0.0 

73 self.forces = np.zeros((4 * natoms // 3, 3)) 

74 

75 C = cell.diagonal() 

76 assert (cell == np.diag(C)).all(), 'not orthorhombic' 

77 assert ((C >= 2 * self.rc) | ~pbc).all(), 'cutoff too large' 

78 

79 # Get dx,dy,dz from first atom of each mol to same atom of all other 

80 # and find min. distance. Everything moves according to this analysis. 

81 for a in range(nmol - 1): 

82 D = xpos[(a + 1) * 4 :: 4] - xpos[a * 4] 

83 shift = np.zeros_like(D) 

84 for i, periodic in enumerate(pbc): 

85 if periodic: 

86 shift[:, i] = np.rint(D[:, i] / C[i]) * C[i] 

87 q_v = xcharges[(a + 1) * 4 :] 

88 

89 # Min. img. position list as seen for molecule !a! 

90 position_list = np.zeros(((nmol - 1 - a) * 4, 3)) 

91 

92 for j in range(4): 

93 position_list[j::4] += xpos[(a + 1) * 4 + j :: 4] - shift 

94 

95 # Make the smooth cutoff: 

96 pbcRoo = position_list[::4] - xpos[a * 4] 

97 pbcDoo = np.sum(np.abs(pbcRoo) ** 2, axis=-1) ** (1 / 2) 

98 x1 = pbcDoo > self.rc - self.width 

99 x2 = pbcDoo < self.rc 

100 x12 = np.logical_and(x1, x2) 

101 y = (pbcDoo[x12] - self.rc + self.width) / self.width 

102 t = np.zeros(len(pbcDoo)) 

103 t[x2] = 1.0 

104 t[x12] -= y**2 * (3.0 - 2.0 * y) 

105 dtdd = np.zeros(len(pbcDoo)) 

106 dtdd[x12] -= 6.0 / self.width * y * (1.0 - y) 

107 self.energy_and_forces(a, xpos, position_list, q_v, nmol, t, dtdd) 

108 

109 if self.pcpot: 

110 e, f = self.pcpot.calculate(xcharges, xpos) 

111 self.energy += e 

112 self.forces += f 

113 

114 f = self.redistribute_forces(self.forces) 

115 

116 self.results['energy'] = self.energy 

117 self.results['forces'] = f 

118 

119 def energy_and_forces(self, a, xpos, position_list, q_v, nmol, t, dtdd): 

120 """energy and forces on molecule a from all other molecules. 

121 cutoff is based on O-O Distance.""" 

122 

123 # LJ part - only O-O interactions 

124 epsil = np.tile([epsilon0], nmol - 1 - a) 

125 sigma = np.tile([sigma0], nmol - 1 - a) 

126 DOO = position_list[::4] - xpos[a * 4] 

127 d2 = (DOO**2).sum(1) 

128 d = np.sqrt(d2) 

129 e_lj = 4 * epsil * (sigma**12 / d**12 - sigma**6 / d**6) 

130 f_lj = ( 

131 ( 

132 4 * epsil * (12 * sigma**12 / d**13 - 6 * sigma**6 / d**7) * t 

133 - e_lj * dtdd 

134 )[:, np.newaxis] 

135 * DOO 

136 / d[:, np.newaxis] 

137 ) 

138 

139 self.forces[a * 4] -= f_lj.sum(0) 

140 self.forces[(a + 1) * 4 :: 4] += f_lj 

141 

142 # Electrostatics 

143 e_elec = 0 

144 all_cut = np.repeat(t, 4) 

145 for i in range(4): 

146 D = position_list - xpos[a * 4 + i] 

147 d2_all = (D**2).sum(axis=1) 

148 d_all = np.sqrt(d2_all) 

149 e = k_c * q_v[i] * q_v / d_all 

150 e_elec += np.dot(all_cut, e).sum() 

151 e_f = e.reshape(nmol - a - 1, 4).sum(1) 

152 F = (e / d_all * all_cut)[:, np.newaxis] * D / d_all[:, np.newaxis] 

153 FOO = -(e_f * dtdd)[:, np.newaxis] * DOO / d[:, np.newaxis] 

154 self.forces[(a + 1) * 4 + 0 :: 4] += FOO 

155 self.forces[a * 4] -= FOO.sum(0) 

156 self.forces[(a + 1) * 4 :] += F 

157 self.forces[a * 4 + i] -= F.sum(0) 

158 

159 self.energy += np.dot(e_lj, t) + e_elec 

160 

161 def add_virtual_sites(self, pos): 

162 # Order: OHHM,OHHM,... 

163 # DOI: 10.1002/(SICI)1096-987X(199906)20:8 

164 b = 0.15 

165 xatomspos = np.zeros((4 * len(pos) // 3, 3)) 

166 for w in range(0, len(pos), 3): 

167 r_i = pos[w] # O pos 

168 r_j = pos[w + 1] # H1 pos 

169 r_k = pos[w + 2] # H2 pos 

170 n = (r_j + r_k) / 2 - r_i 

171 n /= np.linalg.norm(n) 

172 r_d = r_i + b * n 

173 

174 x = 4 * w // 3 

175 xatomspos[x + 0] = r_i 

176 xatomspos[x + 1] = r_j 

177 xatomspos[x + 2] = r_k 

178 xatomspos[x + 3] = r_d 

179 

180 return xatomspos 

181 

182 def get_virtual_charges(self, atoms): 

183 charges = np.empty(len(atoms) * 4 // 3) 

184 charges[0::4] = 0.00 # O 

185 charges[1::4] = qH # H1 

186 charges[2::4] = qH # H2 

187 charges[3::4] = -2 * qH # X1 

188 return charges 

189 

190 def redistribute_forces(self, forces): 

191 f = forces 

192 b = 0.15 

193 a = 0.5 

194 pos = self.atoms.positions 

195 for w in range(0, len(pos), 3): 

196 r_i = pos[w] # O pos 

197 r_j = pos[w + 1] # H1 pos 

198 r_k = pos[w + 2] # H2 pos 

199 r_ij = r_j - r_i 

200 r_jk = r_k - r_j 

201 r_d = r_i + b * (r_ij + a * r_jk) / np.linalg.norm(r_ij + a * r_jk) 

202 r_id = r_d - r_i 

203 gamma = b / np.linalg.norm(r_ij + a * r_jk) 

204 

205 x = w * 4 // 3 

206 Fd = f[x + 3] # force on M 

207 F1 = (np.dot(r_id, Fd) / np.dot(r_id, r_id)) * r_id 

208 Fi = Fd - gamma * (Fd - F1) # Force from M on O 

209 Fj = (1 - a) * gamma * (Fd - F1) # Force from M on H1 

210 Fk = a * gamma * (Fd - F1) # Force from M on H2 

211 

212 f[x] += Fi 

213 f[x + 1] += Fj 

214 f[x + 2] += Fk 

215 

216 # remove virtual sites from force array 

217 f = np.delete(f, list(range(3, f.shape[0], 4)), axis=0) 

218 return f