Coverage for gpaw/test/response/test_hchain_supercell.py: 100%
35 statements
« prev ^ index » next coverage.py v7.7.1, created at 2025-07-08 00:17 +0000
« prev ^ index » next coverage.py v7.7.1, created at 2025-07-08 00:17 +0000
1# Test of the handling of degenerate bands in response code
2import pytest
3from ase import Atoms
4from gpaw import GPAW, PW
5from gpaw.response.df import DielectricFunction
6from gpaw.test import findpeak
7import numpy as np
10def get_hydrogen_chain_dielectric_function(NH, NK):
11 a = Atoms('H', cell=[1, 1, 2], pbc=True)
12 a.center()
13 a = a.repeat((1, 1, NH))
14 a.calc = GPAW(mode=PW(200, force_complex_dtype=True),
15 kpts={'size': (1, 1, NK), 'gamma': True},
16 parallel={'band': 1},
17 gpts=(10, 10, 10 * NH))
18 a.get_potential_energy()
19 a.calc.diagonalize_full_hamiltonian(nbands=2 * NH)
20 a.calc.write('H_chain.gpw', 'all')
22 DF = DielectricFunction('H_chain.gpw', ecut=1e-3, hilbert=False,
23 frequencies={'type': 'nonlinear',
24 'domega0': None,
25 'omega2': np.inf,
26 'omegamax': None},
27 intraband=False)
28 eps_NLF, eps_LF = DF.get_dielectric_function(direction='z')
29 omega_w = DF.get_frequencies()
30 return omega_w, eps_LF
33@pytest.mark.dielectricfunction
34@pytest.mark.serial
35@pytest.mark.response
36def test_hyd_chain_response(in_tmp_dir):
37 NH_i = [2**n for n in [0, 2]]
38 NK_i = [2**n for n in [4, 2]]
40 opeak_old = np.nan
41 peak_old = np.nan
43 for NH, NK in zip(NH_i, NK_i):
44 omega_w, eps_w = get_hydrogen_chain_dielectric_function(NH, NK)
45 eels_w = -(1. / eps_w).imag
46 opeak, peak = findpeak(omega_w, eels_w)
48 # Test for consistency
49 if not np.isnan(opeak_old):
50 assert opeak == pytest.approx(opeak_old, abs=1e-3)
51 assert peak == pytest.approx(peak_old, abs=1e-3)
52 opeak_old = opeak
53 peak_old = peak