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

28 statements  

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

1import pytest 

2from ase import Atoms 

3import numpy as np 

4from gpaw import GPAW, FermiDirac, Davidson, Mixer 

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_pbc_cdft(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(True) 

17 sys.set_initial_magnetic_moments([0.5, 0.5]) 

18 

19 # Calculator for the initial state 

20 calc_a = GPAW( 

21 h=0.2, 

22 mode='fd', 

23 basis='dzp', 

24 charge=1, 

25 xc='PBE', 

26 symmetry='off', kpts=(2, 1, 1), 

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

28 eigensolver=Davidson(3), 

29 spinpol=True, # only spin-polarized calculations are supported 

30 nbands=4, 

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

32 txt='He2+_initial.txt', 

33 convergence={ 

34 'eigenstates': 1.0, 

35 'density': 1.0, 

36 'energy': 1.0, 

37 'bands': 4}) 

38 

39 sys.calc = calc_a 

40 sys.get_potential_energy() 

41 # Set initial state cdft 

42 cdft_a = CDFT( 

43 calc=calc_a, 

44 atoms=sys, 

45 charge_regions=[[0]], # choose atom 0 as the constrained region 

46 charges=[1], # constrain +1 charge 

47 charge_coefs=[27], # initial guess for Vc 

48 method='L-BFGS-B', # Vc optimization method 

49 txt='He2+_initial.cdft', # cDFT output file 

50 minimizer_options={'gtol': 0.1}) # tolerance for cdft 

51 

52 # Get cdft energy 

53 sys.calc = cdft_a 

54 sys.get_potential_energy() 

55 

56 # the same for the final state 

57 calc_b = GPAW( 

58 h=0.2, 

59 mode='fd', 

60 basis='dzp', 

61 charge=1, 

62 xc='PBE', 

63 symmetry='off', kpts=(2, 1, 1), 

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

65 eigensolver=Davidson(3), 

66 spinpol=True, # only spin-polarized calculations are supported 

67 nbands=4, 

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

69 txt='He2+_final.txt', 

70 convergence={ 

71 'eigenstates': 1.0, 

72 'density': 1.0, 

73 'energy': 1.0, 

74 'bands': 4}) 

75 

76 cdft_b = CDFT( 

77 calc=calc_b, 

78 atoms=sys, 

79 charge_regions=[[1]], # choose atom 1 

80 charges=[1], # constrained charge +1 

81 charge_coefs=[27], 

82 method='L-BFGS-B', 

83 txt='He2+_final.cdft', 

84 minimizer_options={'gtol': 0.1}) 

85 

86 sys.calc = cdft_b 

87 sys.get_potential_energy() 

88 

89 # Now for the coupling parameter 

90 coupling = CouplingParameters(cdft_a, cdft_b, AE=False) 

91 H12 = coupling.get_coupling_term() # use original cDFT method 

92 assert np.imag(H12) == 0