Coverage for gpaw/test/lcaotddft/test_lcaotddft_vs_lrtddft.py: 100%

55 statements  

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

1import pytest 

2import numpy as np 

3 

4from ase.units import Hartree 

5from gpaw import GPAW 

6from gpaw.lcaotddft import LCAOTDDFT 

7from gpaw.lcaotddft.dipolemomentwriter import DipoleMomentWriter 

8from gpaw.tddft.spectrum import photoabsorption_spectrum as spec_td 

9from gpaw.lrtddft import LrTDDFT 

10from gpaw.lrtddft import photoabsorption_spectrum as spec_lr 

11from gpaw.lrtddft2 import LrTDDFT2 

12from gpaw.mpi import world 

13 

14 

15pytestmark = [pytest.mark.usefixtures('module_tmp_path')] 

16 

17 

18@pytest.fixture(scope='module') 

19def time_propagation_calculation(gpw_files): 

20 td_calc = LCAOTDDFT(gpw_files['na2_tddft_sz'], 

21 txt='na2_tddft_sz_td.out') 

22 DipoleMomentWriter(td_calc, 'dm.dat') 

23 td_calc.absorption_kick([0, 0, 1e-5]) 

24 td_calc.propagate(30, 150) 

25 spec_td('dm.dat', 'spec_td.dat', 

26 e_min=0, e_max=10, width=0.5, delta_e=0.1) 

27 world.barrier() 

28 

29 # Scale energy out due to \omega vs \omega_I difference in 

30 # broadened spectra in RT-TDDFT and LR-TDDFT 

31 data_ej = np.loadtxt('spec_td.dat') 

32 spec_e = data_ej[:, 3] 

33 spec_e[1:] /= data_ej[1:, 0] 

34 return spec_e 

35 

36 

37@pytest.fixture(scope='module') 

38def lrtddft_calculation(gpw_files): 

39 calc = GPAW(gpw_files['na2_tddft_sz'], txt=None) 

40 lr = LrTDDFT(calc, xc='LDA', txt='lr.out') 

41 lr.diagonalize() 

42 spec_lr(lr, 'spec_lr.dat', 

43 e_min=0, e_max=10, width=0.5, delta_e=0.1) 

44 world.barrier() 

45 

46 # Scale energy out due to \omega vs \omega_I difference in 

47 # broadened spectra in RT-TDDFT and LR-TDDFT 

48 data_ej = np.loadtxt('spec_lr.dat') 

49 spec_e = data_ej[:, 4] 

50 spec_e[1:] /= lr[0].get_energy() * Hartree 

51 return spec_e 

52 

53 

54@pytest.fixture(scope='module') 

55def lrtddft2_calculation(gpw_files): 

56 calc = GPAW(gpw_files['na2_tddft_sz'], txt='lr2.out') 

57 lr2 = LrTDDFT2('lr2', calc, fxc='LDA') 

58 lr2.calculate() 

59 lr2.get_spectrum('spec_lr2.dat', 0, 10.1, 0.1, width=0.5) 

60 world.barrier() 

61 

62 # Scale energy out due to \omega vs \omega_I difference in 

63 # broadened spectra in RT-TDDFT and LR-TDDFT 

64 data_ej = np.loadtxt('spec_lr2.dat') 

65 spec_e = data_ej[:, 5] 

66 spec_e[1:] /= lr2.lr_transitions.get_transitions()[0][0] 

67 return spec_e 

68 

69 

70@pytest.mark.rttddft 

71def test_lcaotddft_vs_lrtddft(time_propagation_calculation, 

72 lrtddft_calculation): 

73 # One can decrease the tolerance by decreasing the time step 

74 # and other parameters 

75 assert (time_propagation_calculation 

76 == pytest.approx(lrtddft_calculation, abs=1e-2)) 

77 

78 

79@pytest.mark.rttddft 

80def test_lcaotddft_vs_lrtddft2(time_propagation_calculation, 

81 lrtddft2_calculation): 

82 # One can decrease the tolerance by decreasing the time step 

83 # and other parameters 

84 assert (time_propagation_calculation 

85 == pytest.approx(lrtddft2_calculation, abs=1e-2)) 

86 

87 

88@pytest.mark.rttddft 

89def test_lrtddft_vs_lrtddft2(lrtddft_calculation, 

90 lrtddft2_calculation): 

91 assert (lrtddft_calculation 

92 == pytest.approx(lrtddft2_calculation, abs=1e-3))