Peano
Loading...
Searching...
No Matches
InterpolateAndRestrictCells.py
Go to the documentation of this file.
1# This file is part of the Peano project. For conditions of distribution and
2# use, please see the copyright notice at www.peano-framework.org
3import enum
4
5import jinja2
6
7from peano4.api.solversteps.ActionSet import ActionSet
8
9
11 """!
12
13 Initialise the patches of new cells from their parent, and hand the
14 patches of erased cells over to their parent
15
16 The action set realises the data transfer of a dynamically adaptive
17 mesh for patches which live on the finest mesh level only:
18
19 - If a mesh sweep refines a cell, Peano creates its 3^d children.
20 createCell() interpolates the parent's patch into each child's patch
21 (::toolbox::blockstructured::interpolateCell_AoS_...). The parent's
22 patch is still available in this sweep, as the parent has not been
23 refined when the sweep has started.
24 - If a mesh sweep erases the children of a cell, the parent becomes a
25 leaf again. touchCellFirstTime() clears the parent's patch, and
26 destroyCell() then restricts each child's patch into the parent's patch
27 (::toolbox::blockstructured::restrictCell_AoS_...). The averaging
28 accumulates the children's contributions, so the parent has to be
29 cleared first.
30
31 The patch is a NxNxN patch, i.e. N volumes per axis.
32
33 """
34
35 class Interpolation(enum.Enum):
36 """!
37
38 Interpolation schemes
39
40 The value of each entry is the suffix of the C++ routine
41 ::toolbox::blockstructured::interpolateCell_AoS_<suffix>().
42
43 - PiecewiseConstant copies the coarse value into all fine volumes
44 which it covers.
45 - Linear interpolates d-linearly. Along the boundary of the coarse
46 patch, it extrapolates constantly, as linear extrapolation can
47 produce unphysical values such as negative densities next to steep
48 gradients.
49
50 """
51
52 PiecewiseConstant = "piecewise_constant"
53 Linear = "linear_with_constant_extrapolation"
54
55 class Restriction(enum.Enum):
56 """!
57
58 Restriction schemes
59
60 The value of each entry is the suffix of the C++ routine
61 ::toolbox::blockstructured::restrictCell_AoS_<suffix>().
62
63 - Averaging sets each coarse volume to the average of the 3^d fine
64 volumes it covers.
65
66 """
67
68 Averaging = "averaging"
69
70 TemplateCreateCell = jinja2.Template(
71 """
72 logTraceInWith1Argument( "createCell(...)---InterpolateAndRestrictCells", marker.toString() );
73 ::toolbox::blockstructured::interpolateCell_AoS_{{INTERPOLATION}}(
74 marker,
75 {{DOFS_PER_AXIS}},
76 {{UNKNOWNS}},
77 coarseGridCell{{CELL_NAME}}.value,
78 fineGridCell{{CELL_NAME}}.value
79 );
80 logTraceOut( "createCell(...)---InterpolateAndRestrictCells" );
81"""
82 )
83
84 TemplateTouchCellFirstTime = jinja2.Template(
85 """
86 if ( marker.hasBeenRefined() and not marker.willBeRefined() ) {
87 logTraceInWith1Argument( "touchCellFirstTime(...)---InterpolateAndRestrictCells", marker.toString() );
88 ::toolbox::blockstructured::clearCell(
89 marker,
90 {{DOFS_PER_AXIS}},
91 {{UNKNOWNS}},
92 fineGridCell{{CELL_NAME}}.value
93 );
94 logTraceOut( "touchCellFirstTime(...)---InterpolateAndRestrictCells" );
95 }
96"""
97 )
98
99 TemplateDestroyCell = jinja2.Template(
100 """
101 logTraceInWith1Argument( "destroyCell(...)---InterpolateAndRestrictCells", marker.toString() );
102 ::toolbox::blockstructured::restrictCell_AoS_{{RESTRICTION}}(
103 marker,
104 {{DOFS_PER_AXIS}},
105 {{UNKNOWNS}},
106 fineGridCell{{CELL_NAME}}.value,
107 coarseGridCell{{CELL_NAME}}.value
108 );
109 logTraceOut( "destroyCell(...)---InterpolateAndRestrictCells" );
110"""
111 )
112
114 self,
115 patch,
116 interpolation: "InterpolateAndRestrictCells.Interpolation" = None,
117 restriction: "InterpolateAndRestrictCells.Restriction" = None,
118 additional_includes: str = "",
119 ):
120 """!
121
122 @param patch: peano4.api.datamodel.Patch
123 The cell data which the action set interpolates and restricts.
124
125 @param interpolation: InterpolateAndRestrictCells.Interpolation
126 Interpolation scheme. Defaults to Interpolation.Linear.
127
128 @param restriction: InterpolateAndRestrictCells.Restriction
129 Restriction scheme. Defaults to Restriction.Averaging.
130
131 @param additional_includes: str
132 Further include statements.
133
134 """
135 super().__init__(descend_invocation_order=1, parallel=False)
137 InterpolateAndRestrictCells.Interpolation.Linear
138 if interpolation is None
139 else interpolation
140 )
141 self.restriction = (
142 InterpolateAndRestrictCells.Restriction.Averaging
143 if restriction is None
144 else restriction
145 )
146 assert isinstance(
148 ), self.interpolation
149 assert isinstance(
151 ), self.restriction
152 self.d = {
153 "UNKNOWNS": patch.no_of_unknowns,
154 "DOFS_PER_AXIS": patch.dim[0],
155 "CELL_NAME": patch.name,
156 "INTERPOLATION": self.interpolation.value,
157 "RESTRICTION": self.restriction.value,
158 }
159 self.additional_includes = additional_includes
160
162 return ""
163
165 return ""
166
168 return " return std::vector< peano4::grid::GridControlEvent >();\n"
169
171 return "\n"
172
174 return "\n"
175
176 def get_body_of_operation(self, operation_name):
177 if operation_name == ActionSet.OPERATION_CREATE_CELL:
178 return self.TemplateCreateCell.render(**self.d)
179 if operation_name == ActionSet.OPERATION_TOUCH_CELL_FIRST_TIME:
180 return self.TemplateTouchCellFirstTime.render(**self.d)
181 if operation_name == ActionSet.OPERATION_DESTROY_CELL:
182 return self.TemplateDestroyCell.render(**self.d)
183 return ""
184
186 return __name__.replace(".py", "").replace(".", "_")
187
189 return False
190
191 def get_attributes(self):
192 return ""
193
194 def get_includes(self):
195 return (
196 """
197#include "toolbox/blockstructured/Interpolation.h"
198#include "toolbox/blockstructured/Restriction.h"
199"""
201 )
Initialise the patches of new cells from their parent, and hand the patches of erased cells over to t...
__init__(self, patch, "InterpolateAndRestrictCells.Interpolation" interpolation=None, "InterpolateAndRestrictCells.Restriction" restriction=None, str additional_includes="")
get_attributes(self)
Return attributes as copied and pasted into the generated class.
get_body_of_operation(self, operation_name)
Return actual C++ code snippets to be inserted into C++ code.
get_body_of_createGridControlEventsForNextTraversal(self)
Return the grid control events that this action set wants to trigger.
user_should_modify_template(self)
Is the user allowed to modify the output.
Action set (reactions to events)
Definition ActionSet.py:6