vtktimeserieswriter.hh 3.98 KB
Newer Older
1
2
3
#pragma once

#include <string>
4
#include <tuple>
5
6
7
#include <vector>

#include <dune/vtk/filewriter.hh>
Praetorius, Simon's avatar
Praetorius, Simon committed
8
#include <dune/vtk/forward.hh>
9
#include <dune/vtk/vtktypes.hh>
10
11
#include <dune/vtk/utility/filesystem.hh>
#include <dune/vtk/utility/uid.hh>
12
13
14

namespace Dune
{
15
16
17
18
19
20
  /// File-Writer for Vtk timeseries .vtu files
  /**
   * \tparam VtkWriter  Type of a FileWriter derived from \ref VtkWriterInterface that
   *                    additionally supports writeTimeseriesSerialFile() and writeTimeseriesParallelFile(),
   *                    e.g. \ref VtkUnstructuredGridWriter.
   **/
21
22
  template <class VtkWriter>
  class VtkTimeseriesWriter
23
      : public FileWriter
24
25
26
27
28
  {
  protected:
    using Self = VtkTimeseriesWriter;
    using pos_type = typename std::ostream::pos_type;

29
30
    template <class W>
    using HasWriteTimeseriesSerialFile = decltype(&W::writeTimeseriesSerialFile);
31
    // static_assert(Std::is_detected<HasWriteTimeseriesSerialFile, VtkWriter>::value, "");
32
33
34

    template <class W>
    using HasWriteTimeseriesParallelFile = decltype(&W::writeTimeseriesParallelFile);
35
    // static_assert(Std::is_detected<HasWriteTimeseriesParallelFile, VtkWriter>::value, "");
36

37
38
39
40
41
  public:
    /// Constructor, stores the gridView
    template <class... Args, disableCopyMove<Self, Args...> = 0>
    VtkTimeseriesWriter (Args&&... args)
      : vtkWriter_{std::forward<Args>(args)...}
42
43
    {
      assert(vtkWriter_.format_ != Vtk::ASCII && "Timeseries writer requires APPENDED mode");
44
45
46
47
48
49
50
      std::srand(std::time(nullptr));
      // put temporary file to /tmp directory
      tmpDir_ = filesystem::path("/tmp/vtktimeserieswriter_" + uid(10) + "/");
      assert( filesystem::exists("/tmp") );
      filesystem::create_directories(tmpDir_);
    }

51
52
    /// Remove all written intermediate files and remove temporary directory
    ~VtkTimeseriesWriter ();
53

54
55
56
57
58
59
60
61
    /// Write the attached data to the file
    /**
     * Create intermediate files for the data associated to the current timestep `time`.
     *
     * \param time  The time value of the written data
     * \param fn  Filename of the file to write to. Only the base part
     *            (without dir and extentsion) is used to write the intermediate
     *            file into a tmp directory.
62
     * \param tmpDir  If the directory is given, it is used as tmp dir, otherwise /tmp.
63
64
     * \param writeCollection  Create a timeseries file directly
     **/
65
66
67
    void writeTimestep (double time, std::string const& fn,
                        Std::optional<std::string> tmpDir = {},
                        bool writeCollection = true) const;
68

69
    /// Writes all timesteps to single timeseries file.
70
    // NOTE: requires an aforgoing call to \ref writeTimestep
71
72
    /**
     * Create a timeseries file with all timesteps written by \ref writeTimestep.
73
74
75
     *
     * \param fn   Filename of the Timeseries file. May contain a directory and any file extension.
     * \param dir  The optional parameter specifies the directory of the partition files.
76
     **/
77
    virtual void write (std::string const& fn, Std::optional<std::string> dir = {}) const override;
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96

    /// Attach point data to the writer, \see VtkFunction for possible arguments
    template <class Function, class... Args>
    VtkTimeseriesWriter& addPointData (Function const& fct, Args&&... args)
    {
      vtkWriter_.addPointData(fct, std::forward<Args>(args)...);
      return *this;
    }

    /// Attach cell data to the writer, \see VtkFunction for possible arguments
    template <class Function, class... Args>
    VtkTimeseriesWriter& addCellData (Function const& fct, Args&&... args)
    {
      vtkWriter_.addCellData(fct, std::forward<Args>(args)...);
      return *this;
    }

  protected:
    VtkWriter vtkWriter_;
97
    filesystem::path tmpDir_;
98

99
    mutable bool initialized_ = false;
100
101

    // block size of attached data
102
    mutable std::vector<std::uint64_t> blocks_;
103

104
105
    mutable std::string filenameMesh_;
    mutable std::vector<std::pair<double, std::string>> timesteps_;
106
107
108
109
110
  };

} // end namespace Dune

#include "vtktimeserieswriter.impl.hh"