Repository navigation
Expand file tree
/
Copy pathtest_PyACECalculator.py
More file actions
317 lines (247 loc) · 8.99 KB
/
Copy pathtest_PyACECalculator.py
File metadata and controls
317 lines (247 loc) · 8.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import pickle
import numpy as np
import pytest
from ase.build import bulk
from ase.atoms import Atoms
from pyace import *
def create_dimer(x):
return Atoms(["Al"] * 2, positions=[[0, 0, 0], [0, 0, x]], pbc=False)
def create_trimer(x):
return Atoms(["Al"] * 3, positions=[[0, 0, 0], [0, 0, x], [0, 0, 2 * x]], pbc=False)
def test_setup():
block = BBasisFunctionsSpecificationBlock()
block.block_name = "Al"
block.nradmaxi = 1
block.lmaxi = 0
block.npoti = "FinnisSinclair"
block.fs_parameters = [1, 1]
block.rcutij = 8.7
block.dcutij = 0.01
block.NameOfCutoffFunctionij = "cos"
block.nradbaseij = 1
block.radbase = "ChebExpCos"
block.radparameters = [3.0]
block.radcoefficients = [[[1]]]
block.funcspecs = [
BBasisFunctionSpecification(["Al", "Al"], ns=[1], ls=[0], LS=[], coeffs=[1.]),
BBasisFunctionSpecification(["Al", "Al", "Al"], ns=[1, 1], ls=[0, 0], LS=[], coeffs=[2])
]
basisConfiguration = BBasisConfiguration()
basisConfiguration.deltaSplineBins = 0.001
basisConfiguration.funcspecs_blocks = [block]
a = create_dimer(1)
print(a)
calc = PyACECalculator(basis_set=basisConfiguration)
a.set_calculator(calc)
e1 = (a.get_potential_energy())
f1 = a.get_forces()
print(e1)
print(f1)
def test_load_YAML():
a = create_dimer(1)
print(a)
calc = PyACECalculator(basis_set="tests/Al.pbe.13.2.yaml")
a.set_calculator(calc)
e1 = (a.get_potential_energy())
f1 = a.get_forces()
print(e1)
print(f1)
def test_load_ace():
a = create_dimer(1)
print(a)
calc = PyACECalculator(basis_set="tests/Al.pbe.rhocore.ace")
a.set_calculator(calc)
e1 = (a.get_potential_energy())
f1 = a.get_forces()
print(e1)
print(f1)
e0 = 89.15966867577228
f0 = np.array([[0., 0., -193.82052454], [0., 0., 193.82052454]])
assert np.allclose(e0, e1)
assert np.allclose(f0, f1)
def test_load_ace_recursive():
a = create_dimer(1)
print(a)
calc = PyACECalculator(basis_set="tests/Al.pbe.rhocore.ace", recursive_evaluator=True, recursive=True)
a.set_calculator(calc)
e1 = (a.get_potential_energy())
f1 = a.get_forces()
print(e1)
print(f1)
e0 = 89.15966867577228
f0 = np.array([[0., 0., -193.82052454], [0., 0., 193.82052454]])
assert np.allclose(e0, e1)
assert np.allclose(f0, f1)
def test_dimer_r1_energy_forces():
a = create_dimer(1)
print(a)
calc = PyACECalculator(basis_set="tests/Al-r1l0.yaml")
a.set_calculator(calc)
energy = a.get_potential_energy()
forces = a.get_forces()
print(energy)
print(forces)
assert (energy - 1.9355078359256011) < 5e-10
assert (forces[0][2] + 0.12757969575334369) < 1e-11
assert (forces[1][2] - 0.12757969575334369) < 1e-11
def test_trimer_r234_energy_forces():
a = create_trimer(1)
print(a)
calculator = PyACECalculator(basis_set="tests/Al-r234.yaml")
a.set_calculator(calculator)
energy = a.get_potential_energy()
forces = a.get_forces()
print(energy)
print(forces)
assert (energy - 28.457404084390994) < 5e-10
assert (forces[0][2] + 13.052474776412932) < 1e-11
assert (forces[1][2] - 0.0000000000000000) < 1e-11
def test_load_YAML_pbc_symmetry_cubic():
a = bulk("Al", "sc", a=1, cubic=True)
print("a=", a)
calc = PyACECalculator(basis_set="tests/Al.pbe.13.2.yaml")
calc.cutoff = np.sqrt(2) * 1
a.set_calculator(calc)
e1 = a.get_potential_energy()
f1 = a.get_forces()
print("ae=", calc.ae)
print("ae.x=", calc.ae.x)
print("ae.species_type=", calc.ae.species_type)
print("ae.neighbour_list=", calc.ae.neighbour_list)
print("len=", list(map(len, calc.ae.neighbour_list)))
print("cutoff=", calc.cutoff)
print(e1)
print(f1)
assert np.max(f1) < 1e-10
def test_load_YAML_pbc_symmetry_fcc_supercell():
a = bulk("Al", "fcc", cubic=True) * (1, 2, 3)
print("a=", a)
calc = PyACECalculator(basis_set="tests/Al.pbe.13.2.yaml")
a.set_calculator(calc)
e1 = a.get_potential_energy()
f1 = a.get_forces()
print("ae=", calc.ae)
print("ae.x=", calc.ae.x)
print("ae.species_type=", calc.ae.species_type)
print("ae.neighbour_list=", calc.ae.neighbour_list)
print("len=", list(map(len, calc.ae.neighbour_list)))
print("cutoff=", calc.cutoff)
print(e1)
print(f1)
assert np.max(f1) < 1e-10
def test_non_supported_element():
a = bulk("C", "sc", a=1, cubic=True)
print("a=", a)
calc = PyACECalculator(basis_set="tests/Al.pbe.13.2.yaml")
with pytest.raises(ValueError):
a.set_calculator(calc)
e1 = a.get_potential_energy()
def test_fcc_stress():
a = bulk("Al", "fcc", a=4.03, cubic=True) * (1, 1, 1)
print("a=", a)
calc = PyACECalculator(basis_set="tests/Al.pbe.in-rank1.ace")
a.set_calculator(calc)
e1 = a.get_potential_energy()
f1 = a.get_forces()
s1 = a.get_stress()
print(e1)
print(f1)
print(s1)
e_ref = -15.75993931278226
s_ref = -4.00944358e-01
assert np.max(f1) < 1e-10
assert abs(e1 - e_ref) < 1e-7
assert abs(s1[0] - s_ref) < 1e-7
def test_relaxation():
from ase.constraints import UnitCellFilter
from ase.optimize import QuasiNewton
calc = PyACECalculator(basis_set="tests/Al.pbe.rhocore-v2.ace")
fcc = bulk("Al", cubic=True)
fcc.set_calculator(calc)
print("Atoms before = ", fcc)
e0 = fcc.get_potential_energy()
f0 = fcc.get_forces()
s0 = fcc.get_stress()
print("e_pot=", e0)
print("forces=", f0)
print("stress=", s0)
ucf = UnitCellFilter(fcc)
qn = QuasiNewton(ucf)
qn.run(fmax=0.005)
print("Atoms after = ", fcc)
e1 = fcc.get_potential_energy()
f1 = fcc.get_forces()
s1 = fcc.get_stress()
print("e_pot=", e1)
print("forces=", f1)
print("stress=", s1)
assert e1 < e0
assert np.linalg.norm(s1) < np.linalg.norm(s0)
assert np.allclose(np.linalg.norm(f1), np.linalg.norm(f0))
def test_PyACEEnsembleCalculator():
basis_sets = ["tests/Al.pbe.13.2.yaml", "tests/Al.pbe.rhocore.ace"]
calc = PyACEEnsembleCalculator(basis_set=basis_sets)
a = create_dimer(1)
a.set_calculator(calc)
e1 = (a.get_potential_energy())
f1 = a.get_forces()
print(e1)
print(f1)
def test_ZBL_analytical_derivative():
calc = PyACECalculator("tests/ZBL_rep.yaml")
def check(at, msg):
at.set_calculator(calc)
num_forces = calc.calculate_numerical_forces(at, d=1e-6)
an_forces = at.get_forces()
print(f"{num_forces=}, {an_forces=}")
assert np.allclose(num_forces, an_forces), msg+f": {num_forces=}, {an_forces=}"
at = Atoms("H2", positions=[[0, 0, 0], [0, 0, 4]]) # ACE
check(at, "ACE forces are inconsistent")
at = Atoms("H2", positions=[[0, 0, 0], [0, 0, 2.5]]) # ACE-ZBL
check(at, "ACE-ZBL forces are inconsistent")
at = Atoms("H2", positions=[[0, 0, 0], [0, 0, 1.5]]) # ZBL
check(at, "ZBL forces are inconsistent")
def minimize_dimer(calc, elements):
from ase.optimize import BFGS
at = Atoms(elements, positions=[[0, 0, 0], [0, 0, 2.0]], pbc=False)
at.set_calculator(calc)
energies = [at.get_potential_energy()]
forces = [at.get_forces()]
opt = BFGS(at, logfile=None)
opt.attach(lambda: (energies.append(at.get_potential_energy()),
forces.append(at.get_forces())))
opt.run(fmax=1e-3, steps=50)
return np.array(energies), np.array(forces)
def check_pickle_roundtrip(calc, elements=("Al", "Al")):
calc2 = pickle.loads(pickle.dumps(calc))
# minimize the dimer with the original and the unpickled calculator and
# compare energies/forces along the whole trajectory
e1, f1 = minimize_dimer(calc, elements)
e2, f2 = minimize_dimer(calc2, elements)
assert len(e1) > 1
assert e1.shape == e2.shape
assert np.allclose(e1, e2)
assert np.allclose(f1, f2)
return calc2
def test_pickle_bbasis_calculator():
calc = PyACECalculator(basis_set="tests/Al.pbe.13.2.yaml")
calc2 = check_pickle_roundtrip(calc)
assert isinstance(calc2.basis, ACEBBasisSet)
assert isinstance(calc2.evaluator, ACEBEvaluator)
def test_pickle_ctilde_calculator():
calc = PyACECalculator(basis_set="tests/Al.pbe.rhocore.ace")
calc2 = check_pickle_roundtrip(calc)
assert isinstance(calc2.basis, ACECTildeBasisSet)
assert isinstance(calc2.evaluator, ACECTildeEvaluator)
def test_pickle_yace_calculator():
calc = PyACECalculator(basis_set="tests/Al-Ni_opt_all.yace")
calc2 = check_pickle_roundtrip(calc, elements=("Al", "Ni"))
assert isinstance(calc2.basis, ACECTildeBasisSet)
assert isinstance(calc2.evaluator, ACECTildeEvaluator)
def test_pickle_ctilde_recursive_calculator():
from pyace.evaluator import ACERecursiveEvaluator
calc = PyACECalculator(basis_set="tests/Al.pbe.rhocore.ace",
recursive_evaluator=True, recursive=True)
calc2 = check_pickle_roundtrip(calc)
assert isinstance(calc2.basis, ACECTildeBasisSet)
assert isinstance(calc2.evaluator, ACERecursiveEvaluator)