Coverage for ase/calculators/acn.py: 93.29%

164 statements  

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

1import numpy as np 

2 

3import ase.units as units 

4from ase.calculators.calculator import Calculator, all_changes 

5from ase.data import atomic_masses 

6from ase.geometry import find_mic 

7 

8# Electrostatic constant 

9k_c = units.Hartree * units.Bohr 

10 

11# Force field parameters 

12q_me = 0.206 

13q_c = 0.247 

14q_n = -0.453 

15sigma_me = 3.775 

16sigma_c = 3.650 

17sigma_n = 3.200 

18epsilon_me = 0.7824 * units.kJ / units.mol 

19epsilon_c = 0.544 * units.kJ / units.mol 

20epsilon_n = 0.6276 * units.kJ / units.mol 

21r_mec = 1.458 

22r_cn = 1.157 

23r_men = r_mec + r_cn 

24m_me = atomic_masses[6] + 3 * atomic_masses[1] 

25m_c = atomic_masses[6] 

26m_n = atomic_masses[7] 

27 

28 

29def combine_lj_lorenz_berthelot(sigma, epsilon): 

30 """Combine LJ parameters according to the Lorenz-Berthelot rule""" 

31 sigma_c = np.zeros((len(sigma), len(sigma))) 

32 epsilon_c = np.zeros_like(sigma_c) 

33 

34 for ii in range(len(sigma)): 

35 sigma_c[:, ii] = (sigma[ii] + sigma) / 2 

36 epsilon_c[:, ii] = (epsilon[ii] * epsilon) ** 0.5 

37 return sigma_c, epsilon_c 

38 

39 

40class ACN(Calculator): 

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

42 nolabel = True 

43 

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

45 """Three-site potential for acetonitrile. 

46 

47 Atom sequence must be: 

48 MeCNMeCN ... MeCN or NCMeNCMe ... NCMe 

49 

50 When performing molecular dynamics (MD), forces are redistributed 

51 and only Me and N sites propagated based on a scheme for MD of 

52 rigid triatomic molecules from Ciccotti et al. Molecular Physics 

53 1982 (https://doi.org/10.1080/00268978200100942). Apply constraints 

54 using the FixLinearTriatomic to fix the geometry of the acetonitrile 

55 molecules. 

56 

57 rc: float 

58 Cutoff radius for Coulomb interactions. 

59 width: float 

60 Width for cutoff function for Coulomb interactions. 

61 

62 References 

63 ---------- 

64 

65 https://doi.org/10.1080/08927020108024509 

66 """ 

67 self.rc = rc 

68 self.width = width 

69 self.forces = None 

70 Calculator.__init__(self) 

71 self.sites_per_mol = 3 

72 self.pcpot = None 

73 

74 def calculate( 

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

76 ): 

77 if properties is None: 

78 properties = ['energy'] 

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

80 

81 Z = atoms.numbers 

82 masses = atoms.get_masses() 

83 if Z[0] == 7: 

84 n = 0 

85 me = 2 

86 sigma = np.array([sigma_n, sigma_c, sigma_me]) 

87 epsilon = np.array([epsilon_n, epsilon_c, epsilon_me]) 

88 else: 

89 n = 2 

90 me = 0 

91 sigma = np.array([sigma_me, sigma_c, sigma_n]) 

92 epsilon = np.array([epsilon_me, epsilon_c, epsilon_n]) 

93 assert (Z[n::3] == 7).all(), 'incorrect atoms sequence' 

94 assert (Z[1::3] == 6).all(), 'incorrect atoms sequence' 

95 assert (masses[n::3] == m_n).all(), 'incorrect masses' 

96 assert (masses[1::3] == m_c).all(), 'incorrect masses' 

97 assert (masses[me::3] == m_me).all(), 'incorrect masses' 

98 

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

100 pbc = self.atoms.pbc 

101 cd = self.atoms.cell.diagonal() 

102 nm = len(R) 

103 

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

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

106 

107 charges = self.get_virtual_charges(atoms[:3]) 

108 

109 # LJ parameters 

110 sigma_co, epsilon_co = combine_lj_lorenz_berthelot(sigma, epsilon) 

111 

112 energy = 0.0 

113 self.forces = np.zeros((3 * nm, 3)) 

114 

115 for m in range(nm - 1): 

116 Dmm = R[m + 1 :, 1] - R[m, 1] 

117 # MIC PBCs 

118 Dmm_min, Dmm_min_len = find_mic(Dmm, atoms.cell, pbc) 

119 shift = Dmm_min - Dmm 

120 

121 # Smooth cutoff 

122 cut, dcut = self.cutoff(Dmm_min_len) 

123 

124 for j in range(3): 

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

126 D_len2 = (D**2).sum(axis=2) 

127 D_len = D_len2**0.5 

128 # Coulomb interactions 

129 e = charges[j] * charges / D_len * k_c 

130 energy += np.dot(cut, e).sum() 

131 F = (e / D_len2 * cut[:, np.newaxis])[:, :, np.newaxis] * D 

132 Fmm = -(e.sum(1) * dcut / Dmm_min_len)[:, np.newaxis] * Dmm_min 

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

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

135 self.forces[(m + 1) * 3 + 1 :: 3] += Fmm 

136 self.forces[m * 3 + 1] -= Fmm.sum(0) 

137 # LJ interactions 

138 c6 = (sigma_co[:, j] ** 2 / D_len2) ** 3 

139 c12 = c6**2 

140 e = 4 * epsilon_co[:, j] * (c12 - c6) 

141 energy += np.dot(cut, e).sum() 

142 F = ( 

143 24 

144 * epsilon_co[:, j] 

145 * (2 * c12 - c6) 

146 / D_len2 

147 * cut[:, np.newaxis] 

148 )[:, :, np.newaxis] * D 

149 Fmm = -(e.sum(1) * dcut / Dmm_min_len)[:, np.newaxis] * Dmm_min 

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

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

152 self.forces[(m + 1) * 3 + 1 :: 3] += Fmm 

153 self.forces[m * 3 + 1] -= Fmm.sum(0) 

154 

155 if self.pcpot: 

156 e, f = self.pcpot.calculate( 

157 np.tile(charges, nm), self.atoms.positions 

158 ) 

159 energy += e 

160 self.forces += f 

161 

162 self.results['energy'] = energy 

163 self.results['forces'] = self.forces 

164 

165 def redistribute_forces(self, forces): 

166 return forces 

167 

168 def get_molcoms(self, nm): 

169 molcoms = np.zeros((nm, 3)) 

170 for m in range(nm): 

171 molcoms[m] = self.atoms[m * 3 : (m + 1) * 3].get_center_of_mass() 

172 return molcoms 

173 

174 def cutoff(self, d): 

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

176 x2 = d < self.rc 

177 x12 = np.logical_and(x1, x2) 

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

179 cut = np.zeros(len(d)) # cutoff function 

180 cut[x2] = 1.0 

181 cut[x12] -= y**2 * (3.0 - 2.0 * y) 

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

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

184 return cut, dtdd 

185 

186 def embed(self, charges): 

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

188 self.pcpot = PointChargePotential(charges) 

189 return self.pcpot 

190 

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

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

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

194 system_changes.append('positions') 

195 return system_changes 

196 

197 def add_virtual_sites(self, positions): 

198 return positions # no virtual sites 

199 

200 def get_virtual_charges(self, atoms): 

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

202 Z = atoms.numbers 

203 if Z[0] == 7: 

204 n = 0 

205 me = 2 

206 else: 

207 n = 2 

208 me = 0 

209 charges[me::3] = q_me 

210 charges[1::3] = q_c 

211 charges[n::3] = q_n 

212 return charges 

213 

214 

215class PointChargePotential: 

216 def __init__(self, mmcharges): 

217 """Point-charge potential for ACN. 

218 

219 Only used for testing QMMM. 

220 """ 

221 self.mmcharges = mmcharges 

222 self.mmpositions = None 

223 self.mmforces = None 

224 

225 def set_positions(self, mmpositions): 

226 self.mmpositions = mmpositions 

227 

228 def calculate(self, qmcharges, qmpositions): 

229 energy = 0.0 

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

231 qmforces = np.zeros_like(qmpositions) 

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

233 d = qmpositions - R 

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

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

236 energy += e.sum() 

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

238 qmforces += f 

239 F -= f.sum(0) 

240 self.mmpositions = None 

241 return energy, qmforces 

242 

243 def get_forces(self, calc): 

244 return self.mmforces