Coverage for gpaw/test/xc/test_qna_force.py: 100%
25 statements
« prev ^ index » next coverage.py v7.7.1, created at 2025-07-08 00:17 +0000
« prev ^ index » next coverage.py v7.7.1, created at 2025-07-08 00:17 +0000
1import pytest
2from ase.parallel import parprint
3from gpaw import GPAW
6@pytest.mark.old_gpaw_only
7def test_xc_qna_force(in_tmp_dir, gpw_files):
8 calc = GPAW(gpw_files['Cu3Au_qna'], parallel=dict(domain=1))
9 atoms = calc.get_atoms()
10 # Displace atoms to have non-zero forces in the first place
11 atoms[0].position[0] += 0.1
13 dx_array = [-0.005, 0.000, 0.005]
14 E = []
16 for i, dx in enumerate(dx_array):
18 atoms[0].position[0] += dx
19 atoms.calc = calc
20 E.append(atoms.get_potential_energy(force_consistent=True))
21 if i == 1:
22 F = atoms.get_forces()[0, 0]
23 atoms[0].position[0] -= dx
25 F_num = -(E[-1] - E[0]) / (dx_array[-1] - dx_array[0])
26 F_err = F_num - F
28 parprint('Analytical force = ', F)
29 parprint('Numerical force = ', F_num)
30 parprint('Difference = ', F_err)
31 assert abs(F_err) < 0.01, F_err
32 eerr = abs(E[-1] - 277.546724 + 0.12677918230332352)
33 assert eerr < 1e-3, eerr