Coverage for pySDC/projects/Resilience/hook.py: 69%

29 statements  

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

1from pySDC.core.hooks import Hooks 

2from pySDC.implementations.hooks.log_solution import LogSolution 

3from pySDC.implementations.hooks.log_embedded_error_estimate import LogEmbeddedErrorEstimate 

4from pySDC.implementations.hooks.log_extrapolated_error_estimate import LogExtrapolationErrorEstimate 

5from pySDC.implementations.hooks.log_step_size import LogStepSize 

6 

7 

8hook_collection = [LogSolution, LogEmbeddedErrorEstimate, LogExtrapolationErrorEstimate, LogStepSize] 

9 

10 

11class LogData(Hooks): 

12 """ 

13 Record data required for analysis of problems in the resilience project 

14 """ 

15 

16 def pre_run(self, step, level_number): 

17 """ 

18 Record initial conditions 

19 """ 

20 super().pre_run(step, level_number) 

21 

22 L = step.levels[level_number] 

23 self.add_to_stats(process=0, time=0, level=0, iter=0, sweep=0, type='u0', value=L.u[0]) 

24 

25 def post_step(self, step, level_number): 

26 """ 

27 Record final solutions as well as step size and error estimates 

28 """ 

29 super().post_step(step, level_number) 

30 

31 L = step.levels[level_number] 

32 

33 # add the following with two names because I use both in different projects -.- 

34 self.increment_stats( 

35 process=step.status.slot, 

36 time=L.time, 

37 level=L.level_index, 

38 iter=step.status.iter, 

39 sweep=L.status.sweep, 

40 type='sweeps', 

41 value=step.status.iter, 

42 ) 

43 self.increment_stats( 

44 process=step.status.slot, 

45 time=L.time + L.dt, 

46 level=L.level_index, 

47 iter=step.status.iter, 

48 sweep=L.status.sweep, 

49 type='k', 

50 value=step.status.iter, 

51 ) 

52 

53 

54class LogUold(Hooks): 

55 """ 

56 Log last iterate at the end of the step. Since the hook comes after override of uold, we need to do this in each 

57 iteration. But we don't know which will be the last, so we just do `iter=-1` to override the previous value. 

58 """ 

59 

60 def post_iteration(self, step, level_number): 

61 super().post_iteration(step, level_number) 

62 L = step.levels[level_number] 

63 self.add_to_stats( 

64 process=step.status.slot, 

65 time=L.time + L.dt, 

66 level=L.level_index, 

67 iter=-1, 

68 sweep=L.status.sweep, 

69 type='uold', 

70 value=L.uold[-1], 

71 ) 

72 

73 

74class LogUAllIter(Hooks): 

75 """ 

76 Log solution and errors after each iteration 

77 """ 

78 

79 def post_iteration(self, step, level_number): 

80 super(LogUAllIter, self).post_iteration(step, level_number) 

81 

82 L = step.levels[level_number] 

83 L.sweep.compute_end_point() 

84 

85 self.add_to_stats( 

86 process=step.status.slot, 

87 time=L.time, 

88 level=L.level_index, 

89 iter=step.status.iter, 

90 sweep=L.status.sweep, 

91 type='u', 

92 value=L.uend, 

93 ) 

94 self.add_to_stats( 

95 process=step.status.slot, 

96 time=L.time, 

97 level=L.level_index, 

98 iter=step.status.iter, 

99 sweep=L.status.sweep, 

100 type='error_embedded_estimate', 

101 value=L.status.get('error_embedded_estimate'), 

102 ) 

103 self.add_to_stats( 

104 process=step.status.slot, 

105 time=L.time, 

106 level=L.level_index, 

107 iter=step.status.iter, 

108 sweep=L.status.sweep, 

109 type='error_extrapolation_estimate', 

110 value=L.status.get('error_extrapolation_estimate'), 

111 )