Peano 4
Loading...
Searching...
No Matches
VTKTextFileWriter.cpp
Go to the documentation of this file.
2
3#include <filesystem>
4#include <fstream>
5#include <iomanip>
6#include <stdio.h>
7
9 "unstructured::vtk::"
10 "VTKTextFileWriter");
11
13 = "# vtk DataFile Version 2.0\n "
14 "Generated by Peano3 output component $Revision: 1.2 $ Author: Tobias Weinzierl\n "
15 "ASCII\n ";
16
18 const std::string& fileName,
19 const std::string& indexFileName,
21 double timeStamp,
22 const int precision
23):
24 _writtenToFile(false),
25 _precision(precision),
26 _doubleOrFloat(setDoubleOrFloatString(precision)),
27 _numberOfVertices(0),
28 _numberOfCells(0),
29 _numberOfCellEntries(0),
30 _fileName(fileName) {
31 if (fileName.rfind(".vtk") != std::string::npos) {
33 "writeToFile()",
34 "filename should not end with .vtk as routine adds extension automatically. Chosen filename prefix=" << fileName
35 );
36 }
37 if (mode != tarch::plotter::PVDTimeSeriesWriter::IndexFileMode::NoIndexFile and indexFileName.rfind(".pvd") != std::string::npos) {
39 "writeToFile()",
40 "index filename should not end with .pvd as routine adds extension automatically. Chosen filename prefix="
41 << indexFileName
42 );
43 }
44
45 // VTK does not support more precise values
46 const double DefaultTimeStampPrecision = 1e-5;
47
48 switch (mode) {
51 tarch::plotter::PVDTimeSeriesWriter::appendNewData(indexFileName, fileName + ".vtk", timeStamp);
52 break;
54 if (not std::filesystem::exists(indexFileName + ".pvd")) {
55 logInfo("PeanoTextPatchFileWriter(...)", "no index file " << indexFileName << " found. Create new one");
57 } else if (tarch::la::smaller(
58 timeStamp,
61 timeStamp,
63 DefaultTimeStampPrecision
64 )
65 )) {
67 "PeanoTextPatchFileWriter(...)",
68 "there is an index file "
69 << indexFileName << " with data for time stamp "
71 << ". Will be overwritten as we dump data for time " << timeStamp
72 );
74 }
75
76 tarch::plotter::PVDTimeSeriesWriter::appendNewData(indexFileName, fileName + ".vtk", timeStamp);
77 break;
79 break;
80 }
81}
82
84 if (!_writtenToFile) {
86 _numberOfVertices,
87 0,
88 "Still vertices in vtk writer pipeline. Maybe you forgot to call writeToFile() on a data vtk writer?"
89 );
91 _numberOfCells,
92 0,
93 "Still cells in vtk writer pipeline. Maybe you forgot to call writeToFile() on a data vtk writer?"
94 );
96 _numberOfCellEntries,
97 0,
98 "Still cell entries in vtk writer pipeline. Maybe you forgot to call writeToFile() on a data vtk writer?"
99 );
100 }
101}
102
104 _writtenToFile = false;
105 _numberOfVertices = 0;
106 _numberOfCells = 0;
107 _numberOfCellEntries = 0;
108 _vertexDescription = "";
109 _cellDescription = "";
110 _cellTypeDescription = "";
111 _vertexDataDescription = "";
112 _cellDataDescription = "";
113}
114
116 assertion(!_writtenToFile);
117
118 std::ostringstream filenameStream;
119 filenameStream << _fileName << ".vtk";
120 const std::string filename = filenameStream.str();
121
122 std::ofstream out;
123 out.open(filename.c_str());
124 if ((!out.fail()) && out.is_open()) {
125 logDebug("close()", "opened data file " + filename);
126 out << std::setprecision(_precision);
127
128 out << HEADER << std::endl << std::endl;
129
130 out
131 << "DATASET UNSTRUCTURED_GRID" << std::endl
132 << "POINTS " << _numberOfVertices << " " << _doubleOrFloat << std::endl
133 << std::endl;
134 out << _vertexDescription << std::endl << std::endl;
135
136 out << "CELLS " << _numberOfCells << " " << _numberOfCellEntries << std::endl << std::endl;
137 out << _cellDescription << std::endl << std::endl;
138
139 out << "CELL_TYPES " << _numberOfCells << std::endl << std::endl;
140 out << _cellTypeDescription << std::endl << std::endl;
141
142 if (_numberOfVertices > 0 && !_vertexDataDescription.empty()) {
143 out << "POINT_DATA " << _numberOfVertices << std::endl << std::endl;
144 out << _vertexDataDescription << std::endl << std::endl;
145 }
146
147 if (_numberOfCells > 0 && !_cellDataDescription.empty()) {
148 out << "CELL_DATA " << _numberOfCells << std::endl << std::endl;
149 out << _cellDataDescription << std::endl << std::endl;
150 }
151
152 logDebug("close()", "data written to " + filename);
153 _writtenToFile = true;
154 return true;
155 } else {
156 logError("close()", "unable to write output file " + filename);
157 return false;
158 }
159}
160
162
167
172
174 const std::string& identifier
175) const {
176 if (identifier.empty()) {
178 "validateDataWriterIdentifier(string)",
179 "identifier for vtk file is empty. Spaces are not allowed for vtk data field identifiers and some vtk "
180 "visualisers might crash."
181 );
182 }
183 if (identifier.find(' ') != std::string::npos) {
185 "validateDataWriterIdentifier(string)",
186 "identifier \""
187 << identifier
188 << "\" contains spaces. Spaces are not allowed for vtk data field identifiers and some vtk visualisers might "
189 "crash."
190 );
191 }
192}
193
195 vtk::VTKTextFileWriter::createCellDataWriter(const std::string& identifier, int recordsPerCell) {
196 validateDataWriterIdentifier(identifier);
198 identifier, *this, recordsPerCell
199 );
200}
201
203 unstructured::vtk::VTKTextFileWriter::createVertexDataWriter(const std::string& identifier, int recordsPerVertex) {
204 validateDataWriterIdentifier(identifier);
206 identifier, *this, recordsPerVertex
207 );
208}
#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.
VTKTextFileWriter(const std::string &fileName, const std::string &indexFileName, tarch::plotter::PVDTimeSeriesWriter::IndexFileMode modeFile, double timeStamp, const int precision=6)
virtual VertexWriter * createVertexWriter() override
Caller has to destroy this instance manually.
virtual VertexDataWriter * createVertexDataWriter(const std::string &identifier, int recordsPerVertex) override
Caller has to destroy this instance manually.
virtual CellWriter * createCellWriter() override
Caller has to destroy this instance manually.
void validateDataWriterIdentifier(const std::string &identifier) const
virtual CellDataWriter * createCellDataWriter(const std::string &identifier, int recordsPerCell) override
Caller has to destroy this instance manually.
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