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.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 if ( {{GUARD_PREDICATE}} ) {
165 int vertexIndices[TwoPowerD];
166
167 const double PatchScaling = 1.0;
168
169 assertion( _writer!=nullptr );
170 assertion( _dataWriter!=nullptr );
171
172 const int patchIndex = _writer->plotPatch(
173 marker.x() - marker.h() * PatchScaling * 0.5,
174 marker.h() * PatchScaling
175 );
176
177 int cellIndex = _dataWriter->getFirstCellWithinPatch(patchIndex);
178 int currentDoF = 0;
179
180 dfor(k,{{DOFS_PER_AXIS}}) {
181 {% if SELECT_DOFS==None %}
182 {{DATATYPE}}* data = fineGridCell{{NAME}}.value + currentDoF;
183 {% else %}
184 {{DATATYPE}} data[] = {
185 fineGridCell{{NAME}}.value[ currentDoF + {{SELECT_DOFS[0]}} ]
186 {% for i in SELECT_DOFS[1:] %}
187 , fineGridCell{{NAME}}.value[ currentDoF + {{i}} ]
188 {% endfor %}
189 };
190 {% endif %}
191 _dataWriter->plotCell( cellIndex, data );
192 cellIndex++;
193 currentDoF += {{UNKNOWNS}};
194 }
195 }
196"""
197
198
199 """!
200
201 Used if we have data associated with the vertices of the small patch
202 associated with a face.
203
204 """
205 __Template_TouchFaceFirstTime_VertexPlot = """
206 if ( {{GUARD_PREDICATE}} ) {
207 int vertexIndices[TwoPowerD];
208
209 const double PatchScaling = 1.0;
210
211 assertion( _writer!=nullptr );
212 assertion( _dataWriter!=nullptr );
213
214 const int patchIndex = _writer->plotPatch(
215 marker.x() - marker.h() * PatchScaling * 0.5,
216 marker.h() * PatchScaling
217 );
218
219 int vertexIndex = _dataWriter->getFirstVertexWithinPatch(patchIndex);
220 int currentDoF = 0;
221
222 dfor(k,{{DOFS_PER_AXIS}}) {
223 {% if SELECT_DOFS==None %}
224 {{DATATYPE}}* data = fineGridCell{{NAME}}.value + currentDoF;
225 {% else %}
226 {{DATATYPE}} data[] = {
227 fineGridCell{{NAME}}.value[ currentDoF + {{SELECT_DOFS[0]}} ]
228 {% for i in SELECT_DOFS[1:] %}
229 , fineGridCell{{NAME}}.value[ currentDoF + {{i}} ]
230 {% endfor %}
231 };
232 {% endif %}
233 _dataWriter->plotVertex( vertexIndex, data );
234 vertexIndex++;
235 currentDoF += {{UNKNOWNS}};
236 }
237 }
238"""
239
240
241 __Template_BeginTraversal_Generic = """
242 tarch::mpi::Lock lock( _semaphore );
243
244 static int counter = -1;
245 counter++;
246
247 std::ostringstream snapshotFileName;
248 snapshotFileName << "{{FILENAME}}-" << counter;
249
250 if (tarch::mpi::Rank::getInstance().getNumberOfRanks()>1 ) {
251 snapshotFileName << "-rank-" << tarch::mpi::Rank::getInstance().getRank();
252 }
253
254 _writer = new tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter(
255 Dimensions, snapshotFileName.str(), "{{FILENAME}}",
256 tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter::IndexFileMode::AppendNewData,
257 {{TIMESTAMP}}
258 );
259
260 #if Dimensions==2
261 {{MAPPING_2D}}
262 #else
263 {{MAPPING_3D}}
264 #endif
265"""
266
267
268
269 __Template_BeginTraversal_CellPlot = __Template_BeginTraversal_Generic + """
270 _dataWriter = _writer->createCellDataWriter( "{{NAME}}", {{DOFS_PER_AXIS}}, {{PLOTTED_UNKNOWNS}}, "{{DESCRIPTION}}", "{{METADATA}}", mapping );
271 _dataWriter->setPrecision( {{PRECISION}} );
272"""
273
274
275 __Template_BeginTraversal_VertexPlot = __Template_BeginTraversal_Generic + """
276 _dataWriter = _writer->createVertexDataWriter( "{{NAME}}", {{DOFS_PER_AXIS}}, {{PLOTTED_UNKNOWNS}}, "{{DESCRIPTION}}", "{{METADATA}}", mapping );
277 _dataWriter->setPrecision( {{PRECISION}} );
278"""
279
280
281 def get_body_of_operation(self,operation_name):
282 result = "\n"
283 if operation_name==ActionSet.OPERATION_TOUCH_FACE_FIRST_TIME and self._plot_cell_data:
284 result = jinja2.Template( self.__Template_TouchFaceFirstTime_CellPlot ).render(**self.d)
285 if operation_name==ActionSet.OPERATION_TOUCH_FACE_FIRST_TIME and not self._plot_cell_data:
286 result = jinja2.Template( self.__Template_TouchFaceFirstTime_VertexPlot ).render(**self.d)
287 if operation_name==ActionSet.OPERATION_BEGIN_TRAVERSAL and self._plot_cell_data:
288 result = jinja2.Template( self.__Template_BeginTraversal_CellPlot ).render(**self.d)
289 if operation_name==ActionSet.OPERATION_BEGIN_TRAVERSAL and not self._plot_cell_data:
290 result = jinja2.Template( self.__Template_BeginTraversal_VertexPlot ).render(**self.d)
291 if operation_name==ActionSet.OPERATION_END_TRAVERSAL:
292 result = jinja2.Template( self.__Template_EndTraversal ).render(**self.d)
293 return result
294
295
296 def get_attributes(self):
297 if self._plot_cell_data:
298 return """
299 static tarch::mpi::BooleanSemaphore _semaphore;
300
301 int _treeNumber;
302
303 tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter* _writer;
304 tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter::CellDataWriter* _dataWriter;
305"""
306 else:
307 return """
308 static tarch::mpi::BooleanSemaphore _semaphore;
309
310 int _treeNumber;
311
312 tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter* _writer;
313 tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter::VertexDataWriter* _dataWriter;
314"""
315
316
317 def get_includes(self):
318 return """
319#include "tarch/plotter/griddata/blockstructured/PeanoTextPatchFileWriter.h"
320#include "tarch/multicore/Lock.h"
321#include "tarch/multicore/BooleanSemaphore.h"
322#include "tarch/mpi/Lock.h"
323#include "tarch/mpi/BooleanSemaphore.h"
324#include "peano4/utils/Loop.h"
325#include "peano4/parallel/SpacetreeSet.h"
326""" + self.additional_includes
327
328
329 def get_static_initialisations(self,full_qualified_classname):
330 return """
331tarch::mpi::BooleanSemaphore """ + full_qualified_classname + """::_semaphore;
332"""
333
Action set (reactions to events)
Definition ActionSet.py:6
__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.