Peano
Loading...
Searching...
No Matches
test_exagrype_fosoccz4_solver.py
Go to the documentation of this file.
1import pytest
2
3import peano4.api
5import exahype2.api
6import exagrype.api
7
8
9def _project(tmp_path):
10 exahype2_project = exahype2.api.Project(
11 ["probe"], "probe", "probe", directory=str(tmp_path)
12 )
13 exahype2_project.set_global_simulation_parameters(
14 dimensions=3,
15 offset=[0.0, 0.0, 0.0],
16 size=[1.0, 1.0, 1.0],
17 min_end_time=1.0,
18 periodic_BC=[True, True, True],
19 )
20 return exahype2_project, peano4.api.Project(["probe"], "probe", str(tmp_path))
21
22
23def _realise_pair(pair, tmp_path):
24 """! Let the host realise the pair and return the time step graph of the project"""
25 exahype2_project, peano_project = _project(tmp_path)
26 graphs = [peano4.api.taskgraph.Graph() for _ in range(4)]
27 pair.generate_solver_realisation(exahype2_project, peano_project, *graphs)
28 return graphs[1]
29
30
31def _configure(pair, enforce=None):
32 """! Configure the CCZ4 solver of the pair with FD4 and lopsided advection"""
33 solver = pair.ccz4
34 Kernel = exahype2.api.rkfd4.CentralDifferencesWithRungeKutta.RightHandSideKernel
35 # Lopsided advection widens the halo to the three that the reconstruction needs
37 solver,
38 9,
39 rk_order=4,
40 right_hand_side_kernel=Kernel.CentralDifferencesWithLopsidedAdvection,
41 advection_velocity=exagrype.api.FOSOCCZ4Solver.ADVECTION_VELOCITY,
42 advected_quantities=solver.advected_quantities(),
43 )
45 solver, volume_size=1.0 / 27.0
46 )
47 if enforce is not None:
48 solver.enforce_ccz4_constraint(enforce)
49
50
51def _realise(pair, tmp_path, enforce=None):
52 """! Configure the pair with FD4, realise both solvers and return the time step graph"""
53 _configure(pair, enforce)
54 return _realise_pair(pair, tmp_path)
55
56
58 first_order = exagrype.api.FOCCZ4Solver("FO")
59 second_order = exagrype.api.FOSOCCZ4Solver("SO")
60 assert first_order.unknowns == 58
61 assert second_order.unknowns == 25
62 assert second_order.number_of_auxiliary_variables == 33
63 assert second_order.derivatives.size == 33
64 assert second_order.auxiliary_variables == [second_order.derivatives]
65 assert second_order.derivatives.owner is second_order
66 # Unknowns plus derivatives are the unknowns of the first-order solver, in order
67 assert (
68 second_order.variable_names_per_entry()
69 + second_order.derivatives.variable_names_per_entry()
70 != []
71 )
72 assert list(exagrype.api.FOSOCCZ4Solver.Unknowns) + list(
73 exagrype.api.FOSOCCZ4Solver.DerivativeUnknowns
74 ) == list(exagrype.api.FOCCZ4Solver.Unknowns)
75 assert (
76 exagrype.api.FOSOCCZ4Solver.Unknowns
77 | exagrype.api.FOSOCCZ4Solver.DerivativeUnknowns
78 == exagrype.api.FOCCZ4Solver.Unknowns
79 )
80
81
83 assert "CCZ4SO" not in exagrype.api.FOSOCCZ4Solver("SO").parameters
84 assert "CCZ4SO" not in exagrype.api.FOCCZ4Solver("FO").parameters
85 with pytest.raises(KeyError):
86 exagrype.api.FOSOCCZ4Solver("SO", parameters={"CCZ4SO": 0})
87
88
89def _ncp_call(solver):
90 """!
91 C++ which calls the kernel of the non-conservative product. The solver
92 registers its non-conservative product only when it is realised, so it
93 is taken from the hook which writes it.
94 """
95 return solver._ncp_implementation()
96
97
99 first_order = exagrype.api.FOCCZ4Solver("FO")
100 second_order = exagrype.api.FOSOCCZ4Solver("SO")
101 assert "::exagrype::ccz4::ncpFirstOrder(" in _ncp_call(first_order)
102 assert "::exagrype::ccz4::ncpSecondOrder(" in _ncp_call(second_order)
103 for solver, order in [(first_order, "FirstOrder"), (second_order, "SecondOrder")]:
104 assert (
105 f"::exagrype::ccz4::source{order}("
106 in solver.get_pde_term("volumetricSource", stateless=True).implementation
107 )
108 for solver in (first_order, second_order):
109 assert (
110 "::exagrype::ccz4::maxEigenvalue("
111 in solver.get_pde_term("maxEigenvalue", stateless=True).implementation
112 )
113 # The kernels get the primary entries and, as a second pointer, the
114 # derivatives, i.e. the auxiliary variables, without any copy
115 ncp = _ncp_call(second_order)
116 assert "Q, derivatives," in ncp
117 assert "deltaQ, deltaDerivatives," in ncp
118 source = second_order.get_pde_term(
119 "volumetricSource", stateless=True
120 ).implementation
121 assert "Q, derivatives," in source
122 # The first-order solver passes the secondary entries behind the primary ones
123 assert (
124 "Q, Q + 25,"
125 in first_order.get_pde_term("volumetricSource", stateless=True).implementation
126 )
127 assert "deltaQ, deltaQ + 25," in _ncp_call(first_order)
128 # The second-order solver has no storage for the secondary result
129 assert "S,\n Q, derivatives," in source
130 assert "[58]" not in ncp + source
131
132
134 implementation = (
135 exagrype.api.FOSOCCZ4Solver("SO").get_pde_term("initPointWisely").implementation
136 )
137 assert "gaugeWave" in implementation
138 assert "initialValues[58]" in implementation
139 assert "Q[i] = initialValues[i]" in implementation
140 assert "derivatives[i] = initialValues[25 + i]" in implementation
141
142
144 # The second-order kernels remove the advection of all evolved unknowns from the kernels
145 moving_puncture = exagrype.api.FOSOCCZ4Solver(
146 "SO", parameters={"CCZ4b": 1, "CCZ4s": 1}
147 )
148 assert moving_puncture.advected_quantities() == list(
149 exagrype.api.FOSOCCZ4Solver.Unknowns
150 )
151 # The default CCZ4s=0 does not advect the shift and b
152 default = exagrype.api.FOSOCCZ4Solver("SO")
153 assert "shift" not in default.advected_quantities()
154 assert "b" not in default.advected_quantities()
155 assert "G" in default.advected_quantities()
156 assert "lapse" in default.advected_quantities()
157 # Neither does the non-shifting shift
158 non_shifting = exagrype.api.FOSOCCZ4Solver(
159 "SO", parameters={"CCZ4b": 0, "CCZ4s": 1}
160 ).advected_quantities()
161 assert set(moving_puncture.advected_quantities()) - set(non_shifting) == {
162 "shift",
163 "b",
164 }
165
166
168 assert (
169 "enforceCCZ4constraintsOfEvolvedVariables"
170 in exagrype.api.FOSOCCZ4Solver("SO")._enforcement_implementation()
171 )
172 assert (
173 "enforceCCZ4constraintsOfEvolvedVariables"
174 not in exagrype.api.FOCCZ4Solver("FO")._enforcement_implementation()
175 )
176
177
181 assert (
182 exagrype.api.FOSOCCZ4Solver.EnforceCCZ4Constraint
183 is exagrype.api.FOCCZ4Solver.EnforceCCZ4Constraint
184 )
185
186
188 pair = exagrype.api.FOSOSolver("SO")
189 assert isinstance(pair.ccz4, exagrype.api.FOSOCCZ4Solver)
190 assert isinstance(pair.reconstruction, exagrype.api.FOSOReconstructionSolver)
191 assert pair.ccz4.name == "SO"
192 assert pair.reconstruction.name == "SOReconstruction"
193 assert pair.reconstruction.ccz4 is pair.ccz4
194 # Same data, swapped roles
195 assert pair.ccz4.unknowns == 25
196 assert pair.ccz4.number_of_auxiliary_variables == 33
197 assert pair.reconstruction.unknowns == 33
198 # The unknowns of the reconstruction are the auxiliary variables of the CCZ4 solver
199 assert pair.reconstruction.variable_names_per_entry() == [
200 name.removeprefix("Derivatives.")
201 for name in pair.ccz4.derivatives.variable_names_per_entry()
202 ]
203 assert pair.reconstruction.pde_terms == []
204 assert pair.reconstruction.plotters == []
205
206
208 tmp_path,
209):
210 pair = exagrype.api.FOSOSolver("SO", reconstruction_name="Recon")
211 project, _ = _project(tmp_path)
212 project.add_solver(pair)
213 assert project._solvers == [pair.ccz4, pair.reconstruction]
214 assert project._realisations == [pair]
215 assert pair.reconstruction.name == "Recon"
216
217
219 """! Neither solver sees the tasks of the other one, only the host's graph holds both"""
220 pair = exagrype.api.FOSOSolver("SO")
221 _configure(pair)
222 exahype2_project, peano_project = _project(tmp_path)
223 recorded = {}
224 for solver in (pair.ccz4, pair.reconstruction):
225 original = solver.generate_solver_realisation
226
227 def record(*args, _solver=solver, _original=original, **kwargs):
228 result = _original(*args, **kwargs)
229 # A snapshot, as the host adds dependencies to the shared tasks afterwards
230 recorded[_solver.name] = (
231 args[3],
232 [(t.name, len(t.dependencies)) for t in args[3].tasks],
233 kwargs,
234 )
235 return result
236
237 solver.generate_solver_realisation = record
238 project_graphs = [peano4.api.taskgraph.Graph() for _ in range(4)]
239 pair.generate_solver_realisation(exahype2_project, peano_project, *project_graphs)
240
241 ccz4_graph, ccz4_tasks, ccz4_kwargs = recorded["SO"]
242 recon_graph, recon_tasks, recon_kwargs = recorded["SOReconstruction"]
243 assert ccz4_graph is not recon_graph
244 assert ccz4_graph is not project_graphs[1]
245 assert not any("Reconstruct derivatives" in name for name, _ in ccz4_tasks)
246 chain = [
247 ("Reconstruct primary variables", 1),
248 ("Interpolate primary variables on hanging faces", 0),
249 ("Interpolate derivative variables on hanging faces", 0),
250 ("Reconstruct derivative variables", 1),
251 ("Reconstruct derivatives", 2),
252 ("Project derivative variables onto faces", 1),
253 ("Restrict derivative variables on hanging faces", 1),
254 ("Roll over derivative variable faces", 1),
255 ]
256 assert recon_tasks == [
257 (f"SOReconstruction_{task_name} stage {stage}", dependencies)
258 for stage in range(4)
259 for task_name, dependencies in chain
260 ]
261 # Concatenation: the project holds the tasks of both, wrapped into the global tasks
262 assert len(project_graphs[1].tasks) == len(ccz4_tasks) + len(recon_tasks) + 2
263 # Both are intermediate steps: The host adds the global tasks afterwards
264 assert ccz4_kwargs == {"add_global_tasks": False}
265 assert recon_kwargs == {"add_global_tasks": False}
266
267
269 titles = {}
270
271 def report(self, title="Task graph"):
272 titles[title] = [t.name for t in self.tasks]
273
274 monkeypatch.setattr(peano4.api.taskgraph.Graph, "log_as_text", report)
275 monkeypatch.setattr(
276 peano4.api.taskgraph.Graph, "visualise", lambda self, title="": None
277 )
278 previous = peano4.api.Logger().verbosity
279 peano4.api.Logger().verbosity = peano4.api.LoggerVerbosity.TEXT
280 try:
281 _realise(exagrype.api.FOSOSolver("SO"), tmp_path)
282 finally:
283 peano4.api.Logger().verbosity = previous
284 own_ccz4 = titles["Time stepping task graph for SO (CCZ4 part)"]
285 own_reconstruction = titles[
286 "Time stepping task graph for SOReconstruction (reconstruction part of SO)"
287 ]
288 host = titles[
289 "Time stepping task graph for host SO (concatenation of the graphs of SO, SOReconstruction)"
290 ]
291 assert not any("Reconstruct derivatives" in name for name in own_ccz4)
292 assert len(own_reconstruction) == 4 * 8
293 # The host adds the global tasks after it has concatenated the graphs
294 assert sorted(host) == sorted(
295 own_ccz4
296 + own_reconstruction
297 + [
298 "SO_Disable load balancing",
299 "SO_Dynamic mesh refinement and load balancing",
300 ]
301 )
302 assert not any("Disable load balancing" in name for name in own_ccz4)
303
304
306 pair = exagrype.api.FOSOSolver("SO")
307 _realise(pair, tmp_path)
308 methods = dict(pair.reconstruction._abstract_solver_methods)
309 for getter in [
310 "getMinTimeStamp",
311 "getMaxTimeStamp",
312 "getMinTimeStepSize",
313 "getMaxTimeStepSize",
314 ]:
315 definition = methods[f"double {getter}() const override;"]("A")
316 assert f"repositories::instanceOfSO.{getter}()" in definition
317 assert "SolverRepository.h" in pair.reconstruction.user_solver_includes
318
319
321 time_step = _realise(exagrype.api.FOSOSolver("SO"), tmp_path)
322 order = time_step.ordered_tasks()
323 for stage in range(4):
324 reconstruct = next(
325 task
326 for task in order
327 if task.name == f"SO_Interpolate Derivatives on hanging faces stage {stage}"
328 )
329 chain = {
330 task.name: task
331 for task in order
332 if task.name.startswith("SOReconstruction_")
333 and task.name.endswith(f" stage {stage}")
334 }
335 first_tasks = [
336 chain[
337 f"SOReconstruction_Interpolate primary variables on hanging faces stage {stage}"
338 ],
339 chain[
340 f"SOReconstruction_Interpolate derivative variables on hanging faces stage {stage}"
341 ],
342 ]
343 last = chain[
344 f"SOReconstruction_Roll over derivative variable faces stage {stage}"
345 ]
346 assert last in reconstruct.dependencies
347 assert order.index(last) < order.index(reconstruct)
348 if stage > 0:
349 # The reconstruction follows what the solver's reconstruction followed
350 for first in first_tasks:
351 assert any(
352 dependency.name.startswith("SO_Roll over stage")
353 or dependency.name.startswith("SO_Roll over solution")
354 for dependency in first.dependencies
355 )
356 assert f"SO_Prepare stage {stage}" in {
357 task.name for task in order[: order.index(first)]
358 }
359
360
362 pair = exagrype.api.FOSOSolver("SO")
363 solver = pair.ccz4
364 time_step = _realise(pair, tmp_path)
365 Access = peano4.api.taskgraph.Task.DataAccess
366 tasks = {task.name: task for task in time_step.tasks}
367 primary = tasks["SOReconstruction_Reconstruct primary variables stage 1"]
368 assert primary.cell_data_access[solver._stage_input] == Access.READ_AND_WRITE
369 assert (
370 primary.face_data_access[solver._patch_overlap_current_time_step]
371 == Access.READ_AND_WRITE
372 )
373 derivatives = tasks["SOReconstruction_Reconstruct derivative variables stage 1"]
374 assert (
375 derivatives.cell_data_access[solver.derivatives.patch] == Access.READ_AND_WRITE
376 )
377 assert (
378 derivatives.face_data_access[solver.derivatives.patch_overlap_current_time_step]
379 == Access.READ_AND_WRITE
380 )
381
382
384 time_step = _realise(
386 tmp_path,
387 enforce=exagrype.api.FOSOCCZ4Solver.EnforceCCZ4Constraint.IntermediateSteps,
388 )
389 names = [task.name for task in time_step.ordered_tasks()]
390 assert any("Enforce CCZ4 constraints" in name for name in names)
391 assert sum("Reconstruct derivatives" in name for name in names) == 4
392
393
395 pair = exagrype.api.FOSOSolver("SO")
396 solver = pair.ccz4
397 pair.numerical_scheme = exahype2.api.fv.RusanovWithExplicitEuler(solver, 9)
398 solver.grid_construction = exahype2.api.fv.SetupSimulationOverRegularGrid(
399 solver, volume_size=1.0 / 27.0
400 )
401 order = _realise_pair(pair, tmp_path).ordered_tasks()
402 reconstruct = next(
403 task
404 for task in order
405 if task.name == "SO_Interpolate Derivatives on hanging faces"
406 )
407 last = next(
408 task
409 for task in order
410 if task.name == "SOReconstruction_Roll over derivative variable faces"
411 )
412 assert last in reconstruct.dependencies
413 assert order.index(last) < order.index(reconstruct)
414 assert sum("Reconstruct derivatives" in task.name for task in order) == 1
415
416
418 pair = exagrype.api.FOSOSolver("SO")
420 pair.ccz4, 9
421 )
422 with pytest.raises(Exception):
423 pair.reconstruction.create_task_graph(None, None, 0, 1)
424
425
427 pair = exagrype.api.FOSOSolver("SO")
429 pair.ccz4, 9, rk_order=4
430 )
431 assert pair.ccz4.numerical_scheme.HALO_SIZE == 2
432 with pytest.raises(ValueError, match="at least three"):
433 pair.reconstruction.create_task_graph(None, None, 0, 1)
434
435
437 pair = exagrype.api.FOSOSolver("SO")
438 solver = pair.ccz4
439 pair.numerical_scheme = exagrype.api.FD4WithKODissipation(
440 solver, 9, rk_order=4, kreiss_oliger_sigma=1.0
441 )
442 solver.grid_construction = exahype2.api.rkfd4.SetupSimulationOverRegularGrid(
443 solver, volume_size=1.0 / 27.0
444 )
445 time_step = _realise_pair(pair, tmp_path)
446 assert sum("Reconstruct derivatives" in task.name for task in time_step.tasks) == 4
447 assert pair.reconstruction.numerical_scheme.halo_size == 3
448
449
450@pytest.mark.parametrize(
452)
454 """!
455 Finite Volumes and Finite Differences both call the static terms. The
456 non-conservative product is registered when the solver is realised
457 (test_realisation_registers_the_ncp_which_fits_the_numerical_scheme in
458 test_exagrype_ccz4_solver.py checks that it is stateless).
459 """
460 solver = solver_class("S")
461 for name in ["volumetricSource", "maxEigenvalue"]:
462 assert solver.get_pde_term(name, stateless=True).stateless
463 with pytest.raises(KeyError):
464 solver.get_pde_term(name)
465
466
468 tmp_path,
469):
470 time_step = _realise(exagrype.api.FOSOSolver("SO"), tmp_path)
471 tasks = {task.name: task for task in time_step.tasks}
472 for stage in range(4):
473 unknowns = tasks[f"SO_Interpolate hanging faces stage {stage}"]
474 derivatives = tasks[
475 f"SO_Interpolate Derivatives on hanging faces stage {stage}"
476 ]
477 reconstruct = tasks[f"SO_Reconstruct stage {stage}"]
478 reconstruct_derivatives = tasks[
479 f"SO_Reconstruct auxiliary variables Derivatives stage {stage}"
480 ]
481 chain_end = tasks[
482 f"SOReconstruction_Roll over derivative variable faces stage {stage}"
483 ]
484 # The unknowns do not wait for the derivatives and vice versa
485 assert unknowns not in derivatives.dependencies
486 assert derivatives not in unknowns.dependencies
487 assert unknowns in reconstruct.dependencies
488 assert derivatives in reconstruct_derivatives.dependencies
489 assert reconstruct_derivatives not in reconstruct.dependencies
490 assert reconstruct not in reconstruct_derivatives.dependencies
491 # The reconstruction chain is a block in front of both
492 assert chain_end in reconstruct.dependencies
493 # The derivatives are interpolated after they have been recomputed
494 assert chain_end in derivatives.dependencies
495 # The right-hand side waits for both reconstructions
496 rhs = tasks[f"SO_Evaluate Runge-Kutta right hand side stage {stage}"]
497 assert reconstruct in rhs.dependencies
498 assert reconstruct_derivatives in rhs.dependencies
Everything the first-order and the second-order CCZ4 solvers share.
Fourth-order Finite Differences with Kreiss-Oliger dissipation.
Solver for the Einstein equations in the first-order CCZ4 formulation.
Solver for the Einstein equations in the first-/second-order reconstructed CCZ4 formulation.
Reconstructs the first derivatives which exagrype.api.FOSOCCZ4Solver needs.
CCZ4 in the first-/second-order reconstructed formulation as a coupled pair.
Definition FOSOSolver.py:66
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....
Singleton logger for the Peano 4 Python API.
Definition Logger.py:37
Represents a Peano 4 project.
Definition Project.py:26
Holds a sequence of Task instances.
Definition Graph.py:9
This file is part of the ExaGRyPE project.
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_host_reports_the_concatenated_graph(tmp_path, monkeypatch)
test_interpolations_and_reconstructions_of_the_ccz4_solver_run_in_parallel(tmp_path)
_realise_pair(pair, tmp_path)
Let the host realise the pair and return the time step graph of the project.
_realise(pair, tmp_path, enforce=None)
Configure the pair with FD4, realise both solvers and return the time step graph.
test_pde_terms_are_stateless(solver_class)
Finite Volumes and Finite Differences both call the static terms.
test_project_registers_the_solvers_of_the_host_but_lets_the_host_realise_them(tmp_path)
_ncp_call(solver)
C++ which calls the kernel of the non-conservative product.
test_reconstruction_adapts_to_the_larger_halo_of_the_dissipation_scheme(tmp_path)
_configure(pair, enforce=None)
Configure the CCZ4 solver of the pair with FD4 and lopsided advection.
test_reconstruction_solver_forwards_the_time_step_data_of_the_ccz4_solver(tmp_path)
test_solvers_build_separate_graphs_which_the_host_concatenates(tmp_path)
Neither solver sees the tasks of the other one, only the host's graph holds both.