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
84 logTraceInWith1Argument("Plot::touchCellFirstTime", fineGridCell{{SOLVER_NAME}}.{{GETTER}}());
85
86 int patchIndex = _writer->plotPatch(
87 marker.x()-0.5 * marker.h(),
88 marker.h()
89 );
90
91 int valuePatchVertexIndex = _dataWriter->getFirstVertexWithinPatch(patchIndex);
92 int currentDoF = 0;
93 /*
94 d-dimensional forloop
95 */
96 dfor(k,repositories::{{SOLVER_INSTANCE}}.PolyDegree+1) {
97 double* value = fineGridCell{{SOLVER_NAME}}.{{GETTER}}().data();
98 _dataWriter->plotVertex( valuePatchVertexIndex, value+currentDoF );
99 valuePatchVertexIndex++;
100 currentDoF++;
101 }
102
103
104
105 logTraceOut("Plot::touchCellFirstTime");
106}
107"""
108
109 templateTouchFaceFirstTime="""
110 if (fineGridFace{{SOLVER_NAME}}.getType()!=facedata::{{SOLVER_NAME}}::Type::Coarse)
111 {
112 logTraceInWith3Arguments("touchFaceFirstTimeOutput", marker.x(), fineGridFace{{SOLVER_NAME}}.getSolution(),fineGridFace{{SOLVER_NAME}}.getProjection());
113 logTraceOut("touchFaceFirstTimeOutput");
114 }
115"""
116
117 __Template_BeginTraversal = """
118 if ({{PLOT_PREDICATE}}) {
119 tarch::mpi::Lock lock( _semaphore );
120
121 static int counter = -1;
122 counter++;
123
124 std::ostringstream snapshotFileName;
125 snapshotFileName << "{{SOLVER_NAME}}.{{VARIABLE}}-" << counter ;
126
127 if (tarch::mpi::Rank::getInstance().getNumberOfRanks()>1 ) {
128 snapshotFileName << "-rank-" << tarch::mpi::Rank::getInstance().getRank();
129 }
130
131 _writer = new tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter(
132 Dimensions, snapshotFileName.str(), "{{SOLVER_NAME}}.{{VARIABLE}}",
133 tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter::IndexFileMode::AppendNewData,
134 counter
135 );
136
137 _dataWriter = _writer->createVertexDataWriter(
138 "{{VARIABLE}}",
139 repositories::{{SOLVER_INSTANCE}}.PolyDegree+1, // nodes per axis
140 repositories::{{SOLVER_INSTANCE}}.UnknownsPerCellNode,
141 "Solution",
142 "Solution as delivered through MF Solver"
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 if operation_name==ActionSet.OPERATION_TOUCH_FACE_FIRST_TIME:
159 result = jinja2.Template( self.templateTouchFaceFirstTime).render(**self.d)
160 return result
161
162
163 def get_attributes(self):
164 return """
165 static tarch::mpi::BooleanSemaphore _semaphore;
166
167 int _treeNumber;
168
169 tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter* _writer;
170 tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter::VertexDataWriter* _dataWriter;
171 tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter::VertexDataWriter* _rhsWriter;
172"""
173
174
175 def get_includes(self):
176 return """
177#include "tarch/plotter/griddata/blockstructured/PeanoTextPatchFileWriter.h"
178#include "tarch/mpi/Lock.h"
179#include "tarch/mpi/BooleanSemaphore.h"
180#include "tarch/multicore/Lock.h"
181#include "tarch/multicore/BooleanSemaphore.h"
182#include "peano4/utils/Loop.h"
183#include "peano4/parallel/SpacetreeSet.h"
184#include "../repositories/SolverRepository.h"
185"""
186
187
188 def get_static_initialisations(self,full_qualified_classname):
189 return """
190tarch::mpi::BooleanSemaphore """ + full_qualified_classname + """::_semaphore;
191"""
192
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.