Peano
Loading...
Searching...
No Matches
PlotGridInPeanoBlockFormat.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
5
6import jinja2
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 NoMetaFile = "no-meta-file"
18 CountTimeSteps = "count-time-steps"
19
20 def __init__(self, filename, cell_unknown=None, time_stamp_evaluation=NoMetaFile, guard_predicate="true", additional_includes=""):
21 """
22 Plot only the grid structure
23
24 :Attibutes:
25
26 filename: String
27 Name of the output file.
28
29 cell_unknown: None or cell data
30 If you use cell unknowns, pass any unknown in. As we do not dump
31 any semantic information about unknowns, it does not matter which
32 one you choose. If you don't have cell unknowns at all, pass in
33 None.
34
35 time_stamp_evaluation: String
36 C++ expression returning a double. You can also hand in NoMetaFile
37 or CountTimeSteps.
38
39 """
40 super( PlotGridInPeanoBlockFormat, self ).__init__()
41
42 self.d = {}
43 self.d[ "FILENAME" ] = filename
44 self.d[ "CELL_WRAPPER" ] = "Cell"
45 if cell_unknown!=None:
46 self.d[ "CELL_WRAPPER" ] += cell_unknown.name
47 self.d[ "GUARD_PREDICATE" ] = guard_predicate
48 self.additional_includes = additional_includes
49 self.d[ "TIMESTAMP" ] = time_stamp_evaluation
50
51
52 __Template_Constructor = """
53 _writer = nullptr;
54 _dataWriter = nullptr;
55 _treeNumber = treeNumber;
56
57 // An MPI lock (critical section) would be important!
58
59 logDebug( "PlotGrid2PlotGridInPeanoBlockFormat1()", "created tree instance for " << treeNumber );
60"""
61
62
64 return self.__Template_Constructor.format(**self.d)
65
66
67 __Template_EndTraversal = jinja2.Template("""
68 assertion1(_dataWriter!=nullptr,_treeNumber);
69
70 _dataWriter->close();
71 _writer->writeToFile();
72
73 delete _dataWriter;
74 delete _writer;
75
76 _dataWriter = nullptr;
77 _writer = nullptr;
78""")
79
80
82 return ""
83
84
86 return " return std::vector< peano4::grid::GridControlEvent >();\n"
87
88
90 return __name__.replace(".py", "").replace(".", "_")
91
92
94 return False
95
96
97 __Template_TouchCellFirstTime = jinja2.Template("""
98 int vertexIndices[TwoPowerD];
99
100 int indices = _writer->plotPatch(
101 marker.x() - marker.h() * 0.5,
102 marker.h()
103 );
104
105 assertion( _dataWriter!=nullptr );
106
107 double markerData[] = {
108 marker.hasBeenRefined() ? 1.0 : 0.0,
109 marker.willBeRefined() ? 1.0 : 0.0,
110 marker.isLocal() ? 1.0 : 0.0,
111 marker.hasBeenEnclaveCell() ? 1.0 : 0.0,
112 marker.willBeEnclaveCell() ? 1.0 : 0.0
113 };
114 _dataWriter->plotCell(indices,markerData);
115""")
116
117
118 __Template_BeginTraversal = jinja2.Template("""
119 tarch::mpi::Lock lock( _semaphore );
120
121 static int counter = -1;
122 counter++;
123
124 std::ostringstream snapshotFileName;
125 snapshotFileName << "{{FILENAME}}-" << counter;
126
127 if (tarch::mpi::Rank::getInstance().getNumberOfRanks()>1 ) {
128 snapshotFileName << "-rank-" << tarch::mpi::Rank::getInstance().getRank();
129 }
130
131 {% if TIMESTAMP==\"""" + NoMetaFile + """\" %}
132 _writer = new tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter(
133 Dimensions, snapshotFileName.str(), "{{FILENAME}}",
134 tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter::IndexFileMode::NoIndexFile,
135 0.0
136 );
137 {% elif TIMESTAMP==\"""" + CountTimeSteps + """\" %}
138 static int timeStep = -1;
139 timeStep++;
140 _writer = new tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter(
141 Dimensions, snapshotFileName.str(), "{{FILENAME}}",
142 tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter::IndexFileMode::AppendNewData,
143 timeStep
144 );
145 {% else %}
146 _writer = new tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter(
147 Dimensions, snapshotFileName.str(), "{{FILENAME}}",
148 tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter::IndexFileMode::AppendNewData,
149 {{TIMESTAMP}}
150 );
151 {% endif %}
152
153 _dataWriter = _writer->createCellDataWriter( "cell-marker", 1, 5, "is-refined,has-been-refined,local,enclave" );
154""")
155
156
157 def get_body_of_operation(self,operation_name):
158 result = "\n"
159 if operation_name==ActionSet.OPERATION_TOUCH_CELL_FIRST_TIME:
160 result = self.__Template_TouchCellFirstTime.render(**self.d)
161 if operation_name==ActionSet.OPERATION_BEGIN_TRAVERSAL:
162 result = self.__Template_BeginTraversal.render(**self.d)
163 if operation_name==ActionSet.OPERATION_END_TRAVERSAL:
164 result = self.__Template_EndTraversal.render(**self.d)
165 return result
166
167
168 def get_guard_of_operation(self,operation_name):
169 if operation_name==ActionSet.OPERATION_TOUCH_CELL_FIRST_TIME:
170 return self.d[ "GUARD_PREDICATE" ]
171 return "true"
172
173
174 def get_attributes(self):
175 return """
176 static tarch::mpi::BooleanSemaphore _semaphore;
177
178 int _treeNumber;
179
180 tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter* _writer;
181 tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter::CellDataWriter* _dataWriter;
182"""
183
184
185 def get_includes(self):
186 return """
187#include "tarch/plotter/griddata/blockstructured/PeanoTextPatchFileWriter.h"
188#include "tarch/multicore/Lock.h"
189#include "tarch/multicore/BooleanSemaphore.h"
190#include "tarch/mpi/Lock.h"
191#include "tarch/mpi/BooleanSemaphore.h"
192#include "peano4/parallel/SpacetreeSet.h"
193""" + self.additional_includes
194
195
196 def get_static_initialisations(self,full_qualified_classname):
197 return """
198tarch::mpi::BooleanSemaphore """ + full_qualified_classname + """::_semaphore;
199"""
200
201
202
Action set (reactions to events)
Definition ActionSet.py:6
get_attributes(self)
Return attributes as copied and pasted into the generated class.
get_body_of_createGridControlEventsForNextTraversal(self)
Return the grid control events that this action set wants to trigger.
get_body_of_operation(self, operation_name)
Return actual C++ code snippets to be inserted into C++ code.
__init__(self, filename, cell_unknown=None, time_stamp_evaluation=NoMetaFile, guard_predicate="true", additional_includes="")
Plot only the grid structure.
get_guard_of_operation(self, operation_name)
Return the guard of an operation.