Coverage for gpaw/test/cdft/test_cdft_restart_two_regions.py: 100%

25 statements  

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

1import pytest 

2from ase import Atoms 

3import numpy as np 

4from gpaw import GPAW, FermiDirac, Davidson, Mixer, restart 

5from gpaw.cdft.cdft import CDFT 

6from gpaw.cdft.cdft_coupling import CouplingParameters 

7from gpaw.mpi import world 

8 

9 

10@pytest.mark.old_gpaw_only 

11@pytest.mark.skipif(world.size > 1, reason='cdft coupling not parallel') 

12def test_cdft_restart(in_tmp_dir): 

13 distance = 2.5 

14 sys = Atoms('He2', positions=([0., 0., 0.], [0., 0., distance])) 

15 sys.center(3) 

16 sys.set_pbc(False) 

17 sys.set_initial_magnetic_moments([0.5, 0.5]) 

18 

19 calc_b = GPAW(h=0.2, 

20 mode='fd', 

21 basis='dzp', 

22 charge=1, 

23 xc='PBE', symmetry='off', 

24 occupations=FermiDirac(0., fixmagmom=True), 

25 eigensolver=Davidson(3), 

26 spinpol=True, 

27 nbands=4, 

28 mixer=Mixer(beta=0.25, nmaxold=3, weight=100.0), 

29 txt='He2+_final_%3.2f.txt' % distance, 

30 convergence={'eigenstates': 1.0, 

31 'density': 1.0, 

32 'energy': 1.0, 

33 'bands': 4}) 

34 

35 cdft_b = CDFT(calc=calc_b, 

36 atoms=sys, 

37 charge_regions=[[1], [0]], 

38 charges=[1, 0], 

39 charge_coefs=[27, 0], 

40 method='L-BFGS-B', 

41 txt='He2+_final_%3.2f.cdft' % distance, 

42 minimizer_options={'gtol': 0.1}) 

43 sys.calc = cdft_b 

44 sys.get_potential_energy() 

45 sys.calc.calc.write('He2.gpw', mode='all') 

46 

47 # Restart 

48 

49 atoms, calc = restart('He2.gpw') 

50 

51 coupling = CouplingParameters(calc_a=calc, calc_b=calc, 

52 wfs_a='He2.gpw', wfs_b='He2.gpw', 

53 Va=[27, 0], Vb=[27, 0], 

54 charge_regions_A=[[1], [0]], 

55 charge_regions_B=[[1], [0]]) 

56 overlaps = coupling.get_pair_density_matrix(calc, calc)[0] 

57 for i in [0, 1, 2]: 

58 assert (np.isclose(np.real(overlaps[0, i, i]), 1.))