Coverage for gpaw/test/xc/test_xcatom.py: 100%

39 statements  

« prev     ^ index     » next       coverage.py v7.7.1, created at 2025-07-09 00:21 +0000

1import numpy as np 

2import numpy.random as ra 

3from gpaw.setup import create_setup 

4from gpaw.xc import XC 

5import pytest 

6 

7 

8def test_xc_xcatom(): 

9 x = 0.000001 

10 rng = ra.default_rng(8) 

11 for xc in ['LDA', 'PBE']: 

12 print(xc) 

13 xc = XC(xc) 

14 s = create_setup('N', xc) 

15 ni = s.ni 

16 nii = ni * (ni + 1) // 2 

17 D_p = 0.1 * rng.random(nii) + 0.2 

18 H_p = np.zeros(nii) 

19 

20 xc.calculate_paw_correction( 

21 s, D_p.reshape(1, -1), H_p.reshape(1, -1)) 

22 dD_p = x * rng.random(nii) 

23 dE = np.dot(H_p, dD_p) / x 

24 D_p += dD_p 

25 Ep = xc.calculate_paw_correction(s, D_p.reshape(1, -1)) 

26 D_p -= 2 * dD_p 

27 Em = xc.calculate_paw_correction(s, D_p.reshape(1, -1)) 

28 print(dE, dE - 0.5 * (Ep - Em) / x) 

29 assert dE == pytest.approx(0.5 * (Ep - Em) / x, abs=1e-6) 

30 

31 Ems = xc.calculate_paw_correction(s, np.array([0.5 * D_p, 0.5 * D_p])) 

32 print(Em - Ems) 

33 assert Em == pytest.approx(Ems, abs=1.0e-12) 

34 

35 D_sp = 0.1 * rng.random((2, nii)) + 0.2 

36 H_sp = np.zeros((2, nii)) 

37 

38 xc.calculate_paw_correction(s, D_sp, H_sp) 

39 dD_sp = x * rng.random((2, nii)) 

40 dE = np.dot(H_sp.ravel(), dD_sp.ravel()) / x 

41 D_sp += dD_sp 

42 Ep = xc.calculate_paw_correction(s, D_sp) 

43 D_sp -= 2 * dD_sp 

44 Em = xc.calculate_paw_correction(s, D_sp) 

45 print(dE, dE - 0.5 * (Ep - Em) / x) 

46 assert dE == pytest.approx(0.5 * (Ep - Em) / x, abs=1e-6)