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

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

#include <dune/vtk/filewriter.hh>
#include <dune/vtk/vtktypes.hh>
9
10
#include <dune/vtk/utility/filesystem.hh>
#include <dune/vtk/utility/uid.hh>
11
12
13

namespace Dune
{
14
15
16
17
18
19
  /// 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.
   **/
20
21
  template <class VtkWriter>
  class VtkTimeseriesWriter
22
      : public FileWriter
23
24
25
26
27
  {
  protected:
    using Self = VtkTimeseriesWriter;
    using pos_type = typename std::ostream::pos_type;

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

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

36
37
38
39
40
  public:
    /// Constructor, stores the gridView
    template <class... Args, disableCopyMove<Self, Args...> = 0>
    VtkTimeseriesWriter (Args&&... args)
      : vtkWriter_{std::forward<Args>(args)...}
41
42
    {
      assert(vtkWriter_.format_ != Vtk::ASCII && "Timeseries writer requires APPENDED mode");
43
44
45
46
47
48
49
      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_);
    }

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

53
54
55
56
57
58
59
60
61
62
63
    /// 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.
     * \param writeCollection  Create a timeseries file directly
     **/
    void writeTimestep (double time, std::string const& fn, bool writeCollection = true) const;
64

65
66
    /// Writes all timesteps to single timeseries file.
    // NOTE: requires an aforging call to \ref writeTimestep
67
68
69
    /**
     * Create a timeseries file with all timesteps written by \ref writeTimestep.
     **/
70
    virtual void write (std::string const& fn) const override;
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89

    /// 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_;
90
    filesystem::path tmpDir_;
91

92
    mutable bool initialized_ = false;
93
94

    // block size of attached data
95
    mutable std::vector<std::uint64_t> blocks_;
96

97
98
    mutable std::string filenameMesh_;
    mutable std::vector<std::pair<double, std::string>> timesteps_;
99
100
101
102
103
  };

} // end namespace Dune

#include "vtktimeserieswriter.impl.hh"