Coverage for gpaw/test/response/test_chi0_band_exclusion.py: 100%
50 statements
« prev ^ index » next coverage.py v7.7.1, created at 2025-07-20 00:19 +0000
« prev ^ index » next coverage.py v7.7.1, created at 2025-07-20 00:19 +0000
1from gpaw.mpi import world
2import numpy as np
3from ase.units import Ha
4from gpaw.response.pair import get_gs_and_context
5from gpaw.response.chi0 import (Chi0Calculator, get_frequency_descriptor,
6 get_omegamax)
7import pytest
10@pytest.mark.response
11def test_chi0_band_exclusion(in_tmp_dir, gpw_files):
12 """Testing the removal of the lowest three valence bands in a chi0
13 calculation for Ni. This is done by comparing two chi0 calculation: one
14 includs all bands but limits the frequency grid to exclude the transitions
15 from the 3 lowest valence bands, and the other explicitly excludes
16 these bands but extends the grid to cover their transition range.
17 The real part is obtained via a Hilbert transform"""
19 gs, context = get_gs_and_context(
20 gpw_files['ni_pw'], txt=None, world=world, timer=None)
22 ecut = 40
23 eta = 0.1
24 nbands_max = 14
26 omegamax2 = get_omegamax(gs, nbands=slice(0, nbands_max))
28 wd2 = get_frequency_descriptor(
29 {'type': 'nonlinear', 'domega0': omegamax2 / 4000, 'omega2': 10},
30 gs=gs, nbands=slice(0, nbands_max))
32 wd1 = get_frequency_descriptor(
33 {'type': 'nonlinear', 'domega0': omegamax2 / 4000, 'omega2': 10},
34 gs=gs,
35 nbands=slice(3, nbands_max))
37 omegamax2 = np.max(wd2.omega_w) * Ha
38 omegamax1 = np.max(wd1.omega_w) * Ha
40 assert omegamax1 == pytest.approx(45.223, abs=1e-3)
41 assert omegamax2 == pytest.approx(100.713, abs=1e-3)
43 assert np.allclose(wd1.omega_w, wd2.omega_w[:len(wd1)])
45 chi0calc1 = Chi0Calculator(gs, context,
46 wd=wd1, nbands=nbands_max,
47 hilbert=True,
48 eta=eta,
49 ecut=ecut,
50 eshift=None)
52 chi0_data1 = chi0calc1.calculate(q_c=[0, 0, 0])
54 chi0calc2 = Chi0Calculator(gs, context,
55 wd=wd2, nbands=slice(3, nbands_max),
56 hilbert=True,
57 eta=eta,
58 ecut=ecut,
59 eshift=None)
61 chi0_data2 = chi0calc2.calculate(q_c=[0, 0, 0])
63 chi0_data1_body = \
64 chi0_data1.body.data_WgG
65 chi0_data2_body = \
66 chi0_data2.body.data_WgG
68 # The two chi0 calculations are compared only on up to the maximum
69 # of wd1 excluding transitions from the three lowest valence band
70 nw = len(wd1)
72 assert chi0_data1_body[:nw] == pytest.approx(
73 chi0_data2_body[:nw], rel=1e-3, abs=1e-4)
74 assert chi0_data1.chi0_WxvG[:nw] == pytest.approx(
75 chi0_data2.chi0_WxvG[:nw], rel=1e-3, abs=1e-4)
76 assert chi0_data1.chi0_Wvv[:nw] == pytest.approx(
77 chi0_data2.chi0_Wvv[:nw], rel=1e-3, abs=1e-4)
79 # test assertion error when n1 >= n2
80 n2 = gs.nocc2
81 m1 = gs.nocc1
82 with pytest.raises(AssertionError):
83 chi0calc = Chi0Calculator(gs, context,
84 wd=wd2, nbands=slice(n2, nbands_max),
85 hilbert=True,
86 eta=eta,
87 ecut=ecut,
88 eshift=None)
89 chi0calc.calculate(q_c=[0, 0, 0])
91 with pytest.raises(AssertionError):
92 chi0calc = Chi0Calculator(gs, context,
93 wd=wd2, nbands=slice(n2 + 1, nbands_max),
94 hilbert=True,
95 eta=eta,
96 ecut=ecut,
97 eshift=None)
98 chi0calc.calculate(q_c=[0, 0, 0])
100 # test assertion error when n1 > m1
101 with pytest.raises(AssertionError):
102 chi0calc = Chi0Calculator(gs, context,
103 wd=wd2, nbands=slice(m1 + 1, nbands_max),
104 hilbert=True,
105 eta=eta,
106 ecut=ecut,
107 eshift=None)
108 chi0calc.calculate(q_c=[0, 0, 0])
110 # test assertion error if step size is not none or 1
111 with pytest.raises(AssertionError):
112 chi0calc = Chi0Calculator(gs, context,
113 wd=wd2, nbands=slice(3, nbands_max, 3),
114 hilbert=True,
115 eta=eta,
116 ecut=ecut,
117 eshift=None)
118 chi0calc.calculate(q_c=[0, 0, 0])
120 # test assertion error if n1 is negative
121 with pytest.raises(AssertionError):
122 chi0calc = Chi0Calculator(gs, context,
123 wd=wd2, nbands=slice(-1, nbands_max),
124 hilbert=True,
125 eta=eta,
126 ecut=ecut,
127 eshift=None)
128 chi0calc.calculate(q_c=[0, 0, 0])
130 # test assertion error if m2 is negative
131 with pytest.raises(AssertionError):
132 chi0calc = Chi0Calculator(gs, context,
133 wd=wd2, nbands=slice(3, -2),
134 hilbert=True,
135 eta=eta,
136 ecut=ecut,
137 eshift=None)
138 chi0calc.calculate(q_c=[0, 0, 0])