Peano 4
Loading...
Searching...
No Matches
VTKBinaryFileWriter.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 "VTKBinaryFileWriter");
11
13 = "# vtk DataFile Version 2.0\n "
14 "Generated by Peano3 output component $Revision: 1.2 $ Author: Tobias Weinzierl\n "
15 "BINARY\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.clear();
109 _cellDescription.clear();
110 _cellTypeDescription.clear();
111 _vertexDataDescription.clear();
112 _cellDataDescription.clear();
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(), std::ios::binary);
124 if ((!out.fail()) && out.is_open()) {
125 logDebug("close()", "opened data file " + filename);
126
127 out << HEADER << std::endl << std::endl;
128
129 out << "DATASET UNSTRUCTURED_GRID" << std::endl
130 << "POINTS " << _numberOfVertices << " " << _doubleOrFloat << std::endl;
131 out << _vertexDescription.rdbuf() << std::endl << std::endl;
132
133 out << "CELLS " << _numberOfCells << " " << _numberOfCellEntries << std::endl;
134 out << _cellDescription.rdbuf() << std::endl << std::endl;
135
136 out << "CELL_TYPES " << _numberOfCells << std::endl;
137 out << _cellTypeDescription.rdbuf() << std::endl << std::endl;
138
139 if (_numberOfVertices > 0 && !_vertexDataDescription.str().empty()) {
140 out << "POINT_DATA " << _numberOfVertices << std::endl;
141 out << _vertexDataDescription.rdbuf() << std::endl << std::endl;
142 }
143
144 if (_numberOfCells > 0 && !_cellDataDescription.str().empty()) {
145 out << "CELL_DATA " << _numberOfCells << std::endl;
146 out << _cellDataDescription.rdbuf() << std::endl << std::endl;
147 }
148
149 logDebug("close()", "data written to " + filename);
150
151 _writtenToFile = true;
152 return true;
153 } else {
154 logError("close()", "unable to write output file " + filename);
155 return false;
156 }
157}
158
160
165
170
172 const std::string& identifier
173) const {
174 if (identifier.empty()) {
176 "validateDataWriterIdentifier(string)",
177 "identifier for vtk file is empty. Spaces are not allowed for vtk data field identifiers and some vtk "
178 "visualisers might crash."
179 );
180 }
181 if (identifier.find(' ') != std::string::npos) {
183 "validateDataWriterIdentifier(string)",
184 "identifier \""
185 << identifier
186 << "\" contains spaces. Spaces are not allowed for vtk data field identifiers and some vtk visualisers might "
187 "crash."
188 );
189 }
190}
191
193 vtk::VTKBinaryFileWriter::createCellDataWriter(const std::string& identifier, int recordsPerCell) {
194 validateDataWriterIdentifier(identifier);
196 identifier, *this, recordsPerCell
197 );
198}
199
201 unstructured::vtk::VTKBinaryFileWriter::createVertexDataWriter(const std::string& identifier, int recordsPerVertex) {
202 validateDataWriterIdentifier(identifier);
204 identifier, *this, recordsPerVertex
205 );
206}
#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.
void validateDataWriterIdentifier(const std::string &identifier) const
virtual CellWriter * createCellWriter() override
Caller has to destroy this instance manually.
VTKBinaryFileWriter(const std::string &fileName, const std::string &indexFileName, tarch::plotter::PVDTimeSeriesWriter::IndexFileMode modeFile, double timeStamp, const int precision=6)
virtual CellDataWriter * createCellDataWriter(const std::string &identifier, int recordsPerCell) 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 VertexWriter * createVertexWriter() 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