Coverage for gpaw/test/parallel/test_fd_parallel_kpt.py: 12%

98 statements  

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

1import pytest 

2from gpaw.mpi import world 

3import sys 

4 

5from gpaw.utilities import devnull 

6 

7from gpaw import GPAW, FermiDirac 

8from gpaw import KohnShamConvergenceError 

9from gpaw.utilities import compiled_with_sl 

10from gpaw.forces import calculate_forces 

11 

12from ase.build import molecule 

13 

14# Calculates energy and forces for various parallelizations 

15 

16pytestmark = pytest.mark.skipif(world.size < 4, 

17 reason='world.size < 4') 

18 

19 

20@pytest.mark.old_gpaw_only 

21def test_parallel_fd_parallel_kpt(): 

22 tolerance = 4e-5 

23 

24 parallel = dict() 

25 

26 basekwargs = dict(mode='fd', 

27 eigensolver='rmm-diis', 

28 maxiter=3, 

29 # basis='dzp', 

30 # nbands=18, 

31 nbands=6, 

32 kpts=(4, 4, 4), # 8 kpts in the IBZ 

33 parallel=parallel) 

34 

35 Eref = None 

36 Fref_av = None 

37 

38 def run(formula='H2O', vacuum=1.5, cell=None, pbc=1, **morekwargs): 

39 print(formula, parallel) 

40 system = molecule(formula) 

41 kwargs = dict(basekwargs) 

42 kwargs.update(morekwargs) 

43 calc = GPAW(**kwargs) 

44 system.calc = calc 

45 system.center(vacuum) 

46 if cell is None: 

47 system.center(vacuum) 

48 else: 

49 system.set_cell(cell) 

50 system.set_pbc(pbc) 

51 

52 try: 

53 system.get_potential_energy() 

54 except KohnShamConvergenceError: 

55 pass 

56 

57 E = calc.hamiltonian.e_total_free 

58 F_av = calculate_forces(calc.wfs, calc.density, 

59 calc.hamiltonian) 

60 

61 nonlocal Eref, Fref_av 

62 if Eref is None: 

63 Eref = E 

64 Fref_av = F_av 

65 

66 eerr = abs(E - Eref) 

67 ferr = abs(F_av - Fref_av).max() 

68 

69 if calc.wfs.world.rank == 0: 

70 print('Energy', E) 

71 print() 

72 print('Forces') 

73 print(F_av) 

74 print() 

75 print('Errs', eerr, ferr) 

76 

77 if eerr > tolerance or ferr > tolerance: 

78 if calc.wfs.world.rank == 0: 

79 stderr = sys.stderr 

80 else: 

81 stderr = devnull 

82 if eerr > tolerance: 

83 print('Failed!', file=stderr) 

84 print('E = %f, Eref = %f' % (E, Eref), file=stderr) 

85 msg = 'Energy err larger than tolerance: %f' % eerr 

86 if ferr > tolerance: 

87 print('Failed!', file=stderr) 

88 print('Forces:', file=stderr) 

89 print(F_av, file=stderr) 

90 print(file=stderr) 

91 print('Ref forces:', file=stderr) 

92 print(Fref_av, file=stderr) 

93 print(file=stderr) 

94 msg = 'Force err larger than tolerance: %f' % ferr 

95 print(file=stderr) 

96 print('Args:', file=stderr) 

97 print(formula, vacuum, cell, pbc, morekwargs, file=stderr) 

98 print(parallel, file=stderr) 

99 raise AssertionError(msg) 

100 

101 # reference: 

102 # kpt-parallelization = 8, 

103 # state-parallelization = 1, 

104 # domain-decomposition = (1,1,1) 

105 run() 

106 

107 # kpt-parallelization = 2, 

108 # state-parallelization = 2, 

109 # domain-decomposition = (1,2,1) 

110 parallel['band'] = 2 

111 parallel['domain'] = (1, 2, 1) 

112 run() 

113 

114 if compiled_with_sl(): 

115 # kpt-parallelization = 2, 

116 # state-parallelization = 2, 

117 # domain-decomposition = (1,2,1) 

118 # with blacs 

119 parallel['sl_default'] = (2, 2, 2) 

120 run() 

121 

122 # perform spin polarization test 

123 parallel = dict() 

124 

125 basekwargs = dict(mode='fd', 

126 eigensolver='rmm-diis', 

127 maxiter=3, 

128 nbands=6, 

129 kpts=(4, 4, 4), # 8 kpts in the IBZ 

130 parallel=parallel) 

131 

132 Eref = None 

133 Fref_av = None 

134 

135 OH_kwargs = dict(formula='NH2', vacuum=1.5, pbc=1, spinpol=1, 

136 occupations=FermiDirac(width=0.1)) 

137 

138 # reference: 

139 # kpt-parallelization = 4, 

140 # spin-polarization = 2, 

141 # state-parallelization = 1, 

142 # domain-decomposition = (1,1,1) 

143 run(**OH_kwargs) 

144 

145 # kpt-parallelization = 2, 

146 # spin-polarization = 2, 

147 # domain-decomposition = (1, 2, 1) 

148 parallel['domain'] = (1, 2, 1) 

149 run(**OH_kwargs) 

150 

151 # kpt-parallelization = 2, 

152 # spin-polarization = 2, 

153 # state-parallelization = 2, 

154 # domain-decomposition = (1, 1, 1) 

155 del parallel['domain'] 

156 parallel['band'] = 2 

157 run(**OH_kwargs) 

158 

159 # do last test plus buffer_size keyword 

160 parallel['buffer_size'] = 150 

161 run(**OH_kwargs) 

162 

163 if compiled_with_sl(): 

164 # kpt-parallelization = 2, 

165 # spin-polarization = 2, 

166 # state-parallelization = 2, 

167 # domain-decomposition = (1, 2, 1) 

168 # with blacs 

169 del parallel['buffer_size'] 

170 parallel['domain'] = (1, 2, 1) 

171 parallel['sl_default'] = (2, 1, 2) 

172 run(**OH_kwargs) 

173 

174 # kpt-parallelization = 2, 

175 # state-parallelization = 2, 

176 # domain-decomposition = (1, 2, 1) 

177 # with blacs 

178 parallel['sl_default'] = (2, 2, 2) 

179 run(**OH_kwargs) 

180 

181 # do last test plus buffer_size keyword 

182 parallel['buffer_size'] = 150 

183 run(**OH_kwargs)