Coverage for pySDC/projects/TOMS/AllenCahn_monitor.py: 100%

35 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-04-29 09:02 +0000

1import numpy as np 

2 

3from pySDC.core.Hooks import hooks 

4 

5 

6class monitor(hooks): 

7 def __init__(self): 

8 """ 

9 Initialization of Allen-Cahn monitoring 

10 """ 

11 super(monitor, self).__init__() 

12 

13 self.init_radius = None 

14 

15 def pre_run(self, step, level_number): 

16 """ 

17 Overwrite standard pre run hook 

18 

19 Args: 

20 step (pySDC.Step.step): the current step 

21 level_number (int): the current level number 

22 """ 

23 super(monitor, self).pre_run(step, level_number) 

24 L = step.levels[0] 

25 

26 c = np.count_nonzero(L.u[0] > 0.0) 

27 radius = np.sqrt(c / np.pi) * L.prob.dx 

28 

29 radius1 = 0 

30 rows, cols = np.where(L.u[0] > 0.0) 

31 for r in rows: 

32 radius1 = max(radius1, abs(L.prob.xvalues[r])) 

33 

34 rows1 = np.where(L.u[0][int((L.prob.init[0][0]) / 2), : int((L.prob.init[0][0]) / 2)] > -0.99) 

35 rows2 = np.where(L.u[0][int((L.prob.init[0][0]) / 2), : int((L.prob.init[0][0]) / 2)] < 0.99) 

36 interface_width = (rows2[0][-1] - rows1[0][0]) * L.prob.dx / L.prob.eps 

37 

38 self.init_radius = L.prob.radius 

39 

40 if L.time == 0.0: 

41 self.add_to_stats( 

42 process=step.status.slot, 

43 time=L.time, 

44 level=-1, 

45 iter=step.status.iter, 

46 sweep=L.status.sweep, 

47 type='computed_radius', 

48 value=radius, 

49 ) 

50 self.add_to_stats( 

51 process=step.status.slot, 

52 time=L.time, 

53 level=-1, 

54 iter=step.status.iter, 

55 sweep=L.status.sweep, 

56 type='exact_radius', 

57 value=self.init_radius, 

58 ) 

59 self.add_to_stats( 

60 process=step.status.slot, 

61 time=L.time, 

62 level=-1, 

63 iter=step.status.iter, 

64 sweep=L.status.sweep, 

65 type='interface_width', 

66 value=interface_width, 

67 ) 

68 

69 def post_step(self, step, level_number): 

70 """ 

71 Overwrite standard post step hook 

72 

73 Args: 

74 step (pySDC.Step.step): the current step 

75 level_number (int): the current level number 

76 """ 

77 super(monitor, self).post_step(step, level_number) 

78 

79 # some abbreviations 

80 L = step.levels[0] 

81 

82 c = np.count_nonzero(L.uend >= 0.0) 

83 radius = np.sqrt(c / np.pi) * L.prob.dx 

84 

85 exact_radius = np.sqrt(max(self.init_radius**2 - 2.0 * (L.time + L.dt), 0)) 

86 rows1 = np.where(L.uend[int((L.prob.init[0][0]) / 2), : int((L.prob.init[0][0]) / 2)] > -0.99) 

87 rows2 = np.where(L.uend[int((L.prob.init[0][0]) / 2), : int((L.prob.init[0][0]) / 2)] < 0.99) 

88 interface_width = (rows2[0][-1] - rows1[0][0]) * L.prob.dx / L.prob.eps 

89 

90 self.add_to_stats( 

91 process=step.status.slot, 

92 time=L.time + L.dt, 

93 level=-1, 

94 iter=step.status.iter, 

95 sweep=L.status.sweep, 

96 type='computed_radius', 

97 value=radius, 

98 ) 

99 self.add_to_stats( 

100 process=step.status.slot, 

101 time=L.time + L.dt, 

102 level=-1, 

103 iter=step.status.iter, 

104 sweep=L.status.sweep, 

105 type='exact_radius', 

106 value=exact_radius, 

107 ) 

108 self.add_to_stats( 

109 process=step.status.slot, 

110 time=L.time + L.dt, 

111 level=-1, 

112 iter=step.status.iter, 

113 sweep=L.status.sweep, 

114 type='interface_width', 

115 value=interface_width, 

116 )