Peano
Loading...
Searching...
No Matches
MatrixFreeJacobi.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
7
8
10
11
12
13 def __init__(self,
14 vertex_data,
15 relaxation_coefficient,
16 rhs_expr,
17 unknown_setter="setU",
18 unknown_getter="getU",
19 residual_setter="setResidual",
20 residual_getter="getResidual",
21 store_residual_persistently_and_update_in_touch_first = True
22 ):
23 """
24
25 :Attibutes:
26
27 vertex_data: DaStGen object
28 This object must have at least two attributes: A double value u and a double
29 value residual.
30
31 kappa_expr: C++ code (string)
32 Material parameter. Pass in 1.0 if there's no jump anywhere.
33
34
35 """
36 self.d = {}
37 self.d[ "UNKNOWN" ] = vertex_data.name
38 self.d[ "UNKNOWN_SETTER" ] = unknown_setter
39 self.d[ "UNKNOWN_GETTER" ] = unknown_getter
40 self.d[ "RESIDUAL_SETTER" ] = residual_setter
41 self.d[ "RESIDUAL_GETTER" ] = residual_getter
42 self.d[ "OMEGA" ] = relaxation_coefficient
43 self.d[ "RHS" ] = rhs_expr
45 self._store_residual_persistently_and_update_in_touch_first = store_residual_persistently_and_update_in_touch_first
46
47
49 return " return std::vector< peano4::grid::GridControlEvent >();\n"
50
51
53 return __name__.replace(".py", "").replace(".", "_")
54
55
57 return False
58
59
60 __Template_Constructor = jinja2.Template("""
61 _cellStiffnessMatrix = toolbox::finiteelements::StencilFactory::getLaplacian( 0.0, 1.0 );
62""")
63
64
65 #def get_constructor_body(self):
66 # return self.__Template_Constructor.format(**self.d)
67
68
69 Template_ResetResidual = """
70 fineGridVertex{{UNKNOWN}}.{{RESIDUAL_SETTER}}(0.0);
71"""
72
73
74 """
75
76 This is where you should interpolate
77
78 @todo Einbauen! Obwohl wir wissen, dass es ueberschrieben wird
79
80 """
81 Template_CreateHangingVertex = """
82 fineGridVertex{{UNKNOWN}}.{{RESIDUAL_SETTER}}(0.0);
83"""
84
85
86 Template_TouchCellFirstTime = """
87 const tarch::la::Vector<ThreePowerD,double> stencil = toolbox::finiteelements::getLaplacian(
88 marker.h(), 1.0
89 );
90 const toolbox::finiteelements::ElementWiseAssemblyMatrix A = toolbox::finiteelements::getElementWiseAssemblyMatrix(
91 stencil
92 );
93
94 tarch::la::Vector<TwoPowerD,double> u;
95 for (int i=0; i<TwoPowerD; i++) {
96 u(i) = fineGridVertices{{UNKNOWN}}(i).{{UNKNOWN_GETTER}}();
97 }
98
99 tarch::la::Vector<TwoPowerD,double> r = A * u;
100
101 for (int i=0; i<TwoPowerD; i++) {
102 fineGridVertices{{UNKNOWN}}(i).{{RESIDUAL_SETTER}}(
103 fineGridVertices{{UNKNOWN}}(i).{{RESIDUAL_GETTER}}() + r(i)
104 );
105 }
106"""
107
108
109 Template_UpdateValue = """
110 const tarch::la::Vector<ThreePowerD,double> stencil = toolbox::finiteelements::getLaplacian(
111 marker.h(), 1.0
112 );
113 const double diag = stencil(ThreePowerD/2);
114 const double residual = {{RHS}} * tarch::la::volume(marker.h()) - fineGridVertex{{UNKNOWN}}.{{RESIDUAL_GETTER}}();
115 fineGridVertex{{UNKNOWN}}.{{UNKNOWN_SETTER}}(
116 fineGridVertex{{UNKNOWN}}.{{UNKNOWN_GETTER}}()
117 +
118 {{OMEGA}} * residual / diag
119 );
120
121 if ( marker.coincidesWithCoarseGridVertex() ) {
122 tarch::la::Vector<Dimensions,int> parent = marker.getRelativePositionWithinFatherCell() / 3;
123 coarseGridVertices{{UNKNOWN}}( peano4::utils::dLinearised(parent,2) ).{{UNKNOWN_SETTER}}(
124 fineGridVertex{{UNKNOWN}}.{{UNKNOWN_GETTER}}()
125 );
126 }
127"""
128
129
130 Template_CreatePersistentVertex = """
131"""
132
133 def get_body_of_operation(self,operation_name):
134 result = "\n"
135 if operation_name==ActionSet.OPERATION_TOUCH_VERTEX_FIRST_TIME:
136 result = """
137logTraceIn( "touchVertexFirstTime()" );
138"""
140 result += jinja2.Template(self.Template_UpdateValue).render(**self.d)
141 result += jinja2.Template(self.Template_ResetResidual).render(**self.d)
142 result += """
143logTraceOut( "touchVertexFirstTime()" );
144"""
145 if operation_name==ActionSet.OPERATION_CREATE_HANGING_VERTEX:
146 result = """
147logTraceIn( "createHangingVertex()" );
148"""
149 result += jinja2.Template(self.Template_CreateHangingVertex).render(**self.d)
150 result += """
151logTraceOut( "createHangingVertex()" );
152"""
153 if operation_name==ActionSet.OPERATION_CREATE_PERSISTENT_VERTEX:
154 result = """
155logTraceIn( "createPersistentVertex()" );
156"""
157 result += jinja2.Template(self.Template_CreatePersistentVertex).render(**self.d)
158 result += """
159logTraceOut( "createPersistentVertex()" );
160"""
161 if operation_name==ActionSet.OPERATION_TOUCH_CELL_FIRST_TIME:
162 result = """
163logTraceIn( "touchCellFirstTime()" );
164"""
165 result += jinja2.Template(self.Template_TouchCellFirstTime).render(**self.d)
166 result += """
167logTraceOut( "touchCellFirstTime()" );
168"""
169 if operation_name==ActionSet.OPERATION_TOUCH_VERTEX_LAST_TIME:
170 result = """
171logTraceIn( "touchVertexLastTime()" );
172"""
174 result += jinja2.Template(self.Template_UpdateValue).render(**self.d)
175 result += """
176logTraceOut( "touchVertexLastTime()" );
177"""
178 return result
179
180
181 def get_attributes(self):
182 return """
183 toolbox::finiteelements::ElementWiseAssemblyMatrix _cellStiffnessMatrix;
184"""
185
186
187 def get_includes(self):
188 return """
189#include "toolbox/finiteelements/ElementMatrix.h"
190#include "toolbox/finiteelements/StencilFactory.h"
191
192#include "peano4/utils/Loop.h"
193""" + self.additional_includes
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.
get_attributes(self)
Return attributes as copied and pasted into the generated class.
get_includes(self)
Return include statements that you need.
user_should_modify_template(self)
Is the user allowed to modify the output.
__init__(self, vertex_data, relaxation_coefficient, rhs_expr, unknown_setter="setU", unknown_getter="getU", residual_setter="setResidual", residual_getter="getResidual", store_residual_persistently_and_update_in_touch_first=True)
:Attibutes: