Peano
Loading...
Searching...
No Matches
PlotPatchesOverFacesInPeanoBlockFormat.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.api.solversteps.ActionSet import ActionSet
4
5import jinja2
6
7
9 """!
10
11 Plotter of face structure
12 @todo have not been implemented yet
13
14 """
15
16 def __init__(self,
17 filename,
18 patch,
19 dataset_name,
20 description, time_stamp_evaluation,
21 plot_cell_data=True,
22 metadata = "",
23 mapping = [],
24 guard="true",
25 additional_includes="",
26 precision=3,
27 select_dofs = None,
28 dataType="double"):
29 """!
30
31 Construct the block plotter
32
33 plot_cell_data: Boolean
34 Shall I plot cell data or vertex data. If you map the patches onto
35 vertex data, then I have to decrease the dofs pre axis, as we basically
36 plot the dual grid.
37
38 description: String
39
40 mapping: Series of d-tuples which describe how to distort quadrature/sampling
41 points within a reference cube/square. Can be empty alternatively.
42
43 select_dofs: [Int] or None
44 You can make the plotter only plot some of the dofs of interest
45
46 """
47 super(PlotPatchesOverFacesInPeanoBlockFormat,self).__init__(descend_invocation_order=1,parallel=False)
48
49 valid_datatypes = {"float", "double", "long double", "_Float16", "std::float16_t", "__bf16", "std::bfloat16_t"}
50
51 self._plot_cell_data = plot_cell_data
52
53 self.d = {}
54 self.d[ "FILENAME" ] = filename
55 self.d[ "GUARD_PREDICATE" ] = guard
56
57 self.additional_includes = additional_includes
58
59 dofs_per_axis = patch.dim[0]
60
61 self.d[ "DOFS_PER_AXIS" ] = str(dofs_per_axis)
62
63 if select_dofs==None:
64 self.d[ "PLOTTED_UNKNOWNS" ] = str(patch.no_of_unknowns)
65 else:
66 self.d[ "PLOTTED_UNKNOWNS" ] = str(len(select_dofs))
67 self.d[ "UNKNOWNS" ] = str(patch.no_of_unknowns)
68 self.d[ "SELECT_DOFS" ] = select_dofs
69
70 self.d[ "NAME" ] = dataset_name
71 self.d[ "DESCRIPTION" ] = description
72 self.d[ "METADATA" ] = metadata
73 if len(mapping)==0:
74 self.d[ "MAPPING_2D"] = "double* mapping = nullptr;"
75 self.d[ "MAPPING_3D"] = "double* mapping = nullptr;"
76 else:
77 counter = 0
78 self.d[ "MAPPING_2D"] = "double mapping[" + str(2*dofs_per_axis*dofs_per_axis) + "] = {"
79 self.d[ "MAPPING_3D"] = "double mapping[" + str(3*dofs_per_axis*dofs_per_axis*dofs_per_axis) + "] = {"
80 for i in mapping:
81 if counter>0:
82 separator = ", "
83 else:
84 separator = " "
85 if counter<dofs_per_axis*dofs_per_axis:
86 self.d[ "MAPPING_2D"] += separator + str(i[0])
87 self.d[ "MAPPING_2D"] += ", " + str(i[1])
88 self.d[ "MAPPING_2D"] += "\n"
89 self.d[ "MAPPING_3D"] += separator + str(i[0])
90 self.d[ "MAPPING_3D"] += ", " + str(i[1])
91 if len(i)>2:
92 self.d[ "MAPPING_3D"] += ", " + str(i[2])
93 else:
94 self.d[ "MAPPING_3D"] += ", 0.0"
95 self.d[ "MAPPING_3D"] += "\n"
96 counter += 1
97
98 self.d[ "MAPPING_2D"] += "};"
99 self.d[ "MAPPING_3D"] += "};"
100
101 for i in patch.dim:
102 if i!=patch.dim[0]:
103 print( "Error: patch plotter requires patch to have same dimension along all coordinate axes")
104
105 if dataType not in valid_datatypes:
106 raise Exception("Chosen datatype " + dataType + " for plotting of patches is not a recognized choice.")
107
108 self.d[ "PRECISION" ] = precision
109 self.d[ "DATATYPE" ] = dataType
110 self.d[ "TIMESTAMP" ] = time_stamp_evaluation
111
112
113 __Template_Constructor = """
114 _writer = nullptr;
115 _dataWriter = nullptr;
116 _treeNumber = treeNumber;
117
118 logDebug( "PlotGrid2PlotGridInPeanoBlockFormat1()", "created tree instance for " << treeNumber );
119"""
120
121
123 return self.__Template_Constructor.format(**self.d)
124
125
126 __Template_EndTraversal = """
127 assertion1( _dataWriter!=nullptr, _treeNumber );
128 assertion1( _writer!=nullptr, _treeNumber );
129
130 _dataWriter->close();
131 _writer->writeToFile();
132
133 delete _dataWriter;
134 delete _writer;
135
136 _dataWriter = nullptr;
137 _writer = nullptr;
138"""
139
140
142 return ""
143
144
146 return " return std::vector< peano4::grid::GridControlEvent >();\n"
147
148
150 return __name__.replace(".py", "").replace(".", "_")
151
152
154 return False
155
156
157 """!
158
159 Used if we have data associated with the cell centres of the small patch
160 associated with a face.
161
162 """
163 __Template_TouchFaceFirstTime_CellPlot = """
164 int vertexIndices[TwoPowerD];
165
166 const double PatchScaling = 1.0;
167
168 assertion( _writer!=nullptr );
169 assertion( _dataWriter!=nullptr );
170
171 const int patchIndex = _writer->plotPatch(
172 marker.x() - marker.h() * PatchScaling * 0.5,
173 marker.h() * PatchScaling
174 );
175
176 int cellIndex = _dataWriter->getFirstCellWithinPatch(patchIndex);
177 int currentDoF = 0;
178
179 dfor(k,{{DOFS_PER_AXIS}}) {
180 {% if SELECT_DOFS==None %}
181 {{DATATYPE}}* data = fineGridCell{{NAME}}.value + currentDoF;
182 {% else %}
183 {{DATATYPE}} data[] = {
184 fineGridCell{{NAME}}.value[ currentDoF + {{SELECT_DOFS[0]}} ]
185 {% for i in SELECT_DOFS[1:] %}
186 , fineGridCell{{NAME}}.value[ currentDoF + {{i}} ]
187 {% endfor %}
188 };
189 {% endif %}
190 _dataWriter->plotCell( cellIndex, data );
191 cellIndex++;
192 currentDoF += {{UNKNOWNS}};
193 }
194"""
195
196
197 """!
198
199 Used if we have data associated with the vertices of the small patch
200 associated with a face.
201
202 """
203 __Template_TouchFaceFirstTime_VertexPlot = """
204 int vertexIndices[TwoPowerD];
205
206 const double PatchScaling = 1.0;
207
208 assertion( _writer!=nullptr );
209 assertion( _dataWriter!=nullptr );
210
211 const int patchIndex = _writer->plotPatch(
212 marker.x() - marker.h() * PatchScaling * 0.5,
213 marker.h() * PatchScaling
214 );
215
216 int vertexIndex = _dataWriter->getFirstVertexWithinPatch(patchIndex);
217 int currentDoF = 0;
218
219 dfor(k,{{DOFS_PER_AXIS}}) {
220 {% if SELECT_DOFS==None %}
221 {{DATATYPE}}* data = fineGridCell{{NAME}}.value + currentDoF;
222 {% else %}
223 {{DATATYPE}} data[] = {
224 fineGridCell{{NAME}}.value[ currentDoF + {{SELECT_DOFS[0]}} ]
225 {% for i in SELECT_DOFS[1:] %}
226 , fineGridCell{{NAME}}.value[ currentDoF + {{i}} ]
227 {% endfor %}
228 };
229 {% endif %}
230 _dataWriter->plotVertex( vertexIndex, data );
231 vertexIndex++;
232 currentDoF += {{UNKNOWNS}};
233 }
234"""
235
236
237 __Template_BeginTraversal_Generic = """
238 tarch::mpi::Lock lock( _semaphore );
239
240 static int counter = -1;
241 counter++;
242
243 std::ostringstream snapshotFileName;
244 snapshotFileName << "{{FILENAME}}-" << counter;
245
246 if (tarch::mpi::Rank::getInstance().getNumberOfRanks()>1 ) {
247 snapshotFileName << "-rank-" << tarch::mpi::Rank::getInstance().getRank();
248 }
249
250 _writer = new tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter(
251 Dimensions, snapshotFileName.str(), "{{FILENAME}}",
252 tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter::IndexFileMode::AppendNewData,
253 {{TIMESTAMP}}
254 );
255
256 #if Dimensions==2
257 {{MAPPING_2D}}
258 #else
259 {{MAPPING_3D}}
260 #endif
261"""
262
263
264
265 __Template_BeginTraversal_CellPlot = __Template_BeginTraversal_Generic + """
266 _dataWriter = _writer->createCellDataWriter( "{{NAME}}", {{DOFS_PER_AXIS}}, {{PLOTTED_UNKNOWNS}}, "{{DESCRIPTION}}", "{{METADATA}}", mapping );
267 _dataWriter->setPrecision( {{PRECISION}} );
268"""
269
270
271 __Template_BeginTraversal_VertexPlot = __Template_BeginTraversal_Generic + """
272 _dataWriter = _writer->createVertexDataWriter( "{{NAME}}", {{DOFS_PER_AXIS}}, {{PLOTTED_UNKNOWNS}}, "{{DESCRIPTION}}", "{{METADATA}}", mapping );
273 _dataWriter->setPrecision( {{PRECISION}} );
274"""
275
276
277 def get_body_of_operation(self,operation_name):
278 result = "\n"
279 if operation_name==ActionSet.OPERATION_TOUCH_FACE_FIRST_TIME and self._plot_cell_data:
280 result = jinja2.Template( self.__Template_TouchFaceFirstTime_CellPlot ).render(**self.d)
281 if operation_name==ActionSet.OPERATION_TOUCH_FACE_FIRST_TIME and not self._plot_cell_data:
282 result = jinja2.Template( self.__Template_TouchFaceFirstTime_VertexPlot ).render(**self.d)
283 if operation_name==ActionSet.OPERATION_BEGIN_TRAVERSAL and self._plot_cell_data:
284 result = jinja2.Template( self.__Template_BeginTraversal_CellPlot ).render(**self.d)
285 if operation_name==ActionSet.OPERATION_BEGIN_TRAVERSAL and not self._plot_cell_data:
286 result = jinja2.Template( self.__Template_BeginTraversal_VertexPlot ).render(**self.d)
287 if operation_name==ActionSet.OPERATION_END_TRAVERSAL:
288 result = jinja2.Template( self.__Template_EndTraversal ).render(**self.d)
289 return result
290
291
292 def get_guard_of_operation(self,operation_name):
293 if operation_name==ActionSet.OPERATION_TOUCH_FACE_FIRST_TIME:
294 return self.d[ "GUARD_PREDICATE" ]
295 return "true"
296
297
298 def get_attributes(self):
299 if self._plot_cell_data:
300 return """
301 static tarch::mpi::BooleanSemaphore _semaphore;
302
303 int _treeNumber;
304
305 tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter* _writer;
306 tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter::CellDataWriter* _dataWriter;
307"""
308 else:
309 return """
310 static tarch::mpi::BooleanSemaphore _semaphore;
311
312 int _treeNumber;
313
314 tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter* _writer;
315 tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter::VertexDataWriter* _dataWriter;
316"""
317
318
319 def get_includes(self):
320 return """
321#include "tarch/plotter/griddata/blockstructured/PeanoTextPatchFileWriter.h"
322#include "tarch/multicore/Lock.h"
323#include "tarch/multicore/BooleanSemaphore.h"
324#include "tarch/mpi/Lock.h"
325#include "tarch/mpi/BooleanSemaphore.h"
326#include "peano4/utils/Loop.h"
327#include "peano4/parallel/SpacetreeSet.h"
328""" + self.additional_includes
329
330
331 def get_static_initialisations(self,full_qualified_classname):
332 return """
333tarch::mpi::BooleanSemaphore """ + full_qualified_classname + """::_semaphore;
334"""
335
get_body_of_operation(self, operation_name)
Return actual C++ code snippets to be inserted into C++ code.
get_body_of_createGridControlEventsForNextTraversal(self)
Return the grid control events that this action set wants to trigger.
get_attributes(self)
Return attributes as copied and pasted into the generated class.
__init__(self, filename, patch, dataset_name, description, time_stamp_evaluation, plot_cell_data=True, metadata="", mapping=[], guard="true", additional_includes="", precision=3, select_dofs=None, dataType="double")
Construct the block plotter.
Action set (reactions to events)
Definition ActionSet.py:6