vtkunstructuredgridwriter.hh 4.32 KB
Newer Older
Praetorius, Simon's avatar
Praetorius, Simon committed
1
2
3
4
5
6
#pragma once

#include <array>
#include <iosfwd>
#include <map>

7
#include <dune/vtk/filewriter.hh>
Stenger, Florian's avatar
Stenger, Florian committed
8
9
#include <dune/vtk/function.hh>
#include <dune/vtk/types.hh>
10
11
#include <dune/vtk/datacollectors/continuousdatacollector.hh>

12
#include <dune/vtk/vtkwriterinterface.hh>
Praetorius, Simon's avatar
Praetorius, Simon committed
13

14
namespace Dune
Praetorius, Simon's avatar
Praetorius, Simon committed
15
16
{
  /// File-Writer for VTK .vtu files
17
18
19
20
  /**
   * Requirement:
   * - DataCollector must be a model of \ref DataCollector
   **/
21
  template <class GridView, class DataCollector = Vtk::ContinuousDataCollector<GridView>>
Praetorius, Simon's avatar
Praetorius, Simon committed
22
  class VtkUnstructuredGridWriter
23
      : public VtkWriterInterface<GridView, DataCollector>
Praetorius, Simon's avatar
Praetorius, Simon committed
24
  {
25
26
    template <class> friend class VtkTimeseriesWriter;

Praetorius, Simon's avatar
Praetorius, Simon committed
27
28
    static constexpr int dimension = GridView::dimension;

29
    using Super = VtkWriterInterface<GridView, DataCollector>;
Praetorius, Simon's avatar
Praetorius, Simon committed
30
31
32
    using pos_type = typename Super::pos_type;

  public:
33
34
    /// forwarding constructor to \ref VtkWriterInterface
    using Super::Super;
Praetorius, Simon's avatar
Praetorius, Simon committed
35
36
37

  private:
    /// Write a serial VTK file in Unstructured format
38
    virtual void writeSerialFile (std::ofstream& out) const override;
Praetorius, Simon's avatar
Praetorius, Simon committed
39
40
41
42

    /// Write a parallel VTK file `pfilename.pvtu` in Unstructured format,
    /// with `size` the number of pieces and serial files given by `pfilename_p[i].vtu`
    /// for [i] in [0,...,size).
43
    virtual void writeParallelFile (std::ofstream& out, std::string const& pfilename, int size) const override;
Praetorius, Simon's avatar
Praetorius, Simon committed
44

45
    /// Write a series of timesteps in one file
46
47
48
49
50
51
52
53
    /**
     * \param filename      The name of the output file
     * \param filenameMesh  The name of a file where the mesh is stored. Must exist.
     * \param timesteps     A vector of pairs (timestep, filename) where the filename indicates
     *                      a file where the data of the timestep is stored.
     * \param blocks        A list of block sizes of the binary data stored in the files.
     *                      Order: (points, cells, pointdata[0], celldata[0], pointdata[1], celldata[1],...)
     **/
54
    void writeTimeseriesSerialFile (std::ofstream& out,
55
56
57
58
59
                                    std::string const& filenameMesh,
                                    std::vector<std::pair<double, std::string>> const& timesteps,
                                    std::vector<std::uint64_t> const& blocks) const;

    /// Write parallel VTK file for series of timesteps
60
61
    void writeTimeseriesParallelFile (std::ofstream& out,
                                      std::string const& pfilename, int size,
62
                                      std::vector<std::pair<double, std::string>> const& timesteps) const;
63

Praetorius, Simon's avatar
Praetorius, Simon committed
64
65
66
67
68
    virtual std::string fileExtension () const override
    {
      return "vtu";
    }

69
70
    virtual void writeGridAppended (std::ofstream& out, std::vector<std::uint64_t>& blocks) const override;

71
72
73
    // Write the element connectivity to the output stream `out`. In case
    // of binary format, stores the streampos of XML attributes "offset" in the
    // vector `offsets`.
74
    void writeCells (std::ofstream& out,
75
                     std::vector<pos_type>& offsets,
76
                     std::optional<std::size_t> timestep = {}) const;
77

78
79
    void writePointIds (std::ofstream& out,
                        std::vector<pos_type>& offsets,
80
                        std::optional<std::size_t> timestep = {}) const;
81

Praetorius, Simon's avatar
Praetorius, Simon committed
82
83
84
85
  private:
    using Super::dataCollector_;
    using Super::format_;
    using Super::datatype_;
86
    using Super::headertype_;
Praetorius, Simon's avatar
Praetorius, Simon committed
87
88
89
90
91
92

    // attached data
    using Super::pointData_;
    using Super::cellData_;
  };

93
94
95
96
  // deduction guides
  template <class GridView,
    class = std::void_t<typename GridView::IndexSet>>
  VtkUnstructuredGridWriter(GridView const&, Vtk::FormatTypes = Vtk::BINARY, Vtk::DataTypes = Vtk::FLOAT32)
Stenger, Florian's avatar
Stenger, Florian committed
97
    -> VtkUnstructuredGridWriter<GridView, Vtk::ContinuousDataCollector<GridView>>;
98
99
100
101
102
103
104
105

  template <class DataCollector,
    class = std::void_t<typename DataCollector::GridView>>
  VtkUnstructuredGridWriter(DataCollector&, Vtk::FormatTypes = Vtk::BINARY, Vtk::DataTypes = Vtk::FLOAT32)
    -> VtkUnstructuredGridWriter<typename DataCollector::GridView, DataCollector>;

  template <class DataCollector,
    class = std::void_t<typename DataCollector::GridView>>
106
107
108
  VtkUnstructuredGridWriter(std::shared_ptr<DataCollector>, Vtk::FormatTypes = Vtk::BINARY, Vtk::DataTypes = Vtk::FLOAT32)
    -> VtkUnstructuredGridWriter<typename DataCollector::GridView, DataCollector>;

109
} // end namespace Dune
Praetorius, Simon's avatar
Praetorius, Simon committed
110

111
#include "vtkunstructuredgridwriter.impl.hh"