Coverage for ase/calculators/emt.py: 98.37%

184 statements  

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

1"""Effective medium theory potential.""" 

2 

3from collections import defaultdict 

4from math import log, sqrt 

5 

6import numpy as np 

7 

8from ase.calculators.calculator import ( 

9 Calculator, 

10 PropertyNotImplementedError, 

11 all_changes, 

12) 

13from ase.data import atomic_numbers, chemical_symbols 

14from ase.neighborlist import NeighborList 

15from ase.units import Bohr 

16 

17parameters = { 

18 # E0 s0 V0 eta2 kappa lambda n0 

19 # eV bohr eV bohr^-1 bohr^-1 bohr^-1 bohr^-3 

20 'Al': (-3.28, 3.00, 1.493, 1.240, 2.000, 1.169, 0.00700), 

21 'Cu': (-3.51, 2.67, 2.476, 1.652, 2.740, 1.906, 0.00910), 

22 'Ag': (-2.96, 3.01, 2.132, 1.652, 2.790, 1.892, 0.00547), 

23 'Au': (-3.80, 3.00, 2.321, 1.674, 2.873, 2.182, 0.00703), 

24 'Ni': (-4.44, 2.60, 3.673, 1.669, 2.757, 1.948, 0.01030), 

25 'Pd': (-3.90, 2.87, 2.773, 1.818, 3.107, 2.155, 0.00688), 

26 'Pt': (-5.85, 2.90, 4.067, 1.812, 3.145, 2.192, 0.00802), 

27 # extra parameters - just for fun ... 

28 'H': (-3.21, 1.31, 0.132, 2.652, 2.790, 3.892, 0.00547), 

29 'C': (-3.50, 1.81, 0.332, 1.652, 2.790, 1.892, 0.01322), 

30 'N': (-5.10, 1.88, 0.132, 1.652, 2.790, 1.892, 0.01222), 

31 'O': (-4.60, 1.95, 0.332, 1.652, 2.790, 1.892, 0.00850), 

32} 

33 

34beta = 1.809 # (16 * pi / 3)**(1.0 / 3) / 2**0.5, preserve historical rounding 

35 

36 

37class EMT(Calculator): 

38 """Python implementation of the Effective Medium Potential. 

39 

40 Supports the following standard EMT metals: 

41 Al, Cu, Ag, Au, Ni, Pd and Pt. 

42 

43 In addition, the following elements are supported. 

44 They are NOT well described by EMT, and the parameters 

45 are not for any serious use: 

46 H, C, N, O 

47 

48 Parameters 

49 ---------- 

50 asap_cutoff : bool, default: False 

51 If True the cutoff mimics how ASAP does it; most importantly the global 

52 cutoff is chosen from the largest atom present in the simulation. 

53 If False it is chosen from the largest atom in the parameter table. 

54 True gives the behaviour of ASAP and older EMT implementations, 

55 although the results are not bitwise identical. 

56 

57 Notes 

58 ----- 

59 Formulation mostly follows Jacobsen *et al*. [1]_ 

60 `Documentation in ASAP can also be referred to <https://gitlab.com/asap/ 

61 asap/blob/master/docs/manual/potentials/emt.pdf>`_. 

62 

63 .. [1] K. W. Jacobsen, P. Stoltze, and J. K. Nørskov, 

64 Surf. Sci. 366, 394 (1996). 

65 """ 

66 

67 implemented_properties = [ 

68 'energy', 

69 'free_energy', 

70 'energies', 

71 'forces', 

72 'stress', 

73 'magmom', 

74 'magmoms', 

75 ] 

76 

77 nolabel = True 

78 

79 default_parameters = {'asap_cutoff': False} 

80 

81 def __init__(self, **kwargs): 

82 Calculator.__init__(self, **kwargs) 

83 

84 def initialize(self, atoms): 

85 self.rc, self.rc_list, self.acut = self._calc_cutoff(atoms) 

86 

87 numbers = atoms.get_atomic_numbers() 

88 

89 # ia2iz : map from idx of atoms to idx of atomic numbers in self.par 

90 unique_numbers, self.ia2iz = np.unique(numbers, return_inverse=True) 

91 self.par = defaultdict(lambda: np.empty(len(unique_numbers))) 

92 for i, Z in enumerate(unique_numbers): 

93 sym = chemical_symbols[Z] 

94 if sym not in parameters: 

95 raise NotImplementedError(f'No EMT-potential for {sym}') 

96 p = parameters[sym] 

97 s0 = p[1] * Bohr 

98 eta2 = p[3] / Bohr 

99 kappa = p[4] / Bohr 

100 gamma1, gamma2 = self._calc_gammas(s0, eta2, kappa) 

101 self.par['Z'][i] = Z 

102 self.par['E0'][i] = p[0] 

103 self.par['s0'][i] = s0 

104 self.par['V0'][i] = p[2] 

105 self.par['eta2'][i] = eta2 

106 self.par['kappa'][i] = kappa 

107 self.par['lambda'][i] = p[5] / Bohr 

108 self.par['n0'][i] = p[6] / Bohr**3 

109 self.par['inv12gamma1'][i] = 1.0 / (12.0 * gamma1) 

110 self.par['neghalfv0overgamma2'][i] = -0.5 * p[2] / gamma2 

111 

112 self.chi = self.par['n0'][None, :] / self.par['n0'][:, None] 

113 

114 self.energies = np.empty(len(atoms)) 

115 self.forces = np.empty((len(atoms), 3)) 

116 self.stress = np.empty((3, 3)) 

117 self.deds = np.empty(len(atoms)) 

118 

119 self.nl = NeighborList( 

120 [0.5 * self.rc_list] * len(atoms), 

121 self_interaction=False, 

122 bothways=True, 

123 ) 

124 

125 def _calc_cutoff(self, atoms): 

126 """Calculate parameters of the logistic smoothing function etc. 

127 

128 The logistic smoothing function is given by 

129 

130 .. math: 

131 

132 w(r) = \\frac{1}{1 + \\exp a (r - r_\\mathrm{c})} 

133 

134 Returns 

135 ------- 

136 rc : float 

137 "Midpoint" of the logistic smoothing function, set to be the mean 

138 of the 3rd and the 4th nearest-neighbor distances in FCC. 

139 rc_list : float 

140 Cutoff radius for the neighbor search, set to be slightly larger 

141 than ``rc`` depending on ``asap_cutoff``. 

142 acut : float 

143 "Slope" of the smoothing function, set for the smoothing function 

144 value to be ``1e-4`` at the 4th nearest-neighbor distance in FCC. 

145 

146 Notes 

147 ----- 

148 ``maxseq`` is the present FCC Wigner-Seitz radius. ``beta * maxseq`` 

149 (`r1nn`) is the corresponding 1st nearest-neighbor distance in FCC. 

150 The 2nd, 3rd, 4th nearest-neighbor distances in FCC are given using 

151 ``r1nn`` by ``sqrt(2) * r1nn``, ``sqrt(3) * r1nn``, ``sqrt(4) * r1nn``, 

152 respectively. 

153 """ 

154 numbers = atoms.get_atomic_numbers() 

155 if self.parameters['asap_cutoff']: 

156 relevant_pars = { 

157 symb: p 

158 for symb, p in parameters.items() 

159 if atomic_numbers[symb] in numbers 

160 } 

161 else: 

162 relevant_pars = parameters 

163 maxseq = max(par[1] for par in relevant_pars.values()) * Bohr 

164 r1nn = beta * maxseq # 1st NN distance in FCC 

165 rc = r1nn * 0.5 * (sqrt(3.0) + 2.0) # mean of 3NN and 4NN dists. 

166 r4nn = r1nn * 2.0 # 4NN distance in FCC 

167 eps = 1e-4 # value at r4nn, should be small 

168 

169 # "slope" is set so that the function value becomes eps at r4nn 

170 acut = log(1.0 / eps - 1.0) / (r4nn - rc) 

171 

172 rc_list = rc * 1.045 if self.parameters['asap_cutoff'] else rc + 0.5 

173 

174 return rc, rc_list, acut 

175 

176 def _calc_gammas(self, s0, eta2, kappa): 

177 n = np.array([12, 6, 24]) # numbers of 1, 2, 3NN sites in fcc 

178 r = beta * s0 * np.sqrt([1.0, 2.0, 3.0]) # distances of 1, 2, 3NNs 

179 w = 1.0 / (1.0 + np.exp(self.acut * (r - self.rc))) 

180 x = n * w / 12.0 

181 gamma1 = x @ np.exp(-eta2 * (r - beta * s0)) 

182 gamma2 = x @ np.exp(-kappa / beta * (r - beta * s0)) 

183 return gamma1, gamma2 

184 

185 def calculate( 

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

187 ): 

188 if properties is None: 

189 properties = ['energy'] 

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

191 

192 if 'numbers' in system_changes: 

193 self.initialize(self.atoms) 

194 

195 self.nl.update(self.atoms) 

196 

197 self.energies[:] = 0.0 

198 self.forces[:] = 0.0 

199 self.stress[:] = 0.0 

200 self.deds[:] = 0.0 

201 

202 natoms = len(self.atoms) 

203 

204 # store nearest neighbor info for all the atoms 

205 # suffixes 's' and 'o': contributions from self and the other atoms 

206 ps = {} 

207 for a1 in range(natoms): 

208 a2, d, r = self._get_neighbors(a1) 

209 if len(a2) == 0: 

210 continue 

211 w, dwdroverw = self._calc_theta(r) 

212 dsigma1s, dsigma1o = self._calc_dsigma1(a1, a2, r, w) 

213 dsigma2s, dsigma2o = self._calc_dsigma2(a1, a2, r, w) 

214 ps[a1] = { 

215 'a2': a2, 

216 'd': d, 

217 'r': r, 

218 'invr': 1.0 / r, 

219 'w': w, 

220 'dwdroverw': dwdroverw, 

221 'dsigma1s': dsigma1s, 

222 'dsigma1o': dsigma1o, 

223 'dsigma2s': dsigma2s, 

224 'dsigma2o': dsigma2o, 

225 } 

226 

227 # deds is computed in _calc_e_c_a2 

228 # since deds for all the atoms are used later in _calc_f_c_a2, 

229 # _calc_e_c_a2 must be called beforehand for all the atoms 

230 for a1, p in ps.items(): 

231 a2 = p['a2'] 

232 dsigma1s = p['dsigma1s'] 

233 self._calc_e_c_a2(a1, dsigma1s) 

234 

235 for a1, p in ps.items(): 

236 a2 = p['a2'] 

237 d = p['d'] 

238 invr = p['invr'] 

239 dwdroverw = p['dwdroverw'] 

240 dsigma1s = p['dsigma1s'] 

241 dsigma1o = p['dsigma1o'] 

242 dsigma2s = p['dsigma2s'] 

243 dsigma2o = p['dsigma2o'] 

244 self._calc_fs_c_a2(a1, a2, d, invr, dwdroverw, dsigma1s, dsigma1o) 

245 self._calc_efs_a1(a1, a2, d, invr, dwdroverw, dsigma2s, dsigma2o) 

246 

247 # subtract E0 (ASAP convention) 

248 self.energies -= self.par['E0'][self.ia2iz] 

249 

250 energy = np.add.reduce(self.energies, axis=0) 

251 self.results['energy'] = self.results['free_energy'] = energy 

252 self.results['energies'] = self.energies 

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

254 

255 if self.atoms.cell.rank == 3: 

256 self.stress = (self.stress + self.stress.T) * 0.5 # symmetrize 

257 self.stress /= self.atoms.get_volume() 

258 self.results['stress'] = self.stress.flat[[0, 4, 8, 5, 2, 1]] 

259 elif 'stress' in properties: 

260 raise PropertyNotImplementedError 

261 

262 def _get_neighbors(self, a1): 

263 positions = self.atoms.positions 

264 cell = self.atoms.cell 

265 neighbors, offsets = self.nl.get_neighbors(a1) 

266 offsets = np.dot(offsets, cell) 

267 d = positions[neighbors] + offsets - positions[a1] 

268 r = np.sqrt(np.add.reduce(d**2, axis=1)) 

269 mask = r < self.rc_list 

270 return neighbors[mask], d[mask], r[mask] 

271 

272 def _calc_theta(self, r): 

273 """Calculate cutoff function and its r derivative""" 

274 w = 1.0 / (1.0 + np.exp(self.acut * (r - self.rc))) 

275 dwdroverw = self.acut * (w - 1.0) 

276 return w, dwdroverw 

277 

278 def _calc_dsigma1(self, a1, a2, r, w): 

279 """Calculate contributions of neighbors to sigma1""" 

280 s0s = self.par['s0'][self.ia2iz[a1]] 

281 s0o = self.par['s0'][self.ia2iz[a2]] 

282 eta2s = self.par['eta2'][self.ia2iz[a1]] 

283 eta2o = self.par['eta2'][self.ia2iz[a2]] 

284 chi = self.chi[self.ia2iz[a1], self.ia2iz[a2]] 

285 

286 dsigma1s = np.exp(-eta2o * (r - beta * s0o)) * chi * w 

287 dsigma1o = np.exp(-eta2s * (r - beta * s0s)) / chi * w 

288 

289 return dsigma1s, dsigma1o 

290 

291 def _calc_dsigma2(self, a1, a2, r, w): 

292 """Calculate contributions of neighbors to sigma2""" 

293 s0s = self.par['s0'][self.ia2iz[a1]] 

294 s0o = self.par['s0'][self.ia2iz[a2]] 

295 kappas = self.par['kappa'][self.ia2iz[a1]] 

296 kappao = self.par['kappa'][self.ia2iz[a2]] 

297 chi = self.chi[self.ia2iz[a1], self.ia2iz[a2]] 

298 

299 dsigma2s = np.exp(-kappao * (r / beta - s0o)) * chi * w 

300 dsigma2o = np.exp(-kappas * (r / beta - s0s)) / chi * w 

301 

302 return dsigma2s, dsigma2o 

303 

304 def _calc_e_c_a2(self, a1, dsigma1s): 

305 """Calculate E_c and the second term of E_AS and their s derivatives""" 

306 e0s = self.par['E0'][self.ia2iz[a1]] 

307 v0s = self.par['V0'][self.ia2iz[a1]] 

308 eta2s = self.par['eta2'][self.ia2iz[a1]] 

309 lmds = self.par['lambda'][self.ia2iz[a1]] 

310 kappas = self.par['kappa'][self.ia2iz[a1]] 

311 inv12gamma1s = self.par['inv12gamma1'][self.ia2iz[a1]] 

312 

313 sigma1 = np.add.reduce(dsigma1s) 

314 ds = -1.0 * np.log(sigma1 * inv12gamma1s) / (beta * eta2s) 

315 

316 lmdsds = lmds * ds 

317 expneglmdds = np.exp(-1.0 * lmdsds) 

318 self.energies[a1] += e0s * (1.0 + lmdsds) * expneglmdds 

319 self.deds[a1] += -1.0 * e0s * lmds * lmdsds * expneglmdds 

320 

321 sixv0expnegkppds = 6.0 * v0s * np.exp(-1.0 * kappas * ds) 

322 self.energies[a1] += sixv0expnegkppds 

323 self.deds[a1] += -1.0 * kappas * sixv0expnegkppds 

324 

325 self.deds[a1] /= -1.0 * beta * eta2s * sigma1 # factor from ds/dr 

326 

327 def _calc_efs_a1(self, a1, a2, d, invr, dwdroverw, dsigma2s, dsigma2o): 

328 """Calculate the first term of E_AS and derivatives""" 

329 neghalfv0overgamma2s = self.par['neghalfv0overgamma2'][self.ia2iz[a1]] 

330 neghalfv0overgamma2o = self.par['neghalfv0overgamma2'][self.ia2iz[a2]] 

331 kappas = self.par['kappa'][self.ia2iz[a1]] 

332 kappao = self.par['kappa'][self.ia2iz[a2]] 

333 

334 es = neghalfv0overgamma2s * dsigma2s 

335 eo = neghalfv0overgamma2o * dsigma2o 

336 self.energies[a1] += 0.5 * np.add.reduce(es + eo, axis=0) 

337 

338 dedrs = es * (dwdroverw - kappao / beta) 

339 dedro = eo * (dwdroverw - kappas / beta) 

340 f = ((dedrs + dedro) * invr)[:, None] * d 

341 self.forces[a1] += np.add.reduce(f, axis=0) 

342 self.stress += 0.5 * np.dot(d.T, f) # compensate double counting 

343 

344 def _calc_fs_c_a2(self, a1, a2, d, invr, dwdroverw, dsigma1s, dsigma1o): 

345 """Calculate forces and stress from E_c and the second term of E_AS""" 

346 eta2s = self.par['eta2'][self.ia2iz[a1]] 

347 eta2o = self.par['eta2'][self.ia2iz[a2]] 

348 

349 ddsigma1sdr = dsigma1s * (dwdroverw - eta2o) 

350 ddsigma1odr = dsigma1o * (dwdroverw - eta2s) 

351 dedrs = self.deds[a1] * ddsigma1sdr 

352 dedro = self.deds[a2] * ddsigma1odr 

353 f = ((dedrs + dedro) * invr)[:, None] * d 

354 self.forces[a1] += np.add.reduce(f, axis=0) 

355 self.stress += 0.5 * np.dot(d.T, f) # compensate double counting