Peano
Loading...
Searching...
No Matches
test_task_graph_postprocessing.py
Go to the documentation of this file.
1import types
2
3import pytest
4
5import peano4.api
8
9import exahype2.api
11
13
14
15def _cell_task(name):
16 return Task(name=name, entity=Task.GridEntity.CELL)
17
18
21 a, b, c, d = (_cell_task(name) for name in "abcd")
22 b.add_dependency(a)
23 c.add_dependency(a)
24 c.add_dependency(b)
25 for task in [a, b, c]:
26 graph.add_task(task)
27
28 graph.insert_after(a, d)
29
30 assert d in graph.tasks
31 assert d.dependencies == [a]
32 assert b.dependencies == [d]
33 assert c.dependencies == [d, b]
34 order = graph.ordered_tasks()
35 assert order.index(a) < order.index(d) < order.index(b) < order.index(c)
36
37
40 a, b = _cell_task("a"), _cell_task("b")
41 graph.add_task(a)
42 graph.insert_after(a, b)
43 assert graph.ordered_tasks() == [a, b]
44
45
48 a, b, c, d, e = (_cell_task(name) for name in "abcde")
49 b.add_dependency(a)
50 c.add_dependency(a)
51 d.add_dependency(b)
52 d.add_dependency(c)
53 for task in [a, b, c, d]:
54 graph.add_task(task)
55
56 graph.insert_after([b, c], e)
57
58 assert e.dependencies == [b, c]
59 assert d.dependencies == [e]
60 order = graph.ordered_tasks()
61 assert order.index(b) < order.index(e) < order.index(d)
62 assert order.index(c) < order.index(e)
63
64
67 a, b, c, d = (_cell_task(name) for name in "abcd")
68 b.add_dependency(a)
69 c.add_dependency(b)
70 for task in [a, b, c]:
71 graph.add_task(task)
72 with pytest.raises(AssertionError):
73 graph.insert_after([a, c], d)
74
75
78 a = Task(name="a", entity=Task.GridEntity.CELL, tags={"x"})
79 b = _cell_task("b")
80 c = _cell_task("c")
81 c.tags.add("x")
82 for task in [a, b, c]:
83 graph.add_task(task)
84 assert graph.find_tasks("x") == [a, c]
85 assert graph.find_tasks("y") == []
86
87
90 a = Task(name="a", entity=Task.GridEntity.CELL, tags=["x", "y"])
91 b = Task(name="b", entity=Task.GridEntity.CELL, tags={"y"})
92 c = _cell_task("c")
93 for task in [a, b, c]:
94 graph.add_task(task)
95 assert graph.find_tasks("x") == [a]
96 assert graph.find_tasks("y") == [a, b]
97 assert graph.find_tasks("x", "y") == [a, b]
98
99
101 scheme_factory, grid_construction_factory, enforce, tmp_path
102):
103 """! Time step graph of a CCZ4 solver with the given constraint enforcement"""
104 import exagrype.api
105
106 solver = exagrype.api.FOCCZ4Solver("Probe")
107 solver.add_pde_term(exagrype.api.initialvalues.gauge_wave())
108 solver.numerical_scheme = scheme_factory(solver)
109 solver.grid_construction = grid_construction_factory(solver)
110 solver.enforce_ccz4_constraint(enforce)
111
112 exahype2_project = exahype2.api.Project(
113 ["probe"], "probe", "probe", directory=str(tmp_path)
114 )
115 exahype2_project.set_global_simulation_parameters(
116 dimensions=3,
117 offset=[0.0, 0.0, 0.0],
118 size=[1.0, 1.0, 1.0],
119 min_end_time=1.0,
120 periodic_BC=[True, True, True],
121 )
122 peano_project = peano4.api.Project(["probe"], "probe", str(tmp_path))
123 graphs = [peano4.api.taskgraph.Graph() for _ in range(4)]
124 solver.generate_solver_realisation(exahype2_project, peano_project, *graphs)
125 return solver, {task.name: task for task in graphs[1].tasks}
126
127
128def _enforcements(tasks):
129 return sorted(name for name in tasks if "_Enforce CCZ4 constraints after" in name)
130
131
132def _runge_kutta(solver):
134 solver, patch_size=4, rk_order=3
135 )
136
137
139 return exahype2.api.rkfd4.SetupSimulationOverRegularGrid(solver, volume_size=0.1)
140
141
142def _fv_grid(solver):
143 return exahype2.api.fv.SetupSimulationOverRegularGrid(solver, volume_size=0.1)
144
145
147 import exagrype.api
148
149 solver, tasks = _postprocessed_time_step(
150 _runge_kutta,
151 _runge_kutta_grid,
152 exagrype.api.FOCCZ4Solver.EnforceCCZ4Constraint.No,
153 tmp_path,
154 )
155 assert _enforcements(tasks) == []
156
157
159 import exagrype.api
160
161 solver, tasks = _postprocessed_time_step(
162 _runge_kutta,
163 _runge_kutta_grid,
164 exagrype.api.FOCCZ4Solver.EnforceCCZ4Constraint.IntermediateSteps,
165 tmp_path,
166 )
167 assert len(_enforcements(tasks)) == 3
168 for anchor, successor, patch in [
169 ("Prepare stage 1", "Project stage 1 onto faces", solver._stage_input),
170 ("Prepare stage 2", "Project stage 2 onto faces", solver._stage_input),
171 ("Complete time step", "Project solution onto faces", solver._patch),
172 ]:
173 clip = tasks[f"Probe_Enforce CCZ4 constraints after {anchor}"]
174 assert clip.dependencies == [tasks[f"Probe_{anchor}"]]
175 assert tasks[f"Probe_{successor}"].dependencies == [clip]
176 [action_set] = clip.action_sets
177 assert isinstance(
178 action_set, toolbox.blockstructured.api.ApplyPointWiselyToPatch
179 )
180 body = action_set.get_body_of_operation(
181 peano4.api.solversteps.ActionSet.OPERATION_TOUCH_CELL_FIRST_TIME
182 )
183 assert "::exagrype::ccz4::enforceCCZ4constraints(Q);" in body
184 assert f"fineGridCell{patch.name}.value" in body
185 # With adaptive time stepping, the maximum eigenvalue of the constrained
186 # solution is determined after the projection
187 assert (
188 tasks["Probe_Project solution onto faces"]
189 in tasks["Probe_Compute max eigenvalue"].dependencies
190 )
191
192
194 import exagrype.api
195
196 solver, tasks = _postprocessed_time_step(
197 _runge_kutta,
198 _runge_kutta_grid,
199 exagrype.api.FOCCZ4Solver.EnforceCCZ4Constraint.OnlyFinalSolution,
200 tmp_path,
201 )
202 assert _enforcements(tasks) == [
203 "Probe_Enforce CCZ4 constraints after Complete time step"
204 ]
205
206
208 tmp_path,
209):
210 import exagrype.api
211
212 solver, tasks = _postprocessed_time_step(
214 _fv_grid,
215 exagrype.api.FOCCZ4Solver.EnforceCCZ4Constraint.OnlyFinalSolution,
216 tmp_path,
217 )
218 [name] = _enforcements(tasks)
219 clip = tasks[name]
220 # One task after both branches, guarded by the plain cell guard
221 assert set(clip.dependencies) == {
222 tasks["Probe_Update cell"],
223 tasks["Probe_Insert patch interior"],
224 }
225 [action_set] = clip.action_sets
226 assert action_set.guard == solver.grid_selector.createCellGuard()
227 for consumer in [
228 "Compute max eigenvalue",
229 "Spawn patch interior update",
230 "Discard patch interior",
231 ]:
232 dependencies = tasks[f"Probe_{consumer}"].dependencies
233 assert clip in dependencies
234 assert tasks["Probe_Update cell"] not in dependencies
235 assert tasks["Probe_Insert patch interior"] not in dependencies
236
237
239 import exagrype.api
240
241 solver, tasks = _postprocessed_time_step(
242 lambda solver: exahype2.api.fv.RusanovWithExplicitEuler(solver, 4),
243 _fv_grid,
244 exagrype.api.FOCCZ4Solver.EnforceCCZ4Constraint.IntermediateSteps,
245 tmp_path,
246 )
247 clip = tasks["Probe_Enforce CCZ4 constraints after Update cell"]
248 assert _enforcements(tasks) == [clip.name]
249 assert tasks["Probe_Compute max eigenvalue"].dependencies == [clip]
250
251
253 import exagrype.api
254
255 class UnknownScheme(exahype2.api.Solver.NumericalScheme):
256
257 patch_size = 4
258 halo_size = 1
259
260 def construct_output(
261 self, exahype2_project, peano_project, time_step, add_global_tasks=True
262 ):
263 pass
264
265 class NoGridConstruction(exahype2.api.Solver.GridConstruction):
266 def construct_output(self, exahype2_project, peano_project, setup_simulation):
267 pass
268
269 with pytest.raises(TypeError, match="UnknownScheme"):
271 UnknownScheme,
272 NoGridConstruction,
273 exagrype.api.FOCCZ4Solver.EnforceCCZ4Constraint.OnlyFinalSolution,
274 tmp_path,
275 )
276
277
280 a, b, c, d = (_cell_task(name) for name in "abcd")
281 b.add_dependency(a)
282 c.add_dependency(a)
283 d.add_dependency(b)
284 for task in [a, b, c, d]:
285 graph.add_task(task)
286
287 assert graph.sink_tasks() == [c, d]
288
289
292 assert graph.sink_tasks() == []
293 a, b = _cell_task("a"), _cell_task("b")
294 b.add_dependency(a)
295 graph.add_task(a)
296 graph.add_task(b)
297 assert graph.sink_tasks() == [b]
Solver for the Einstein equations in the first-order CCZ4 formulation.
ExaHyPE 2 project.
Definition Project.py:15
Finite Volumes with a Rusanov Riemann solver and explicit Euler time stepping, realised over a block-...
Finite Volumes with a Rusanov Riemann solver and optimistic explicit Euler time stepping,...
Setup phase counterpart to RusanovWithExplicitEuler: sets the initial condition for a Finite Volume s...
Fourth-order central Finite Differences with explicit Runge-Kutta time stepping, realised over a bloc...
Setup phase of the fourth-order Finite Differences with Runge-Kutta time stepping (exahype2....
Represents a Peano 4 project.
Definition Project.py:26
Holds a sequence of Task instances.
Definition Graph.py:9
One task within a peano4.api.taskgraph.Graph.
Definition Task.py:8
exahype2.api.InitPointWisely gauge_wave()
Gauge wave along the x-axis,.
This file is part of the ExaGRyPE project.
Definition __init__.py:1
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_constraint_enforcement_after_both_writers_of_optimistic_finite_volumes(tmp_path)
_postprocessed_time_step(scheme_factory, grid_construction_factory, enforce, tmp_path)
Time step graph of a CCZ4 solver with the given constraint enforcement.