GCC Code Coverage Report


Directory: gridformat/
File: gridformat/vtk/pvtp_writer.hpp
Date: 2026-10-12 17:50:57
Exec Total Coverage
Lines: 51 53 96.2%
Functions: 77 167 46.1%
Branches: 79 164 48.2%

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::PVTPWriter
7 */
8 #ifndef GRIDFORMAT_VTK_PVTP_WRITER_HPP_
9 #define GRIDFORMAT_VTK_PVTP_WRITER_HPP_
10
11 #include <ostream>
12 #include <string>
13 #include <filesystem>
14
15 #include <gridformat/common/exceptions.hpp>
16 #include <gridformat/common/lvalue_reference.hpp>
17 #include <gridformat/common/output_file.hpp>
18 #include <gridformat/parallel/communication.hpp>
19 #include <gridformat/parallel/helpers.hpp>
20
21 #include <gridformat/grid/grid.hpp>
22 #include <gridformat/xml/element.hpp>
23 #include <gridformat/vtk/vtp_writer.hpp>
24 #include <gridformat/vtk/parallel.hpp>
25
26 namespace GridFormat {
27
28 /*!
29 * \ingroup VTK
30 * \brief Writer for parallel .pvtu files
31 */
32 template<Concepts::UnstructuredGrid Grid,
33 Concepts::Communicator Communicator>
34 class PVTPWriter : public VTK::XMLWriterBase<Grid, PVTPWriter<Grid, Communicator>> {
35 using ParentType = VTK::XMLWriterBase<Grid, PVTPWriter<Grid, Communicator>>;
36
37 public:
38 121 explicit PVTPWriter(LValueReferenceOf<const Grid> grid,
39 Communicator comm,
40 VTK::XMLOptions xml_opts = {})
41 121 : ParentType(grid.get(), ".pvtp", false, xml_opts)
42
2/4
✓ Branch 1 taken 121 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 121 times.
✗ Branch 5 not taken.
363 , _comm(comm)
43 121 {}
44
45 26 const Communicator& communicator() const {
46 26 return _comm;
47 }
48
49 private:
50 Communicator _comm;
51
52 70 PVTPWriter _with(VTK::XMLOptions xml_opts) const override {
53
1/2
✓ Branch 4 taken 70 times.
✗ Branch 5 not taken.
70 return PVTPWriter{this->grid(), _comm, std::move(xml_opts)};
54 }
55
56 ✗ void _write(std::ostream&) const override {
57 ✗ throw InvalidState(
58 "PVTPWriter does not support direct export into stream. "
59 "Use overload with filename instead!"
60 );
61 }
62
63 75 virtual void _write(const std::string& filename_with_ext) const override {
64 // the collective invocations also ensure that all pieces are written before the main file,
65 // and that the main file is written before returning
66
2/2
✓ Branch 1 taken 69 times.
✓ Branch 2 taken 6 times.
75 Parallel::invoke_collectively(_comm, [&] () {
67 75 _write_piece(filename_with_ext);
68
2/4
✓ Branch 1 taken 75 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 75 times.
✗ Branch 5 not taken.
162 }, "Writing the pieces of '" + filename_with_ext + "'");
69
2/2
✓ Branch 1 taken 66 times.
✓ Branch 2 taken 3 times.
69 Parallel::invoke_collectively(_comm, [&] () {
70
2/2
✓ Branch 1 taken 35 times.
✓ Branch 2 taken 34 times.
69 if (Parallel::rank(_comm) == 0)
71 35 _write_pvtu_file(filename_with_ext);
72
2/4
✓ Branch 1 taken 69 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 69 times.
✗ Branch 5 not taken.
144 }, "Writing '" + filename_with_ext + "'");
73 66 }
74
75 75 void _write_piece(const std::string& par_filename) const {
76
1/2
✓ Branch 3 taken 75 times.
✗ Branch 4 not taken.
75 VTPWriter writer{this->grid(), this->_xml_opts};
77
1/2
✓ Branch 1 taken 75 times.
✗ Branch 2 not taken.
75 this->copy_fields(writer);
78
6/8
✓ Branch 1 taken 74 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 74 times.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 69 times.
✓ Branch 8 taken 5 times.
80 writer.write(PVTK::piece_basefilename(par_filename, Parallel::rank(_comm)));
79 75 }
80
81 35 void _write_pvtu_file(const std::string& filename_with_ext) const {
82
1/2
✓ Branch 1 taken 35 times.
✗ Branch 2 not taken.
70 XMLElement pvtk_xml("VTKFile");
83
2/4
✓ Branch 1 taken 35 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 35 times.
✗ Branch 5 not taken.
70 pvtk_xml.set_attribute("type", "PPolyData");
84
85
2/4
✓ Branch 1 taken 35 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 35 times.
✗ Branch 5 not taken.
70 XMLElement& grid = pvtk_xml.add_child("PPolyData");
86
2/4
✓ Branch 1 taken 35 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 35 times.
✗ Branch 5 not taken.
70 XMLElement& ppoint_data = grid.add_child("PPointData");
87
2/4
✓ Branch 1 taken 35 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 35 times.
✗ Branch 5 not taken.
35 XMLElement& pcell_data = grid.add_child("PCellData");
88 210 std::visit([&] (const auto& encoder) {
89 175 std::visit([&] (const auto& data_format) {
90 35 PVTK::PDataArrayHelper pdata_helper{encoder, data_format, ppoint_data};
91 35 PVTK::PDataArrayHelper cdata_helper{encoder, data_format, pcell_data};
92
8/24
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 28 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 28 times.
✗ Branch 11 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✓ Branch 19 taken 1 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 1 times.
✗ Branch 23 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
✓ Branch 31 taken 2 times.
✗ Branch 32 not taken.
✓ Branch 34 taken 2 times.
✗ Branch 35 not taken.
707 std::ranges::for_each(this->_point_field_names(), [&] (const std::string& name) {
93 168 pdata_helper.add(name, this->_get_point_field(name));
94 });
95
8/24
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 28 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 28 times.
✗ Branch 11 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✓ Branch 19 taken 1 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 1 times.
✗ Branch 23 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
✓ Branch 31 taken 2 times.
✗ Branch 32 not taken.
✓ Branch 34 taken 2 times.
✗ Branch 35 not taken.
703 std::ranges::for_each(this->_cell_field_names(), [&] (const std::string& name) {
96 167 cdata_helper.add(name, this->_get_cell_field(name));
97 });
98
3/6
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 29 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 2 times.
✗ Branch 8 not taken.
35 }, this->_xml_settings.data_format);
99
1/2
✓ Branch 1 taken 35 times.
✗ Branch 2 not taken.
35 }, this->_xml_settings.encoder);
100
101
4/8
✓ Branch 1 taken 35 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 35 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 35 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 35 times.
✗ Branch 11 not taken.
105 XMLElement& point_array = grid.add_child("PPoints").add_child("PDataArray");
102
2/4
✓ Branch 1 taken 35 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 35 times.
✗ Branch 5 not taken.
70 point_array.set_attribute("NumberOfComponents", "3");
103 140 std::visit([&] <typename T> (const Precision<T>& prec) {
104
6/12
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 33 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 33 times.
✗ Branch 9 not taken.
✓ Branch 16 taken 2 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 2 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 2 times.
✗ Branch 23 not taken.
105 point_array.set_attribute("type", VTK::attribute_name(prec));
105
1/2
✓ Branch 1 taken 35 times.
✗ Branch 2 not taken.
35 }, this->_xml_settings.coordinate_precision);
106
107
2/4
✓ Branch 1 taken 35 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 35 times.
✗ Branch 5 not taken.
35 std::ranges::for_each(Parallel::ranks(_comm), [&] (int rank) {
108
5/10
✓ Branch 1 taken 69 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 69 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 69 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 69 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 69 times.
✗ Branch 14 not taken.
414 grid.add_child("Piece").set_attribute("Source", std::filesystem::path{
109
2/4
✓ Branch 1 taken 69 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 69 times.
✗ Branch 5 not taken.
138 PVTK::piece_basefilename(filename_with_ext, rank) + ".vtp"
110
1/2
✓ Branch 1 taken 69 times.
✗ Branch 2 not taken.
207 }.filename());
111 });
112
113
2/4
✓ Branch 2 taken 35 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 35 times.
✗ Branch 6 not taken.
35 this->_set_default_active_fields(pvtk_xml.get_child("PPolyData"));
114
2/2
✓ Branch 1 taken 33 times.
✓ Branch 2 taken 2 times.
35 write_to(filename_with_ext, [&] (std::ostream& s) {
115
2/4
✓ Branch 1 taken 33 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 33 times.
✗ Branch 5 not taken.
33 write_xml_with_version_header(pvtk_xml, s, Indentation{{.width = 2}});
116 });
117 35 }
118 };
119
120 template<typename G, Concepts::Communicator C>
121 PVTPWriter(G&&, const C&, VTK::XMLOptions = {}) -> PVTPWriter<std::remove_cvref_t<G>, C>;
122
123 } // namespace GridFormat
124
125 #endif // GRIDFORMAT_VTK_PVTP_WRITER_HPP_
126