Peano
Loading...
Searching...
No Matches
FOSOReconstructionSolver.py
Go to the documentation of this file.
1""" This file is part of the ExaGRyPE project. For conditions of distribution and
2 use, please see the copyright notice at www.peano-framework.org
3"""
4
5import exahype2.api
6import peano4.api
10
11from .actionsets import ApplyReconstructionFunctor
12
13
15 """!
16
17 Reconstructs the first derivatives which exagrype.api.FOSOCCZ4Solver needs
18
19 The solver is the second partner in the ping-pong of the first-/second-order
20 reconstructed CCZ4 formulation (@ref page_exagrype_fo_so_ccz4). Where the
21 CCZ4 solver sees 25 unknowns and 33 auxiliary variables (the derivatives),
22 the reconstruction solver sees the same 58 entries the other way round: its
23 33 unknowns are the derivatives, and its 25 auxiliary variables are the
24 unknowns of the CCZ4 solver. Nothing is copied: the derivatives are the
25 very patch that the CCZ4 solver holds as auxiliary variables "Derivatives",
26 and the reconstruction reads the CCZ4 solver's patch.
27
28 It is a real exahype2.api.Solver (it has a solver class and a global
29 instance in the solver repository), yet it is much simpler than a normal
30 solver:
31
32 - It has no PDE terms and no compute kernel. Its only compute step
33 evaluates the stencil on the unknowns of the partner.
34 - It computes no time step size. Its C++ class forwards the time stamps
35 and time step sizes of the CCZ4 solver, so the global minimum over all
36 solvers is the one of the CCZ4 solver.
37 - It does neither add lopsided advection nor enforce constraints.
38 - It has no grid construction of its own, no plotter and no mesh
39 adaptation. The CCZ4 solver builds the mesh and initialises the
40 derivatives.
41
42 Users do not create the solver themselves: exagrype.api.FOSOSolver
43 creates it and realises it right after the CCZ4 solver. The solver has no
44 numerical scheme of its own: the scheme of the host builds the CCZ4 solver's
45 graph and asks this solver for its graph per stage (create_task_graph()). The reconstruction
46 has to be realised after the CCZ4 solver, as it uses its data.
47
48 ## Task graph
49
50 For each stage of the Runge-Kutta scheme of the CCZ4 solver (once for FV),
51 the solver creates a chain of tasks in a task graph of its own:
52
53 1. Interpolate the coarse face data of the primary variables and of the
54 derivatives onto hanging faces (two parallel tasks). From the point of view
55 of this solver, the primary variables are auxiliary variables that are to be
56 interpolated, and the derivative variables are the unknowns.
57 2. Reconstruct the primary variables and the derivative variables
58 (halo from the cell and the faces). The two tasks run in parallel,
59 and each waits for its interpolation.
60 3. Apply the stencil, i.e. recompute the derivatives from the primary variables.
61 4. Project the derivatives onto the faces.
62 5. Restrict them on hanging faces, but only if the derivatives' mode
63 (@ref exahype2.api.AuxiliaryVariables.Mode) is CHANGING_ON_FACES, which it is.
64 6. Roll the face data over.
65
66 The graph has no dependencies between stages. The host concatenates the graph with the
67 one of the CCZ4 solver (exagrype.api.FOSOSolver._couple()): The first tasks of
68 a stage wait for what the CCZ4 solver's interpolation of the derivatives
69 in this stage waited for, and the interpolation of the primary variables
70 additionally waits for the CCZ4 solver's interpolation of the unknowns (both
71 write the same faces).
72 The chain is a block between the CCZ4 solver's interpolation of the unknowns and
73 the rest of the stage: The CCZ4 solver's interpolation of the derivatives
74 (and thus the reconstruction of their halos) and its reconstruction of
75 the unknowns wait for the last task. In terms of
76 @ref page_exahype_programmers_guide_coupling, this is a multiplicative
77 coupling, with the coupling repeated per stage.
78
79 ## Shortcomings
80
81 - Domain boundaries other than periodic ones and Sommerfeld boundary
82 conditions are not supported, as the reconstruction reads the halo of the
83 CCZ4 solver's unknowns before the Dirichlet boundary conditions
84 are applied. The reconstruction takes the same dependencies as the
85 CCZ4 solver's reconstruction, which makes it work in principle, but this has
86 not been tested.
87 - Supported are exahype2.api.rkfd4.CentralDifferencesWithRungeKutta
88 (fourth-order stencil, halo of at least three grid cells, one task per
89 Runge-Kutta stage) and exahype2.api.fv.RusanovWithExplicitEuler
90 (second-order central differences, overlap of one is enough, a single
91 task as there is a single stage). The optimistic Finite Volume scheme is
92 not supported.
93
94 """
95
96 class NoGridConstruction(exahype2.api.Solver.GridConstruction):
97 """!
98
99 The reconstruction solver builds no mesh
100
101 The CCZ4 solver constructs the mesh, and initialises the derivatives
102 which are the unknowns of the reconstruction solver.
103
104 """
105
106 def construct_output(self, exahype2_project, peano_project, setup_simulation):
107 pass
108
109 def __init__(self, name: str, ccz4: "FOSOCCZ4Solver"):
110 """!
111
112 @param name: str
113 Name of the solver, @see exahype2.api.Solver.
114
115 @param ccz4: exagrype.api.FOSOCCZ4Solver
116 The CCZ4 solver whose derivatives this solver reconstructs.
117
118 """
119 if not hasattr(ccz4, "derivatives"):
120 raise TypeError(
121 f"The reconstruction serves a FOSOCCZ4Solver, but got {type(ccz4).__name__}"
122 )
123 super().__init__(name, dict(ccz4.DerivativeUnknowns))
124 self.ccz4 = ccz4
125 """! The partner whose derivatives we reconstruct """
126 self.grid_selector = ccz4.grid_selector
127 self._scratch_halos = None
129 self.add_user_solver_includes('\n#include "repositories/SolverRepository.h"\n')
131
132 @property
133 def task_graph_title_suffix(self) -> str:
134 return f" (reconstruction part of {self.ccz4.name})"
135
137 """!
138
139 The solver does not time step. It therefore hands out what the CCZ4
140 solver hands out. Otherwise, the global minimum and maximum which the
141 solver repository evaluates over all solvers would be spoilt.
142
143 """
144 for getter in [
145 "getMinTimeStamp",
146 "getMaxTimeStamp",
147 "getMinTimeStepSize",
148 "getMaxTimeStepSize",
149 "getAdmissibleTimeStepSize",
150 ]:
152 f"double {getter}() const override;",
153 lambda qualified_class_name, getter=getter: (
154 f"double {qualified_class_name}::{getter}() const {{\n"
155 f" return repositories::{self.ccz4.get_name_of_global_instance()}.{getter}();\n"
156 "}\n"
157 ),
158 )
159
160 def _layout(self):
161 """!
162
163 Validate the scheme of the CCZ4 solver and return how to reconstruct
164
165 @return scheme, actionsets, reconstruction_function, stages, per_stage
166
167 """
168 reconstruction = self
169 ccz4 = reconstruction.ccz4
170 name = reconstruction.name
171 scheme = ccz4.numerical_scheme
172 match scheme:
174 if scheme.HALO_SIZE < 3:
175 raise ValueError(
176 f"Reconstruction {name}: the halo of the scheme of {ccz4.name} has to be "
177 f"at least three, but is {scheme.HALO_SIZE}. The solver evaluates fluxes and "
178 "non-conservative products on the faces of the cell, i.e. it needs the "
179 "derivatives one layer outside of the cell, and the fourth-order stencil reaches "
180 "two further layers. Use a scheme with lopsided advection or "
181 "exagrype.api.FD4WithKODissipation."
182 )
184 reconstruction_function = "reconstructFirstDerivativesFD4"
185 stages = scheme.rk_order
186 per_stage = True
188 # Second-order central differences reach one layer out, and the
189 # derivatives of the halo come from the neighbour via the faces
190 actionsets = exahype2.api.fv.actionsets
191 reconstruction_function = (
192 "reconstructFirstDerivativesCentralDifferences"
193 )
194 stages = 1
195 per_stage = False
196 case _:
197 raise TypeError(
198 f"Reconstruction {name}: solver {ccz4.name} has to use "
199 "exahype2.api.rkfd4.CentralDifferencesWithRungeKutta (or a subclass) or "
200 "exahype2.api.fv.RusanovWithExplicitEuler as numerical scheme, "
201 f"but uses {type(scheme).__name__}"
202 )
203 derivatives = ccz4.derivatives
204 if derivatives.patch is None:
205 raise ValueError(
206 f"Reconstruction {name}: solver {ccz4.name} has not been realised yet. "
207 "The CCZ4 solver has to be realised before its reconstruction."
208 )
209 return scheme, actionsets, reconstruction_function, stages, per_stage
210
211 def partner_task_names(self, stage: int):
212 """!
213
214 Names of the tasks of the CCZ4 solver which the reconstruction of a stage interacts with
215
216 @param stage: int
217 Stage of the Runge-Kutta scheme, 0 for Finite Volumes.
218
219 @return unknowns_interpolation, derivatives_interpolation, reconstruction
220 Names of the tasks that interpolate the unknowns onto hanging faces,
221 interpolate the derivatives, and reconstruct the unknowns (halo).
222 The first reconstruction tasks of the stage wait for what the second one
223 waits for, and the second and third one wait for the last
224 reconstruction task. @ref exagrype.api.FOSOSolver._couple()
225
226 """
227 ccz4 = self.ccz4
228 suffix = f" stage {stage}" if self._layout()[4] else ""
229 return (
230 f"{ccz4.name}_Interpolate hanging faces{suffix}",
231 f"{ccz4.name}_Interpolate {ccz4.derivatives.name} on hanging faces{suffix}",
232 f"{ccz4.name}_Reconstruct{suffix}",
233 )
234
236 self, exahype2_project, peano_project, stage: int, number_of_stages: int
237 ):
238 """!
239
240 Create the chain of reconstruction tasks of one stage
241
242 The CCZ4 solver has to be realised before, as the routine needs its
243 data. The graph holds one chain of tasks, without any dependency on the
244 CCZ4 solver's tasks. Its sources are the two interpolation tasks, its only
245 sink the roll over. The host (exagrype.api.FOSOSolver) wires the chain
246 into the stage of the CCZ4 solver.
247
248 @param exahype2_project: exahype2.api.Project
249 The project that hosts the solvers (unused).
250
251 @param peano_project: peano4.api.Project
252 The Peano project that the exahype2 project assembles.
253
254 @param stage: int
255 Stage of the Runge-Kutta scheme of the CCZ4 solver, 0 for Finite Volumes.
256
257 @param number_of_stages: int
258 Number of stages (unused).
259
260 @return peano4.api.taskgraph.Graph
261
262 """
263 reconstruction = self
264 ccz4 = self.ccz4
265 name = self.name
266 scheme, actionsets, reconstruction_function, stages, per_stage = self._layout()
267 derivatives = ccz4.derivatives
269 Access = peano4.api.taskgraph.Task.DataAccess
270 Entity = peano4.api.taskgraph.Task.GridEntity
271 n = scheme.patch_size
272 h = scheme.HALO_SIZE if per_stage else scheme.overlap
273 cell_guard = ccz4.grid_selector.createCellGuard()
274 face_guard = ccz4.grid_selector.createFaceGuard()
275 includes = (
276 ccz4._get_default_includes()
277 + ccz4.user_solver_includes
278 + '\n#include "exagrype/ccz4/SecondOrderAuxiliaryVariablesReconstruction.h"\n'
279 )
280
281 # The reconstructed primary variables and derivative variables.
282 # Nobody besides the tasks of the stage reads them, i.e. they are
283 # scratch arrays.
284 def create_scratch_halo(unknowns, halo_name):
286 (n + 2 * h, n + 2 * h, n + 2 * h), unknowns, halo_name
287 )
288 halo.grid_selector = ccz4.grid_selector
290 halo, "double"
291 )
292 peano_project.datamodel.add_cell(halo)
293 return halo
294
295 # One pair for all stages
296 if self._scratch_halos is None:
297 self._scratch_halos = (
298 create_scratch_halo(ccz4.unknowns, name + "PrimaryVariablesWithHalo"),
299 create_scratch_halo(
300 derivatives.size, name + derivatives.data_name + "WithHalo"
301 ),
302 )
303 primary_halo, derivative_halo = self._scratch_halos
304
305 functor = (
306 f"::exagrype::ccz4::{reconstruction_function}("
307 f"oldQWithHalo, marker.h(), {n}, {h}, newQ);"
308 )
309
310 stage_input = ccz4._patch if stage == 0 else ccz4._stage_input
311 suffix = f" stage {stage}" if per_stage else ""
312
313 def create_task(task_name):
315 name=f"{name}_{task_name}{suffix}", entity=Entity.CELL
316 )
317 graph.add_task(task)
318 return task
319
320
324 reconstruct_primary = create_task("Reconstruct primary variables")
325 reconstruct_primary.use_cell(stage_input, Access.READ_AND_WRITE)
326 reconstruct_primary.use_cell(primary_halo, Access.WRITE)
327 reconstruct_primary.use_face(
328 ccz4._patch_overlap_current_time_step, Access.READ_AND_WRITE
329 )
330 reconstruct_primary.add_action_set(
331 toolbox.blockstructured.api.ReconstructPatchWithHalo(
332 patch=stage_input,
333 patch_overlap=ccz4._patch_overlap_current_time_step,
334 reconstructed_patch=primary_halo,
335 guard=cell_guard,
336 add_assertions_to_halo_exchange=False,
337 additional_includes=includes,
338 )
339 )
340
344 interpolate_primary = create_task(
345 "Interpolate primary variables on hanging faces"
346 )
347 interpolate_primary.use_face(
348 ccz4._patch_overlap_current_time_step, Access.READ_AND_WRITE
349 )
350 interpolate_primary.add_action_set(
351 toolbox.blockstructured.api.InterpolateHangingFaces(
352 ccz4._patch_overlap_current_time_step,
353 scheme=scheme.interpolation,
354 additional_includes=includes,
355 )
356 )
357 interpolate_derivatives = create_task(
358 "Interpolate derivative variables on hanging faces"
359 )
360 interpolate_derivatives.use_face(
361 derivatives.patch_overlap_current_time_step, Access.READ_AND_WRITE
362 )
363 interpolate_derivatives.add_action_set(
364 toolbox.blockstructured.api.InterpolateHangingFaces(
365 derivatives.patch_overlap_current_time_step,
366 scheme=scheme.interpolation,
367 additional_includes=includes,
368 )
369 )
370 reconstruct_derivatives = create_task("Reconstruct derivative variables")
371 reconstruct_derivatives.use_cell(derivatives.patch, Access.READ_AND_WRITE)
372 reconstruct_derivatives.use_cell(derivative_halo, Access.WRITE)
373 reconstruct_derivatives.use_face(
374 derivatives.patch_overlap_current_time_step, Access.READ_AND_WRITE
375 )
376 reconstruct_derivatives.add_action_set(
377 toolbox.blockstructured.api.ReconstructPatchWithHalo(
378 patch=derivatives.patch,
379 patch_overlap=derivatives.patch_overlap_current_time_step,
380 reconstructed_patch=derivative_halo,
381 guard=cell_guard,
382 add_assertions_to_halo_exchange=False,
383 additional_includes=includes,
384 )
385 )
386
387
388 apply_stencil = create_task("Reconstruct derivatives")
389 apply_stencil.use_cell(primary_halo, Access.READ_AND_DISCARD)
390 apply_stencil.use_cell(derivative_halo, Access.READ_AND_DISCARD)
391 apply_stencil.use_cell(derivatives.patch, Access.READ_AND_WRITE)
392 apply_stencil.add_action_set(
393 ApplyReconstructionFunctor(
394 patch=derivatives.patch,
395 primary_variables_with_halo=primary_halo,
396 derivative_variables_with_halo=derivative_halo,
397 functor_implementation=functor,
398 guard=cell_guard,
399 additional_includes=includes,
400 )
401 )
402 apply_stencil.add_dependency(reconstruct_primary)
403 apply_stencil.add_dependency(reconstruct_derivatives)
404
405
406 project = create_task("Project derivative variables onto faces")
407 project.use_cell(derivatives.patch, Access.READ_AND_WRITE)
408 project.use_face(derivatives.patch_overlap_update, Access.TEMPORARY)
409
413 project.use_face(derivatives.patch_overlap_current_time_step, Access.WRITE)
414 project.add_action_set(
415 actionsets.ProjectPatchOntoFaces(
416 derivatives.patch,
417 derivatives.patch_overlap_update,
418 guard=cell_guard,
419 includes=includes,
420 )
421 )
422 project.add_dependency(apply_stencil)
423 last_task = project
424
425
428 if derivatives.mode == exahype2.api.AuxiliaryVariables.Mode.CHANGING_ON_FACES:
429 restrict = create_task("Restrict derivative variables on hanging faces")
430 restrict.use_face(derivatives.patch_overlap_update, Access.TEMPORARY)
431 restrict.add_action_set(
432 toolbox.blockstructured.api.RestrictHangingFaces(
433 derivatives.patch_overlap_update,
434 scheme=scheme.restriction,
435 clear_guard=face_guard,
436 additional_includes=includes,
437 )
438 )
439 restrict.add_dependency(last_task)
440 last_task = restrict
441
442
443 roll_over = create_task("Roll over derivative variable faces")
444 roll_over.use_face(derivatives.patch_overlap_update, Access.TEMPORARY)
445 roll_over.use_face(
446 derivatives.patch_overlap_current_time_step, Access.TEMPORARY
447 )
448 roll_over.add_action_set(
449 actionsets.RollOverUpdatedFace(
450 derivatives.patch_overlap_current_time_step,
451 derivatives.patch_overlap_update,
452 guard=face_guard,
453 includes=includes,
454 )
455 )
456 roll_over.add_dependency(last_task)
457
458 reconstruct_derivatives.add_dependency(interpolate_derivatives)
459 reconstruct_primary.add_dependency(interpolate_primary)
460 return graph
461
462 def create_readme_descriptor(self, domain_offset, domain_size) -> str:
463 return (
464 f"Reconstruction {self.name}: reconstructs the {self.unknowns} first "
465 f"derivatives of solver {self.ccz4.name} from its {self.ccz4.unknowns} unknowns"
466 )
Reconstructs the first derivatives which exagrype.api.FOSOCCZ4Solver needs.
_layout(self)
Validate the scheme of the CCZ4 solver and return how to reconstruct.
partner_task_names(self, int stage)
Names of the tasks of the CCZ4 solver which the reconstruction of a stage interacts with.
str create_readme_descriptor(self, domain_offset, domain_size)
Return a human-readable description of this solver instance, to be embedded into the generated projec...
create_task_graph(self, exahype2_project, peano_project, int stage, int number_of_stages)
Create the chain of reconstruction tasks of one stage.
Abstract base class for all ExaHyPE 2 solvers.
Definition Solver.py:17
add_abstract_solver_method(self, str declaration, Callable[[str], str] definition_generator)
Add a raw C++ method to the generated Abstract<SolverName> class: declaration goes into the class bod...
Definition Solver.py:976
add_user_solver_includes(self, str value)
Add further includes to this property, if your solver requires some additional routines from other he...
Definition Solver.py:825
str name(self)
Unique solver name.
Definition Solver.py:653
Finite Volumes with a Rusanov Riemann solver and explicit Euler time stepping, realised over a block-...
Fourth-order central Finite Differences with explicit Runge-Kutta time stepping, realised over a bloc...
Patch implementation of a degree of freedom.
Definition Patch.py:7
Holds a sequence of Task instances.
Definition Graph.py:9
One task within a peano4.api.taskgraph.Graph.
Definition Task.py:8
The task graph abstraction allows users to formalise their solver steps over small task graphs which ...
Definition __init__.py:1