Coverage for gpaw/test/exx/test_unocc.py: 95%

39 statements  

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

1import pytest 

2from ase import Atoms 

3from ase.parallel import parprint 

4 

5from gpaw import GPAW 

6from gpaw.xc.hybrid import HybridXC 

7 

8 

9@pytest.mark.libxc 

10@pytest.mark.hybrids 

11def test_exx_unocc(): 

12 

13 loa = Atoms('Be2', 

14 [(0, 0, 0), (2.45, 0, 0)], 

15 cell=[5.9, 4.8, 5.0]) 

16 loa.center() 

17 

18 txt = None 

19 xc = 'PBE0' 

20 nbands = 4 

21 

22 unocc = True 

23 load = False 

24 

25 base_params = dict( 

26 mode='fd', 

27 h=0.3, 

28 eigensolver='rmm-diis', 

29 nbands=nbands, 

30 convergence={'eigenstates': 1e-4}, 

31 txt=txt) 

32 # usual calculation 

33 fname = 'Be2.gpw' 

34 if not load: 

35 xco = HybridXC(xc) 

36 cocc = GPAW(**base_params, xc=xco) 

37 cocc.calculate(loa) 

38 else: 

39 cocc = GPAW(fname) 

40 cocc.converge_wave_functions() 

41 fo_n = 1. * cocc.get_occupation_numbers() 

42 eo_n = 1. * cocc.get_eigenvalues() 

43 

44 if unocc: 

45 # apply Fock opeartor also to unoccupied orbitals 

46 xcu = HybridXC(xc, unocc=True) 

47 cunocc = GPAW(**base_params, xc=xcu) 

48 cunocc.calculate(loa) 

49 

50 parprint(' HF occ HF unocc diff') 

51 parprint('Energy %10.4f %10.4f %10.4f' % 

52 (cocc.get_potential_energy(), 

53 cunocc.get_potential_energy(), 

54 cocc.get_potential_energy() - cunocc.get_potential_energy() 

55 )) 

56 assert cocc.get_potential_energy() == pytest.approx( 

57 cunocc.get_potential_energy(), abs=1.e-4) 

58 

59 fu_n = cunocc.get_occupation_numbers() 

60 eu_n = cunocc.get_eigenvalues() 

61 

62 parprint('Eigenvalues:') 

63 for eo, fo, eu, fu in zip(eo_n, fo_n, eu_n, fu_n): 

64 parprint('%8.4f %5.2f %8.4f %5.2f %8.4f' % 

65 (eo, fo, eu, fu, eu - eo)) 

66 if fo > 0.01: 

67 assert eo == pytest.approx(eu, abs=3.5e-4)