GridFormat 0.2.1
I/O-Library for grid-like data structures
Loading...
Searching...
No Matches
hdf_reader.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2022-2023 Dennis Gläser <dennis.glaeser@iws.uni-stuttgart.de>
2// SPDX-License-Identifier: MIT
8#ifndef GRIDFORMAT_VTK_HDF_READER_HPP_
9#define GRIDFORMAT_VTK_HDF_READER_HPP_
10#if GRIDFORMAT_HAVE_HIGH_FIVE
11
12#include <memory>
13#include <ranges>
14#include <iterator>
15#include <type_traits>
16
17#include <gridformat/parallel/communication.hpp>
18
22
23namespace GridFormat {
24
30template<typename Communicator = GridFormat::NullCommunicator>
31class VTKHDFReader : public GridReader {
32 public:
33 explicit VTKHDFReader() requires (std::is_default_constructible_v<Communicator>) = default;
34 explicit VTKHDFReader(const Communicator& comm) : _comm{comm} {}
35
36 private:
37 void _open(const std::string& filename, typename GridReader::FieldNames& fields) override {
38 std::string image_err;
39 std::string unstructured_err;
40 try {
42 reader.open(filename);
43 _reader = std::make_unique<VTKHDFImageGridReader>(std::move(reader));
44 } catch (const Exception& image_e) {
45 image_err = image_e.what();
46 try {
47 using C = Communicator;
49 reader.open(filename);
50 _reader = std::make_unique<VTKHDFUnstructuredGridReader<C>>(std::move(reader));
51 } catch (const Exception& unstructured_e) {
52 unstructured_err = unstructured_e.what();
53 }
54 }
55
56 if (!_reader)
57 throw IOError(
58 "Could not open '" + filename + "' as vtk-hdf file.\n" +
59 "Error when trying to read as 'ImageData': " + image_err + "\n" +
60 "Error when trying to read as 'UnstructuredGrid': " + unstructured_err
61 );
62
63 _copy_fields(fields);
64 }
65
66 std::string _name() const override {
67 if (_reader)
68 return _reader->name();
69 return "VTKHDFReader";
70 }
71
72 void _close() override {
73 if (_reader)
74 _reader->close();
75 _reader.reset();
76 }
77
78 void _visit_cells(const CellVisitor& v) const override {
79 _access().visit_cells(v);
80 }
81
82 FieldPtr _points() const override {
83 return _access().points();
84 }
85
86 FieldPtr _cell_field(std::string_view name) const override {
87 return _access().cell_field(name);
88 }
89
90 FieldPtr _point_field(std::string_view name) const override {
91 return _access().point_field(name);
92 }
93
94 FieldPtr _meta_data_field(std::string_view name) const override {
95 return _access().meta_data_field(name);
96 }
97
98 std::size_t _number_of_cells() const override {
99 return _access().number_of_cells();
100 }
101
102 std::size_t _number_of_points() const override {
103 return _access().number_of_points();
104 }
105
106 std::size_t _number_of_pieces() const override {
107 return _access().number_of_pieces();
108 }
109
110 typename GridReader::PieceLocation _location() const override {
111 return _access().location();
112 }
113
114 std::vector<double> _ordinates(unsigned int i) const override {
115 return _access().ordinates(i);
116 }
117
118 std::array<double, 3> _spacing() const override {
119 return _access().spacing();
120 }
121
122 std::array<double, 3> _origin() const override {
123 return _access().origin();
124 }
125
126 std::array<double, 3> _basis_vector(unsigned int i) const override {
127 return _access().basis_vector(i);
128 }
129
130 bool _is_sequence() const override {
131 return _access().is_sequence();
132 }
133
134 std::size_t _number_of_steps() const override {
135 return _access().number_of_steps();
136 }
137
138 double _time_at_step(std::size_t step) const override {
139 return _access().time_at_step(step);
140 }
141
142 void _set_step(std::size_t step, typename GridReader::FieldNames& names) override {
143 _access().set_step(step);
144 names.clear();
145 _copy_fields(names);
146 }
147
148 void _copy_fields(typename GridReader::FieldNames& names) {
149 std::ranges::copy(cell_field_names(_access()), std::back_inserter(names.cell_fields));
150 std::ranges::copy(point_field_names(_access()), std::back_inserter(names.point_fields));
151 std::ranges::copy(meta_data_field_names(_access()), std::back_inserter(names.meta_data_fields));
152 }
153
154 const GridReader& _access() const {
155 if (!_reader)
156 throw InvalidState("No active file opened");
157 return *_reader;
158 }
159
160 GridReader& _access() {
161 if (!_reader)
162 throw InvalidState("No active file opened");
163 return *_reader;
164 }
165
166 Communicator _comm;
167 std::unique_ptr<GridReader> _reader;
168};
169
170} // namespace GridFormat
171
172#endif // GRIDFORMAT_HAVE_HIGH_FIVE
173#endif // GRIDFORMAT_VTK_HDF_READER_HPP_
Abstract base class for all readers, defines the common interface.
Definition: reader.hpp:51
void visit_cells(const CellVisitor &visitor) const
Visit all cells in the grid read from the file.
Definition: reader.hpp:201
bool is_sequence() const
Return true if the read file is a sequence.
Definition: reader.hpp:152
std::size_t number_of_pieces() const
Definition: reader.hpp:108
FieldPtr meta_data_field(std::string_view name) const
Return the meta data field with the given name.
Definition: reader.hpp:221
const std::string & filename() const
Return the name of the opened grid file (empty string until open() is called)
Definition: reader.hpp:92
friend std::ranges::range auto point_field_names(const GridReader &reader)
Return a range over the names of all read point fields.
Definition: reader.hpp:231
std::string name() const
Return the name of this reader.
Definition: reader.hpp:73
Vector origin() const
Return the origin of the grid (only available for image grid formats)
Definition: reader.hpp:140
friend std::ranges::range auto meta_data_field_names(const GridReader &reader)
Return a range over the names of all read metadata fields.
Definition: reader.hpp:236
void open(const std::string &filename)
Open the given grid file.
Definition: reader.hpp:78
void set_step(std::size_t step_idx)
Set the step from which to read data (only available for sequence formats)
Definition: reader.hpp:167
FieldPtr point_field(std::string_view name) const
Return the point field with the given name.
Definition: reader.hpp:216
double time_at_step(std::size_t step_idx) const
Return the time at the current step (only available for sequence formats)
Definition: reader.hpp:162
PieceLocation location() const
Return the location of this piece in a structured grid (only available for structured grid formats)
Definition: reader.hpp:123
FieldPtr cell_field(std::string_view name) const
Return the cell field with the given name.
Definition: reader.hpp:211
friend std::ranges::range auto cell_field_names(const GridReader &reader)
Return a range over the names of all read cell fields.
Definition: reader.hpp:226
FieldPtr points() const
Return the points of the grid as field.
Definition: reader.hpp:206
std::vector< double > ordinates(unsigned int direction) const
Return the ordinates of the grid (only available for rectilinear grid formats)
Definition: reader.hpp:128
std::size_t number_of_cells() const
Return the number of cells in the grid read from the file.
Definition: reader.hpp:97
std::size_t number_of_points() const
Return the number of points in the grid read from the file.
Definition: reader.hpp:102
Vector spacing() const
Return the spacing of the grid (only available for image grid formats)
Definition: reader.hpp:135
Vector basis_vector(unsigned int direction) const
Return the basis vector of the grid in the given direction (only available for image grid formats)
Definition: reader.hpp:145
std::size_t number_of_steps() const
Return the number of available steps (only available for sequence formats)
Definition: reader.hpp:157
Reader for the VTK-HDF file format for image grids.
Definition: hdf_image_grid_reader.hpp:37
Convenience reader for the vtk-hdf file format that supports both the image & unstructured grid file ...
Definition: hdf_reader.hpp:31
Reader for the VTK-HDF file format for unstructured grids.
Definition: hdf_unstructured_grid_reader.hpp:36
Base class for grid data readers.
std::shared_ptr< const Field > FieldPtr
Pointer type used by writers/readers for fields.
Definition: field.hpp:186
Reader for the VTK HDF file format for image grids.
Reader for the VTK HDF file format for unstructured grids.
Definition: reader.hpp:266
Describes the location of a piece within a distributed structured grid.
Definition: reader.hpp:57