FileWriter.hpp 3.58 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 <dune/amdis/Initfile.hpp>
Praetorius, Simon's avatar
Praetorius, Simon committed
13
14
15
#include <dune/amdis/common/Size.hpp>
#include <dune/amdis/common/ValueCategory.hpp>
#include <dune/amdis/gridfunctions/DOFVectorView.hpp>
16
#include <dune/amdis/io/FileWriterInterface.hpp>
17
#include <dune/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 Traits, class TreePath>
47
  class FileWriter
48
      : public FileWriterInterface
49
50
  {
  private: // typedefs and static constants
51
    using GlobalBasis = typename Traits::GlobalBasis;
52
    using GridView = typename GlobalBasis::GridView;
Praetorius, Simon's avatar
Praetorius, Simon committed
53
54
    using Vector = DOFVectorConstView<GlobalBasis,TreePath>;
    using Range = typename Vector::Range;
55

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

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

62
  public:
63
    /// Constructor.
64
    FileWriter(std::string baseName,
Praetorius, Simon's avatar
Praetorius, Simon committed
65
               Vector const& dofvector)
66
      : FileWriterInterface(baseName)
Praetorius, Simon's avatar
Praetorius, Simon committed
67
      , dofvector_(dofvector)
68
    {
Praetorius, Simon's avatar
Praetorius, Simon committed
69
70
      int m = Parameters::get<int>(baseName + "->ParaView mode").value_or(0);
      mode_ = (m == 0 ? Dune::VTK::ascii : Dune::VTK::appendedraw);
71

Praetorius, Simon's avatar
Praetorius, Simon committed
72
      int subSampling = Parameters::get<int>(baseName + "->subsampling").value_or(0);
73

Praetorius, Simon's avatar
Praetorius, Simon committed
74
75
76
77
      if (subSampling > 0)
        vtkWriter_ = std::make_shared<Dune::SubsamplingVTKWriter<GridView>>(gridView(), subSampling);
      else
        vtkWriter_ = std::make_shared<Dune::VTKWriter<GridView>>(gridView());
78

Praetorius, Simon's avatar
Praetorius, Simon committed
79
80
      vtkSeqWriter_ = std::make_shared<Dune::VTKSequenceWriter<GridView>>(vtkWriter_, filename_, dir_, "");
      vtkWriter_->addVertexData(dofvector_, Dune::VTK::FieldInfo(name_, VTKFieldType<Range>, VTKFieldSize<Range>));
81
82
    }

83

84
85
86
    /// Implements \ref FileWriterInterface::writeFiles
    virtual void writeFiles(AdaptInfo& adaptInfo, bool force) override
    {
Praetorius, Simon's avatar
Praetorius, Simon committed
87
      vtkSeqWriter_->write(adaptInfo.getTime(), mode_);
88
89
90
    }

  protected:
Praetorius, Simon's avatar
Praetorius, Simon committed
91
    GridView const& gridView() const
92
    {
Praetorius, Simon's avatar
Praetorius, Simon committed
93
      return dofvector_.basis().gridView();
94
95
    }

96
  private:
Praetorius, Simon's avatar
Praetorius, Simon committed
97
    Vector dofvector_;
98
99
100

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

Praetorius, Simon's avatar
Praetorius, Simon committed
102
103
    // represents VTK::OutputType: ascii, appendedraw
    Dune::VTK::OutputType mode_;
104
  };
105

106

Praetorius, Simon's avatar
Praetorius, Simon committed
107
  template <class Traits, class GlobalBasis, class TreePath>
108
109
  std::shared_ptr<FileWriter<Traits,TreePath>> makeFileWriterPtr(
      std::string baseName,
Praetorius, Simon's avatar
Praetorius, Simon committed
110
      DOFVectorConstView<GlobalBasis,TreePath> const& dofvector)
111
  {
Praetorius, Simon's avatar
Praetorius, Simon committed
112
    return std::make_shared<FileWriter<Traits,TreePath>>(baseName, dofvector);
113
114
  }

115
} // end namespace AMDiS