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

64 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-04-29 09:02 +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, while the convergence controllers are allowed to add more variables in a controlled fashion 

25 later on using the `add_variable` function. 

26 """ 

27 

28 def __init__(self): 

29 self.residual = None 

30 self.unlocked = False 

31 self.updated = False 

32 self.time = None 

33 self.dt_new = None 

34 self.sweep = None 

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

36 self._freeze() 

37 

38 

39class level(FrozenClass): 

40 """ 

41 Level class containing all management functionality for a single level 

42 

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

44 know about other levels. 

45 

46 Attributes: 

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

48 status (__Status): status object 

49 level_index (int): custom string naming this level 

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

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

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

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

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

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

56 """ 

57 

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

59 """ 

60 Initialization routine 

61 

62 Args: 

63 problem_class: problem class 

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

65 sweeper_class: sweeper class 

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

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

68 level_index (int): custom name for this level 

69 """ 

70 

71 # instantiate sweeper, problem and hooks 

72 self.__sweep = sweeper_class(sweeper_params) 

73 self.__prob = problem_class(**problem_params) 

74 

75 # set level parameters and status 

76 self.params = _Pars(level_params) 

77 self.status = _Status() 

78 

79 # set name 

80 self.level_index = level_index 

81 

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

83 self.uend = None 

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

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

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

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

88 

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

90 

91 # pass this level to the sweeper for easy access 

92 self.sweep.level = self 

93 

94 self.__tag = None 

95 

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

97 self._freeze() 

98 

99 def reset_level(self, reset_status=True): 

100 """ 

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

102 

103 Args: 

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

105 

106 Returns: 

107 None 

108 """ 

109 

110 # reset status 

111 if reset_status: 

112 self.status = _Status() 

113 

114 # all data back to None 

115 self.uend = None 

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

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

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

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

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

121 

122 @property 

123 def sweep(self): 

124 """ 

125 Getter for the sweeper 

126 

127 Returns: 

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

129 """ 

130 return self.__sweep 

131 

132 @property 

133 def prob(self): 

134 """ 

135 Getter for the problem 

136 

137 Returns: 

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

139 """ 

140 return self.__prob 

141 

142 @property 

143 def time(self): 

144 """ 

145 Meta-getter for the current time 

146 

147 Returns: 

148 float: referencing status time for convenience 

149 """ 

150 return self.status.time 

151 

152 @property 

153 def dt(self): 

154 """ 

155 Meta-getter for the time-step size 

156 

157 Returns: 

158 float: referencing dt from parameters for convenience 

159 """ 

160 return self.params.dt 

161 

162 @property 

163 def tag(self): 

164 """ 

165 Getter for tag 

166 

167 Returns: 

168 tag for sending/receiving 

169 """ 

170 return self.__tag 

171 

172 @tag.setter 

173 def tag(self, t): 

174 """ 

175 Setter for tag 

176 

177 Args: 

178 t: new tag for sending/receiving 

179 """ 

180 self.__tag = t