Peano
Loading...
Searching...
No Matches
PlotVertexDataInPeanoBlockFormat.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# Doxygen has some issues parsing this file. As I did not manage to find out
10# where these issues come from, I manually exclude this file from the
11# documentation in documentation/Doxyfile.
12#
13
14
16 NoMetaFile = "no-meta-file"
17 CountTimeSteps = "count-time-steps"
18
19 def __init__(self,
20 filename,
21 vertex_unknown,
22 getter,description,
23 time_stamp_evaluation,
24 number_of_unknows_per_vertex=1,
25 guard_predicate="true",
26 additional_includes=""
27 ):
28 """!
29
30 Plot grid and assume all data are associated with vertices
31
32 filename: String
33 Name of the output file
34
35 vertex_unknown: (DaStGen) object tied to a vertex
36 The object you have associated with a vertex and that you want to print.
37 The code will access its name via
38
39 fineGridVertices{VERTEX_UNKNOWN_NAME}
40
41 in the generated source code
42
43 getter: String (C++ code)
44 Getter acting on the vertex. Could be something alike getU() for example.
45 If there's no getter but you want to directly access the data, remove
46 any brackets from the passed string. Please note that Peano's plotter
47 routines usually require plain value (double) pointers. So if a string
48 like
49
50 getU()
51
52 returns a tarch vector, you have to pass in
53
54 getU().data()
55
56 to make the getter compatible with plotters' interface.
57
58 time_stamp_evaluation: String
59 C++ expression returning a double. This is used to write the time series
60 file for a series of snapshots. Pass in "0" if you solve a stationary
61 problem, e.g. You can also hand in the constants NoMetaFile or
62 CountTimeSteps.
63
64 number_of_unknows_per_vertex: Integer
65 Has to match to the getter.
66
67 """
68 super( PlotVertexDataInPeanoBlockFormat, self ).__init__()
69
70 self.d = {}
71 self.d[ "FILENAME" ] = filename
72 self.d[ "VERTEX_UNKNOWN_NAME" ] = vertex_unknown.name
73 self.d[ "GETTER" ] = getter
74 self.d[ "NUMBER_OF_UNKNOWNS_PER_VERTEX" ] = number_of_unknows_per_vertex
75 self.d[ "DESCRIPTION" ] = description
76 self.d[ "TIMESTAMP" ] = time_stamp_evaluation
77 self.d[ "GUARD_PREDICATE" ] = guard_predicate
78 self.additional_includes = additional_includes
79
80
81 __Template_Constructor = """
82 _writer = nullptr;
83 _dataWriter = nullptr;
84 _treeNumber = treeNumber;
85
86 // An MPI lock (critical section) would be important!
87
88 logDebug( "PlotGrid2PlotGridInPeanoBlockFormat1()", "created tree instance for " << treeNumber );
89"""
90
91
93 return self.__Template_Constructor.format(**self.d)
94
95
96 __Template_EndTraversal = """
97 assertion(_dataWriter!=nullptr);
98 assertion1(_dataWriter!=nullptr,_treeNumber);
99
100 _dataWriter->close();
101 _writer->writeToFile();
102
103 delete _dataWriter;
104 delete _writer;
105
106 _dataWriter = nullptr;
107 _writer = nullptr;
108"""
109
110
112 return ""
113
114
116 return " return std::vector< peano4::grid::GridControlEvent >();\n"
117
118
120 return __name__.replace(".py", "").replace(".", "_")
121
122
124 return False
125
126
127 __Template_TouchCellFirstTime = """
128 int vertexIndices[TwoPowerD];
129
130 int patchIndex = _writer->plotPatch(
131 marker.x()-0.5 * marker.h(),
132 marker.h()
133 );
134
135 assertion( _dataWriter!=nullptr );
136 int vertexIndex = _dataWriter->getFirstVertexWithinPatch(patchIndex);
137 dfor2(k)
138 auto data = fineGridVertices{{VERTEX_UNKNOWN_NAME}}(kScalar).{{GETTER}};
139 _dataWriter->plotVertex( vertexIndex, data );
140 vertexIndex++;
141 enddforx
142"""
143
144
145 __Template_BeginTraversal = """
146 tarch::mpi::Lock lock( _semaphore );
147
148 static int counter = -1;
149 counter++;
150
151 std::ostringstream snapshotFileName;
152 snapshotFileName << "{{FILENAME}}-" << counter;
153
154 if (tarch::mpi::Rank::getInstance().getNumberOfRanks()>1 ) {
155 snapshotFileName << "-rank-" << tarch::mpi::Rank::getInstance().getRank();
156 }
157
158 {% if TIMESTAMP==\"""" + NoMetaFile + """\" %}
159 _writer = new tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter(
160 Dimensions, snapshotFileName.str(), "{{FILENAME}}",
161 tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter::IndexFileMode::NoIndexFile,
162 0.0
163 );
164 {% elif TIMESTAMP==\"""" + CountTimeSteps + """\" %}
165 static int timeStep = -1;
166 timeStep++;
167 _writer = new tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter(
168 Dimensions, snapshotFileName.str(), "{{FILENAME}}",
169 tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter::IndexFileMode::AppendNewData,
170 timeStep
171 );
172 {% else %}
173 _writer = new tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter(
174 Dimensions, snapshotFileName.str(), "{{FILENAME}}",
175 tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter::IndexFileMode::AppendNewData,
176 {{TIMESTAMP}}
177 );
178 {% endif %}
179
180 _dataWriter = _writer->createVertexDataWriter(
181 "{{VERTEX_UNKNOWN_NAME}}",
182 2, // number of dofs per axis per cell
183 {{NUMBER_OF_UNKNOWNS_PER_VERTEX}},
184 "{{DESCRIPTION}}"
185 );
186"""
187
188
189 def get_body_of_operation(self,operation_name):
190 result = "\n"
191 if operation_name==ActionSet.OPERATION_TOUCH_CELL_FIRST_TIME:
192 result = jinja2.Template( self.__Template_TouchCellFirstTime).render(**self.d)
193 if operation_name==ActionSet.OPERATION_BEGIN_TRAVERSAL:
194 result = jinja2.Template( self.__Template_BeginTraversal).render(**self.d)
195 if operation_name==ActionSet.OPERATION_END_TRAVERSAL:
196 result = jinja2.Template( self.__Template_EndTraversal).render(**self.d)
197 return result
198
199
200 def get_guard_of_operation(self,operation_name):
201 if operation_name==ActionSet.OPERATION_TOUCH_CELL_FIRST_TIME:
202 return self.d[ "GUARD_PREDICATE" ]
203 return "true"
204
205
206 def get_attributes(self):
207 return """
208 static tarch::mpi::BooleanSemaphore _semaphore;
209
210 int _treeNumber;
211
212 tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter* _writer;
213 tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter::VertexDataWriter* _dataWriter;
214"""
215
216
217 def get_includes(self):
218 return """
219#include "tarch/plotter/griddata/blockstructured/PeanoTextPatchFileWriter.h"
220#include "tarch/mpi/Lock.h"
221#include "tarch/mpi/BooleanSemaphore.h"
222#include "tarch/multicore/Lock.h"
223#include "tarch/multicore/BooleanSemaphore.h"
224#include "peano4/utils/Loop.h"
225#include "peano4/parallel/SpacetreeSet.h"
226""" + self.additional_includes
227
228
229 def get_static_initialisations(self,full_qualified_classname):
230 return """
231tarch::mpi::BooleanSemaphore """ + full_qualified_classname + """::_semaphore;
232"""
233
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.
get_attributes(self)
Return attributes as copied and pasted into the generated class.
__init__(self, filename, vertex_unknown, getter, description, time_stamp_evaluation, number_of_unknows_per_vertex=1, guard_predicate="true", additional_includes="")
Plot grid and assume all data are associated with vertices.
get_body_of_operation(self, operation_name)
Return actual C++ code snippets to be inserted into C++ code.