Coverage for gpaw/test/exx/test_kpts.py: 98%
52 statements
« prev ^ index » next coverage.py v7.7.1, created at 2025-07-14 00:18 +0000
« prev ^ index » next coverage.py v7.7.1, created at 2025-07-14 00:18 +0000
1"""Test case where q=k1-k2 has component outside 0<=q<1 range."""
2from typing import Tuple
4import pytest
5import numpy as np
6from ase import Atoms
8from gpaw import GPAW, PW
9from gpaw.hybrids.eigenvalues import non_self_consistent_eigenvalues
10from gpaw.mpi import size
11from gpaw.new.pw.nschse import NonSelfConsistentHSE06
12from gpaw.new.ase_interface import GPAW as NewGPAW
15@pytest.fixture(scope='module')
16def atoms() -> Atoms:
17 n = 7
18 a = Atoms('HH',
19 cell=[2, 2, 2.5, 90, 90, 60],
20 pbc=1,
21 positions=[[0, 0, 0], [0, 0, 0.75]])
22 parallel = dict(zip(['domain', 'kpt', 'band'],
23 {1: [1, 1, 1],
24 2: [2, 1, 1],
25 4: [2, 2, 1],
26 8: [2, 2, 2]}[size]))
27 a.calc = GPAW(mode=PW(200),
28 kpts=(n, n, 1),
29 xc='PBE',
30 parallel=parallel)
31 a.get_potential_energy()
32 return a
35def bandgap(eps: np.ndarray) -> Tuple[int, int, float]:
36 """Find band-gap."""
37 k1 = eps[0, :, 0].argmax()
38 k2 = eps[0, :, 1].argmin()
39 return k1, k2, eps[0, k2, 1] - eps[0, k1, 0]
42gaps = {'EXX': 21.45,
43 'PBE0': 13.93,
44 'HSE06': 14.44,
45 'PBE': 11.63}
48@pytest.mark.libxc
49@pytest.mark.hybrids
50@pytest.mark.parametrize('xc', ['EXX', 'PBE0', 'HSE06'])
51def test_kpts(xc: str, atoms: Atoms) -> None:
52 c = atoms.calc
53 e0, v0, v = non_self_consistent_eigenvalues(c, xc)
54 e = e0 - v0 + v
55 k1, k2, gap = bandgap(e)
56 assert k1 == 4 and k2 == 7
57 assert gap == pytest.approx(gaps[xc], abs=0.01)
58 k1, k2, gap = bandgap(e0)
59 assert k1 == 4 and k2 == 7
60 assert gap == pytest.approx(gaps['PBE'], abs=0.01)
63def test_2d_non_self_consistent():
64 a = Atoms('Li',
65 [[0.0, 0.0, 1.0]],
66 cell=[1.5, 1.5, 2.0, 90, 90, 120],
67 pbc=(1, 1, 0))
69 n = 2
70 a.calc = NewGPAW(
71 mode=PW(200),
72 convergence={'density': 1e-5},
73 kpts=(n, n, 1),
74 txt=None)
75 a.get_potential_energy()
77 eref_kn = np.array(
78 [[-6.0938938, 31.82737621, 36.83364518, 53.28369147],
79 [13.0202785, 28.45570036, 38.86882486, 43.44290272]])
81 if a.calc.dft.comm.size == 1:
82 e0, v0, v = non_self_consistent_eigenvalues(a.calc, 'HSE06')
83 e_skn = e0 - v0 + v
84 assert e_skn[0] == pytest.approx(eref_kn, rel=1e-5)
86 hse = NonSelfConsistentHSE06.from_dft_calculation(a.calc.dft)
87 _, e_skn = hse.calculate(a.calc.dft.ibzwfs)
88 assert e_skn[0] == pytest.approx(eref_kn, rel=1e-5)
89 _, e_skn = hse.calculate(a.calc.dft.ibzwfs, na=0, nb=1)
90 assert e_skn[0, :, 0] == pytest.approx(eref_kn[:, 0], rel=1e-5)
93if __name__ == '__main__':
94 test_2d_non_self_consistent()