Coverage for gpaw/test/lrtddft/test_lrtddft_log.py: 100%
46 statements
« prev ^ index » next coverage.py v7.7.1, created at 2025-07-09 00:21 +0000
« prev ^ index » next coverage.py v7.7.1, created at 2025-07-09 00:21 +0000
1import pytest
2from ase import Atom, Atoms
3from ase.parallel import world
5from gpaw import GPAW
6from gpaw.lrtddft import LrTDDFT
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))
18 if calculator is not None:
19 H2.calc = calculator
21 return H2
24def run_and_delete(txt):
25 outfname = 'gpawlog.txt'
26 calc = GPAW(mode='fd', xc='PBE', h=0.25, nbands=5, txt=outfname)
27 calc.calculate(get_H2(calc))
28 exlst = LrTDDFT(calc, restrict={'eps': 0.4, 'jend': 3}, txt=txt)
29 del calc
30 del exlst
31 world.barrier()
34@pytest.mark.lrtddft
35def test_log(in_tmp_dir):
36 defname = 'gpawlog.txt'
37 # LrTDDFT outputs to the same log like gpaw
38 run_and_delete(txt=defname)
39 if world.rank == 0:
40 with open(defname) as f:
41 string = f.read()
42 assert 'Kohn-Sham single transitions' in string
43 assert 'Linear response TDDFT calculation' in string
44 world.barrier()
46 # silent LrTDDFT
47 run_and_delete(txt=None)
48 if world.rank == 0:
49 with open(defname) as f:
50 assert 'RPA kss[0]' not in f.read()
51 world.barrier()
53 # output to own file
54 ownfname = 'lrtddftlog.txt'
55 run_and_delete(txt=ownfname)
57 if world.rank == 0:
58 with open(defname) as f:
59 assert 'Linear response TDDFT calculation' not in f.read()
60 with open(ownfname) as f:
61 string = f.read()
62 assert 'Kohn-Sham single transitions' in string
63 assert 'Linear response TDDFT calculation' in string
64 world.barrier()