Peano
Loading...
Searching...
No Matches
test_rkfd4_time_step.py
Go to the documentation of this file.
1import types
2
3import pytest
4
5import peano4.api
8
9import exahype2.api
10
11
12def _solver(time_step_size=None, rk_order=3, boundary="dirichlet"):
13 """!
14
15 @param boundary: str
16 "dirichlet", "sommerfeld", or None for a solver without boundary
17 condition term, i.e. for a domain that is periodic along all axes.
18
19 """
20 solver = exahype2.api.Solver("Probe", 2)
21 solver.add_pde_term(exahype2.api.Flux(implementation="F[0] = Q[0]; F[1] = Q[1];"))
22 solver.add_pde_term(exahype2.api.MaxEigenvalue(implementation="*lambdaMax = 1.0;"))
23 if boundary == "dirichlet":
24 solver.add_pde_term(
25 exahype2.api.DirichletBoundaryConditions(implementation="Q[0] = 0.0;")
26 )
27 elif boundary == "sommerfeld":
28 solver.add_pde_term(
29 exahype2.api.SommerfeldBoundaryConditions(implementation="Q[0] = 0.0;")
30 )
32 solver, patch_size=4, rk_order=rk_order, time_step_size=time_step_size
33 )
34 return solver
35
36
37def _construct_time_step(solver, directory, periodic_BC=(False, False, False)):
38 exahype2_project = types.SimpleNamespace(periodic_BC=list(periodic_BC))
39 peano_project = peano4.api.Project(["probe"], "probe", str(directory))
40 time_step = peano4.api.taskgraph.Graph()
41 solver.numerical_scheme.construct_output(exahype2_project, peano_project, time_step)
42 return {task.name: task for task in time_step.tasks}
43
44
46 tasks = _construct_time_step(_solver(rk_order=3), tmp_path)
47 assert set(tasks) == {
48 "Probe_Apply Dirichlet BCs stage 0",
49 "Probe_Apply Dirichlet BCs stage 1",
50 "Probe_Apply Dirichlet BCs stage 2",
51 "Probe_Reconstruct stage 0",
52 "Probe_Reconstruct stage 1",
53 "Probe_Reconstruct stage 2",
54 "Probe_Disable load balancing",
55 "Probe_Evaluate Runge-Kutta right hand side stage 0",
56 "Probe_Evaluate Runge-Kutta right hand side stage 1",
57 "Probe_Evaluate Runge-Kutta right hand side stage 2",
58 "Probe_Prepare stage 1",
59 "Probe_Prepare stage 2",
60 "Probe_Complete time step",
61 "Probe_Project stage 1 onto faces",
62 "Probe_Project stage 2 onto faces",
63 "Probe_Project solution onto faces",
64 "Probe_Roll over stage 1 faces",
65 "Probe_Roll over stage 2 faces",
66 "Probe_Roll over solution faces",
67 "Probe_Compute max eigenvalue",
68 "Probe_Reduce max eigenvalue",
69 "Probe_Compute time step size",
70 f"Probe_{exahype2.api.Solver.NumericalScheme.DYNAMIC_MESH_REFINEMENT_TASK_NAME}",
71 }
72 # The maximum eigenvalue of the new solution is determined right after the
73 # projection, i.e. in parallel to the roll over. If it ran in parallel to the
74 # projection, its producer-consumer split would push the roll over into the
75 # next mesh sweep, away from the temporary face data of the projection
76 # (adrs/0002-eigenvalue-follows-projection.md)
77 assert (
78 tasks["Probe_Complete time step"]
79 in tasks["Probe_Project solution onto faces"].dependencies
80 )
81 assert (
82 tasks["Probe_Project solution onto faces"]
83 in tasks["Probe_Compute max eigenvalue"].dependencies
84 )
85 assert (
86 tasks["Probe_Compute max eigenvalue"]
87 not in tasks["Probe_Roll over solution faces"].dependencies
88 )
89 # The eigenvalue is stored per cell, a task of its own reduces it, and the
90 # time step size is computed from the outcome
91 assert (
92 tasks["Probe_Compute max eigenvalue"]
93 in tasks["Probe_Reduce max eigenvalue"].dependencies
94 )
95 assert (
96 tasks["Probe_Reduce max eigenvalue"]
97 in tasks["Probe_Compute time step size"].dependencies
98 )
99 for stage in [1, 2]:
100 assert (
101 tasks[f"Probe_Prepare stage {stage}"]
102 in tasks[f"Probe_Project stage {stage} onto faces"].dependencies
103 )
104 # The projection hosts the restriction, the roll over follows
105 assert (
106 tasks[f"Probe_Project stage {stage} onto faces"]
107 in tasks[f"Probe_Roll over stage {stage} faces"].dependencies
108 )
109 # The reconstruction interpolates the faces itself, i.e. it waits for the roll over
110 assert (
111 tasks[f"Probe_Roll over stage {stage} faces"]
112 in tasks[f"Probe_Reconstruct stage {stage}"].dependencies
113 )
114 # The boundary values have to be set after the previous stage's roll-over
115 assert (
116 tasks["Probe_Roll over stage 1 faces"]
117 in tasks["Probe_Apply Dirichlet BCs stage 1"].dependencies
118 )
119 assert (
120 tasks["Probe_Apply Dirichlet BCs stage 1"]
121 in tasks["Probe_Reconstruct stage 1"].dependencies
122 )
123 assert (
124 tasks["Probe_Roll over solution faces"]
125 in tasks["Probe_Compute time step size"].dependencies
126 )
127 # The load balancing is switched off before the first stage only
128 assert (
129 tasks["Probe_Disable load balancing"]
130 in tasks["Probe_Apply Dirichlet BCs stage 0"].dependencies
131 )
132 # The reconstruction holds the interpolation and the reconstruction
133 assert [
134 type(a).__name__ for a in tasks["Probe_Reconstruct stage 1"].action_sets
135 ] == [
136 "InterpolateHangingFaces",
137 "ReconstructPatchWithHalo",
138 ]
139 assert [
140 type(a).__name__ for a in tasks["Probe_Project stage 1 onto faces"].action_sets
141 ] == ["ProjectPatchOntoFaces", "RestrictHangingFaces"]
142 # All other tasks of the scheme hold exactly one action set, so that the task
143 # graph compiler can run the tasks of several solvers in parallel
144 for name in [
145 "Probe_Evaluate Runge-Kutta right hand side stage 1",
146 "Probe_Roll over stage 1 faces",
147 ]:
148 assert len(tasks[name].action_sets) == 1, name
149
150
151def _tagged(tasks, tag):
152 return {name for name, task in tasks.items() if tag in task.tags}
153
154
156 solver = _solver(rk_order=3)
157 tasks = _construct_time_step(solver, tmp_path)
158 final = _tagged(tasks, exahype2.api.Solver.SolutionUpdateTaskTag)
159 intermediate = _tagged(tasks, exahype2.api.Solver.IntermediateSolutionUpdateTag)
160 assert set(final) == {"Probe_Complete time step"}
161 assert set(intermediate) == {"Probe_Prepare stage 1", "Probe_Prepare stage 2"}
162 tagged = final | intermediate
163 # The combination must not project onto the faces itself: the projection
164 # has to see the postprocessed solution
165 for name in tagged:
166 assert not any(
168 for action_set in tasks[name].action_sets
169 )
170
171
173 tasks = _construct_time_step(_solver(rk_order=3), tmp_path)
174 [action_set] = tasks["Probe_Apply Dirichlet BCs stage 2"].action_sets
175 body = action_set.get_body_of_operation(
176 peano4.api.solversteps.ActionSet.OPERATION_TOUCH_FACE_FIRST_TIME
177 )
178 # c_2 = 1 for Kutta's third-order scheme
179 assert (
180 "repositories::instanceOfProbe.getMinTimeStamp() + 1.0 * "
181 "repositories::instanceOfProbe.getMinTimeStepSize()," in body
182 )
183
184
186 tasks = _construct_time_step(
187 _solver(rk_order=2, boundary=None), tmp_path, periodic_BC=(True, True, True)
188 )
189 assert not any("Dirichlet" in name for name in tasks)
190 assert not any(
191 isinstance(
192 action_set,
194 )
195 for task in tasks.values()
196 for action_set in task.action_sets
197 )
198
199
201 with pytest.raises(ValueError, match="all axes are periodic"):
202 _construct_time_step(_solver(), tmp_path, periodic_BC=(True, True, True))
203
204
205def _roll_overs(tasks):
206 return {
207 name: action_set
208 for name, task in tasks.items()
209 for action_set in task.action_sets
211 }
212
213
215 tasks = _construct_time_step(_solver(rk_order=3, boundary="sommerfeld"), tmp_path)
216 assert not any("Dirichlet" in name for name in tasks)
217 roll_overs = _roll_overs(tasks)
218 assert set(roll_overs) == {
219 "Probe_Roll over stage 1 faces",
220 "Probe_Roll over stage 2 faces",
221 "Probe_Roll over solution faces",
222 }
223 # Kutta's third-order scheme has c = 0, 1/2, 1, i.e. the roll overs
224 # advance the ghost grid cells by 1/2, 1/2 and 0 times the time step size
225 for name, increment in [
226 ("Probe_Roll over stage 1 faces", "0.5"),
227 ("Probe_Roll over stage 2 faces", "0.5"),
228 ("Probe_Roll over solution faces", "0.0"),
229 ]:
230 action_set = roll_overs[name]
231 assert isinstance(
232 action_set,
234 )
235 body = action_set.get_body_of_operation(
236 peano4.api.solversteps.ActionSet.OPERATION_TOUCH_FACE_LAST_TIME
237 )
238 assert "::exahype2::blockstructured::applySommerfeldBoundaryConditions(" in body
239 assert (
240 f"{increment} * repositories::instanceOfProbe.getMinTimeStepSize()," in body
241 )
242 assert "tarch::la::Vector<Dimensions, double>({0.0, 0.0, 0.0})" in body
243 # The Sommerfeld update has to precede the copy into the persistent data
244 assert body.index("applySommerfeldBoundaryConditions") < body.index(
245 "copyHalfOfHalo"
246 )
247
248
250 solver = _solver(rk_order=3, boundary="sommerfeld")
251 # The third-order scheme of Shu and Osher has c = 0, 1, 1/2
252 monkeypatch.setattr(
253 solver.numerical_scheme.butcher_tableau,
254 "relative_time_step_sizes",
255 lambda: [0.0, 1.0, 0.5],
256 )
257 with pytest.raises(ValueError, match="decrease"):
258 _construct_time_step(solver, tmp_path)
259
260
262 solver = _solver(rk_order=3)
263 monkeypatch.setattr(
264 solver.numerical_scheme.butcher_tableau,
265 "relative_time_step_sizes",
266 lambda: [0.0, 1.0, 0.5],
267 )
268 tasks = _construct_time_step(solver, tmp_path)
269 assert "Probe_Apply Dirichlet BCs stage 2" in tasks
270
271
273 solver = _solver(rk_order=4, boundary=None)
274 tasks = _construct_time_step(solver, tmp_path, periodic_BC=(True, True, True))
275 tableau = solver.numerical_scheme.butcher_tableau
276
277 def combination(task_name):
278 [action_set] = [
279 action_set
280 for action_set in tasks[task_name].action_sets
281 if isinstance(
283 )
284 ]
285 return action_set
286
287 for stage in range(1, 4):
288 action_set = combination(f"Probe_Prepare stage {stage}")
289 assert action_set.weights == tableau.weight_matrix()[stage]
290 assert action_set.output is solver._stage_input
291 final = combination("Probe_Complete time step")
292 assert final.weights == tableau.final_estimate_weights()
293 assert final.output is solver._patch
294
295
297 _construct_time_step(_solver(), tmp_path)
298 kernels = tmp_path / "kernels"
299 for name in [
300 "Probe_fd4_2d",
301 "Probe_fd4_3d",
302 "Probe_fd4_eigenvalue_2d",
303 "Probe_fd4_eigenvalue_3d",
304 "rungekutta_linear_combination",
305 ]:
306 assert (kernels / (name + ".h")).exists(), name
307 assert (kernels / (name + ".cpph")).exists(), name
308 wrapper = (kernels / "Probe_fd4.h").read_text()
309 assert '#include "Probe_fd4_2d.h"' in wrapper
310 assert '#include "Probe_fd4_eigenvalue_3d.h"' in wrapper
311 assert "Probe_rightHandSideWithFD4" in (kernels / "Probe_fd4_2d.cpph").read_text()
312
313
315 solver = _solver(time_step_size=0.01, boundary=None)
316 tasks = _construct_time_step(solver, tmp_path, periodic_BC=(True, True, True))
317 assert not (tmp_path / "kernels" / "Probe_fd4_eigenvalue_2d.h").exists()
318 assert not any(
320 for task in tasks.values()
321 for action_set in task.action_sets
322 )
323 [time_step_size] = tasks["Probe_Compute time step size"].action_sets
324 assert (
325 "const double admissibleTimeStepSize = 0.01;"
326 in time_step_size.get_body_of_unprepareTraversal()
327 )
328
329
331 solver = _solver(rk_order=4, boundary=None)
332 tasks = _construct_time_step(solver, tmp_path, periodic_BC=(True, True, True))
333 [action_set] = tasks[
334 "Probe_Evaluate Runge-Kutta right hand side stage 2"
335 ].action_sets
336 body = action_set.get_body_of_operation(
337 peano4.api.solversteps.ActionSet.OPERATION_TOUCH_CELL_FIRST_TIME
338 )
339 # c_2 = 2/3 for Kutta's 3/8 rule, and each stage's estimate holds 2*4^d values
340 assert "getMinTimeStamp() + 0.6666666666666666 * timeStepSize" in body
341 assert "fineGridCellProbeQRhsEstimates.value + 64;" in body
342 assert "fineGridCellProbeQRhsEstimates.value + 256;" in body
343 assert (
344 "Probe_rightHandSideWithFD4<\n 4,\n 2,\n 2,\n 0\n>(\n patchData,\n [&]("
345 in body
346 )
347
348
350 solver = exahype2.api.Solver("Probe", 1)
351 solver.add_pde_term(exahype2.api.Flux(implementation="F[0] = Q[0];"))
353 solver, patch_size=4, rk_order=2
354 )
355 with pytest.raises(AssertionError):
356 _construct_time_step(solver, tmp_path, periodic_BC=(True, True, True))
357
358
360 """!
361 Peano's stacks require that each stored datum is loaded by the next
362 sweep which uses it. The reconstruction of a later stage is the last
363 reader of the stage input, as the next combination writes it from
364 scratch, so it must not store it.
365 """
366 solver = _solver(rk_order=4, boundary=None)
367 tasks = _construct_time_step(solver, tmp_path, periodic_BC=(True, True, True))
368 Access = peano4.api.taskgraph.Task.DataAccess
369 assert (
370 tasks["Probe_Reconstruct stage 0"].cell_data_access[solver._patch]
371 == Access.READ_AND_WRITE
372 )
373 for stage in range(1, 4):
374 reconstruction = tasks[f"Probe_Reconstruct stage {stage}"]
375 assert (
376 reconstruction.cell_data_access[solver._stage_input]
377 == Access.READ_AND_DISCARD
378 )
379 assert (
380 tasks[f"Probe_Prepare stage {stage}"].cell_data_access[solver._stage_input]
381 == Access.WRITE
382 )
383
384
385@pytest.mark.parametrize("time_step_size", [None, 0.01])
387 tmp_path, time_step_size
388):
389 solver = _solver(rk_order=2, boundary=None, time_step_size=time_step_size)
390 tasks = _construct_time_step(solver, tmp_path, periodic_BC=(True, True, True))
391 task = tasks["Probe_Compute time step size"]
392 assert task.entity == peano4.api.taskgraph.Task.GridEntity.GLOBAL_END_OF_STEP
393 assert not task.cell_data
394 # Nothing, not even the refinement, waits for a task that runs at the very end
395 assert not any(task in other.dependencies for other in tasks.values())
396 # The mesh refinement is independent of the time step size
397 assert (
398 task
399 not in tasks[
400 f"Probe_{exahype2.api.Solver.NumericalScheme.DYNAMIC_MESH_REFINEMENT_TASK_NAME}"
401 ].dependencies
402 )
403
404
406 tasks = _construct_time_step(_solver(rk_order=3), tmp_path)
407 Access = peano4.api.taskgraph.Task.DataAccess
408
409 def access_to_estimates(name):
410 (qualifier,) = [
411 access
412 for data, access in tasks[name].cell_data_access.items()
413 if data.name.endswith("RhsEstimates")
414 ]
415 return qualifier
416
417 # Write-only first would leak one patch per cell and time step
418 assert (
419 access_to_estimates("Probe_Evaluate Runge-Kutta right hand side stage 0")
420 == Access.READ_AND_WRITE
421 )
422 assert (
423 access_to_estimates("Probe_Evaluate Runge-Kutta right hand side stage 1")
424 == Access.READ_AND_WRITE
425 )
426 assert access_to_estimates("Probe_Prepare stage 1") == Access.READ_AND_WRITE
427 assert access_to_estimates("Probe_Prepare stage 2") == Access.READ_AND_WRITE
428 assert access_to_estimates("Probe_Complete time step") == Access.READ_AND_DISCARD
The conservative flux F(Q) of a hyperbolic PDE.
Definition Flux.py:8
The maximum absolute eigenvalue of the flux Jacobian in a given direction, i.e.
Abstract base class for all ExaHyPE 2 solvers.
Definition Solver.py:17
Fourth-order central Finite Differences with explicit Runge-Kutta time stepping, realised over a bloc...
Combine the solution with the right-hand side estimates of the Runge-Kutta stages.
Store the maximum eigenvalue over all grid cells of a patch.
Project a patch's data onto the overlap held on its surrounding faces, so the halo layers can be reco...
Copy patch_overlap_update - the face overlap exahype2.api.rkfd4.actionsets.ProjectPatchOntoFaces has ...
Represents a Peano 4 project.
Definition Project.py:26
Holds a sequence of Task instances.
Definition Graph.py:9
The Python-side model of "what happens during one mesh sweep".
Definition __init__.py:1
The task graph abstraction allows users to formalise their solver steps over small task graphs which ...
Definition __init__.py:1
test_dirichlet_accepts_decreasing_stage_time_stamps(tmp_path, monkeypatch)
test_stage_combinations_use_rows_of_butcher_tableau(tmp_path)
test_one_reconstruction_per_stage_and_chain_of_stages(tmp_path)
test_periodic_domain_needs_no_boundary_tasks(tmp_path)
test_fixed_time_step_size_skips_eigenvalue(tmp_path)
test_boundary_values_use_stage_time_stamp(tmp_path)
test_right_hand_side_uses_stage_time_stamp_and_estimate_slot(tmp_path)
test_estimates_are_discarded_last_but_not_written_first(tmp_path)
test_stage_input_is_loaded_once_after_each_store(tmp_path)
Peano's stacks require that each stored datum is loaded by the next sweep which uses it.
test_sommerfeld_replaces_roll_over_and_omits_boundary_tasks(tmp_path)
test_stage_combinations_are_tagged_as_solution_updates(tmp_path)
test_adaptive_time_stepping_requires_max_eigenvalue(tmp_path)
test_periodic_domain_rejects_boundary_condition_term(tmp_path)
_solver(time_step_size=None, rk_order=3, boundary="dirichlet")
test_kernels_are_generated_for_both_dimensions(tmp_path)
test_sommerfeld_rejects_decreasing_stage_time_stamps(tmp_path, monkeypatch)
test_time_step_size_acts_at_the_end_of_the_time_step_and_needs_no_traversal(tmp_path, time_step_size)
_construct_time_step(solver, directory, periodic_BC=(False, False, False))