Coverage for pySDC/core/level.py: 100%

64 statements  

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

1from pySDC.helpers.pysdc_helper import FrozenClass 

2 

3 

4# short helper class to add params as attributes 

5class _Pars(FrozenClass): 

6 def __init__(self, params): 

7 self.dt = None 

8 self.dt_initial = None 

9 self.restol = -1.0 

10 self.nsweeps = 1 

11 self.residual_type = 'full_abs' 

12 for k, v in params.items(): 

13 setattr(self, k, v) 

14 # freeze class, no further attributes allowed from this point 

15 self._freeze() 

16 

17 self.dt_initial = self.dt * 1.0 if self.dt is not None else None 

18 

19 

20# short helper class to bundle all status variables 

21class _Status(FrozenClass): 

22 """ 

23 This class carries the status of the level. All variables that the core SDC / PFASST functionality depend on are 

24 initialized here. 

25 """ 

26 

27 def __init__(self): 

28 self.residual = None 

29 self.unlocked = False 

30 self.updated = False 

31 self.time = None 

32 self.dt_new = None 

33 self.sweep = None 

34 # freeze class, no further attributes allowed from this point 

35 self._freeze() 

36 

37 

38class Level(FrozenClass): 

39 """ 

40 Level class containing all management functionality for a single level 

41 

42 A level contains all data structures, types and objects to perform sweeps on this particular level. It does not 

43 know about other levels. 

44 

45 Attributes: 

46 params (__Pars): parameter object containing the custom parameters passed by the user 

47 status (__Status): status object 

48 level_index (int): custom string naming this level 

49 uend: dof values at the right end point of the interval 

50 u (list of dtype_u): dof values at the nodes 

51 uold (list of dtype_u): copy of dof values for saving data during restriction) 

52 f (list of dtype_f): RHS values at the nodes 

53 fold (list of dtype_f): copy of RHS values for saving data during restriction 

54 tau (list of dtype_u): FAS correction, allocated via step class if necessary 

55 """ 

56 

57 def __init__(self, problem_class, problem_params, sweeper_class, sweeper_params, level_params, level_index): 

58 """ 

59 Initialization routine 

60 

61 Args: 

62 problem_class: problem class 

63 problem_params (dict): parameters for the problem to be initialized 

64 sweeper_class: sweeper class 

65 sweeper_params (dict): parameters for the sweeper (contains collocation) 

66 level_params (dict): parameters given by the user, will be added as attributes 

67 level_index (int): custom name for this level 

68 """ 

69 

70 # instantiate sweeper, problem and hooks 

71 self.__sweep = sweeper_class(sweeper_params) 

72 self.__prob = problem_class(**problem_params) 

73 

74 # set level parameters and status 

75 self.params = _Pars(level_params) 

76 self.status = _Status() 

77 

78 # set name 

79 self.level_index = level_index 

80 

81 # empty data at the nodes, the right end point and tau 

82 self.uend = None 

83 self.u = [None] * (self.sweep.coll.num_nodes + 1) 

84 self.uold = [None] * (self.sweep.coll.num_nodes + 1) 

85 self.f = [None] * (self.sweep.coll.num_nodes + 1) 

86 self.fold = [None] * (self.sweep.coll.num_nodes + 1) 

87 

88 self.tau = [None] * self.sweep.coll.num_nodes 

89 

90 # pass this level to the sweeper for easy access 

91 self.sweep.level = self 

92 

93 self.__tag = None 

94 

95 # freeze class, no further attributes allowed from this point 

96 self._freeze() 

97 

98 def reset_level(self, reset_status=True): 

99 """ 

100 Routine to clean-up the level for the next time step 

101 

102 Args: 

103 reset_status (bool): Reset the status or only the solution 

104 

105 Returns: 

106 None 

107 """ 

108 

109 # reset status 

110 if reset_status: 

111 self.status = _Status() 

112 

113 # all data back to None 

114 self.uend = None 

115 self.u = [None] * (self.sweep.coll.num_nodes + 1) 

116 self.uold = [None] * (self.sweep.coll.num_nodes + 1) 

117 self.f = [None] * (self.sweep.coll.num_nodes + 1) 

118 self.fold = [None] * (self.sweep.coll.num_nodes + 1) 

119 self.tau = [None] * self.sweep.coll.num_nodes 

120 

121 @property 

122 def sweep(self): 

123 """ 

124 Getter for the sweeper 

125 

126 Returns: 

127 pySDC.Sweeper.sweeper: the sweeper associated to this level 

128 """ 

129 return self.__sweep 

130 

131 @property 

132 def prob(self): 

133 """ 

134 Getter for the problem 

135 

136 Returns: 

137 pySDC.Problem.ptype: the problem associated to this level 

138 """ 

139 return self.__prob 

140 

141 @property 

142 def time(self): 

143 """ 

144 Meta-getter for the current time 

145 

146 Returns: 

147 float: referencing status time for convenience 

148 """ 

149 return self.status.time 

150 

151 @property 

152 def dt(self): 

153 """ 

154 Meta-getter for the time-step size 

155 

156 Returns: 

157 float: referencing dt from parameters for convenience 

158 """ 

159 return self.params.dt 

160 

161 @property 

162 def tag(self): 

163 """ 

164 Getter for tag 

165 

166 Returns: 

167 tag for sending/receiving 

168 """ 

169 return self.__tag 

170 

171 @tag.setter 

172 def tag(self, t): 

173 """ 

174 Setter for tag 

175 

176 Args: 

177 t: new tag for sending/receiving 

178 """ 

179 self.__tag = t