Coverage for gpaw/test/poisson/test_poisson_restart.py: 100%

53 statements  

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

1import pytest 

2from gpaw.mpi import world 

3import numpy as np 

4 

5from ase.build import molecule 

6from gpaw import GPAW 

7from gpaw.tddft import TDDFT as GRIDTDDFT 

8from gpaw.lcaotddft import LCAOTDDFT 

9from gpaw.poisson import PoissonSolver as PS 

10from gpaw.poisson_moment import MomentCorrectionPoissonSolver 

11from gpaw.poisson_extravacuum import ExtraVacuumPoissonSolver 

12 

13pytestmark = pytest.mark.skipif(world.size > 2, 

14 reason='world.size > 2') 

15 

16 

17@pytest.mark.old_gpaw_only 

18def test_poisson_poisson_restart(in_tmp_dir): 

19 name = 'Na2' 

20 gpts = np.array([16, 16, 24]) 

21 # Uncomment the following line if you want to run the test with 4 cpus 

22 # gpts *= 2 

23 

24 poissonsolver_i = [] 

25 

26 ps = PS() 

27 poissonsolver_i.append(ps) 

28 

29 ps = MomentCorrectionPoissonSolver(poissonsolver=PS(), 

30 moment_corrections=4) 

31 poissonsolver_i.append(ps) 

32 

33 mom_corr_i = [{'moms': range(4), 'center': [16, 16, 4]}, 

34 {'moms': range(4), 'center': [16, 16, 20]}] 

35 ps = MomentCorrectionPoissonSolver(poissonsolver=PS(), 

36 moment_corrections=4) 

37 poissonsolver_i.append(ps) 

38 

39 ps = MomentCorrectionPoissonSolver(poissonsolver=PS(), 

40 moment_corrections=mom_corr_i) 

41 poissonsolver_i.append(ps) 

42 

43 ps1 = MomentCorrectionPoissonSolver(poissonsolver=PS(), 

44 moment_corrections=mom_corr_i[0:1]) 

45 ps = MomentCorrectionPoissonSolver(poissonsolver=ps1, 

46 moment_corrections=mom_corr_i[1:2]) 

47 poissonsolver_i.append(ps) 

48 

49 ps = ExtraVacuumPoissonSolver(gpts * 2, PS()) 

50 poissonsolver_i.append(ps) 

51 

52 ps = ExtraVacuumPoissonSolver(gpts * 2, PS(), PS(), 2) 

53 poissonsolver_i.append(ps) 

54 

55 ps1 = ExtraVacuumPoissonSolver(gpts, PS(), PS(), 1) 

56 ps = ExtraVacuumPoissonSolver(gpts, ps1, PS(), 1) 

57 poissonsolver_i.append(ps) 

58 

59 for poissonsolver in poissonsolver_i: 

60 for mode in ['fd', 'lcao']: 

61 atoms = molecule(name) 

62 atoms.center(vacuum=3.0) 

63 

64 # Standard ground state calculation 

65 # Use loose convergence criterion for speed 

66 calc = GPAW(nbands=2, gpts=gpts / 2, setups={'Na': '1'}, txt=None, 

67 poissonsolver=poissonsolver, 

68 mode=mode, 

69 symmetry={'point_group': False}, 

70 convergence={'energy': 1.0, 

71 'density': 1.0, 

72 'eigenstates': 1.0}) 

73 atoms.calc = calc 

74 atoms.get_potential_energy() 

75 descr = calc.hamiltonian.poisson.get_description() 

76 calc.write('%s_gs.gpw' % name, mode='all') 

77 

78 # Restart ground state 

79 calc = GPAW('%s_gs.gpw' % name, txt=None) 

80 ps = calc.hamiltonian.poisson 

81 assert descr == ps.get_description(), \ 

82 'poisson solver has changed in GPAW / %s' % mode 

83 

84 # Time-propagation TDDFT 

85 if mode == 'lcao': 

86 TDDFT = LCAOTDDFT 

87 else: 

88 TDDFT = GRIDTDDFT 

89 calc = TDDFT('%s_gs.gpw' % name, txt=None) 

90 ps = calc.hamiltonian.poisson 

91 assert descr == ps.get_description(), \ 

92 'poisson solver has changed in TDDFT / %s' % mode