Coverage for pySDC/tutorial/step_6/A_run_non_MPI_controller.py: 100%

97 statements  

« prev     ^ index     » next       coverage.py v7.6.7, created at 2024-11-16 14:51 +0000

1from pathlib import Path 

2 

3from pySDC.helpers.stats_helper import get_sorted 

4 

5from pySDC.implementations.controller_classes.controller_nonMPI import controller_nonMPI 

6from pySDC.implementations.problem_classes.HeatEquation_ND_FD import heatNd_unforced 

7from pySDC.implementations.sweeper_classes.generic_implicit import generic_implicit 

8from pySDC.implementations.transfer_classes.TransferMesh import mesh_to_mesh 

9 

10 

11def main(num_proc_list=None, fname=None, multi_level=True): 

12 """ 

13 A simple test program to run PFASST 

14 

15 Args: 

16 num_proc_list: list of number of processes to test with 

17 fname: filename/path for output 

18 multi_level (bool): do multi-level run or single-level 

19 """ 

20 

21 if multi_level: 

22 description, controller_params, t0, Tend = set_parameters_ml() 

23 else: 

24 assert all(num_proc == 1 for num_proc in num_proc_list), ( 

25 'ERROR: single-level run can only use 1 processor, got %s' % num_proc_list 

26 ) 

27 description, controller_params, t0, Tend = set_parameters_sl() 

28 

29 Path("data").mkdir(parents=True, exist_ok=True) 

30 f = open('data/' + fname, 'w') 

31 # loop over different numbers of processes 

32 for num_proc in num_proc_list: 

33 out = 'Working with %2i processes...' % num_proc 

34 f.write(out + '\n') 

35 print(out) 

36 

37 # instantiate controllers 

38 controller = controller_nonMPI(num_procs=num_proc, controller_params=controller_params, description=description) 

39 

40 # get initial values on finest level 

41 P = controller.MS[0].levels[0].prob 

42 uinit = P.u_exact(t0) 

43 

44 # call main functions to get things done... 

45 uend, stats = controller.run(u0=uinit, t0=t0, Tend=Tend) 

46 

47 # compute exact solution and compare with both results 

48 uex = P.u_exact(Tend) 

49 err = abs(uex - uend) 

50 

51 out = 'Error vs. exact solution: %12.8e' % err 

52 f.write(out + '\n') 

53 print(out) 

54 

55 # filter statistics by type (number of iterations) 

56 iter_counts = get_sorted(stats, type='niter', sortby='time') 

57 

58 # compute and print statistics 

59 for item in iter_counts: 

60 out = 'Number of iterations for time %4.2f: %1i ' % (item[0], item[1]) 

61 f.write(out + '\n') 

62 print(out) 

63 

64 f.write('\n') 

65 print() 

66 

67 assert all(item[1] <= 8 for item in iter_counts), "ERROR: weird iteration counts, got %s" % iter_counts 

68 

69 f.close() 

70 

71 

72def set_parameters_ml(): 

73 """ 

74 Helper routine to set parameters for the following multi-level runs 

75 

76 Returns: 

77 dict: dictionary containing the simulation parameters 

78 dict: dictionary containing the controller parameters 

79 float: starting time 

80 float: end time 

81 """ 

82 # initialize level parameters 

83 level_params = {} 

84 level_params['restol'] = 5e-10 

85 level_params['dt'] = 0.125 

86 

87 # initialize sweeper parameters 

88 sweeper_params = {} 

89 sweeper_params['quad_type'] = 'RADAU-RIGHT' 

90 sweeper_params['num_nodes'] = [3] 

91 sweeper_params['QI'] = 'LU' 

92 

93 # initialize problem parameters 

94 problem_params = {} 

95 problem_params['nu'] = 0.1 # diffusion coefficient 

96 problem_params['freq'] = 2 # frequency for the test value 

97 problem_params['nvars'] = [63, 31] # number of degrees of freedom for each level 

98 problem_params['bc'] = 'dirichlet-zero' # boundary conditions 

99 

100 # initialize step parameters 

101 step_params = {} 

102 step_params['maxiter'] = 50 

103 step_params['errtol'] = 1e-05 

104 

105 # initialize space transfer parameters 

106 space_transfer_params = {} 

107 space_transfer_params['rorder'] = 2 

108 space_transfer_params['iorder'] = 6 

109 

110 # initialize controller parameters 

111 controller_params = {} 

112 controller_params['logger_level'] = 30 

113 controller_params['all_to_done'] = True # can ask the controller to keep iterating all steps until the end 

114 controller_params['predict_type'] = 'pfasst_burnin' # activate iteration estimator 

115 

116 # fill description dictionary for easy step instantiation 

117 description = {} 

118 description['problem_class'] = heatNd_unforced 

119 description['problem_params'] = problem_params 

120 description['sweeper_class'] = generic_implicit 

121 description['sweeper_params'] = sweeper_params 

122 description['level_params'] = level_params 

123 description['step_params'] = step_params 

124 description['space_transfer_class'] = mesh_to_mesh 

125 description['space_transfer_params'] = space_transfer_params 

126 

127 # set time parameters 

128 t0 = 0.0 

129 Tend = 1.0 

130 

131 return description, controller_params, t0, Tend 

132 

133 

134def set_parameters_sl(): 

135 """ 

136 Helper routine to set parameters for the following multi-level runs 

137 

138 Returns: 

139 dict: dictionary containing the simulation parameters 

140 dict: dictionary containing the controller parameters 

141 float: starting time 

142 float: end time 

143 """ 

144 # initialize level parameters 

145 level_params = {} 

146 level_params['restol'] = 5e-10 

147 level_params['dt'] = 0.125 

148 

149 # initialize sweeper parameters 

150 sweeper_params = {} 

151 sweeper_params['quad_type'] = 'RADAU-RIGHT' 

152 sweeper_params['num_nodes'] = 3 

153 sweeper_params['QI'] = 'LU' 

154 

155 # initialize problem parameters 

156 problem_params = {} 

157 problem_params['nu'] = 0.1 # diffusion coefficient 

158 problem_params['freq'] = 2 # frequency for the test value 

159 problem_params['nvars'] = 63 # number of degrees of freedom for each level 

160 problem_params['bc'] = 'dirichlet-zero' # boundary conditions 

161 

162 # initialize step parameters 

163 step_params = {} 

164 step_params['maxiter'] = 50 

165 

166 # initialize controller parameters 

167 controller_params = {} 

168 controller_params['logger_level'] = 30 

169 

170 # fill description dictionary for easy step instantiation 

171 description = {} 

172 description['problem_class'] = heatNd_unforced 

173 description['problem_params'] = problem_params 

174 description['sweeper_class'] = generic_implicit 

175 description['sweeper_params'] = sweeper_params 

176 description['level_params'] = level_params 

177 description['step_params'] = step_params 

178 

179 # set time parameters 

180 t0 = 0.0 

181 Tend = 1.0 

182 

183 return description, controller_params, t0, Tend 

184 

185 

186if __name__ == "__main__": 

187 main(num_proc_list=[1], fname='step_6_A_sl_out.txt', multi_level=False) 

188 main(num_proc_list=[1, 2, 4, 8], fname='step_6_A_ml_out.txt', multi_level=True)