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