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 | * \copydoc GridFormat::VTRWriter | ||
7 | */ | ||
8 | #ifndef GRIDFORMAT_VTK_VTR_WRITER_HPP_ | ||
9 | #define GRIDFORMAT_VTK_VTR_WRITER_HPP_ | ||
10 | |||
11 | #include <array> | ||
12 | #include <ranges> | ||
13 | #include <ostream> | ||
14 | #include <utility> | ||
15 | #include <string> | ||
16 | #include <optional> | ||
17 | #include <iterator> | ||
18 | |||
19 | #include <gridformat/common/field.hpp> | ||
20 | #include <gridformat/common/field_storage.hpp> | ||
21 | #include <gridformat/common/lvalue_reference.hpp> | ||
22 | |||
23 | #include <gridformat/grid/grid.hpp> | ||
24 | #include <gridformat/grid/type_traits.hpp> | ||
25 | #include <gridformat/vtk/common.hpp> | ||
26 | #include <gridformat/vtk/xml.hpp> | ||
27 | |||
28 | namespace GridFormat { | ||
29 | |||
30 | /*! | ||
31 | * \ingroup VTK | ||
32 | * \brief Writer for .vtr file format | ||
33 | */ | ||
34 | template<Concepts::RectilinearGrid Grid> | ||
35 | class VTRWriter : public VTK::XMLWriterBase<Grid, VTRWriter<Grid>> { | ||
36 | using ParentType = VTK::XMLWriterBase<Grid, VTRWriter<Grid>>; | ||
37 | using CType = CoordinateType<Grid>; | ||
38 | static constexpr std::size_t dim = dimension<Grid>; | ||
39 | static constexpr std::size_t space_dim = 3; | ||
40 | static_assert(dim <= 3); | ||
41 | |||
42 | public: | ||
43 | struct Domain { | ||
44 | std::array<std::size_t, dim> whole_extent; | ||
45 | }; | ||
46 | |||
47 | using Offset = std::array<std::size_t, dim>; | ||
48 | |||
49 | 14216 | explicit VTRWriter(LValueReferenceOf<const Grid> grid, | |
50 | VTK::XMLOptions xml_opts = {}) | ||
51 |
2/4✓ Branch 2 taken 7174 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 7174 times.
✗ Branch 7 not taken.
|
42648 | : ParentType(grid.get(), ".vtr", true, std::move(xml_opts)) |
52 | 14216 | {} | |
53 | |||
54 | 4122 | VTRWriter as_piece_for(Domain domain) const { | |
55 | 4122 | auto result = this->with(this->_xml_opts); | |
56 | 4122 | result._domain = std::move(domain); | |
57 | 4122 | result._offset = _offset; | |
58 | 4122 | return result; | |
59 | } | ||
60 | |||
61 | 4122 | VTRWriter with_offset(Offset offset) const { | |
62 | 4122 | auto result = this->with(this->_xml_opts); | |
63 | 4122 | result._offset = std::move(offset); | |
64 | 4122 | result._domain = _domain; | |
65 | 4122 | return result; | |
66 | } | ||
67 | |||
68 | private: | ||
69 | 9504 | VTRWriter _with(VTK::XMLOptions xml_opts) const override { | |
70 |
1/2✓ Branch 4 taken 4794 times.
✗ Branch 5 not taken.
|
9504 | return VTRWriter{this->grid(), std::move(xml_opts)}; |
71 | } | ||
72 | |||
73 | 4759 | void _write(std::ostream& s) const override { | |
74 |
2/4✓ Branch 1 taken 2409 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2409 times.
✗ Branch 5 not taken.
|
9518 | auto context = this->_get_write_context("RectilinearGrid"); |
75 |
1/2✓ Branch 1 taken 2409 times.
✗ Branch 2 not taken.
|
4759 | _set_attributes(context); |
76 | |||
77 | 4759 | FieldStorage vtk_point_fields; | |
78 | 4759 | FieldStorage vtk_cell_fields; | |
79 |
2/4✓ Branch 1 taken 2409 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2409 times.
✗ Branch 5 not taken.
|
29607 | std::ranges::for_each(this->_point_field_names(), [&] (const std::string& name) { |
80 |
6/12✓ Branch 1 taken 2941 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2941 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 2941 times.
✗ Branch 8 not taken.
✓ Branch 15 taken 9722 times.
✗ Branch 16 not taken.
✓ Branch 18 taken 9722 times.
✗ Branch 19 not taken.
✓ Branch 21 taken 9722 times.
✗ Branch 22 not taken.
|
12663 | vtk_cell_fields.set(name, VTK::make_vtk_field(this->_get_point_field_ptr(name))); |
81 |
4/8✓ Branch 2 taken 2941 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 2941 times.
✗ Branch 7 not taken.
✓ Branch 12 taken 9722 times.
✗ Branch 13 not taken.
✓ Branch 16 taken 9722 times.
✗ Branch 17 not taken.
|
12663 | this->_set_data_array(context, "Piece/PointData", name, vtk_cell_fields.get(name)); |
82 | }); | ||
83 |
2/4✓ Branch 1 taken 2409 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2409 times.
✗ Branch 5 not taken.
|
29607 | std::ranges::for_each(this->_cell_field_names(), [&] (const std::string& name) { |
84 |
6/12✓ Branch 1 taken 2938 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2938 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 2938 times.
✗ Branch 8 not taken.
✓ Branch 15 taken 9722 times.
✗ Branch 16 not taken.
✓ Branch 18 taken 9722 times.
✗ Branch 19 not taken.
✓ Branch 21 taken 9722 times.
✗ Branch 22 not taken.
|
12660 | vtk_cell_fields.set(name, VTK::make_vtk_field(this->_get_cell_field_ptr(name))); |
85 |
4/8✓ Branch 2 taken 2938 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 2938 times.
✗ Branch 7 not taken.
✓ Branch 12 taken 9722 times.
✗ Branch 13 not taken.
✓ Branch 16 taken 9722 times.
✗ Branch 17 not taken.
|
12660 | this->_set_data_array(context, "Piece/CellData", name, vtk_cell_fields.get(name)); |
86 | }); | ||
87 | |||
88 |
1/2✓ Branch 1 taken 2409 times.
✗ Branch 2 not taken.
|
4759 | const auto coord_fields = _make_ordinate_fields(); |
89 |
2/2✓ Branch 0 taken 6675 times.
✓ Branch 1 taken 2133 times.
|
17380 | for (unsigned dir = 0; dir < space_dim; ++dir) |
90 |
3/4✓ Branch 4 taken 6675 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 6399 times.
✓ Branch 9 taken 276 times.
|
14277 | this->_set_data_array(context, "Piece/Coordinates", "X_" + std::to_string(dir), *coord_fields[dir]); |
91 |
1/2✓ Branch 2 taken 2133 times.
✗ Branch 3 not taken.
|
4207 | this->_write_xml(std::move(context), s); |
92 | 6415 | } | |
93 | |||
94 | 4759 | void _set_attributes(typename ParentType::WriteContext& context) const { | |
95 | 4759 | _set_domain_attributes(context); | |
96 | 4759 | _set_extent_attributes(context); | |
97 | 4759 | } | |
98 | |||
99 | 4759 | void _set_domain_attributes(typename ParentType::WriteContext& context) const { | |
100 | using VTK::CommonDetail::extents_string; | ||
101 |
2/2✓ Branch 1 taken 2082 times.
✓ Branch 2 taken 327 times.
|
4759 | if (_domain) |
102 |
3/6✓ Branch 2 taken 2082 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 2082 times.
✗ Branch 6 not taken.
✓ Branch 9 taken 2082 times.
✗ Branch 10 not taken.
|
12366 | this->_set_attribute(context, "", "WholeExtent", extents_string(_domain->whole_extent)); |
103 | else | ||
104 |
3/6✓ Branch 2 taken 327 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 327 times.
✗ Branch 6 not taken.
✓ Branch 9 taken 327 times.
✗ Branch 10 not taken.
|
1911 | this->_set_attribute(context, "", "WholeExtent", extents_string(this->grid())); |
105 | 4759 | } | |
106 | |||
107 | 4759 | void _set_extent_attributes(typename ParentType::WriteContext& context) const { | |
108 | using VTK::CommonDetail::extents_string; | ||
109 |
2/2✓ Branch 1 taken 2082 times.
✓ Branch 2 taken 327 times.
|
4759 | if (int i = 0; _offset) { |
110 | 4122 | auto begin = (*_offset); | |
111 |
1/2✓ Branch 2 taken 18 times.
✗ Branch 3 not taken.
|
4122 | auto end = GridFormat::extents(this->grid()); |
112 |
1/2✓ Branch 1 taken 2082 times.
✗ Branch 2 not taken.
|
9918 | std::ranges::for_each(end, [&] (std::integral auto& ex) { ex += begin[i++]; }); |
113 |
3/6✓ Branch 1 taken 2082 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2082 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 2082 times.
✗ Branch 9 not taken.
|
12366 | this->_set_attribute(context, "Piece", "Extent", extents_string(begin, end)); |
114 | } else { | ||
115 |
3/6✓ Branch 2 taken 327 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 327 times.
✗ Branch 6 not taken.
✓ Branch 9 taken 327 times.
✗ Branch 10 not taken.
|
1911 | this->_set_attribute(context, "Piece", "Extent", extents_string(this->grid())); |
116 | } | ||
117 | 4759 | } | |
118 | |||
119 | 4759 | std::array<FieldPtr, space_dim> _make_ordinate_fields() const { | |
120 | 4759 | std::array<FieldPtr, space_dim> result; | |
121 | 2468 | std::visit([&] <typename T> (const Precision<T>& prec) { | |
122 |
8/8✓ Branch 0 taken 1533 times.
✓ Branch 1 taken 511 times.
✓ Branch 2 taken 4866 times.
✓ Branch 3 taken 1622 times.
✓ Branch 4 taken 180 times.
✓ Branch 5 taken 60 times.
✓ Branch 6 taken 648 times.
✓ Branch 7 taken 216 times.
|
9636 | for (unsigned dir = 0; dir < space_dim; ++dir) { |
123 |
6/8✓ Branch 0 taken 1022 times.
✓ Branch 1 taken 511 times.
✓ Branch 2 taken 4866 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 120 times.
✓ Branch 5 taken 60 times.
✓ Branch 6 taken 648 times.
✗ Branch 7 not taken.
|
7227 | if (dir < dim) |
124 |
8/16✓ Branch 2 taken 1022 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 1022 times.
✗ Branch 7 not taken.
✓ Branch 18 taken 4866 times.
✗ Branch 19 not taken.
✓ Branch 22 taken 4866 times.
✗ Branch 23 not taken.
✓ Branch 34 taken 120 times.
✗ Branch 35 not taken.
✓ Branch 38 taken 120 times.
✗ Branch 39 not taken.
✓ Branch 50 taken 648 times.
✗ Branch 51 not taken.
✓ Branch 54 taken 648 times.
✗ Branch 55 not taken.
|
6656 | result[dir] = make_field_ptr(RangeField{ordinates(this->grid(), dir), prec}); |
125 | else | ||
126 |
2/8✓ Branch 3 taken 511 times.
✗ Branch 4 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✓ Branch 27 taken 60 times.
✗ Branch 28 not taken.
✗ Branch 39 not taken.
✗ Branch 40 not taken.
|
571 | result[dir] = make_field_ptr(RangeField{std::vector<double>{}, prec}); |
127 | } | ||
128 |
1/2✓ Branch 1 taken 2409 times.
✗ Branch 2 not taken.
|
4759 | }, this->_xml_settings.coordinate_precision); |
129 | 4759 | return result; | |
130 | ✗ | } | |
131 | |||
132 | std::optional<Domain> _domain; | ||
133 | std::optional<Offset> _offset; | ||
134 | }; | ||
135 | |||
136 | template<typename G> | ||
137 | VTRWriter(G&&, VTK::XMLOptions = {}) -> VTRWriter<std::remove_cvref_t<G>>; | ||
138 | |||
139 | namespace Traits { | ||
140 | |||
141 | template<typename... Args> | ||
142 | struct WritesConnectivity<VTRWriter<Args...>> : public std::false_type {}; | ||
143 | |||
144 | } // namespace Traits | ||
145 | } // namespace GridFormat | ||
146 | |||
147 | #endif // GRIDFORMAT_VTK_VTR_WRITER_HPP_ | ||
148 |