Peano
Loading...
Searching...
No Matches
InnerCouplingGenerator.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 abc
6
7import peano4.api
9
10from ..actionsets import SynchroniseTimeStepSizes
11from .create_copy_task import create_copy_task
12
13
15 """!
16
17 Numerical scheme of the coupled solver exagrype.api.scalarfield.CCZ4WithScalarField_InnerCoupling
18
19 The generator builds the task graph of the whole combination of CCZ4 and the
20 scalar field. The scheme-specific parts are the numerical schemes of the two
21 solvers (_create_scheme()) and what a stage of the time integrator is
22 (_number_of_stages(), _stage_input(), _end_of_stage()). The four concrete generators
23 only provide these. Whether the coupling is multiplicative or additive
24 is the class attribute MULTIPLICATIVE.
25
26 ## Task graph
27
28 attach_to() installs a numerical scheme on each solver. The schemes are
29 building blocks: they create the data and the kernels of their solver, and the tasks
30 of its stages. The generator's construct_output() then
31
32 1. realises both solvers into graphs of their own (without global tasks) and merges them;
33 2. adds per stage k a task "Copy CCZ4 fields into ... stage k". It copies the input of
34 CCZ4's stage into the auxiliary variables of the scalar field, projects them onto the faces,
35 restricts them on hanging faces, and rolls them over. It reads the stage input before
36 CCZ4's reconstruction discards it, and it waits for the end of stage k-1 of both solvers;
37 3. lets the first tasks of the stage of the solvers wait for the copy task. This is additive
38 coupling: the stages of the solvers run in parallel. For multiplicative coupling, the first
39 tasks of the scalar field's stage also wait for the end of CCZ4's stage, i.e. the stage
40 of the scalar field follows the one of CCZ4;
41 4. adds a final global end-of-step task which enforces the minimum of the time step sizes, and
42 wraps the graph into the load balancing and the dynamic mesh refinement (through CCZ4's scheme).
43
44 The scalar field sees the CCZ4 data of the very time level of its own stage in both flavours.
45 This is what keeps the order of the time integrator: if the scalar field saw the outcome of CCZ4's stage,
46 its Runge-Kutta scheme would see the spacetime at the wrong time level.
47 Hence, the data of the two flavours are the same for a one-directional coupling, and
48 they differ in the schedule only.
49
50 """
51
52 MULTIPLICATIVE = False
53 """! The stage of the scalar field follows the stage of CCZ4 if True, they run in parallel if False """
54 COPY_TASK_NAME = "Copy CCZ4 fields into {} stage {}"
55 """! Name of the task that fills the auxiliary variables; arguments are the name of the scalar field and the stage """
56 SYNCHRONISE_TASK_NAME = "Synchronise time step sizes"
57 """! Name of the final task """
58
59 def __init__(self, patch_size: int):
60 """!
61
62 @param patch_size: int
63 Number of grid cells (volumes) per patch and axis. The same for both solvers.
64
65 """
66 self.patch_size = patch_size
67 self._host = None
68 self._schemes = {}
69
70 def attach_to(self, host):
71 """!
72
73 Install the numerical schemes of the building blocks on the solvers of the host
74
75 The host calls it when the user assigns the generator to its numerical_scheme.
76
77 """
78 self._host = host
79 self._schemes = {solver: self._create_scheme(solver) for solver in host.solvers}
80 for solver, scheme in self._schemes.items():
81 solver.numerical_scheme = scheme
82
84 self,
85 exahype2_project,
86 peano_project,
87 setup_simulation,
88 time_step,
89 plot,
90 mesh_adaptation,
91 ):
92 """!
93
94 Build the task graph of the coupled system, and add it to time_step
95
96 The arguments are those of exahype2.api.Solver.generate_solver_realisation().
97
98 """
99 ccz4, scalar_field = self._host.solvers
100 for solver in self._host.solvers:
101 if solver.numerical_scheme is not self._schemes[solver]:
102 raise ValueError(
103 f"Solver {solver.name} holds a numerical scheme of its own, "
104 f"but the numerical scheme of the coupled solver {self._host.name} rules"
105 )
106
107 graphs = {}
108 for solver in self._host.solvers:
109 graphs[solver] = peano4.api.taskgraph.Graph()
110 solver.generate_solver_realisation(
111 exahype2_project,
112 peano_project,
113 setup_simulation,
114 graphs[solver],
115 plot,
116 mesh_adaptation,
117 add_global_tasks=False,
118 )
119 coupled_time_step = peano4.api.taskgraph.Graph()
120 coupled_time_step.merge(graphs[ccz4])
121 coupled_time_step.merge(graphs[scalar_field])
122
123
124 previous_end = {ccz4: [], scalar_field: []}
125 for stage in range(self._number_of_stages()):
126 first = {
127 solver: self._first_tasks(graphs[solver], previous_end[solver])
128 for solver in self._host.solvers
129 }
130 end = {
131 solver: self._end_of_stage(solver, graphs[solver], stage)
132 for solver in self._host.solvers
133 }
134 copy = create_copy_task(
135 self.COPY_TASK_NAME.format(scalar_field.name, stage),
136 self._stage_input(ccz4, stage),
137 scalar_field,
138 self._host.ccz4_auxiliary_copies,
139 )
140 for task in previous_end[ccz4] + previous_end[scalar_field]:
141 copy.add_dependency(task)
142 for task in first[ccz4] + first[scalar_field]:
143 task.add_dependency(copy)
144 if self.MULTIPLICATIVE:
145 for task in first[scalar_field]:
146 for dependency in end[ccz4]:
147 task.add_dependency(dependency)
148 coupled_time_step.add_task(copy)
149 previous_end = end
150
151 synchronise = peano4.api.taskgraph.Task(
152 name=self.SYNCHRONISE_TASK_NAME,
153 entity=peano4.api.taskgraph.Task.GridEntity.GLOBAL_END_OF_STEP,
154 )
155 for task in coupled_time_step.sink_tasks():
156 synchronise.add_dependency(task)
157 synchronise.add_action_set(SynchroniseTimeStepSizes([ccz4, scalar_field]))
158 coupled_time_step.add_task(synchronise)
159
160 ccz4.numerical_scheme.wrap_task_graph_in_load_balancing_and_amr_tasks(
161 coupled_time_step
162 )
163 if ccz4.mesh_adaptation is not None:
164 ccz4.mesh_adaptation.construct_output(
165 exahype2_project, peano_project, mesh_adaptation
166 )
167
168 title = f"Time stepping task graph for host {self._host.name}"
169 if peano4.api.Logger().is_visualisation:
170 coupled_time_step.visualise(title=title)
171 if peano4.api.Logger().is_text:
172 coupled_time_step.log_as_text(title=title)
173 time_step.merge(coupled_time_step)
174
175 @staticmethod
176 def _first_tasks(graph, previous_end):
177 """!
178
179 The tasks of a solver's graph that start a stage
180
181 @param graph: peano4.api.taskgraph.Graph
182 The graph of one solver.
183
184 @param previous_end: List[peano4.api.taskgraph.Task]
185 The tasks that end the previous stage, empty for the first stage.
186 The first tasks are the sources of the graph in that case, and
187 otherwise all tasks that wait for one of previous_end.
188
189 """
190 if not previous_end:
191 return graph.source_tasks()
192 return [
193 task
194 for task in graph.tasks
195 if any(end in task.dependencies for end in previous_end)
196 ]
197
198 @abc.abstractmethod
199 def _create_scheme(self, solver):
200 """! The numerical scheme of one of the solvers, i.e. a building block of the whole scheme """
201
202 @abc.abstractmethod
203 def _number_of_stages(self) -> int:
204 """! Number of stages of the time integrator """
205
206 @abc.abstractmethod
207 def _stage_input(self, solver, stage):
208 """! The patch which a solver's stage reads as input (it holds the solver's unknowns of the stage's time level) """
209
210 @abc.abstractmethod
211 def _end_of_stage(self, solver, graph, stage):
212 """!
213
214 The tasks of a solver's graph that end a stage
215
216 Global tasks do not count.
217
218 """
Numerical scheme of the coupled solver exagrype.api.scalarfield.CCZ4WithScalarField_InnerCoupling.
int _number_of_stages(self)
Number of stages of the time integrator.
construct_output(self, exahype2_project, peano_project, setup_simulation, time_step, plot, mesh_adaptation)
Build the task graph of the coupled system, and add it to time_step.
attach_to(self, host)
Install the numerical schemes of the building blocks on the solvers of the host.
_end_of_stage(self, solver, graph, stage)
The tasks of a solver's graph that end a stage.
_create_scheme(self, solver)
The numerical scheme of one of the solvers, i.e.
_first_tasks(graph, previous_end)
The tasks of a solver's graph that start a stage.
_stage_input(self, solver, stage)
The patch which a solver's stage reads as input (it holds the solver's unknowns of the stage's time l...
dict _schemes
Install the numerical schemes of the building blocks on the solvers of the host.
_host
Install the numerical schemes of the building blocks on the solvers of the host.
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