Peano
Loading...
Searching...
No Matches
SetupSimulationOverRegularGrid.py
Go to the documentation of this file.
1# This file is part of the ExaHyPE2 project. For conditions of distribution and
2# use, please see the copyright notice at www.peano-framework.org
3import peano4.api
5import toolbox.loadbalancing.api.actionsets
6
7from ..InitPointWisely import InitPointWisely
8from ..Solver import Solver
9from ..AuxiliaryVariables import add_auxiliary_variables_to_setup
10from .actionsets import InitialCondition
11from .actionsets import ProjectPatchOntoFaces
12from .actionsets import RollOverUpdatedFace
13from .actionsets import RollOverUpdatedFaceWithSommerfeldBoundaryConditions
14
15
17 """!
18
19 Setup phase counterpart to RusanovWithExplicitEuler: sets the initial
20 condition for a Finite Volume scheme with a Rusanov Riemann solver,
21 realised over a block-structured mesh, and prepares the data required
22 for the first time step.
23
24 Unlike SetupSimulationWithUserCodedRefinementCriterion, this variant
25 does not refine the mesh at all: it assumes the mesh is already the
26 regular grid it is going to stay, so setup only has to fill in the
27 initial condition and project it onto the faces.
28
29 The solver this is attached to has to have an exahype2.api.InitPointWisely
30 PDE term registered (via solver.add_pde_term(...)), as that is what fills
31 in the initial condition this class' setup task graph relies on;
32 construct_output() asserts this before it builds anything.
33
34 """
35
36
48 FinishedInitialRegularMeshSetup = "exahype2.api.fv.SetupSimulationOverRegularGrid.FinishedInitialRegularMeshSetup"
49
50 def __init__(self, solver: Solver, volume_size: float):
51 """!
52
53 @param volume_size: float
54 Maximum edge length of one Finite Volume. Every mesh cell hosts one
55 patch of patch_size volumes per axis. The setup refines every cell
56 whose edge length is greater than or equal to volume_size times the
57 patch size (@ref peano4.api.actionsets.ConstructRegularGrid), i.e.
58 the resulting volumes are strictly smaller than volume_size. If you
59 want volumes of exactly 1/27 with patches of size 9, pass in a value
60 slightly bigger than 1/27: 1/27 itself yields cells of size 1/9 and
61 hence volumes of size 1/81.
62
63 """
64 super().__init__(solver)
65 self.volume_size = volume_size
66
67
68
70 exahype2_project,
71 peano_project,
72 setup_simulation):
73 """!
74
75 Create the actual sequence of tasks following the description
76 from @ref page_exahype_numerical_scheme "Adding a numerical scheme", as grid construction,
77 numerical scheme, but also plotting all follow the same pattern
78 of tasks and dependencies.
79
80 The routine first builds the task graph through
81 _construct_task_graph() and then reports it through
82 _report_task_graph().
83
84 """
85 self._construct_task_graph(exahype2_project, peano_project, setup_simulation)
86 self._report_task_graph(setup_simulation)
87
89 exahype2_project,
90 peano_project,
91 setup_simulation):
92 """!
93
94 Add the tasks of the setup over a regular mesh to setup_simulation
95
96 The initial condition task carries the tag
97 FinishedInitialRegularMeshSetup.
98
99 """
100 peano4.api.Logger().log("Create setup task graph for Finite Volume scheme over a regular, block-structured mesh with Rusanov flux")
101
102 assert any(isinstance(term, InitPointWisely) for term in self._solver.pde_terms), (
103 f"Solver {self._solver.name} has no InitPointWisely PDE term registered. "
104 "SetupSimulationOverRegularGrid needs one to fill in the initial condition - "
105 "add one via solver.add_pde_term(exahype2.api.InitPointWisely(...))."
106 )
107
108 # Repeat while the mesh still changes, while the load balancing still
109 # wants to split, or while a split is still being carried out. Splits
110 # within these sweeps are safe, as the sweeps do not hold any data
111 # that lives from one sweep to the next.
112 refine_task = peano4.api.taskgraph.Task(name=f"{self._solver.name}_Refine mesh",
113 entity=peano4.api.taskgraph.Task.GridEntity.CELL,
114 task_has_to_repeat=(
115 "not peano4::grid::isGridStationary( peano4::parallel::SpacetreeSet::getInstance().getGridStatistics() )"
116 " or ( ::toolbox::loadbalancing::LoadBalancingService::getInstance().hasLoadBalancing()"
117 " and ( not ::toolbox::loadbalancing::LoadBalancingService::getInstance().getLoadBalancing()->hasStagnated()"
118 " or ::toolbox::loadbalancing::LoadBalancingService::getInstance().getLoadBalancing()->hasSplitRecently() ) )"
119 )
120 )
121 consolidate_load_balancing_task = peano4.api.taskgraph.Task(name=f"{self._solver.name}_Consolidate mesh",
122 entity=peano4.api.taskgraph.Task.GridEntity.CELL,
123 task_has_to_repeat=(
124 "not peano4::grid::isGridStationary( peano4::parallel::SpacetreeSet::getInstance().getGridStatistics() )"
125 " or ( ::toolbox::loadbalancing::LoadBalancingService::getInstance().hasLoadBalancing()"
126 " and ::toolbox::loadbalancing::LoadBalancingService::getInstance().getLoadBalancing()->hasSplitRecently() )"
127 )
128 )
129
130 assert hasattr(self._solver.numerical_scheme, "patch_size"), (
131 f"Solver {self._solver.name}'s numerical scheme has no attribute "
132 "patch_size. SetupSimulationOverRegularGrid assumes the numerical "
133 "scheme has added this field to itself immediately upon construction."
134 )
135 # Load balancing is expensive. Once the load balancing has split,
136 # we therefore do not add any further mesh elements for a while, so
137 # the mesh and the new trees can catch up: if we kept on refining,
138 # the freshly split trees would immediately become unbalanced again,
139 # and the load balancing would have to split once more. The
140 # refinement hence only continues if no load balancing is installed
141 # or if the installed load balancing has not split recently.
142 refine_task.add_action_set(
144 self.volume_size * self._solver.numerical_scheme.patch_size,
145 guard=(
146 "not ::toolbox::loadbalancing::LoadBalancingService::getInstance().hasLoadBalancing()"
147 " or not ::toolbox::loadbalancing::LoadBalancingService::getInstance().getLoadBalancing()->hasSplitRecently()"
148 ),
149 includes="""
150#include "toolbox/loadbalancing/AbstractLoadBalancing.h"
151#include "toolbox/loadbalancing/LoadBalancingService.h"
152""",
153 )
154 )
155 # Inform the load balancing after every mesh refinement sweep
156 refine_task.add_action_set(toolbox.loadbalancing.api.actionsets.LoadBalancing())
157 # Once the mesh is constructed, switch the load balancing off and let
158 # the mesh consolidate: we keep on running mesh sweeps until the
159 # mesh is stationary and the most recent splits have been carried out.
160 consolidate_load_balancing_task.add_action_set(
161 toolbox.loadbalancing.api.actionsets.ToggleLoadBalancing(
162 enable_load_balancing=False, plug_into_prepare_traversal=True
163 )
164 )
165 consolidate_load_balancing_task.add_action_set(toolbox.loadbalancing.api.actionsets.LoadBalancing())
166
167 initial_condition_task = peano4.api.taskgraph.Task(name=f"{self._solver.name} Initial condition", entity=peano4.api.taskgraph.Task.GridEntity.CELL)
168 initial_condition_task.tags.add(self.FinishedInitialRegularMeshSetup)
169 project_on_faces_task = peano4.api.taskgraph.Task(name=f"{self._solver.name} Project on faces", entity=peano4.api.taskgraph.Task.GridEntity.CELL)
170
171 consolidate_load_balancing_task.add_dependency(refine_task)
172 initial_condition_task.add_dependency(consolidate_load_balancing_task)
173 project_on_faces_task.add_dependency(initial_condition_task)
174
175 initial_condition_task.add_action_set(
176 InitialCondition(self._solver, guard=self._solver.grid_selector.createCellGuard())
177 )
178 project_on_faces_task.add_action_set(
179 ProjectPatchOntoFaces(
180 self._solver._patch,
181 self._solver._patch_overlap_update,
182 guard=self._solver.grid_selector.createCellGuard(),
183 includes=self._solver._get_default_includes() + self._solver.user_solver_includes,
184 )
185 )
186
189 if hasattr(self._solver.numerical_scheme, "create_restriction_of_hanging_faces"):
190 project_on_faces_task.add_action_set(
191 self._solver.numerical_scheme.create_restriction_of_hanging_faces()
192 )
193
195 project_on_faces_task.add_action_set(
196 RollOverUpdatedFaceWithSommerfeldBoundaryConditions.create_roll_over(
197 self._solver,
198 exahype2_project,
199 guard=self._solver.grid_selector.createFaceGuard(),
200 includes=self._solver._get_default_includes() + self._solver.user_solver_includes,
201 )
202 )
203
204 add_auxiliary_variables_to_setup(
205 self._solver, initial_condition_task, project_on_faces_task, ProjectPatchOntoFaces, RollOverUpdatedFace
206 )
207
208 setup_simulation.add_task(refine_task)
209 setup_simulation.add_task(consolidate_load_balancing_task)
210 setup_simulation.add_task(initial_condition_task)
211 setup_simulation.add_task(project_on_faces_task)
212
213 initial_condition_task.use_cell(self._solver._patch, peano4.api.taskgraph.Task.DataAccess.WRITE)
214 project_on_faces_task.use_cell(self._solver._patch, peano4.api.taskgraph.Task.DataAccess.READ_AND_WRITE)
215 project_on_faces_task.use_face(self._solver._patch_overlap_update, peano4.api.taskgraph.Task.DataAccess.TEMPORARY)
216 project_on_faces_task.use_face(self._solver._patch_overlap_current_time_step, peano4.api.taskgraph.Task.DataAccess.WRITE)
217
218 def _report_task_graph(self, setup_simulation):
219 """!
220
221 Visualise or log the setup task graph, depending on the logger's verbosity
222
223 """
224 if peano4.api.Logger().is_visualisation:
225 setup_simulation.visualise(title="Setup task graph for {}".format(self._solver.name))
226 if peano4.api.Logger().is_text:
227 setup_simulation.log_as_text(title="Setup task graph for {}".format(self._solver.name))
How a Solver drives the construction of the mesh it runs on.
Definition Solver.py:242
Setup phase counterpart to RusanovWithExplicitEuler: sets the initial condition for a Finite Volume s...
_construct_task_graph(self, exahype2_project, peano_project, setup_simulation)
Add the tasks of the setup over a regular mesh to setup_simulation.
construct_output(self, exahype2_project, peano_project, setup_simulation)
Create the actual sequence of tasks following the description from Adding a numerical scheme,...
_report_task_graph(self, setup_simulation)
Visualise or log the setup task graph, depending on the logger's verbosity.
str FinishedInitialRegularMeshSetup
Tag of the task which completes the setup over the initial regular mesh.
Singleton logger for the Peano 4 Python API.
Definition Logger.py:37
Refine the mesh - solver-agnostic - down to a regular grid of a given resolution.
One task within a peano4.api.taskgraph.Graph.
Definition Task.py:8
This is a set of generic action sets that multiple Peano users or extensions built on top of Peano mi...
Definition __init__.py:1