dune-vtk 2.12
Loading...
Searching...
No Matches
vtkreader.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#include <fstream>
4#include <iterator>
5#include <sstream>
6#include <string>
7
8#if HAVE_VTK_ZLIB
9#include <zlib.h>
10#endif
11
14
15#include "utility/errors.hh"
16#include "utility/filesystem.hh"
17#include "utility/string.hh"
18
19namespace Dune::Vtk {
20
21template <class Grid, class Creator, class Field>
23{
24 // check whether file exists!
25 if (!Vtk::exists(filename))
26 DUNE_THROW(IOError, "File " << filename << " does not exist!");
27
28 std::ifstream input(filename, std::ios_base::in | std::ios_base::binary);
29 VTK_ASSERT(input.is_open());
30
32 if (ext == ".vtu") {
33 readSerialFileFromStream(input, fillCreator);
34 pieces_.push_back(filename);
35 } else if (ext == ".pvtu") {
36 readParallelFileFromStream(input, comm().rank(), comm().size(), fillCreator);
37 } else {
38 DUNE_THROW(Dune::Vtk::VtkError, "File has unknown file-extension '" << ext << "'. Allowed are only '.vtu' and '.pvtu'.");
39 }
40 read_ = true;
41}
42
43
44template <class Grid, class Creator, class Field>
46{
47 clear();
48 compressor_ = Vtk::CompressorTypes::NONE;
53 unsigned int data_components = 0;
55
56 Sections section = NO_SECTION;
57 for (std::string line; std::getline(input, line); ) {
58 Vtk::ltrim(line);
59
60 if (isSection(line, "VTKFile", section)) {
61 bool closed = false;
62 auto attr = parseXml(line, closed);
63
64 if (!attr["type"].empty())
65 VTK_ASSERT_MSG(attr["type"] == "UnstructuredGrid", "VtkReader supports UnstructuredGrid types");
66 if (!attr["byte_order"].empty())
67 VTK_ASSERT_MSG(attr["byte_order"] == "LittleEndian", "LittleEndian byte order supported");
68
69 if (attr["header_type"] == "UInt32")
71 else if (attr["header_type"] == "UInt64")
73
74 if (attr["compressor"] == "vtkZLibDataCompressor")
75 compressor_ = Vtk::CompressorTypes::ZLIB;
76 else if (attr["compressor"] == "vtkLZ4DataCompressor")
77 compressor_ = Vtk::CompressorTypes::LZ4;
78 else if (attr["compressor"] == "vtkLZMADataCompressor")
79 compressor_ = Vtk::CompressorTypes::LZMA;
80
81 section = VTK_FILE;
82 }
83 else if (isSection(line, "/VTKFile", section, VTK_FILE)) {
84 section = NO_SECTION;
85 break;
86 }
87 else if (isSection(line, "UnstructuredGrid", section, VTK_FILE))
88 section = UNSTRUCTURED_GRID;
89 else if (isSection(line, "/UnstructuredGrid", section, UNSTRUCTURED_GRID))
90 section = VTK_FILE;
91 else if (isSection(line, "Piece", section, UNSTRUCTURED_GRID)) {
92 bool closed = false;
93 auto attr = parseXml(line, closed);
94
95 VTK_ASSERT_MSG(attr.count("NumberOfPoints") > 0 && attr.count("NumberOfCells") > 0,
96 "Number of points or cells in file must be > 0");
97 numberOfPoints_ = std::stoul(attr["NumberOfPoints"]);
98 numberOfCells_ = std::stoul(attr["NumberOfCells"]);
99 section = PIECE;
100 }
101 else if (isSection(line, "/Piece", section, PIECE))
102 section = UNSTRUCTURED_GRID;
103 else if (isSection(line, "PointData", section, PIECE))
104 section = POINT_DATA;
105 else if (isSection(line, "/PointData", section, POINT_DATA))
106 section = PIECE;
107 else if (isSection(line, "CellData", section, PIECE))
108 section = CELL_DATA;
109 else if (isSection(line, "/CellData", section, CELL_DATA))
110 section = PIECE;
111 else if (isSection(line, "Points", section, PIECE))
112 section = POINTS;
113 else if (isSection(line, "/Points", section, POINTS))
114 section = PIECE;
115 else if (isSection(line, "Cells", section, PIECE))
116 section = CELLS;
117 else if (isSection(line, "/Cells", section, CELLS))
118 section = PIECE;
119 else if (line.substr(1,9) == "DataArray") {
120 bool closed = false;
121 auto attr = parseXml(line, closed);
122
123 data_type = Vtk::dataTypeOf(attr["type"]);
124
125 // Use Section.Name as id
126 data_id = toString(section) + "." + attr["Name"];
127
128 if (section == POINTS)
129 // In the Points section must only be one DataArray with id=Points
130 data_id = "Points";
131
132 data_components = 1;
133 if (!attr["NumberOfComponents"].empty())
134 data_components = std::stoul(attr["NumberOfComponents"]);
135
136 // determine FormatType
137 data_format = Vtk::to_lower(attr["format"]);
138 if (data_format == "appended") {
140 } else {
141 format_ = Vtk::FormatTypes::ASCII;
142 }
143
144 // Offset makes sense in appended mode only
145 data_offset = 0;
146 if (!attr["offset"].empty()) {
147 data_offset = std::stoul(attr["offset"]);
148 VTK_ASSERT_MSG(data_format == "appended", "Attribute 'offset' only supported by appended mode");
149 }
150
151 // Store attributes of DataArray
152 dataArray_[data_id] = {attr["Name"], data_type, data_components, data_offset, section};
153
154 // Skip section in appended mode
155 if (data_format == "appended") {
156 if (!closed) {
157 while (std::getline(input, line)) {
158 Vtk::ltrim(line);
159 if (line.substr(1,10) == "/DataArray")
160 break;
161 }
162 }
163 continue;
164 }
165
166 if (section == POINT_DATA)
167 section = PD_DATA_ARRAY;
168 else if (section == POINTS)
169 section = POINTS_DATA_ARRAY;
170 else if (section == CELL_DATA)
171 section = CD_DATA_ARRAY;
172 else if (section == CELLS)
173 section = CELLS_DATA_ARRAY;
174 else
175 DUNE_THROW(Dune::Vtk::VtkError, "Wrong section for <DataArray>");
176 }
177 else if (line.substr(1,10) == "/DataArray") {
178 if (section == PD_DATA_ARRAY)
179 section = POINT_DATA;
180 else if (section == POINTS_DATA_ARRAY)
181 section = POINTS;
182 else if (section == CD_DATA_ARRAY)
183 section = CELL_DATA;
184 else if (section == CELLS_DATA_ARRAY)
185 section = CELLS;
186 else
187 DUNE_THROW(Dune::Vtk::VtkError, "Wrong section for </DataArray>");
188 }
189 else if (isSection(line, "AppendedData", section, VTK_FILE)) {
190 bool closed = false;
191 auto attr = parseXml(line, closed);
192 if (!attr["encoding"].empty())
193 VTK_ASSERT_MSG(attr["encoding"] == "raw", "base64 encoding not supported");
195 offset0_ = findAppendedDataPosition(input);
197 [&](auto f, auto h) {
198 this->readPointsAppended(f,h,input);
199 this->readCellsAppended(h,input);
200 });
202 // read point and cell data
203 for (auto const& d : dataArray_) {
204 if (d.second.section == POINT_DATA) {
206 [&](auto f, auto h) {
207 this->readPointDataAppended(f,h,input,d.first);
208 });
209 }
210 else if (d.second.section == CELL_DATA) {
212 [&](auto f, auto h) {
213 this->readCellDataAppended(f,h,input,d.first);
214 });
215 }
216 }
217
218 section = NO_SECTION; // finish reading after appended section
219 break;
220 }
221 else if (isSection(line, "/AppendedData", section, APPENDED_DATA))
222 section = VTK_FILE;
223
224 switch (section) {
225 case PD_DATA_ARRAY:
226 section = readPointData(input, data_id);
227 break;
228 case POINTS_DATA_ARRAY:
229 section = readPoints(input, data_id);
230 break;
231 case CD_DATA_ARRAY:
232 section = readCellData(input, data_id);
233 break;
234 case CELLS_DATA_ARRAY:
235 section = readCells(input, data_id);
236 break;
237 default:
238 break;
239 }
240 }
241
242 if (section != NO_SECTION)
243 DUNE_THROW(Dune::Vtk::VtkError, "VTK-File is incomplete. It must end with </VTKFile>!");
244
245 if (fillCreator)
246 fillGridCreator();
247}
248
249
250template <class Grid, class Creator, class Field>
252{
253 clear();
254
256 compressor_ = Vtk::CompressorTypes::NONE;
257
258 Sections section = NO_SECTION;
259 for (std::string line; std::getline(input, line); ) {
260 Vtk::ltrim(line);
261
262 if (isSection(line, "VTKFile", section)) {
263 bool closed = false;
264 auto attr = parseXml(line, closed);
265
266 if (!attr["type"].empty())
267 VTK_ASSERT_MSG(attr["type"] == "PUnstructuredGrid", "VtkReader supports PUnstructuredGrid types");
268 if (!attr["version"].empty())
269 VTK_ASSERT_MSG(std::stod(attr["version"]) == 1.0, "File format must be 1.0");
270 if (!attr["byte_order"].empty())
271 VTK_ASSERT_MSG(attr["byte_order"] == "LittleEndian", "LittleEndian byte order supported");
272
273 if (attr["header_type"] == "UInt32")
275 else if (attr["header_type"] == "UInt64")
277
278 if (attr["compressor"] == "vtkZLibDataCompressor")
279 compressor_ = Vtk::CompressorTypes::ZLIB;
280 else if (attr["compressor"] == "vtkLZ4DataCompressor")
281 compressor_ = Vtk::CompressorTypes::LZ4;
282 else if (attr["compressor"] == "vtkLZMADataCompressor")
283 compressor_ = Vtk::CompressorTypes::LZMA;
284
285 section = VTK_FILE;
286 }
287 else if (isSection(line, "/VTKFile", section, VTK_FILE)) {
288 section = NO_SECTION;
289 break;
290 } else if (isSection(line, "PUnstructuredGrid", section, VTK_FILE))
291 section = UNSTRUCTURED_GRID;
292 else if (isSection(line, "/PUnstructuredGrid", section, UNSTRUCTURED_GRID))
293 section = VTK_FILE;
294 else if (isSection(line, "Piece", section, UNSTRUCTURED_GRID)) {
295 bool closed = false;
296 auto attr = parseXml(line, closed);
297
298 VTK_ASSERT_MSG(attr.count("Source") > 0, "No source files for partitions provided");
299 pieces_.push_back(attr["Source"]);
300 }
301 }
302
303 VTK_ASSERT_MSG(section == NO_SECTION, "VTK-File is incomplete. It must end with </VTKFile>!");
304
305 if (fillCreator)
306 fillGridCreator();
307}
308
309
310// @{ implementation detail
317template <class IStream, class T, class Sections>
319 Sections section, Sections parent_section)
320{
321 values.reserve(max_size < std::size_t(-1) ? max_size : 0);
322 using S = std::conditional_t<(sizeof(T) <= 1), std::uint16_t, T>; // problem when reading chars as ints
323
324 std::size_t idx = 0;
325 for (std::string line; std::getline(input, line);) {
326 Vtk::trim(line);
327 if (line.substr(1,10) == "/DataArray")
328 return parent_section;
329 if (line[0] == '<')
330 break;
331
332 std::istringstream stream(line);
333 S value;
334 for (; stream >> value; idx++)
335 values.push_back(T(value));
336 if (idx >= max_size)
337 break;
338 }
339
340 return section;
341}
342
343template <class IStream, class Sections>
344Sections skipRestOfDataArray (IStream& input, Sections section, Sections parent_section)
345{
346 for (std::string line; std::getline(input, line);) {
347 Vtk::ltrim(line);
348 if (line.substr(1,10) == "/DataArray")
349 return parent_section;
350 }
351
352 return section;
353}
354// @}
355
356
357// Read values stored on the cells with name `name`
358template <class Grid, class Creator, class Field>
359typename VtkReader<Grid,Creator,Field>::Sections
361{
362 VTK_ASSERT(numberOfCells_ > 0);
363 unsigned int components = dataArray_[id].components;
364
365 Sections sec = NO_SECTION;
366 std::vector<Field>& values = cellData_[id];
367 sec = readDataArray(input, values, components*numberOfCells_, CD_DATA_ARRAY, CELL_DATA);
368 if (sec != CELL_DATA)
369 sec = skipRestOfDataArray(input, CD_DATA_ARRAY, CELL_DATA);
370 VTK_ASSERT(sec == CELL_DATA);
371 VTK_ASSERT(values.size() == components*numberOfCells_);
372
373 return sec;
374}
375
376
377template <class Grid, class Creator, class Field>
378 template <class FloatType, class HeaderType>
380{
381 VTK_ASSERT(numberOfCells_ > 0);
382 unsigned int components = dataArray_[id].components;
383
385 readAppended(input, values, HeaderType(dataArray_[id].offset));
386 VTK_ASSERT(values.size() == components*numberOfCells_);
387
388 cellData_[id].resize(values.size());
389 std::copy(values.begin(), values.end(), cellData_[id].begin());
390}
391
392
393template <class Grid, class Creator, class Field>
394typename VtkReader<Grid,Creator,Field>::Sections
396{
397 VTK_ASSERT(numberOfPoints_ > 0);
398 unsigned int components = dataArray_[id].components;
399
400 Sections sec = NO_SECTION;
401 std::vector<Field>& values = pointData_[id];
402 sec = readDataArray(input, values, components*numberOfPoints_, PD_DATA_ARRAY, POINT_DATA);
403 if (sec != POINT_DATA)
404 sec = skipRestOfDataArray(input, PD_DATA_ARRAY, POINT_DATA);
405 VTK_ASSERT(sec == POINT_DATA);
406 VTK_ASSERT(values.size() == components*numberOfPoints_);
407
408 return sec;
409}
410
411
412template <class Grid, class Creator, class Field>
413 template <class FloatType, class HeaderType>
415{
416 VTK_ASSERT(numberOfPoints_ > 0);
417 unsigned int components = dataArray_[id].components;
418
420 readAppended(input, values, HeaderType(dataArray_[id].offset));
421 VTK_ASSERT(values.size() == components*numberOfPoints_);
422
423 pointData_[id].resize(values.size());
424 std::copy(values.begin(), values.end(), pointData_[id].begin());
425}
426
427
428template <class Grid, class Creator, class Field>
429typename VtkReader<Grid,Creator,Field>::Sections
431{
432 using T = typename GlobalCoordinate::value_type;
433 VTK_ASSERT(numberOfPoints_ > 0);
434 VTK_ASSERT(id == "Points");
435 VTK_ASSERT(dataArray_["Points"].components == 3u);
436
437 Sections sec = NO_SECTION;
438
440 sec = readDataArray(input, point_values, 3*numberOfPoints_, POINTS_DATA_ARRAY, POINTS);
441 if (sec != POINTS)
442 sec = skipRestOfDataArray(input, POINTS_DATA_ARRAY, POINTS);
443 VTK_ASSERT(sec == POINTS);
444 VTK_ASSERT(point_values.size() == 3*numberOfPoints_);
445
446 // extract points from continuous values
447 GlobalCoordinate p;
448 vec_points.reserve(numberOfPoints_);
449 std::size_t idx = 0;
450 for (std::size_t i = 0; i < numberOfPoints_; ++i) {
451 for (std::size_t j = 0; j < p.size(); ++j)
452 p[j] = point_values[idx++];
453 idx += (3u - p.size());
454 vec_points.push_back(p);
455 }
456
457 return sec;
458}
459
460
461template <class Grid, class Creator, class Field>
462 template <class FloatType, class HeaderType>
464{
465 VTK_ASSERT(numberOfPoints_ > 0);
466 VTK_ASSERT(dataArray_["Points"].components == 3u);
468 readAppended(input, point_values, HeaderType(dataArray_["Points"].offset));
469 VTK_ASSERT(point_values.size() == 3*numberOfPoints_);
470
471 // extract points from continuous values
473 vec_points.reserve(numberOfPoints_);
474 std::size_t idx = 0;
475 for (std::size_t i = 0; i < numberOfPoints_; ++i) {
476 for (std::size_t j = 0; j < p.size(); ++j)
477 p[j] = FloatType(point_values[idx++]);
478 idx += (3u - p.size());
479 vec_points.push_back(p);
480 }
481}
482
483
484template <class Grid, class Creator, class Field>
485typename VtkReader<Grid,Creator,Field>::Sections
487{
488 Sections sec = CELLS_DATA_ARRAY;
489
490 VTK_ASSERT(numberOfCells_ > 0);
491 if (id == "Cells.types") {
492 sec = readDataArray(input, vec_types, numberOfCells_, CELLS_DATA_ARRAY, CELLS);
493 VTK_ASSERT(vec_types.size() == numberOfCells_);
494 } else if (id == "Cells.offsets") {
495 sec = readDataArray(input, vec_offsets, numberOfCells_, CELLS_DATA_ARRAY, CELLS);
496 VTK_ASSERT(vec_offsets.size() == numberOfCells_);
497 } else if (id == "Cells.connectivity") {
498 sec = readDataArray(input, vec_connectivity, std::size_t(-1), CELLS_DATA_ARRAY, CELLS);
499 } else if (id == "Cells.global_point_ids") {
500 sec = readDataArray(input, vec_point_ids, numberOfPoints_, CELLS_DATA_ARRAY, CELLS);
501 VTK_ASSERT(vec_point_ids.size() == numberOfPoints_);
502 }
503
504 return sec;
505}
506
507
508template <class Grid, class Creator, class Field>
509 template <class HeaderType>
511{
512 VTK_ASSERT(numberOfCells_ > 0);
513 auto types_data = dataArray_["Cells.types"];
514 auto offsets_data = dataArray_["Cells.offsets"];
515 auto connectivity_data = dataArray_["Cells.connectivity"];
516
518 readAppended(input, vec_types, HeaderType(types_data.offset));
519 VTK_ASSERT(vec_types.size() == numberOfCells_);
520
522 readAppended(input, vec_offsets, HeaderType(offsets_data.offset));
523 else if (offsets_data.type == Vtk::DataTypes::INT32) {
525 readAppended(input, offsets, HeaderType(offsets_data.offset));
526 vec_offsets.resize(offsets.size());
527 std::copy(offsets.begin(), offsets.end(), vec_offsets.begin());
528 }
529 else { DUNE_THROW(Dune::NotImplemented, "Unsupported DataType in Cell offsets."); }
530 VTK_ASSERT(vec_offsets.size() == numberOfCells_);
531
533 readAppended(input, vec_connectivity, HeaderType(connectivity_data.offset));
534 else if (connectivity_data.type == Vtk::DataTypes::INT32) {
535 std::vector<std::int32_t> connectivity;
536 readAppended(input, connectivity, HeaderType(connectivity_data.offset));
537 vec_connectivity.resize(connectivity.size());
538 std::copy(connectivity.begin(), connectivity.end(), vec_connectivity.begin());
539 }
540 else { DUNE_THROW(Dune::NotImplemented, "Unsupported DataType in Cell connectivity."); }
541 VTK_ASSERT(vec_connectivity.size() == std::size_t(vec_offsets.back()));
542
543 if (dataArray_.count("Cells.global_point_ids") > 0) {
544 auto point_id_data = dataArray_["Cells.global_point_ids"];
546 readAppended(input, vec_point_ids, HeaderType(point_id_data.offset));
547 VTK_ASSERT(vec_point_ids.size() == numberOfPoints_);
548 }
549}
550
551
552// @{ implementation detail
553namespace {
554
562template <class T, class IStream>
564 [[maybe_unused]] unsigned char* buffer_in,
568{
569#if HAVE_VTK_ZLIB
572
573 Bytef* compressed_buffer = reinterpret_cast<Bytef*>(buffer_in);
574 Bytef* uncompressed_buffer = reinterpret_cast<Bytef*>(buffer);
575
578
580 std::cerr << "Zlib error while uncompressing data.\n";
581 std::abort();
582 }
583 VTK_ASSERT(uLongf(bs) == uncompressed_space);
584#else
585 std::cerr << "ZLib Compression not supported. Provide the ZLIB package to CMake." << std::endl;
586 std::abort();
587#endif
588}
589
590template <class T, class IStream>
591void read_compressed_lz4 (T* /* buffer */, unsigned char* /* buffer_in */,
592 std::uint64_t /* bs */, std::uint64_t /* cbs */, IStream& /* input */)
593{
594#if HAVE_VTK_LZ4
595 std::cerr << "LZ4 Compression not yet implemented" << std::endl;
596 std::abort();
597#else
598 std::cerr << "LZ4 Compression not supported. Provide the LZ4 package to CMake." << std::endl;
599 std::abort();
600#endif
601}
602
603template <class T, class IStream>
604void read_compressed_lzma (T* /* buffer */, unsigned char* /* buffer_in */,
605 std::uint64_t /* bs */, std::uint64_t /* cbs */, IStream& /* input */)
606{
607#if HAVE_VTK_LZMA
608 std::cerr << "LZMA Compression not yet implemented" << std::endl;
609 std::abort();
610#else
611 std::cerr << "LZMA Compression not supported. Provide the LZMA package to CMake." << std::endl;
612 std::abort();
613#endif
614}
615
616}
617// @}
618
619
620template <class Grid, class Creator, class Field>
621 template <class FloatType, class HeaderType>
622void VtkReader<Grid,Creator,Field>::readAppended (std::ifstream& input, std::vector<FloatType>& values, HeaderType offset)
623{
624 input.seekg(offset0_ + offset);
625
626 HeaderType size = 0;
627
628 HeaderType num_blocks = 0;
629 HeaderType block_size = 0;
630 HeaderType last_block_size = 0;
631 std::vector<HeaderType> cbs; // compressed block sizes
632
633 // read total size / block-size(s)
634 if (compressor_ != Vtk::CompressorTypes::NONE) {
635 input.read((char*)&num_blocks, sizeof(HeaderType));
636 input.read((char*)&block_size, sizeof(HeaderType));
637 input.read((char*)&last_block_size, sizeof(HeaderType));
638
639 VTK_ASSERT(block_size % sizeof(FloatType) == 0);
640
641 // total size of the uncompressed data
642 size = block_size * (num_blocks-1) + last_block_size;
643
644 // size of the compressed blocks
645 cbs.resize(num_blocks);
646 input.read((char*)cbs.data(), num_blocks*sizeof(HeaderType));
647 } else {
648 input.read((char*)&size, sizeof(HeaderType));
649 }
650 VTK_ASSERT(size > 0 && (size % sizeof(FloatType)) == 0);
651 values.resize(size / sizeof(FloatType));
652
653 if (compressor_ != Vtk::CompressorTypes::NONE) {
654 // upper bound for compressed block-size
655 HeaderType compressed_block_size = block_size + (block_size + 999)/1000 + 12;
656 // number of values in the full blocks
657 std::size_t num_values = block_size / sizeof(FloatType);
658
659 std::vector<unsigned char> buffer_in(compressed_block_size);
660 for (std::size_t i = 0; i < std::size_t(num_blocks); ++i) {
661 HeaderType bs = i < std::size_t(num_blocks-1) ? block_size : last_block_size;
662
663 switch (compressor_) {
665 read_compressed_zlib(values.data() + i*num_values, buffer_in.data(), bs, cbs[i], input);
666 break;
668 read_compressed_lz4(values.data() + i*num_values, buffer_in.data(), bs, cbs[i], input);
669 break;
671 read_compressed_lzma(values.data() + i*num_values, buffer_in.data(), bs, cbs[i], input);
672 break;
673 default:
674 VTK_ASSERT_MSG(false, "Unsupported Compressor type.");
675 break;
676 }
677 }
678 } else {
679 input.read((char*)(values.data()), size);
681 }
682}
683
684
685template <class Grid, class Creator, class Field>
687{
688 VTK_ASSERT(vec_points.size() == numberOfPoints_);
689 VTK_ASSERT(vec_types.size() == numberOfCells_);
690 VTK_ASSERT(vec_offsets.size() == numberOfCells_);
691
692 if (!vec_points.empty())
693 creator_->insertVertices(vec_points, vec_point_ids);
694 if (!vec_types.empty())
695 creator_->insertElements(vec_types, vec_offsets, vec_connectivity);
696 if (insertPieces)
697 creator_->insertPieces(pieces_);
698}
699
700
701// Convert section into string
702template <class Grid, class Creator, class Field>
704{
705 switch (s) {
706 case VTK_FILE:
707 return "VTKFile";
708 case UNSTRUCTURED_GRID:
709 return "UnstructuredGrid";
710 case PIECE:
711 return "Piece";
712 case POINT_DATA:
713 return "PointData";
714 case CELL_DATA:
715 return "CellData";
716 case POINTS:
717 return "Points";
718 case CELLS:
719 return "Cells";
720 case APPENDED_DATA:
721 return "AppendedData";
722 case PD_DATA_ARRAY:
723 case CD_DATA_ARRAY:
724 case POINTS_DATA_ARRAY:
725 case CELLS_DATA_ARRAY:
726 return "DataArray";
727 default:
728 return "Unknown";
729 }
730}
731
732
733// Assume input already read the line <AppendedData ...>
734template <class Grid, class Creator, class Field>
735std::uint64_t VtkReader<Grid,Creator,Field>::findAppendedDataPosition (std::ifstream& input) const
736{
737 char c = 0;
738 while (input.get(c) && std::isblank(c)) { /*do nothing*/ }
739
740 std::uint64_t offset = input.tellg();
741 if (c != '_')
742 --offset; // if char is not '_', assume it is part of the data.
743
744 return offset;
745}
746
747
748template <class Grid, class Creator, class Field>
749std::map<std::string, std::string> VtkReader<Grid,Creator,Field>::parseXml (std::string const& line, bool& closed)
750{
751 closed = false;
753
754 Sections sec = NO_SECTION;
755 bool escape = false;
756
757 std::string name = "";
758 std::string value = "";
759 for (auto c : line) {
760 switch (sec) {
761 case NO_SECTION:
762 if (std::isalpha(c) || c == '_') {
763 name.clear();
764 sec = XML_NAME;
765 name.push_back(c);
766 } else if (c == '/') {
767 closed = true;
768 }
769 break;
770 case XML_NAME:
771 if (std::isalpha(c) || c == '_')
772 name.push_back(c);
773 else
774 sec = (c == '=' ? XML_NAME_ASSIGN : NO_SECTION);
775 break;
776 case XML_NAME_ASSIGN:
777 value.clear();
778 escape = false;
779 VTK_ASSERT_MSG( c == '"', "Format error!" );
780 sec = XML_VALUE;
781 break;
782 case XML_VALUE:
783 if (c == '"' && !escape) {
784 attr[name] = value;
785 sec = NO_SECTION;
786 } else if (c == '\\' && !escape) {
787 escape = true;
788 } else {
789 value.push_back(c);
790 escape = false;
791 }
792 break;
793 default:
794 VTK_ASSERT_MSG(false, "Format error!");
795 }
796 }
797
798 return attr;
799}
800
801
802template <class Grid, class Creator, class Field>
803void VtkReader<Grid,Creator,Field>::clear ()
804{
805 vec_points.clear();
806 vec_point_ids.clear();
807 vec_types.clear();
808 vec_offsets.clear();
809 vec_connectivity.clear();
810 dataArray_.clear();
811 pieces_.clear();
812
813 numberOfCells_ = 0;
814 numberOfPoints_ = 0;
815 offset0_ = 0;
816 read_ = false;
817}
818
819} // end namespace Dune::Vtk
#define VTK_ASSERT_MSG(cond, text)
check if condition cond holds; otherwise, throw a VtkError with a message.
Definition errors.hh:21
#define VTK_ASSERT(cond)
check if condition cond holds; otherwise, throw a VtkError.
Definition errors.hh:31
Definition datacollector.hh:19
std::string & ltrim(std::string &str)
trim a string from the left
Definition string.hh:30
std::string & trim(std::string &str)
trim a string from both sides
Definition string.hh:52
Sections skipRestOfDataArray(IStream &input, Sections section, Sections parent_section)
Definition vtkreader.impl.hh:344
Vtk::DataTypes dataTypeOf()
Definition types.hh:80
std::string to_lower(std::string input)
convert all characters in a string to upper case
Definition string.hh:22
Sections readDataArray(IStream &input, std::vector< T > &values, std::size_t max_size, Sections section, Sections parent_section)
Definition vtkreader.impl.hh:318
DataTypes
Definition types.hh:53
bool exists(Path const &p)
Test whether the path is a valid (existing and accessible) file / directory.
Definition filesystem.cc:135
const char * name()
int id()
int size() const
void clear()
bool empty() const
#define DUNE_THROW(E,...)
auto comm() const
Definition datacollectorinterface.hh:112
Implementation of DataCollector for cells, with continuous data.
Definition celltypedatacollector.hh:20
Definition errors.hh:14
Definition filesystem.hh:15
Path extension() const
Returns the file extension path component.
Definition filesystem.cc:78
std::string string() const
Return the path as string.
Definition filesystem.cc:28
File-Reader for Vtk unstructured .vtu files.
Definition vtkreader.hh:39
typename GridCreator::GlobalCoordinate GlobalCoordinate
Type of global world coordinates.
Definition vtkreader.hh:68
void readParallelFileFromStream(std::ifstream &input, int rank, int size, bool create=true)
Read the grid from and input stream, referring to a .pvtu file, into the GridFactory factory_.
Definition vtkreader.impl.hh:251
void readSerialFileFromStream(std::ifstream &input, bool create=true)
Read the grid from an input stream, referring to a .vtu file, into the GridFactory factory_.
Definition vtkreader.impl.hh:45
void read(std::string const &filename, bool fillCreator=true)
Read the grid from file with filename into the GridCreator.
Definition vtkreader.impl.hh:22
void fillGridCreator(bool insertPieces=true)
Definition vtkreader.impl.hh:686
Definition vtkreader.hh:348
T abort(T... args)
T begin(T... args)
T clear(T... args)
T copy(T... args)
T data(T... args)
T end(T... args)
T endl(T... args)
T gcount(T... args)
T get(T... args)
T getline(T... args)
T read(T... args)
T resize(T... args)
T seekg(T... args)
T size(T... args)
T stod(T... args)
T stoul(T... args)
T tellg(T... args)