Coverage for gpaw/test/response/test_chi0_update.py: 100%

32 statements  

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

1# General modules 

2import pytest 

3import numpy as np 

4 

5# Script modules 

6from ase.build import bulk 

7 

8from gpaw import GPAW, PW, FermiDirac 

9from gpaw.response import ResponseGroundStateAdapter 

10from gpaw.response.chi0 import Chi0Calculator, get_frequency_descriptor 

11 

12 

13@pytest.mark.response 

14@pytest.mark.serial 

15def test_si_update_consistency(in_tmp_dir): 

16 """Test that we get consistent results, when calculating 

17 chi0 in one or multiple calls to update_chi0.""" 

18 

19 # ---------- Inputs ---------- # 

20 

21 # Ground state calculation 

22 xc = 'LDA' 

23 a = 5.431 

24 pw = 200 

25 nbands = 8 

26 kpts = 4 

27 occw = 0.05 

28 

29 # Response calculation 

30 q_c = np.array([0., 0., 0.]) 

31 intermediate_m = 5 

32 

33 # ---------- Script ---------- # 

34 

35 # Ground state calculation 

36 atoms = bulk('Si', 'diamond', a=a) 

37 atoms.center() 

38 calc = GPAW(mode=PW(pw), 

39 nbands=nbands, 

40 kpts=(kpts, kpts, kpts), 

41 parallel={'domain': 1}, 

42 occupations=FermiDirac(width=occw), 

43 xc=xc) 

44 

45 atoms.calc = calc 

46 atoms.get_potential_energy() 

47 calc.write('Si', 'all') 

48 

49 # Response calculation 

50 gs = ResponseGroundStateAdapter.from_gpw_file('Si') 

51 chi0 = Chi0Calculator(gs, wd=get_frequency_descriptor(gs=gs), 

52 hilbert=True, intraband=False) 

53 

54 chi0_full = chi0.create_chi0(q_c) 

55 chi0_steps = chi0.create_chi0(q_c) 

56 spins = range(gs.nspins) 

57 # Add chi0 contribution from all the unoccupied bands 

58 chi0.update_chi0(chi0_full, m1=gs.nocc1, m2=nbands, spins=spins) 

59 # Add chi0 contribution from *some* of the unoccupied bands 

60 chi0.update_chi0(chi0_steps, m1=gs.nocc1, m2=intermediate_m, spins=spins) 

61 # Add chi0 contribution from the remaining unoccupied bands 

62 chi0.update_chi0(chi0_steps, m1=intermediate_m, m2=nbands, spins=spins) 

63 

64 # Compare the output chi0 body 

65 assert chi0_steps.chi0_WgG == pytest.approx(chi0_full.chi0_WgG, 

66 abs=1e-8, rel=1e-6)