14 os.path.dirname(__file__),
"..",
"..",
"src",
"exagrype",
"ccz4",
"Kernels.h"
17 KERNELS_HEADER = kernels_header.read()
22 assert solver.unknowns == 58
23 assert solver.unknowns == exagrype.api.FOCCZ4Solver.NUMBER_OF_CCZ4_ENTRIES
24 assert solver.number_of_auxiliary_variables == 0
25 assert solver.variable_names_per_entry()[16] ==
"lapse"
26 assert solver.variable_names_per_entry()[23] ==
"Kex"
27 assert solver.variable_names_per_entry()[24] ==
"phi"
29 assert solver.variable_names_per_entry()[55:] == [
"P[0]",
"P[1]",
"P[2]"]
34 C++ which calls the kernel of term. The solver registers its
35 non-conservative product only when it is realised, so the call of
36 the kernel is taken from the hook which writes it.
38 if term == exahype2.api.NonconservativeProduct.NAME:
39 return solver._ncp_implementation()
40 return solver.get_pde_term(term, stateless=
True).implementation
45 The non-conservative product is missing until the solver is realised
46 (test_realisation_registers_the_ncp_which_fits_the_numerical_scheme)
49 names = {term.name
for term
in solver.pde_terms}
51 exahype2.api.VolumetricSource.NAME,
52 exahype2.api.MaxEigenvalue.NAME,
54 assert "::exagrype::ccz4::ncpFirstOrder(" in _call(
55 solver, exahype2.api.NonconservativeProduct.NAME
58 "::exagrype::ccz4::sourceFirstOrder("
59 in solver.get_pde_term(
"volumetricSource", stateless=
True).implementation
62 "::exagrype::ccz4::maxEigenvalue("
63 in solver.get_pde_term(
"maxEigenvalue", stateless=
True).implementation
66 assert not solver.requires_user_class
71 assert "initPointWisely" not in {term.name
for term
in solver.pde_terms}
77 assert "gaugeWave" in solver.get_pde_term(
"initPointWisely").implementation
80 "::exagrype::initialvalues::flat("
81 in solver.get_pde_term(
"initPointWisely").implementation
87 "CCZ4", parameters={
"CCZ4k1": 0.0,
"CCZ4LapseType": 1}
89 assert solver.parameters[
"CCZ4k1"] == 0.0
90 assert solver.parameters[
"CCZ4LapseType"] == 1
92 solver.parameters[
"CCZ4GLMd"]
93 == exagrype.api.FOCCZ4Solver.DefaultParameters[
"CCZ4GLMd"]
98 with pytest.raises(KeyError):
102@pytest.mark.parametrize("key", ["CCZ4ds", "CCZ4xi", "CCZ4sk", "CCZ4bs", "CCZ4SO"])
104 assert key
not in exagrype.api.FOCCZ4Solver.DefaultParameters
105 with pytest.raises(KeyError):
110 defaults = exagrype.api.FOCCZ4Solver.DefaultParameters
111 assert defaults[
"CCZ4b"] == 1
112 assert defaults[
"CCZ4s"] == 0
113 for key
in exagrype.api.FOCCZ4Solver.Switches:
114 assert isinstance(defaults[key], int)
117 attributes =
"\n".join(solver._abstract_solver_attributes)
118 assert "static constexpr int CCZ4b = 1;" in attributes
119 assert "static constexpr int CCZ4s = 1;" in attributes
120 assert "static constexpr double CCZ4f = 0.75;" in attributes
123@pytest.mark.parametrize("value", [1.0, 2, -1, True, "1"])
125 with pytest.raises(TypeError):
127 with pytest.raises(TypeError):
131@pytest.mark.parametrize(
134@pytest.mark.parametrize(
135 "term", [
"nonconservativeProduct",
"volumetricSource",
"maxEigenvalue"]
140 The CCZ4 parameters of a kernel call match the kernel's declaration
142 The parameters are doubles and integers, i.e. the compiler does not
143 notice if a call passes them in the wrong order. The test reads the
144 name of the kernel from the generated call and the parameters from its
145 declaration in Kernels.h, i.e. it holds no names of its own
146 (tests/unit/adrs/0001-unit-tests-query-names-from-the-code.md).
149 solver = solver_class(
"CCZ4")
150 call =
_call(solver, term)
151 kernel = re.search(
r"::exagrype::ccz4::(\w+)\(", call).group(1)
152 declaration = re.search(rf
"void {kernel}\((.*?)\);", KERNELS_HEADER, re.DOTALL)
153 declared_parameters = re.findall(
r"\bCCZ4\w+", declaration.group(1))
155 assert declared_parameters
156 assert re.findall(
r"\bCCZ4\w+", call) == declared_parameters
157 assert set(declared_parameters) <= set(solver_class.DefaultParameters)
162 Enforce = exagrype.api.FOCCZ4Solver.EnforceCCZ4Constraint
163 assert solver._enforce_ccz4_constraint == Enforce.No
164 solver.enforce_ccz4_constraint(Enforce.IntermediateSteps)
165 assert solver._enforce_ccz4_constraint == Enforce.IntermediateSteps
166 solver.enforce_ccz4_constraint(Enforce.No)
167 assert solver._enforce_ccz4_constraint == Enforce.No
168 with pytest.raises(TypeError):
169 solver.enforce_ccz4_constraint(
True)
172Kernel = exahype2.api.rkfd4.CentralDifferencesWithRungeKutta.RightHandSideKernel
183def _lopsided(solver, advected_quantities=None, advection_velocity=None):
184 """! FD4 with lopsided advection, by default of the quantities the solver asks for"""
185 return exahype2.api.rkfd4.CentralDifferencesWithRungeKutta(
189 right_hand_side_kernel=Kernel.CentralDifferencesWithLopsidedAdvection,
190 advection_velocity=advection_velocity or type(solver).ADVECTION_VELOCITY,
191 advected_quantities=(
192 solver.advected_quantities()
193 if advected_quantities is None
194 else advected_quantities
199def _finite_volumes(solver):
200 return exahype2.api.fv.RusanovWithExplicitEuler(solver, 9)
203@pytest.mark.parametrize(
204 "solver_class", [exagrype.api.FOCCZ4Solver, exagrype.api.FOSOCCZ4Solver]
206@pytest.mark.parametrize(
207 "parameters", [{}, {"CCZ4s": 1, "CCZ4b": 1}, {"CCZ4s": 1, "CCZ4b": 0}]
209@pytest.mark.parametrize(
210 "scheme_factory, advection",
212 (_central_differences, True),
213 (_dissipation, True),
215 (_finite_volumes, False),
218def test_ncp_adds_advection_iff_the_scheme_does_not(
219 solver_class, parameters, scheme_factory, advection
223 The non-conservative product adds the advection terms if and only if the
224 Finite Differences do not (src/exagrype/ccz4/adrs/0003-the-solver-picks-who-adds-the-advection.md).
225 The Finite Volumes add none, and neither does the non-conservative product.
228 solver = solver_class("CCZ4", parameters=parameters)
229 solver.numerical_scheme = scheme_factory(solver)
230 assert solver.ncp_adds_advection() == advection
233@pytest.mark.parametrize(
234 "solver_class", [exagrype.api.FOCCZ4Solver, exagrype.api.FOSOCCZ4Solver]
236def test_ncp_adds_advection_rejects_lopsided_advection_of_other_quantities(
241 A scheme which advects other quantities than those the kernels leave
242 out would add some advection terms twice or miss some
245 frozen_shift = solver_class("CCZ4")
246 moving_shift = solver_class("CCZ4", parameters={"CCZ4s": 1, "CCZ4b": 1})
248 dict(advected_quantities=frozen_shift.advected_quantities()[1:]),
249 dict(advected_quantities=moving_shift.advected_quantities()),
250 dict(advection_velocity="b"),
252 for choice in wrong_choices:
253 solver = solver_class("CCZ4")
254 solver.numerical_scheme = _lopsided(solver, **choice)
255 with pytest.raises(ValueError):
256 solver.ncp_adds_advection()
259def test_ncp_adds_advection_rejects_unknown_schemes():
260 solver = exagrype.api.FOCCZ4Solver("CCZ4")
261 with pytest.raises(TypeError):
262 solver.ncp_adds_advection()
265def _constexpr_list(code, name):
268 for entry in re.search(rf"constexpr int {name}\[\] = {{(.*?)}};", code)
274@pytest.mark.parametrize(
275 "solver_class", [exagrype.api.FOCCZ4Solver, exagrype.api.FOSOCCZ4Solver]
277@pytest.mark.parametrize(
279 [{}, {"CCZ4s": 1, "CCZ4b": 1}, {"CCZ4s": 1, "CCZ4b": 0}, {"CCZ4s": 0, "CCZ4b": 1}],
281def test_central_advection_adds_what_the_lopsided_advection_would_add(
282 solver_class, parameters
286 The advection which the non-conservative product appends covers exactly
287 the quantities and the velocity which the lopsided advection of the
288 scheme gets, i.e. either way the same terms are added. Both read them
289 from advected_quantities() and ADVECTION_VELOCITY, the one
290 definition of the advection terms. The call passes the arguments of
291 exahype2::fd::addCentralAdvection() in the order of its declaration.
294 solver = solver_class("CCZ4", parameters=parameters)
295 scheme = _lopsided(solver)
296 code = solver._advection_implementation()
298 quantities = _constexpr_list(code, "quantities")
299 assert quantities == scheme.advected_quantities
300 assert _constexpr_list(code, "velocity") == scheme.advection_velocity
301 assert all(index < solver.unknowns for index in quantities)
303 call = re.search(r"::exahype2::fd::addCentralAdvection\((.*?)\);", code)
304 assert call.group(1).split(", ") == [
311 str(len(quantities)),
315 os.path.dirname(__file__),
321 "CentralAdvection.h",
324 declaration = re.search(
325 r"void addCentralAdvection\((.*?)\);", header.read(), re.DOTALL
327 declared = [parameter.split()[-1] for parameter in declaration.group(1).split(",")]
335 "numberOfQuantities",
339def _realise(solver, tmp_path):
340 """! Realise a first-order solver with gauge wave initial data on the unit cube"""
341 exahype2_project = exahype2.api.Project(
342 ["probe"], "probe", "probe", directory=str(tmp_path)
344 exahype2_project.set_global_simulation_parameters(
346 offset=[0.0, 0.0, 0.0],
347 size=[1.0, 1.0, 1.0],
349 periodic_BC=[True, True, True],
351 peano_project = peano4.api.Project(["probe"], "probe", str(tmp_path))
352 graphs = [peano4.api.taskgraph.Graph() for _ in range(4)]
353 solver.generate_solver_realisation(exahype2_project, peano_project, *graphs)
356def _solver_with(scheme_factory):
357 solver = exagrype.api.FOCCZ4Solver("CCZ4", parameters={"CCZ4s": 1, "CCZ4b": 1})
358 solver.add_pde_term(exagrype.api.initialvalues.gauge_wave())
359 solver.numerical_scheme = scheme_factory(solver)
360 solver.grid_construction = exahype2.api.rkfd4.SetupSimulationOverRegularGrid(
361 solver, volume_size=1.0 / 27.0
366def _non_conservative_products(solver):
369 for term in solver.pde_terms
370 if term.name == exahype2.api.NonconservativeProduct.NAME
374@pytest.mark.parametrize(
375 "scheme_factory, advection",
376 [(_central_differences, True), (_lopsided, False)],
378def test_realisation_registers_the_ncp_which_fits_the_numerical_scheme(
379 scheme_factory, advection, tmp_path
383 The solver is created before its numerical scheme, so it can pick the
384 non-conservative product only when it is realised. It registers a
385 single, stateless one
388 solver = _solver_with(scheme_factory)
389 assert not _non_conservative_products(solver)
390 _realise(solver, tmp_path)
392 (term,) = _non_conservative_products(solver)
393 assert term.stateless
394 assert "::exagrype::ccz4::ncpFirstOrder(" in term.implementation
395 assert ("::exahype2::fd::addCentralAdvection(" in term.implementation) == advection
398def test_realising_twice_replaces_the_ncp_of_the_first_realisation(tmp_path):
399 """! A project can be generated several times"""
400 solver = _solver_with(_central_differences)
401 _realise(solver, tmp_path / "first")
402 _realise(solver, tmp_path / "second")
403 (term,) = _non_conservative_products(solver)
404 assert term.implementation.count("::exahype2::fd::addCentralAdvection(") == 1
407@pytest.mark.parametrize("stateless", [True, False])
408def test_realisation_rejects_an_ncp_added_by_somebody_else(stateless, tmp_path):
411 The solver would replace it silently, and it might add advection terms
412 the scheme adds as well, or miss them
413 (src/exagrype/ccz4/adrs/0003-the-solver-picks-who-adds-the-advection.md)
416 solver = _solver_with(_central_differences)
418 exahype2.api.NonconservativeProduct(implementation="", stateless=stateless)
420 with pytest.raises(ValueError, match="add_pde_term"):
421 _realise(solver, tmp_path)
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.
Fourth-order central Finite Differences with explicit Runge-Kutta time stepping, realised over a bloc...
exahype2.api.InitPointWisely gauge_wave()
Gauge wave along the x-axis,.
exahype2.api.InitPointWisely flat()
Flat (Minkowski) spacetime,.
This file is part of the ExaGRyPE project.
The task graph abstraction allows users to formalise their solver steps over small task graphs which ...
test_kernel_calls_pass_the_parameters_of_the_kernel_signature(solver_class, term)
The CCZ4 parameters of a kernel call match the kernel's declaration.
test_switches_are_integers()
test_solver_has_58_named_unknowns()
_call(solver, term)
C++ which calls the kernel of term.
test_parameters_override_defaults()
test_initial_condition_is_added_from_outside()
test_constraint_enforcement_is_opt_in()
test_removed_parameters_are_rejected(key)
test_unknown_parameter_is_rejected()
test_solver_has_no_initial_condition_of_its_own()
test_solver_registers_ccz4_pde_terms()
The non-conservative product is missing until the solver is realised (test_realisation_registers_the_...
_lopsided(solver, advected_quantities=None, advection_velocity=None)
FD4 with lopsided advection, by default of the quantities the solver asks for.
_central_differences(solver)
test_switches_reject_anything_but_zero_and_one(value)