Coverage for gpaw/test/response/test_qeh.py: 26%

62 statements  

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

1import pytest 

2import numpy as np 

3 

4from gpaw.response.df import DielectricFunction 

5from ase.parallel import world 

6from ase.units import Hartree 

7 

8 

9def dielectric(calc, domega, omega2, rate=0.0, 

10 ecut=20, nblocks=1, cyl_pw=False): 

11 if cyl_pw: 

12 from gpaw.response.qpd import SingleCylQPWDescriptor 

13 ecut = { 

14 'class': SingleCylQPWDescriptor, 

15 'kwargs': {'ecut_xy': ecut / Hartree, 

16 'ecut_z': ecut / 2 / Hartree} 

17 } 

18 diel = DielectricFunction(calc=calc, 

19 frequencies={'type': 'nonlinear', 

20 'omegamax': 10, 

21 'domega0': domega, 

22 'omega2': omega2}, 

23 nblocks=nblocks, 

24 ecut=ecut, 

25 rate=rate, 

26 truncation='2D') 

27 return diel 

28 

29 

30@pytest.mark.dielectricfunction 

31@pytest.mark.serial 

32@pytest.mark.response 

33def test_basics(in_tmp_dir, gpw_files): 

34 pytest.importorskip('qeh') 

35 from gpaw.response.qeh import QEHChiCalc 

36 

37 df = dielectric(gpw_files['graphene_pw'], 0.1, 0.5, 

38 ecut=10, rate=0.01, cyl_pw=False) 

39 

40 chicalc = QEHChiCalc(df) 

41 

42 assert len(chicalc.get_q_grid(q_max=0.6)) == 3 

43 assert len(chicalc.get_q_grid(q_max=2.6)) == 6 

44 assert len(chicalc.get_q_grid(q_max=2.6)[0].P_rv) == 2 

45 assert len(chicalc.get_z_grid()) == 30 

46 

47 q_q = chicalc.get_q_grid(q_max=0.6) 

48 chi_wGG, G_Gv, wblocks = chicalc.get_chi_wGG(qpoint=q_q[2]) 

49 

50 assert chi_wGG[0, 0, 0] == pytest.approx(-3.134762463291029e-10 

51 + 3.407232927207498e-27j) 

52 assert chi_wGG[3, 2, 1] == pytest.approx(-2.69008628970302e-10 

53 - 6.74306768078481e-11j) 

54 assert chi_wGG.shape[1] == len(G_Gv) 

55 

56 df2 = dielectric(gpw_files['graphene_pw'], 0.1, 0.5, 

57 ecut=10, rate=0.01, cyl_pw=True) 

58 

59 chicalc = QEHChiCalc(df2) 

60 chi2_wGG, G2_Gv, wblocks = chicalc.get_chi_wGG(qpoint=q_q[2]) 

61 

62 assert chi2_wGG[0, 0, 0] == pytest.approx(chi_wGG[0, 0, 0]) 

63 G1 = np.argmin(np.linalg.norm(G_Gv[None, 1] - G2_Gv, axis=1)) 

64 G2 = np.argmin(np.linalg.norm(G_Gv[None, 2] - G2_Gv, axis=1)) 

65 assert chi2_wGG[3, G2, G1] == pytest.approx(chi_wGG[3, 2, 1]) 

66 assert chi2_wGG.shape[1] == len(G2_Gv) 

67 

68 

69@pytest.mark.skipif(world.size == 1, reason='Features already tested ' 

70 'in serial in test_basics') 

71@pytest.mark.skipif(world.size > 6, reason='Parallelization for ' 

72 'small test-system broken for many cores') 

73@pytest.mark.dielectricfunction 

74@pytest.mark.response 

75def test_qeh_parallel(in_tmp_dir, gpw_files): 

76 pytest.importorskip('qeh') 

77 from gpaw.response.qeh import QEHChiCalc 

78 

79 df = dielectric(gpw_files['mos2_pw'], 0.05, 0.5, nblocks=world.size, 

80 cyl_pw=False) 

81 chicalc = QEHChiCalc(df) 

82 

83 q_q = chicalc.get_q_grid(q_max=0.6) 

84 chi_wGG, G_Gv, wblocks = chicalc.get_chi_wGG(qpoint=q_q[2]) 

85 chi_wGG = wblocks.all_gather(chi_wGG) 

86 

87 df2 = dielectric(gpw_files['mos2_pw'], 0.05, 0.5, nblocks=world.size, 

88 cyl_pw=True) 

89 chicalc = QEHChiCalc(df2) 

90 chi2_wGG, G2_Gv, wblocks = chicalc.get_chi_wGG(qpoint=q_q[2]) 

91 chi2_wGG = wblocks.all_gather(chi2_wGG) 

92 

93 if world.rank == 0: 

94 assert chi_wGG.shape[0] == 23 

95 assert chi_wGG[0, 0, 0] == pytest.approx(-0.004795395871467905 

96 + 1.8961244666811292e-19j) 

97 assert chi_wGG[3, 2, 1] == pytest.approx(0.004216141281855623 

98 + 6.949418840278167e-05j) 

99 assert chi_wGG.shape[1] == len(G_Gv) 

100 

101 assert chi2_wGG[0, 0, 0] == pytest.approx(chi_wGG[0, 0, 0], rel=1e-2) 

102 G1 = np.argmin(np.linalg.norm(G_Gv[None, 1] - G2_Gv, axis=1)) 

103 G2 = np.argmin(np.linalg.norm(G_Gv[None, 2] - G2_Gv, axis=1)) 

104 assert chi2_wGG[3, G2, G1] == pytest.approx(chi_wGG[3, 2, 1], rel=1e-2) 

105 assert chi2_wGG.shape[1] == len(G2_Gv)