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

1import numpy as np 

2 

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 

6 

7 

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. 

10 

11 Ported from JuLIP.jl. 

12 

13 https://github.com/JuliaMolSim/JuLIP.jl/blob/master/src/cutoffs.jl 

14 

15 https://en.wikipedia.org/wiki/Smoothstep 

16 

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. 

25 

26 Returns 

27 ------- 

28 np.ndarray 

29 Sigmoid-like function smoothly interpolating (r0, 1) and (r1, 0). 

30 

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 ) 

36 

37 

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 ) 

46 

47 

48class MorsePotential(Calculator): 

49 """Morse potential.""" 

50 

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 

66 

67 def __init__(self, neighbor_list=ase_neighbor_list, **kwargs): 

68 r""" 

69 

70 The pairwise energy between atoms *i* and *j* is given by 

71 

72 .. math:: 

73 

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) 

78 

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 

88 

89 .. math:: 

90 

91 k = 2 \epsilon \left(\frac{\rho_0}{r_0}\right)^2. 

92 

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 

100 

101 Notes 

102 ----- 

103 The default values are chosen to be similar as Lennard-Jones. 

104 

105 """ 

106 self.neighbor_list = neighbor_list 

107 Calculator.__init__(self, **kwargs) 

108 

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 

120 

121 number_of_atoms = len(self.atoms) 

122 

123 forces = np.zeros((number_of_atoms, 3)) 

124 

125 i, _j, d, D = self.neighbor_list('ijdD', atoms, rcut2) 

126 dhat = (D / d[:, None]).T 

127 

128 expf = np.exp(rho0 * (1.0 - d / r0)) 

129 

130 cutoff_fn = fcut(d, rcut1, rcut2) 

131 d_cutoff_fn = fcut_d(d, rcut1, rcut2) 

132 

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'] 

141 

142 # derivatives of `pair_energies` with respect to `d` 

143 de = (-2.0 * epsilon * rho0 / r0) * expf * (expf - 1.0) 

144 

145 # smoothened `de` 

146 de = de * cutoff_fn + pairwise_energies * d_cutoff_fn 

147 

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 

156 

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)