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 of the fourth-order Finite Differences with Runge-Kutta time
20 stepping (@ref exahype2.api.rkfd4): sets the initial condition over a
21 regular block-structured mesh and prepares the data required for the
22 first time step.
23
24 The mesh is not refined adaptively: setup constructs the regular grid
25 the simulation is going to stay on, fills in the initial condition and
26 projects it onto the faces, where the first Runge-Kutta stage picks it
27 up to reconstruct the halo of each patch.
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 The class is a copy of exahype2.api.fv.SetupSimulationOverRegularGrid,
35 as rkfd4 does not depend on the Finite Volume package. It works against
36 the data the numerical scheme assigns to the solver: solver._patch,
37 solver._patch_overlap_update and solver._patch_overlap_current_time_step,
38 plus the numerical scheme's patch_size.
39
40 """
41
42 def __init__(self, solver: Solver, volume_size: float):
43 """!
44
45 @param volume_size: float
46 Maximum edge length of one Finite Differences grid cell, i.e. the
47 maximum mesh width h of the scheme. Every mesh cell hosts one
48 patch of patch_size grid cells per axis. The setup refines every
49 mesh cell whose edge length is greater than or equal to
50 volume_size times the patch size (@ref
51 peano4.api.actionsets.ConstructRegularGrid), i.e. the resulting
52 grid cells are strictly smaller than volume_size. If you want grid
53 cells of exactly 1/27 with patches of size 9, pass in a value
54 slightly bigger than 1/27: 1/27 itself yields mesh cells of size
55 1/9 and hence grid cells of size 1/81.
56
57 """
58 super().__init__(solver)
59 self.volume_size = volume_size
60
61 def construct_output(self, exahype2_project, peano_project, setup_simulation):
62 """!
63
64 Create the actual sequence of tasks following the description
65 from @ref page_exahype_numerical_scheme "Adding a numerical scheme", as grid construction,
66 numerical scheme, but also plotting all follow the same pattern
67 of tasks and dependencies.
68
69 """
71 "Create setup task graph for fourth-order Finite Differences with Runge-Kutta over a regular, block-structured mesh"
72 )
73
74 assert any(
75 isinstance(term, InitPointWisely) for term in self._solver.pde_terms
76 ), (
77 f"Solver {self._solver.name} has no InitPointWisely PDE term registered. "
78 "SetupSimulationOverRegularGrid needs one to fill in the initial condition - "
79 "add one via solver.add_pde_term(exahype2.api.InitPointWisely(...))."
80 )
81
82 # Repeat while the mesh still changes, while the load balancing still
83 # wants to split, or while a split is still being carried out. Splits
84 # within these sweeps are safe, as the sweeps do not hold any data
85 # that lives from one sweep to the next.
86 refine_task = peano4.api.taskgraph.Task(
87 name=f"{self._solver.name}_Refine mesh",
88 entity=peano4.api.taskgraph.Task.GridEntity.CELL,
89 task_has_to_repeat=(
90 "not peano4::grid::isGridStationary( peano4::parallel::SpacetreeSet::getInstance().getGridStatistics() )"
91 " or ( ::toolbox::loadbalancing::LoadBalancingService::getInstance().hasLoadBalancing()"
92 " and ( not ::toolbox::loadbalancing::LoadBalancingService::getInstance().getLoadBalancing()->hasStagnated()"
93 " or ::toolbox::loadbalancing::LoadBalancingService::getInstance().getLoadBalancing()->hasSplitRecently() ) )"
94 ),
95 )
96 consolidate_load_balancing_task = peano4.api.taskgraph.Task(
97 name=f"{self._solver.name}_Consolidate mesh",
98 entity=peano4.api.taskgraph.Task.GridEntity.CELL,
99 task_has_to_repeat=(
100 "not peano4::grid::isGridStationary( peano4::parallel::SpacetreeSet::getInstance().getGridStatistics() )"
101 " or ( ::toolbox::loadbalancing::LoadBalancingService::getInstance().hasLoadBalancing()"
102 " and ::toolbox::loadbalancing::LoadBalancingService::getInstance().getLoadBalancing()->hasSplitRecently() )"
103 ),
104 )
105
106 assert hasattr(self._solver.numerical_scheme, "patch_size"), (
107 f"Solver {self._solver.name}'s numerical scheme has no attribute "
108 "patch_size. SetupSimulationOverRegularGrid assumes the numerical "
109 "scheme has added this field to itself immediately upon construction."
110 )
111 # Load balancing is expensive. Once the load balancing has split,
112 # we therefore do not add any further mesh elements for a while, so
113 # the mesh and the new trees can catch up: if we kept on refining,
114 # the freshly split trees would immediately become unbalanced again,
115 # and the load balancing would have to split once more. The
116 # refinement hence only continues if no load balancing is installed
117 # or if the installed load balancing has not split recently.
118 refine_task.add_action_set(
120 self.volume_size * self._solver.numerical_scheme.patch_size,
121 guard=(
122 "not ::toolbox::loadbalancing::LoadBalancingService::getInstance().hasLoadBalancing()"
123 " or not ::toolbox::loadbalancing::LoadBalancingService::getInstance().getLoadBalancing()->hasSplitRecently()"
124 ),
125 includes="""
126#include "toolbox/loadbalancing/AbstractLoadBalancing.h"
127#include "toolbox/loadbalancing/LoadBalancingService.h"
128""",
129 )
130 )
131 # Inform the load balancing after every mesh refinement sweep
132 refine_task.add_action_set(toolbox.loadbalancing.api.actionsets.LoadBalancing())
133 # Once the mesh is constructed, switch the load balancing off and let
134 # the mesh consolidate: we keep on running mesh sweeps until the
135 # mesh is stationary and the most recent splits have been carried out.
136 consolidate_load_balancing_task.add_action_set(
137 toolbox.loadbalancing.api.actionsets.ToggleLoadBalancing(
138 enable_load_balancing=False, plug_into_prepare_traversal=True
139 )
140 )
141 consolidate_load_balancing_task.add_action_set(
142 toolbox.loadbalancing.api.actionsets.LoadBalancing()
143 )
144
145 initial_condition_task = peano4.api.taskgraph.Task(
146 name=f"{self._solver.name} Initial condition",
147 entity=peano4.api.taskgraph.Task.GridEntity.CELL,
148 )
149 project_on_faces_task = peano4.api.taskgraph.Task(
150 name=f"{self._solver.name} Project on faces",
151 entity=peano4.api.taskgraph.Task.GridEntity.CELL,
152 )
153
154 consolidate_load_balancing_task.add_dependency(refine_task)
155 initial_condition_task.add_dependency(consolidate_load_balancing_task)
156 project_on_faces_task.add_dependency(initial_condition_task)
157
158 initial_condition_task.add_action_set(
159 InitialCondition(
160 self._solver, guard=self._solver.grid_selector.createCellGuard()
161 )
162 )
163 project_on_faces_task.add_action_set(
164 ProjectPatchOntoFaces(
165 self._solver._patch,
166 self._solver._patch_overlap_update,
167 guard=self._solver.grid_selector.createCellGuard(),
168 includes=self._solver._get_default_includes()
169 + self._solver.user_solver_includes,
170 )
171 )
172 # Sommerfeld boundary conditions initialise the ghost grid cells with
173 # the initial condition (@ref exahype2.api.blockstructured.BoundaryConditions)
174 project_on_faces_task.add_action_set(
175 RollOverUpdatedFaceWithSommerfeldBoundaryConditions.create_roll_over(
176 self._solver,
177 exahype2_project,
178 guard=self._solver.grid_selector.createFaceGuard(),
179 includes=self._solver._get_default_includes()
180 + self._solver.user_solver_includes,
181 )
182 )
183
184 add_auxiliary_variables_to_setup(
185 self._solver, initial_condition_task, project_on_faces_task, ProjectPatchOntoFaces, RollOverUpdatedFace
186 )
187
188 setup_simulation.add_task(refine_task)
189 setup_simulation.add_task(consolidate_load_balancing_task)
190 setup_simulation.add_task(initial_condition_task)
191 setup_simulation.add_task(project_on_faces_task)
192
193 initial_condition_task.use_cell(
194 self._solver._patch, peano4.api.taskgraph.Task.DataAccess.WRITE
195 )
196 project_on_faces_task.use_cell(
197 self._solver._patch, peano4.api.taskgraph.Task.DataAccess.READ_AND_WRITE
198 )
199 project_on_faces_task.use_face(
200 self._solver._patch_overlap_update,
201 peano4.api.taskgraph.Task.DataAccess.TEMPORARY,
202 )
203 project_on_faces_task.use_face(
204 self._solver._patch_overlap_current_time_step,
205 peano4.api.taskgraph.Task.DataAccess.WRITE,
206 )
207
208 if peano4.api.Logger().is_visualisation:
209 setup_simulation.visualise(
210 title="Setup task graph for {}".format(self._solver.name)
211 )
212 if peano4.api.Logger().is_text:
213 setup_simulation.log_as_text(
214 title="Setup task graph for {}".format(self._solver.name)
215 )
How a Solver drives the construction of the mesh it runs on.
Definition Solver.py:242
Setup phase of the fourth-order Finite Differences with Runge-Kutta time stepping (exahype2....
construct_output(self, exahype2_project, peano_project, setup_simulation)
Create the actual sequence of tasks following the description from Adding a numerical scheme,...
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