8#ifndef GRIDFORMAT_VTK_PVD_WRITER_HPP_
9#define GRIDFORMAT_VTK_PVD_WRITER_HPP_
20#include <gridformat/parallel/communication.hpp>
21#include <gridformat/xml/element.hpp>
23#include <gridformat/grid.hpp>
33 explicit WriterStorage(W&& w) : _w(std::move(w)) {}
36 W& _writer() {
return _w; }
37 const W& _writer()
const {
return _w; }
50template<
typename VTKWriter>
51class PVDWriter :
public PVDDetail::WriterStorage<VTKWriter>,
53 using Storage = PVDDetail::WriterStorage<VTKWriter>;
57 explicit PVDWriter(VTKWriter&& writer, std::string base_filename)
58 : Storage(std::move(writer))
59 ,
ParentType(Storage::_writer().grid(), Storage::_writer().writer_options())
60 , _base_filename{std::move(base_filename)}
61 , _pvd_filename{_base_filename +
".pvd"}
63 _xml.set_attribute(
"type",
"Collection");
64 _xml.set_attribute(
"version",
"1.0");
65 _xml.add_child(
"Collection");
66 this->_writer().clear();
70 std::string _write(
double _time)
override {
71 const auto time_step_index = _xml.get_child(
"Collection").number_of_children();
72 const auto vtk_filename = _write_time_step_file(time_step_index);
73 _add_dataset(_time, vtk_filename);
76 if (Parallel::rank(communicator) == 0) {
77 std::ofstream pvd_file(_pvd_filename, std::ios::out);
78 write_xml_with_version_header(_xml, pvd_file, Indentation{{.width = 2}});
80 Parallel::barrier(communicator);
85 std::string _write_time_step_file(
const std::integral
auto index) {
86 this->copy_fields(this->_writer());
87 const auto filename = this->_writer().write(
88 _base_filename +
"-" + _get_file_number_string(index)
90 this->_writer().clear();
94 std::string _get_file_number_string(
const std::integral
auto index)
const {
95 std::ostringstream file_number;
96 file_number << std::setw(5) << std::setfill(
'0') << index;
97 return file_number.str();
100 XMLElement& _add_dataset(
const Concepts::Scalar
auto _time,
const std::string& filename) {
101 auto& dataset = _xml.get_child(
"Collection").add_child(
"DataSet");
102 dataset.set_attribute(
"timestep", _time);
103 dataset.set_attribute(
"group",
"");
104 dataset.set_attribute(
"part",
"0");
105 dataset.set_attribute(
"name",
"");
106 dataset.set_attribute(
"file", std::filesystem::path{filename}.filename());
110 std::string _base_filename;
111 std::filesystem::path _pvd_filename;
115template<
typename VTKWriter>
120template<
typename VTKWriter>
Base classes for grid data writers.