Peano
Loading...
Searching...
No Matches
PlotCellDataInPeanoBlockFormat.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
8
9#
10# Doxygen has some issues parsing this file. As I did not manage to find out
11# where these issues come from, I manually exclude this file from the
12# documentation in documentation/Doxyfile.
13#
14
15
17 """!
18
19 This writer writes a fixed number of unknown per cell
20
21 Originally, I had considered this plotter to be used if you have a Finite
22 Volume-type discretisation with N unknowns associated to a cell. However, it
23 turns out that there might be other use cases: People might want to add N
24 unknowns but actually specify that these unknowns are distributed in a certain
25 way. So you can alter the meta information, but per default it is set to one
26 vector of entries per cell. If you want to visualise patch data, I still
27 recommend to use the tailored patch data writer, which plays nicely together
28 with the Python data model.
29
30 """
31 NoMetaFile = "no-meta-file"
32 CountTimeSteps = "count-time-steps"
33 CastToDouble = -1
34
35 def __init__(self,
36 filename,
37 cell_unknown,
38 getter,
39 description,
40 time_stamp_evaluation,
41 number_of_unknows_per_cell=1,
42 guard_predicate="true",
43 additional_includes=""
44 ):
45 """!
46 Plot only the grid structure
47
48 filename: String
49 Name of the output file
50
51 vertex_unknown: (DaStGen) object tied to a vertex
52 The object you have associated with a vertex and that you want to print.
53 The code will access its name via
54
55 fineGridCell{CELL_UNKNOWN_NAME}
56
57 in the generated source code
58
59 getter: String (C++ code)
60 Getter acting on the vertex. Could be something alike getU() for example.
61 If there's no getter but you want to directly access the data, remove
62 any brackets from the passed string.
63
64 time_stamp_evaluation: String
65 C++ expression returning a double. This is used to write the time series
66 file for a series of snapshots. Pass in "0" if you solve a stationary
67 problem, e.g.
68
69 number_of_unknows_per_cell: Integer
70 Has to match to the getter. You can hand in a CastToDouble which instructs the
71 plotter to explicitly cast the entry to double.
72 """
73 super( PlotCellDataInPeanoBlockFormat, self ).__init__()
74
75 self.d = {}
76 self.d[ "FILENAME" ] = filename
77 self.d[ "CELL_UNKNOWN_NAME" ] = cell_unknown.name
78 self.d[ "GETTER" ] = getter
79 self.d[ "NUMBER_OF_UNKNOWNS_PER_CELL" ] = number_of_unknows_per_cell
80 self.d[ "DESCRIPTION" ] = description
81 self.d[ "TIMESTAMP" ] = time_stamp_evaluation
82 self.d[ "GUARD_PREDICATE" ] = guard_predicate
83 self.d[ "META_DATA" ] = ""
84 self.additional_includes = additional_includes
85
86
87 __Template_Constructor = """
88 _writer = nullptr;
89 _dataWriter = nullptr;
90 _treeNumber = treeNumber;
91
92 // An MPI lock (critical section) would be important!
93
94 logDebug( "PlotGrid2PlotGridInPeanoBlockFormat1()", "created tree instance for " << treeNumber );
95"""
96
97
99 return self.__Template_Constructor.format(**self.d)
100
101
102 __Template_EndTraversal = """
103 assertion(_dataWriter!=nullptr);
104 assertion1(_dataWriter!=nullptr,_treeNumber);
105
106 _dataWriter->close();
107 _writer->writeToFile();
108
109 delete _dataWriter;
110 delete _writer;
111
112 _dataWriter = nullptr;
113 _writer = nullptr;
114"""
115
116
118 return ""
119
120
122 return " return std::vector< peano4::grid::GridControlEvent >();\n"
123
124
126 return __name__.replace(".py", "").replace(".", "_")
127
128
130 return False
131
132
133 Template_TouchCellFirstTime = """
134 int cellIndex = _writer->plotPatch(
135 marker.x()-0.5 * marker.h(),
136 marker.h()
137 );
138
139 {% if NUMBER_OF_UNKNOWNS_PER_CELL<0 %}
140 double data = static_cast<double>(fineGridCell{{CELL_UNKNOWN_NAME}}.{{GETTER}});
141 {% else %}
142 auto data = fineGridCell{{CELL_UNKNOWN_NAME}}.{{GETTER}};
143 {% endif %}
144 _dataWriter->plotCell( cellIndex, data );
145"""
146
147
148 __Template_BeginTraversal = """
149 tarch::mpi::Lock lock( _semaphore );
150
151 static int counter = -1;
152 counter++;
153
154 std::ostringstream snapshotFileName;
155 snapshotFileName << "{{FILENAME}}-" << counter;
156
157 if (tarch::mpi::Rank::getInstance().getNumberOfRanks()>1 ) {
158 snapshotFileName << "-rank-" << tarch::mpi::Rank::getInstance().getRank();
159 }
160
161 {% if TIMESTAMP==\"""" + NoMetaFile + """\" %}
162 _writer = new tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter(
163 Dimensions, snapshotFileName.str(), "{{FILENAME}}",
164 tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter::IndexFileMode::NoIndexFile,
165 0.0
166 );
167 {% elif TIMESTAMP==\"""" + CountTimeSteps + """\" %}
168 static int timeStep = -1;
169 timeStep++;
170 _writer = new tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter(
171 Dimensions, snapshotFileName.str(), "{{FILENAME}}",
172 tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter::IndexFileMode::AppendNewData,
173 timeStep
174 );
175 {% else %}
176 _writer = new tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter(
177 Dimensions, snapshotFileName.str(), "{{FILENAME}}",
178 tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter::IndexFileMode::AppendNewData,
179 {{TIMESTAMP}}
180 );
181 {% endif %}
182
183 _dataWriter = _writer->createCellDataWriter(
184 "{{CELL_UNKNOWN_NAME}}",
185 1, // nodes per axis
186 {% if NUMBER_OF_UNKNOWNS_PER_CELL<0 %}
187 1, // records per cell
188 {% else %}
189 {{NUMBER_OF_UNKNOWNS_PER_CELL}},
190 {% endif %}
191 "{{DESCRIPTION}}",
192 "{{META_DATA}}"
193 );
194"""
195
196
197 def get_body_of_operation(self,operation_name):
198 result = "\n"
199 if operation_name==ActionSet.OPERATION_TOUCH_CELL_FIRST_TIME:
200 result = jinja2.Template( self.Template_TouchCellFirstTime).render(**self.d)
201 if operation_name==ActionSet.OPERATION_BEGIN_TRAVERSAL:
202 result = jinja2.Template( self.__Template_BeginTraversal).render(**self.d)
203 if operation_name==ActionSet.OPERATION_END_TRAVERSAL:
204 result = jinja2.Template( self.__Template_EndTraversal).render(**self.d)
205 return result
206
207
208 def get_guard_of_operation(self,operation_name):
209 if operation_name==ActionSet.OPERATION_TOUCH_CELL_FIRST_TIME:
210 return self.d[ "GUARD_PREDICATE" ]
211 return "true"
212
213
214 def get_attributes(self):
215 return """
216 static tarch::mpi::BooleanSemaphore _semaphore;
217
218 int _treeNumber;
219
220 tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter* _writer;
221 tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter::CellDataWriter* _dataWriter;
222"""
223
224
225 def get_includes(self):
226 return """
227#include "tarch/plotter/griddata/blockstructured/PeanoTextPatchFileWriter.h"
228#include "tarch/mpi/Lock.h"
229#include "tarch/mpi/BooleanSemaphore.h"
230#include "tarch/multicore/Lock.h"
231#include "tarch/multicore/BooleanSemaphore.h"
232#include "peano4/utils/Loop.h"
233#include "peano4/parallel/SpacetreeSet.h"
234""" + self.additional_includes
235
236
237 def get_static_initialisations(self,full_qualified_classname):
238 return """
239tarch::mpi::BooleanSemaphore """ + full_qualified_classname + """::_semaphore;
240"""
Action set (reactions to events)
Definition ActionSet.py:6
get_body_of_createGridControlEventsForNextTraversal(self)
Return the grid control events that this action set wants to trigger.
__init__(self, filename, cell_unknown, getter, description, time_stamp_evaluation, number_of_unknows_per_cell=1, guard_predicate="true", additional_includes="")
Plot only the grid structure.
get_body_of_operation(self, operation_name)
Return actual C++ code snippets to be inserted into C++ code.
get_attributes(self)
Return attributes as copied and pasted into the generated class.