Coverage for ase/calculators/morse.py: 97.83%
46 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-27 15:21 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-27 15:21 +0000
1import numpy as np
3from ase.calculators.calculator import Calculator, all_changes
4from ase.neighborlist import neighbor_list as ase_neighbor_list
5from ase.stress import full_3x3_to_voigt_6_stress
8def fcut(r: np.ndarray, r0: float, r1: float) -> np.ndarray:
9 """Piecewise quintic C^{2,1} regular polynomial for use as a smooth cutoff.
11 Ported from JuLIP.jl.
13 https://github.com/JuliaMolSim/JuLIP.jl/blob/master/src/cutoffs.jl
15 https://en.wikipedia.org/wiki/Smoothstep
17 Parameters
18 ----------
19 r : np.ndarray
20 Distances between atoms.
21 r0 : float
22 Inner cutoff radius.
23 r1 : float
24 Outder cutoff radius.
26 Returns
27 -------
28 np.ndarray
29 Sigmoid-like function smoothly interpolating (r0, 1) and (r1, 0).
31 """
32 s = 1.0 - (r - r0) / (r1 - r0)
33 return (s >= 1.0) + ((s > 0.0) & (s < 1.0)) * (
34 6.0 * s**5 - 15.0 * s**4 + 10.0 * s**3
35 )
38def fcut_d(r: np.ndarray, r0: float, r1: float) -> np.ndarray:
39 """Derivative of fcut() function defined above."""
40 s = 1.0 - (r - r0) / (r1 - r0)
41 return -(
42 ((s > 0.0) & (s < 1.0))
43 * (30.0 * s**4 - 60.0 * s**3 + 30.0 * s**2)
44 / (r1 - r0)
45 )
48class MorsePotential(Calculator):
49 """Morse potential."""
51 implemented_properties = [
52 'energy',
53 'energies',
54 'free_energy',
55 'forces',
56 'stress',
57 ]
58 default_parameters = {
59 'epsilon': 1.0,
60 'rho0': 6.0,
61 'r0': 1.0,
62 'rcut1': 1.9,
63 'rcut2': 2.7,
64 }
65 nolabel = True
67 def __init__(self, neighbor_list=ase_neighbor_list, **kwargs):
68 r"""
70 The pairwise energy between atoms *i* and *j* is given by
72 .. math::
74 V_{ij} = \epsilon \left(
75 \mathrm{e}^{-2 \rho_0 (r_{ij} / r_0 - 1)}
76 - 2 \mathrm{e}^{- \rho_0 (r_{ij} / r_0 - 1)}
77 \right)
79 Parameters
80 ----------
81 epsilon : float, default 1.0
82 Absolute minimum depth.
83 r0 : float, default 1.0
84 Minimum distance.
85 rho0 : float, default 6.0
86 Exponential prefactor.
87 The force constant in the potential minimum is given by
89 .. math::
91 k = 2 \epsilon \left(\frac{\rho_0}{r_0}\right)^2.
93 rcut1 : float, default 1.9
94 Distance starting a smooth cutoff normalized by ``r0``.
95 rcut2 : float, default 2.7
96 Distance ending a smooth cutoff normalized by ``r0``.
97 neighbor_list : callable, optional
98 neighbor_list function compatible with
99 ase.neighborlist.neighbor_list
101 Notes
102 -----
103 The default values are chosen to be similar as Lennard-Jones.
105 """
106 self.neighbor_list = neighbor_list
107 Calculator.__init__(self, **kwargs)
109 def calculate(
110 self, atoms=None, properties=None, system_changes=all_changes
111 ):
112 if properties is None:
113 properties = ['energy']
114 Calculator.calculate(self, atoms, properties, system_changes)
115 epsilon = self.parameters['epsilon']
116 rho0 = self.parameters['rho0']
117 r0 = self.parameters['r0']
118 rcut1 = self.parameters['rcut1'] * r0
119 rcut2 = self.parameters['rcut2'] * r0
121 number_of_atoms = len(self.atoms)
123 forces = np.zeros((number_of_atoms, 3))
125 i, _j, d, D = self.neighbor_list('ijdD', atoms, rcut2)
126 dhat = (D / d[:, None]).T
128 expf = np.exp(rho0 * (1.0 - d / r0))
130 cutoff_fn = fcut(d, rcut1, rcut2)
131 d_cutoff_fn = fcut_d(d, rcut1, rcut2)
133 pairwise_energies = epsilon * expf * (expf - 2.0)
134 self.results['energies'] = np.bincount(
135 i,
136 weights=0.5 * (pairwise_energies * cutoff_fn),
137 minlength=number_of_atoms,
138 )
139 self.results['energy'] = self.results['energies'].sum()
140 self.results['free_energy'] = self.results['energy']
142 # derivatives of `pair_energies` with respect to `d`
143 de = (-2.0 * epsilon * rho0 / r0) * expf * (expf - 1.0)
145 # smoothened `de`
146 de = de * cutoff_fn + pairwise_energies * d_cutoff_fn
148 de_vec = (de * dhat).T
149 for dim in range(3):
150 forces[:, dim] = np.bincount(
151 i,
152 weights=de_vec[:, dim],
153 minlength=number_of_atoms,
154 )
155 self.results['forces'] = forces
157 if self.atoms.cell.rank == 3:
158 stress = 0.5 * (D.T @ de_vec) / self.atoms.get_volume()
159 self.results['stress'] = full_3x3_to_voigt_6_stress(stress)