13 Very simple plotter that should be used in combination with Patches.
14 Patches with varying datatypes are supported via the "dataType" parameter,
15 but this defaults to doubles. This parameter should contain the C-style-
16 name of the type of the variable in the patch, e.g. float, double, etc.
17 This plotter works only for cell patches, i.e. patches associated with
18 faces or vertices are not supported.
20 At the moment, I can only plot one dataset (patch type) per plotter.
21 In theory, the underlying Peano plotter however could dump multiple
24 Also, this dump expects incoming data to be AoS.
31 description, time_stamp_evaluation,
36 additional_includes="",
40 restart_preprocess=False,
42 metadata_evaluation=None,
46 Construct the block plotter
48 plot_cell_data: Boolean
49 Shall I plot cell data or vertex data. If you map the patches onto
50 vertex data, then I have to decrease the dofs pre axis, as we basically
55 mapping: Series of d-tuples which describe how to distort quadrature/sampling
56 points within a reference cube/square. Can be empty alternatively.
59 Number of significant digits of the plotted values. The writer
60 does not round or set small values to zero, so 17 writes the doubles
61 without loss, e.g. for a checkpoint. Patch offsets and sizes use at
63 (@ref tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter).
65 select_dofs: [Int] or None
66 You can make the plotter only plot some of the dofs of interest
69 Factor by which the plotted patch's box is scaled relative to the
70 cell, around the cell's centre. 1.0 plots the patch over the cell.
71 A patch with N dofs per axis plus a halo of M dofs on each side
72 uses (N+2M)/N, so every halo value sits at its true coordinate.
74 metadata_evaluation: str or None
75 C++ expression which yields a std::string at runtime. If set, its
76 value replaces metadata in the meta-data block of the file header.
77 Use it for data which is only known when the file is written, such
81 Write the plotted values as raw doubles
82 (@ref tarch::plotter::griddata::blockstructured::PeanoBinaryPatchFileWriter)
84 (@ref tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter).
85 The file header and the metadata remain text in either case, and the
86 file header says which of the two formats the file has. A binary
87 file is smaller and bit-exact, i.e. precision then only affects the
88 patch offsets and sizes.
91 super(PlotPatchesInPeanoBlockFormat,self).
__init__(descend_invocation_order=1,parallel=
False)
93 valid_datatypes = {
"float",
"double",
"long double",
"_Float16",
"std::float16_t",
"__bf16",
"std::bfloat16_t"}
98 self.
d[
"FILENAME" ] = filename
99 self.
d[
"GUARD_PREDICATE" ] = guard
103 dofs_per_axis = patch.dim[0]
105 self.
d[
"DOFS_PER_AXIS" ] = str(dofs_per_axis)
107 if select_dofs==
None:
108 self.
d[
"PLOTTED_UNKNOWNS" ] = str(patch.no_of_unknowns)
110 self.
d[
"PLOTTED_UNKNOWNS" ] = str(len(select_dofs))
111 self.
d[
"UNKNOWNS" ] = str(patch.no_of_unknowns)
112 self.
d[
"SELECT_DOFS" ] = select_dofs
114 self.
d[
"NAME" ] = dataset_name
115 self.
d[
"DESCRIPTION" ] = description
116 self.
d[
"METADATA" ] = metadata
118 self.
d[
"MAPPING_2D"] =
"double* mapping = nullptr;"
119 self.
d[
"MAPPING_3D"] =
"double* mapping = nullptr;"
122 self.
d[
"MAPPING_2D"] =
"double mapping[" + str(2*dofs_per_axis*dofs_per_axis) +
"] = {"
123 self.
d[
"MAPPING_3D"] =
"double mapping[" + str(3*dofs_per_axis*dofs_per_axis*dofs_per_axis) +
"] = {"
129 if counter<dofs_per_axis*dofs_per_axis:
130 self.
d[
"MAPPING_2D"] += separator + str(i[0])
131 self.
d[
"MAPPING_2D"] +=
", " + str(i[1])
132 self.
d[
"MAPPING_2D"] +=
"\n"
133 self.
d[
"MAPPING_3D"] += separator + str(i[0])
134 self.
d[
"MAPPING_3D"] +=
", " + str(i[1])
136 self.
d[
"MAPPING_3D"] +=
", " + str(i[2])
138 self.
d[
"MAPPING_3D"] +=
", 0.0"
139 self.
d[
"MAPPING_3D"] +=
"\n"
142 self.
d[
"MAPPING_2D"] +=
"};"
143 self.
d[
"MAPPING_3D"] +=
"};"
147 print(
"Error: patch plotter requires patch to have same dimension along all coordinate axes")
149 if dataType
not in valid_datatypes:
150 raise Exception(
"Chosen datatype " + dataType +
" for plotting of patches is not a recognized choice.")
152 self.
d[
"PRECISION" ] = precision
153 self.
d[
"WRITER_CLASS" ] =
"PeanoBinaryPatchFileWriter" if binary
else "PeanoTextPatchFileWriter"
154 self.
d[
"DATATYPE" ] = dataType
155 self.
d[
"TIMESTAMP" ] = time_stamp_evaluation
156 self.
d[
"PATCH_SCALING" ] = patch_scaling
157 self.
d[
"METADATA_EVALUATION" ] = metadata_evaluation
160 self.
d[
"WRITER_PRECISION" ] = max(precision, 6)
165 __Template_Constructor =
"""
167 _dataWriter = nullptr;
168 _treeNumber = treeNumber;
170 logDebug( "PlotGrid2PlotGridInPeanoBlockFormat1()", "created tree instance for " << treeNumber );
178 __Template_EndTraversal =
"""
179 assertion1( _dataWriter!=nullptr, _treeNumber );
180 assertion1( _writer!=nullptr, _treeNumber );
182 _dataWriter->close();
183 _writer->writeToFile();
188 _dataWriter = nullptr;
198 return " return std::vector< peano4::grid::GridControlEvent >();\n"
202 return __name__.replace(
".py",
"").replace(
".",
"_")
209 __Template_TouchCellFirstTime_CellPlot =
"""
210 int vertexIndices[TwoPowerD];
212 const double PatchScaling = {{PATCH_SCALING}};
214 assertion( _writer!=nullptr );
215 assertion( _dataWriter!=nullptr );
217 const int patchIndex = _writer->plotPatch(
218 marker.x() - marker.h() * PatchScaling * 0.5,
219 marker.h() * PatchScaling
222 int cellIndex = _dataWriter->getFirstCellWithinPatch(patchIndex);
225 dfor(k,{{DOFS_PER_AXIS}}) {
226 {% if SELECT_DOFS==None %}
227 {{DATATYPE}}* data = fineGridCell{{NAME}}.value + currentDoF;
229 {{DATATYPE}} data[] = {
230 fineGridCell{{NAME}}.value[ currentDoF + {{SELECT_DOFS[0]}} ]
231 {% for i in SELECT_DOFS[1:] %}
232 , fineGridCell{{NAME}}.value[ currentDoF + {{i}} ]
236 _dataWriter->plotCell( cellIndex, data );
238 currentDoF += {{UNKNOWNS}};
245 __Template_TouchCellFirstTime_VertexPlot =
"""
246 int vertexIndices[TwoPowerD];
248 const double PatchScaling = {{PATCH_SCALING}};
250 assertion( _writer!=nullptr );
251 assertion( _dataWriter!=nullptr );
253 const int patchIndex = _writer->plotPatch(
254 marker.x() - marker.h() * PatchScaling * 0.5,
255 marker.h() * PatchScaling
258 int vertexIndex = _dataWriter->getFirstVertexWithinPatch(patchIndex);
261 dfor(k,{{DOFS_PER_AXIS}}) {
262 {% if SELECT_DOFS==None %}
263 {{DATATYPE}}* data = fineGridCell{{NAME}}.value + currentDoF;
265 {{DATATYPE}} data[] = {
266 fineGridCell{{NAME}}.value[ currentDoF + {{SELECT_DOFS[0]}} ]
267 {% for i in SELECT_DOFS[1:] %}
268 , fineGridCell{{NAME}}.value[ currentDoF + {{i}} ]
272 _dataWriter->plotVertex( vertexIndex, data );
274 currentDoF += {{UNKNOWNS}};
278 __Template_BeginTraversal_Generic =
"""
279 std::ostringstream snapshotFileName;
280 snapshotFileName << "{{FILENAME}}-" << counter;
282 if (tarch::mpi::Rank::getInstance().getNumberOfRanks()>1 ) {
283 snapshotFileName << "-rank-" << tarch::mpi::Rank::getInstance().getRank();
286 _writer = new tarch::plotter::griddata::blockstructured::{{WRITER_CLASS}}(
287 Dimensions, snapshotFileName.str(), "{{FILENAME}}",
288 tarch::plotter::griddata::blockstructured::{{WRITER_CLASS}}::IndexFileMode::AppendNewData,
300 __Template_BeginTraversal_Generic_Restart =
"""
301 tarch::mpi::Lock lock( _semaphore );
303 static int counter = -1;
306 static bool preprocessFinished=false;
307 if (not preprocessFinished){
308 if ( tarch::mpi::Rank::getInstance().isGlobalMaster()){
309 counter=tarch::plotter::griddata::blockstructured::restartPreprocess("{{FILENAME}}", CheckpointTimeStamp);
311 MPI_Bcast(&counter, 1, MPI_INT, 0, MPI_COMM_WORLD);
312 preprocessFinished=true;
315"""+__Template_BeginTraversal_Generic
317 __Template_BeginTraversal_Generic =
"""
318 tarch::mpi::Lock lock( _semaphore );
320 static int counter = -1;
323"""+__Template_BeginTraversal_Generic
326 __Template_BeginTraversal_CellPlot = __Template_BeginTraversal_Generic +
"""
327 _dataWriter = _writer->createCellDataWriter( "{{NAME}}", {{DOFS_PER_AXIS}}, {{PLOTTED_UNKNOWNS}}, "{{DESCRIPTION}}", {% if METADATA_EVALUATION %}{{METADATA_EVALUATION}}{% else %}"{{METADATA}}"{% endif %}, mapping );
328 _dataWriter->setPrecision( {{PRECISION}} );
330 __Template_BeginTraversal_CellPlot_Restart = __Template_BeginTraversal_Generic_Restart +
"""
331 _dataWriter = _writer->createCellDataWriter( "{{NAME}}", {{DOFS_PER_AXIS}}, {{PLOTTED_UNKNOWNS}}, "{{DESCRIPTION}}", {% if METADATA_EVALUATION %}{{METADATA_EVALUATION}}{% else %}"{{METADATA}}"{% endif %}, mapping );
332 _dataWriter->setPrecision( {{PRECISION}} );
335 __Template_BeginTraversal_VertexPlot = __Template_BeginTraversal_Generic +
"""
336 _dataWriter = _writer->createVertexDataWriter( "{{NAME}}", {{DOFS_PER_AXIS}}, {{PLOTTED_UNKNOWNS}}, "{{DESCRIPTION}}", {% if METADATA_EVALUATION %}{{METADATA_EVALUATION}}{% else %}"{{METADATA}}"{% endif %}, mapping );
337 _dataWriter->setPrecision( {{PRECISION}} );
339 __Template_BeginTraversal_VertexPlot_Restart = __Template_BeginTraversal_Generic_Restart +
"""
340 _dataWriter = _writer->createVertexDataWriter( "{{NAME}}", {{DOFS_PER_AXIS}}, {{PLOTTED_UNKNOWNS}}, "{{DESCRIPTION}}", {% if METADATA_EVALUATION %}{{METADATA_EVALUATION}}{% else %}"{{METADATA}}"{% endif %}, mapping );
341 _dataWriter->setPrecision( {{PRECISION}} );
346 if operation_name==ActionSet.OPERATION_TOUCH_CELL_FIRST_TIME
and self.
_plot_cell_data:
348 if operation_name==ActionSet.OPERATION_TOUCH_CELL_FIRST_TIME
and not self.
_plot_cell_data:
351 if operation_name==ActionSet.OPERATION_BEGIN_TRAVERSAL
and self.
_plot_cell_data:
357 if operation_name==ActionSet.OPERATION_BEGIN_TRAVERSAL
and not self.
_plot_cell_data:
363 if operation_name==ActionSet.OPERATION_END_TRAVERSAL:
369 if operation_name==ActionSet.OPERATION_TOUCH_CELL_FIRST_TIME:
370 return self.
d[
"GUARD_PREDICATE" ]
375 writer =
"tarch::plotter::griddata::blockstructured::" + self.
d[
"WRITER_CLASS"]
376 data_writer =
"CellDataWriter" if self.
_plot_cell_data else "VertexDataWriter"
378 static tarch::mpi::BooleanSemaphore _semaphore;
382 """ + writer +
"""* _writer;
383 """ + writer +
"::" + data_writer +
"""* _dataWriter;
389#include "tarch/plotter/griddata/blockstructured/PeanoTextPatchFileWriter.h"
390#include "tarch/plotter/griddata/blockstructured/PeanoBinaryPatchFileWriter.h"
391#include "tarch/multicore/Lock.h"
392#include "tarch/multicore/BooleanSemaphore.h"
393#include "tarch/mpi/Lock.h"
394#include "tarch/mpi/BooleanSemaphore.h"
395#include "peano4/utils/Loop.h"
396#include "peano4/parallel/SpacetreeSet.h"
402tarch::mpi::BooleanSemaphore """ + full_qualified_classname +
"""::_semaphore;
Action set (reactions to events)