dune-vtk 2.12
Loading...
Searching...
No Matches
vtkreader.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 <iosfwd>
6#include <map>
7#include <memory>
8#include <vector>
9
13
15#include <dune/vtk/types.hh>
17
18// default GridCreator
21
22namespace Dune::Vtk
23{
25
36 template <class G, class GC = Vtk::ContinuousGridCreator<G>, class FT = double>
38 : public Vtk::FileReader<G, VtkReader<G, GC, FT>>
39 {
40 // Sections visited during the xml parsing
41 enum Sections {
42 NO_SECTION = 0, VTK_FILE, UNSTRUCTURED_GRID, PIECE, POINT_DATA, PD_DATA_ARRAY, CELL_DATA, CD_DATA_ARRAY,
43 POINTS, POINTS_DATA_ARRAY, CELLS, CELLS_DATA_ARRAY, APPENDED_DATA, XML_NAME, XML_NAME_ASSIGN, XML_VALUE
44 };
45
46 public:
47 // Type storing information about read data
56
57 public:
59 using Grid = G;
60
62 using GridCreator = GC;
63
65 using FieldType = FT;
66
68 using GlobalCoordinate = typename GridCreator::GlobalCoordinate;
69
70 // Template representing a grid-function that is created in getPointData() and getCellData()
71 // with Context either Vtk::PointContext or Vtk::CellContext, respectively.
72 // To each GridCreator a GridFunction is associated, see, e.g. Vtk::ContinuousGridFunction
73 // or Vtk::LagrangeGridFunction.
74 template <class Context, class Range>
76
77 public:
79
85 template <class... Args,
87 explicit VtkReader (Args&&... args)
88 : VtkReader(std::make_shared<GridCreator>(std::forward<Args>(args)...))
89 {}
90
95
98 : VtkReader(std::make_shared<GridCreator>(std::move(creator)))
99 {}
100
103 : creator_(std::move(creator))
104 {}
105
107
115 void read (std::string const& filename, bool fillCreator = true);
116
119 {
120 return *creator_;
121 }
122
126 {
127 return creator_->createGrid();
128 }
129
132 template <class Range = DynamicVector<FieldType>>
134 {
135 auto data_it = dataArray_.find("PointData." + name);
136 auto point_it = pointData_.find("PointData." + name);
137 VTK_ASSERT_MSG(data_it != dataArray_.end() && point_it != pointData_.end(),
138 "The data to extract is not found in point-data. Try `getCellData()` instead!");
139 VTK_ASSERT(data_it->second.section == POINT_DATA);
140
141 return {*creator_, point_it->second, data_it->second.name, data_it->second.components,
142 data_it->second.type, vec_types, vec_offsets, vec_connectivity};
143 }
144
148 {
150 attributes.reserve(pointData_.size());
151 for (auto const& da : dataArray_) {
152 if (da.second.section == POINT_DATA)
153 attributes.push_back(da.second);
154 }
155 return attributes;
156 }
157
160 template <class Range = DynamicVector<FieldType>>
162 {
163 auto data_it = dataArray_.find("CellData." + name);
164 auto cell_it = cellData_.find("CellData." + name);
165 VTK_ASSERT_MSG(data_it != dataArray_.end() && cell_it != cellData_.end(),
166 "The data to extract is not found in cell-data. Try `getPointData()` instead!");
167 VTK_ASSERT(data_it->second.section == CELL_DATA);
168
169 return {*creator_, cell_it->second, data_it->second.name, data_it->second.components,
170 data_it->second.type, vec_types, vec_offsets, vec_connectivity};
171 }
172
176 {
178 attributes.reserve(cellData_.size());
179 for (auto const& da : dataArray_) {
180 if (da.second.section == CELL_DATA)
181 attributes.push_back(da.second);
182 }
183 return attributes;
184 }
185
188
190
195
197
201 void readParallelFileFromStream (std::ifstream& input, int rank, int size, bool create = true);
202
205 void fillGridCreator (bool insertPieces = true);
206
208
211 {
212 return pieces_;
213 }
214
215#ifndef DOXYGEN
216 // Implementation of the FileReader interface
217 static void fillFactoryImpl (GridFactory<Grid>& factory, std::string const& filename)
218 {
219 VtkReader reader{factory};
220 reader.read(filename);
221 }
222#endif
223
224 private:
225 // Read values stored on the cells with ID `id`
226 Sections readCellData (std::ifstream& input, std::string id);
227
228 template <class F, class H>
229 void readCellDataAppended (MetaType<F>, MetaType<H>, std::ifstream& input, std::string id);
230
231 // Read values stored on the points with ID `id`
232 Sections readPointData (std::ifstream& input, std::string id);
233
234 template <class F, class H>
235 void readPointDataAppended (MetaType<F>, MetaType<H>, std::ifstream& input, std::string id);
236
237
238 // Read vertex coordinates from `input` stream and store in into `factory`
239 Sections readPoints (std::ifstream& input, std::string id);
240
241 template <class F, class H>
242 void readPointsAppended (MetaType<F>, MetaType<H>, std::ifstream& input);
243
244
245 // Read cell type, cell offsets and connectivity from `input` stream
246 Sections readCells (std::ifstream& input, std::string id);
247
248 template <class H>
249 void readCellsAppended (MetaType<H>, std::ifstream& input);
250
251 // Read data from appended section in vtk file, starting from `offset`
252 template <class FloatType, class HeaderType>
253 void readAppended (std::ifstream& input, std::vector<FloatType>& values, HeaderType offset);
254
255 // Test whether line belongs to section
256 bool isSection (std::string line,
257 std::string key,
258 Sections current,
259 Sections parent = NO_SECTION) const
260 {
261 bool result = line.substr(1, key.length()) == key;
262 if (result && current != parent)
263 DUNE_THROW(Exception , "<" << key << "> in wrong section." );
264 return result;
265 }
266
267 // Convert a section into a string
268 std::string toString (Sections s) const;
269
270 // Find beginning of appended binary data
271 std::uint64_t findAppendedDataPosition (std::ifstream& input) const;
272
273 // Read attributes from current xml tag
274 std::map<std::string, std::string> parseXml (std::string const& line, bool& closed);
275
276 // clear all vectors
277 void clear ();
278
279 auto comm () const
280 {
282 }
283
284 private:
286
289
292
293 // Temporary data to construct the grid elements
295 std::vector<std::uint64_t> vec_point_ids; //< Global unique vertex ID
296 std::vector<std::uint8_t> vec_types; //< VTK cell type ID
297 std::vector<std::int64_t> vec_offsets; //< offset of vertices of cell
298 std::vector<std::int64_t> vec_connectivity; //< vertex indices of cell
299
300 std::size_t numberOfCells_ = 0; //< Number of cells in the grid
301 std::size_t numberOfPoints_ = 0; //< Number of vertices in the grid
302
303 // offset information for appended data
304 // map: id -> {Name,DataType,NumberOfComponents,Offset}
306
307 // storage for internal point and cell data
308 // map: id -> vector of data entries per point/cell
311
312 // vector of filenames of parallel pieces
314
316 std::uint64_t offset0_ = 0;
317
318 bool read_ = false;
319 };
320
321 // deduction guides
322 template <class Grid>
325
326 template <class GridCreator,
328 VtkReader (GridCreator&)
330
331 template <class GridCreator,
333 VtkReader (GridCreator&&)
335
336 template <class GridCreator,
340
341} // end namespace Dune::Vtk
342
343namespace Dune
344{
345 template <class Grid, class GC = Vtk::ContinuousGridCreator<Grid>, class FieldType = double>
346 class [[deprecated("Use Vtk::VtkReader instead.")]] VtkReader
347 : public Vtk::VtkReader<Grid,GC,FieldType>
348 {
350 public:
351 using Base::Base;
352 };
353
354#pragma GCC diagnostic push
355#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
356 // deduction guides
357 template <class Grid>
360
361 template <class GridCreator,
363 VtkReader (GridCreator&)
365
366 template <class GridCreator,
370#pragma GCC diagnostic pop
371
372} // end namespace Dune
373
374#include "vtkreader.impl.hh"
Macro for wrapping error checks and throwing exceptions.
#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
STL namespace.
Definition datacollector.hh:19
FormatTypes
Type used for representing the output format.
Definition types.hh:21
CompressorTypes
Definition types.hh:150
DataTypes
Definition types.hh:53
const char * name()
static type * create(const M &mat, bool verbose, bool reusevector)
int size() const
#define DUNE_THROW(E,...)
std::shared_ptr< T > stackobject_to_shared_ptr(T &t)
static Communication< MPICommunicator > getCommunication()
Implementation of DataCollector for cells, with continuous data.
Definition celltypedatacollector.hh:20
Definition filereader.hh:16
static void fillFactoryImpl(GridFactory< Grid > &, std::string const &, Args &&...)
Definition filereader.hh:71
File-Reader for Vtk unstructured .vtu files.
Definition vtkreader.hh:39
typename GridCreator::GlobalCoordinate GlobalCoordinate
Type of global world coordinates.
Definition vtkreader.hh:68
GridFunction< Vtk::CellContext, Range > getCellData(std::string const &name) const
Definition vtkreader.hh:161
std::unique_ptr< Grid > createGrid() const
Definition vtkreader.hh:125
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
VtkReader(GridCreator &creator)
Constructor. Stores the references in a non-destroying shared_ptr.
Definition vtkreader.hh:92
FT FieldType
Type of the components of the data to extract from the file.
Definition vtkreader.hh:65
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
VtkReader(Args &&... args)
Constructor. Creates a new GridCreator with the passed factory.
Definition vtkreader.hh:87
GridFunction< Vtk::PointContext, Range > getPointData(std::string const &name) const
Definition vtkreader.hh:133
std::vector< DataArrayAttributes > getCellDataAttributes() const
Definition vtkreader.hh:175
GridCreator & gridCreator()
Obtains the creator of the reader.
Definition vtkreader.hh:118
typename Vtk::AssociatedGridFunction< GridCreator, Range, Context >::type GridFunction
Definition vtkreader.hh:75
void read(std::string const &filename, bool fillCreator=true)
Read the grid from file with filename into the GridCreator.
Definition vtkreader.impl.hh:22
GC GridCreator
Type of the associated grid creator.
Definition vtkreader.hh:62
std::vector< DataArrayAttributes > getPointDataAttributes() const
Definition vtkreader.hh:147
VtkReader(std::shared_ptr< GridCreator > creator)
Constructor. Stores the shared_ptr.
Definition vtkreader.hh:102
void fillGridCreator(bool insertPieces=true)
Definition vtkreader.impl.hh:686
std::vector< std::string > const & pieces() const
Return the filenames of parallel pieces.
Definition vtkreader.hh:210
VtkReader(GridCreator &&creator)
Constructor. Stores the references in a non-destroying shared_ptr.
Definition vtkreader.hh:97
Sections section
Definition vtkreader.hh:54
std::string name
Definition vtkreader.hh:50
std::uint64_t offset
Definition vtkreader.hh:53
Vtk::DataTypes type
Definition vtkreader.hh:51
unsigned int components
Definition vtkreader.hh:52
Definition vtkreader.hh:348
T end(T... args)
T find(T... args)
T size(T... args)