Peano
Loading...
Searching...
No Matches
SpawnTaskToUpdateCellInterior.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 .AbstractFVActionSet import AbstractFVActionSet
4from .UpdateCellInterior import UpdateCellInterior
5
6import peano4.api
7
8
10 """!
11
12 Update the patch interior with a damped time step size
13
14 The action set takes the admissible time step size of the current time
15 step, i.e. the third entry of solver._max_eigenvalue_and_time_step_sizes
16 (@see exahype2.api.fv.actionsets.ReadjustTimeStepSizeForOptimisticGlobalTimestepping),
17 multiplies it with damp_time_step_size, and stores the result as
18 estimated time step size in the second entry. Afterwards, it updates the
19 patch interior with this damped time step size (@see
20 exahype2.api.fv.actionsets.UpdateCellInterior and
21 exahype2.api.fv.RusanovWithOptimisticExplicitEuler). As the admissible
22 time step size is never damped itself, the estimates do not shrink from
23 time step to time step.
24
25 The action set has to run before
26 exahype2.api.fv.actionsets.ComputeGlobalCFLCondition, which is why
27 exahype2.api.fv.RusanovWithOptimisticExplicitEuler makes the CFL
28 condition's task depend on the spawn's task. This way, the order in
29 which the tasks are added to the task graph does not matter. The CFL
30 condition advances the solver's time stamp and time step size
31 straightaway. Before it runs, the solver still holds the time stamp
32 and time step size of the current time step. The patch, however, has
33 already been advanced by the current time step. The patch interior
34 update hence starts from the solver's time stamp plus its time step
35 size.
36
37 If the admissible time step size is zero, i.e. in the very first time
38 step, there is nothing to estimate from. The action set then does not
39 update the patch interior. It sets the estimated time step size
40 (second entry) to the maximum double value, so the next time step
41 takes the traditional route.
42
43 ## Spawning the update as a task
44
45 If the action set is given the cell data holding a task number, it does
46 not update the patch interior straightaway. Instead, it takes a new task
47 number, stores it in the cell data, and spawns the update as a task
48 with this number. exahype2.api.fv.actionsets.InsertPatchInterior and
49 exahype2.api.fv.actionsets.DiscardPatchInterior wait for this task.
50 If there is nothing to update, the action set stores
51 tarch::multicore::TaskEnumerator::NoNumber instead.
52
53 The task captures the cell data by value. As the patch interior and the
54 eigenvalue data are managed via smart pointers, the task writes into the
55 cell's data. The patch, however, is handed to the task as a private copy,
56 as the next time step updates the patch's shell, which the task reads,
57 while the task might still be running. The time stamp and the time step
58 size are evaluated before the task is spawned, as the CFL condition
59 changes the solver's values afterwards.
60
61 """
62
63 def __init__(self, solver, damp_time_step_size, guard, task_number=None):
64 """!
65
66 @param solver: exahype2.api.Solver
67 Has to have solver._patch, solver._patch_interior,
68 solver._max_eigenvalue_and_time_step_sizes and
69 solver.compute_kernel_call_on_patch_interior set - @see
70 exahype2.api.fv.RusanovWithOptimisticExplicitEuler.
71
72 @param damp_time_step_size: float
73 Factor the solver's current time step size is scaled with to
74 obtain the estimated time step size of the patch interior update.
75
76 @param guard: str
77 C++ boolean expression, evaluated per cell in touchCellFirstTime,
78 guarding whether the patch interior of that cell is updated.
79
80 @param task_number: peano4.api.datamodel.DaStGen2
81 Cell data holding the number of the task updating the patch
82 interior. If None, the update runs straightaway.
83
84 """
85 AbstractFVActionSet.__init__(self, solver)
86 self.damp_time_step_size = damp_time_step_size
87 self.guard = guard
88 self.task_number = task_number
89 if task_number is None:
91 solver,
92 guard,
93 time_step_size="dampedTimeStepSize",
94 time_stamp=f"repositories::{solver.get_name_of_global_instance()}.getMinTimeStamp() + "
95 f"repositories::{solver.get_name_of_global_instance()}.getMinTimeStepSize()",
96 )
97 else:
99 solver,
100 guard,
101 time_step_size="dampedTimeStepSize",
102 time_stamp="timeStampOfPatchInteriorUpdate",
103 input="patchCopy->data()",
104 )
105
106 def get_body_of_operation(self, operation_name):
107 result = ""
108 if operation_name == peano4.api.solversteps.ActionSet.OPERATION_TOUCH_CELL_FIRST_TIME:
109 solver = f"repositories::{self._solver.get_name_of_global_instance()}"
110 data = f"fineGridCell{self._solver._max_eigenvalue_and_time_step_sizes.name}"
111 update = self._update_cell_interior.get_body_of_operation(operation_name)
112 if self.task_number is None:
113 result = f"""
114 if ( tarch::la::equals({data}.getMaxEigenvalue(2), 0.0) ) {{
115 {data}.setMaxEigenvalue( 1, std::numeric_limits<double>::max() );
116 }}
117 else {{
118 double dampedTimeStepSize = {self.damp_time_step_size} * {data}.getMaxEigenvalue(2);
119 {data}.setMaxEigenvalue( 1, dampedTimeStepSize );
120""" + update + """
121 }
122"""
123 else:
124 task_number = f"fineGridCell{self.task_number.name}"
125 patch = f"fineGridCell{self._solver._patch.name}"
126 result = f"""
127 if ( tarch::la::equals({data}.getMaxEigenvalue(2), 0.0) ) {{
128 {data}.setMaxEigenvalue( 1, std::numeric_limits<double>::max() );
129 {task_number}.setTaskNumber( tarch::multicore::TaskEnumerator::NoNumber );
130 }}
131 else {{
132 const double dampedTimeStepSize = {self.damp_time_step_size} * {data}.getMaxEigenvalue(2);
133 const double timeStampOfPatchInteriorUpdate = {solver}.getMinTimeStamp() + {solver}.getMinTimeStepSize();
134 {data}.setMaxEigenvalue( 1, dampedTimeStepSize );
135
136 // The next time step updates the patch's shell while the task might still run
137 auto patchCopy = std::make_shared< std::vector<double> >( {patch}.Cardinality );
138 for (int i=0; i<{patch}.Cardinality; i++) {{
139 (*patchCopy)[i] = {patch}.value[i];
140 }}
141
142 const int taskNumber = {task_number}.getTaskEnumerator().getNumber();
143 {task_number}.setTaskNumber( taskNumber );
144 tarch::multicore::spawnTask(
145 new tarch::multicore::TaskWithCopyOfFunctor(
146 tarch::multicore::Task::DontFuse,
147 tarch::multicore::Task::DefaultPriority,
148 [=]() mutable {{
149""" + update + f"""
150 }}
151 ),
152 tarch::multicore::NoInDependencies,
153 taskNumber
154 );
155 }}
156"""
157 return result
158
159 def get_guard_of_operation(self, operation_name):
160 if operation_name == peano4.api.solversteps.ActionSet.OPERATION_TOUCH_CELL_FIRST_TIME:
161 return self.guard
162 return "true"
163
164 def get_includes(self):
165 result = super().get_includes() + """
166#include <limits>
167#include "tarch/la/ScalarOperations.h"
168"""
169 if self.task_number is not None:
170 result += """
171#include <memory>
172#include <vector>
173#include "tarch/multicore/multicore.h"
174#include "tarch/multicore/Task.h"
175#include "tarch/multicore/TaskEnumerator.h"
176"""
177 return result
178
180 return __name__.replace(".py", "").replace(".", "_")
get_body_of_operation(self, operation_name)
Return actual C++ code snippets to be inserted into C++ code.
get_action_set_name(self)
You should replicate this function in each subclass, so you get meaningful action set names (otherwis...
Update the patch interior of a Finite Volume patch.