Coverage for pySDC/tutorial/step_3/A_getting_statistics.py: 100%

54 statements  

« prev     ^ index     » next       coverage.py v7.6.9, created at 2024-12-20 14:51 +0000

1from pathlib import Path 

2 

3from pySDC.helpers.stats_helper import get_list_of_types, get_sorted 

4 

5from pySDC.implementations.controller_classes.controller_nonMPI import controller_nonMPI 

6from pySDC.implementations.problem_classes.HeatEquation_ND_FD import heatNd_forced 

7from pySDC.implementations.sweeper_classes.imex_1st_order import imex_1st_order 

8 

9 

10def main(): 

11 """ 

12 A simple test program to describe how to get statistics of a run 

13 """ 

14 

15 # run simulation 

16 stats = run_simulation() 

17 

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

19 f = open('data/step_3_A_out.txt', 'w') 

20 out = 'List of registered statistic types: %s' % get_list_of_types(stats) 

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

22 print(out) 

23 

24 # filter statistics by first time interval and type (residual) 

25 residuals = get_sorted(stats, time=0.1, type='residual_post_iteration', sortby='iter') 

26 

27 for item in residuals: 

28 out = 'Residual in iteration %2i: %8.4e' % item 

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

30 print(out) 

31 

32 # get and convert filtered statistics to list of iterations count, sorted by time 

33 # the get_sorted function is just a shortcut for sort_stats(filter_stats()) with all the same arguments 

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

35 

36 for item in iter_counts: 

37 out = 'Number of iterations at time %4.2f: %2i' % item 

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

39 print(out) 

40 

41 f.close() 

42 

43 assert all(item[1] == 12 for item in iter_counts), ( 

44 'ERROR: number of iterations are not as expected, got %s' % iter_counts 

45 ) 

46 

47 

48def run_simulation(): 

49 """ 

50 A simple test program to run IMEX SDC for a single time step 

51 """ 

52 # initialize level parameters 

53 level_params = {} 

54 level_params['restol'] = 1e-10 

55 level_params['dt'] = 0.1 

56 

57 # initialize sweeper parameters 

58 sweeper_params = {} 

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

60 sweeper_params['num_nodes'] = 3 

61 

62 # initialize problem parameters 

63 problem_params = {} 

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

65 problem_params['freq'] = 4 # frequency for the test value 

66 problem_params['nvars'] = 1023 # number of degrees of freedom 

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

68 

69 # initialize step parameters 

70 step_params = {} 

71 step_params['maxiter'] = 20 

72 

73 # initialize controller parameters (<-- this is new!) 

74 controller_params = {} 

75 controller_params['logger_level'] = 30 # reduce verbosity of each run 

76 

77 # Fill description dictionary for easy hierarchy creation 

78 description = {} 

79 description['problem_class'] = heatNd_forced 

80 description['problem_params'] = problem_params 

81 description['sweeper_class'] = imex_1st_order 

82 description['sweeper_params'] = sweeper_params 

83 description['level_params'] = level_params 

84 description['step_params'] = step_params 

85 

86 # instantiate the controller (no controller parameters used here) 

87 controller = controller_nonMPI(num_procs=1, controller_params=controller_params, description=description) 

88 

89 # set time parameters 

90 t0 = 0.1 

91 Tend = 0.9 

92 

93 # get initial values on finest level 

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

95 uinit = P.u_exact(t0) 

96 

97 # call main function to get things done... 

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

99 

100 return stats 

101 

102 

103if __name__ == "__main__": 

104 main()