Peano
Loading...
Searching...
No Matches
archive_AssemblePETSCMatrix.py
Go to the documentation of this file.
1'''
2this file is for code which is no longer used
3'''
4
5from peano4.solversteps.ActionSet import ActionSet
6
7import peano4
8import jinja2
9
10
11
12'''
13
14This class is somewhat deprecated. was put in place for demonstrating firedrake code, but we move
15away from this now.
16
17'''
18
20
21 TemplateInitCell = """
22 if (not marker.willBeRefined()){
23 //init vector to collect the cell indices we use
24 std::vector<int> cellDofIndices;
25
26 //get global index
27 std::pair<int,int> cellLocalIndex = std::make_pair(_spacetreeId, fineGridCell{{SOLVER_NAME}}.getNumber());
28 int cellGlobalIndex = repositories::{{SOLVER_INSTANCE}}.getCellLocalToGlobalMap().getGlobalIndex(cellLocalIndex);
29
30 //add this global index, plus each of its degrees of freedom, to the cellDofIndices
31 for (int i=0; i<_cellDofs; i++)
32 cellDofIndices.push_back( cellGlobalIndex + i ); //works because we reserved (eg) 27 indices per cell earlier
33
34 /*
35 enumerate the faces. for each face, we do the following:
36 1. collect the global index of the face, add this to the list faceDofIndices
37 - also add to the list each degree of freedom that lies on this face
38 2. (have already done same for cell indices above)
39 3. insert stencil for FF, CF, FC and CC using these indices
40 */
41
42 for (int face=0; face<2*Dimensions; face++){
43 std::vector<int> faceDofIndices;
44
45 //get global index
46 std::pair<int,int> localIndex = std::make_pair(_spacetreeId, fineGridFaces{{SOLVER_NAME}}(face).getNumber());
47 int globalIndex = repositories::{{SOLVER_INSTANCE}}.getFaceLocalToGlobalMap().getGlobalIndex(localIndex);
48
49 //insert the faceDofs into our temp vector.
50 for (int i=0; i<_faceDofs; i++)
51 faceDofIndices.push_back( globalIndex + i );
52
53 //insert the stencil for CF interactions
54 //use face iterator to index our vector of stencils
55 repositories::instanceOfPetsc.insertLocalMatrix( cellDofIndices, faceDofIndices, _stencilsCF[face] );
56
57 //insert the stencil for FC interactions, only if we are not on the boundary
58 if ( fineGridFaces{{SOLVER_NAME}}(face).getType() == facedata::{{SOLVER_NAME}}::Type::Interior ){
59 repositories::instanceOfPetsc.insertLocalMatrix( faceDofIndices, cellDofIndices, _stencilsFC[face] );
60
61 //also insert the FF stencil intended for the interior
62 //there's only two, so we use 0 for boundary and 1 for interior
63 repositories::instanceOfPetsc.insertLocalMatrix( faceDofIndices, faceDofIndices, _stencilsFF[1] );
64 }
65
66 else if ( fineGridFaces{{SOLVER_NAME}}(face).getType() == facedata::{{SOLVER_NAME}}::Type::Boundary ){
67 //insert FF stencil intended for boundary
68 repositories::instanceOfPetsc.insertLocalMatrix( faceDofIndices, faceDofIndices, _stencilsFF[0] );
69 }
70
71 }
72
73 //before we exit, let's insert the stencil for cell-cell interactions
74 repositories::instanceOfPetsc.insertLocalMatrix( cellDofIndices, cellDofIndices, _stencilCC );
75 }
76"""
77
78 def __init__(self, solver):
79 super( AssemblePETSCMatrixOnCellsAndFaces, self ).__init__()
80 self.d = {}
81 self.d["SOLVER_INSTANCE"] = solver.instance_name()
82 self.d["SOLVER_NAME"] = solver.typename()
83 self.d["CELL_CARDINALITY"] = solver.number_of_cell_unknowns
84 self.d["FACE_CARDINALITY"] = solver.number_of_face_unknowns
85
86 '''
87 HANDLING THE STENCILS!
88 '''
89
90 #FIRST - HANDLE STENCIL FOR FACE-FACE INTERACTIONS
91 #we have two stencils, and we insert them into a std::vector
92 #the 0th one is for the boundary, and the 1st one is for the interior
93
94
95 self.d["stencilRowsFF"] = len(solver.stencilsFF[0])
96 self.d["stencilColsFF"] = len(solver.stencilsFF[0][0])
97 numberOfCols = self.d["stencilColsFF"] #use this to insert a line break after every row
98 self.d["STENCILSFF"] = []
99
100 #pass in stencil as a list of lists
101 #convert it to a flat list for passing as std::initializer_list
102 for stencil in solver.stencilsFF:
103 #flatten it
104 stencil=[item for sublist in stencil for item in sublist]
105 stencilAsString = "\n{" + str(stencil[0])
106 for index, el in enumerate(stencil[1:]):
107 stencilAsString += ", "
108 if index > 0 and (index-1) % numberOfCols == 1:
109 stencilAsString += "\n"
110 stencilAsString += str(el)
111 stencilAsString += "}"
112
113 #add to list of stencils
114 self.d["STENCILSFF"].append(stencilAsString)
115
116 #NEXT - HANDLE STENCIL FOR CELL-FACE INTERACTIONS
117 #this time we have a list of stencils which must be ordered
118
119
120 self.d["stencilRowsCF"] = len(solver.stencilsCF[0]) #solver.stencilCF[0] is the 0th stencil itself
121 self.d["stencilColsCF"] = len(solver.stencilsCF[0][0])#captures the number of columns
122 numberOfCols = self.d["stencilColsCF"] #use this to insert a line break after every row
123 self.d["STENCILSCF"] = [] #init a list. we will insert the stencils into them.
124
125 #pass in stencil as a list of lists
126 #convert it to a flat list for passing as std::initializer_list
127 for stencil in solver.stencilsCF:
128 #flatten it
129 stencil=[item for sublist in stencil for item in sublist]
130
131 #init a string to capture the stencil, we will insert this into the dict at the end
132 stencilAsString = "\n{" + str(stencil[0]) # add "{" to begin the intializer list
133 for index, el in enumerate(stencil[1:]):
134 stencilAsString += ", "
135 if index > 0 and (index-1) % numberOfCols == 1:
136 stencilAsString += "\n"
137 stencilAsString += str(el)
138 stencilAsString += "}" #end the initializer list
139
140 #add to list of stencils
141 self.d["STENCILSCF"].append(stencilAsString)
142
143 #NEXT - HANDLE STENCIL FOR FACE-CELL INTERACTIONS
144 #this time we have a list of stencils which must be ordered
145
146
147 self.d["stencilRowsFC"] = len(solver.stencilsFC[0]) #solver.stencilFC[0] is the 0th stencil itself
148 self.d["stencilColsFC"] = len(solver.stencilsFC[0][0])#captures the number of columns
149 numberOfCols = self.d["stencilColsFC"] #use this to insert a line break after every row
150 self.d["STENCILSFC"] = [] #init a list. we will insert the stencils into them
151
152 #pass in stencil as a list of lists
153 #convert it to a flat list for passing as std::initializer_list
154 for stencil in solver.stencilsFC:
155 stencil=[item for sublist in stencil for item in sublist]
156 stencilAsString = "\n{" + str(stencil[0])
157 for index, el in enumerate(stencil[1:]):
158 stencilAsString += ", "
159 if index > 0 and (index-1) % numberOfCols == 1:
160 stencilAsString += "\n"
161 stencilAsString += str(el)
162 stencilAsString += "}"
163
164 #add to list
165 self.d["STENCILSFC"].append(stencilAsString)
166
167 #FINALLY - HANDLE STENCIL FOR CELL-CELL INTERACTIONS
168
169
170 self.d["stencilRowsCC"] = len(solver.stencilCC)
171 self.d["stencilColsCC"] = len(solver.stencilCC[0])
172 numberOfCols = len(solver.stencilCC[0]) #use this to insert a line break after every row
173
174 #pass in stencil as a list of lists
175 #convert it to a flat list for passing as std::initializer_list
176 stencil=[item for sublist in solver.stencilCC for item in sublist]
177 self.d["STENCILCC"] = "\n{" + str(stencil[0])
178 for index, el in enumerate(stencil[1:]):
179 self.d["STENCILCC"] += ", "
180 if index > 0 and (index-1) % numberOfCols == 1:
181 self.d["STENCILCC"] += "\n"
182 self.d["STENCILCC"] += str(el)
183 self.d["STENCILCC"] += "}"
184
186 return f"""
187 _spacetreeId = treeNumber;
188"""
189
190
191 def get_body_of_operation(self, operation_name):
192 """!
193 for now, only touch face first time
194 """
195
196
197
198 result = ""
199 if operation_name==peano4.solversteps.ActionSet.OPERATION_TOUCH_CELL_FIRST_TIME:
200 result = jinja2.Template(self.TemplateInitCell).render(**self.d)
201 pass
202 return result
203
205 """!
206
207 Configure name of generated C++ action set
208
209 This action set will end up in the directory observers with a name that
210 reflects how the observer (initialisation) is mapped onto this action
211 set. The name pattern is ObserverName2ActionSetIdentifier where this
212 routine co-determines the ActionSetIdentifier. We make is reflect the
213 Python class name.
214
215 """
216 return __name__.replace(".py", "").replace(".", "_")
217
219 """!
220
221 The action set that Peano will generate that corresponds to this class
222 should not be modified by users and can safely be overwritten every time
223 we run the Python toolkit.
224
225 """
226 return False
227
228 def get_includes(self):
229 """!
230
231Consult petsc.Project for details
232
233"""
234 return """
235#include "tarch/la/Matrix.h"
236#include "repositories/SolverRepository.h"
237"""
238 def get_attributes(self):
239 """!
240 """
241
242 #construct the output strings for stencilsCF and stencilsFC
243 stencilsCF = '{' #start the std vector
244 for stencil in self.d["STENCILSCF"]:
245 stencilsCF += stencil
246 stencilsCF += ",\n"
247 stencilsCF += "}" #end the std vector
248 #same again
249 stencilsFC = '{' #start the std vector
250 for stencil in self.d["STENCILSFC"]:
251 stencilsFC += stencil
252 stencilsFC += ",\n"
253 stencilsFC += "}" #end the std vector
254
255 #also for stencilsFF
256 stencilsFF = '{' #start the std vector
257 for stencil in self.d["STENCILSFF"]:
258 stencilsFF += stencil
259 stencilsFF += ",\n"
260 stencilsFF += "}" #end the std vector
261
262
263 return f"""
264 int _spacetreeId;
265 const int _faceDofs = {self.d["FACE_CARDINALITY"]};
266 const int _cellDofs = {self.d["CELL_CARDINALITY"]};
267
268 //we need a std::vector for these now, since we have mutiple
269 const std::vector< tarch::la::Matrix<{self.d["stencilRowsFF"]},{self.d["stencilColsFF"]},double> > _stencilsFF =
270 {stencilsFF};
271
272 //we need a std::vector for these now, since we have mutiple
273 const std::vector< tarch::la::Matrix<{self.d["stencilRowsCF"]},{self.d["stencilColsCF"]},double> > _stencilsCF =
274 {stencilsCF};
275
276 //we need a std::vector for these now, since we have mutiple
277 const std::vector< tarch::la::Matrix<{self.d["stencilRowsFC"]},{self.d["stencilColsFC"]},double> > _stencilsFC =
278 {stencilsFC};
279
280 const tarch::la::Matrix<{self.d["stencilRowsCC"]},{self.d["stencilColsCC"]},double> _stencilCC = {self.d["STENCILCC"]};
281"""
user_should_modify_template(self)
The action set that Peano will generate that corresponds to this class should not be modified by user...
get_action_set_name(self)
Configure name of generated C++ action set.
get_body_of_operation(self, operation_name)
for now, only touch face first time
get_attributes(self)
Return attributes as copied and pasted into the generated class.
Action set (reactions to events)
Definition ActionSet.py:6