dune-vtk 2.12
Loading...
Searching...
No Matches
unstructuredgridwriter.impl.hh
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file AUTHORS.md
2// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception OR LGPL-3.0-or-later
3#pragma once
4
5#include <iomanip>
6#include <iostream>
7#include <iterator>
8#include <fstream>
9#include <sstream>
10#include <string>
11
13#include <dune/geometry/type.hh>
14
18
19namespace Dune::Vtk {
20
21template <class GV, class DC>
22void UnstructuredGridWriter<GV,DC>
23 ::writeSerialFile (std::ofstream& out) const
24{
25 std::vector<pos_type> offsets; // pos => offset
26 this->writeHeader(out, "UnstructuredGrid");
27 out << "<UnstructuredGrid>\n";
28
29 out << "<Piece"
30 << " NumberOfPoints=\"" << dataCollector_->numPoints() << "\""
31 << " NumberOfCells=\"" << dataCollector_->numCells() << "\""
32 << ">\n";
33
34 // Write point coordinates
35 out << "<Points>\n";
36 this->writePoints(out, offsets);
37 out << "</Points>\n";
38
39 // Write element connectivity, types and offsets
40 out << "<Cells>\n";
41 writeCells(out, offsets);
42 writePointIds(out, offsets);
43 out << "</Cells>\n";
44
45 // Write data associated with grid points
46 out << "<PointData" << this->getNames(pointData_) << ">\n";
47 for (auto const& v : pointData_)
48 this->writeData(out, offsets, v, Super::POINT_DATA);
49 out << "</PointData>\n";
50
51 // Write data associated with grid cells
52 out << "<CellData" << this->getNames(cellData_) << ">\n";
53 for (auto const& v : cellData_)
54 this->writeData(out, offsets, v, Super::CELL_DATA);
55 out << "</CellData>\n";
56
57 out << "</Piece>\n";
58 out << "</UnstructuredGrid>\n";
59
60 this->writeAppended(out, offsets);
61 out << "</VTKFile>";
62}
63
64
65template <class GV, class DC>
66void UnstructuredGridWriter<GV,DC>
67 ::writeParallelFile (std::ofstream& out, std::string const& pfilename, int size) const
68{
69 this->writeHeader(out, "PUnstructuredGrid");
70 out << "<PUnstructuredGrid GhostLevel=\"0\">\n";
71
72 // Write points
73 out << "<PPoints>\n";
74 out << "<PDataArray"
75 << " type=\"" << to_string(datatype_) << "\""
76 << " NumberOfComponents=\"3\""
77 << " />\n";
78 out << "</PPoints>\n";
79
80 // Write data associated with grid points
81 out << "<PPointData" << this->getNames(pointData_) << ">\n";
82 for (auto const& v : pointData_) {
83 out << "<PDataArray"
84 << " Name=\"" << v.name() << "\""
85 << " type=\"" << to_string(v.dataType()) << "\""
86 << " NumberOfComponents=\"" << v.numComponents() << "\""
87 << " />\n";
88 }
89 out << "</PPointData>\n";
90
91 // Write data associated with grid cells
92 out << "<PCellData" << this->getNames(cellData_) << ">\n";
93 for (auto const& v : cellData_) {
94 out << "<PDataArray"
95 << " Name=\"" << v.name() << "\""
96 << " type=\"" << to_string(v.dataType()) << "\""
97 << " NumberOfComponents=\"" << v.numComponents() << "\""
98 << " />\n";
99 }
100 out << "</PCellData>\n";
101
102 // Write piece file references
103 for (int p = 0; p < size; ++p) {
104 std::string piece_source = pfilename + "_p" + std::to_string(p) + "." + this->fileExtension();
105 out << "<Piece Source=\"" << piece_source << "\" />\n";
106 }
107
108 out << "</PUnstructuredGrid>\n";
109 out << "</VTKFile>";
110}
111
112
113template <class GV, class DC>
114void UnstructuredGridWriter<GV,DC>
115 ::writeTimeseriesSerialFile (std::ofstream& out,
116 std::string const& filenameMesh,
118 std::vector<std::uint64_t> const& blocks) const
119{
120 assert(is_a(format_, Vtk::FormatTypes::APPENDED));
121
122 std::vector<std::vector<pos_type>> offsets(timesteps.size()); // pos => offset
123 this->writeHeader(out, "UnstructuredGrid");
124 out << "<UnstructuredGrid"
125 << " TimeValues=\"";
126 {
127 std::size_t i = 0;
128 for (auto const& timestep : timesteps)
129 out << timestep.first << (++i % 6 != 0 ? ' ' : '\n');
130 }
131 out << "\">\n";
132
133 out << "<Piece"
134 << " NumberOfPoints=\"" << dataCollector_->numPoints() << "\""
135 << " NumberOfCells=\"" << dataCollector_->numCells() << "\""
136 << ">\n";
137
138 // Write point coordinates
139 out << "<Points>\n";
140 for (std::size_t i = 0; i < timesteps.size(); ++i) {
141 this->writePoints(out, offsets[i], i);
142 }
143 out << "</Points>\n";
144
145 // Write element connectivity, types and offsets
146 out << "<Cells>\n";
147 for (std::size_t i = 0; i < timesteps.size(); ++i) {
148 writeCells(out, offsets[i], i);
149 writePointIds(out, offsets[i], i);
150 }
151 out << "</Cells>\n";
152
153 const std::size_t shift = offsets[0].size(); // number of blocks to write the grid
154
155 // Write data associated with grid points
156 out << "<PointData" << this->getNames(pointData_) << ">\n";
157 for (std::size_t i = 0; i < timesteps.size(); ++i) {
158 for (auto const& v : pointData_)
159 this->writeData(out, offsets[i], v, Super::POINT_DATA, i);
160 }
161 out << "</PointData>\n";
162
163 // Write data associated with grid cells
164 out << "<CellData" << this->getNames(cellData_) << ">\n";
165 for (std::size_t i = 0; i < timesteps.size(); ++i) {
166 for (auto const& v : cellData_)
167 this->writeData(out, offsets[i], v, Super::CELL_DATA, i);
168 }
169 out << "</CellData>\n";
170
171 out << "</Piece>\n";
172 out << "</UnstructuredGrid>\n";
173
174 out << "<AppendedData encoding=\"raw\">\n_";
175 [[maybe_unused]] pos_type appended_pos = out.tellp();
176
177 { // write grid (points, cells)
178 std::ifstream file_mesh(filenameMesh, std::ios_base::in | std::ios_base::binary);
179 out << file_mesh.rdbuf();
180 assert( std::uint64_t(out.tellp()) == std::accumulate(blocks.begin(), std::next(blocks.begin(),shift), std::uint64_t(appended_pos)) );
181 }
182
183 // write point-data and cell-data
184 for (auto const& timestep : timesteps) {
185 std::ifstream file(timestep.second, std::ios_base::in | std::ios_base::binary);
186 out << file.rdbuf();
187 }
188 out << "</AppendedData>\n";
189
190 out << "</VTKFile>";
191
192 // write correct offsets in file.
193 pos_type offset = 0;
194 for (std::size_t i = 0; i < timesteps.size(); ++i) {
195 offset = 0;
196 auto const& off = offsets[i];
197
198 // write mesh data offsets
199 for (std::size_t j = 0; j < shift; ++j) {
200 out.seekp(off[j]);
201 out << '"' << offset << '"';
202 offset += pos_type(blocks[j]);
203 }
204 }
205
206 std::size_t j = shift;
207 for (std::size_t i = 0; i < timesteps.size(); ++i) {
208 auto const& off = offsets[i];
209
210 for (std::size_t k = shift; k < off.size(); ++k) {
211 out.seekp(off[k]);
212 out << '"' << offset << '"';
213 offset += pos_type(blocks[j++]);
214 }
215 }
216}
217
218
219template <class GV, class DC>
220void UnstructuredGridWriter<GV,DC>
221 ::writeTimeseriesParallelFile (std::ofstream& out,
222 std::string const& pfilename,
223 int size,
224 std::vector<std::pair<double, std::string>> const& timesteps) const
225{
226 this->writeHeader(out, "PUnstructuredGrid");
227 out << "<PUnstructuredGrid GhostLevel=\"0\""
228 << " TimeValues=\"";
229 {
230 std::size_t i = 0;
231 for (auto const& timestep : timesteps)
232 out << timestep.first << (++i % 6 != 0 ? ' ' : '\n');
233 }
234 out << "\">\n";
235
236 // Write points
237 out << "<PPoints>\n";
238 out << "<PDataArray"
239 << " type=\"" << to_string(datatype_) << "\""
240 << " NumberOfComponents=\"3\""
241 << " />\n";
242 out << "</PPoints>\n";
243
244 // Write data associated with grid points
245 out << "<PPointData" << this->getNames(pointData_) << ">\n";
246 for (std::size_t i = 0; i < timesteps.size(); ++i) {
247 for (auto const& v : pointData_) {
248 out << "<PDataArray"
249 << " Name=\"" << v.name() << "\""
250 << " type=\"" << to_string(v.dataType()) << "\""
251 << " NumberOfComponents=\"" << v.numComponents() << "\""
252 << " TimeStep=\"" << i << "\""
253 << " />\n";
254 }
255 }
256 out << "</PPointData>\n";
257
258 // Write data associated with grid cells
259 out << "<PCellData" << this->getNames(cellData_) << ">\n";
260 for (std::size_t i = 0; i < timesteps.size(); ++i) {
261 for (auto const& v : cellData_) {
262 out << "<PDataArray"
263 << " Name=\"" << v.name() << "\""
264 << " type=\"" << to_string(v.dataType()) << "\""
265 << " NumberOfComponents=\"" << v.numComponents() << "\""
266 << " TimeStep=\"" << i << "\""
267 << " />\n";
268 }
269 }
270 out << "</PCellData>\n";
271
272 // Write piece file references
273 for (int p = 0; p < size; ++p) {
274 std::string piece_source = pfilename + "_p" + std::to_string(p) + "." + this->fileExtension();
275 out << "<Piece Source=\"" << piece_source << "\" />\n";
276 }
277
278 out << "</PUnstructuredGrid>\n";
279 out << "</VTKFile>";
280}
281
282
283template <class GV, class DC>
284void UnstructuredGridWriter<GV,DC>
285 ::writeCells (std::ofstream& out, std::vector<pos_type>& offsets,
286 std::optional<std::size_t> timestep) const
287{
288 if (format_ == Vtk::FormatTypes::ASCII) {
289 auto cells = dataCollector_->cells();
290 out << "<DataArray type=\"Int64\" Name=\"connectivity\" format=\"ascii\"";
291 if (timestep)
292 out << " TimeStep=\"" << *timestep << "\"";
293 out << ">\n";
294 this->writeValuesAscii(out, cells.connectivity);
295 out << "</DataArray>\n";
296
297 out << "<DataArray type=\"Int64\" Name=\"offsets\" format=\"ascii\"";
298 if (timestep)
299 out << " TimeStep=\"" << *timestep << "\"";
300 out << ">\n";
301 this->writeValuesAscii(out, cells.offsets);
302 out << "</DataArray>\n";
303
304 out << "<DataArray type=\"UInt8\" Name=\"types\" format=\"ascii\"";
305 if (timestep)
306 out << " TimeStep=\"" << *timestep << "\"";
307 out << ">\n";
308 this->writeValuesAscii(out, cells.types);
309 out << "</DataArray>\n";
310 }
311 else { // Vtk::FormatTypes::APPENDED format
312 out << "<DataArray type=\"Int64\" Name=\"connectivity\" format=\"appended\"";
313 if (timestep)
314 out << " TimeStep=\"" << *timestep << "\"";
315 out << " offset=";
316 offsets.push_back(out.tellp());
318 out << "/>\n";
319
320 out << "<DataArray type=\"Int64\" Name=\"offsets\" format=\"appended\"";
321 if (timestep)
322 out << " TimeStep=\"" << *timestep << "\"";
323 out << " offset=";
324 offsets.push_back(out.tellp());
326 out << "/>\n";
327
328 out << "<DataArray type=\"UInt8\" Name=\"types\" format=\"appended\"";
329 if (timestep)
330 out << " TimeStep=\"" << *timestep << "\"";
331 out << " offset=";
332 offsets.push_back(out.tellp());
334 out << "/>\n";
335 }
336}
337
338
339template <class GV, class DC>
340void UnstructuredGridWriter<GV,DC>
341 ::writePointIds (std::ofstream& out,
342 std::vector<pos_type>& offsets,
343 std::optional<std::size_t> timestep) const
344{
345 auto ids = dataCollector_->pointIds();
346 if (ids.empty())
347 return;
348
349 if (format_ == Vtk::FormatTypes::ASCII) {
350 out << "<DataArray type=\"UInt64\" Name=\"global_point_ids\" format=\"ascii\"";
351 if (timestep)
352 out << " TimeStep=\"" << *timestep << "\"";
353 out << ">\n";
354 this->writeValuesAscii(out, ids);
355 out << "</DataArray>\n";
356 }
357 else { // Vtk::FormatTypes::APPENDED format
358 out << "<DataArray type=\"UInt64\" Name=\"global_point_ids\" format=\"appended\"";
359 if (timestep)
360 out << " TimeStep=\"" << *timestep << "\"";
361 out << " offset=";
362 offsets.push_back(out.tellp());
364 out << "/>\n";
365 }
366}
367
368
369template <class GV, class DC>
370void UnstructuredGridWriter<GV,DC>
371 ::writeGridAppended (std::ofstream& out, std::vector<std::uint64_t>& blocks) const
372{
373 assert(is_a(format_, Vtk::FormatTypes::APPENDED) && "Function should by called only in appended mode!\n");
374
375 Vtk::mapDataTypes<std::is_floating_point, std::is_integral>(datatype_, headertype_,
376 [&](auto f, auto h) {
377 using F = typename decltype(f)::type;
378 using H = typename decltype(h)::type;
379
380 // write points
381 blocks.push_back(this->template writeValuesAppended<H>(out, dataCollector_->template points<F>()));
382
383 // write connectivity, offsets, and types
384 auto cells = dataCollector_->cells();
385 blocks.push_back(this->template writeValuesAppended<H>(out, cells.connectivity));
386 blocks.push_back(this->template writeValuesAppended<H>(out, cells.offsets));
387 blocks.push_back(this->template writeValuesAppended<H>(out, cells.types));
388
389 // optionally, write global point IDs
390 auto ids = dataCollector_->pointIds();
391 if (!ids.empty())
392 blocks.push_back(this->template writeValuesAppended<H>(out, ids));
393 });
394}
395
396} // end namespace Dune::Vtk
Definition datacollector.hh:19
constexpr bool is_a(E a, Integer b)
Definition enum.hh:11
std::string to_string(Vtk::FormatTypes)
Definition types.cc:13
int size() const
std::vector< std::int64_t > offsets
Definition unstructureddatacollector.hh:16
std::vector< std::int64_t > connectivity
Definition unstructureddatacollector.hh:17
std::vector< std::uint8_t > types
Definition unstructureddatacollector.hh:15
Cells cells() const
Return cell types, offsets, and connectivity.
Definition unstructureddatacollector.hh:47
T accumulate(T... args)
T begin(T... args)
T next(T... args)
T push_back(T... args)
T rdbuf(T... args)
T seekp(T... args)
T size(T... args)
T tellp(T... args)
T to_string(T... args)