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
10 def __init__(self,
11 solver,
12 variable_name,
13 getter,
14 plot_predicate="true",
15 cell_predicate="true"
16 ):
17 """!
18
19 Plot grid and assume all data are associated with vertices
20
21 This is a copy of the one in toolbox, but for the time being
22 we hardcode the number of unknowns per vertex that we plot
23 to 1, and we make some important customisations. Particularly
24 regarding the place we plot.
25
26 \param plot_predicate: string
27 This determines whether we want to plot in this cell. This prevents
28 spurious creation of patch files that we don't want to print.
29
30 \param cell_predicate: string
31 This determines whether we want to plot in this cell. For instance,
32 we use this flag to prevent plotting on levels that are not the
33 finest level.
34
35 """
36 super( PlotVertexDataInPeanoBlockFormat, self ).__init__()
37
38 self.d = {}
39 self.d["SOLVER_INSTANCE"] = solver.instance_name()
40 self.d["SOLVER_NAME"] = solver.typename()
41 self.d[ "VARIABLE" ] = variable_name
42 self.d[ "GETTER" ] = getter
43 self.d["PLOT_PREDICATE"] = plot_predicate
44 self.d["CELL_PREDICATE"] = cell_predicate
45
46 __Template_Constructor = """
47 _writer = nullptr;
48 _dataWriter = nullptr;
49 _treeNumber = treeNumber;
50
51 // An MPI lock (critical section) would be important!
52
53 logDebug( "PlotGrid2PlotGridInPeanoBlockFormat1()", "created tree instance for " << treeNumber );
54"""
55
56
58 return self.__Template_Constructor.format(**self.d)
59
60
61 __Template_EndTraversal = """
62 if ( {{PLOT_PREDICATE}} ) {
63 assertion(_dataWriter!=nullptr);
64 assertion1(_dataWriter!=nullptr,_treeNumber);
65
66 _dataWriter->close();
67 _writer->writeToFile();
68
69 delete _dataWriter;
70 delete _writer;
71
72 _dataWriter = nullptr;
73 _writer = nullptr;
74 }
75"""
76
77
79 return ""
80
81
83 return " return std::vector< peano4::grid::GridControlEvent >();\n"
84
85
87 return __name__.replace(".py", "").replace(".", "_")
88
89
91 return False
92
93
94 __Template_TouchCellFirstTime = """
95 if (
96 fineGridCell{{SOLVER_NAME}}.getType() == celldata::{{SOLVER_NAME}}::Type::Interior
97 and
98 {{PLOT_PREDICATE}}
99 and
100 {{CELL_PREDICATE}}
101 ) {
102 int vertexIndices[TwoPowerD];
103
104 int patchIndex = _writer->plotPatch(
105 marker.x()-0.5 * marker.h(),
106 marker.h()
107 );
108
109 int vertexIndex = _dataWriter->getFirstVertexWithinPatch(patchIndex);
110 for (int i=0; i<TwoPowerD; i++) {
111 double* value = fineGridVertices{{SOLVER_NAME}}(i).{{GETTER}}().data();
112 _dataWriter->plotVertex( vertexIndex+i, value );
113 }
114}
115"""
116
117
118 __Template_BeginTraversal = """
119 if ({{PLOT_PREDICATE}}) {
120 tarch::mpi::Lock lock( _semaphore );
121
122 static int counter = -1;
123 counter++;
124
125 std::ostringstream snapshotFileName;
126 snapshotFileName << "{{SOLVER_NAME}}.{{VARIABLE}}-" << counter ;
127
128 if (tarch::mpi::Rank::getInstance().getNumberOfRanks()>1 ) {
129 snapshotFileName << "-rank-" << tarch::mpi::Rank::getInstance().getRank();
130 }
131
132 _writer = new tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter(
133 Dimensions, snapshotFileName.str(), "{{SOLVER_NAME}}.{{VARIABLE}}",
134 tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter::IndexFileMode::AppendNewData,
135 counter
136 );
137
138 _dataWriter = _writer->createVertexDataWriter(
139 "{{VARIABLE}}",
140 2, // number of dofs per axis per cell
141 repositories::{{SOLVER_INSTANCE}}.VertexUnknowns,
142 "Solution of {{SOLVER_NAME}}"
143 );
144
145 _dataWriter->setPrecision(16);
146 }
147"""
148
149
150 def get_body_of_operation(self,operation_name):
151 result = "\n"
152 if operation_name==ActionSet.OPERATION_TOUCH_CELL_FIRST_TIME:
153 result = jinja2.Template( self.__Template_TouchCellFirstTime).render(**self.d)
154 if operation_name==ActionSet.OPERATION_BEGIN_TRAVERSAL:
155 result = jinja2.Template( self.__Template_BeginTraversal).render(**self.d)
156 if operation_name==ActionSet.OPERATION_END_TRAVERSAL:
157 result = jinja2.Template( self.__Template_EndTraversal).render(**self.d)
158 return result
159
160
161 def get_attributes(self):
162 return """
163 static tarch::mpi::BooleanSemaphore _semaphore;
164
165 int _treeNumber;
166
167 tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter* _writer;
168 tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter::VertexDataWriter* _dataWriter;
169"""
170
171
172 def get_includes(self):
173 return """
174#include "tarch/plotter/griddata/blockstructured/PeanoTextPatchFileWriter.h"
175#include "tarch/mpi/Lock.h"
176#include "tarch/mpi/BooleanSemaphore.h"
177#include "tarch/multicore/Lock.h"
178#include "tarch/multicore/BooleanSemaphore.h"
179#include "peano4/utils/Loop.h"
180#include "peano4/parallel/SpacetreeSet.h"
181#include "../repositories/SolverRepository.h"
182"""
183
184
185 def get_static_initialisations(self,full_qualified_classname):
186 return """
187tarch::mpi::BooleanSemaphore """ + full_qualified_classname + """::_semaphore;
188"""
189
Action set (reactions to events)
Definition ActionSet.py:6
__init__(self, solver, variable_name, getter, plot_predicate="true", cell_predicate="true")
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.
get_attributes(self)
Return attributes as copied and pasted into the generated class.