Peano
Loading...
Searching...
No Matches
FOSOSolver.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
5from typing import Dict, List, Union
6
7import exahype2.api
8import peano4.api
10
11from .FOSOCCZ4Solver import FOSOCCZ4Solver
12from .FOSOReconstructionSolver import FOSOReconstructionSolver
13
14
15class _HostedScheme(exahype2.api.Solver.NumericalScheme):
16 """!
17
18 The numerical scheme of the reconstruction solver
19
20 The solver has no scheme of its own, but the generic parts of the solver
21 still query one for patch size, halo and so forth. The hosted scheme
22 forwards all such queries to the scheme of the CCZ4 solver. Its
23 construct_output() asks the reconstruction solver for its task graph once
24 per Runge-Kutta stage of CCZ4
25 (@ref exahype2.api.Solver.Solver.create_task_graph()) and puts the graphs
26 into the time stepping graph. The graphs are not connected: wiring them is
27 the job of FOSOSolver._couple(). The scheme remembers the graphs in
28 stage_graphs.
29
30 """
31
32 def __init__(self, solver, ccz4):
33 super().__init__(solver)
34 self._ccz4 = ccz4
36 """! The graph that the solver returned for each stage, filled by construct_output() """
37
38 def __getattr__(self, name):
39 # Only called for attributes which are not found otherwise
40 if name.startswith("_"):
41 raise AttributeError(name)
42 return getattr(self._ccz4.numerical_scheme, name)
43
45 """! FOSOSolver wraps the concatenated graph through the scheme of CCZ4"""
46
48 self, exahype2_project, peano_project, time_step, add_global_tasks=True
49 ):
50 solver = self._solver
51 stages = getattr(self._ccz4.numerical_scheme, "rk_order", 1)
52 self.stage_graphs = []
53 for stage in range(stages):
54 graph = solver.create_task_graph(
55 exahype2_project, peano_project, stage, stages
56 )
57 time_step.merge(graph)
58 self.stage_graphs.append(graph)
59 title = f"Time stepping task graph for {solver.name}"
60 if peano4.api.Logger().is_visualisation:
61 time_step.visualise(title=title)
62 if peano4.api.Logger().is_text:
63 time_step.log_as_text(title=title)
64
65
67 """!
68
69 CCZ4 in the first-/second-order reconstructed formulation as a coupled pair
70
71 The object hosts two solvers, which take turns in the time stepping task
72 graph (@ref page_exagrype_fo_so_ccz4, @ref page_exahype_programmers_guide_coupling):
73
74 - ccz4 (exagrype.api.FOSOCCZ4Solver) evolves the 25 primary unknowns of
75 CCZ4. It holds the 33 derivatives as auxiliary variables. It is the
76 the one which owns the global tasks: Both solvers are
77 realised as intermediate steps, and after concatenation and coupling, the
78 host wraps the whole graph into the load balancing and dynamic mesh
79 refinement tasks through the scheme of ccz4
80 (wrap_task_graph_in_load_balancing_and_amr_tasks()).
81 - reconstruction (exagrype.api.FOSOReconstructionSolver) evolves the
82 derivatives, i.e. it recomputes them from the primary unknowns in
83 front of each Runge-Kutta stage of ccz4.
84
85 The host holds the numerical scheme: Build it for ccz4 and assign it to
86 the host. The reconstruction solver receives a scheme that forwards to it. Configure ccz4 otherwise as any other solver (grid
87 construction, plotter, constraint enforcement, ...), and then add the
88 host to the project with exahype2.api.Project.add_solver(). The
89 reconstruction needs no configuration: it takes patch size and halo from
90 the scheme.
91
92 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
93 solver = exagrype.api.FOSOSolver("SO")
94 solver.numerical_scheme = exahype2.api.rkfd4.CentralDifferencesWithRungeKutta(solver.ccz4, patch_size, rk_order=4, ...)
95 ...
96 project.add_solver(solver)
97 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
98
99 ## Task graph
100
101 Both solvers build a task graph of their own, and report it: the scheme of the host
102 builds the graph of ccz4, and asks the reconstruction solver for its graph
103 once per stage (exagrype.api.FOSOReconstructionSolver.create_task_graph()). The reconstruction
104 solver's graph is a chain per stage (see @ref exagrype.api.FOSOReconstructionSolver),
105 and the chains of different stages are not connected.
106 The host concatenates the graphs and wires them (_couple()): The first tasks of
107 each stage's chain take over the dependencies of the CCZ4 solver's reconstruction
108 of this stage, and the latter depends on the last task of the chain. The host
109 reports the outcome.
110
111 """
112
114 self,
115 name: str,
116 parameters: Dict[str, Union[int, float]] = None,
117 initial_condition: exahype2.api.InitPointWisely = None,
118 reconstruction_name: str = None,
119 ):
120 """!
121
122 @param name: str
123 Name of the CCZ4 solver, @see exahype2.api.Solver.
124
125 @param parameters: Dict[str, Union[int, float]]
126 CCZ4 parameters, @see exagrype.api.FOSOCCZ4Solver.
127
128 @param initial_condition: exahype2.api.InitPointWisely
129 Initial condition, @see exagrype.api.FOSOCCZ4Solver.
130
131 @param reconstruction_name: str
132 Name of the reconstruction solver. Defaults to name plus
133 "Reconstruction".
134
135 """
136 self.ccz4 = FOSOCCZ4Solver(name, parameters, initial_condition)
137 """! The solver that evolves the primary unknowns """
139 reconstruction_name
140 if reconstruction_name is not None
141 else name + "Reconstruction",
142 self.ccz4,
143 )
144 """! The solver that reconstructs the derivatives """
145 self._name = name
147
148 @property
149 def name(self) -> str:
150 return self._name
151
152 @property
153 def solvers(self):
154 """! The solvers in the order of their realisation: CCZ4 first, as the reconstruction needs its data """
155 return [self.ccz4, self.reconstruction]
156
157 @property
158 def numerical_scheme(self) -> exahype2.api.Solver.NumericalScheme:
159 """! The scheme of ccz4. Assigning it hands a forwarding scheme to the reconstruction solver """
160 return self._numerical_scheme
161
162 @numerical_scheme.setter
163 def numerical_scheme(self, value: exahype2.api.Solver.NumericalScheme):
164 self.ccz4.numerical_scheme = value
165 self._numerical_scheme = value
166 self.reconstruction.numerical_scheme = _HostedScheme(
167 self.reconstruction, self.ccz4
168 )
169
171 self,
172 exahype2_project,
173 peano_project,
174 setup_simulation,
175 time_step,
176 plot,
177 mesh_adaptation,
178 ):
179 """!
180
181 Realise both solvers, couple their time stepping graphs, and add the outcome to the project's graphs
182
183 The arguments are those of exahype2.api.Solver.generate_solver_realisation().
184 Both solvers are realised without global tasks. The load balancing and the dynamic
185 mesh refinement wrap the coupled time step graph afterwards, through the scheme of CCZ4.
186
187 """
188 peano4.api.Logger().new_section("Host " + self.name)
189 if self._numerical_scheme is None:
190 raise ValueError(
191 f"Host {self.name} has no numerical_scheme. Assign one to the host."
192 )
193 coupled_time_step = peano4.api.taskgraph.Graph()
194 for solver in self.solvers:
195 own_time_step = peano4.api.taskgraph.Graph()
196 solver.generate_solver_realisation(
197 exahype2_project,
198 peano_project,
199 setup_simulation,
200 own_time_step,
201 plot,
202 mesh_adaptation,
203 add_global_tasks=False,
204 )
205 coupled_time_step.merge(own_time_step)
206 self._couple(exahype2_project, peano_project, coupled_time_step)
207
208 self.ccz4.numerical_scheme.wrap_task_graph_in_load_balancing_and_amr_tasks(
209 coupled_time_step
210 )
211 if self.ccz4.mesh_adaptation is not None:
212 self.ccz4.mesh_adaptation.construct_output(
213 exahype2_project, peano_project, mesh_adaptation
214 )
215
216 title = f"Time stepping task graph for host {self.name}"
217 if peano4.api.Logger().is_visualisation:
218 coupled_time_step.visualise(title=title)
219 if peano4.api.Logger().is_text:
220 coupled_time_step.log_as_text(title=title)
221 time_step.merge(coupled_time_step)
222
223 def _couple(self, exahype2_project, peano_project, time_step):
224 """!
225
226 Let each stage of the CCZ4 solver use the freshly reconstructed derivatives
227
228 In each stage, the CCZ4 solver interpolates the unknowns and the
229 derivatives onto the hanging faces, reconstructs their halos, and
230 evaluates the right-hand side. The reconstruction solver recomputes
231 the derivatives from the unknowns in between, i.e.
232
233 - the first reconstruction tasks of the stage wait for whatever the
234 CCZ4 solver's interpolation of the derivatives waits for,
235 - the interpolation of the primary variables also waits for the
236 CCZ4 solver's interpolation of the unknowns, as both write the same
237 faces, and
238 - the CCZ4 solver's interpolation of the derivatives and its
239 reconstruction of the unknowns wait for the last reconstruction task.
240
241 The chain is hence a block within the stage: The maximum concurrency of
242 the merged graph does not exceed the one of the two graphs.
243
244 @param exahype2_project: exahype2.api.Project
245 The project that hosts the pair (unused).
246
247 @param peano_project: peano4.api.Project
248 The Peano project that the exahype2 project assembles (unused).
249
250 @param time_step: peano4.api.taskgraph.Graph
251 The concatenated time stepping graphs of both solvers. It receives the dependencies.
252
253 """
254
255 def find(task_name, stage):
256 found = [t for t in time_step.tasks if t.name == task_name]
257 if len(found) != 1:
258 raise ValueError(
259 f"Host {self.name}: cannot find the task {task_name} "
260 f"of stage {stage} of solver {self.ccz4.name} in its time stepping task graph"
261 )
262 return found[0]
263
264
265 for stage, graph in enumerate(
266 self.reconstruction.numerical_scheme.stage_graphs
267 ):
268 first_tasks = graph.source_tasks()
269 (last_task,) = graph.sink_tasks()
270 (
271 unknowns_name,
272 derivatives_name,
273 reconstruction_name,
274 ) = self.reconstruction.partner_task_names(stage)
275 unknowns_interpolation = find(unknowns_name, stage)
276 derivatives_interpolation = find(derivatives_name, stage)
277 for dependency in derivatives_interpolation.dependencies:
278 for first_task in first_tasks:
279 first_task.add_dependency(dependency)
280 first_tasks[0].add_dependency(unknowns_interpolation)
281 derivatives_interpolation.add_dependency(last_task)
282 find(reconstruction_name, stage).add_dependency(last_task)
Solver for the Einstein equations in the first-/second-order reconstructed CCZ4 formulation.
Reconstructs the first derivatives which exagrype.api.FOSOCCZ4Solver needs.
CCZ4 in the first-/second-order reconstructed formulation as a coupled pair.
Definition FOSOSolver.py:66
exahype2.api.Solver.NumericalScheme numerical_scheme(self)
The scheme of ccz4.
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.
__init__(self, str name, Dict[str, Union[int, float]] parameters=None, exahype2.api.InitPointWisely initial_condition=None, str reconstruction_name=None)
_couple(self, exahype2_project, peano_project, time_step)
Let each stage of the CCZ4 solver use the freshly reconstructed derivatives.
The numerical scheme of the reconstruction solver.
Definition FOSOSolver.py:15
wrap_task_graph_in_load_balancing_and_amr_tasks(self, time_step)
FOSOSolver wraps the concatenated graph through the scheme of CCZ4.
Definition FOSOSolver.py:44
construct_output(self, exahype2_project, peano_project, time_step, add_global_tasks=True)
Definition FOSOSolver.py:49
Point-wise initial condition: given the physical position of a single degree of freedom,...
Singleton logger for the Peano 4 Python API.
Definition Logger.py:37
Holds a sequence of Task instances.
Definition Graph.py:9
The task graph abstraction allows users to formalise their solver steps over small task graphs which ...
Definition __init__.py:1