pvdwriter.impl.hh 2.52 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
#pragma once

#include <iomanip>

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

namespace Dune {

template <class W>
void PvdWriter<W>
12
  ::writeTimestep (double time, std::string const& fn, bool writeCollection) const
13
14
15
16
17
18
19
{
  auto p = filesystem::path(fn);
  auto name = p.stem();
  p.remove_filename();
  p /= name.string();

  std::string ext = "." + vtkWriter_.getFileExtension();
20
  std::string filename = p.string() + "_t" + std::to_string(timesteps_.size());
21
22
23
24
25
26
27
28
29
30

  int rank = 0;
  int num_ranks = 1;
#ifdef HAVE_MPI
  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  MPI_Comm_size(MPI_COMM_WORLD, &num_ranks);
  if (num_ranks > 1)
    ext = ".p" + vtkWriter_.getFileExtension();
#endif

31
32
  timesteps_.emplace_back(time, filename + ext);
  vtkWriter_.write(filename + ext);
33

34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
  if (rank == 0 && writeCollection) {
    std::ofstream out(p.string() + ".pvd", std::ios_base::ate | std::ios::binary);
    assert(out.is_open());

    out.imbue(std::locale::classic());
    out << std::setprecision(datatype_ == Vtk::FLOAT32
      ? std::numeric_limits<float>::digits10+2
      : std::numeric_limits<double>::digits10+2);

    writeFile(out);
  }
}


template <class W>
void PvdWriter<W>
  ::write (std::string const& fn) const
{
  auto p = filesystem::path(fn);
  auto name = p.stem();
  p.remove_filename();
  p /= name.string();

  int rank = 0;
  int num_ranks = 1;
#ifdef HAVE_MPI
  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  MPI_Comm_size(MPI_COMM_WORLD, &num_ranks);
#endif

64
65
66
67
68
69
70
71
72
  if (rank == 0) {
    std::ofstream out(p.string() + ".pvd", std::ios_base::ate | std::ios::binary);
    assert(out.is_open());

    out.imbue(std::locale::classic());
    out << std::setprecision(datatype_ == Vtk::FLOAT32
      ? std::numeric_limits<float>::digits10+2
      : std::numeric_limits<double>::digits10+2);

73
    writeFile(out);
74
  }
75
76
77
78
79
}


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

  out << "<Collection>\n";

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

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

} // end namespace Dune