GCC Code Coverage Report


Directory: gridformat/
File: gridformat/vtk/hdf_reader.hpp
Date: 2026-10-03 16:36:24
Exec Total Coverage
Lines: 71 82 86.6%
Functions: 41 48 85.4%
Branches: 24 72 33.3%

Line Branch Exec Source
1 // SPDX-FileCopyrightText: 2022-2023 Dennis Gläser <dennis.glaeser@iws.uni-stuttgart.de>
2 // SPDX-License-Identifier: MIT
3 /*!
4 * \file
5 * \ingroup VTK
6 * \brief Readers for the VTK HDF file formats.
7 */
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
19 #include <gridformat/grid/reader.hpp>
20 #include <gridformat/vtk/hdf_image_grid_reader.hpp>
21 #include <gridformat/vtk/hdf_unstructured_grid_reader.hpp>
22
23 namespace GridFormat {
24
25 /*!
26 * \ingroup VTK
27 * \brief Convenience reader for the vtk-hdf file format that supports both the
28 * image & unstructured grid file formats.
29 */
30 template<typename Communicator = GridFormat::NullCommunicator>
31 class VTKHDFReader : public GridReader {
32 public:
33 32 explicit VTKHDFReader() requires (std::is_default_constructible_v<Communicator>) = default;
34 48 explicit VTKHDFReader(const Communicator& comm) : _comm{comm} {}
35
36 private:
37 124 void _open(const std::string& filename, typename GridReader::FieldNames& fields) override {
38 124 std::string image_err;
39 124 std::string unstructured_err;
40 try {
41
1/2
✓ Branch 1 taken 82 times.
✗ Branch 2 not taken.
124 VTKHDFImageGridReader reader;
42
2/2
✓ Branch 1 taken 31 times.
✓ Branch 2 taken 51 times.
124 reader.open(filename);
43
1/2
✓ Branch 2 taken 31 times.
✗ Branch 3 not taken.
47 _reader = std::make_unique<VTKHDFImageGridReader>(std::move(reader));
44
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 51 times.
278 } catch (const Exception& image_e) {
45
1/2
✓ Branch 2 taken 51 times.
✗ Branch 3 not taken.
77 image_err = image_e.what();
46 try {
47 using C = Communicator;
48
1/2
✓ Branch 1 taken 51 times.
✗ Branch 2 not taken.
77 VTKHDFUnstructuredGridReader<C> reader{_comm};
49
1/2
✓ Branch 1 taken 51 times.
✗ Branch 2 not taken.
77 reader.open(filename);
50
1/2
✓ Branch 2 taken 51 times.
✗ Branch 3 not taken.
77 _reader = std::make_unique<VTKHDFUnstructuredGridReader<C>>(std::move(reader));
51
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
77 } catch (const Exception& unstructured_e) {
52 ✗ unstructured_err = unstructured_e.what();
53 }
54 }
55
56
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 82 times.
124 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
1/2
✓ Branch 1 taken 82 times.
✗ Branch 2 not taken.
124 _copy_fields(fields);
64 124 }
65
66 2 std::string _name() const override {
67
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 if (_reader)
68 2 return _reader->name();
69 ✗ return "VTKHDFReader";
70 }
71
72 90 void _close() override {
73
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 54 times.
90 if (_reader)
74 ✗ _reader->close();
75 90 _reader.reset();
76 90 }
77
78 622 void _visit_cells(const CellVisitor& v) const override {
79 622 _access().visit_cells(v);
80 622 }
81
82 210 FieldPtr _points() const override {
83 210 return _access().points();
84 }
85
86 1732 FieldPtr _cell_field(std::string_view name) const override {
87 1732 return _access().cell_field(name);
88 }
89
90 1797 FieldPtr _point_field(std::string_view name) const override {
91 1797 return _access().point_field(name);
92 }
93
94 319 FieldPtr _meta_data_field(std::string_view name) const override {
95 319 return _access().meta_data_field(name);
96 }
97
98 679 std::size_t _number_of_cells() const override {
99 679 return _access().number_of_cells();
100 }
101
102 867 std::size_t _number_of_points() const override {
103 867 return _access().number_of_points();
104 }
105
106 23 std::size_t _number_of_pieces() const override {
107 23 return _access().number_of_pieces();
108 }
109
110 4 typename GridReader::PieceLocation _location() const override {
111 4 return _access().location();
112 }
113
114 ✗ std::vector<double> _ordinates(unsigned int i) const override {
115 ✗ return _access().ordinates(i);
116 }
117
118 3 std::array<double, 3> _spacing() const override {
119 3 return _access().spacing();
120 }
121
122 2 std::array<double, 3> _origin() const override {
123 2 return _access().origin();
124 }
125
126 6 std::array<double, 3> _basis_vector(unsigned int i) const override {
127 6 return _access().basis_vector(i);
128 }
129
130 86 bool _is_sequence() const override {
131 86 return _access().is_sequence();
132 }
133
134 158 std::size_t _number_of_steps() const override {
135 158 return _access().number_of_steps();
136 }
137
138 245 double _time_at_step(std::size_t step) const override {
139 245 return _access().time_at_step(step);
140 }
141
142 335 void _set_step(std::size_t step, typename GridReader::FieldNames& names) override {
143 335 _access().set_step(step);
144 335 names.clear();
145 335 _copy_fields(names);
146 335 }
147
148 459 void _copy_fields(typename GridReader::FieldNames& names) {
149
3/6
✓ Branch 2 taken 302 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 302 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 302 times.
✗ Branch 9 not taken.
459 std::ranges::copy(cell_field_names(_access()), std::back_inserter(names.cell_fields));
150
3/6
✓ Branch 2 taken 302 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 302 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 302 times.
✗ Branch 9 not taken.
459 std::ranges::copy(point_field_names(_access()), std::back_inserter(names.point_fields));
151
3/6
✓ Branch 2 taken 302 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 302 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 302 times.
✗ Branch 9 not taken.
459 std::ranges::copy(meta_data_field_names(_access()), std::back_inserter(names.meta_data_fields));
152 459 }
153
154 6753 const GridReader& _access() const {
155
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4256 times.
6753 if (!_reader)
156 ✗ throw InvalidState("No active file opened");
157 6753 return *_reader;
158 }
159
160 1126 GridReader& _access() {
161
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 540 times.
1126 if (!_reader)
162 ✗ throw InvalidState("No active file opened");
163 1126 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_
174