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