Coverage for gpaw/test/rsf_yukawa/test_rsf_ivo_sing_mg.py: 95%

41 statements  

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

1"""Test the calculation of the excitation energy of Na2 by RSF and IVOs.""" 

2import pytest 

3from ase import Atoms 

4from ase.units import Hartree 

5 

6import gpaw.cgpaw as cgpaw 

7from gpaw import GPAW 

8from gpaw.utilities.adjust_cell import adjust_cell 

9from gpaw.eigensolvers import RMMDIIS 

10from gpaw.lrtddft import LrTDDFT 

11from gpaw.mpi import world 

12from gpaw.occupations import FermiDirac 

13from gpaw.test import gen 

14 

15 

16@pytest.mark.hybrids 

17def test_rsf_yukawa_rsf_ivo_sing_mg(in_tmp_dir, add_cwd_to_setup_paths): 

18 libxc_version = getattr(cgpaw, 'libxc_version', '2.x.y') 

19 if int(libxc_version.split('.')[0]) < 3: 

20 from unittest import SkipTest 

21 raise SkipTest 

22 

23 h = 0.35 # Gridspacing 

24 e_singlet = 4.61 

25 e_singlet_lr = 5.54 

26 

27 gen('Mg', xcname='PBE', scalarrel=True, exx=True, yukawa_gamma=0.38) 

28 

29 c = {'energy': 0.05, 'eigenstates': 3, 'density': 3} 

30 na2 = Atoms('Mg', positions=[[0, 0, 0]]) 

31 adjust_cell(na2, 2.5, h=h) 

32 calc = GPAW(mode='fd', 

33 txt='mg_ivo.txt', 

34 xc='LCY-PBE:omega=0.38:excitation=singlet', 

35 eigensolver=RMMDIIS(), h=h, occupations=FermiDirac(width=0.0), 

36 spinpol=False, convergence=c) 

37 na2.calc = calc 

38 na2.get_potential_energy() 

39 (eps_homo, eps_lumo) = calc.get_homo_lumo() 

40 e_ex = eps_lumo - eps_homo 

41 assert e_singlet == pytest.approx(e_ex, abs=0.15) 

42 calc.write('mg.gpw') 

43 

44 c2 = GPAW('mg.gpw') 

45 ihomo = int(c2.get_occupation_numbers().sum() / 2 + 0.5) - 1 

46 assert c2.hamiltonian.xc.excitation == 'singlet' 

47 lr = LrTDDFT(calc, txt='LCY_TDDFT_Mg.log', 

48 restrict={'istart': ihomo, 'jend': ihomo + 1}, nspins=2) 

49 lr.write('LCY_TDDFT_Mg.ex.gz') 

50 if world.rank == 0: 

51 lr2 = LrTDDFT.read('LCY_TDDFT_Mg.ex.gz') 

52 lr2.diagonalize() 

53 ex_lr = lr2[1].get_energy() * Hartree 

54 assert e_singlet_lr == pytest.approx(ex_lr, abs=0.15)