Coverage for gpaw/test/pw/test_si_stress_libvdwxc.py: 45%
44 statements
« prev ^ index » next coverage.py v7.7.1, created at 2025-07-12 00:18 +0000
« prev ^ index » next coverage.py v7.7.1, created at 2025-07-12 00:18 +0000
1import numpy as np
2import pytest
3from ase.build import bulk
4from ase.calculators.fd import calculate_numerical_stress
6from gpaw.utilities import compiled_with_libvdwxc
7from gpaw import GPAW, PW, Mixer
8from gpaw.mpi import world
9from gpaw.test import gen
10from gpaw.xc.libvdwxc import vdw_df, libvdwxc_has_spin, libvdwxc_has_stress
13skip_reason = "libvdwxc version does not implement stress"
14if compiled_with_libvdwxc():
15 try:
16 skip_cond = not libvdwxc_has_stress()
17 except SystemError:
18 skip_cond = True
19else:
20 skip_cond = True
23def _check_libvdwxc_stress(in_tmp_dir, gpaw_new, xc, setups, s_ref):
24 assert libvdwxc_has_spin() and libvdwxc_has_stress()
25 si = bulk('Si')
26 si.calc = GPAW(mode=PW(200),
27 mixer=Mixer(0.7, 5, 50.0),
28 xc=xc,
29 kpts=(1, 1, 2), # Run (1, 1, 2) to avoid gamma pt code
30 convergence={'energy': 1e-8},
31 parallel={'domain': min(2, world.size)},
32 setups=setups,
33 txt='si_stress.txt')
35 si.set_cell(np.dot(si.cell,
36 [[1.02, 0, 0.03],
37 [0, 0.99, -0.02],
38 [0.2, -0.01, 1.03]]),
39 scale_atoms=True)
41 si.get_potential_energy()
43 if not gpaw_new:
44 # Trigger nasty bug (fixed in !486):
45 si.calc.wfs.pt.blocksize = si.calc.wfs.pd.maxmyng - 1
47 s_analytical = si.get_stress()
48 if s_ref is None:
49 s_ref = calculate_numerical_stress(si, 1e-5)
50 print("REF", s_ref)
51 print(s_analytical)
52 s_err = s_analytical - s_ref
53 assert np.all(abs(s_err) < 1e-4)
56@pytest.mark.stress
57@pytest.mark.skipif(skip_cond, reason=skip_reason)
58def test_pw_si_stress_libvdwxc_gga(in_tmp_dir, gpaw_new):
59 s_ref = [-0.17538969, -0.08992427, -0.14401403,
60 -0.05984851, -0.00058295, 0.04351124]
61 xc = vdw_df()
62 _check_libvdwxc_stress(in_tmp_dir, gpaw_new, xc, "paw", s_ref)
65@pytest.mark.stress
66@pytest.mark.skipif(skip_cond, reason=skip_reason)
67def test_pw_si_stress_libvdwxc_mgga(in_tmp_dir, gpaw_new):
68 setups = {'Si': gen('Si', xcname='PBEsol')}
69 s_ref = [-0.13724147, -0.05995537, -0.11039066,
70 -0.05893786, 0.01830188, 0.04031258]
71 xc = {'name': 'mBEEF-vdW', 'backend': 'libvdwxc'}
72 _check_libvdwxc_stress(in_tmp_dir, gpaw_new, xc, setups, s_ref)