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.CCZ4Solver("Probe")
107 solver.numerical_scheme = scheme_factory(solver)
108 solver.grid_construction = grid_construction_factory(solver)
109 solver.enforce_ccz4_constraint(enforce)
110
111 exahype2_project = exahype2.api.Project(
112 ["probe"], "probe", "probe", directory=str(tmp_path)
113 )
114 exahype2_project.set_global_simulation_parameters(
115 dimensions=3,
116 offset=[0.0, 0.0, 0.0],
117 size=[1.0, 1.0, 1.0],
118 min_end_time=1.0,
119 periodic_BC=[True, True, True],
120 )
121 peano_project = peano4.api.Project(["probe"], "probe", str(tmp_path))
122 graphs = [peano4.api.taskgraph.Graph() for _ in range(4)]
123 solver.generate_solver_realisation(exahype2_project, peano_project, *graphs)
124 return solver, {task.name: task for task in graphs[1].tasks}
125
126
127def _enforcements(tasks):
128 return sorted(name for name in tasks if "_Enforce CCZ4 constraints after" in name)
129
130
131def _runge_kutta(solver):
133 solver, patch_size=4, rk_order=3
134 )
135
136
138 return exahype2.api.rkfd4.SetupSimulationOverRegularGrid(solver, volume_size=0.1)
139
140
141def _fv_grid(solver):
142 return exahype2.api.fv.SetupSimulationOverRegularGrid(solver, volume_size=0.1)
143
144
146 import exagrype.api
147
148 solver, tasks = _postprocessed_time_step(
149 _runge_kutta,
150 _runge_kutta_grid,
151 exagrype.api.CCZ4Solver.EnforceCCZ4Constraint.No,
152 tmp_path,
153 )
154 assert _enforcements(tasks) == []
155
156
158 import exagrype.api
159
160 solver, tasks = _postprocessed_time_step(
161 _runge_kutta,
162 _runge_kutta_grid,
163 exagrype.api.CCZ4Solver.EnforceCCZ4Constraint.IntermediateSteps,
164 tmp_path,
165 )
166 assert len(_enforcements(tasks)) == 3
167 for anchor, successor, patch in [
168 ("Prepare stage 1", "Project stage 1 onto faces", solver._stage_input),
169 ("Prepare stage 2", "Project stage 2 onto faces", solver._stage_input),
170 ("Complete time step", "Project solution onto faces", solver._patch),
171 ]:
172 clip = tasks[f"Probe_Enforce CCZ4 constraints after {anchor}"]
173 assert clip.dependencies == [tasks[f"Probe_{anchor}"]]
174 assert tasks[f"Probe_{successor}"].dependencies == [clip]
175 [action_set] = clip.action_sets
176 assert isinstance(
177 action_set, toolbox.blockstructured.api.ApplyPointWiselyToPatch
178 )
179 body = action_set.get_body_of_operation(
180 peano4.api.solversteps.ActionSet.OPERATION_TOUCH_CELL_FIRST_TIME
181 )
182 assert "::exagrype::ccz4::enforceCCZ4constraints(Q);" in body
183 assert f"fineGridCell{patch.name}.value" in body
184
185
187 import exagrype.api
188
189 solver, tasks = _postprocessed_time_step(
190 _runge_kutta,
191 _runge_kutta_grid,
192 exagrype.api.CCZ4Solver.EnforceCCZ4Constraint.OnlyFinalSolution,
193 tmp_path,
194 )
195 assert _enforcements(tasks) == [
196 "Probe_Enforce CCZ4 constraints after Complete time step"
197 ]
198
199
201 tmp_path,
202):
203 import exagrype.api
204
205 solver, tasks = _postprocessed_time_step(
207 _fv_grid,
208 exagrype.api.CCZ4Solver.EnforceCCZ4Constraint.OnlyFinalSolution,
209 tmp_path,
210 )
211 [name] = _enforcements(tasks)
212 clip = tasks[name]
213 # One task after both branches, guarded by the plain cell guard
214 assert set(clip.dependencies) == {
215 tasks["Probe_Update cell"],
216 tasks["Probe_Insert patch interior"],
217 }
218 [action_set] = clip.action_sets
219 assert action_set.guard == solver.grid_selector.createCellGuard()
220 for consumer in [
221 "Project on faces",
222 "Compute global CFL condition",
223 "Spawn patch interior update",
224 "Discard patch interior",
225 ]:
226 dependencies = tasks[f"Probe_{consumer}"].dependencies
227 assert clip in dependencies
228 assert tasks["Probe_Update cell"] not in dependencies
229 assert tasks["Probe_Insert patch interior"] not in dependencies
230
231
233 import exagrype.api
234
235 solver, tasks = _postprocessed_time_step(
236 lambda solver: exahype2.api.fv.RusanovWithExplicitEuler(solver, 4),
237 _fv_grid,
238 exagrype.api.CCZ4Solver.EnforceCCZ4Constraint.IntermediateSteps,
239 tmp_path,
240 )
241 clip = tasks["Probe_Enforce CCZ4 constraints after Update cell"]
242 assert _enforcements(tasks) == [clip.name]
243 assert tasks["Probe_Project on faces"].dependencies == [clip]
244
245
247 import exagrype.api
248
249 class UnknownScheme(exahype2.api.Solver.NumericalScheme):
250 def construct_output(self, exahype2_project, peano_project, time_step):
251 pass
252
253 class NoGridConstruction(exahype2.api.Solver.GridConstruction):
254 def construct_output(self, exahype2_project, peano_project, setup_simulation):
255 pass
256
257 with pytest.raises(TypeError, match="UnknownScheme"):
259 UnknownScheme,
260 NoGridConstruction,
261 exagrype.api.CCZ4Solver.EnforceCCZ4Constraint.OnlyFinalSolution,
262 tmp_path,
263 )
Solver for the Einstein equations in the first-order CCZ4 formulation.
Definition CCZ4Solver.py:16
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:23
Holds a sequence of Task instances.
Definition Graph.py:9
One task within a peano4.api.taskgraph.Graph.
Definition Task.py:8
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.