Coverage for gpaw/test/gllb/test_restart_eigenvalues.py: 100%
33 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
1import pytest
2import numpy as np
3from ase.build import bulk
4from gpaw import GPAW
7refs = {'GLLBSC':
8 [[-6.96783989, 5.3992398, 5.40304598, 5.40304598, 8.50339453,
9 8.50339453, 8.50489331, 8.7972915],
10 [-5.53899418, -0.14491454, 4.14051678, 4.14051678, 7.89976594,
11 10.62148051, 10.62148051, 15.47779579]],
12 'GLLBSC:width=0.1':
13 [[-6.90967462, 5.45874896, 5.46258594, 5.46258594, 8.56019914,
14 8.56019914, 8.56166802, 8.8487109],
15 [-5.48079026, -0.08685927, 4.2001473, 4.2001473, 7.95314291,
16 10.67741742, 10.67741742, 15.53082277]],
17 'GLLBSCM':
18 [[-6.55170494, 5.8177223, 5.82167851, 5.82167851, 8.9114778,
19 8.9114778, 8.9128251, 9.16145391],
20 [-5.12306231, 0.27223217, 4.56254371, 4.56254371, 8.28319508,
21 11.02205235, 11.02205235, 15.85416557]],
22 'GLLBSC:metallic=1:width=0.1':
23 [[-6.51118445, 5.85791198, 5.86187878, 5.86187878, 8.95128038,
24 8.95128038, 8.95261673, 9.19621624],
25 [-5.08260244, 0.3129949, 4.60331705, 4.60331705, 8.32039946,
26 11.06096213, 11.06096213, 15.89026172]],
27 'GLLBSC:stencil=1':
28 [[-6.96553391, 5.39977866, 5.40347256, 5.40347256, 8.5044520,
29 8.50445200, 8.50610892, 8.79785566],
30 [-5.53673488, -0.14290236, 4.14161043, 4.14161043, 7.90076722,
31 10.62164739, 10.62164739, 15.47627092]]
32 }
35@pytest.mark.gllb
36@pytest.mark.libxc
37@pytest.mark.parametrize('xc', ['GLLBSC', 'GLLBSC:width=0.1',
38 'GLLBSCM', 'GLLBSC:metallic=1:width=0.1',
39 'GLLBSC:stencil=1'])
40def test_restart_eigenvalues(xc, in_tmp_dir):
41 test_kpts = [[0, 0, 0], [1. / 3, 1. / 3, 1. / 3]]
43 atoms = bulk('Si')
44 calc = GPAW(mode='lcao',
45 basis='sz(dzp)',
46 h=0.3,
47 nbands=8,
48 xc=xc,
49 kpts={'size': (3, 3, 3), 'gamma': True},
50 txt='gs.out')
51 atoms.calc = calc
52 atoms.get_potential_energy()
54 eref_s = calc.hamiltonian.xc.response.eref_s
55 eref_source_s = calc.hamiltonian.xc.response.eref_source_s
57 kpt_i = [0, 3]
58 calc_kpts = calc.get_ibz_k_points()[kpt_i]
59 assert np.allclose(calc_kpts, test_kpts, rtol=0, atol=1e-8), \
60 "Wrong kpt indices"
61 eig_in = [calc.get_eigenvalues(kpt=kpt) for kpt in kpt_i]
62 eig_in = np.array(eig_in)
63 calc.write('gs.gpw')
65 # Check calculation against reference
66 ref_eig_in = refs[xc]
67 assert np.allclose(eig_in, ref_eig_in, rtol=0, atol=1e-6), \
68 "{} error = {}".format(xc, np.max(np.abs(eig_in - ref_eig_in)))
70 # Restart
71 calc = GPAW('gs.gpw')
72 calc = calc.fixed_density(kpts=test_kpts, txt='gs2.out')
73 # Check that calculation was triggered
74 scf = calc.scf
75 assert scf is not None and scf.niter is not None and scf.niter > 0, \
76 "Test error: SCF was not run in restart"
77 eig2_in = [calc.get_eigenvalues(kpt=kpt) for kpt in range(len(kpt_i))]
78 eig2_in = np.array(eig2_in)
80 # Check restarted eigenvalues
81 assert np.allclose(eig_in, eig2_in, rtol=0, atol=1e-8), \
82 "{} restart error = {}".format(xc, np.max(np.abs(eig_in - eig2_in)))
84 # Check that reference energy source information is restored
85 assert eref_s == calc.hamiltonian.xc.response.eref_s
86 assert eref_source_s == calc.hamiltonian.xc.response.eref_source_s