VirtualFluids 0.2.0
Parallel CFD LBM Solver
Loading...
Searching...
No Matches
WbWriterVtkXmlImageBinary.cpp
Go to the documentation of this file.
1//=======================================================================================
2// ____ ____ __ ______ __________ __ __ __ __
3// \ \ | | | | | _ \ |___ ___| | | | | / \ | |
4// \ \ | | | | | |_) | | | | | | | / \ | |
5// \ \ | | | | | _ / | | | | | | / /\ \ | |
6// \ \ | | | | | | \ \ | | | \__/ | / ____ \ | |____
7// \ \ | | |__| |__| \__\ |__| \________/ /__/ \__\ |_______|
8// \ \ | | ________________________________________________________________
9// \ \ | | | ______________________________________________________________|
10// \ \| | | | __ __ __ __ ______ _______
11// \ | | |_____ | | | | | | | | | _ \ / _____)
12// \ | | _____| | | | | | | | | | | \ \ \_______
13// \ | | | | |_____ | \_/ | | | | |_/ / _____ |
14// \ _____| |__| |________| \_______/ |__| |______/ (_______/
15//
16// This file is part of VirtualFluids. VirtualFluids is free software: you can
17// redistribute it and/or modify it under the terms of the GNU General Public
18// License as published by the Free Software Foundation, either version 3 of
19// the License, or (at your option) any later version.
20//
21// VirtualFluids is distributed in the hope that it will be useful, but WITHOUT
22// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24// for more details.
25//
26// SPDX-License-Identifier: GPL-3.0-or-later
27// SPDX-FileCopyrightText: Copyright © VirtualFluids Project contributors, see AUTHORS.md in root folder
28//
33//=======================================================================================
36#include <cstring>
37
38using namespace std;
39
40/*===============================================================================*/
41const std::string WbWriterVtkXmlImageBinary::pvdEndTag = " </Collection>\n</VTKFile>";
42/*===============================================================================*/
44 const double &timeStep, const bool &sepGroups)
45{
46 string vtkfilename = filename + ".pvd";
47 ofstream out(vtkfilename.c_str());
48 if (!out) {
49 out.clear(); // flags ruecksetzen (ansonsten liefert utern if(!out) weiterhin true!!!
51 if (path.size() > 0) {
53 out.open(vtkfilename.c_str());
54 }
55 if (!out)
56 throw UbException(UB_EXARGS, "couldn't open file " + vtkfilename);
57 }
58
59 string endian;
61 endian = "LittleEndian";
62 else
63 endian = "BigEndian";
64 out << "<VTKFile type=\"Collection\" version=\"0.1\" byte_order=\"" << endian << "\" >" << endl;
65 out << " <Collection>" << endl;
66
67 int group = 0, part = 0;
68 for (size_t i = 0; i < filenames.size(); i++) {
69 out << " <DataSet timestep=\"" << timeStep << "\" group=\"" << group << "\" part=\"" << part
70 << "\" file=\"" << filenames[i] << "\"/>" << endl;
71 if (sepGroups)
72 group++;
73 else
74 part++;
75 }
76 out << pvdEndTag;
77 out.close();
78
79 return vtkfilename;
80}
81/*===============================================================================*/
83 const double &timeStep, const bool &sepGroups)
84{
85 string vtkfilename = filename;
86 fstream test(vtkfilename.c_str(), ios::in);
87 if (!test) {
88 test.clear();
89 vtkfilename += ".pvd";
90 test.open(vtkfilename.c_str(), ios::in);
91 if (!test)
92 return this->writeCollection(filename, filenames, timeStep, sepGroups);
93 }
94
95 fstream out(vtkfilename.c_str(), ios::in | ios::out);
96 out.seekp(-(int)pvdEndTag.size() - 1, ios_base::end);
97
98 int group = 0;
99 for (size_t i = 0; i < filenames.size(); i++) {
100 out << " <DataSet timestep=\"" << timeStep << "\" group=\"" << group << "\" part=\"" << i << "\" file=\""
101 << filenames[i] << "\"/>" << endl;
102 if (sepGroups)
103 group++;
104 }
105 out << pvdEndTag;
106
107 return vtkfilename;
108}
109/*===============================================================================*/
111 const UbTupleFloat3 &origin, const UbTupleFloat3 &spacing,
114{
115 string vtkfilename = filename + ".pvti";
116 UBLOG(logDEBUG1, "WbWriterVtkXmlImageBinary::writeParallelFile to " << vtkfilename << " - start");
117
118 ofstream out(vtkfilename.c_str());
119 if (!out) {
120 out.clear(); // flags ruecksetzen (ansonsten liefert utern if(!out) weiterhin true!!!
122 if (path.size() > 0) {
124 out.open(vtkfilename.c_str());
125 }
126 if (!out)
127 throw UbException(UB_EXARGS, "couldn't open file " + vtkfilename);
128 }
129
130 // VTK FILE
131 out << "<VTKFile type=\"PImageData\" version=\"0.1\" byte_order=\"LittleEndian\">"
132 << "\n";
133 out << " <PImageData "
134 << "WholeExtent=\"" << val<1>(wholeExtent) << " "
135 << val<2>(wholeExtent) << " "
136 << val<3>(wholeExtent) << " "
137 << val<4>(wholeExtent) << " "
138 << val<5>(wholeExtent) << " "
139 << val<6>(wholeExtent) << "\" "
140 << "GhostLevel=\"0\" "
141 << "Origin=\"" << val<1>(origin) << " "
142 << val<2>(origin) << " "
143 << val<3>(origin) << "\" "
144 << "Spacing=\"" << val<1>(spacing) << " "
145 << val<2>(spacing) << " "
146 << val<3>(spacing) << "\" "
147 << "> \n";
148 out << " <PPointData>\n";
149 for (size_t s = 0; s < pointDataNames.size(); s++)
150 out << " <PDataArray type=\"Float32\" Name=\"" << pointDataNames[s] << "\"/>\n";
151 out << " </PPointData>\n";
152 if (cellDataNames.size() > 0) {
153 out << " <PCellData>\n";
154 for (size_t s = 0; s < cellDataNames.size(); s++)
155 out << " <PDataArray type=\"Float32\" Name=\"" << cellDataNames[s] << "\"/>\n";
156 out << " </PCellData>\n";
157 }
158 for (size_t s = 0; s < pieceSources.size(); s++)
159 out << " <Piece Extent=\"" << val<1>(pieceExtents[s]) << " "
160 << val<2>(pieceExtents[s]) << " "
161 << val<3>(pieceExtents[s]) << " "
162 << val<4>(pieceExtents[s]) << " "
163 << val<5>(pieceExtents[s]) << " "
164 << val<6>(pieceExtents[s]) << "\" Source=\"" << pieceSources[s] << "\"/>\n";
165 out << " </PImageData>\n";
166 out << "</VTKFile>";
167 out << endl;
168 out.close();
169 UBLOG(logDEBUG1, "WbWriterVtkXmlImageBinary::writeParallelFile to " << vtkfilename << " - end");
170
171 return vtkfilename;
172}
173/*===============================================================================*/
175 vector<UbTupleInt8> & /*cells*/, vector<string> &datanames,
177{
178 string vtkfilename = filename + getFileExtension();
179 UBLOG(logDEBUG1, "WbWriterVtkXmlImageBinary::writeOctsWithCellData to " << vtkfilename << " - start");
180
181 vector<string> nodeDataNames;
183
184 UbTupleFloat3 origin, spacing;
185 UbTupleInt6 extent;
186
187 getMetaDataOfImage(nodes, origin, spacing, extent);
188
189 this->writeData(vtkfilename, nodeDataNames, datanames, nodedata, celldata, extent, origin, spacing, extent);
190 UBLOG(logDEBUG1, "WbWriterVtkXmlImageBinary::writeOctsWithCellData to " << vtkfilename << " - end");
191
192 return vtkfilename;
193}
194/*===============================================================================*/
196 vector<UbTupleUInt8> & /*cells*/, vector<string> &datanames,
198{
199 string vtkfilename = filename + getFileExtension();
200 UBLOG(logDEBUG1, "WbWriterVtkXmlImageBinary::writeOctsWithNodeData to " << vtkfilename << " - start");
201
202 vector<string> cellDataNames;
203 vector<vector<double>> cellData;
204
205 UbTupleFloat3 origin, spacing;
206 UbTupleInt6 extent;
207
208 getMetaDataOfImage(nodes, origin, spacing, extent);
209
210 this->writeData(vtkfilename, datanames, cellDataNames, nodedata, cellData, extent, origin, spacing, extent);
211
212 UBLOG(logDEBUG1, "WbWriterVtkXmlImageBinary::writeOctsWithNodeData to " << vtkfilename << " - end");
213
214 return vtkfilename;
215}
216/*===============================================================================*/
219{
220 string vtkfilename = filename + getFileExtension();
221 UBLOG(logDEBUG1, "WbWriterVtkXmlImageBinary::writeNodesWithNodeData to " << vtkfilename << " - start");
222
223 vector<string> cellDataNames;
224 vector<vector<double>> cellData;
225
226 UbTupleFloat3 origin, spacing;
227 UbTupleInt6 extent;
228
229 getMetaDataOfImage(nodes, origin, spacing, extent);
230 this->writeData(vtkfilename, datanames, cellDataNames, nodedata, cellData, extent, origin, spacing, extent);
231
232 return vtkfilename;
233}
234
235void WbWriterVtkXmlImageBinary::getMetaDataOfImage(vector<UbTupleFloat3> &nodes, UbTupleFloat3 &origin,
236 UbTupleFloat3 &spacing, UbTupleInt6 &extent)
237{
238 int nofNodes = (int)nodes.size();
239 val<1>(origin) = val<1>(nodes[0]);
240 val<2>(origin) = val<2>(nodes[0]);
241 val<3>(origin) = val<3>(nodes[0]);
242
243 float l_x = val<1>(nodes[nofNodes-1])-val<1>(origin);
244 float l_y = val<2>(nodes[nofNodes-1])-val<2>(origin);
245
246 val<1>(spacing) = val<1>(nodes[1])-val<1>(nodes[0]);
247 int nx = (l_x) / val<1>(spacing);
248 val<2>(spacing) = val<2>(nodes[nx])-val<2>(nodes[0]);
249 int ny = (l_y) / val<2>(spacing);
250 val<3>(spacing) = val<3>(nodes[nx*ny])-val<3>(nodes[0]);
251
252 val<1>(extent) = val<1>(origin) / val<1>(spacing); val<2>(extent) = val<1>(nodes[nofNodes - 1]) / val<1>(spacing);
253 val<3>(extent) = val<2>(origin) / val<2>(spacing); val<4>(extent) = val<2>(nodes[nofNodes - 1]) / val<2>(spacing);
254 val<5>(extent) = val<3>(origin) / val<3>(spacing); val<6>(extent) = val<3>(nodes[nofNodes - 1]) / val<3>(spacing);
255
256}
257
261 UbTupleFloat3 &origin, UbTupleFloat3 &spacing, UbTupleInt6 &extent,
262 unsigned int precision)
263{
264 ofstream out(vtkfilename.c_str(), ios::out | ios::binary);
265 out.precision(precision);
266
267 if (!out) {
268 out.clear(); // flags ruecksetzen (ansonsten liefert utern if(!out) weiterhin true!!!
270 if (path.size() > 0) {
272 out.open(vtkfilename.c_str(), ios::out | ios::binary);
273 }
274 if (!out)
275 throw UbException(UB_EXARGS, "couldn't open file " + vtkfilename);
276 }
277
278 size_t nPoints = pointDataNames.size() > 0 ? nodedata[0].size() : celldata[0].size();
279
280 int bytesPerByteVal = 4; //==sizeof(int)
281
282 int bytesScalarData = 1 /*scalar */ * (int)nPoints * sizeof(double);
283
284 int offset = 0;
285
286 // VTK FILE
287 out << "<?xml version=\"1.0\"?>\n";
288 out << "<VTKFile type=\"ImageData\" version=\"0.1\" byte_order=\"LittleEndian\" >"
289 << "\n";
290 out << " <ImageData "
291 << "WholeExtent=\"" << val<1>(wholeExtent) << " "
292 << val<2>(wholeExtent) << " "
293 << val<3>(wholeExtent) << " "
294 << val<4>(wholeExtent) << " "
295 << val<5>(wholeExtent) << " "
296 << val<6>(wholeExtent) << "\" "
297 << "Origin=\"" << val<1>(origin) << " "
298 << val<2>(origin) << " "
299 << val<3>(origin) << "\" "
300 << "Spacing=\"" << val<1>(spacing) << " "
301 << val<2>(spacing) << " "
302 << val<3>(spacing) << "\""
303 << "> \n";
304 out << " <Piece Extent=\"" << val<1>(extent) << " "
305 << val<2>(extent) << " "
306 << val<3>(extent) << " "
307 << val<4>(extent) << " "
308 << val<5>(extent) << " "
309 << val<6>(extent) << "\">\n";
310
311 // DATA SECTION
312 if (pointDataNames.size() > 0) {
313 out << " <PointData>\n";
314 for (size_t s = 0; s < pointDataNames.size(); ++s) {
315 out << " <DataArray type=\"Float64\" Name=\"" << pointDataNames[s]
316 << "\" format=\"appended\" offset=\"" << offset << "\" /> \n";
317 offset += (bytesPerByteVal + bytesScalarData);
318 }
319 out << " </PointData>\n";
320 }
321
322 if (cellDataNames.size() > 0) {
323 out << " <CellData>\n";
324 for (size_t s = 0; s < cellDataNames.size(); ++s) {
325 out << " <DataArray type=\"Float64\" Name=\"" << cellDataNames[s]
326 << "\" format=\"appended\" offset=\"" << offset << "\" /> \n";
327 offset += (bytesPerByteVal + bytesScalarData);
328 }
329 out << " </CellData>\n";
330 }
331
332 out << " </Piece>\n";
333 out << " </ImageData>\n";
334
335 // AppendedData SECTION
336 out << " <AppendedData encoding=\"raw\">\n";
337 out << "_";
338
339 // DATA SECTION
340 // pointData
341 for (size_t s = 0; s < pointDataNames.size(); ++s) {
342 out.write((char *)&bytesScalarData, bytesPerByteVal);
343 for (size_t d = 0; d < nodedata[s].size(); ++d) {
344 double tmp = nodedata[s][d];
345 out.write((char *)&tmp, sizeof(double));
346 }
347 }
348
349 // cellData
350 for (size_t s = 0; s < cellDataNames.size(); ++s) {
351 out.write((char *)&bytesScalarData, bytesPerByteVal);
352 for (size_t d = 0; d < celldata[s].size(); ++d) {
353 double tmp = celldata[s][d];
354 out.write((char *)&tmp, sizeof(double));
355 }
356 }
357 out << "\n </AppendedData>\n";
358 out << "</VTKFile>";
359 out << endl;
360 out.close();
361}
362
std::string getFileExtension() override
std::shared_ptr< T > SPtr
const uint ny
const uint nx
#define UBLOG(level, logtext)
Definition UbLogger.h:327
#define UB_EXARGS
Definition UbException.h:73
@ logDEBUG1
Definition UbLogger.h:54
std::string addFilesToCollection(const std::string &filename, const std::vector< std::string > &filenames, const double &timestep, const bool &sepGroups)
void writeData(const std::string &vtkfilename, std::vector< std::string > &pointDataNames, std::vector< std::string > &cellDataNames, std::vector< std::vector< double > > &nodedata, std::vector< std::vector< double > > &celldata, UbTupleInt6 &wholeExtent, UbTupleFloat3 &origin, UbTupleFloat3 &spacing, UbTupleInt6 &extent, unsigned int precision=6)
std::string writeCollection(const std::string &filename, const std::vector< std::string > &filenames, const double &timestep, const bool &sepGroups)
std::string writeOctsWithNodeData(const std::string &filename, std::vector< UbTupleFloat3 > &nodes, std::vector< UbTupleUInt8 > &cells, std::vector< std::string > &datanames, std::vector< std::vector< double > > &nodedata) override
std::string writeNodesWithNodeData(const std::string &filename, std::vector< UbTupleFloat3 > &nodes, std::vector< std::string > &datanames, std::vector< std::vector< double > > &nodedata) override
std::string writeParallelFile(const std::string &filename, const UbTupleInt6 &wholeExtent, const UbTupleFloat3 &origin, const UbTupleFloat3 &spacing, std::vector< std::string > &pieceSources, std::vector< UbTupleInt6 > &pieceExtents, std::vector< std::string > &pointDataNames, std::vector< std::string > &cellDataNames)
std::string writeOctsWithCellData(const std::string &filename, std::vector< UbTupleFloat3 > &nodes, std::vector< UbTupleInt8 > &cells, std::vector< std::string > &datanames, std::vector< std::vector< double > > &celldata) override
bool makeDirectory(const std::string &dir)
Definition UbSystem.h:280
std::string getPathFromString(const std::string &fileStringWithPath)
Definition UbSystem.h:345
bool isLittleEndian()
Definition UbSystem.h:418