8#ifndef GRIDFORMAT_VTK_PVTS_WRITER_HPP_
9#define GRIDFORMAT_VTK_PVTS_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::StructuredGrid Grid,
40 Concepts::Communicator Communicator>
43 using CT = CoordinateType<Grid>;
45 static constexpr std::size_t space_dim = 3;
46 static constexpr std::size_t dim = dimension<Grid>;
47 static constexpr int root_rank = 0;
50 explicit PVTSWriter(LValueReferenceOf<const Grid> grid,
53 :
ParentType(grid.get(),
".pvts",
true, xml_opts)
57 const Communicator& communicator()
const {
65 return PVTSWriter{this->grid(), _comm, std::move(xml_opts)};
68 void _write(std::ostream&)
const override {
70 "PVTSWriter does not support direct export into stream. "
71 "Use overload with filename instead!"
75 virtual void _write(
const std::string& filename_with_ext)
const override {
76 const auto& local_extents = extents(this->grid());
77 const auto [origin, is_negative_axis] = _get_origin_and_orientations(local_extents);
80 const auto all_origins = Parallel::gather(_comm, origin, root_rank);
81 const auto all_extents = Parallel::gather(_comm, local_extents, root_rank);
82 const auto [exts_begin, exts_end, whole_extent, _] = helper.compute_extents_and_origin(
88 const auto my_whole_extent = Parallel::broadcast(_comm, whole_extent, root_rank);
89 const auto my_extent_offset = Parallel::scatter(_comm, Ranges::flat(exts_begin), root_rank);
91 _write_piece(filename_with_ext, Ranges::to_array<dim>(my_extent_offset), {my_whole_extent});
92 Parallel::barrier(_comm);
93 if (Parallel::rank(_comm) == 0)
94 _write_pvts_file(filename_with_ext, my_whole_extent, exts_begin, exts_end);
95 Parallel::barrier(_comm);
98 auto _get_origin_and_orientations(
const std::ranges::range
auto& extents)
const {
99 auto is_negative_axis = Ranges::filled_array<dim>(
false);
100 std::ranges::for_each(extents, [&, dir=0] (
const auto& e)
mutable {
102 is_negative_axis[dir] = _is_negative_axis(dir);
106 std::array<CT, dim> origin;
107 static constexpr auto origin_loc = _origin_location();
108 for (
const auto& p : points(this->grid())) {
109 const auto& loc = location(this->grid(), p);
110 const auto& pos = coordinates(this->grid(), p);
111 if (std::ranges::equal(loc, origin_loc)) {
112 std::ranges::copy(pos, origin.begin());
113 return std::make_tuple(std::move(origin), std::move(is_negative_axis));
117 throw InvalidState(
"Could not determine origin");
120 static constexpr auto _origin_location() {
121 std::array<std::size_t, dim> origin;
122 std::ranges::fill(origin, 0);
126 bool _is_negative_axis(
unsigned axis)
const {
127 for (
const auto& p0 : points(this->grid())) {
128 const auto i0 = Ranges::at(axis, location(this->grid(), p0));
129 const auto x0 = Ranges::at(axis, coordinates(this->grid(), p0));
130 for (
const auto& p1 : points(this->grid())) {
131 const auto i1 = Ranges::at(axis, location(this->grid(), p1));
132 const auto x1 = Ranges::at(axis, coordinates(this->grid(), p1));
137 throw InvalidState(
"Could not determine axis orientation");
140 void _write_piece(
const std::string& par_filename,
141 const std::array<std::size_t, dim>& offset,
143 auto writer =
VTSWriter{this->grid(), this->_xml_opts}
144 .as_piece_for(std::move(domain))
145 .with_offset(offset);
146 this->copy_fields(writer);
147 writer.write(PVTK::piece_basefilename(par_filename, Parallel::rank(_comm)));
150 void _write_pvts_file(
const std::string& filename_with_ext,
151 const std::array<std::size_t, dim>& extents,
152 const std::vector<std::array<std::size_t, dim>>& proc_extents_begin,
153 const std::vector<std::array<std::size_t, dim>>& proc_extents_end)
const {
154 std::ofstream file_stream(filename_with_ext, std::ios::out);
156 XMLElement pvtk_xml(
"VTKFile");
157 pvtk_xml.set_attribute(
"type",
"PStructuredGrid");
159 XMLElement& grid = pvtk_xml.add_child(
"PStructuredGrid");
160 grid.set_attribute(
"WholeExtent", VTK::CommonDetail::extents_string(extents));
162 XMLElement& ppoint_data = grid.add_child(
"PPointData");
163 XMLElement& pcell_data = grid.add_child(
"PCellData");
164 XMLElement& pcoords = grid.add_child(
"PPoints");
165 std::visit([&] (
const auto& encoder) {
166 std::visit([&] (
const auto& data_format) {
169 std::ranges::for_each(this->_point_field_names(), [&] (
const std::string& name) {
170 pdata_helper.add(name, this->_get_point_field(name));
172 std::ranges::for_each(this->_cell_field_names(), [&] (
const std::string& name) {
173 cdata_helper.add(name, this->_get_cell_field(name));
176 std::visit([&] <
typename T> (
const Precision<T>& prec) {
177 XMLElement& pdata_array = pcoords.add_child(
"PDataArray");
178 pdata_array.set_attribute(
"NumberOfComponents", std::to_string(space_dim));
179 pdata_array.set_attribute(
"Name",
"Coordinates");
180 pdata_array.set_attribute(
"format", VTK::data_format_name(encoder, data_format));
181 pdata_array.set_attribute(
"type", VTK::attribute_name(prec));
182 }, this->_xml_settings.coordinate_precision);
183 }, this->_xml_settings.data_format);
184 }, this->_xml_settings.encoder);
186 std::ranges::for_each(Parallel::ranks(_comm), [&] (
int rank) {
187 std::array<std::size_t, dim> extents_begin;
188 std::array<std::size_t, dim> extents_end;
189 for (
unsigned dir = 0; dir < dim; ++dir) {
190 extents_begin[dir] = proc_extents_begin[rank][dir];
191 extents_end[dir] = proc_extents_end[rank][dir];
193 auto& piece = grid.add_child(
"Piece");
194 piece.set_attribute(
"Extent", VTK::CommonDetail::extents_string(extents_begin, extents_end));
195 piece.set_attribute(
"Source", std::filesystem::path{
196 PVTK::piece_basefilename(filename_with_ext, rank) +
".vts"
200 this->_set_default_active_fields(pvtk_xml.get_child(
"PStructuredGrid"));
201 write_xml_with_version_header(pvtk_xml, file_stream, Indentation{{.width = 2}});
205template<
typename G, Concepts::Communicator C>
Helper function for writing parallel VTK files.
Definition: vts_writer.hpp:42