8#ifndef GRIDFORMAT_VTK_PVTI_WRITER_HPP_
9#define GRIDFORMAT_VTK_PVTI_WRITER_HPP_
21#include <gridformat/common/ranges.hpp>
22#include <gridformat/common/exceptions.hpp>
23#include <gridformat/common/lvalue_reference.hpp>
25#include <gridformat/parallel/communication.hpp>
26#include <gridformat/parallel/helpers.hpp>
28#include <gridformat/grid/grid.hpp>
29#include <gridformat/xml/element.hpp>
39template<Concepts::ImageGrid Grid,
40 Concepts::Communicator Communicator>
43 using CT = CoordinateType<Grid>;
45 static constexpr std::size_t dim = dimension<Grid>;
46 static constexpr int root_rank = 0;
49 explicit PVTIWriter(LValueReferenceOf<const Grid> grid,
52 :
ParentType(grid.get(),
".pvti",
true, xml_opts)
56 const Communicator& communicator()
const {
64 return PVTIWriter{this->grid(), _comm, std::move(xml_opts)};
67 void _write(std::ostream&)
const override {
69 "PVTIWriter does not support direct export into stream. "
70 "Use overload with filename instead!"
74 virtual void _write(
const std::string& filename_with_ext)
const override {
75 const auto& local_origin = origin(this->grid());
76 const auto& local_extents = extents(this->grid());
79 const auto all_origins = Parallel::gather(_comm, local_origin, root_rank);
80 const auto all_extents = Parallel::gather(_comm, local_extents, root_rank);
81 const auto is_negative_axis = VTK::CommonDetail::structured_grid_axis_orientation(spacing(this->grid()));
82 const auto [exts_begin, exts_end, whole_extent, origin] = helper.compute_extents_and_origin(
89 const auto my_whole_extent = Parallel::broadcast(_comm, whole_extent, root_rank);
90 const auto my_whole_origin = Parallel::broadcast(_comm, origin, root_rank);
91 const auto my_extent_offset = Parallel::scatter(_comm, Ranges::flat(exts_begin), root_rank);
93 _write_piece(filename_with_ext, Ranges::to_array<dim>(my_extent_offset), {my_whole_origin, my_whole_extent});
94 Parallel::barrier(_comm);
95 if (Parallel::rank(_comm) == 0)
96 _write_pvti_file(filename_with_ext, my_whole_origin, my_whole_extent, exts_begin, exts_end);
97 Parallel::barrier(_comm);
100 void _write_piece(
const std::string& par_filename,
101 const std::array<std::size_t, dim>& offset,
103 auto writer =
VTIWriter{this->grid(), this->_xml_opts}
104 .as_piece_for(std::move(domain))
105 .with_offset(offset);
106 this->copy_fields(writer);
107 writer.write(PVTK::piece_basefilename(par_filename, Parallel::rank(_comm)));
110 void _write_pvti_file(
const std::string& filename_with_ext,
111 const std::array<CT, dim>& origin,
112 const std::array<std::size_t, dim>& extents,
113 const std::vector<std::array<std::size_t, dim>>& proc_extents_begin,
114 const std::vector<std::array<std::size_t, dim>>& proc_extents_end)
const {
115 std::ofstream file_stream(filename_with_ext, std::ios::out);
117 XMLElement pvtk_xml(
"VTKFile");
118 pvtk_xml.set_attribute(
"type",
"PImageData");
120 XMLElement& grid = pvtk_xml.add_child(
"PImageData");
121 grid.set_attribute(
"WholeExtent", VTK::CommonDetail::extents_string(extents));
122 grid.set_attribute(
"Origin", VTK::CommonDetail::number_string_3d(origin));
123 grid.set_attribute(
"Spacing", VTK::CommonDetail::number_string_3d(spacing(this->grid())));
124 grid.set_attribute(
"Direction", VTK::CommonDetail::direction_string(basis(this->grid())));
126 XMLElement& ppoint_data = grid.add_child(
"PPointData");
127 XMLElement& pcell_data = grid.add_child(
"PCellData");
128 std::visit([&] (
const auto& encoder) {
129 std::visit([&] (
const auto& data_format) {
132 std::ranges::for_each(this->_point_field_names(), [&] (
const std::string& name) {
133 pdata_helper.add(name, this->_get_point_field(name));
135 std::ranges::for_each(this->_cell_field_names(), [&] (
const std::string& name) {
136 cdata_helper.add(name, this->_get_cell_field(name));
138 }, this->_xml_settings.data_format);
139 }, this->_xml_settings.encoder);
141 std::ranges::for_each(Parallel::ranks(_comm), [&] (
int rank) {
142 std::array<std::size_t, dim> extents_begin;
143 std::array<std::size_t, dim> extents_end;
144 for (
unsigned dir = 0; dir < dim; ++dir) {
145 extents_begin[dir] = proc_extents_begin[rank][dir];
146 extents_end[dir] = proc_extents_end[rank][dir];
149 auto& piece = grid.add_child(
"Piece");
150 piece.set_attribute(
"Extent", VTK::CommonDetail::extents_string(extents_begin, extents_end));
151 piece.set_attribute(
"Source", std::filesystem::path{
152 PVTK::piece_basefilename(filename_with_ext, rank) +
".vti"
156 this->_set_default_active_fields(pvtk_xml.get_child(
"PImageData"));
157 write_xml_with_version_header(pvtk_xml, file_stream, Indentation{{.width = 2}});
161template<
typename G, Concepts::Communicator C>
166template<
typename... Args>
Helper function for writing parallel VTK files.
Definition: vti_writer.hpp:39