Peano
Loading...
Searching...
No Matches
InitDofs.py
Go to the documentation of this file.
1"""!
2The action sets in this file are intended for use with new MG solvers.
3
4We have a mix of behaviours to add in - namely determining max refinement
5level during our first descent, as well as standard stuff eg determining
6which facets are boundary / interior etc.
7
8For the CG, we need to determine:
9 - the number of refinement levels available
10 - the level of the finest level
11
12We need to add some bookkeeping to the abstract solver to keep track of
13these, because the solver level matters.
14
15We need to associate an int "level" with each vertex, so we give each Vertex
16this attribute (Level) and set it to -1 initially. Then during
17touchVertexFirstTime, we set the "level" of the fineGridVertex to be one more
18than the level of the coarse vertex
19
20Slight problem is that we don't have the coarse vertex in scope at this stage.
21So we need to maintain this hierarchy for the cells, as well as setting the
22level of the vertices to be one more than the coarse cells.
23"""
24
25
26from peano4.solversteps.ActionSet import ActionSet
27
28import peano4
29import jinja2
30
32 templateTouchCellFirstTime="""
33
34 // set the level...
35 fineGridCell{{SOLVER_NAME}}.setLevel(
36 coarseGridCell{{SOLVER_NAME}}.getLevel() + 1
37 );
38
39 celldata::{{SOLVER_NAME}}::Type dofType = repositories::{{SOLVER_INSTANCE}}.getCellType(marker.x(),marker.h());
40 switch(dofType)
41 {
42 case celldata::{{SOLVER_NAME}}::Type::Outside:
43 {
44 assertion(false);
45 }
46 break;
47
48 case celldata::{{SOLVER_NAME}}::Type::Interior:
49 {
50 fineGridCell{{SOLVER_NAME}}.setType( celldata::{{SOLVER_NAME}}::Type::Interior );
51 repositories::{{SOLVER_INSTANCE}}.updateMinMaxMeshSize( marker.h() );
52 }
53 break;
54 }
55
56 """
57
58 templateTouchVertexFirstTime="""
59
60 // set its level...
61 fineGridVertex{{SOLVER_NAME}}.setLevel(
62 coarseGridCell{{SOLVER_NAME}}.getLevel() + 1
63 );
64
65 tarch::la::Vector<{{VERTEX_CARDINALITY}}, double> zeroes { 0 };
66 fineGridVertex{{SOLVER_NAME}}.setResidual( zeroes );
67 fineGridVertex{{SOLVER_NAME}}.setDelta( zeroes );
68 fineGridVertex{{SOLVER_NAME}}.setOldU( zeroes );
69 fineGridVertex{{SOLVER_NAME}}.setDiag( zeroes );
70
71 vertexdata::{{SOLVER_NAME}}::Type dofType = repositories::{{SOLVER_INSTANCE}}.getVertexType(marker.x(),marker.h());
72
73 switch(dofType)
74 {
75 case vertexdata::{{SOLVER_NAME}}::Type::Boundary:
76 {
77 fineGridVertex{{SOLVER_NAME}}.setType( vertexdata::{{SOLVER_NAME}}::Type::Boundary );
78 if ( !marker.willBeRefined() ) {
79 // we have boundary vertex
80 // init its solution too
81 tarch::la::Vector<{{VERTEX_CARDINALITY}}, double> u;
82 tarch::la::Vector<{{VERTEX_CARDINALITY}}, double> rhs;
83
84 // send these into init
85 repositories::{{SOLVER_INSTANCE}}.initVertex( marker.x(), marker.h(), u, rhs );
86
87 // store
88 fineGridVertex{{SOLVER_NAME}}.setU( u );
89 fineGridVertex{{SOLVER_NAME}}.setRhs( rhs );
90
91 // if we are on the finest level, we need to alert the abstract solver...
92 repositories::{{SOLVER_INSTANCE}}.setActiveLevel( fineGridVertex{{SOLVER_NAME}}.getLevel() );
93 repositories::{{SOLVER_INSTANCE}}.setFinestLevel( fineGridVertex{{SOLVER_NAME}}.getLevel() );
94 }
95 }
96 break;
97
98 case vertexdata::{{SOLVER_NAME}}::Type::Interior:
99 {
100 fineGridVertex{{SOLVER_NAME}}.setType( vertexdata::{{SOLVER_NAME}}::Type::Interior );
101
102 if ( !marker.willBeRefined() ){
103 // init its solution since we are on the finest level
104 tarch::la::Vector<{{VERTEX_CARDINALITY}}, double> u;
105 tarch::la::Vector<{{VERTEX_CARDINALITY}}, double> rhs;
106
107 // send these into init
108 repositories::{{SOLVER_INSTANCE}}.initVertex( marker.x(), marker.h(), u, rhs );
109
110 // store
111 fineGridVertex{{SOLVER_NAME}}.setU( u );
112 fineGridVertex{{SOLVER_NAME}}.setRhs( rhs );
113
114 // if we are on the finest level, we need to alert the abstract solver...
115 repositories::{{SOLVER_INSTANCE}}.setActiveLevel( fineGridVertex{{SOLVER_NAME}}.getLevel() );
116 repositories::{{SOLVER_INSTANCE}}.setFinestLevel( fineGridVertex{{SOLVER_NAME}}.getLevel() );
117 }
118 }
119 break;
120
121 case vertexdata::{{SOLVER_NAME}}::Type::Coarse:
122 assertionMsg(false, "should not be returned by user" );
123 break;
124
125 case vertexdata::{{SOLVER_NAME}}::Type::Undefined:
126 assertionMsg(false, "should not be returned by user" );
127 break;
128 }
129
130
131 """
132
133 templateEndTraversal="""
134 repositories::{{SOLVER_INSTANCE}}.validateRefinementLevel();
135 """
136
137 def __init__(self,
138 solver):
139 super( InitDofsCollocatedMG, self ).__init__()
140 self.d = {}
141 self.d["SOLVER_INSTANCE"] = solver.instance_name()
142 self.d["SOLVER_NAME"] = solver.typename()
143 self.d["VERTEX_CARDINALITY"] = solver._unknowns_per_vertex_node
144
145 def get_body_of_operation(self,operation_name):
146 result = ""
147 if operation_name==peano4.solversteps.ActionSet.OPERATION_TOUCH_VERTEX_FIRST_TIME:
148 result = jinja2.Template(self.templateTouchVertexFirstTime).render(**self.d)
149 pass
150 if operation_name==peano4.solversteps.ActionSet.OPERATION_TOUCH_CELL_FIRST_TIME:
151 result = jinja2.Template(self.templateTouchCellFirstTime).render(**self.d)
152 pass
153 if operation_name==peano4.solversteps.ActionSet.OPERATION_END_TRAVERSAL:
154 result = jinja2.Template(self.templateEndTraversal).render(**self.d)
155 return result
156
158 """!
159
160 Configure name of generated C++ action set
161
162 This action set will end up in the directory observers with a name that
163 reflects how the observer (initialisation) is mapped onto this action
164 set. The name pattern is ObserverName2ActionSetIdentifier where this
165 routine co-determines the ActionSetIdentifier. We make is reflect the
166 Python class name.
167
168 """
169 return __name__.replace(".py", "").replace(".", "_")
170
172 """!
173
174 The action set that Peano will generate that corresponds to this class
175 should not be modified by users and can safely be overwritten every time
176 we run the Python toolkit.
177
178 """
179 return False
180
181 def get_includes(self):
182 """!
183
184 We need the solver repository in this action set, as we directly access
185 the solver object. We also need access to Peano's d-dimensional loops.
186
187 """
188 return """
189#include "repositories/SolverRepository.h"
190#include "peano4/utils/Loop.h"
191"""
Action set (reactions to events)
Definition ActionSet.py:6
user_should_modify_template(self)
The action set that Peano will generate that corresponds to this class should not be modified by user...
Definition InitDofs.py:171
get_body_of_operation(self, operation_name)
Return actual C++ code snippets to be inserted into C++ code.
Definition InitDofs.py:145
get_includes(self)
We need the solver repository in this action set, as we directly access the solver object.
Definition InitDofs.py:181
get_action_set_name(self)
Configure name of generated C++ action set.
Definition InitDofs.py:157