GridFormat 0.6.0
I/O-Library for grid-like data structures
Loading...
Searching...
No Matches
pvd_writer.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2022-2023 Dennis Gläser <dennis.glaeser@iws.uni-stuttgart.de>
2// SPDX-License-Identifier: MIT
8#ifndef GRIDFORMAT_VTK_PVD_WRITER_HPP_
9#define GRIDFORMAT_VTK_PVD_WRITER_HPP_
10
11#include <iomanip>
12#include <sstream>
13#include <utility>
14#include <string>
15#include <ranges>
16#include <type_traits>
17#include <filesystem>
18
19#include <gridformat/common/output_file.hpp>
20#include <gridformat/parallel/communication.hpp>
21#include <gridformat/parallel/helpers.hpp>
22#include <gridformat/xml/element.hpp>
24#include <gridformat/grid.hpp>
25
26namespace GridFormat {
27
28#ifndef DOXYGEN
29namespace PVDDetail {
30
31 template<typename W>
32 class WriterStorage {
33 public:
34 explicit WriterStorage(W&& w) : _w(std::move(w)) {}
35
36 protected:
37 W& _writer() { return _w; }
38 const W& _writer() const { return _w; }
39
40 private:
41 W _w;
42 };
43
44} // namespace PVDDetail
45#endif // DOXYGEN
46
51template<typename VTKWriter>
52class PVDWriter : public PVDDetail::WriterStorage<VTKWriter>,
53 public TimeSeriesGridWriter<typename VTKWriter::Grid> {
54 using Storage = PVDDetail::WriterStorage<VTKWriter>;
55 using ParentType = TimeSeriesGridWriter<typename VTKWriter::Grid>;
56
57 public:
58 explicit PVDWriter(VTKWriter&& writer, std::string base_filename)
59 : Storage(std::move(writer))
60 , ParentType(Storage::_writer().grid(), Storage::_writer().writer_options())
61 , _base_filename{std::move(base_filename)}
62 , _pvd_filename{_base_filename + ".pvd"}
63 , _xml{"VTKFile"} {
64 _xml.set_attribute("type", "Collection");
65 _xml.set_attribute("version", "1.0");
66 _xml.add_child("Collection");
67 this->_writer().clear();
68 }
69
70 private:
71 std::string _write(double _time) override {
72 const auto time_step_index = _xml.get_child("Collection").number_of_children();
73 const auto vtk_filename = _write_time_step_file(time_step_index);
74 _add_dataset(_time, vtk_filename);
75
76 const auto& communicator = Traits::CommunicatorAccess<VTKWriter>::get(this->_writer());
77 // the collective invocation also makes sure all processes exit here after the pvd file is written
78 Parallel::invoke_collectively(communicator, [&] () {
79 if (Parallel::rank(communicator) == 0) {
80 write_to(_pvd_filename, [&] (std::ostream& s) {
81 write_xml_with_version_header(_xml, s, Indentation{{.width = 2}});
82 });
83 }
84 }, "Writing '" + _pvd_filename.string() + "'");
85
86 return _pvd_filename;
87 }
88
89 std::string _write_time_step_file(const std::integral auto index) {
90 this->copy_fields(this->_writer());
91 const auto filename = this->_writer().write(
92 _base_filename + "-" + _get_file_number_string(index)
93 );
94 this->_writer().clear();
95 return filename;
96 }
97
98 std::string _get_file_number_string(const std::integral auto index) const {
99 std::ostringstream file_number;
100 file_number << std::setw(5) << std::setfill('0') << index;
101 return file_number.str();
102 }
103
104 XMLElement& _add_dataset(const Concepts::Scalar auto _time, const std::string& filename) {
105 auto& dataset = _xml.get_child("Collection").add_child("DataSet");
106 dataset.set_attribute("timestep", _time);
107 dataset.set_attribute("group", "");
108 dataset.set_attribute("part", "0");
109 dataset.set_attribute("name", "");
110 dataset.set_attribute("file", std::filesystem::path{filename}.filename());
111 return dataset;
112 }
113
114 std::string _base_filename;
115 std::filesystem::path _pvd_filename;
116 XMLElement _xml;
117};
118
119template<typename VTKWriter>
121
122namespace Traits {
123
124template<typename VTKWriter>
125struct WritesConnectivity<PVDWriter<VTKWriter>> : public WritesConnectivity<VTKWriter> {};
126
127} // namespace Traits
128} // namespace GridFormat
129
130#endif // GRIDFORMAT_VTK_PVD_WRITER_HPP_
Writer for .pvd time-series file format.
Definition pvd_writer.hpp:53
Base classes for grid data writers.
Can be specialized by writers in case the file format does not contain connectivity information.
Definition writer.hpp:37