38 explicit PVTUWriter(LValueReferenceOf<const Grid> grid,
41 : ParentType(grid.get(),
".pvtu",
false, xml_opts)
45 const Communicator& communicator()
const {
53 return PVTUWriter{this->grid(), _comm, std::move(xml_opts)};
56 void _write(std::ostream&)
const override {
58 "PVTUWriter does not support direct export into stream. "
59 "Use overload with filename instead!"
63 virtual void _write(
const std::string& filename_with_ext)
const override {
66 Parallel::invoke_collectively(_comm, [&] () {
67 _write_piece(filename_with_ext);
68 },
"Writing the pieces of '" + filename_with_ext +
"'");
69 Parallel::invoke_collectively(_comm, [&] () {
70 if (Parallel::rank(_comm) == 0)
71 _write_pvtu_file(filename_with_ext);
72 },
"Writing '" + filename_with_ext +
"'");
75 void _write_piece(
const std::string& par_filename)
const {
76 VTUWriter writer{this->grid(), this->_xml_opts};
77 this->copy_fields(writer);
81 void _write_pvtu_file(
const std::string& filename_with_ext)
const {
82 XMLElement pvtk_xml(
"VTKFile");
83 pvtk_xml.set_attribute(
"type",
"PUnstructuredGrid");
85 XMLElement& grid = pvtk_xml.add_child(
"PUnstructuredGrid");
86 XMLElement& ppoint_data = grid.add_child(
"PPointData");
87 XMLElement& pcell_data = grid.add_child(
"PCellData");
88 std::visit([&] (
const auto& encoder) {
89 std::visit([&] (
const auto& data_format) {
92 std::ranges::for_each(this->_point_field_names(), [&] (
const std::string& name) {
93 pdata_helper.add(name, this->_get_point_field(name));
95 std::ranges::for_each(this->_cell_field_names(), [&] (
const std::string& name) {
96 cdata_helper.add(name, this->_get_cell_field(name));
98 }, this->_xml_settings.data_format);
99 }, this->_xml_settings.encoder);
101 XMLElement& point_array = grid.add_child(
"PPoints").add_child(
"PDataArray");
102 point_array.set_attribute(
"NumberOfComponents",
"3");
103 std::visit([&] <
typename T> (
const Precision<T>& prec) {
104 point_array.set_attribute(
"type", VTK::attribute_name(prec));
105 }, this->_xml_settings.coordinate_precision);
107 std::ranges::for_each(Parallel::ranks(_comm), [&] (
int rank) {
108 grid.add_child(
"Piece").set_attribute(
"Source", std::filesystem::path{
113 this->_set_default_active_fields(pvtk_xml.get_child(
"PUnstructuredGrid"));
114 write_to(filename_with_ext, [&] (std::ostream& s) {
115 write_xml_with_version_header(pvtk_xml, s, Indentation{{.width = 2}});