GCC Code Coverage Report


Directory: gridformat/
File: gridformat/vtk/hdf_reader.hpp
Date: 2024-11-10 16:24:00
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 16 explicit VTKHDFReader() requires (std::is_default_constructible_v<Communicator>) = default;
34 48 explicit VTKHDFReader(const Communicator& comm) : _comm{comm} {}
35
36 private:
37 108 void _open(const std::string& filename, typename GridReader::FieldNames& fields) override {
38 108 std::string image_err;
39 108 std::string unstructured_err;
40 try {
41
1/2
✓ Branch 1 taken 66 times.
✗ Branch 2 not taken.
108 VTKHDFImageGridReader reader;
42
2/2
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 38 times.
108 reader.open(filename);
43
1/2
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
44 _reader = std::make_unique<VTKHDFImageGridReader>(std::move(reader));
44
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 38 times.
236 } catch (const Exception& image_e) {
45
1/2
✓ Branch 2 taken 38 times.
✗ Branch 3 not taken.
64 image_err = image_e.what();
46 try {
47 using C = Communicator;
48
1/2
✓ Branch 1 taken 38 times.
✗ Branch 2 not taken.
64 VTKHDFUnstructuredGridReader<C> reader{_comm};
49
1/2
✓ Branch 1 taken 38 times.
✗ Branch 2 not taken.
64 reader.open(filename);
50
1/2
✓ Branch 2 taken 38 times.
✗ Branch 3 not taken.
64 _reader = std::make_unique<VTKHDFUnstructuredGridReader<C>>(std::move(reader));
51
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
64 } catch (const Exception& unstructured_e) {
52 unstructured_err = unstructured_e.what();
53 }
54 }
55
56
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 66 times.
108 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 66 times.
✗ Branch 2 not taken.
108 _copy_fields(fields);
64 108 }
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 618 void _visit_cells(const CellVisitor& v) const override {
79 618 _access().visit_cells(v);
80 618 }
81
82 206 FieldPtr _points() const override {
83 206 return _access().points();
84 }
85
86 1722 FieldPtr _cell_field(std::string_view name) const override {
87 1722 return _access().cell_field(name);
88 }
89
90 1793 FieldPtr _point_field(std::string_view name) const override {
91 1793 return _access().point_field(name);
92 }
93
94 105 FieldPtr _meta_data_field(std::string_view name) const override {
95 105 return _access().meta_data_field(name);
96 }
97
98 655 std::size_t _number_of_cells() const override {
99 655 return _access().number_of_cells();
100 }
101
102 859 std::size_t _number_of_points() const override {
103 859 return _access().number_of_points();
104 }
105
106 19 std::size_t _number_of_pieces() const override {
107 19 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 112 std::size_t _number_of_steps() const override {
135 112 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 305 void _set_step(std::size_t step, typename GridReader::FieldNames& names) override {
143 305 _access().set_step(step);
144 305 names.clear();
145 305 _copy_fields(names);
146 305 }
147
148 413 void _copy_fields(typename GridReader::FieldNames& names) {
149
3/6
✓ Branch 2 taken 256 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 256 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 256 times.
✗ Branch 9 not taken.
413 std::ranges::copy(cell_field_names(_access()), std::back_inserter(names.cell_fields));
150
3/6
✓ Branch 2 taken 256 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 256 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 256 times.
✗ Branch 9 not taken.
413 std::ranges::copy(point_field_names(_access()), std::back_inserter(names.point_fields));
151
3/6
✓ Branch 2 taken 256 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 256 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 256 times.
✗ Branch 9 not taken.
413 std::ranges::copy(meta_data_field_names(_access()), std::back_inserter(names.meta_data_fields));
152 413 }
153
154 6435 const GridReader& _access() const {
155
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3996 times.
6435 if (!_reader)
156 throw InvalidState("No active file opened");
157 6435 return *_reader;
158 }
159
160 958 GridReader& _access() {
161
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 372 times.
958 if (!_reader)
162 throw InvalidState("No active file opened");
163 958 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