pvdwriter.impl.hh 2.88 KB
Newer Older
1
2
3
#pragma once

#include <iomanip>
4
#include <limits>
5
6
7
8
9
10
11
12

#include <dune/vtk/utility/filesystem.hh>
#include <dune/vtk/utility/string.hh>

namespace Dune {

template <class W>
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
20
21
  Vtk::Path fn_dir = p;
  Vtk::Path data_dir = dir ? Vtk::Path(*dir) : fn_dir;
  Vtk::Path rel_dir = Vtk::relative(data_dir, fn_dir);
22
23
24
25

  std::string pvd_fn = fn_dir.string() + '/' + name.string();
  std::string seq_fn = data_dir.string() + '/' + name.string() + "_t" + std::to_string(timesteps_.size());
  std::string rel_fn = rel_dir.string() + '/' + name.string() + "_t" + std::to_string(timesteps_.size());
26
27
28

  std::string ext = "." + vtkWriter_.getFileExtension();

29
30
31
  int commRank = vtkWriter_.comm().rank();
  int commSize = vtkWriter_.comm().size();
  if (commSize > 1)
32
33
    ext = ".p" + vtkWriter_.getFileExtension();

34
35
  timesteps_.emplace_back(time, rel_fn + ext);
  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
40
41
    assert(out.is_open());

    out.imbue(std::locale::classic());
42
    out << std::setprecision(datatype_ == Vtk::DataTypes::FLOAT32
43
44
      ? std::numeric_limits<float>::max_digits10
      : std::numeric_limits<double>::max_digits10);
45
46
47
48
49
50
51

    writeFile(out);
  }
}


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
59
  p /= name.string();

60
61
  std::string outputFilename;

62
63
  int commRank = vtkWriter_.comm().rank();
  if (commRank == 0) {
64
65
    outputFilename = p.string() + ".pvd";
    std::ofstream out(outputFilename, std::ios_base::ate | std::ios::binary);
66
67
68
    assert(out.is_open());

    out.imbue(std::locale::classic());
69
    out << std::setprecision(datatype_ == Vtk::DataTypes::FLOAT32
70
71
      ? std::numeric_limits<float>::max_digits10
      : std::numeric_limits<double>::max_digits10);
72

73
    writeFile(out);
74
  }
75
76

  return outputFilename;
77
78
79
80
81
}


template <class W>
void PvdWriter<W>
82
  ::writeFile (std::ofstream& out) const
83
84
85
86
87
{
  out << "<?xml version=\"1.0\"?>\n";
  out << "<VTKFile"
      << " type=\"Collection\""
      << " version=\"0.1\""
88
      << (format_ != Vtk::FormatTypes::ASCII ? " byte_order=\"" + vtkWriter_.getEndian() + "\"" : "")
Praetorius, Simon's avatar
Praetorius, Simon committed
89
      << (format_ == Vtk::FormatTypes::COMPRESSED ? " compressor=\"vtkZLibDataCompressor\"" : "")
90
91
92
93
94
      << ">\n";

  out << "<Collection>\n";

  // Write all timesteps
95
  for (auto const& timestep : timesteps_) {
96
97
98
99
100
101
102
103
104
105
106
107
    out << "<DataSet"
        << " timestep=\"" << timestep.first << "\""
        << " part=\"0\""
        << " file=\"" << timestep.second << "\""
        << " />\n";
  }

  out << "</Collection>\n";
  out << "</VTKFile>";
}

} // end namespace Dune