Peano
Loading...
Searching...
No Matches
PlotDGDataInPeanoBlockFormat.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 ):
16 """
17
18 Plot the DG mesh
19
20 """
21 super( PlotDGDataInPeanoBlockFormat, self ).__init__()
22
23 self.d = {}
24 self.d["SOLVER_INSTANCE"] = solver.instance_name()
25 self.d["SOLVER_NAME"] = solver.typename()
26 self.d[ "VARIABLE" ] = variable_name
27 self.d[ "GETTER" ] = getter
28 self.d["PLOT_PREDICATE"] = plot_predicate
29
30 __Template_Constructor = """
31 _writer = nullptr;
32 _dataWriter = nullptr;
33 _treeNumber = treeNumber;
34
35 // An MPI lock (critical section) would be important!
36
37 logDebug( "PlotGrid2PlotGridInPeanoBlockFormat1()", "created tree instance for " << treeNumber );
38"""
39
40
42 return self.__Template_Constructor.format(**self.d)
43
44
45 __Template_EndTraversal = """
46 if ( {{PLOT_PREDICATE}} ) {
47 assertion(_dataWriter!=nullptr);
48 assertion1(_dataWriter!=nullptr,_treeNumber);
49
50 _dataWriter->close();
51 _writer->writeToFile();
52
53 delete _dataWriter;
54 delete _writer;
55
56 _dataWriter = nullptr;
57 _writer = nullptr;
58 }
59"""
60
62 return ""
63
64
66 return " return std::vector< peano4::grid::GridControlEvent >();\n"
67
68
70 return __name__.replace(".py", "").replace(".", "_")
71
72
74 return False
75
76
77 Template_TouchCellFirstTime = """
78if (
79 fineGridCell{{SOLVER_NAME}}.getType() == celldata::{{SOLVER_NAME}}::Type::Interior
80 and
81 {{PLOT_PREDICATE}}
82) {
83 tarch::mpi::Lock lock( _semaphore );
84
85 logTraceInWith1Argument("touchCellFirstTime(...)", fineGridCell{{SOLVER_NAME}}.{{GETTER}}());
86
87 int patchIndex = _writer->plotPatch(
88 marker.x()-0.5 * marker.h(),
89 marker.h()
90 );
91
92 int valuePatchVertexIndex = _dataWriter->getFirstVertexWithinPatch(patchIndex);
93 int currentDoF = 0;
94 /*
95 d-dimensional forloop
96 */
97 dfor(k,repositories::{{SOLVER_INSTANCE}}.PolyDegree+1) {
98 double* value = fineGridCell{{SOLVER_NAME}}.{{GETTER}}().data();
99 _dataWriter->plotVertex( valuePatchVertexIndex, value+currentDoF );
100 valuePatchVertexIndex++;
101 currentDoF++;
102 }
103
104 logTraceOut("touchCellFirstTime(...)");
105}
106"""
107
108
109 __Template_BeginTraversal = """
110 if ({{PLOT_PREDICATE}}) {
111 tarch::mpi::Lock lock( _semaphore );
112
113 static int counter = -1;
114 counter++;
115
116 std::ostringstream snapshotFileName;
117 snapshotFileName << "{{SOLVER_NAME}}.{{VARIABLE}}-" << counter ;
118
119 if (tarch::mpi::Rank::getInstance().getNumberOfRanks()>1 ) {
120 snapshotFileName << "-rank-" << tarch::mpi::Rank::getInstance().getRank();
121 }
122
123 _writer = new tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter(
124 Dimensions, snapshotFileName.str(), "{{SOLVER_NAME}}.{{VARIABLE}}",
125 tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter::IndexFileMode::AppendNewData,
126 counter
127 );
128
129 double spatialArrangementOfIntegrationPoints[repositories::{{SOLVER_INSTANCE}}.NodesPerCell*Dimensions];
130 int k=0;
131 dfor(i,repositories::{{SOLVER_INSTANCE}}.PolyDegree+1) {
132 for (int d=0; d<Dimensions; d++) {
133 spatialArrangementOfIntegrationPoints[k] = repositories::{{SOLVER_INSTANCE}}.getIntegrationPoint(i)(d);
134 k++;
135 assertion(k<=repositories::{{SOLVER_INSTANCE}}.NodesPerCell*repositories::{{SOLVER_INSTANCE}}.NodesPerCell*Dimensions);
136 }
137 }
138
139 _dataWriter = _writer->createVertexDataWriter(
140 "{{VARIABLE}}",
141 repositories::{{SOLVER_INSTANCE}}.PolyDegree+1, // nodes per axis
142 repositories::{{SOLVER_INSTANCE}}.UnknownsPerCellNode,
143 "Solution of {{SOLVER_NAME}}",
144 "",
145 spatialArrangementOfIntegrationPoints
146 );
147
148 _dataWriter->setPrecision(16);
149 }
150"""
151
152
153 def get_body_of_operation(self,operation_name):
154 result = "\n"
155 if operation_name==ActionSet.OPERATION_TOUCH_CELL_FIRST_TIME:
156 result = jinja2.Template( self.Template_TouchCellFirstTime).render(**self.d)
157 if operation_name==ActionSet.OPERATION_BEGIN_TRAVERSAL:
158 result = jinja2.Template( self.__Template_BeginTraversal).render(**self.d)
159 if operation_name==ActionSet.OPERATION_END_TRAVERSAL:
160 result = jinja2.Template( self.__Template_EndTraversal).render(**self.d)
161 return result
162
163
164 def get_attributes(self):
165 return """
166 static tarch::mpi::BooleanSemaphore _semaphore;
167
168 int _treeNumber;
169
170 tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter* _writer;
171 tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter::VertexDataWriter* _dataWriter;
172 tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter::VertexDataWriter* _rhsWriter;
173"""
174
175
176 def get_includes(self):
177 return """
178#include "tarch/plotter/griddata/blockstructured/PeanoTextPatchFileWriter.h"
179#include "tarch/mpi/Lock.h"
180#include "tarch/mpi/BooleanSemaphore.h"
181#include "tarch/multicore/Lock.h"
182#include "tarch/multicore/BooleanSemaphore.h"
183#include "peano4/utils/Loop.h"
184#include "peano4/parallel/SpacetreeSet.h"
185#include "../repositories/SolverRepository.h"
186"""
187
188
189 def get_static_initialisations(self,full_qualified_classname):
190 return """
191tarch::mpi::BooleanSemaphore """ + full_qualified_classname + """::_semaphore;
192"""
193
Action set (reactions to events)
Definition ActionSet.py:6
get_body_of_operation(self, operation_name)
Return actual C++ code snippets to be inserted into C++ code.
__init__(self, solver, variable_name, getter, plot_predicate="true")
Plot the DG mesh.
get_attributes(self)
Return attributes as copied and pasted into the generated class.