Peano 4
Loading...
Searching...
No Matches
VTUTextFileWriter.cpp
Go to the documentation of this file.
2
3#include <filesystem>
4#include <fstream>
5#include <iomanip>
6#include <stdio.h>
7
8#include "tarch/mpi/Rank.h"
10
12 "unstructured::vtk::"
13 "VTUTextFileWriter");
14
15const std::string tarch::plotter::griddata::unstructured::vtk::VTUTextFileWriter::HEADER = "<?xml version=\"1.0\"?>\n\
16<!-- Generated by Peano3 output component $Revision: 1.2 $ Author: Tobias Weinzierl -->\n";
17
19 const std::string& fileName,
20 const std::string& indexFileName,
22 double timeStamp,
23 const int precision
24):
25 _dataType(precision < 7 ? "Float32" : "Float64"),
26 _writtenToFile(false),
27 _numberOfVertices(0),
28 _numberOfCells(0),
29 _fileName(fileName) {
30 if (fileName.rfind(".vtu") != std::string::npos) {
32 "writeToFile()",
33 "filename should not end with .vtu as routine adds extension automatically. Chosen filename prefix=" << fileName
34 );
35 }
36 if (mode != tarch::plotter::PVDTimeSeriesWriter::IndexFileMode::NoIndexFile and indexFileName.rfind(".pvd") != std::string::npos) {
38 "writeToFile()",
39 "index filename should not end with .pvd as routine adds extension automatically. Chosen filename prefix="
40 << indexFileName
41 );
42 }
43
44 // VTK does not support more precise values
45 const double DefaultTimeStampPrecision = 1e-5;
46
47 switch (mode) {
50 tarch::plotter::PVDTimeSeriesWriter::appendNewData(indexFileName, fileName + ".vtu", timeStamp);
51 break;
53 if (not std::filesystem::exists(indexFileName + ".pvd")) {
54 logInfo("PeanoTextPatchFileWriter(...)", "no index file " << indexFileName << " found. Create new one");
56 } else if (tarch::la::smaller(
57 timeStamp,
60 timeStamp,
62 DefaultTimeStampPrecision
63 )
64 )) {
66 "PeanoTextPatchFileWriter(...)",
67 "there is an index file "
68 << indexFileName << " with data for time stamp "
70 << ". Will be overwritten as we dump data for time " << timeStamp
71 );
73 }
74
75 tarch::plotter::PVDTimeSeriesWriter::appendNewData(indexFileName, fileName + ".vtu", timeStamp);
76 break;
78 break;
79 }
80}
81
83 if (!_writtenToFile) {
85 _numberOfVertices,
86 0,
87 "Still vertices in vtk writer pipeline. Maybe you forgot to call writeToFile() on a data vtk writer?"
88 );
90 _numberOfCells,
91 0,
92 "Still cells in vtk writer pipeline. Maybe you forgot to call writeToFile() on a data vtk writer?"
93 );
94 }
95}
96
98 _writtenToFile = false;
99 _numberOfVertices = 0;
100 _numberOfCells = 0;
101 _vertexDescription = "";
102 _cellDescription = "";
103 _vertexDataDescription = "";
104 _cellDataDescription = "";
105 _parallelVertexDataDescription = "";
106 _parallelCellDataDescription = "";
107}
108
110 assertion(!_writtenToFile);
111
112 std::ofstream out;
113 out.open((_fileName + ".vtu").c_str());
114 if ((!out.fail()) && out.is_open()) {
115 logDebug("writeToFile()", "opened data file " + _fileName);
116
117 out
118 << HEADER << std::endl
119 << std::endl
120 << "<VTKFile type=\"UnstructuredGrid\" version=\"0.1\" byte_order=\"LittleEndian\">" << std::endl
121 << "<UnstructuredGrid>" << std::endl
122 << "<Piece NumberOfPoints=\"" << _numberOfVertices << "\" NumberOfCells=\"" << _numberOfCells << "\">"
123 << std::endl
124 << _vertexDescription << std::endl
125 << _cellDescription << std::endl
126 << "<PointData>" << std::endl
127 << _vertexDataDescription << std::endl
128 << "</PointData>" << std::endl
129 << "<CellData>" << std::endl
130 << _cellDataDescription << std::endl
131 << "</CellData>\n\
132 </Piece>\n\
133 </UnstructuredGrid>\n\
134 </VTKFile>\n";
135
136 logDebug("writeToFile()", "data written to " + _fileName);
137 _writtenToFile = true;
138 } else {
139 logError("writeToFile()", "unable to write output file " + _fileName);
140 return false;
141 }
142
143 return true;
144}
145
147 const std::string& metaDataFilename, const std::vector<std::string>& files
148) {
149 if (metaDataFilename.rfind(".pvtu") != std::string::npos) {
151 "writeMetaDataFileForParallelSnapshot()",
152 "meta file name should not end with .pvtu as routine adds extension automatically. Chosen filename prefix="
153 << metaDataFilename
154 );
155 }
156
157 std::ofstream metaOut;
158 metaOut.open((metaDataFilename + ".pvtu").c_str());
159 if ((!metaOut.fail()) && metaOut.is_open()) {
160 logDebug("writeMetaDataFileForParallelSnapshot()", "opened data file " + metaDataFilename);
161
162 metaOut
163 << "<?xml version=\"1.0\"?>" << std::endl
164 << "<VTKFile type=\"PUnstructuredGrid\" version=\"0.1\" byte_order=\"LittleEndian\">" << std::endl
165 << "<PUnstructuredGrid GhostLevel=\"0\">" << std::endl
166 << "<PPoints>" << std::endl
167 << "<PDataArray type=\"" << _dataType << "\" Name=\"coordinates\" NumberOfComponents=\"3\"/>" << std::endl
168 << "</PPoints>" << std::endl
169 << "<PCells>" << std::endl
170 << "<PDataArray type=\"Int32\" Name=\"connectivity\" NumberOfComponents=\"1\"/>" << std::endl
171 << "<PDataArray type=\"Int32\" Name=\"offsets\" NumberOfComponents=\"1\"/>" << std::endl
172 << "<PDataArray type=\"UInt8\" Name=\"types\" NumberOfComponents=\"1\"/>" << std::endl
173 << "</PCells>" << std::endl
174 << "<PPointData>" << std::endl
175 << _parallelVertexDataDescription << std::endl
176 << "</PPointData>" << std::endl
177 << "<PCellData>" << std::endl
178 << _parallelCellDataDescription << std::endl
179 << "</PCellData>" << std::endl;
180
181 for (auto p : files) {
182 std::ostringstream referencedFilename;
183 if (p.find("/") != std::string::npos) {
184 referencedFilename << p.substr(p.rfind("/") + 1);
185 } else {
186 referencedFilename << p;
187 }
188
189 if (p.rfind(".vtu") != std::string::npos) {
191 "writeMetaDataFileForParallelSnapshot()",
192 "file name passed to meta file writer should not end with .vtu as routine adds extension automatically. "
193 "Chosen filename prefix="
194 << p
195 );
196 }
197
198 referencedFilename << ".vtu";
199 metaOut << "<Piece Source=\"" << referencedFilename.str() << "\"/>" << std::endl;
200 }
201
202 metaOut << "</PUnstructuredGrid>" << std::endl << "</VTKFile>" << std::endl;
203
204 metaOut.close();
205 } else {
206 logError("writeMetaDataFileForParallelSnapshot()", "unable to write meta data file " + metaDataFilename);
207 }
208}
209
211
216
221
223 const std::string& identifier
224) const {
225 if (identifier.empty()) {
227 "validateDataWriterIdentifier(string)",
228 "identifier for vtk file is empty. Spaces are not allowed for vtk data field identifiers and some vtk "
229 "visualisers might crash."
230 );
231 }
232 if (identifier.find(' ') != std::string::npos) {
234 "validateDataWriterIdentifier(string)",
235 "identifier \""
236 << identifier
237 << "\" contains spaces. Spaces are not allowed for vtk data field identifiers and some vtk visualisers might "
238 "crash."
239 );
240 }
241}
242
244 vtk::VTUTextFileWriter::createCellDataWriter(const std::string& identifier, int recordsPerCell) {
245 validateDataWriterIdentifier(identifier);
247 identifier, *this, recordsPerCell, _dataType
248 );
249}
250
252 unstructured::vtk::VTUTextFileWriter::createVertexDataWriter(const std::string& identifier, int recordsPerVertex) {
253 validateDataWriterIdentifier(identifier);
255 identifier, *this, recordsPerVertex, _dataType
256 );
257}
#define assertion(expr)
#define assertionEqualsMsg(lhs, rhs, msg)
#define logError(methodName, logMacroMessageStream)
Wrapper macro around tarch::tarch::logging::Log to improve logging.
Definition Log.h:464
#define logDebug(methodName, logMacroMessageStream)
Definition Log.h:50
#define logWarning(methodName, logMacroMessageStream)
Wrapper macro around tarch::tarch::logging::Log to improve logging.
Definition Log.h:440
#define logInfo(methodName, logMacroMessageStream)
Wrapper macro around tarch::tarch::logging::Log to improve logging.
Definition Log.h:411
Log Device.
Definition Log.h:516
static void appendNewData(const std::string &snapshotFileName, const std::string &dataFile, double timeStamp)
static void createEmptyIndexFile(const std::string &dataFile)
static double getLatestTimeStepInIndexFile(std::string dataFile)
This is the vertex writer you have to create to plot the vertices.
This is the vertex writer you have to create to plot the vertices.
virtual VertexDataWriter * createVertexDataWriter(const std::string &identifier, int recordsPerVertex) override
Caller has to destroy this instance manually.
void writeMetaDataFileForParallelSnapshot(const std::string &metaDataFilename, const std::vector< std::string > &files)
This can be called on any writer (if there are multiple ones in a parallel run), but it cannot be sta...
void validateDataWriterIdentifier(const std::string &identifier) const
virtual VertexWriter * createVertexWriter() override
Caller has to destroy this instance manually.
virtual CellDataWriter * createCellDataWriter(const std::string &identifier, int recordsPerCell) override
Caller has to destroy this instance manually.
virtual CellWriter * createCellWriter() override
Caller has to destroy this instance manually.
VTUTextFileWriter(const std::string &fileName, const std::string &indexFileName, tarch::plotter::PVDTimeSeriesWriter::IndexFileMode modeFile, double timeStamp, const int precision=6)
virtual void clear() override
Clear the writer, i.e.
static bool smaller(double lhs, double rhs, double tolerance=NUMERICAL_ZERO_DIFFERENCE) InlineMethod
Smaller operator for floating point values.
double relativeEpsNormaledAgainstValueGreaterOne(double valueA, double valueB=std::numeric_limits< double >::min(), double eps=NUMERICAL_ZERO_DIFFERENCE)
Determine a relative tolerance from one or two values.
Definition Scalar.cpp:10