Peano
Loading...
Searching...
No Matches
ComputeTimeStepSize.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
3from .AbstractRKFD4ActionSet import AbstractRKFD4ActionSet
4
5import peano4.api
6
7
9 """!
10
11 Determine the time step size of the next Runge-Kutta step and advance
12 the solver's time stamp
13
14 The action set supports two modes:
15
16 - Fixed time step sizes: the next time step size is the one passed to
17 the constructor.
18 - Adaptive time step sizes: exahype2.api.rkfd4.actionsets.ComputeMaxEigenvalue
19 reduces the maximum eigenvalue of every patch into the solver. The
20 action set reads and resets it (getAndResetMaxEigenvalue()), takes the
21 maximum over all ranks, and derives the next time step size from it
22 through the CFL condition
23 @f$ \\Delta t = C h / (d \\lambda _{max}) @f$, with h the minimal grid
24 cell size, d the dimension and C the CFL constant.
25
26 In both modes, unprepareTraversal() feeds the next time step size into
27 ::exahype2::Solver's own time step bookkeeping
28 (updateCell()/finishTimeStep()/startTimeStep()). The time stamp
29 advances by the time step size the Runge-Kutta step has just used. The
30 last time step is not truncated: it may overshoot MaxTerminalTime, and
31 the simulation stops afterwards, i.e. MaxTerminalTime is a lower bound
32 on the final time. The solver starts with a time step size of zero, so
33 the very first Runge-Kutta step does not alter the solution: it only
34 yields the first time step size.
35
36 Global time stepping: all cells share one time stamp and time step size.
37 As getMinTimeStamp()==getMaxTimeStamp() and
38 getMinTimeStepSize()==getMaxTimeStepSize() always hold, a single
39 updateCell() call per time step is enough.
40
41 The action set only acts in unprepareTraversal() and touches no mesh data.
42 The task that hosts it hence is a GLOBAL_END_OF_STEP task
43 (@ref peano4.api.taskgraph.Task.Task.GridEntity), which the task graph
44 compiler fuses into the last traversal of the time step instead of
45 spending a traversal on it. Why the reduction over the cells happens
46 elsewhere: a task that touches cells could not be of this kind.
47
48 The class is derived from exahype2.api.fv.actionsets.ComputeGlobalCFLCondition,
49 as rkfd4 does not depend on the Finite Volume package. Consult the
50 latter for details on the reduction over ranks.
51
52 """
53
54 def __init__(self, solver, patch_size, cfl_constant=None, time_step_size=None):
55 """!
56
57 @param solver: exahype2.api.Solver
58 With adaptive time stepping, the solver has to host the reduction of
59 the maximum eigenvalue (@ref exahype2.api.rkfd4.CentralDifferencesWithRungeKutta).
60
61 @param patch_size: int
62 Number of grid cells per patch and axis. The CFL condition is
63 evaluated over the grid cell size, i.e. the mesh cell size divided
64 by patch_size.
65
66 @param cfl_constant: float or None
67 CFL constant for adaptive time stepping.
68
69 @param time_step_size: float or None
70 Time step size for fixed time stepping. Exactly one of
71 cfl_constant and time_step_size has to be set.
72
73 """
74 AbstractRKFD4ActionSet.__init__(self, solver)
75 if (cfl_constant is None) == (time_step_size is None):
76 raise ValueError("Set exactly one of cfl_constant and time_step_size")
77 self.patch_size = patch_size
78 self.cfl_constant = cfl_constant
79 self.time_step_size = time_step_size
80
81 @property
82 def is_adaptive(self) -> bool:
83 return self.cfl_constant is not None
84
86 instance = "repositories::" + self._solver.get_name_of_global_instance()
87 if self.is_adaptive:
88 admissible_time_step_size = f"""
89 const peano4::grid::GridStatistics globalStatistics = peano4::parallel::SpacetreeSet::getInstance().getGridStatistics();
90 const double globalMinGridCellH = tarch::la::max( globalStatistics.getMinH() ) / {self.patch_size};
91
92 double localMaxEigenvalue = {instance}.getAndResetMaxEigenvalue();
93 double globalMaxEigenvalue = localMaxEigenvalue;
94 #ifdef Parallel
95 tarch::mpi::Rank::getInstance().allReduce(
96 &localMaxEigenvalue, &globalMaxEigenvalue, 1, MPI_DOUBLE, MPI_MAX,
97 [&]() -> void {{ tarch::services::ServiceRepository::getInstance().receiveDanglingMessages(); }}
98 );
99 #endif
100 const double admissibleTimeStepSize = {self.cfl_constant!r} * globalMinGridCellH / globalMaxEigenvalue / Dimensions;
101"""
102 else:
103 admissible_time_step_size = f"""
104 const double admissibleTimeStepSize = {self.time_step_size!r};
105"""
106 return (
107 admissible_time_step_size
108 + f"""
109 // The time stamp advances by the time step size the update used,
110 // newTimeStepSize is the one the next update will use
111 const double usedTimeStepSize = {instance}.getMaxTimeStepSize();
112 const double newTimeStamp = {instance}.getMaxTimeStamp() + usedTimeStepSize;
113 // The last time step is not truncated, i.e. it may overshoot MaxTerminalTime
114 const double newTimeStepSize = admissibleTimeStepSize;
115
116 {instance}.updateCell( newTimeStepSize, newTimeStamp );
117 {instance}.finishTimeStep();
118 {instance}.startTimeStep(
119 {instance}.getMinTimeStamp(),
120 {instance}.getMaxTimeStamp(),
121 {instance}.getMinTimeStepSize(),
122 {instance}.getMaxTimeStepSize()
123 );
124 logInfo( "unprepareTraversal()", "t=" << {instance}.getMaxTimeStamp() << ", new dt=" << newTimeStepSize );
125"""
126 )
127
128 def get_includes(self):
129 return (
130 AbstractRKFD4ActionSet.get_includes(self)
131 + """
132#include <algorithm>
133#include "tarch/mpi/Rank.h"
134#include "tarch/services/ServiceRepository.h"
135#include "tarch/la/VectorOperations.h"
136#include "peano4/parallel/SpacetreeSet.h"
137#include "peano4/grid/grid.h"
138"""
139 )
140
142 return __name__.replace(".py", "").replace(".", "_")
Base class of the action sets of exahype2.api.rkfd4 which work against the solver.
Determine the time step size of the next Runge-Kutta step and advance the solver's time stamp.
get_action_set_name(self)
You should replicate this function in each subclass, so you get meaningful action set names (otherwis...
get_includes(self)
The includes cover the kernels the numerical scheme generates (exahype2.api.rkfd4....
__init__(self, solver, patch_size, cfl_constant=None, time_step_size=None)