dune-vtk 2.12
Loading...
Searching...
No Matches
pvdwriter.impl.hh
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file AUTHORS.md
2// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception OR LGPL-3.0-or-later
3#pragma once
4
5#include <fstream>
6#include <iomanip>
7#include <limits>
8
11
12namespace Dune::Vtk {
13
14template <class W>
15void PvdWriter<W>
16 ::writeTimestep (double time, std::string const& fn, std::optional<std::string> dir, bool writeCollection) const
17{
18 auto p = Vtk::Path(fn);
19 auto name = p.stem();
20 p.removeFilename();
21
22 Vtk::Path fn_dir = p;
23 if (!Vtk::exists(fn_dir))
25
27 if (data_dir.isRelative())
31
33
34 std::string pvd_fn = fn_dir.string() + '/' + name.string();
35 std::string seq_fn = data_dir.string() + '/' + name.string() + "_t" + std::to_string(timesteps_.size());
36 std::string rel_fn = rel_dir.string() + '/' + name.string() + "_t" + std::to_string(timesteps_.size());
37
38 std::string ext = "." + vtkWriter_->getFileExtension();
39
40 int commRank = vtkWriter_->comm().rank();
41 int commSize = vtkWriter_->comm().size();
42 if (commSize > 1)
43 ext = ".p" + vtkWriter_->getFileExtension();
44
45 timesteps_.emplace_back(time, rel_fn + ext);
46 vtkWriter_->write(seq_fn + ext);
47
48 if (commRank == 0 && writeCollection) {
49 std::ofstream out(pvd_fn + ".pvd", std::ios_base::ate | std::ios::binary);
50 assert(out.is_open());
51
56
57 writeFile(out);
58 }
59}
61
62template <class W>
65{
66 auto p = Vtk::Path(fn);
67 auto name = p.stem();
68 p.removeFilename();
69 p /= name.string();
72
73 int commRank = vtkWriter_->comm().rank();
74 if (commRank == 0) {
75 outputFilename = p.string() + ".pvd";
76 std::ofstream out(outputFilename, std::ios_base::ate | std::ios::binary);
77 assert(out.is_open());
78
83
84 writeFile(out);
85 }
86
87 return outputFilename;
88}
89
91template <class W>
92void PvdWriter<W>
94{
95 out << "<?xml version=\"1.0\"?>\n";
96 out << "<VTKFile"
97 << " type=\"Collection\""
98 << " version=\"0.1\""
99 << (format_ != Vtk::FormatTypes::ASCII ? " byte_order=\"" + vtkWriter_->getEndian() + "\"" : "")
100 << (format_ == Vtk::FormatTypes::COMPRESSED ? " compressor=\"vtkZLibDataCompressor\"" : "")
101 << ">\n";
102
103 out << "<Collection>\n";
104
105 // Write all timesteps
106 for (auto const& timestep : timesteps_) {
107 out << "<DataSet"
108 << " timestep=\"" << timestep.first << "\""
109 << " part=\"0\""
110 << " file=\"" << timestep.second << "\""
111 << " />\n";
112 }
113
114 out << "</Collection>\n";
115 out << "</VTKFile>";
116}
117
118} // end namespace Dune::Vtk
Definition datacollector.hh:19
Path relative(Path const &a, Path const &b)
Find the path of a relative to directory of b
Definition filesystem.cc:174
bool createDirectories(Path const &p)
Create directory and non existing parent directories.
Definition filesystem.cc:141
bool exists(Path const &p)
Test whether the path is a valid (existing and accessible) file / directory.
Definition filesystem.cc:135
const char * name()
auto comm() const
Definition datacollectorinterface.hh:112
Implementation of DataCollector for cells, with continuous data.
Definition celltypedatacollector.hh:20
File-Writer for ParaView .pvd files.
Definition pvdwriter.hh:22
Definition filesystem.hh:15
T classic(T... args)
T imbue(T... args)
T is_open(T... args)
T setprecision(T... args)
T to_string(T... args)