Peano
Loading...
Searching...
No Matches
PlotDGPatchesInPeanoBlockFormat.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 getter,
13 variable,
14 plot_predicate = "true"
15 ):
16 """
17
18 Plot the DG mesh
19
20 """
21 super( PlotDGPatchesInPeanoBlockFormat, self ).__init__()
22
23 self.d = {}
24 self.d["SOLVER_INSTANCE"] = solver.instance_name()
25 self.d["SOLVER_NAME"] = solver.typename()
26 self.d[ "GETTER" ] = getter
27 self.d[ "VARIABLE" ] = variable
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 int patchIndex = _writer->plotPatch(
86 marker.x()-0.5 * marker.h(),
87 marker.h()
88 );
89
90 int valuePatchVertexIndex = _dataWriter->getFirstVertexWithinPatch(patchIndex);
91 int currentDoF = 0;
92 /*
93 d-dimensional forloop
94 */
95 dfor(k,repositories::{{SOLVER_INSTANCE}}.PolyDegree+1) {
96 double* value = fineGridCell{{GETTER}}().data();
97 _dataWriter->plotVertex( valuePatchVertexIndex, value+currentDoF );
98 valuePatchVertexIndex++;
99 currentDoF++;
100 }
101
102}
103"""
104
105 __Template_BeginTraversal = """
106 if ({{PLOT_PREDICATE}}) {
107 tarch::mpi::Lock lock( _semaphore );
108
109 static int counter = -1;
110 counter++;
111
112 std::ostringstream snapshotFileName;
113 snapshotFileName << "{{SOLVER_NAME}}.{{VARIABLE}}-" << counter ;
114
115 if (tarch::mpi::Rank::getInstance().getNumberOfRanks()>1 ) {
116 snapshotFileName << "-rank-" << tarch::mpi::Rank::getInstance().getRank();
117 }
118
119 _writer = new tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter(
120 Dimensions, snapshotFileName.str(), "{{SOLVER_NAME}}.{{VARIABLE}}",
121 tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter::IndexFileMode::AppendNewData,
122 counter
123 );
124
125 _dataWriter = _writer->createVertexDataWriter(
126 "{{VARIABLE}}",
127 repositories::{{SOLVER_INSTANCE}}.PolyDegree+1, // nodes per axis
128 repositories::{{SOLVER_INSTANCE}}.UnknownsPerCellNode,
129 "Solution",
130 "Solution as delivered through MF Solver"
131 );
132
133 _dataWriter->setPrecision(16);
134 }
135"""
136
137
138 def get_body_of_operation(self,operation_name):
139 result = "\n"
140 if operation_name==ActionSet.OPERATION_TOUCH_CELL_FIRST_TIME:
141 result = jinja2.Template( self.Template_TouchCellFirstTime).render(**self.d)
142 if operation_name==ActionSet.OPERATION_BEGIN_TRAVERSAL:
143 result = jinja2.Template( self.__Template_BeginTraversal).render(**self.d)
144 if operation_name==ActionSet.OPERATION_END_TRAVERSAL:
145 result = jinja2.Template( self.__Template_EndTraversal).render(**self.d)
146 return result
147
148
149 def get_attributes(self):
150 return """
151 static tarch::mpi::BooleanSemaphore _semaphore;
152
153 int _treeNumber;
154
155 tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter* _writer;
156 tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter::VertexDataWriter* _dataWriter;
157 tarch::plotter::griddata::blockstructured::PeanoTextPatchFileWriter::VertexDataWriter* _rhsWriter;
158"""
159
160
161 def get_includes(self):
162 return """
163#include "tarch/plotter/griddata/blockstructured/PeanoTextPatchFileWriter.h"
164#include "tarch/mpi/Lock.h"
165#include "tarch/mpi/BooleanSemaphore.h"
166#include "tarch/multicore/Lock.h"
167#include "tarch/multicore/BooleanSemaphore.h"
168#include "peano4/utils/Loop.h"
169#include "peano4/parallel/SpacetreeSet.h"
170#include "../repositories/SolverRepository.h"
171"""
172
173
174 def get_static_initialisations(self,full_qualified_classname):
175 return """
176tarch::mpi::BooleanSemaphore """ + full_qualified_classname + """::_semaphore;
177"""
178
Action set (reactions to events)
Definition ActionSet.py:6
__init__(self, solver, getter, variable, plot_predicate="true")
Plot the DG mesh.
get_attributes(self)
Return attributes as copied and pasted into the generated class.
get_body_of_operation(self, operation_name)
Return actual C++ code snippets to be inserted into C++ code.