FileWriter.hpp 4.37 KB
Newer Older
1
2
3
4
5
#pragma once

#include <string>
#include <memory>

6
#include <dune/functions/functionspacebases/lagrangebasis.hh>
7
#include <dune/grid/io/file/vtk/vtkwriter.hh>
8
#include <dune/grid/io/file/vtk/vtksequencewriter.hh>
9
10
//#include <dune/geometry/referenceelements.hh>
#include <dune/typetree/childextraction.hh>
11

12
#include <amdis/FileWriterInterface.hpp>
13
#include <amdis/Initfile.hpp>
14
15
#include <amdis/common/Filesystem.hpp>
#include <amdis/common/StaticSize.hpp>
16
#include <amdis/common/ValueCategory.hpp>
17
#include <amdis/gridfunctions/DiscreteFunction.hpp>
18
19
20

namespace AMDiS
{
Praetorius, Simon's avatar
Praetorius, Simon committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
  namespace Impl
  {
    template <class Tag> struct VTKFieldTypeImpl;
    template <>
    struct VTKFieldTypeImpl<tag::scalar> {
      static const Dune::VTK::FieldInfo::Type value = Dune::VTK::FieldInfo::Type::scalar;
    };
    template <>
    struct VTKFieldTypeImpl<tag::vector> {
      static const Dune::VTK::FieldInfo::Type value = Dune::VTK::FieldInfo::Type::vector;
    };
    template <>
    struct VTKFieldTypeImpl<tag::matrix> {
      static const Dune::VTK::FieldInfo::Type value = Dune::VTK::FieldInfo::Type::tensor;
    };

  } // end namespace Impl

  template <class Range>
  constexpr Dune::VTK::FieldInfo::Type VTKFieldType = Impl::VTKFieldTypeImpl<ValueCategory_t<Range>>::value;

  template <class Range>
43
  constexpr std::size_t VTKFieldSize = Size_v<Range>;
Praetorius, Simon's avatar
Praetorius, Simon committed
44
45


46
  template <class GB, class VT, class TP>
47
  class FileWriter
48
      : public FileWriterInterface
49
  {
50
51
52
53
54
    using GlobalBasis = GB;
    using ValueType = VT;
    using TreePath = TP;

  private:
55
    using GridView = typename GlobalBasis::GridView;
56
    using Range = typename DiscreteFunction<GB,VT,TP>::Range;
57

58
    /// Dimension of the mesh
59
    static constexpr int dim = GridView::dimension;
60

61
    /// Dimension of the world
62
    static constexpr int dow = GridView::dimensionworld;
63

64
  public:
65
    /// Constructor.
66
67
    FileWriter(std::string const& name, DiscreteFunction<GB,VT,TP> const& discreteFct)
      : FileWriterInterface(name)
68
      , discreteFct_(discreteFct)
69
      , animation_(false)
70
    {
71
      Parameters::get(name + "->ParaView animation", animation_);
72

73
      int m = Parameters::get<int>(name + "->ParaView mode").value_or(0);
74
75
      mode_ = (m == 0 ? Dune::VTK::ascii : Dune::VTK::appendedraw);

76
      init(name, ValueCategory_t<Range>{});
77
78
79
80
    }

    template <class ValueCat>
    void init(std::string const& baseName, ValueCat)
81
    {
82
83
84
      auto subSampling = Parameters::get<int>(baseName + "->subsampling");
      if (subSampling)
        vtkWriter_ = std::make_shared<Dune::SubsamplingVTKWriter<GridView>>(gridView(), subSampling.value());
Praetorius, Simon's avatar
Praetorius, Simon committed
85
86
      else
        vtkWriter_ = std::make_shared<Dune::VTKWriter<GridView>>(gridView());
87

88
89
90
      if (animation_)
        vtkSeqWriter_ = std::make_shared<Dune::VTKSequenceWriter<GridView>>(vtkWriter_, filename_, dir_, "");

91
      vtkWriter_->addVertexData(discreteFct_, Dune::VTK::FieldInfo(name_, VTKFieldType<Range>, VTKFieldSize<Range>));
92
93
    }

94
    void init(std::string const&, tag::unknown) {}
95

96
    /// Implements \ref FileWriterInterface::writeFiles
97
    void writeFiles(AdaptInfo& adaptInfo, bool force) override
98
    {
99
      test_exit(filesystem::exists(dir_), "Output directory '{}' does not exist!",dir_);
100

101
      if (vtkSeqWriter_)
Praetorius, Simon's avatar
Praetorius, Simon committed
102
        vtkSeqWriter_->write(adaptInfo.time(), mode_);
103
104
      else if (vtkWriter_)
        vtkWriter_->write(filesystem::path({dir_, filename_}).string(), mode_);
105
106
107
    }

  protected:
Praetorius, Simon's avatar
Praetorius, Simon committed
108
    GridView const& gridView() const
109
    {
110
      return discreteFct_.basis().gridView();
111
112
    }

113
  private:
114
    DiscreteFunction<GB,VT,TP> discreteFct_;
115
116
117

    std::shared_ptr<Dune::VTKWriter<GridView>> vtkWriter_;
    std::shared_ptr<Dune::VTKSequenceWriter<GridView>> vtkSeqWriter_;
118

119
120
121
    // write .pvd if animation=true, otherwise write only .vtu
    bool animation_;

Praetorius, Simon's avatar
Praetorius, Simon committed
122
123
    // represents VTK::OutputType: ascii, appendedraw
    Dune::VTK::OutputType mode_;
124
  };
125

126
127
128
129
130
131
  template <class GlobalBasis, class ValueType, class TreePath>
  auto makeFileWriter(std::string const& name,
      DiscreteFunction<GlobalBasis,ValueType,TreePath> const& discreteFct)
  {
    return FileWriter<GlobalBasis,ValueType,TreePath>{name, discreteFct};
  }
132

133
  template <class GlobalBasis, class ValueType, class TreePath>
134
  auto makeFileWriterPtr(std::string const& name,
135
      DiscreteFunction<GlobalBasis,ValueType,TreePath> const& discreteFct)
136
  {
137
    return std::make_unique<FileWriter<GlobalBasis,ValueType,TreePath>>(name, discreteFct);
138
139
  }

140
} // end namespace AMDiS