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

29 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-03-27 07:06 +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 

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

8 

9 

10class LogData(Hooks): 

11 """ 

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

13 """ 

14 

15 def pre_run(self, step, level_number): 

16 """ 

17 Record initial conditions 

18 """ 

19 super().pre_run(step, level_number) 

20 

21 L = step.levels[level_number] 

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

23 

24 def post_step(self, step, level_number): 

25 """ 

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

27 """ 

28 super().post_step(step, level_number) 

29 

30 L = step.levels[level_number] 

31 

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

33 self.increment_stats( 

34 process=step.status.slot, 

35 time=L.time, 

36 level=L.level_index, 

37 iter=step.status.iter, 

38 sweep=L.status.sweep, 

39 type='sweeps', 

40 value=step.status.iter, 

41 ) 

42 self.increment_stats( 

43 process=step.status.slot, 

44 time=L.time + L.dt, 

45 level=L.level_index, 

46 iter=step.status.iter, 

47 sweep=L.status.sweep, 

48 type='k', 

49 value=step.status.iter, 

50 ) 

51 

52 

53class LogUold(Hooks): 

54 """ 

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

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

57 """ 

58 

59 def post_iteration(self, step, level_number): 

60 super().post_iteration(step, level_number) 

61 L = step.levels[level_number] 

62 self.add_to_stats( 

63 process=step.status.slot, 

64 time=L.time + L.dt, 

65 level=L.level_index, 

66 iter=-1, 

67 sweep=L.status.sweep, 

68 type='uold', 

69 value=L.uold[-1], 

70 ) 

71 

72 

73class LogUAllIter(Hooks): 

74 """ 

75 Log solution and errors after each iteration 

76 """ 

77 

78 def post_iteration(self, step, level_number): 

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

80 

81 L = step.levels[level_number] 

82 L.sweep.compute_end_point() 

83 

84 self.add_to_stats( 

85 process=step.status.slot, 

86 time=L.time, 

87 level=L.level_index, 

88 iter=step.status.iter, 

89 sweep=L.status.sweep, 

90 type='u', 

91 value=L.uend, 

92 ) 

93 self.add_to_stats( 

94 process=step.status.slot, 

95 time=L.time, 

96 level=L.level_index, 

97 iter=step.status.iter, 

98 sweep=L.status.sweep, 

99 type='error_embedded_estimate', 

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

101 ) 

102 self.add_to_stats( 

103 process=step.status.slot, 

104 time=L.time, 

105 level=L.level_index, 

106 iter=step.status.iter, 

107 sweep=L.status.sweep, 

108 type='error_extrapolation_estimate', 

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

110 )