|
Peano
|
A brief discussion of how to couple differerent solvers.
ExaHyPE offers the opportunity to host multiple solver files within one mesh, to run them in parallel and to couple them as they run. This is useful for a variety of challenges:
If a solver A wants to exchange information with B, then A's information ends up as auxiliar data in B. Mathematically, this can be phrased as follows: Assume we solve the coupled problem
\[\begin{aligned} \frac{\partial Q^{(A)}}{\partial t} + \nabla . F^{(A)}(Q^{(A)}, Q^{(B)}, \nabla Q^{(A)}, \nabla Q^{(B)}) + \mathcal{B}^{(A)}(Q^{(A)}, Q^{(B)}) . \nabla Q^{(A)} & = & S^{(A)}(Q^{(A)}, Q^{(B)}) \\ \frac{\partial Q^{(B)}}{\partial t} + \nabla . F^{(B)}(Q^{(A)}, Q^{(B)}, \nabla Q^{(A)}, \nabla Q^{(B)}) + \mathcal{B}^{(B)}(Q^{(A)}, Q^{(B)}) . \nabla Q^{(B)} & = & S^{(B)}(Q^{(A)}, Q^{(B)}) \end{aligned} \]
This is not how ExaHyPE tackles this problem straightaway, unless you want to write a completely new code base from scratch, which basically solves one PDE over \( Q := ( Q^{(A)}, Q^{(B)} ) \). Instead, it breaks down the problem into
\[\begin{aligned} \frac{\partial Q^{(A)}}{\partial t} + \nabla . F^{(A)}_{{\color{blue}{M^{(A)}}}}(Q^{(A)}, \nabla Q^{(A)}) + \mathcal{B}^{(A)}_{{\color{blue}{M^{(A)}}}}(Q^{(A)}) . \nabla Q^{(A)} & = & S^{(A)}_{{\color{blue}{M^{(A)}}}}(Q^{(A)}) \\ \frac{\partial Q^{(B)}}{\partial t} + \nabla . F^{(B)}_{{\color{blue}{M^{(B)}}}}(Q^{(B)}, \nabla Q^{(B)}) + \mathcal{B}^{(B)}_{{\color{blue}{M^{(B)}}}}(Q^{(B)}) . \nabla Q^{(B)} & = & S^{(B)}_{{\color{blue}{M^{(B)}}}}(Q^{(B)}) \\ {\color{blue}{M^{(A)}}} & = & \Pi_{B\to A}\,Q^{(B)} \\ {\color{blue}{M^{(B)}}} & = & \Pi_{A\to B}\,Q^{(A)} \end{aligned} \]
or even
\[\begin{aligned} \frac{\partial Q^{(A)}}{\partial t} + \nabla . F^{(A)}(Q^{(A)}, \nabla Q^{(A)}) + \nabla . {\color{red}{\tilde{F}^{(A)}_{{\color{blue} M^{(A)}}}(Q^{(A)}, \nabla Q^{(A)})}} + \mathcal{B}^{(A)}(Q^{(A)}) . \nabla Q^{(A)} + {\color{red}{\tilde{\mathcal{B}}^{(A)}_{{\color{blue} M^{(A)}}}(Q^{(A)})}} . \nabla Q^{(A)} & = & S^{(A)}(Q^{(A)}) + {\color{red}{\tilde{ S}^{(A)}_{{\color{blue} M^{(A)}}}(Q^{(A)})}} \\ \frac{\partial Q^{(B)}}{\partial t} + \nabla . F^{(B)}(Q^{(B)}, \nabla Q^{(B)}) + \nabla . {\color{red}{\tilde{F}^{(B)}_{{\color{blue} M^{(B)}}}(Q^{(B)}, \nabla Q^{(B)})}} + \mathcal{B}^{(B)}(Q^{(B)}) . \nabla Q^{(B)} + {\color{red}{\tilde{\mathcal{B}}^{(B)}_{{\color{blue} M^{(B)}}}(Q^{(B)})}} . \nabla Q^{(B)} & = & S^{(B)}(Q^{(B)}) + {\color{red}{\tilde{ S}^{(B)}_{{\color{blue} M^{(B)}}}(Q^{(B)})}} \\ {\color{blue}{M^{(A)}}} & = & \Pi_{B\to A}\,Q^{(B)} \\ {\color{blue}{M^{(B)}}} & = & \Pi_{A\to B}\,Q^{(A)} \end{aligned} \]
So here's what's happening here and what we try to express through these formulae:
Historically, we used to name auxiliary data material parameters. We switched to auxiliary, as material suggests that data are invariant. They are not.
As of the summer 2026, ExaHyPE allows users to attach an arbitrary number of auxiliary variables to each solver, and indeed an auxiliary variable of one solver can be the real variable of another one. This is both to simplify coupling, but also to avoid that users have to copy around data all the time, which only stresses the memory bandwidth.
While we could add the two solvers from the equation above one by one, we typically don't do this in ExaHyPE:
A user adds one coupling object (a "host", e.g. exagrype.api.euler.CCZ4WithEuler_OuterAdditiveCoupling), which then brings its embedded solvers along. These aggregated solvers then later will create their task graphs how to realise a feature independently, and it is then the host who has to merge these subgraphs properly:
In an outer coupling, each solver has its numerical scheme of its own. The mapping of the solvers to C++ remains solver-specific.
All examples below couple the same two solvers, so you can compare the arising task graphs. The solvers are
Each solver alone yields the following time stepping task graph (each box is a task, the arrows are dependencies, and the tasks run top-down). Euler has one stage (reconstruct the halo, update, compute the eigenvalue for the next time step size, project onto the faces):
CCZ4 runs two Runge-Kutta stages (reconstruct, evaluate the right-hand side, prepare, project, roll over, ...) and then wraps up:
In both cases, the last task is a global end-of-step task which determines the next time step size. The coupling does not touch the numerical schemes, it only adds tasks and dependencies. Each solver gets a set of auxiliary variables which holds the data of the other solver ("EulerFieldsForCCZ4", "CCZ4FieldsForEuler"): this is why CCZ4 contains a task "Reconstruct auxiliary variables EulerFields stage 0", while Euler contains "Reconstruct auxiliary variables CCZ4Fields". The pictures are generated by coupling-illustrations.py in the directory of this page.
In both outer couplings, the host adds the following building blocks:
The numerical scheme of each solver, i.e. the time stepping, patch size and halo, remain whatever the user picked. Load balancing and dynamic mesh refinement (global tasks) exist once and wrap the whole graph, but they do not show up in the pictures.
Multiplicative coupling means that the state of the simulation evolves according to
\( Q(t+\delta t) = Q(t) + \left( \mathcal{U}^{(A)} \circ \mathcal{U}^{(B)} \right) Q(t). \)
In the context of a solver, this means that solver B updates some variables as well as some auxiliary data. Both these variables (and potentially auxiliary data) then feed into solver A (as auxiliary values) and then induce some updates. Overall, solver B feeds into A. One comes after the other.
We call this outer coupling as each solver above can have its own numerical scheme. Notably, if A and B each host a higher-order integrator, these integrators are still only coupled once. In many cases, this reduces the overall convergence order, but there are solvers which just work fine this way.
ExaHyPE internally works with the notion of a task graph, which simplies the modelling of this type of coupling dramatically. We simply take the task graph of solver 1 and merge the one of solver 2 into it, and then wire them, (exagrype::api::euler::CCZ4WithEuler_OuterMultiplicativeCoupling is the worked example for Euler and CCZ4; exagrype::api::FOSOSolver is the example for the inner variant, where tasks are inserted per Runge-Kutta stage). All the remainder (notably finding the right number of required mesh sweeps) is done by ExaHyPE and Peano under the hood.
The image above shows multiplicative coupling for two solvers: The first one advanced their data (blue) feeding from the auxiliary variables (which might change but most of the time are invariant). After that, the evolved solver data become auxiliary variables to the second solver.
For an explicit Euler, it is very easy to rewrite this coupling scheme explicitly:
\[\begin{aligned} M^{(A)}& := & Q^{(B)}(t) \\ Q^{(A)}(t+0.5\Delta t) & = & Q^{(A)}(t) + \Delta t \ \frac{ \partial Q^{(A)} }{\partial t}(t) \\ M^{(B)}& := & Q^{(A)}(t+0.5\Delta t) \\ Q^{(B)}(t+\Delta t) & = & Q^{(B)}(t) + \Delta t \ \frac{ \partial Q^{(B)} }{\partial t}(t) \\ Q^{(A)}(t+\Delta t) & := & Q^{(A)}(t+0.5\Delta t) \end{aligned} \]
where the actual physics remains determined by
\[\begin{aligned} \frac{\partial Q^{(A)}}{\partial t}(t) + \nabla . F^{(A)}(Q^{(A)}, \nabla Q^{(A)}) + \nabla . {\color{red}{\tilde{F}^{(A)}_{{\color{blue} M^{(B)}}}(Q^{(A)}, \nabla Q^{(A)})}} + \mathcal{B}^{(A)}(Q^{(A)}) . \nabla Q^{(A)} + {\color{red}{\tilde{\mathcal{B}}^{(A)}_{{\color{blue} M^{(B)}}}(Q^{(A)})}} . \nabla Q^{(A)} & = & S^{(A)}(Q^{(A)}) + {\color{red}{\tilde{ S}^{(A)}_{{\color{blue} M^{(B)}}}(Q^{(A)})}} \\ \frac{\partial Q^{(B)}}{\partial t} + \nabla . F^{(B)}(Q^{(B)}, \nabla Q^{(B)}) + \nabla . {\color{red}{\tilde{F}^{(B)}_{{\color{blue} M^{(A)}}}(Q^{(B)}, \nabla Q^{(B)})}} + \mathcal{B}^{(B)}(Q^{(B)}) . \nabla Q^{(B)} + {\color{red}{\tilde{\mathcal{B}}^{(B)}_{{\color{blue} M^{(A)}}}(Q^{(B)})}} . \nabla Q^{(B)} & = & S^{(B)}(Q^{(B)}) + {\color{red}{\tilde{ S}^{(B)}_{{\color{blue} M^{(A)}}}(Q^{(B)})}} \\ \end{aligned} \]
The host exagrype::api::euler::CCZ4WithEuler_OuterMultiplicativeCoupling chains the graphs of the running example:
Read the picture top-down:
CCZ4's own end-of-step task "Compute time step size" does not sit in front of the second prepare task. It stays at the end of the time step, as a global end-of-step task may not have ordinary tasks depend on it. That is why the second prepare task waits for the last cell tasks of CCZ4 instead, and the host does not use the plain sink tasks of CCZ4's graph. The ordering is logical: the task graph compiler is free to fuse tasks into mesh sweeps and to overlap anything which the dependencies allow. Here, the dependencies leave little room, as everything of Euler waits for the end of CCZ4.
Inner multiplicative coupling means that we have N different solvers and invoke them one after the other. They are still running in sequence. However, in this setup, the numerical scheme sits within the host. The host decides how time advances.
Per internal step, it now calls the sequence of solvers. So if we have Runge-Kutta of fourth order with four shots, then each shot triggers two substeps if we wire together two steps.
The numerical scheme of such a coupled solver is called a generator. The user assigns it to the host (the coupled solver), and not to the aggregated solvers: the host rejects aggregated solvers which hold a numerical scheme of their own.
There is one generator for each combination of Finite Volumes (explicit Euler) and Runge-Kutta with fourth-order Finite Differences, and of multiplicative and additive coupling (four in total, exagrype::api::scalarfield::FiniteVolumesInnerMultiplicative, exagrype::api::scalarfield::FiniteVolumesInnerAdditive, exagrype::api::scalarfield::RungeKuttaFD4InnerMultiplicative and exagrype::api::scalarfield::RungeKuttaFD4InnerAdditive). All of them derive from exagrype::api::scalarfield::InnerCouplingGenerator.
The generator constructs the task graph of the whole combination:
We deliberately compose building blocks and do not rewrite the numerical schemes: a rewrite would duplicate the data structures, the kernels and the boundary treatment of both schemes for each pair of solvers.
In stage \( k \) of a Runge-Kutta scheme, a solver evaluates its right-hand side at the stage input \( Y_k \), which belongs to the time stamp \( t + c_k \Delta t \). For the integrator to keep its order, each solver has to see the data of the other solver at the same time stamp, i.e. the other solver's stage input \( Y_k \). It is not the other solver's outcome \( Y_{k+1} \) of this stage: this belongs to a different time stamp, and the order condition \( \sum_k b_k c_k = 1/2 \) would break (the classic Runge-Kutta scheme of order four yields 3/4, i.e. the scheme would be first order). Consequently,
In an outer coupling, the consumer works through all of its stages with the data of one time stamp (the one when the producer finished, or started). This is why outer couplings are first order in time: the spacetime is frozen within the time step. Inner couplings retain the order of the time integrator.
Additive coupling means that the state of the simulation evolved according to
\( Q(t+\delta t) = Q(t) + \mathcal{U}_1 Q(t) + \mathcal{U}_2 Q(t) = (id + \mathcal{U}_1 + \mathcal{U}_2 ) Q(t). \)
In ExaHyPE, we do not provide a special mechanism for additive coupling. Instead, we model the additive coupling via data dependencies. Let A1->A2->A3->A4 be the task graph of solver A. B1->B2->B3 is the graph of B. We simply let the two chains run in parallel. For this, we introduce an empty meta task C->A1|B1 and drop the arising task graph down into a task compiler.
If A and B work on completely different data, this is kind of trivial. If they share read-only data. If this is not the case, we have to copy data temporarily around.
The image above illustrates the challenge: We want to evolve A and B at the same time. A's current solution fees as auxiliary variable into B. Therefore, we first explicitly copy A's data into a temporary field, and then we evolve the two systems in parallel.
The "outer" annotation to the term "additive coupling" again implies that the solvers each own their own numerical integration scheme. We exchange the auxiliary variables initially, and then we let each solver march in time. Nothing stops us from letting once advance with Finite Volumes and the other one with Runge-Kutta. However, if we have to 3rd order Runge-Kutta schemes, the clue is that the solvers do not exchange data after each intermediate step. Instead, they only exchange data at the very start (and potentially as a final wrap-up). They only are coupled in the outer time integration loop.
The host exagrype::api::euler::CCZ4WithEuler_OuterAdditiveCoupling (driven by benchmarks/exagrype/gauge-wave-with-Euler) puts the graphs of the running example next to each other:
Read the picture top-down:
CCZ4 owns the global tasks (the load balancing switch and the dynamic mesh refinement), i.e. these exist once and wrap the whole graph.
Both outer couplings are realised by the same host pattern, and in practice they differ in the wiring of the graphs only. Everything else is identical: the host is a plain class that offers the solvers and generate_solver_realisation(), each solver receives a set of auxiliary variables that holds the unknowns of the other solver, the numerical schemes are assigned to the solvers, and two "prepare auxiliary variables" tasks copy, project and restrict the data onto the faces. A final global task synchronises the time step sizes.
The only difference is which tasks the prepare tasks are wired to:
Consequently, an additive solver B sees the state of A at the begin of the time step, while a multiplicative B sees the updated state of A. The additive tasks of A and B can overlap, whereas multiplicative steps are logically sequential. The final synchronisation task depends on the end of the whole graph in both cases. Each solver's graph ends with a global end-of-step task, which computes the time step size. In the multiplicative coupling, the prep task of B waits for the one of A, too, and the task graph compiler does not fuse it into the mesh sweep of that task: it runs in a sweep of its own (one sweep more than if it only waited for the last cell tasks). The additive reference is exagrype.api.euler.CCZ4WithEuler_OuterAdditiveCoupling, driven by benchmarks/exagrype/gauge-wave-with-Euler. The AI skill (see below) generates both flavours from the same recipe.
Internal additive coupling follows the pattern we have seen when we distinguish outer and inner multiplicative coupling: Here, the time integrator sits in the host. Users are not allowed to attach a time integrator to the aggregated solvers.
As the time integrator steps through time with potentially many Runge Kutta shots, each intermediate step exchanges the auxiliary data and launches the solvers' fundamental steps in parallel. The generators exagrype::api::scalarfield::FiniteVolumesInnerAdditive and exagrype::api::scalarfield::RungeKuttaFD4InnerAdditive realise this: per stage, the copy task precedes the first tasks of both solvers, and nothing orders the two stages relative to each other. The task graph compiler fuses the tasks of the two solvers into the same mesh sweeps. The details are in the section on inner multiplicative coupling above.
The benchmark benchmarks/exagrype/gauge-wave-with-scalar-field (design notes: DESIGN.md there) couples CCZ4 on the gauge wave to four scalar fields (exagrype::api::scalarfield::ScalarField), which the lapse of CCZ4 advects:
\( \partial_t \phi + \alpha^2 \partial_x \phi = 0. \)
The coupling is a fake coupling: only the advection velocity depends on the spacetime, the field does not feed back. There is one auxiliary set, which holds the 58 entries of CCZ4 in the scalar field solver, and hence one copy task per stage. The advection velocity is the metric component \( g_{xx} = 1 - A \sin(2 \pi (x-t)) \) of the gauge wave, and the equation has the closed-form solution
\( \phi_m(x,t) = \sin(2 \theta_0 + m \pi/4), \quad \tan \theta_0 = e^{2 \pi A t} \tan (\pi (x-t)), \)
so that the error of the scalar field is measurable. The script convergence-study.py there runs the outer multiplicative coupling and both inner couplings with rk4fd4 and with Finite Volumes, and checks the estimated orders. The data-flow argument above predicts a first-order outer coupling and inner couplings which reach the order of the scheme. At the time of writing, the benchmark generates and runs for all combinations tried, but the convergence study has not been completed (see DESIGN.md), i.e. these orders are expectations and not yet measurements.
Often, solvers have to synchronise with each other. For most solvers, they might for example want to agree on a joint time step size. For this, we have to embed the arising task graph into global tasks which ensure that the solver attributes become consistent. In the outer couplings above, this is the global end-of-step task which synchronises the time step sizes (exagrype::api::actionsets::SynchroniseTimeStepSizes). In inner couplings, where the host steps through the stages of a Runge-Kutta scheme, such a task can sit behind each stage.
There is a dedicated AI skill to couple solvers written down as recipe in documentation/exahype2/programmers-guide/coupling/SKILL.md. You can invoke it via
The skill covers many technical aspects:
Couplings in ExaGRyPE are deliberately minimalist, and so are the ones you write. The two reference couplings, exagrype.api.euler.CCZ4WithEuler_OuterAdditiveCoupling and exagrype.api.euler.CCZ4WithEuler_OuterMultiplicativeCoupling, are plain classes: no base class, two named solvers, no loops, no generic helpers, and next to the docstrings only a few dozen lines of code. Whatever the user does not ask for is not built. Ask at most the following questions, propose the default in brackets, and move on as soon as the user accepts it:
Do not ask about, and do not build, the following unless the user brings it up: data-race analyses, restricting the copied unknowns, global user-defined synchronisation, bi-directional physics terms. They are listed as optional extras at the end. Defaults you do not ask about: copy all unknowns of the producer, interpolate piecewise linearly, enforce the minimum time step size, the first solver owns load balancing and AMR.
Keep it simple. There is no generic framework class to derive from: write one plain Python class per coupling, with a few dozen lines of code, and copy the pattern from exagrype.api.euler.CCZ4WithEuler_OuterAdditiveCoupling (outer coupling) rather than inventing new machinery. Do not add base classes, registries or hooks. The class is what the user adds to the project (project.add_solver(coupled)). It offers
How the graphs are put together is the only part that differs between couplings. Write it out in generate_solver_realisation() (spaghetti code is fine: there are two solvers, so no loop, no helper hierarchy):
This is the whole coupling of a one-directional multiplicative coupling (CCZ4 runs first, Euler sees its result). It generates a complete Peano project (checked for Euler + CCZ4 with text logging). Everything else in the reference couplings is a second direction, comments, or the second solver's copy of the same code:
_create_prep_task() (~45 lines: one cell task with four action sets, see Step 2) is the only larger piece. Copy it unchanged from a reference coupling into the new class, together with the constant PREP_TASK_NAME, rather than importing it or moving it into a shared base: each coupling stays readable on its own. For a second direction, add a second auxiliary set and a second prep task. For the additive flavour, replace the three append() lines by Graph.merge() of the solver graphs plus the dozen lines of _couple() from the reference.
In an outer coupling every solver has a complete numerical scheme of its own, assigned as for any other solver (coupled.ccz4.numerical_scheme = ..., coupled.euler.numerical_scheme = ...). In an inner coupling the user assigns one scheme (a generator) to the coupling and none to the solvers; see the section on inner couplings below and exagrype.api.scalarfield. The mapping of each solver to C++ (SolverToCPPFiles, grid construction, plotters) remains solver-specific.
Composition happens through dependencies. It does not copy tasks. Use Graph.source_tasks() / Graph.sink_tasks() to find where to hook in, Graph.append() to run a whole graph after another one, and Graph.merge() to put graphs next to each other. If possible, display a rendering of each of these graphs to the user.
Some tasks belong to the mesh rather than to a solver: the task that switches the load balancing off, the last task which evaluates the adaptivity criterion and switches the load balancing on again, and the mesh adaptation phase. They must exist once. Each solver's generate_solver_realisation() takes a flag add_global_tasks; the coupling passes False to all solvers and wraps the coupled graph itself (see above). Ask the user which solver's adaptivity criterion should drive the mesh (by default the first one).
If the coupling is volumetric, ask the user if auxiliary variables of one solver are actually primary (evolved) variables of another one, or if solvers share auxiliary variables accordingly. After that, change the data association accordingly.
Outer additive is the one in benchmarks/exagrype/gauge-wave-with-Euler. Outer multiplicative has a host in exagrype.api.euler which was validated by generating the Euler + CCZ4 project (no C++ build or run yet), and one in exagrype.api.scalarfield (benchmark benchmarks/exagrype/gauge-wave-with-scalar-field). The inner types (one worked example each, for CCZ4 with a scalar field) are described below, after the outer ones. Do not claim more than what the benchmark's convergence study shows.
Both outer flavours are complete and share everything but one step. In both, each solver brings its own numerical scheme (time stepping, patch size and halo may differ), and the solvers exchange data only once per time step, never per Runge-Kutta stage. Ask the user which flavour they want (Capture Intent) and then follow Steps 1 to 4. Only Step 2 branches.
| outer multiplicative | outer additive | |
|---|---|---|
| Update | Q(t+dt) = (U_A o U_B) Q(t) | Q(t+dt) = (id + U_A + U_B) Q(t) |
| Wiring of the graphs | in sequence: A, copy, B | in parallel: copy, A and B together |
| What B sees of A | A's updated state | A's state at the begin of the time step |
| Prep tasks | two, one in front of A and one between A and B | two, both in front of A and B, run in parallel |
| Host in the repository | exagrype.api.euler.CCZ4WithEuler_OuterMultiplicativeCoupling | exagrype.api.euler.CCZ4WithEuler_OuterAdditiveCoupling |
| Benchmark | benchmarks/exagrype/gauge-wave-with-scalar-field (--coupling outer-multiplicative, scalar field instead of Euler) | benchmarks/exagrype/gauge-wave-with-Euler |
For Euler + CCZ4, the driver of the benchmark is the template for both. The two host classes are independent of each other and differ in how they wire the graphs only; everything else (constructor, generate_solver_realisation(), _create_prep_task()) is deliberately repeated, so that each class reads on its own. If the user wants exactly this pair, use the host class from exagrype.api directly and do not copy it. For other solver pairs, write a host along the same lines. coupling.dox (running example: Euler with Finite Volumes and CCZ4 with Runge-Kutta 2) shows the graphs that each flavour produces; documentation/exahype2/programmers-guide/coupling-illustrations.py regenerates those pictures.
Derive from nothing: one class per coupling, even if it repeats the constructor and the prep tasks of another one. Name the two solvers explicitly (self.ccz4, self.euler), no loops or lists of solvers over which you iterate generically. The constructor does this, in this order:
Each solver ends in a GLOBAL_END_OF_STEP task that computes the next time step size. Other tasks may depend on such a task, but the task graph compiler then does not fuse them into the mesh sweep of that task: they run in a sweep of their own.
Common to both flavours.** The final synchronisation depends on the sinks of the coupled graph (coupled.sink_tasks(); the additive reference filters out GLOBAL tasks, the multiplicative one need not).
Create two prep tasks (GridEntity.CELL, "Prepare auxiliary variables from <producer>"), one per direction. A prep task is the one place that writes new face data:
| data | access |
|---|---|
| producer._patch (persistent patch) | READ_AND_WRITE |
| auxiliary_copies.patch (auxiliary set of the consumer) | READ_AND_WRITE |
| auxiliary_copies.patch_overlap_update | TEMPORARY |
| auxiliary_copies.patch_overlap_current_time_step | READ_AND_WRITE |
Action sets in this order, guarded by the consumer's grid_selector.createCellGuard() (face sets: createFaceGuard()), with the consumer's _get_default_includes() + user_solver_includes:
Interpolation onto hanging faces needs no task: it rides along with the solvers' reconstruction.
Finally add one GridEntity.GLOBAL_END_OF_STEP task "Synchronise time step sizes" that depends on all of final and holds exagrype.api.actionsets.SynchroniseTimeStepSizes([A, B]). It enforces the minimum of the time step sizes on all solvers, the conservative choice. If the user wants further global exchange, put it into this task (see the end-of-step section below).
Branch: outer multiplicative (A first, B second).** Build the chain with Graph.append(), each graph after the one before:
(Each prep task sits in a graph of its own, created through _create_prep_task(graph, producer, consumer, auxiliary_copies).)
Ask the user which solver runs first. For CCZ4 + Euler, CCZ4 first is the default (the fluid then sees the new spacetime). A's graph is complete before the copy, and B starts after it, so there is no data race. Both solvers advance by the same (minimum) step, but B sees A at the end of that step.
Branch: outer additive.** Both prep tasks precede every first task of both solvers:
The two prep tasks do not depend on each other and run in parallel. As both copies are complete when any solver starts, no solver writes a patch that a copy still reads: there is no data race, and no temporary copy is needed. The solvers only see the state of the other one at the begin of the time step. For the additive flavour, the end of _couple() is exactly CCZ4WithEuler_OuterAdditiveCoupling._couple(). Collect first = time_step.source_tasks() before you create the prep tasks, as those are sources, too.
Show the graph of each solver alone, the concatenation and the coupled graph to the user: run the driver with the visualised logger (peano4.api.LoggerVerbosity.VISUALISATION, --mode debug in the benchmark), which renders each graph. The coupled graph has to show the chain (multiplicative) or the diamond (additive) above. Compare it with the pictures in coupling.dox.
Load balancing and AMR wrap the outcome. They come from the solver that owns the global tasks.
Take benchmarks/exagrype/gauge-wave-with-Euler/gauge-wave-with-Euler.py. Only the line that creates the host changes between the flavours.
If both solvers read and write the same mesh data other than the exchanged sets, there might be a race. In the outer flavours, the persistent patches are only read by the prep tasks, which finish before the consumer starts, so the answer is usually no. Ask the user anyway. If yes, add a temporary copy to a prep task and redirect one solver's access to it.
Worked example: exagrype.api.scalarfield (CCZ4WithScalarField_InnerCoupling and four generators), driven by benchmarks/exagrype/gauge-wave-with-scalar-field (design notes: DESIGN.md there). coupling.dox explains the concepts.
An inner coupling has one numerical scheme for the whole system. It is a generator, i.e. a class that assigns itself to the coupled solver. The class structure is as small as the outer one:
Rules that make the order of the time integrator survive (the reasons are in coupling.dox):
Outer couplings freeze the spacetime within the consumer's time step: first order in time, whatever the order of the time integrators. Inner couplings keep the order of the time integrator. If the user shows convergence plots or asks for high order, take the inner coupling. The outer coupling is the right choice if the solvers must be free to pick different schemes or patch sizes.
For outer additive coupling, SynchroniseTimeStepSizes in the final GLOBAL_END_OF_STEP task already is this global coupling (see above). The generic recipe below is for further, user-defined exchange.
If two solvers furthermore have to synchronise each other globally, then we have to insert a WrapUp task after the whole task graph which waits for the last tasks of all the created subtasks. Note that these might fork in their own right, i.e. we have to find all tasks that are not predecessor of any other task. peano4.api.taskgraph.Graph.sink_tasks() does that, for the host's concatenated graph.
The WrapUp tasks only does a global action in unprepare. The Abstract solver base class that's generated get a new functor the says synchronise, and the user is told to supply the missing information.
(Outer couplings: nothing to do. Each solver runs its stages internally, and the host's tasks sit in front of the first and behind the last task of the whole time step. Inner couplings handle the stages themselves, see above.)
The sections below go beyond a minimal coupling. Do not start them on your own initiative.
If we do temporary copies, do a C++ source code analysis and try to find out which unknowns of the copied data are actually read. In principle, the developer could now narrow down the auxiliary variables. This required a code rewrite. It is easier to only copy those unknowns which are actually used. The analysis needs a confirmation by the user if this is all correct.
Analyse if both solvers use some input data from the other solver. If not, we work with uni-directional coupling. Ask the user if they want to insert or append certain coupling tasks which introduce bi-directional coupling between fields, or if some key operations need alteration.
Often, the outcome (primary variables) of one task feed into the right-hand sides of coupled solvers. If this is the case and if we employ additive coupling, then we might need to rerun the dependency analysis and introduce temporary copies.