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