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

1import pytest 

2from ase.parallel import parprint 

3from gpaw import GPAW 

4 

5 

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 

12 

13 dx_array = [-0.005, 0.000, 0.005] 

14 E = [] 

15 

16 for i, dx in enumerate(dx_array): 

17 

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 

24 

25 F_num = -(E[-1] - E[0]) / (dx_array[-1] - dx_array[0]) 

26 F_err = F_num - F 

27 

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