1""" This file is part of the ExaGRyPE project. For conditions of distribution and
2 use, please see the copyright notice at www.peano-framework.org
6from typing
import Dict, List, Union
17 Everything the first-order and the second-order CCZ4 solvers share
19 The base class hosts the CCZ4 parameters, the PDE terms (which delegate
20 to the kernels in exagrype::ccz4) and the optional enforcement of the
21 CCZ4 constraints. The initial condition is up to the user. The kernels
22 always work on the 58 entries of CCZ4, see
23 exagrype.api.FOCCZ4Solver.Unknowns, which they take as two pointers:
24 one to the 25 primary and one to the 33 secondary entries. The two
25 subclasses differ only in where the secondary entries live:
27 - exagrype.api.FOCCZ4Solver evolves all 58 as unknowns. The secondary
28 entries follow the primary ones, i.e. the pointers are Q and Q+25.
29 - exagrype.api.FOSOCCZ4Solver evolves 25 unknowns, and the other 33
30 entries are auxiliary variables, i.e. they live in separate memory.
32 The subclasses therefore override the hooks _ncp_implementation() and
33 _source_implementation(), which call the kernel of their formulation.
34 The base class appends the advection terms to the non-conservative
35 product if the numerical scheme does not add them
36 (@ref exagrype.api.CCZ4SolverBase.CCZ4SolverBase.ncp_adds_advection()).
38 @ref exagrype.api.FOCCZ4Solver, @ref exagrype.api.FOSOCCZ4Solver
42 NUMBER_OF_CCZ4_ENTRIES = 58
43 """! Number of entries which the kernels in exagrype::ccz4 work on """
48 When to push the solution back onto the algebraic CCZ4 constraints
50 - No: never. This is the default.
51 - OnlyFinalSolution: after each task which writes a new solution
52 (exahype2.api.Solver.SolutionUpdateTaskTag), i.e. once per time
54 - IntermediateSteps: in addition after each task which writes an
55 intermediate solution (exahype2.api.Solver.IntermediateSolutionUpdateTag),
56 i.e. after each Runge-Kutta stage. For single-step schemes such as
57 the Finite Volumes, this is the same as OnlyFinalSolution.
59 @ref exagrype.api.CCZ4SolverBase.CCZ4SolverBase.enforce_ccz4_constraint()
85 Default values of the CCZ4 parameters, taken from the legacy ExaGRyPE
86 gauge wave setup: harmonic slicing (CCZ4LapseType=0) and a shift which
87 does not evolve (CCZ4s=0). The legacy black hole runs use 1+log slicing
88 (CCZ4LapseType=1) and the Gamma driver (CCZ4s=1) in its shifting shift
89 variant (CCZ4b=1, which only matters once the shift evolves). Integer
90 values become int constants of the generated solver class, all others
93 The integer parameters are switches:
95 - CCZ4LapseType: 0 for harmonic slicing, 1 for 1+log slicing.
96 - CCZ4b: variant of the Gamma driver, if the shift evolves. 1 is the
97 shifting shift, i.e. the equations of the shift, of b and of the
98 derivatives of the shift hold their advection terms. 0 is the
99 non-shifting shift without these terms
100 (exagrype::ccz4::ncpFirstOrder()).
101 - CCZ4s: 1 evolves the shift (Gamma driver), 0 freezes it.
103 Whether the first-order or the second-order formulation is solved is
104 not a parameter: Pick exagrype.api.FOCCZ4Solver or
105 exagrype.api.FOSOCCZ4Solver.
107 Why there are 58 entries, why the switches are integers and why there
108 are no parameters CCZ4ds and CCZ4xi is written down in
109 src/exagrype/ccz4/adrs/0001-ccz4-has-58-entries-and-integer-switches.md.
113 Switches = (
"CCZ4LapseType",
"CCZ4b",
"CCZ4s")
116 The integer parameters which accept 0 and 1 only.
120 ADVECTION_VELOCITY =
"shift"
123 The variable which advects the unknowns, i.e. the velocity of the
124 advection terms, whoever adds them: the lopsided advection of the
125 numerical scheme or the non-conservative product
126 (@ref exagrype.api.CCZ4SolverBase.CCZ4SolverBase.advected_quantities()).
133 unknowns: Dict[str, int],
134 parameters: Dict[str, Union[int, float]] =
None,
141 Name of the solver (@ref exahype2.api.Solver).
143 @param unknowns: Dict[str, int]
144 The evolved unknowns of the subclass (@ref exahype2.api.Solver).
145 The subclass has to add all entries of CCZ4 that it does not evolve
146 as auxiliary variables, so the entries add up to 58.
148 @param parameters: Dict[str, Union[int, float]]
149 CCZ4 parameters that differ from DefaultParameters, e.g.
150 {"CCZ4k1": 0.0}. Keys which are not in DefaultParameters are
151 rejected, as they most likely are typos. The switches
152 (@ref exagrype.api.CCZ4SolverBase.CCZ4SolverBase.Switches) have
153 to be the integers 0 or 1.
155 The solver has no initial condition. Add one as for any other solver,
156 e.g. solver.add_pde_term(exagrype.api.initialvalues.gauge_wave()), or
157 your own exahype2.api.InitPointWisely.
160 super().
__init__(name, unknowns=unknowns)
165 for key, value
in (parameters
or {}).items():
168 f
"Solver {name}: unknown CCZ4 parameter {key!r}. "
169 f
"Available parameters are {list(self.DefaultParameters)}."
172 isinstance(value, bool)
173 or not isinstance(value, int)
174 or value
not in (0, 1)
177 f
"Solver {name}: CCZ4 parameter {key!r} is an integer "
178 f
"switch, i.e. has to be 0 or 1, but got {value!r}"
183 'static_assert(Dimensions == 3, "CCZ4 (ExaGRyPE) is three-dimensional only");'
190 f
"static constexpr {cpp_type} {key} = {value};"
195#include "exagrype/ccz4/Kernels.h"
196#include "exahype2/fd/CentralAdvection.h"
197#include "exagrype/initialvalues/InitialValues.h"
198#include "exagrype/initialvalues/ApplyTwoPunctures.h"
208 The unknowns whose advection terms the kernels of exagrype::ccz4 leave out
210 The kernels hold no advection terms @f$ \\beta^k \\partial _k Q_q @f$
211 which need the derivative of the advected unknown q itself. This list
212 is the only place which defines these unknowns. Either the numerical
213 scheme adds their advection with lopsided stencils, e.g. via
215 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
216 exahype2.api.rkfd4.CentralDifferencesWithRungeKutta(
217 solver, patch_size, rk_order=4,
218 right_hand_side_kernel = exahype2.api.rkfd4.CentralDifferencesWithRungeKutta.RightHandSideKernel.CentralDifferencesWithLopsidedAdvection,
219 advection_velocity = exagrype.api.FOCCZ4Solver.ADVECTION_VELOCITY,
220 advected_quantities = solver.advected_quantities(),
222 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
224 These are all unknowns besides the conformal metric G, the lapse,
225 the shift and phi: The advection terms of the metric, the lapse,
226 the shift and phi are part of the algebraic source
227 (exagrype::ccz4::sourceFirstOrder()), which expresses them through the
228 auxiliary unknowns, i.e. the first derivatives.
230 or the solver appends it to the non-conservative product, discretised
231 with the central differences of the scheme
232 (exahype2::fd::addCentralAdvection()), if the scheme adds none. The
233 solver rejects a scheme which advects other quantities
234 (@ref exagrype.api.CCZ4SolverBase.CCZ4SolverBase.ncp_adds_advection()).
236 The advection of b and of the derivatives of the shift (dxShift,
237 dyShift, dzShift) belongs to the shifting shift only, i.e. the
238 routine leaves these unknowns out unless CCZ4s=1 and CCZ4b=1.
240 The equation of b holds the time derivative of Ghat (the unknown
241 Z), which comprises the advection of Ghat. The shifting shift
242 subtracts this advection again, i.e. only the advection of b itself
243 is left, which is added as for any other unknown. The non-shifting
244 shift (CCZ4b=0) keeps the advection of Ghat in the equation of b.
245 Who advects each quantity on its own cannot add it there, so the
246 kernel does so with the derivatives it is given
247 (src/exagrype/ccz4/adrs/0002-ccz4b-selects-the-shifting-or-the-non-shifting-shift.md).
251 (@ref exagrype.api.FOCCZ4Solver.FOCCZ4Solver.Unknowns).
254 excluded = {
"G",
"lapse",
"shift",
"phi"}
256 excluded |= {
"b",
"dxShift",
"dyShift",
"dzShift"}
257 return [name
for name
in self.Unknowns
if name
not in excluded]
262 Whether the non-conservative product has to add the advection terms
264 The advection terms @f$ \\beta^k \\partial _k Q_q @f$ of the unknowns
265 of advected_quantities() are added either by the numerical
266 scheme or by the non-conservative product, never by both. You choose
267 the formulation (the solver class) and whether the Finite Differences
268 use lopsided advection (the kernel of the numerical scheme). The
269 solver decides which fits this choice, and
270 generate_solver_realisation() registers the non-conservative product
273 - exahype2.api.rkfd4.CentralDifferencesWithRungeKutta (and its
274 subclass exagrype.api.FD4WithKODissipation) with the kernel
275 CentralDifferences adds no advection terms. The non-conservative
276 product adds them, discretised with the central differences of
278 - The same scheme with CentralDifferencesWithLopsidedAdvection adds
279 the advection itself, and the non-conservative product adds none.
280 The scheme has to advect exactly
281 @ref exagrype.api.CCZ4SolverBase.CCZ4SolverBase.advected_quantities()
282 with ADVECTION_VELOCITY. Otherwise, it would add some terms twice or
283 miss some, and the routine raises a ValueError.
284 - The Finite Volumes of exahype2.api.fv add no advection terms, and
285 the non-conservative product adds none either, i.e. they solve CCZ4
286 without these advection terms, as before
287 (@ref exagrype.api.FOCCZ4Solver). Whether the non-conservative
288 product should add them is open.
290 The rationale is written down in
291 src/exagrype/ccz4/adrs/0003-the-solver-picks-who-adds-the-advection.md.
294 True if the non-conservative product has to add the advection terms.
306 exahype2.api.rkfd4.CentralDifferencesWithRungeKutta.RightHandSideKernel
308 if scheme.right_hand_side_kernel == Kernel.CentralDifferences:
311 if scheme.advection_velocity != velocity
or sorted(
312 scheme.advected_quantities
313 ) != sorted(advected):
315 f
"Solver {self.name}: the lopsided advection of the "
316 f
"numerical scheme advects the quantities "
317 f
"{scheme.advected_quantities} with the velocity "
318 f
"{scheme.advection_velocity}, but the kernels leave the "
319 f
"quantities {advected} to the scheme, to be advected "
320 f
"with the velocity {velocity}. Pass "
321 f
"advection_velocity={type(self).__name__}.ADVECTION_VELOCITY "
322 f
"and advected_quantities=solver.advected_quantities() "
323 f
"to the scheme, or use its kernel CentralDifferences "
324 f
"instead, which lets the solver add the advection."
329 f
"Solver {self.name}: cannot decide who adds the advection "
330 f
"terms for the numerical scheme {type(scheme).__name__}. "
331 "Supported are exahype2.api.fv.RusanovWithExplicitEuler, "
332 "exahype2.api.fv.RusanovWithOptimisticExplicitEuler and "
333 "exahype2.api.rkfd4.CentralDifferencesWithRungeKutta."
338 Indices of the velocity components (ADVECTION_VELOCITY) and of the
339 advected unknowns (advected_quantities()) within Q, in the
340 form both the lopsided advection of the scheme and
341 exahype2::fd::addCentralAdvection() take them
343 resolve = exahype2.api.rkfd4.resolve_quantities
352 Set when the solver pushes the solution back onto the CCZ4 constraints
354 By default, the solver does not enforce the constraints. If you
355 switch the enforcement on, generate_solver_realisation() inserts a
356 task which applies exagrype::ccz4::enforceCCZ4constraints() to each
357 volume or grid point after each tagged solution update of the
360 @param value: CCZ4SolverBase.EnforceCCZ4Constraint
361 No, OnlyFinalSolution or IntermediateSteps.
366 f
"Solver {self.name}: enforce_ccz4_constraint() expects a "
367 f
"CCZ4SolverBase.EnforceCCZ4Constraint, but got {value!r}"
379 add_global_tasks=True,
383 Create the solver's task graphs and insert the constraint enforcement
385 The routine first registers the non-conservative product, with the
386 advection terms if the numerical scheme does not add them
387 (@ref exagrype.api.CCZ4SolverBase.CCZ4SolverBase.ncp_adds_advection()).
388 The solver cannot do this earlier, as the numerical scheme is
389 assigned after the solver has been created. Afterwards, the routine
390 calls the superclass, which lets the grid construction, the
391 numerical scheme and the plotter add their tasks and generates the
392 solver class with the PDE terms. If the constraint enforcement is
394 (enforce_ccz4_constraint()), it afterwards inserts the enforcement
395 after each tagged solution update of the numerical scheme. Which
396 patch a tagged task writes and which guard it uses depends on the
397 scheme, so the routine distinguishes the schemes explicitly
398 (@ref page_exahype2_solver_manipulate_solver_steps). It raises an
399 exception for any other scheme.
401 All insertions work on the patch which the tagged tasks write and
404 - exahype2.api.fv.RusanovWithExplicitEuler: one task after Update
406 - exahype2.api.fv.RusanovWithOptimisticExplicitEuler: one task after
407 both Update cell and Insert patch interior. Each cell runs exactly
408 one of the two, so the task constrains every cell once, after its
410 - exahype2.api.rkfd4.CentralDifferencesWithRungeKutta and its
411 subclass exagrype.api.FD4WithKODissipation: one task after
412 Complete time step and, for IntermediateSteps, one task after each
413 Prepare stage k on the stage input.
415 The scheme's log of the time stepping graph does not show the
416 enforcement. With TEXT or VISUALISATION logging, the routine hence
417 logs the graph once more.
444 stage_input = self._stage_input
447 f
"Solver {self.name}: cannot enforce the CCZ4 constraints for "
448 f
"the numerical scheme {type(scheme).__name__}. Supported are "
449 "exahype2.api.fv.RusanovWithExplicitEuler, "
450 "exahype2.api.fv.RusanovWithOptimisticExplicitEuler and "
451 "exahype2.api.rkfd4.CentralDifferencesWithRungeKutta."
459 time_step, exahype2.api.Solver.SolutionUpdateTaskTag
466 and stage_input
is not None
469 time_step, exahype2.api.Solver.IntermediateSolutionUpdateTag
474 f
"Time stepping task graph for {self.name} with CCZ4 constraint enforcement"
477 time_step.visualise(title=title)
479 time_step.log_as_text(title=title)
483 Tasks of time_step which carry tag and belong to this solver, as the
484 graph holds the tasks of all solvers
488 for task
in time_step.find_tasks(tag)
489 if task.name.startswith(self.
name +
"_")
495 Insert the enforcement of the CCZ4 constraints after anchors
497 The routine creates a task which applies
498 exagrype::ccz4::enforceCCZ4constraints() to each volume or grid point
499 of patch, and inserts it directly after all anchors via
500 peano4.api.taskgraph.Graph.insert_after(). The anchors' successors,
501 e.g. the projection onto the faces, hence see the constrained data
502 only. The task uses the solver's cell guard: If several anchors
503 write patch, each cell is updated by one of them, and the task runs
504 once per cell after all of them.
506 @param time_step: peano4.api.taskgraph.Graph
507 The time stepping task graph which holds the anchors.
509 @param anchors: List[peano4.api.taskgraph.Task]
510 The tasks which write patch. If the list is empty, the routine
513 @param patch: peano4.api.datamodel.Patch
514 The cell data which the anchors write, i.e. the solution or a
520 anchor_names =
" and ".join(
521 anchor.name[len(self.
name) + 1 :]
for anchor
in anchors
524 name=f
"{self.name}_Enforce CCZ4 constraints after {anchor_names}",
525 entity=peano4.api.taskgraph.Task.GridEntity.CELL,
528 task.use_cell(patch, peano4.api.taskgraph.Task.DataAccess.READ_AND_WRITE)
530 toolbox.blockstructured.api.ApplyPointWiselyToPatch(
538 time_step.insert_after(anchors, task)
542 C++ which pushes the unknowns Q of one volume back onto the constraints.
543 Hook for the subclasses, as the unknowns they evolve differ.
545 return "::exagrype::ccz4::enforceCCZ4constraints(Q);"
550 The CCZ4 parameters the solver is generated with (read-only copy).
557 Register the source and the maximum eigenvalue
559 The terms are stateless, i.e. static functions: they only use the
560 compile-time constants of the solver and the kernels of
561 exagrype::ccz4. Both the Finite Volumes (exahype2.api.fv) and the
562 Finite Differences (exahype2.api.rkfd4) call stateless terms. The
563 non-conservative product is not registered here: which one fits
564 depends on the numerical scheme, which is assigned after the solver
565 has been created, so generate_solver_realisation() registers it
566 (@ref exagrype.api.CCZ4SolverBase.CCZ4SolverBase._register_non_conservative_product()).
583 Register the non-conservative product, with or without the advection terms
585 generate_solver_realisation() calls this routine, as the
586 non-conservative product depends on the numerical scheme
587 (@ref exagrype.api.CCZ4SolverBase.CCZ4SolverBase.ncp_adds_advection()).
588 The term calls the kernel of the formulation (_ncp_implementation())
589 and, if advection is set, adds the advection terms of
590 advected_quantities() with ADVECTION_VELOCITY
591 (_advection_implementation()). It replaces the term of a former
592 realisation, so a project can be generated several times.
594 Any other non-conservative product, i.e. one added through
595 add_pde_term(), is rejected: The solver would replace it silently, and
596 it could add advection terms which the scheme adds as well, or miss
597 them (src/exagrype/ccz4/adrs/0003-the-solver-picks-who-adds-the-advection.md).
598 Whoever needs another non-conservative product derives a solver class
599 and overrides _ncp_implementation().
601 @param advection: bool
602 Whether the term adds the advection terms.
605 name = exahype2.api.NonconservativeProduct.NAME
613 f
"Solver {self.name}: a non-conservative product has been added "
614 f
"with add_pde_term(). {type(self).__name__} registers its "
615 f
"non-conservative product itself when it is realised, as the "
616 f
"advection terms depend on the numerical scheme "
617 f
"(ncp_adds_advection()), and would replace yours. Derive a "
618 f
"solver class from {type(self).__name__} and override "
619 f
"_ncp_implementation() instead."
625 implementation=implementation, stateless=
True
632 C++ which calls the non-conservative product kernel
634 The Finite Volume kernels hand over the jump deltaQ along one normal,
635 the fourth-order Finite Differences of exahype2.api.rkfd4 the
636 derivative along one normal times the mesh width. This is what
637 exagrype::ccz4::ncpFirstOrder() and ncpSecondOrder() expect.
638 Hook for the subclasses, as they select the formulation.
641 raise NotImplementedError
646 C++ which adds the advection terms to the non-conservative product
648 The term adds @f$ \\beta^n \\partial _n Q_q @f$ for the normal n of the
649 call and each unknown q of advected_quantities(),
650 discretised with the derivative the scheme hands over
651 (exahype2::fd::addCentralAdvection()). The indices are those which
652 the lopsided advection of a scheme gets, so either way the same
653 terms are added. Both formulations hold the unknowns in Q, and Q
660 constexpr int velocity[] = {{{", ".join(str(i) for i in velocity)}}};
661 constexpr int quantities[] = {{{", ".join(str(i) for i in quantities)}}};
662 ::exahype2::fd::addCentralAdvection(Q, deltaQ, BTimesDeltaQ, normal, velocity, quantities, {len(quantities)});
668 C++ which calls the source term kernel. Hook for the subclasses.
670 raise NotImplementedError
674 ::exagrype::ccz4::maxEigenvalue(Q, normal, CCZ4e, CCZ4GLMc, CCZ4GLMd, lambdaMax);
When to push the solution back onto the algebraic CCZ4 constraints.
Everything the first-order and the second-order CCZ4 solvers share.
str _ncp_implementation(self)
C++ which calls the non-conservative product kernel.
str _source_implementation(self)
C++ which calls the source term kernel.
__init__(self, str name, Dict[str, int] unknowns, Dict[str, Union[int, float]] parameters=None)
Create a CCZ4 solver.
_register_non_conservative_product(self, bool advection)
Register the non-conservative product, with or without the advection terms.
generate_solver_realisation(self, exahype2_project, peano_project, setup_simulation, time_step, plot, mesh_adaptation, add_global_tasks=True)
Create the solver's task graphs and insert the constraint enforcement.
Dict[str, Union[int, float]] parameters(self)
The CCZ4 parameters the solver is generated with (read-only copy).
_register_pde_terms(self)
Register the source and the maximum eigenvalue.
_advection_indices(self)
Indices of the velocity components (ADVECTION_VELOCITY) and of the advected unknowns (advected_quanti...
_enforce_ccz4_constraint
One task after all writers of the new solution: The optimistic Finite Volumes write it in one of two ...
_patch
One task after all writers of the new solution: The optimistic Finite Volumes write it in one of two ...
_tasks_of_this_solver(self, time_step, tag)
Tasks of time_step which carry tag and belong to this solver, as the graph holds the tasks of all sol...
str _advection_implementation(self)
C++ which adds the advection terms to the non-conservative product.
_non_conservative_product
_insert_constraint_enforcement(self, time_step, anchors, patch)
Insert the enforcement of the CCZ4 constraints after anchors.
str _enforcement_implementation(self)
C++ which pushes the unknowns Q of one volume back onto the constraints.
enforce_ccz4_constraint(self, "CCZ4SolverBase.EnforceCCZ4Constraint" value)
Set when the solver pushes the solution back onto the CCZ4 constraints.
str _max_eigenvalue_implementation(self)
List[str] advected_quantities(self)
The unknowns whose advection terms the kernels of exagrype.ccz4 leave out.
bool ncp_adds_advection(self)
Whether the non-conservative product has to add the advection terms.
The maximum absolute eigenvalue of the flux Jacobian in a given direction, i.e.
The non-conservative product B(Q) applied to a jump deltaQ in a given direction, i....
Abstract base class for all ExaHyPE 2 solvers.
"Solver.NumericalScheme" numerical_scheme(self)
add_abstract_solver_attribute(self, str declaration)
Add a raw C++ member attribute declaration to the generated Abstract<SolverName> class (.
str _get_default_includes(self)
Includes every generated action set can rely on being available, regardless of which action sets a co...
add_pde_term(self, PDETerm term)
Register a PDE term with this solver, or replace the one already registered under the same name and s...
add_user_solver_includes(self, str value)
Add further includes to this property, if your solver requires some additional routines from other he...
pde_terms
True iff at least one PDE term has no implementation.
str name(self)
Unique solver name.
The algebraic, volume-distributed source term S(Q) of a hyperbolic PDE - as opposed to PointSource,...
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,...
Fourth-order central Finite Differences with explicit Runge-Kutta time stepping, realised over a bloc...
Singleton logger for the Peano 4 Python API.
One task within a peano4.api.taskgraph.Graph.
The task graph abstraction allows users to formalise their solver steps over small task graphs which ...