Peano
Loading...
Searching...
No Matches
CopyPatchWithLinearInterpolation.py
Go to the documentation of this file.
1""" This file is part of the ExaGRyPE project. For conditions of distribution and
2 use, please see the copyright notice at www.peano-framework.org
3"""
4
5from peano4.api.solversteps.ActionSet import ActionSet
6
7
9 """!
10
11 Copy the data of one patch into another patch, with a different number of volumes per axis
12
13 The two patches hold the same entries per volume, but they may resolve the
14 mesh cell with a different number of volumes per axis, e.g. 8 Finite
15 Difference points and 17 Finite Volumes. The action set hooks into
16 touchCellFirstTime() and fills each volume of the target with the
17 piecewise (tri)linear interpolant of the source, which is evaluated at the
18 centre of the target volume. Both patches discretise the same mesh cell,
19 and the values sit in the centres of their volumes.
20
21 The interpolation uses the source patch only, i.e. it does not look into
22 neighbouring cells. Targets in the outermost half source volume are
23 extrapolated linearly from the two outermost source volumes. The result is
24 second-order accurate for smooth data, but it is not conservative, and it
25 is not continuous across cell boundaries. If both patches have the same
26 size, the action set copies the data (the weights are 0 and 1).
27
28 """
29
31 self,
32 source_patch,
33 target_patch,
34 guard: str = "true",
35 additional_includes: str = "",
36 ):
37 """!
38
39 @param source_patch: peano4.api.datamodel.Patch
40 The cell-associated patch the action set reads. Has to be
41 cubic, without halo, with at least two volumes per axis.
42
43 @param target_patch: peano4.api.datamodel.Patch
44 The cell-associated patch the action set writes. Has to be cubic,
45 without halo, and has to hold as many entries per volume as the source.
46
47 @param guard: str
48 C++ boolean expression, evaluated per cell in touchCellFirstTime.
49
50 @param additional_includes: str
51 Includes that the generated code needs.
52
53 """
54 super().__init__(descend_invocation_order=1, parallel=False)
55 if source_patch.no_of_unknowns != target_patch.no_of_unknowns:
56 raise ValueError(
57 f"Source patch {source_patch.name} holds {source_patch.no_of_unknowns} entries per volume, "
58 f"but target patch {target_patch.name} holds {target_patch.no_of_unknowns}"
59 )
60 if source_patch.dim[0] < 2:
61 raise ValueError("The source patch needs at least two volumes per axis")
62 self._source = source_patch
63 self._target = target_patch
64 self._guard = guard
65 self.additional_includes = additional_includes
66
68 return ""
69
71 return ""
72
74 return " return std::vector< peano4::grid::GridControlEvent >();\n"
75
77 return __name__.replace(".py", "").replace(".", "_")
78
80 return False
81
82 def get_body_of_operation(self, operation_name):
83 if operation_name != ActionSet.OPERATION_TOUCH_CELL_FIRST_TIME:
84 return "\n"
85 source_size = self._source.dim[0]
86 target_size = self._target.dim[0]
87 entries = self._source.no_of_unknowns
88 return f"""
89 constexpr int SourceSize = {source_size};
90 constexpr int TargetSize = {target_size};
91 constexpr int Entries = {entries};
92 const ::exahype2::enumerator::AoSLexicographicEnumerator sourceEnumerator(1,SourceSize,0,Entries,0);
93 const ::exahype2::enumerator::AoSLexicographicEnumerator targetEnumerator(1,TargetSize,0,Entries,0);
94 const double* __restrict__ source = fineGridCell{self._source.name}.value;
95 double* __restrict__ target = fineGridCell{self._target.name}.value;
96
97 // Index of the lower source volume and the weight of the upper one. The weight
98 // is outside [0,1] for the outermost target volumes (linear extrapolation).
99 auto locate = [](int targetIndex, int& lower, double& upperWeight) -> void {{
100 const double position = (targetIndex + 0.5) * SourceSize / TargetSize - 0.5;
101 lower = std::min( std::max( static_cast<int>( std::floor(position) ), 0 ), SourceSize - 2 );
102 upperWeight = position - lower;
103 }};
104
105 for (int z=0; z<TargetSize; z++) {{
106 int z0;
107 double wz;
108 locate(z, z0, wz);
109 for (int y=0; y<TargetSize; y++) {{
110 int y0;
111 double wy;
112 locate(y, y0, wy);
113 for (int x=0; x<TargetSize; x++) {{
114 int x0;
115 double wx;
116 locate(x, x0, wx);
117 tarch::la::Vector<Dimensions,int> targetVolume = {{x, y, z}};
118 for (int entry=0; entry<Entries; entry++) {{
119 double value = 0.0;
120 for (int dz=0; dz<2; dz++)
121 for (int dy=0; dy<2; dy++)
122 for (int dx=0; dx<2; dx++) {{
123 tarch::la::Vector<Dimensions,int> sourceVolume = {{x0+dx, y0+dy, z0+dz}};
124 const double weight = (dx==0 ? 1.0-wx : wx) * (dy==0 ? 1.0-wy : wy) * (dz==0 ? 1.0-wz : wz);
125 value += weight * source[ sourceEnumerator(0, sourceVolume, entry) ];
126 }}
127 target[ targetEnumerator(0, targetVolume, entry) ] = value;
128 }}
129 }}
130 }}
131 }}
132"""
133
134 def get_guard_of_operation(self, operation_name):
135 if operation_name == ActionSet.OPERATION_TOUCH_CELL_FIRST_TIME:
136 return self._guard
137 return "true"
138
139 def get_attributes(self):
140 return ""
141
142 def get_includes(self):
143 return (
145 + """
146#include <algorithm>
147#include <cmath>
148#include "exahype2/enumerator/AoSLexicographicEnumerator.h"
149"""
150 )
Copy the data of one patch into another patch, with a different number of volumes per axis.
get_body_of_createGridControlEventsForNextTraversal(self)
Return the grid control events that this action set wants to trigger.
get_body_of_operation(self, operation_name)
Return actual C++ code snippets to be inserted into C++ code.
__init__(self, source_patch, target_patch, str guard="true", str additional_includes="")
Action set (reactions to events).
Definition ActionSet.py:6