FileWriter.hpp 4.17 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
13
14
15
16
17
#include <amdis/Initfile.hpp>
#include <amdis/common/Size.hpp>
#include <amdis/common/ValueCategory.hpp>
#include <amdis/gridfunctions/DOFVectorView.hpp>
#include <amdis/io/FileWriterInterface.hpp>
#include <amdis/utility/Filesystem.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
43
44
45
  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>
  constexpr std::size_t VTKFieldSize = Size<Range>;


46
  template <class GlobalBasis, class RangeType, class TreePath>
47
  class FileWriter
48
      : public FileWriterInterface
49
50
  {
  private: // typedefs and static constants
51
    using GridView = typename GlobalBasis::GridView;
52
    using Vector = DOFVectorConstView<GlobalBasis,RangeType,TreePath>;
Praetorius, Simon's avatar
Praetorius, Simon committed
53
    using Range = typename Vector::Range;
54

55
    /// Dimension of the mesh
56
    static constexpr int dim = GridView::dimension;
57

58
    /// Dimension of the world
59
    static constexpr int dow = GridView::dimensionworld;
60

61
  public:
62
    /// Constructor.
63
    FileWriter(std::string const& baseName,
Praetorius, Simon's avatar
Praetorius, Simon committed
64
               Vector const& dofvector)
65
      : FileWriterInterface(baseName)
Praetorius, Simon's avatar
Praetorius, Simon committed
66
      , dofvector_(dofvector)
67
      , animation_(false)
68
    {
69
70
71
72
73
      Parameters::get(baseName + "->ParaView animation", animation_);

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

74
75
76
77
78
      init(baseName, ValueCategory_t<Range>{});
    }

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

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

Praetorius, Simon's avatar
Praetorius, Simon committed
89
      vtkWriter_->addVertexData(dofvector_, Dune::VTK::FieldInfo(name_, VTKFieldType<Range>, VTKFieldSize<Range>));
90
91
    }

92
    void init(std::string const&, tag::unknown) {}
93

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

99
100
      if (vtkSeqWriter_)
        vtkSeqWriter_->write(adaptInfo.getTime(), mode_);
101
102
      else if (vtkWriter_)
        vtkWriter_->write(filesystem::path({dir_, filename_}).string(), mode_);
103
104
105
    }

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

111
  private:
Praetorius, Simon's avatar
Praetorius, Simon committed
112
    Vector dofvector_;
113
114
115

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

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

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

124

125
126
127
128
  template <class GlobalBasis, class Range, class TreePath>
  std::shared_ptr<FileWriter<GlobalBasis,Range,TreePath>>
  makeFileWriterPtr(std::string baseName,
      DOFVectorConstView<GlobalBasis,Range,TreePath> const& dofvector)
129
  {
130
    return std::make_shared<FileWriter<GlobalBasis,Range,TreePath>>(baseName, dofvector);
131
132
  }

133
} // end namespace AMDiS