GCC Code Coverage Report


Directory: gridformat/
File: gridformat/vtk/pvtp_writer.hpp
Date: 2024-11-20 14:41:59
Exec Total Coverage
Lines: 49 51 96.1%
Functions: 55 122 45.1%
Branches: 68 150 45.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 * \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 <fstream>
14 #include <filesystem>
15
16 #include <gridformat/common/exceptions.hpp>
17 #include <gridformat/common/lvalue_reference.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 116 explicit PVTPWriter(LValueReferenceOf<const Grid> grid,
39 Communicator comm,
40 VTK::XMLOptions xml_opts = {})
41 116 : ParentType(grid.get(), ".pvtp", false, xml_opts)
42
2/4
✓ Branch 1 taken 116 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 116 times.
✗ Branch 5 not taken.
348 , _comm(comm)
43 116 {}
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 70 virtual void _write(const std::string& filename_with_ext) const override {
64 70 _write_piece(filename_with_ext);
65 66 Parallel::barrier(_comm); // ensure all pieces finished successfully
66
2/2
✓ Branch 1 taken 33 times.
✓ Branch 2 taken 33 times.
66 if (Parallel::rank(_comm) == 0)
67 33 _write_pvtu_file(filename_with_ext);
68 66 Parallel::barrier(_comm); // ensure .pvtu file is written before returning
69 66 }
70
71 70 void _write_piece(const std::string& par_filename) const {
72
1/2
✓ Branch 3 taken 70 times.
✗ Branch 4 not taken.
70 VTPWriter writer{this->grid(), this->_xml_opts};
73
1/2
✓ Branch 1 taken 70 times.
✗ Branch 2 not taken.
70 this->copy_fields(writer);
74
4/6
✓ Branch 1 taken 70 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 70 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 66 times.
✓ Branch 8 taken 4 times.
74 writer.write(PVTK::piece_basefilename(par_filename, Parallel::rank(_comm)));
75 70 }
76
77 33 void _write_pvtu_file(const std::string& filename_with_ext) const {
78
1/2
✓ Branch 1 taken 33 times.
✗ Branch 2 not taken.
33 std::ofstream file_stream(filename_with_ext, std::ios::out);
79
80
1/2
✓ Branch 1 taken 33 times.
✗ Branch 2 not taken.
66 XMLElement pvtk_xml("VTKFile");
81
2/4
✓ Branch 1 taken 33 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 33 times.
✗ Branch 5 not taken.
66 pvtk_xml.set_attribute("type", "PPolyData");
82
83
2/4
✓ Branch 1 taken 33 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 33 times.
✗ Branch 5 not taken.
66 XMLElement& grid = pvtk_xml.add_child("PPolyData");
84
2/4
✓ Branch 1 taken 33 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 33 times.
✗ Branch 5 not taken.
66 XMLElement& ppoint_data = grid.add_child("PPointData");
85
2/4
✓ Branch 1 taken 33 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 33 times.
✗ Branch 5 not taken.
33 XMLElement& pcell_data = grid.add_child("PCellData");
86 165 std::visit([&] (const auto& encoder) {
87 165 std::visit([&] (const auto& data_format) {
88 33 PVTK::PDataArrayHelper pdata_helper{encoder, data_format, ppoint_data};
89 33 PVTK::PDataArrayHelper cdata_helper{encoder, data_format, pcell_data};
90
8/24
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 26 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 26 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.
705 std::ranges::for_each(this->_point_field_names(), [&] (const std::string& name) {
91 168 pdata_helper.add(name, this->_get_point_field(name));
92 });
93
8/24
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 26 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 26 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.
701 std::ranges::for_each(this->_cell_field_names(), [&] (const std::string& name) {
94 167 cdata_helper.add(name, this->_get_cell_field(name));
95 });
96
3/6
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 27 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 2 times.
✗ Branch 8 not taken.
33 }, this->_xml_settings.data_format);
97
1/2
✓ Branch 1 taken 33 times.
✗ Branch 2 not taken.
33 }, this->_xml_settings.encoder);
98
99
4/8
✓ Branch 1 taken 33 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 33 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 33 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 33 times.
✗ Branch 11 not taken.
99 XMLElement& point_array = grid.add_child("PPoints").add_child("PDataArray");
100
2/4
✓ Branch 1 taken 33 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 33 times.
✗ Branch 5 not taken.
66 point_array.set_attribute("NumberOfComponents", "3");
101 132 std::visit([&] <typename T> (const Precision<T>& prec) {
102
6/12
✓ Branch 2 taken 31 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 31 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 31 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.
99 point_array.set_attribute("type", VTK::attribute_name(prec));
103
1/2
✓ Branch 1 taken 33 times.
✗ Branch 2 not taken.
33 }, this->_xml_settings.coordinate_precision);
104
105
2/4
✓ Branch 1 taken 33 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 33 times.
✗ Branch 5 not taken.
33 std::ranges::for_each(Parallel::ranks(_comm), [&] (int rank) {
106
5/10
✓ Branch 1 taken 66 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 66 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 66 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 66 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 66 times.
✗ Branch 14 not taken.
396 grid.add_child("Piece").set_attribute("Source", std::filesystem::path{
107
2/4
✓ Branch 1 taken 66 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 66 times.
✗ Branch 5 not taken.
132 PVTK::piece_basefilename(filename_with_ext, rank) + ".vtp"
108
1/2
✓ Branch 1 taken 66 times.
✗ Branch 2 not taken.
198 }.filename());
109 });
110
111
2/4
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 33 times.
✗ Branch 6 not taken.
33 this->_set_default_active_fields(pvtk_xml.get_child("PPolyData"));
112
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, file_stream, Indentation{{.width = 2}});
113 33 }
114 };
115
116 template<typename G, Concepts::Communicator C>
117 PVTPWriter(G&&, const C&, VTK::XMLOptions = {}) -> PVTPWriter<std::remove_cvref_t<G>, C>;
118
119 } // namespace GridFormat
120
121 #endif // GRIDFORMAT_VTK_PVTP_WRITER_HPP_
122