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
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-20 14:51 +0000
1from pySDC.helpers.pysdc_helper import FrozenClass
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()
17 self.dt_initial = self.dt * 1.0 if self.dt is not None else None
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 """
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()
38class Level(FrozenClass):
39 """
40 Level class containing all management functionality for a single level
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.
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 """
57 def __init__(self, problem_class, problem_params, sweeper_class, sweeper_params, level_params, level_index):
58 """
59 Initialization routine
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 """
70 # instantiate sweeper, problem and hooks
71 self.__sweep = sweeper_class(sweeper_params)
72 self.__prob = problem_class(**problem_params)
74 # set level parameters and status
75 self.params = _Pars(level_params)
76 self.status = _Status()
78 # set name
79 self.level_index = level_index
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)
88 self.tau = [None] * self.sweep.coll.num_nodes
90 # pass this level to the sweeper for easy access
91 self.sweep.level = self
93 self.__tag = None
95 # freeze class, no further attributes allowed from this point
96 self._freeze()
98 def reset_level(self, reset_status=True):
99 """
100 Routine to clean-up the level for the next time step
102 Args:
103 reset_status (bool): Reset the status or only the solution
105 Returns:
106 None
107 """
109 # reset status
110 if reset_status:
111 self.status = _Status()
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
121 @property
122 def sweep(self):
123 """
124 Getter for the sweeper
126 Returns:
127 pySDC.Sweeper.sweeper: the sweeper associated to this level
128 """
129 return self.__sweep
131 @property
132 def prob(self):
133 """
134 Getter for the problem
136 Returns:
137 pySDC.Problem.ptype: the problem associated to this level
138 """
139 return self.__prob
141 @property
142 def time(self):
143 """
144 Meta-getter for the current time
146 Returns:
147 float: referencing status time for convenience
148 """
149 return self.status.time
151 @property
152 def dt(self):
153 """
154 Meta-getter for the time-step size
156 Returns:
157 float: referencing dt from parameters for convenience
158 """
159 return self.params.dt
161 @property
162 def tag(self):
163 """
164 Getter for tag
166 Returns:
167 tag for sending/receiving
168 """
169 return self.__tag
171 @tag.setter
172 def tag(self, t):
173 """
174 Setter for tag
176 Args:
177 t: new tag for sending/receiving
178 """
179 self.__tag = t