PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/ase/calculators/emt.py

https://gitlab.com/gkastlun/ase
Python | 254 lines | 198 code | 33 blank | 23 comment | 27 complexity | a850bb657ecb03bbdecf6fa8340b4665 MD5 | raw file
  1. """Effective medium theory potential."""
  2. from math import sqrt, exp, log
  3. import numpy as np
  4. from ase.data import chemical_symbols, atomic_numbers
  5. from ase.units import Bohr
  6. from ase.neighborlist import NeighborList
  7. from ase.calculators.calculator import (Calculator, all_changes,
  8. PropertyNotImplementedError)
  9. parameters = {
  10. # E0 s0 V0 eta2 kappa lambda n0
  11. # eV bohr eV bohr^-1 bohr^-1 bohr^-1 bohr^-3
  12. 'Al': (-3.28, 3.00, 1.493, 1.240, 2.000, 1.169, 0.00700),
  13. 'Cu': (-3.51, 2.67, 2.476, 1.652, 2.740, 1.906, 0.00910),
  14. 'Ag': (-2.96, 3.01, 2.132, 1.652, 2.790, 1.892, 0.00547),
  15. 'Au': (-3.80, 3.00, 2.321, 1.674, 2.873, 2.182, 0.00703),
  16. 'Ni': (-4.44, 2.60, 3.673, 1.669, 2.757, 1.948, 0.01030),
  17. 'Pd': (-3.90, 2.87, 2.773, 1.818, 3.107, 2.155, 0.00688),
  18. 'Pt': (-5.85, 2.90, 4.067, 1.812, 3.145, 2.192, 0.00802),
  19. # extra parameters - just for fun ...
  20. 'H': (-3.21, 1.31, 0.132, 2.652, 2.790, 3.892, 0.00547),
  21. 'C': (-3.50, 1.81, 0.332, 1.652, 2.790, 1.892, 0.01322),
  22. 'N': (-5.10, 1.88, 0.132, 1.652, 2.790, 1.892, 0.01222),
  23. 'O': (-4.60, 1.95, 0.332, 1.652, 2.790, 1.892, 0.00850)}
  24. beta = 1.809 # (16 * pi / 3)**(1.0 / 3) / 2**0.5, preserve historical rounding
  25. class EMT(Calculator):
  26. """Python implementation of the Effective Medium Potential.
  27. Supports the following standard EMT metals:
  28. Al, Cu, Ag, Au, Ni, Pd and Pt.
  29. In addition, the following elements are supported.
  30. They are NOT well described by EMT, and the parameters
  31. are not for any serious use:
  32. H, C, N, O
  33. The potential takes a single argument, ``asap_cutoff``
  34. (default: False). If set to True, the cutoff mimics
  35. how Asap does it; most importantly the global cutoff
  36. is chosen from the largest atom present in the simulation,
  37. if False it is chosen from the largest atom in the parameter
  38. table. True gives the behaviour of the Asap code and
  39. older EMT implementations, although the results are not
  40. bitwise identical.
  41. """
  42. implemented_properties = ['energy', 'free_energy', 'energies', 'forces',
  43. 'stress', 'magmom', 'magmoms']
  44. nolabel = True
  45. default_parameters = {'asap_cutoff': False}
  46. def __init__(self, **kwargs):
  47. Calculator.__init__(self, **kwargs)
  48. def initialize(self, atoms):
  49. self.par = {}
  50. self.rc = 0.0
  51. self.numbers = atoms.get_atomic_numbers()
  52. if self.parameters.asap_cutoff:
  53. relevant_pars = {}
  54. for symb, p in parameters.items():
  55. if atomic_numbers[symb] in self.numbers:
  56. relevant_pars[symb] = p
  57. else:
  58. relevant_pars = parameters
  59. maxseq = max(par[1] for par in relevant_pars.values()) * Bohr
  60. rc = self.rc = beta * maxseq * 0.5 * (sqrt(3) + sqrt(4))
  61. rr = rc * 2 * sqrt(4) / (sqrt(3) + sqrt(4))
  62. self.acut = np.log(9999.0) / (rr - rc)
  63. if self.parameters.asap_cutoff:
  64. self.rc_list = self.rc * 1.045
  65. else:
  66. self.rc_list = self.rc + 0.5
  67. for Z in self.numbers:
  68. if Z not in self.par:
  69. sym = chemical_symbols[Z]
  70. if sym not in parameters:
  71. raise NotImplementedError('No EMT-potential for {0}'
  72. .format(sym))
  73. p = parameters[sym]
  74. s0 = p[1] * Bohr
  75. eta2 = p[3] / Bohr
  76. kappa = p[4] / Bohr
  77. x = eta2 * beta * s0
  78. gamma1 = 0.0
  79. gamma2 = 0.0
  80. for i, n in enumerate([12, 6, 24]):
  81. r = s0 * beta * sqrt(i + 1)
  82. x = n / (12 * (1.0 + exp(self.acut * (r - rc))))
  83. gamma1 += x * exp(-eta2 * (r - beta * s0))
  84. gamma2 += x * exp(-kappa / beta * (r - beta * s0))
  85. self.par[Z] = {'E0': p[0],
  86. 's0': s0,
  87. 'V0': p[2],
  88. 'eta2': eta2,
  89. 'kappa': kappa,
  90. 'lambda': p[5] / Bohr,
  91. 'n0': p[6] / Bohr**3,
  92. 'rc': rc,
  93. 'gamma1': gamma1,
  94. 'gamma2': gamma2}
  95. self.ksi = {}
  96. for s1, p1 in self.par.items():
  97. self.ksi[s1] = {}
  98. for s2, p2 in self.par.items():
  99. self.ksi[s1][s2] = p2['n0'] / p1['n0']
  100. self.energies = np.empty(len(atoms))
  101. self.forces = np.empty((len(atoms), 3))
  102. self.stress = np.empty((3, 3))
  103. self.sigma1 = np.empty(len(atoms))
  104. self.deds = np.empty(len(atoms))
  105. self.nl = NeighborList([0.5 * self.rc_list] * len(atoms),
  106. self_interaction=False)
  107. def calculate(self, atoms=None, properties=['energy'],
  108. system_changes=all_changes):
  109. Calculator.calculate(self, atoms, properties, system_changes)
  110. if 'numbers' in system_changes:
  111. self.initialize(self.atoms)
  112. positions = self.atoms.positions
  113. numbers = self.atoms.numbers
  114. cell = self.atoms.cell
  115. self.nl.update(self.atoms)
  116. self.energy = 0.0
  117. self.energies[:] = 0
  118. self.sigma1[:] = 0.0
  119. self.forces[:] = 0.0
  120. self.stress[:] = 0.0
  121. natoms = len(self.atoms)
  122. for a1 in range(natoms):
  123. Z1 = numbers[a1]
  124. p1 = self.par[Z1]
  125. ksi = self.ksi[Z1]
  126. neighbors, offsets = self.nl.get_neighbors(a1)
  127. offsets = np.dot(offsets, cell)
  128. for a2, offset in zip(neighbors, offsets):
  129. d = positions[a2] + offset - positions[a1]
  130. r = sqrt(np.dot(d, d))
  131. if r < self.rc_list:
  132. Z2 = numbers[a2]
  133. p2 = self.par[Z2]
  134. self.interact1(a1, a2, d, r, p1, p2, ksi[Z2])
  135. for a in range(natoms):
  136. Z = numbers[a]
  137. p = self.par[Z]
  138. try:
  139. ds = -log(self.sigma1[a] / 12) / (beta * p['eta2'])
  140. except (OverflowError, ValueError):
  141. self.deds[a] = 0.0
  142. self.energy -= p['E0']
  143. self.energies[a] -= p['E0']
  144. continue
  145. x = p['lambda'] * ds
  146. y = exp(-x)
  147. z = 6 * p['V0'] * exp(-p['kappa'] * ds)
  148. self.deds[a] = ((x * y * p['E0'] * p['lambda'] + p['kappa'] * z) /
  149. (self.sigma1[a] * beta * p['eta2']))
  150. E = p['E0'] * ((1 + x) * y - 1) + z
  151. self.energy += E
  152. self.energies[a] += E
  153. for a1 in range(natoms):
  154. Z1 = numbers[a1]
  155. p1 = self.par[Z1]
  156. ksi = self.ksi[Z1]
  157. neighbors, offsets = self.nl.get_neighbors(a1)
  158. offsets = np.dot(offsets, cell)
  159. for a2, offset in zip(neighbors, offsets):
  160. d = positions[a2] + offset - positions[a1]
  161. r = sqrt(np.dot(d, d))
  162. if r < self.rc_list:
  163. Z2 = numbers[a2]
  164. p2 = self.par[Z2]
  165. self.interact2(a1, a2, d, r, p1, p2, ksi[Z2])
  166. self.results['energy'] = self.energy
  167. self.results['energies'] = self.energies
  168. self.results['free_energy'] = self.energy
  169. self.results['forces'] = self.forces
  170. if 'stress' in properties:
  171. if self.atoms.cell.rank == 3:
  172. self.stress += self.stress.T.copy()
  173. self.stress *= -0.5 / self.atoms.get_volume()
  174. self.results['stress'] = self.stress.flat[[0, 4, 8, 5, 2, 1]]
  175. else:
  176. raise PropertyNotImplementedError
  177. def interact1(self, a1, a2, d, r, p1, p2, ksi):
  178. x = exp(self.acut * (r - self.rc))
  179. theta = 1.0 / (1.0 + x)
  180. y1 = (0.5 * p1['V0'] * exp(-p2['kappa'] * (r / beta - p2['s0'])) *
  181. ksi / p1['gamma2'] * theta)
  182. y2 = (0.5 * p2['V0'] * exp(-p1['kappa'] * (r / beta - p1['s0'])) /
  183. ksi / p2['gamma2'] * theta)
  184. self.energy -= y1 + y2
  185. self.energies[a1] -= (y1 + y2) / 2
  186. self.energies[a2] -= (y1 + y2) / 2
  187. f = ((y1 * p2['kappa'] + y2 * p1['kappa']) / beta +
  188. (y1 + y2) * self.acut * theta * x) * d / r
  189. self.forces[a1] += f
  190. self.forces[a2] -= f
  191. self.stress -= np.outer(f, d)
  192. self.sigma1[a1] += (exp(-p2['eta2'] * (r - beta * p2['s0'])) *
  193. ksi * theta / p1['gamma1'])
  194. self.sigma1[a2] += (exp(-p1['eta2'] * (r - beta * p1['s0'])) /
  195. ksi * theta / p2['gamma1'])
  196. def interact2(self, a1, a2, d, r, p1, p2, ksi):
  197. x = exp(self.acut * (r - self.rc))
  198. theta = 1.0 / (1.0 + x)
  199. y1 = (exp(-p2['eta2'] * (r - beta * p2['s0'])) *
  200. ksi / p1['gamma1'] * theta * self.deds[a1])
  201. y2 = (exp(-p1['eta2'] * (r - beta * p1['s0'])) /
  202. ksi / p2['gamma1'] * theta * self.deds[a2])
  203. f = ((y1 * p2['eta2'] + y2 * p1['eta2']) +
  204. (y1 + y2) * self.acut * theta * x) * d / r
  205. self.forces[a1] -= f
  206. self.forces[a2] += f
  207. self.stress += np.outer(f, d)
  208. def main():
  209. import sys
  210. from ase.io import read, write
  211. inputfile = sys.argv[1]
  212. outputfile = sys.argv[2]
  213. atoms = read(inputfile)
  214. atoms.calc = EMT()
  215. atoms.get_stress()
  216. write(outputfile, atoms)
  217. if __name__ == '__main__':
  218. main()