dune-vtk 2.12
Loading...
Searching...
No Matches
pvdwriter.impl.hh
Go to the documentation of this file.
1#pragma once
2
3#include <fstream>
4#include <iomanip>
5#include <limits>
6
9
10namespace Dune::Vtk {
11
12template <class W>
13void PvdWriter<W>
14 ::writeTimestep (double time, std::string const& fn, std::optional<std::string> dir, bool writeCollection) const
15{
16 auto p = Vtk::Path(fn);
17 auto name = p.stem();
18 p.removeFilename();
19
20 Vtk::Path fn_dir = p;
21 if (!Vtk::exists(fn_dir))
23
24 Vtk::Path data_dir = dir ? Vtk::Path(*dir) : fn_dir;
25 if (data_dir.isRelative())
26 data_dir = fn_dir / data_dir;
27 if (!Vtk::exists(data_dir))
28 Vtk::createDirectories(data_dir);
29
30 Vtk::Path rel_dir = Vtk::relative(data_dir, fn_dir);
31
32 std::string pvd_fn = fn_dir.string() + '/' + name.string();
33 std::string seq_fn = data_dir.string() + '/' + name.string() + "_t" + std::to_string(timesteps_.size());
34 std::string rel_fn = rel_dir.string() + '/' + name.string() + "_t" + std::to_string(timesteps_.size());
35
36 std::string ext = "." + vtkWriter_->getFileExtension();
37
38 int commRank = vtkWriter_->comm().rank();
39 int commSize = vtkWriter_->comm().size();
40 if (commSize > 1)
41 ext = ".p" + vtkWriter_->getFileExtension();
42
43 timesteps_.emplace_back(time, rel_fn + ext);
44 vtkWriter_->write(seq_fn + ext);
45
46 if (commRank == 0 && writeCollection) {
47 std::ofstream out(pvd_fn + ".pvd", std::ios_base::ate | std::ios::binary);
48 assert(out.is_open());
49
54
55 writeFile(out);
56 }
57}
59
60template <class W>
63{
64 auto p = Vtk::Path(fn);
65 auto name = p.stem();
66 p.removeFilename();
67 p /= name.string();
69 std::string outputFilename;
70
71 int commRank = vtkWriter_->comm().rank();
72 if (commRank == 0) {
73 outputFilename = p.string() + ".pvd";
74 std::ofstream out(outputFilename, std::ios_base::ate | std::ios::binary);
75 assert(out.is_open());
76
81
82 writeFile(out);
83 }
84
85 return outputFilename;
86}
87
89template <class W>
90void PvdWriter<W>
92{
93 out << "<?xml version=\"1.0\"?>\n";
94 out << "<VTKFile"
95 << " type=\"Collection\""
96 << " version=\"0.1\""
97 << (format_ != Vtk::FormatTypes::ASCII ? " byte_order=\"" + vtkWriter_->getEndian() + "\"" : "")
98 << (format_ == Vtk::FormatTypes::COMPRESSED ? " compressor=\"vtkZLibDataCompressor\"" : "")
99 << ">\n";
100
101 out << "<Collection>\n";
102
103 // Write all timesteps
104 for (auto const& timestep : timesteps_) {
105 out << "<DataSet"
106 << " timestep=\"" << timestep.first << "\""
107 << " part=\"0\""
108 << " file=\"" << timestep.second << "\""
109 << " />\n";
110 }
111
112 out << "</Collection>\n";
113 out << "</VTKFile>";
114}
115
116} // end namespace Dune::Vtk
Definition writer.hh:16
Path relative(Path const &a, Path const &b)
Find the path of a relative to directory of b
Definition filesystem.cc:172
bool createDirectories(Path const &p)
Create directory and non existing parent directories.
Definition filesystem.cc:139
bool exists(Path const &p)
Test whether the path is a valid (existing and accessible) file / directory.
Definition filesystem.cc:133
const char * name()
File-Writer for ParaView .pvd files.
Definition pvdwriter.hh:20
Definition filesystem.hh:13
std::string string() const
Return the path as string.
Definition filesystem.cc:26
bool isRelative() const
Definition filesystem.hh:84
T classic(T... args)
T imbue(T... args)
T is_open(T... args)
T setprecision(T... args)
T size(T... args)
T to_string(T... args)