Coverage for gpaw/test/test_fixdensity_mgga.py: 86%

44 statements  

« prev     ^ index     » next       coverage.py v7.7.1, created at 2025-07-19 00:19 +0000

1import pytest 

2from ase import Atoms 

3 

4from gpaw import GPAW 

5from gpaw.calculator import DeprecatedParameterWarning 

6 

7 

8@pytest.mark.ci 

9@pytest.mark.mgga 

10def test_fixdensity(in_tmp_dir, gpaw_new): 

11 a = 2.5 

12 slab = Atoms('Li', cell=(a, a, 2 * a), pbc=1) 

13 slab.calc = GPAW(mode='fd', 

14 xc='revTPSS', 

15 h=0.15, 

16 kpts=(3, 3, 1), txt='li-1.txt', 

17 parallel=dict(kpt=1)) 

18 slab.get_potential_energy() 

19 slab.calc.write('li.gpw', mode='all') 

20 slab.calc.write('li_nowf.gpw') 

21 

22 # Gamma point: 

23 e1 = slab.calc.get_eigenvalues(kpt=0)[0] 

24 f1 = slab.calc.get_fermi_level() 

25 

26 kpts = [(0, 0, 0)] 

27 

28 # Fix density and continue: 

29 calc = slab.calc.fixed_density( 

30 txt='li-2.txt', 

31 nbands=5, 

32 kpts=kpts) 

33 e2 = calc.get_eigenvalues(kpt=0)[0] 

34 f2 = calc.get_fermi_level() 

35 

36 # Start from gpw-file: 

37 calc = GPAW('li.gpw', txt=None) 

38 calc = calc.fixed_density( 

39 txt='li-3.txt', 

40 nbands=5, 

41 kpts=kpts) 

42 e3 = calc.get_eigenvalues(kpt=0)[0] 

43 f3 = calc.get_fermi_level() 

44 

45 assert f2 == pytest.approx(f1, abs=1e-10) 

46 assert f3 == pytest.approx(f1, abs=1e-10) 

47 assert e2 == pytest.approx(e1, abs=3e-5) 

48 assert e3 == pytest.approx(e1, abs=3e-5) 

49 

50 if not gpaw_new: 

51 calc = GPAW('li.gpw', 

52 txt='li-4.txt', 

53 fixdensity=True, 

54 nbands=5, 

55 kpts=kpts, 

56 symmetry='off') 

57 try: 

58 with pytest.warns(DeprecatedParameterWarning): 

59 calc.get_potential_energy() 

60 except ValueError: 

61 pass 

62 else: 

63 assert False 

64 

65 calc = GPAW('li_nowf.gpw') 

66 if not gpaw_new: 

67 with pytest.raises(ValueError): 

68 calc = calc.fixed_density(txt='li-3.txt', nbands=5, kpts=kpts) 

69 else: 

70 calc = calc.fixed_density(txt='li-3.txt', nbands=5, kpts=kpts) 

71 e4 = calc.get_eigenvalues(kpt=0)[0] 

72 f4 = calc.get_fermi_level() 

73 assert f4 == pytest.approx(f1, abs=1e-10) 

74 assert e4 == pytest.approx(e1, abs=3e-5)