Peano
Loading...
Searching...
No Matches
WriteCheckpoint.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 os
4
5import peano4.api
7
8from ..Solver import Solver
9from .PlotAsVTKFile import _PlotPatchesIfPlotterIsActive
10from .actionsets import ApplyDirichletBoundaryConditions
11
12
14 """!
15
16 Write checkpoints of the fourth-order Finite Differences
17
18 The checkpoint is configured like PlotAsVTKFile, i.e. it is written in
19 regular time intervals. Different to a plot, it writes the whole state
20 of the solver such that a simulation can continue from it. It hence has
21 neither plot filters nor a selection of quantities: It writes all
22 entries of Q, i.e. the unknowns and the auxiliary variables, of all
23 cells which the solver updates.
24
25 - It writes each patch together with its halo. Before it writes the
26 data, it reconstructs the halo exactly as the first Runge-Kutta stage
27 of the time step does: It sets the ghost grid cells along the domain
28 boundary (Dirichlet boundary conditions), interpolates the hanging
29 faces on adaptive meshes and gathers patch and face data into
30 solver._patch_with_halo
31 (toolbox.blockstructured.api.ReconstructPatchWithHalo). A patch
32 with N grid cells per axis and a halo of M grid cells is written as a
33 patch with N+2M grid cells per axis. Its box covers the cell plus the
34 halo, so every grid cell sits at its true position, and neighbouring
35 patches overlap.
36 - It writes all values without loss: values, patch offsets and sizes
37 carry 17 significant digits by default, and the writer does not set
38 small values to zero
39 (@ref tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter).
40 - The meta-data block in the header of each file holds the solver's
41 state (@ref exahype2::Solver::toCheckpointMetaData()): its time
42 stamps, time step sizes, admissible time step size and mesh sizes,
43 plus the attributes the Python API adds to the solver's class, e.g.
44 the time stamps of the next outputs of all of its plotters.
45
46 The files are called checkpoint-SolverName by default. A solver can
47 have a checkpoint next to its other plotters
48 (@ref exahype2.api.Solver.Solver.add_plotter()).
49 A simulation continues from a checkpoint with
50 @ref exahype2.api.blockstructured.LoadCheckpoint.LoadCheckpoint instead of
51 its usual grid construction.
52
53 The reconstruction only reads the patch and face data, i.e. a
54 checkpoint does not change the solution. The Dirichlet boundary
55 conditions overwrite the ghost grid cells with the values they have at
56 the current time stamp anyway, and the next time step sets them again.
57
58 The class is the counterpart of exahype2.api.fv.WriteCheckpoint, as rkfd4
59 does not depend on the Finite Volume package.
60
61 """
62
64 self,
65 solver: Solver,
66 first_plot_time_stamp=0.0,
67 time_in_between_plots=0.1,
68 plotter_precision=17,
69 output_path=".",
70 filename="checkpoint",
71 name: str = None,
72 binary=True,
73 ):
74 """!
75
76 @param solver: exahype2.api.Solver
77 The solver whose state is written.
78
79 @param first_plot_time_stamp: float
80 Time stamp of the first checkpoint.
81
82 @param time_in_between_plots: float
83 Time in between two checkpoints. Has to be positive.
84
85 @param plotter_precision: int
86 Number of significant digits written per value. The default of 17
87 ensures that the values are read back without loss.
88
89 @param output_path: str
90 Directory the checkpoint files are written into.
91
92 @param filename: str
93 The checkpoint files are called filename-SolverName.
94
95 @param name: str or None
96 Name of the plotter (@ref exahype2.api.Solver.Solver.Plotter).
97
98 @param binary: bool
99 Write the values as raw doubles instead of text
100 (@ref tarch::plotter::griddata::blockstructured::PeanoBinaryPatchFileWriter).
101 The checkpoint reader (@ref exahype2::CheckpointReader) reads both
102 formats. Pass False to get human-readable checkpoints.
103
104 """
105 super().__init__(solver, name)
106
107 if time_in_between_plots <= 0:
108 raise Exception("Time in between checkpoints has to be bigger than 0")
109 if plotter_precision <= 0:
110 raise Exception("Precision has to be bigger than 0")
111 self._first_plot_time_stamp = first_plot_time_stamp
112 self._time_in_between_plots = time_in_between_plots
113 self._plotter_precision = plotter_precision
114 self._output_path = output_path
115 self._filename = filename
116 self._binary = binary
117
119 return os.path.join(self._output_path, self._filename + "-" + self._solver_solver.name)
120
121 def construct_output(self, exahype2_project, peano_project, plot):
122 """!
123
124 Add the reconstruction of the halo and the writing of the checkpoint
125 to the plot graph
126
127 All action sets are guarded by the checkpoint's is<Name>Active(),
128 i.e. the tasks do no work in plot phases without checkpoint. The
129 writing task is the last one, so it moves the checkpoint on to its
130 next time stamp.
131
132 """
133 peano4.api.Logger().log(
134 "Create checkpoint task graph for fourth-order Finite Differences with Runge-Kutta over block-structured meshes"
135 )
136
137 for attribute in [
138 "_patch",
139 "_patch_with_halo",
140 "_patch_overlap_current_time_step",
141 ]:
142 assert hasattr(self._solver_solver, attribute), (
143 f"Solver {self._solver.name} has no attribute {attribute}. "
144 "WriteCheckpoint assumes the numerical scheme has added this field "
145 "to the solver immediately upon construction."
146 )
147
150 )
151 self._solver_solver._add_checkpoint_meta_data_to_solver()
152
153 Access = peano4.api.taskgraph.Task.DataAccess
154 is_active = self.create_is_active_predicate()
155 cell_guard = (
156 "("
157 + is_active
158 + ") and ("
159 + self._solver_solver.grid_selector.createCellGuard()
160 + ")"
161 )
162 includes = (
163 self._solver_solver._get_default_includes() + self._solver_solver.user_solver_includes
164 )
165
166 reconstruction_task = peano4.api.taskgraph.Task(
167 name=f"{self._solver.name}_{self.name}_Reconstruct",
168 entity=peano4.api.taskgraph.Task.GridEntity.CELL,
169 may_spawn_background_tasks=False,
170 )
171 write_checkpoint_task = peano4.api.taskgraph.Task(
172 name=f"{self._solver.name}_{self.name}_Write checkpoint",
173 entity=peano4.api.taskgraph.Task.GridEntity.CELL,
174 may_spawn_background_tasks=False,
175 )
176 plot.add_task(reconstruction_task)
177 plot.add_task(write_checkpoint_task)
178
179
180 reconstruction_task.use_cell(self._solver_solver._patch, Access.READ_AND_WRITE)
181 reconstruction_task.use_cell(self._solver_solver._patch_with_halo, Access.WRITE)
182 reconstruction_task.use_face(
183 self._solver_solver._patch_overlap_current_time_step, Access.READ_AND_WRITE
184 )
185 reconstruction_task.add_action_set(
186 toolbox.blockstructured.api.ReconstructPatchWithHalo(
187 patch=self._solver_solver._patch,
188 patch_overlap=self._solver_solver._patch_overlap_current_time_step,
189 reconstructed_patch=self._solver_solver._patch_with_halo,
190 guard=cell_guard,
191 add_assertions_to_halo_exchange=False,
192 additional_includes=includes,
193 )
194 )
195
197 reconstruction_task.add_action_set(
198 toolbox.blockstructured.api.InterpolateHangingFaces(
199 self._solver_solver._patch_overlap_current_time_step,
200 scheme=self._solver_solver.numerical_scheme.interpolation,
201 guard=is_active,
202 additional_includes=includes,
203 )
204 )
205
207 ApplyDirichletBoundaryConditions.add_task_to_time_step(
208 self._solver_solver,
209 exahype2_project,
210 plot,
211 reconstruction_task,
212 name_suffix=f" for {self.name}",
213 relative_time_step_size=0.0,
214 additional_guard=is_active,
215 )
216
217
218 write_checkpoint_task.add_dependency(reconstruction_task)
219 write_checkpoint_task.use_cell(
220 self._solver_solver._patch_with_halo, Access.READ_AND_DISCARD
221 )
222 write_checkpoint_task.add_action_set(
224 is_active_predicate=is_active,
225 finish_call=self.create_finish_call(),
227 patch=self._solver_solver._patch_with_halo,
228 dataset_name=self._solver_solver._patch_with_halo.name,
229 description=", ".join(self._solver_solver.variable_names_per_entry())
230 or self._solver_solver._unknown_identifier(),
231 guard=self._solver_solver.grid_selector.createCellGuard(),
232 additional_includes="""
233#include "../repositories/SolverRepository.h"
234""",
235 precision=self._plotter_precision,
236 binary=self._binary,
237 time_stamp_evaluation="0.5*(repositories::getMinTimeStamp()+repositories::getMaxTimeStamp())",
238 patch_scaling=self._solver_solver._patch_with_halo.dim[0]
239 / self._solver_solver._patch.dim[0],
240 metadata_evaluation="repositories::"
241 + self._solver_solver.get_name_of_global_instance()
242 + ".toCheckpointMetaData()",
243 )
244 )
245
246 if peano4.api.Logger().is_visualisation:
247 plot.visualise(title="Plot task graph for {}".format(self._solver_solver.name))
248 if peano4.api.Logger().is_text:
249 plot.log_as_text(title="Plot task graph for {}".format(self._solver_solver.name))
How a Solver writes out its solution.
Definition Solver.py:124
str output_file_prefix(self)
Prefix of the files the plotter writes, including the path.
Definition Solver.py:199
_add_time_interval_bookkeeping_to_solver(self, float first_time_stamp, float time_in_between)
Make the plotter active in regular time intervals.
Definition Solver.py:245
str create_is_active_predicate(self)
C++ predicate which holds if the plotter is active.
Definition Solver.py:210
str create_finish_call(self)
C++ statement which moves the plotter on to its next output.
Definition Solver.py:226
PlotPatchesInPeanoBlockFormat which only does something if its plotter is active.
Write checkpoints of the fourth-order Finite Differences.
output_file_prefix(self)
Prefix of the files the plotter writes, including the path.
_solver
Set the ghost grid cells along the domain boundary at the current time stamp, i.e.
construct_output(self, exahype2_project, peano_project, plot)
Add the reconstruction of the halo and the writing of the checkpoint to the plot graph.
__init__(self, Solver solver, first_plot_time_stamp=0.0, time_in_between_plots=0.1, plotter_precision=17, output_path=".", filename="checkpoint", str name=None, binary=True)
Singleton logger for the Peano 4 Python API.
Definition Logger.py:37
One task within a peano4.api.taskgraph.Graph.
Definition Task.py:8