vtkunstructuredgridwriter.hh 4.15 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
#include <dune/vtk/datacollectors/continuousdatacollector.hh>
11
#include <dune/vtk/utility/concepts.hh>
12

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

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

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

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

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

    /// 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).
42
    virtual void writeParallelFile (std::ofstream& out, std::string const& pfilename, int size) const override;
Praetorius, Simon's avatar
Praetorius, Simon committed
43

44
    /// Write a series of timesteps in one file
45
46
47
48
49
50
51
52
    /**
     * \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],...)
     **/
53
    void writeTimeseriesSerialFile (std::ofstream& out,
54
55
56
57
58
                                    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
59
60
    void writeTimeseriesParallelFile (std::ofstream& out,
                                      std::string const& pfilename, int size,
61
                                      std::vector<std::pair<double, std::string>> const& timesteps) const;
62

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

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

70
71
72
    // 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`.
73
    void writeCells (std::ofstream& out,
74
                     std::vector<pos_type>& offsets,
75
                     std::optional<std::size_t> timestep = {}) const;
76

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

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

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

92
  // deduction guides
93
94
95
  template <class GridView, class... Args,
    Vtk::IsGridView<GridView> = true>
  VtkUnstructuredGridWriter(GridView, Args...)
Stenger, Florian's avatar
Stenger, Florian committed
96
    -> VtkUnstructuredGridWriter<GridView, Vtk::ContinuousDataCollector<GridView>>;
97

98
99
100
  template <class DataCollector, class... Args,
    Vtk::IsDataCollector<DataCollector> = true>
  VtkUnstructuredGridWriter(DataCollector&, Args...)
101
102
    -> VtkUnstructuredGridWriter<typename DataCollector::GridView, DataCollector>;

103
104
105
  template <class DataCollector, class... Args,
    Vtk::IsDataCollector<DataCollector> = true>
  VtkUnstructuredGridWriter(std::shared_ptr<DataCollector>, Args...)
106
107
    -> VtkUnstructuredGridWriter<typename DataCollector::GridView, DataCollector>;

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

110
#include "vtkunstructuredgridwriter.impl.hh"