Coverage for ase/calculators/tip3p.py: 97.60%

125 statements  

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

1"""TIP3P potential.""" 

2 

3import numpy as np 

4 

5import ase.units as units 

6from ase.calculators.calculator import Calculator, all_changes 

7 

8qH = 0.417 

9sigma0 = 3.15061 

10epsilon0 = 0.1521 * units.kcal / units.mol 

11rOH = 0.9572 

12angleHOH = 104.52 

13thetaHOH = 104.52 / 180 * np.pi # we keep this for backwards compatibility 

14 

15 

16class TIP3P(Calculator): 

17 implemented_properties = ['energy', 'forces'] 

18 nolabel = True 

19 pcpot = None 

20 

21 def __init__(self, rc=5.0, width=1.0): 

22 """TIP3P potential. 

23 

24 rc: float 

25 Cutoff radius for Coulomb part. 

26 width: float 

27 Width for cutoff function for Coulomb part. 

28 """ 

29 self.rc = rc 

30 self.width = width 

31 Calculator.__init__(self) 

32 self.sites_per_mol = 3 

33 

34 def calculate( 

35 self, atoms=None, properties=None, system_changes=all_changes 

36 ): 

37 if properties is None: 

38 properties = ['energy'] 

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

40 

41 R = self.atoms.positions.reshape((-1, 3, 3)) 

42 Z = self.atoms.numbers 

43 pbc = self.atoms.pbc 

44 cell = self.atoms.cell.diagonal() 

45 nh2o = len(R) 

46 

47 assert (self.atoms.cell == np.diag(cell)).all(), 'not orthorhombic' 

48 assert ((cell >= 2 * self.rc) | ~pbc).all(), 'cutoff too large' # ??? 

49 if Z[0] == 8: 

50 o = 0 

51 else: 

52 o = 2 

53 assert (Z[o::3] == 8).all() 

54 assert (Z[(o + 1) % 3 :: 3] == 1).all() 

55 assert (Z[(o + 2) % 3 :: 3] == 1).all() 

56 

57 charges = np.array([qH, qH, qH]) 

58 charges[o] *= -2 

59 

60 energy = 0.0 

61 forces = np.zeros((3 * nh2o, 3)) 

62 

63 for m in range(nh2o - 1): 

64 DOO = R[m + 1 :, o] - R[m, o] 

65 shift = np.zeros_like(DOO) 

66 for i, periodic in enumerate(pbc): 

67 if periodic: 

68 L = cell[i] 

69 shift[:, i] = (DOO[:, i] + L / 2) % L - L / 2 - DOO[:, i] 

70 DOO += shift 

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

72 d = d2**0.5 

73 x1 = d > self.rc - self.width 

74 x2 = d < self.rc 

75 x12 = np.logical_and(x1, x2) 

76 y = (d[x12] - self.rc + self.width) / self.width 

77 t = np.zeros(len(d)) # cutoff function 

78 t[x2] = 1.0 

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

80 dtdd = np.zeros(len(d)) 

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

82 c6 = (sigma0**2 / d2) ** 3 

83 c12 = c6**2 

84 e = 4 * epsilon0 * (c12 - c6) 

85 energy += np.dot(t, e) 

86 F = (24 * epsilon0 * (2 * c12 - c6) / d2 * t - e * dtdd / d)[ 

87 :, np.newaxis 

88 ] * DOO 

89 forces[m * 3 + o] -= F.sum(0) 

90 forces[m * 3 + 3 + o :: 3] += F 

91 

92 for j in range(3): 

93 D = R[m + 1 :] - R[m, j] + shift[:, np.newaxis] 

94 r2 = (D**2).sum(axis=2) 

95 r = r2**0.5 

96 e = charges[j] * charges / r * units.Hartree * units.Bohr 

97 energy += np.dot(t, e).sum() 

98 F = (e / r2 * t[:, np.newaxis])[:, :, np.newaxis] * D 

99 FOO = -(e.sum(1) * dtdd / d)[:, np.newaxis] * DOO 

100 forces[(m + 1) * 3 + o :: 3] += FOO 

101 forces[m * 3 + o] -= FOO.sum(0) 

102 forces[(m + 1) * 3 :] += F.reshape((-1, 3)) 

103 forces[m * 3 + j] -= F.sum(axis=0).sum(axis=0) 

104 

105 if self.pcpot: 

106 e, f = self.pcpot.calculate( 

107 np.tile(charges, nh2o), self.atoms.positions 

108 ) 

109 energy += e 

110 forces += f 

111 

112 self.results['energy'] = energy 

113 self.results['forces'] = forces 

114 

115 def embed(self, charges): 

116 """Embed atoms in point-charges.""" 

117 self.pcpot = PointChargePotential(charges) 

118 return self.pcpot 

119 

120 def check_state(self, atoms, tol=1e-15): 

121 system_changes = Calculator.check_state(self, atoms, tol) 

122 if self.pcpot and self.pcpot.mmpositions is not None: 

123 system_changes.append('positions') 

124 return system_changes 

125 

126 def add_virtual_sites(self, positions): 

127 return positions # no virtual sites 

128 

129 def redistribute_forces(self, forces): 

130 return forces 

131 

132 def get_virtual_charges(self, atoms): 

133 charges = np.empty(len(atoms)) 

134 charges[:] = qH 

135 if atoms.numbers[0] == 8: 

136 charges[::3] = -2 * qH 

137 else: 

138 charges[2::3] = -2 * qH 

139 return charges 

140 

141 

142class PointChargePotential: 

143 def __init__(self, mmcharges): 

144 """Point-charge potential for TIP3P. 

145 

146 Only used for testing QMMM. 

147 """ 

148 self.mmcharges = mmcharges 

149 self.mmpositions = None 

150 self.mmforces = None 

151 

152 def set_positions(self, mmpositions, com_pv=None): 

153 self.mmpositions = mmpositions 

154 

155 def calculate(self, qmcharges, qmpositions): 

156 energy = 0.0 

157 self.mmforces = np.zeros_like(self.mmpositions) 

158 qmforces = np.zeros_like(qmpositions) 

159 for C, R, F in zip(self.mmcharges, self.mmpositions, self.mmforces): 

160 d = qmpositions - R 

161 r2 = (d**2).sum(1) 

162 e = units.Hartree * units.Bohr * C * r2**-0.5 * qmcharges 

163 energy += e.sum() 

164 f = (e / r2)[:, np.newaxis] * d 

165 qmforces += f 

166 F -= f.sum(0) 

167 self.mmpositions = None 

168 return energy, qmforces 

169 

170 def get_forces(self, calc): 

171 return self.mmforces