dune-vtk  0.2
pvdwriter.impl.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <iomanip>
4 #include <limits>
5 
8 
9 namespace Dune {
10 
11 template <class W>
12 void PvdWriter<W>
13  ::writeTimestep (double time, std::string const& fn, std::optional<std::string> dir, bool writeCollection) const
14 {
15  auto p = Vtk::Path(fn);
16  auto name = p.stem();
17  p.removeFilename();
18 
19  Vtk::Path fn_dir = p;
20  Vtk::Path data_dir = dir ? Vtk::Path(*dir) : fn_dir;
21  Vtk::Path rel_dir = Vtk::relative(data_dir, fn_dir);
22 
23  std::string pvd_fn = fn_dir.string() + '/' + name.string();
24  std::string seq_fn = data_dir.string() + '/' + name.string() + "_t" + std::to_string(timesteps_.size());
25  std::string rel_fn = rel_dir.string() + '/' + name.string() + "_t" + std::to_string(timesteps_.size());
26 
27  std::string ext = "." + vtkWriter_.getFileExtension();
28 
29  int commRank = vtkWriter_.comm().rank();
30  int commSize = vtkWriter_.comm().size();
31  if (commSize > 1)
32  ext = ".p" + vtkWriter_.getFileExtension();
33 
34  timesteps_.emplace_back(time, rel_fn + ext);
35  vtkWriter_.write(seq_fn + ext);
36 
37  if (commRank == 0 && writeCollection) {
38  std::ofstream out(pvd_fn + ".pvd", std::ios_base::ate | std::ios::binary);
39  assert(out.is_open());
40 
41  out.imbue(std::locale::classic());
42  out << std::setprecision(datatype_ == Vtk::DataTypes::FLOAT32
43  ? std::numeric_limits<float>::max_digits10
44  : std::numeric_limits<double>::max_digits10);
45 
46  writeFile(out);
47  }
48 }
49 
50 
51 template <class W>
52 std::string PvdWriter<W>
53  ::write (std::string const& fn, std::optional<std::string> /*dir*/) const
54 {
55  auto p = Vtk::Path(fn);
56  auto name = p.stem();
57  p.removeFilename();
58  p /= name.string();
59 
60  std::string outputFilename;
61 
62  int commRank = vtkWriter_.comm().rank();
63  if (commRank == 0) {
64  outputFilename = p.string() + ".pvd";
65  std::ofstream out(outputFilename, std::ios_base::ate | std::ios::binary);
66  assert(out.is_open());
67 
68  out.imbue(std::locale::classic());
69  out << std::setprecision(datatype_ == Vtk::DataTypes::FLOAT32
70  ? std::numeric_limits<float>::max_digits10
71  : std::numeric_limits<double>::max_digits10);
72 
73  writeFile(out);
74  }
75 
76  return outputFilename;
77 }
78 
79 
80 template <class W>
81 void PvdWriter<W>
82  ::writeFile (std::ofstream& out) const
83 {
84  out << "<?xml version=\"1.0\"?>\n";
85  out << "<VTKFile"
86  << " type=\"Collection\""
87  << " version=\"0.1\""
88  << (format_ != Vtk::FormatTypes::ASCII ? " byte_order=\"" + vtkWriter_.getEndian() + "\"" : "")
89  << (format_ == Vtk::FormatTypes::COMPRESSED ? " compressor=\"vtkZLibDataCompressor\"" : "")
90  << ">\n";
91 
92  out << "<Collection>\n";
93 
94  // Write all timesteps
95  for (auto const& timestep : timesteps_) {
96  out << "<DataSet"
97  << " timestep=\"" << timestep.first << "\""
98  << " part=\"0\""
99  << " file=\"" << timestep.second << "\""
100  << " />\n";
101  }
102 
103  out << "</Collection>\n";
104  out << "</VTKFile>";
105 }
106 
107 } // end namespace Dune
Definition: writer.hh:13
std::string to_string(Vtk::FormatTypes type)
Definition: types.cc:12
Path relative(Path const &a, Path const &b)
Find the path of a relative to directory of b
Definition: filesystem.cc:173
void writeTimestep(double time, std::string const &fn, std::optional< std::string > dir={}, bool writeCollection=true) const
Write the attached data to the file.
Definition: pvdwriter.impl.hh:13
virtual std::string write(std::string const &fn, std::optional< std::string > dir={}) const override
Writes collection of timesteps to .pvd file.
Definition: pvdwriter.impl.hh:53
void writeFile(std::ofstream &out) const
Write a series of vtk files in a .pvd ParaView Data file.
Definition: pvdwriter.impl.hh:82
Definition: filesystem.hh:15
std::string string() const
Return the path as string.
Definition: filesystem.cc:27