Coverage for gpaw/test/lrtddft/test_apmb.py: 97%

62 statements  

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

1import pytest 

2from ase import Atom, Atoms 

3from ase.parallel import parprint 

4 

5from gpaw import GPAW, mpi 

6from gpaw.lrtddft import LrTDDFT 

7 

8 

9@pytest.mark.lrtddft 

10@pytest.mark.libxc 

11def test_lrtddft_apmb(): 

12 txt = '-' 

13 txt = '/dev/null' 

14 

15 load = False 

16 

17 if not load: 

18 R = 0.7 # approx. experimental bond length 

19 a = 3.0 

20 c = 4.0 

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

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

23 cell=(a, a, c)) 

24 calc = GPAW(mode='fd', xc='PBE', nbands=2, spinpol=False, txt=txt) 

25 H2.calc = calc 

26 H2.get_potential_energy() 

27 else: 

28 calc = GPAW('H2.gpw', txt=txt) 

29 

30 # DFT only 

31 

32 xc = 'LDA' 

33 

34 # no spin 

35 

36 lr = LrTDDFT(calc, xc=xc) 

37 lr.diagonalize() 

38 

39 lr_ApmB = LrTDDFT(calc, xc=xc, force_ApmB=True) 

40 lr_ApmB.diagonalize() 

41 parprint('lr=', lr) 

42 parprint('ApmB=', lr_ApmB) 

43 assert lr[0].get_energy() == pytest.approx(lr_ApmB[0].get_energy(), 

44 abs=5.e-9) 

45 

46 # with spin 

47 parprint('------ with spin') 

48 

49 if not load: 

50 c_spin = GPAW(mode='fd', xc='PBE', nbands=2, 

51 spinpol=True, parallel={'domain': mpi.world.size}, 

52 txt=txt) 

53 H2.calc = c_spin 

54 c_spin.calculate(H2) 

55 else: 

56 c_spin = GPAW('H2spin.gpw', txt=txt) 

57 lr = LrTDDFT(c_spin, xc=xc) 

58 lr.diagonalize() 

59 

60 lr_ApmB = LrTDDFT(c_spin, xc=xc, force_ApmB=True) 

61 lr_ApmB.diagonalize() 

62 parprint('lr=', lr) 

63 parprint('ApmB=', lr_ApmB) 

64 assert lr[0].get_energy() == pytest.approx(lr_ApmB[0].get_energy(), 

65 abs=5.e-8) 

66 assert lr[1].get_energy() == pytest.approx(lr_ApmB[1].get_energy(), 

67 abs=5.e-8) 

68 

69 # with spin virtual 

70 parprint('------ with virtual spin') 

71 

72 lr = LrTDDFT(calc, xc=xc, nspins=2) 

73 lr.diagonalize() 

74 

75 # ApmB 

76 lr_ApmB = LrTDDFT(calc, xc=xc, nspins=2) 

77 lr_ApmB.diagonalize() 

78 parprint('lr=', lr) 

79 parprint('ApmB=', lr_ApmB) 

80 assert lr[0].get_energy() == pytest.approx(lr_ApmB[0].get_energy(), 

81 abs=5.e-8) 

82 assert lr[1].get_energy() == pytest.approx(lr_ApmB[1].get_energy(), 

83 abs=5.e-8) 

84 

85 # with HF exchange 

86 

87 xc = 'PBE0' 

88 

89 parprint('------ with spin xc=', xc) 

90 lr_spin = LrTDDFT(c_spin, xc=xc) 

91 lr_spin.diagonalize() 

92 parprint('lr=', lr_spin) 

93 

94 parprint('------ with virtual spin xc=', xc) 

95 lr = LrTDDFT(calc, xc=xc, nspins=2) 

96 lr.diagonalize() 

97 parprint('lr=', lr) 

98 assert lr[0].get_energy() == pytest.approx(lr_spin[0].get_energy(), 

99 abs=3.8e-6) 

100 assert lr[1].get_energy() == pytest.approx(lr_spin[1].get_energy(), 

101 abs=3.4e-6)