Peano
Loading...
Searching...
No Matches
ScalarJacobiWithRediscretisation.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
3from peano4.solversteps.ActionSet import ActionSet
4
5
6import jinja2
7import peano4
8import dastgen2
9
10
12
13
14 def construct_face_helper_data(cell_data_model: peano4.datamodel.DaStGen2):
15 """
16
17 We cannot access neighbouring cells within Peano. Therefore, we have
18 to store helper data on the faces such that we can reconstruct the
19 value within the face-connected neighbours.
20
21 cell_data_mode:
22
23 """
24 result = peano4.datamodel.DaStGen2( cell_data_model.name )
25
26 result.data.add_attribute(dastgen2.attributes.Double("OldAverage") )
27 result.data.add_attribute(dastgen2.attributes.Double("NewAverage") )
28
29 # @todo I think this is wrong. It should not be in the generator
30 result.generator.load_store_compute_flag = "::peano4::grid::constructLoadStoreComputeFlag({},{},{})".format(
31 "true", "true", "true" )
32
33 result.generator.send_condition = "true"
34 result.generator.receive_and_merge_condition = "true"
35
36 result.peano4_mpi_and_storage_aspect.merge_implementation += """
37 _NewAverage += neighbour._NewAverage;
38"""
39
40 return result
41
42
43 def __init__(self,
44 cell_data: peano4.datamodel.DaStGen2,
45 face_data: peano4.datamodel.DaStGen2,
46 relaxation_coefficient,
47 rhs_expr,
48 unknown_setter="setU",
49 unknown_getter="getU",
50 guard="true"
51 ):
52 """
53
54 :Attibutes:
55
56 vertex_data: DaStGen object
57 This object must have at least two attributes: A double value u and a double
58 value residual.
59
60 kappa_expr: C++ code (string)
61 Material parameter. Pass in 1.0 if there's no jump anywhere.
62
63
64 """
65 assert relaxation_coefficient>0.0, "Relaxation coefficient has to be positive"
66 assert relaxation_coefficient<2.0, "Relaxation coefficient has to be smaller than 2"
67
68 self.d = {}
69 self.d[ "CELL_NAME" ] = cell_data.name
70 self.d[ "FACE_NAME" ] = face_data.name
71 self.d[ "UNKNOWN_SETTER" ] = unknown_setter
72 self.d[ "UNKNOWN_GETTER" ] = unknown_getter
73 self.d[ "OMEGA" ] = relaxation_coefficient
74 self.d[ "RHS" ] = rhs_expr
75 self.d[ "GUARD" ] = guard
77
78
80 return " return std::vector< peano4::grid::GridControlEvent >();\n"
81
82
84 return __name__.replace(".py", "").replace(".", "_")
85
86
88 return False
89
90
91 #def get_constructor_body(self):
92 # return self.__Template_Constructor.format(**self.d)
93
94
95 _Template_CreateCell= """
96 fineGridCell{{CELL_NAME}}.{{UNKNOWN_SETTER}}(0.0);
97"""
98
99 _Template_CreatePersistentFace = """
100 fineGridFace{{FACE_NAME}}.setNewAverage(0.0);
101 fineGridFace{{FACE_NAME}}.setOldAverage(0.0);
102"""
103
104 _Template_CreateHangingFace = """
105 auto coarseValue = coarseGridFaces{{FACE_NAME}}( marker.getSelectedFaceNumber() ).getOldAverage();
106
107 fineGridFace{{FACE_NAME}}.setOldAverage( coarseValue );
108 fineGridFace{{FACE_NAME}}.setNewAverage( coarseValue );
109"""
110
111 _Template_DestroyHangingFace = """
112 coarseGridFaces{{FACE_NAME}}( marker.getSelectedFaceNumber() ).setNewAverage(
113 coarseGridFaces{{FACE_NAME}}( marker.getSelectedFaceNumber() ).getNewAverage()
114 +
115 ThreePowerD/3.0 * fineGridFace{{FACE_NAME}}.getNewAverage()
116 );
117"""
118
119 _Template_TouchFaceFirstTime = """
120 fineGridFace{{FACE_NAME}}.setOldAverage( fineGridFace{{FACE_NAME}}.getNewAverage() );
121 fineGridFace{{FACE_NAME}}.setNewAverage( 0.0 );
122"""
123
124
125 _Template_TouchCellFirstTime_UpdateSolution = """
126 // @todo Das ist nicht schoen. Ich haette hier gerne nen allgemeinen Stencil
127
128 if ({{GUARD}}) {
129 double diagonal = 0.0;
130 double residual = {{RHS}};
131
132 double centralValue = fineGridCell{{CELL_NAME}}.{{UNKNOWN_GETTER}}();
133
134 for (int d=0; d<Dimensions; d++) {
135 double leftNeighbour = 2.0 * (fineGridFaces{{FACE_NAME}}(d).getOldAverage() - 0.5 * centralValue);
136 double rightNeighbour = 2.0 * (fineGridFaces{{FACE_NAME}}(d+Dimensions).getOldAverage() - 0.5 * centralValue);
137
138 residual -= (-1.0 * leftNeighbour + 2.0 * centralValue - 1.0 * rightNeighbour) / marker.h()(d) / marker.h()(d);
139 diagonal += (2.0) / marker.h()(d) / marker.h()(d);
140 }
141
142 double newValue = fineGridCell{{CELL_NAME}}.{{UNKNOWN_GETTER}}() + {{OMEGA}} / diagonal * residual;
143
144 fineGridCell{{CELL_NAME}}.{{UNKNOWN_SETTER}}( newValue );
145 }
146"""
147
148
149 _Template_TouchCellFirstTime_UpdateFaceHelperData = """
150 for (int d=0; d<Dimensions; d++) {
151 const double newValue = fineGridCell{{CELL_NAME}}.{{UNKNOWN_GETTER}}();
152 fineGridFaces{{FACE_NAME}}(d).setNewAverage( fineGridFaces{{FACE_NAME}}(d).getNewAverage() + 0.5 * newValue );
153 fineGridFaces{{FACE_NAME}}(d+Dimensions).setNewAverage( fineGridFaces{{FACE_NAME}}(d+Dimensions).getNewAverage() + 0.5 * newValue );
154 }
155"""
156
157
158 def get_body_of_operation(self,operation_name):
159 result = "\n"
160 if operation_name==ActionSet.OPERATION_CREATE_CELL:
161 result = jinja2.Template(self._Template_CreateCell).render(**self.d)
162 if operation_name==ActionSet.OPERATION_CREATE_HANGING_FACE:
163 result = jinja2.Template(self._Template_CreateHangingFace).render(**self.d)
164 if operation_name==ActionSet.OPERATION_CREATE_PERSISTENT_FACE:
165 result = jinja2.Template(self._Template_CreatePersistentFace).render(**self.d)
166 if operation_name==ActionSet.OPERATION_TOUCH_FACE_FIRST_TIME:
167 result = jinja2.Template(self._Template_TouchFaceFirstTime).render(**self.d)
168 if operation_name==ActionSet.OPERATION_TOUCH_CELL_FIRST_TIME:
169 result = jinja2.Template(self._Template_TouchCellFirstTime_UpdateSolution).render(**self.d)
170 result += jinja2.Template(self._Template_TouchCellFirstTime_UpdateFaceHelperData).render(**self.d)
171 if operation_name==ActionSet.OPERATION_DESTROY_HANGING_FACE:
172 result = jinja2.Template(self._Template_DestroyHangingFace).render(**self.d)
173 return result
174
175
176 def get_attributes(self):
177 return """
178 toolbox::finiteelements::ElementWiseAssemblyMatrix _cellStiffnessMatrix;
179"""
180
181
182 def get_includes(self):
183 return """
184#include "toolbox/finiteelements/ElementMatrix.h"
185#include "toolbox/finiteelements/StencilFactory.h"
186
187#include "peano4/utils/Loop.h"
188""" + self.additional_includes
189
cardinality is a string (you can thus use symbols as well as long as they will be defined at compile ...
Definition Double.py:6
Default superclass for any data model in Peano which is stored within the grid.
Definition DaStGen2.py:157
Action set (reactions to events)
Definition ActionSet.py:6
get_body_of_operation(self, operation_name)
Return actual C++ code snippets to be inserted into C++ code.
construct_face_helper_data(peano4.datamodel.DaStGen2 cell_data_model)
We cannot access neighbouring cells within Peano.
__init__(self, peano4.datamodel.DaStGen2 cell_data, peano4.datamodel.DaStGen2 face_data, relaxation_coefficient, rhs_expr, unknown_setter="setU", unknown_getter="getU", guard="true")
:Attibutes: