Coverage for gpaw/test/lrtddft/test_lrtddft_basics.py: 100%

43 statements  

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

1import pytest 

2from ase import Atom, Atoms 

3 

4from gpaw import GPAW 

5from gpaw.lrtddft import LrTDDFT 

6from gpaw.mpi import world 

7 

8 

9def get_H2(calculator=None): 

10 """Define H2 and set calculator if given""" 

11 R = 0.7 # approx. experimental bond length 

12 a = 3.0 

13 c = 4.0 

14 H2 = Atoms([Atom('H', (a / 2, a / 2, (c - R) / 2)), 

15 Atom('H', (a / 2, a / 2, (c + R) / 2))], 

16 cell=(a, a, c)) 

17 

18 if calculator is not None: 

19 H2.calc = calculator 

20 

21 return H2 

22 

23 

24@pytest.mark.lrtddft 

25def test_io(in_tmp_dir): 

26 calc = GPAW(mode='fd', xc='PBE', h=0.25, nbands=5, txt=None) 

27 calc.calculate(get_H2(calc)) 

28 exlst = LrTDDFT(calc, restrict={'eps': 0.4, 'jend': 3}) 

29 assert len(exlst) == 3 

30 assert exlst.kss.restrict['eps'] == 0.4 

31 

32 fname = 'lr.dat.gz' 

33 exlst.write(fname) 

34 world.barrier() 

35 

36 lr2 = LrTDDFT.read(fname) 

37 assert len(lr2) == 3 

38 

39 lr3 = LrTDDFT.read(fname, restrict={'jend': 2}) 

40 assert len(lr3) == 2 

41 assert len(lr3.kss) == 2 

42 assert len(lr3.Om.fullkss) == 3 

43 

44 lr4 = LrTDDFT.read(fname, restrict={'energy_range': 20}) 

45 assert len(lr4) == 1 

46 

47 

48@pytest.mark.lrtddft 

49def test_invocation(): 

50 calc = GPAW(xc='PBE', mode='fd', h=0.25, nbands=5, txt=None) 

51 H2 = get_H2(calc) 

52 exlst = LrTDDFT(restrict={'eps': 0.4, 'jend': 3}, txt=None) 

53 exlst.calculate(H2) 

54 assert exlst.xc is not None 

55 assert hasattr(exlst, 'Om') 

56 

57 # traditional way 

58 h2 = get_H2(calc) 

59 h2.get_potential_energy() 

60 exlst2 = LrTDDFT(calc, restrict={'eps': 0.4, 'jend': 3}, txt=None) 

61 assert hasattr(exlst2, 'Om')