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

36 statements  

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

1from ase import Atoms 

2from gpaw import GPAW 

3import numpy as np 

4 

5# spin paired H2 

6 

7 

8def test_xc_pygga(): 

9 d = 0.75 

10 h2 = Atoms('H2', [[0, 0, 0], [0, 0, d]]) 

11 h2.center(vacuum=2.) 

12 

13 e = np.array([]) 

14 f = np.array([]) 

15 

16 for XC in ['pyPBE', 'pyRPBE']: 

17 e = np.array([]) 

18 f = np.array([]) 

19 for i in [2, 0]: 

20 xc = XC[i:] 

21 calc = GPAW(nbands=-1, xc=xc, 

22 h=0.3, 

23 mode='lcao', basis='szp(dzp)') 

24 h2.calc = calc 

25 e = np.append(e, h2.get_potential_energy()) 

26 f = np.append(f, h2.get_forces()) 

27 del calc 

28 

29 assert np.abs(e[0] - e[1]) < 1.e-4 

30 assert np.sum(np.abs(f[0] - f[1])) < 1.e-10 

31 

32 # spin polarized O2 

33 d = 1.2 

34 o2 = Atoms('O2', [[0, 0, 0], [0, 0, d]], magmoms=[1., 1.]) 

35 o2.center(vacuum=2.) 

36 

37 for XC in ['pyPBE', 'pyRPBE']: 

38 e = np.array([]) 

39 f = np.array([]) 

40 for i in [2, 0]: 

41 xc = XC[i:] 

42 calc = GPAW(nbands=-2, xc=xc, 

43 h=0.3, 

44 mode='lcao', basis='szp(dzp)') 

45 o2.calc = calc 

46 e = np.append(e, o2.get_potential_energy()) 

47 f = np.append(f, o2.get_forces()) 

48 del calc 

49 

50 assert np.abs(e[0] - e[1]) < 5.e-3 

51 assert np.sum(np.abs(f[0] - f[1])) < 1.e-4