Coverage for gpaw/test/sic/test_mom_pwsic.py: 86%
37 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
1import io
2import pytest
4from gpaw import GPAW
5import numpy as np
7import numpy.testing as npt
8from gpaw.io.logger import GPAWLogger
9from gpaw.wavefunctions.base import eigenvalue_string
10from gpaw.test.sic._utils import (mk_arr_from_str,
11 extract_lagrange_section,
12 MockWorld)
13from gpaw.mpi import rank
16@pytest.mark.old_gpaw_only
17@pytest.mark.sic
18def test_mom_pwsic(in_tmp_dir, gpw_files):
19 calc = GPAW(gpw_files["h2o_mom_pwsic"])
20 H2O = calc.atoms
21 H2O.calc = calc
22 e = H2O.get_potential_energy()
23 f = H2O.get_forces()
25 # Numeric forces, generated by disabled code below
26 f_num = np.array(
27 [
28 [-2.85022, -3.66863, -0.009059],
29 [3.916824, -0.204146, -0.000065],
30 [-1.204687, 3.822242, 0.000353],
31 ]
32 )
33 numeric = False
34 if numeric:
35 from gpaw.test import calculate_numerical_forces
37 f_num = calculate_numerical_forces(H2O, 0.001)
38 print("Numerical forces")
39 print(f_num)
40 print(f - f_num, np.abs(f - f_num).max())
42 assert f == pytest.approx(f_num, abs=0.3)
43 assert e == pytest.approx(-3.302431, abs=0.2)
45 if rank == 0:
46 logger = GPAWLogger(MockWorld(rank=0))
47 string_io = io.StringIO()
48 logger.fd = string_io
49 calc.wfs.summary_func(logger)
50 lstr = extract_lagrange_section(string_io.getvalue())
52 expect_lagrange_str = """\
53 Band L_ii Occupancy Band L_ii Occupancy
54 0 -27.16794 1.00000 0 -26.69776 1.00000
55 1 -27.03738 1.00000 1 -26.63601 1.00000
56 2 -21.27563 1.00000 2 -21.66586 1.00000
57 3 -4.79667 1.00000 3 -21.65867 1.00000
58 4 -11.29628 0.00000 4 -2.39219 0.00000
59 5 -0.36645 0.00000 5 0.29809 0.00000
60 """
61 expect_eigen_str = """\
62 Band Eigenvalues Occupancy Eigenvalues Occupancy
63 0 -35.88615 1.00000 -37.34498 1.00000
64 1 -21.71059 1.00000 -22.06147 1.00000
65 2 -18.08943 1.00000 -18.72728 1.00000
66 3 -4.59144 1.00000 -18.52456 1.00000
67 4 -11.29628 0.00000 -2.39219 0.00000
68 5 -0.36645 0.00000 0.29809 0.00000
69 """
71 npt.assert_allclose(
72 mk_arr_from_str(expect_lagrange_str, 6),
73 mk_arr_from_str(lstr, 6),
74 atol=0.75,
75 )
77 npt.assert_allclose(
78 mk_arr_from_str(expect_eigen_str, 5),
79 mk_arr_from_str(eigenvalue_string(calc.wfs), 5, skip_rows=1),
80 atol=0.75,
81 )