dune-vtk 2.12
Loading...
Searching...
No Matches
vtkreader.hh
Go to the documentation of this file.
1#pragma once
2
3#include <iosfwd>
4#include <map>
5#include <memory>
6#include <vector>
7
11
13#include <dune/vtk/types.hh>
15
16// default GridCreator
19
20namespace Dune::Vtk
21{
23
34 template <class G, class GC = Vtk::ContinuousGridCreator<G>, class FT = double>
36 : public Vtk::FileReader<G, VtkReader<G, GC, FT>>
37 {
38 // Sections visited during the xml parsing
39 enum Sections {
40 NO_SECTION = 0, VTK_FILE, UNSTRUCTURED_GRID, PIECE, POINT_DATA, PD_DATA_ARRAY, CELL_DATA, CD_DATA_ARRAY,
41 POINTS, POINTS_DATA_ARRAY, CELLS, CELLS_DATA_ARRAY, APPENDED_DATA, XML_NAME, XML_NAME_ASSIGN, XML_VALUE
42 };
43
44 public:
45 // Type storing information about read data
54
55 public:
57 using Grid = G;
58
60 using GridCreator = GC;
61
63 using FieldType = FT;
64
66 using GlobalCoordinate = typename GridCreator::GlobalCoordinate;
67
68 // Template representing a grid-function that is created in getPointData() and getCellData()
69 // with Context either Vtk::PointContext or Vtk::CellContext, respectively.
70 // To each GridCreator a GridFunction is associated, see, e.g. Vtk::ContinuousGridFunction
71 // or Vtk::LagrangeGridFunction.
72 template <class Context, class Range>
74
75 public:
77
83 template <class... Args,
84 std::enable_if_t<std::is_constructible<GridCreator, Args...>::value,int> = 0>
85 explicit VtkReader (Args&&... args)
86 : VtkReader(std::make_shared<GridCreator>(std::forward<Args>(args)...))
87 {}
88
90 explicit VtkReader (GridCreator& creator)
92 {}
93
95 explicit VtkReader (GridCreator&& creator)
96 : VtkReader(std::make_shared<GridCreator>(std::move(creator)))
97 {}
98
101 : creator_(std::move(creator))
102 {}
103
105
113 void read (std::string const& filename, bool fillCreator = true);
114
117 {
118 return *creator_;
119 }
120
124 {
125 return creator_->createGrid();
126 }
127
130 template <class Range = DynamicVector<FieldType>>
132 {
133 auto data_it = dataArray_.find("PointData." + name);
134 auto point_it = pointData_.find("PointData." + name);
135 VTK_ASSERT_MSG(data_it != dataArray_.end() && point_it != pointData_.end(),
136 "The data to extract is not found in point-data. Try `getCellData()` instead!");
137 VTK_ASSERT(data_it->second.section == POINT_DATA);
138
139 return {*creator_, point_it->second, data_it->second.name, data_it->second.components,
140 data_it->second.type, vec_types, vec_offsets, vec_connectivity};
141 }
142
146 {
148 attributes.reserve(pointData_.size());
149 for (auto const& da : dataArray_) {
150 if (da.second.section == POINT_DATA)
151 attributes.push_back(da.second);
152 }
153 return attributes;
154 }
155
158 template <class Range = DynamicVector<FieldType>>
160 {
161 auto data_it = dataArray_.find("CellData." + name);
162 auto cell_it = cellData_.find("CellData." + name);
163 VTK_ASSERT_MSG(data_it != dataArray_.end() && cell_it != cellData_.end(),
164 "The data to extract is not found in cell-data. Try `getPointData()` instead!");
165 VTK_ASSERT(data_it->second.section == CELL_DATA);
166
167 return {*creator_, cell_it->second, data_it->second.name, data_it->second.components,
168 data_it->second.type, vec_types, vec_offsets, vec_connectivity};
169 }
170
174 {
176 attributes.reserve(cellData_.size());
177 for (auto const& da : dataArray_) {
178 if (da.second.section == CELL_DATA)
179 attributes.push_back(da.second);
180 }
181 return attributes;
182 }
183
186
188
192 void readSerialFileFromStream (std::ifstream& input, bool create = true);
193
195
199 void readParallelFileFromStream (std::ifstream& input, int rank, int size, bool create = true);
200
203 void fillGridCreator (bool insertPieces = true);
204
206
209 {
210 return pieces_;
211 }
212
213#ifndef DOXYGEN
214 // Implementation of the FileReader interface
215 static void fillFactoryImpl (GridFactory<Grid>& factory, std::string const& filename)
216 {
217 VtkReader reader{factory};
218 reader.read(filename);
219 }
220#endif
221
222 private:
223 // Read values stored on the cells with ID `id`
224 Sections readCellData (std::ifstream& input, std::string id);
225
226 template <class F, class H>
227 void readCellDataAppended (MetaType<F>, MetaType<H>, std::ifstream& input, std::string id);
228
229 // Read values stored on the points with ID `id`
230 Sections readPointData (std::ifstream& input, std::string id);
231
232 template <class F, class H>
233 void readPointDataAppended (MetaType<F>, MetaType<H>, std::ifstream& input, std::string id);
234
235
236 // Read vertex coordinates from `input` stream and store in into `factory`
237 Sections readPoints (std::ifstream& input, std::string id);
238
239 template <class F, class H>
240 void readPointsAppended (MetaType<F>, MetaType<H>, std::ifstream& input);
241
242
243 // Read cell type, cell offsets and connectivity from `input` stream
244 Sections readCells (std::ifstream& input, std::string id);
245
246 template <class H>
247 void readCellsAppended (MetaType<H>, std::ifstream& input);
248
249 // Read data from appended section in vtk file, starting from `offset`
250 template <class FloatType, class HeaderType>
251 void readAppended (std::ifstream& input, std::vector<FloatType>& values, HeaderType offset);
252
253 // Test whether line belongs to section
254 bool isSection (std::string line,
255 std::string key,
256 Sections current,
257 Sections parent = NO_SECTION) const
258 {
259 bool result = line.substr(1, key.length()) == key;
260 if (result && current != parent)
261 DUNE_THROW(Exception , "<" << key << "> in wrong section." );
262 return result;
263 }
264
265 // Convert a section into a string
266 std::string toString (Sections s) const;
267
268 // Find beginning of appended binary data
269 std::uint64_t findAppendedDataPosition (std::ifstream& input) const;
270
271 // Read attributes from current xml tag
272 std::map<std::string, std::string> parseXml (std::string const& line, bool& closed);
273
274 // clear all vectors
275 void clear ();
276
277 auto comm () const
278 {
280 }
281
282 private:
284
287
290
291 // Temporary data to construct the grid elements
293 std::vector<std::uint64_t> vec_point_ids; //< Global unique vertex ID
294 std::vector<std::uint8_t> vec_types; //< VTK cell type ID
295 std::vector<std::int64_t> vec_offsets; //< offset of vertices of cell
296 std::vector<std::int64_t> vec_connectivity; //< vertex indices of cell
297
298 std::size_t numberOfCells_ = 0; //< Number of cells in the grid
299 std::size_t numberOfPoints_ = 0; //< Number of vertices in the grid
300
301 // offset information for appended data
302 // map: id -> {Name,DataType,NumberOfComponents,Offset}
304
305 // storage for internal point and cell data
306 // map: id -> vector of data entries per point/cell
309
310 // vector of filenames of parallel pieces
312
314 std::uint64_t offset0_ = 0;
315
316 bool read_ = false;
317 };
318
319 // deduction guides
320 template <class Grid>
323
324 template <class GridCreator,
326 VtkReader (GridCreator&)
328
329 template <class GridCreator,
331 VtkReader (GridCreator&&)
333
334 template <class GridCreator,
338
339} // end namespace Dune::Vtk
340
341namespace Dune
342{
343 template <class Grid, class GC = Vtk::ContinuousGridCreator<Grid>, class FieldType = double>
344 class [[deprecated("Use Vtk::VtkReader instead.")]] VtkReader
345 : public Vtk::VtkReader<Grid,GC,FieldType>
346 {
348 public:
349 using Base::Base;
350 };
351
352#pragma GCC diagnostic push
353#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
354 // deduction guides
355 template <class Grid>
358
359 template <class GridCreator,
361 VtkReader (GridCreator&)
363
364 template <class GridCreator,
368#pragma GCC diagnostic pop
369
370} // end namespace Dune
371
372#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:19
#define VTK_ASSERT(cond)
check if condition cond holds; otherwise, throw a VtkError.
Definition errors.hh:29
STL namespace.
Definition writer.hh:16
FormatTypes
Type used for representing the output format.
Definition types.hh:19
CompressorTypes
Definition types.hh:148
DataTypes
Definition types.hh:51
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()
Definition filereader.hh:14
static void fillFactoryImpl(GridFactory< Grid > &, std::string const &, Args &&...)
Definition filereader.hh:69
void type
Definition gridfunctions/common.hh:29
File-Reader for Vtk unstructured .vtu files.
Definition vtkreader.hh:37
typename GridCreator::GlobalCoordinate GlobalCoordinate
Type of global world coordinates.
Definition vtkreader.hh:66
GridFunction< Vtk::CellContext, Range > getCellData(std::string const &name) const
Definition vtkreader.hh:159
std::unique_ptr< Grid > createGrid() const
Definition vtkreader.hh:123
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:249
VtkReader(GridCreator &creator)
Constructor. Stores the references in a non-destroying shared_ptr.
Definition vtkreader.hh:90
FT FieldType
Type of the components of the data to extract from the file.
Definition vtkreader.hh:63
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:43
VtkReader(Args &&... args)
Constructor. Creates a new GridCreator with the passed factory.
Definition vtkreader.hh:85
GridFunction< Vtk::PointContext, Range > getPointData(std::string const &name) const
Definition vtkreader.hh:131
std::vector< DataArrayAttributes > getCellDataAttributes() const
Definition vtkreader.hh:173
GridCreator & gridCreator()
Obtains the creator of the reader.
Definition vtkreader.hh:116
typename Vtk::AssociatedGridFunction< GridCreator, Range, Context >::type GridFunction
Definition vtkreader.hh:73
void read(std::string const &filename, bool fillCreator=true)
Read the grid from file with filename into the GridCreator.
Definition vtkreader.impl.hh:20
G Grid
Type of the grid to construct.
Definition vtkreader.hh:57
GC GridCreator
Type of the associated grid creator.
Definition vtkreader.hh:60
std::vector< DataArrayAttributes > getPointDataAttributes() const
Definition vtkreader.hh:145
VtkReader(std::shared_ptr< GridCreator > creator)
Constructor. Stores the shared_ptr.
Definition vtkreader.hh:100
void fillGridCreator(bool insertPieces=true)
Definition vtkreader.impl.hh:684
std::vector< std::string > const & pieces() const
Return the filenames of parallel pieces.
Definition vtkreader.hh:208
VtkReader(GridCreator &&creator)
Constructor. Stores the references in a non-destroying shared_ptr.
Definition vtkreader.hh:95
Sections section
Definition vtkreader.hh:52
std::string name
Definition vtkreader.hh:48
std::uint64_t offset
Definition vtkreader.hh:51
Vtk::DataTypes type
Definition vtkreader.hh:49
unsigned int components
Definition vtkreader.hh:50
Definition vtkreader.hh:346
T end(T... args)
T find(T... args)
T push_back(T... args)
T reserve(T... args)
T size(T... args)