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
45 __Template_Constructor = """
46 _writer = nullptr;
47 _dataWriter = nullptr;
48 _treeNumber = treeNumber;
49
50 // An MPI lock (critical section) would be important!
51
52 logDebug( "PlotGrid2PlotGridInPeanoBlockFormat1()", "created tree instance for " << treeNumber );
53"""
54
55
57 return self.__Template_Constructor.format(**self.d)
58
59
60 __Template_EndTraversal = """
61 if ( {{PLOT_PREDICATE}} ) {
62 assertion(_dataWriter!=nullptr);
63 assertion1(_dataWriter!=nullptr,_treeNumber);
64
65 _dataWriter->close();
66 _writer->writeToFile();
67
68 delete _dataWriter;
69 delete _writer;
70
71 _dataWriter = nullptr;
72 _writer = nullptr;
73 }
74"""
75
76
78 return ""
79
80
82 return " return std::vector< peano4::grid::GridControlEvent >();\n"
83
84
86 return __name__.replace(".py", "").replace(".", "_")
87
88
90 return False
91
92
93 __Template_TouchCellFirstTime = """
94 if (
95 fineGridCell{{SOLVER_NAME}}.getType() == celldata::{{SOLVER_NAME}}::Type::Interior
96 and
97 {{PLOT_PREDICATE}}
98 ) {
99 int vertexIndices[TwoPowerD];
100
101 int patchIndex = _writer->plotPatch(
102 marker.x()-0.5 * marker.h(),
103 marker.h()
104 );
105
106 int vertexIndex = _dataWriter->getFirstVertexWithinPatch(patchIndex);
107 for (int i=0; i<TwoPowerD; i++) {
108 double* value = fineGridVertices{{SOLVER_NAME}}(i).{{GETTER}}().data();
109 _dataWriter->plotVertex( vertexIndex+i, value );
110 }
111}
112"""
113
114
115 __Template_BeginTraversal = """
116 if ({{PLOT_PREDICATE}}) {
117 tarch::mpi::Lock lock( _semaphore );
118
119 static int counter = -1;
120 counter++;
121
122 std::ostringstream snapshotFileName;
123 snapshotFileName << "{{SOLVER_NAME}}.{{VARIABLE}}-" << counter ;
124
125 if (tarch::mpi::Rank::getInstance().getNumberOfRanks()>1 ) {
126 snapshotFileName << "-rank-" << tarch::mpi::Rank::getInstance().getRank();
127 }
128
129 _writer = new tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter(
130 Dimensions, snapshotFileName.str(), "{{SOLVER_NAME}}.{{VARIABLE}}",
131 tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter::IndexFileMode::AppendNewData,
132 counter
133 );
134
135 _dataWriter = _writer->createVertexDataWriter(
136 "{{VARIABLE}}",
137 2, // number of dofs per axis per cell
138 repositories::{{SOLVER_INSTANCE}}.VertexUnknowns,
139 "Solution",
140 "Solution as delivered through MF Solver"
141 );
142 _dataWriter->setPrecision(16);
143 }
144"""
145
146
147 def get_body_of_operation(self,operation_name):
148 result = "\n"
149 if operation_name==ActionSet.OPERATION_TOUCH_CELL_FIRST_TIME:
150 result = jinja2.Template( self.__Template_TouchCellFirstTime).render(**self.d)
151 if operation_name==ActionSet.OPERATION_BEGIN_TRAVERSAL:
152 result = jinja2.Template( self.__Template_BeginTraversal).render(**self.d)
153 if operation_name==ActionSet.OPERATION_END_TRAVERSAL:
154 result = jinja2.Template( self.__Template_EndTraversal).render(**self.d)
155 return result
156
157
158 def get_attributes(self):
159 return """
160 static tarch::mpi::BooleanSemaphore _semaphore;
161
162 int _treeNumber;
163
164 tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter* _writer;
165 tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter::VertexDataWriter* _dataWriter;
166"""
167
168
169 def get_includes(self):
170 return """
171#include "tarch/plotter/griddata/blockstructured/PeanoTextPatchFileWriter.h"
172#include "tarch/mpi/Lock.h"
173#include "tarch/mpi/BooleanSemaphore.h"
174#include "tarch/multicore/Lock.h"
175#include "tarch/multicore/BooleanSemaphore.h"
176#include "peano4/utils/Loop.h"
177#include "peano4/parallel/SpacetreeSet.h"
178#include "../repositories/SolverRepository.h"
179"""
180
181
182 def get_static_initialisations(self,full_qualified_classname):
183 return """
184tarch::mpi::BooleanSemaphore """ + full_qualified_classname + """::_semaphore;
185"""
186
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.