Peano
Loading...
Searching...
No Matches
CCZ4WithEuler_OuterAdditiveCoupling.py
Go to the documentation of this file.
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
3"""
4
5import exahype2.api
6import peano4.api
9
10from .Euler import Euler
11from ..FOCCZ4Solver import FOCCZ4Solver
12from ..actionsets import CopyPatchWithLinearInterpolation, SynchroniseTimeStepSizes
13
14
16 """!
17
18 First-order CCZ4 and an Euler fluid, coupled only at the begin and the end of each time step
19
20 The class hosts one exagrype.api.FOCCZ4Solver (ccz4) and one
21 exagrype.api.euler.Euler (euler). Add the host, not its solvers, to the project.
22 The coupling is additive and affects the begin and the end of a time step
23 only. In between, the solvers are independent, i.e. each solver brings its
24 own numerical scheme and the schemes can differ in their time stepping, patch
25 size and halo. Assign them to the solvers as for any other solver:
26
27 coupled = exagrype.api.CCZ4WithEuler_OuterAdditiveCoupling()
28 coupled.ccz4.numerical_scheme = exahype2.api.rkfd4.CentralDifferencesWithRungeKutta(coupled.ccz4, 8, rk_order=4)
29 coupled.euler.numerical_scheme = exahype2.api.fv.RusanovWithExplicitEuler(coupled.euler, 17)
30
31 The host sets no initial conditions. Add them to the solvers as to any other solver:
32
33 coupled.ccz4.add_pde_term(exagrype.api.initialvalues.gauge_wave())
34
35 (Euler brings a smooth wave by default.)
36
37 If you prefer one numerical scheme for the whole compound, use the
38 coupling per stage instead (@ref page_exahype_programmers_guide_coupling).
39
40 ## Data
41
42 Each solver reads all variables of the other one as auxiliary variables:
43 ccz4 holds the set EulerFields (5 entries, resolved on the patch of CCZ4),
44 euler holds the set CCZ4Fields (58 entries, resolved on the patch of Euler).
45 The sets are read-only for the solvers, but they sit on the faces, and they
46 change from time step to time step. The solvers hold no persistent copy of
47 the fields of the other solver, i.e. the sets are temporary in the sense
48 that they are overwritten at the begin of each time step. They do not
49 change within a time step
50 (exahype2.api.AuxiliaryVariables.Mode.STATIONARY_THROUGHOUT_TIME_STEPS_ON_FACES),
51 i.e. the Runge-Kutta scheme of CCZ4 reconstructs the halo of the Euler
52 fields in its first stage only and holds it through the later stages.
53
54 ## Task graph
55
56 The time step graph of the host is the concatenation of the graphs of the
57 two solvers, preceded and followed by tasks of the host:
58
59 1. Two prep tasks run in parallel. Each fills the auxiliary variables of one
60 solver with the current solution of the other solver (if the patches have a
61 different size, piecewise linear interpolation, @ref
62 exagrype.api.actionsets.CopyPatchWithLinearInterpolation), projects them onto the faces,
63 restricts the hanging faces onto the coarse ones, and rolls the faces over. The solvers reconstruct the halos of the auxiliary
64 variables from these faces. Interpolation onto hanging faces
65 rides along with the reconstruction of the solvers.
66 2. All first tasks of both solvers wait for both prep tasks, so neither solver
67 writes data that a copy is still reading.
68 3. The solvers run their graphs independently.
69 4. A GLOBAL_END_OF_STEP task waits for the last tasks of both solvers and enforces the
70 minimum of the time step sizes on both of them
71 (@ref exagrype.api.actionsets.SynchroniseTimeStepSizes).
72
73 @dot
74 digraph G {
75 rankdir=LR;
76 node [shape=box, style=rounded, fontname=Helvetica];
77 prepCCZ4 [label="Prepare auxiliary variables\nfrom CCZ4\n(copy, project, restrict, roll over)"];
78 prepEuler [label="Prepare auxiliary variables\nfrom Euler\n(copy, project, restrict, roll over)"];
79 ccz4 [label="Task graph of CCZ4\n(RK4 with FD4)", shape=box3d];
80 euler [label="Task graph of Euler\n(Finite Volumes)", shape=box3d];
81 sync [label="Synchronise time step sizes\n(GLOBAL_END_OF_STEP)"];
82 prepCCZ4 -> ccz4; prepCCZ4 -> euler;
83 prepEuler -> ccz4; prepEuler -> euler;
84 ccz4 -> sync; euler -> sync;
85 }
86 @enddot
87
88 The two prep tasks run in parallel. Both solver graphs wait for both prep tasks, i.e.
89 the arrows into the solver graphs end at the first tasks of each graph, and the arrows
90 out of them start at their last tasks. Load balancing and dynamic mesh refinement wrap the whole picture.
91
92 The synchronisation takes the minimum of the two time step sizes, as this is the
93 conservative choice: the common step then respects the CFL condition of both solvers.
94
95 The restriction sits in the prep task although the mesh of the benchmark is regular. The
96 prep task is the one place that writes new face data, and restriction belongs to
97 the writer (@ref exahype2.api.adrs). It facilitates overlapping domain decomposition and
98 flux-preserving interpolation, which this class does not use.
99
100 The coupling is bi-directional in the data, but a solver only sees the state
101 of the other one at the begin of the time step. Neither the Einstein equations nor
102 the Euler equations make use of the data yet: the PDE terms ignore the
103 auxiliary variables unless you overwrite them.
104
105 ## Global tasks
106
107 CCZ4 owns the load balancing and the dynamic mesh refinement.
108
109 """
110
111 EULER_AUXILIARY_COPIES_NAME = "EulerFields"
112 """! Name of the auxiliary variables of CCZ4 which hold the Euler fields """
113 CCZ4_AUXILIARY_COPIES_NAME = "CCZ4Fields"
114 """! Name of the auxiliary variables of Euler which hold the CCZ4 fields """
115 PREP_TASK_NAME = "Prepare auxiliary variables from {}"
116 """! Name of the tasks that fill the auxiliary variables; the argument is the name of the solver that provides the data """
117 SYNCHRONISE_TASK_NAME = "Synchronise time step sizes"
118 """! Name of the final task """
119
120 def __init__(self, ccz4_name: str = "CCZ4", euler_name: str = "Euler"):
121 """!
122
123 Create the two solvers and the auxiliary variables through which they see each other
124
125 The coupling happens at the begin and at the end of the time step
126 only, so the numerical schemes of the solvers remain flexible.
127
128 @param ccz4_name: str
129 Name of the CCZ4 solver.
130
131 @param euler_name: str
132 Name of the Euler solver.
133
134 """
135 self.ccz4 = FOCCZ4Solver(ccz4_name)
136 self.euler = Euler(euler_name)
137 Mode = exahype2.api.AuxiliaryVariables.Mode
140 dict(Euler.Unknowns),
141 mode=Mode.STATIONARY_THROUGHOUT_TIME_STEPS_ON_FACES,
142 data_name="EulerFieldsForCCZ4",
143 )
146 dict(FOCCZ4Solver.Unknowns),
147 mode=Mode.STATIONARY_THROUGHOUT_TIME_STEPS_ON_FACES,
148 data_name="CCZ4FieldsForEuler",
149 )
150 self.ccz4.add_auxiliary_variables(self.euler_auxiliary_copies)
151 self.euler.add_auxiliary_variables(self.ccz4_auxiliary_copies)
152 self._name = f"{ccz4_name}With{euler_name}"
153
154 @property
155 def name(self) -> str:
156 return self._name
157
158 @property
159 def solvers(self):
160 """ The solvers the project has to register; exahype2.api.Project.add_solver() asks for them """
161 return [self.ccz4, self.euler]
162
164 self,
165 exahype2_project,
166 peano_project,
167 setup_simulation,
168 time_step,
169 plot,
170 mesh_adaptation,
171 ):
172 """!
173
174 Realise both solvers, couple their time stepping graphs, and add the outcome to the project's graphs
175
176 The arguments are those of exahype2.api.Solver.generate_solver_realisation().
177 Both solvers are realised without global tasks. The load balancing and the dynamic
178 mesh refinement wrap the coupled time step graph afterwards, through the scheme of CCZ4.
179
180 """
181 peano4.api.Logger().new_section("Host " + self.name)
182 coupled_time_step = peano4.api.taskgraph.Graph()
183 for solver in self.solvers:
184 own_time_step = peano4.api.taskgraph.Graph()
185 solver.generate_solver_realisation(
186 exahype2_project,
187 peano_project,
188 setup_simulation,
189 own_time_step,
190 plot,
191 mesh_adaptation,
192 add_global_tasks=False,
193 )
194 coupled_time_step.merge(own_time_step)
195 self._couple(exahype2_project, peano_project, coupled_time_step)
196
197 self.ccz4.numerical_scheme.wrap_task_graph_in_load_balancing_and_amr_tasks(
198 coupled_time_step
199 )
200 if self.ccz4.mesh_adaptation is not None:
201 self.ccz4.mesh_adaptation.construct_output(
202 exahype2_project, peano_project, mesh_adaptation
203 )
204
205 title = f"Time stepping task graph for host {self.name}"
206 if peano4.api.Logger().is_visualisation:
207 coupled_time_step.visualise(title=title)
208 if peano4.api.Logger().is_text:
209 coupled_time_step.log_as_text(title=title)
210 time_step.merge(coupled_time_step)
211
212 def _couple(self, exahype2_project, peano_project, time_step):
213 """!
214
215 Add the preamble and the final task around the concatenated graphs
216
217 @param exahype2_project: exahype2.api.Project
218 The project that hosts the system.
219
220 @param peano_project: peano4.api.Project
221 The Peano project that the exahype2 project assembles.
222
223 @param time_step: peano4.api.taskgraph.Graph
224 The concatenation of the time step graphs of both solvers. It
225 receives the new tasks. All its source tasks are first tasks of a
226 solver, and all its sink tasks (but the global ones) are last tasks
227 of a solver.
228
229 """
230 peano4.api.Logger().new_section(
231 "Coupling " + self.ccz4.name + " and " + self.euler.name
232 )
233 Entity = peano4.api.taskgraph.Task.GridEntity
234
235
236 first_tasks = time_step.source_tasks()
237 last_tasks = [
238 task for task in time_step.sink_tasks() if task.entity != Entity.GLOBAL
239 ]
240
241
245 preps = [
246 self._create_prep_task(time_step, self.ccz4, self.euler, self.ccz4_auxiliary_copies),
247 self._create_prep_task(time_step, self.euler, self.ccz4, self.euler_auxiliary_copies),
248 ]
249
250 for first_task in first_tasks:
251 for prep in preps:
252 first_task.add_dependency(prep)
253
254
255 synchronise = peano4.api.taskgraph.Task(
256 name=self.SYNCHRONISE_TASK_NAME, entity=Entity.GLOBAL_END_OF_STEP
257 )
258 for task in last_tasks:
259 synchronise.add_dependency(task)
260 synchronise.add_action_set(SynchroniseTimeStepSizes([self.ccz4, self.euler]))
261 time_step.add_task(synchronise)
262
263
265 self, time_step, source_solver, target_solver, auxiliary_copies
266 ):
267 """!
268
269 Create the task that fills the auxiliary variables of one solver with the solution of the other one
270
271 auxiliary copies (auxiliary_copies) of the target solver (copy, with
272 auxiliary variables auxiliary_copies of the target solver (copy, with
273 interpolation if the patches differ in size), projects them onto the
274 faces, restricts them on hanging faces, and rolls the faces over. It is
275 added to time_step, but no dependencies are set: the caller wires it.
276 It is the one place that writes new face data.
277
278 @param time_step: peano4.api.taskgraph.Graph
279 Receives the task.
280
281 @param source_solver: exahype2.api.Solver
282 Provides the data.
283
284 @param target_solver: exahype2.api.Solver
285 Holds the auxiliary variables.
286
287 @param auxiliary_copies: exahype2.api.AuxiliaryVariables
288 The auxiliary variables of the target solver.
289
290 @return peano4.api.taskgraph.Task
291 The new task.
292
293 """
294 Access = peano4.api.taskgraph.Task.DataAccess
295 Entity = peano4.api.taskgraph.Task.GridEntity
297 includes = (
298 target_solver._get_default_includes() + target_solver.user_solver_includes
299 )
301 name=self.PREP_TASK_NAME.format(source_solver.name),
302 entity=Entity.CELL,
303 )
304 prep.use_cell(source_solver._patch, Access.READ_AND_WRITE)
305 prep.use_cell(auxiliary_copies.patch, Access.READ_AND_WRITE)
306 prep.use_face(auxiliary_copies.patch_overlap_update, Access.TEMPORARY)
307 prep.use_face(auxiliary_copies.patch_overlap_current_time_step, Access.READ_AND_WRITE)
308 prep.add_action_set(
309 CopyPatchWithLinearInterpolation(
310 source_patch=source_solver._patch,
311 target_patch=auxiliary_copies.patch,
312 guard=target_solver.grid_selector.createCellGuard(),
313 additional_includes=includes,
314 )
315 )
316 prep.add_action_set(
317 actionsets.ProjectPatchOntoFaces(
318 auxiliary_copies.patch,
319 auxiliary_copies.patch_overlap_update,
320 guard=target_solver.grid_selector.createCellGuard(),
321 includes=includes,
322 )
323 )
324 prep.add_action_set(
325 toolbox.blockstructured.api.RestrictHangingFaces(
326 auxiliary_copies.patch_overlap_update,
327 scheme=target_solver.numerical_scheme.restriction,
328 clear_guard=target_solver.grid_selector.createFaceGuard(),
329 additional_includes=includes,
330 )
331 )
332 prep.add_action_set(
333 actionsets.RollOverUpdatedFace(
334 auxiliary_copies.patch_overlap_current_time_step,
335 auxiliary_copies.patch_overlap_update,
336 guard=target_solver.grid_selector.createFaceGuard(),
337 includes=includes,
338 )
339 )
340 time_step.add_task(prep)
341 return prep
Solver for the Einstein equations in the first-order CCZ4 formulation.
First-order CCZ4 and an Euler fluid, coupled only at the begin and the end of each time step.
_create_prep_task(self, time_step, source_solver, target_solver, auxiliary_copies)
Create the task that fills the auxiliary variables of one solver with the solution of the other one.
__init__(self, str ccz4_name="CCZ4", str euler_name="Euler")
Create the two solvers and the auxiliary variables through which they see each other.
_couple(self, exahype2_project, peano_project, time_step)
Add the preamble and the final task around the concatenated graphs.
generate_solver_realisation(self, exahype2_project, peano_project, setup_simulation, time_step, plot, mesh_adaptation)
Realise both solvers, couple their time stepping graphs, and add the outcome to the project's graphs.
Compressible Euler equations of an ideal gas.
Definition Euler.py:11
A set of auxiliary variables of a solver.
Singleton logger for the Peano 4 Python API.
Definition Logger.py:37
Holds a sequence of Task instances.
Definition Graph.py:9
One task within a peano4.api.taskgraph.Graph.
Definition Task.py:8
The task graph abstraction allows users to formalise their solver steps over small task graphs which ...
Definition __init__.py:1