FileWriter.hpp 3.87 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;
53
    using Vector = DOFVectorConstView<Traits,TreePath>;
Praetorius, Simon's avatar
Praetorius, Simon committed
54
    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 69 70 71 72 73
    {
      init(baseName, ValueCategory_t<Range>{});
    }

    template <class ValueCat>
    void init(std::string const& baseName, ValueCat)
74
    {
Praetorius, Simon's avatar
Praetorius, Simon committed
75 76
      int m = Parameters::get<int>(baseName + "->ParaView mode").value_or(0);
      mode_ = (m == 0 ? Dune::VTK::ascii : Dune::VTK::appendedraw);
77

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

Praetorius, Simon's avatar
Praetorius, Simon committed
80 81 82 83
      if (subSampling > 0)
        vtkWriter_ = std::make_shared<Dune::SubsamplingVTKWriter<GridView>>(gridView(), subSampling);
      else
        vtkWriter_ = std::make_shared<Dune::VTKWriter<GridView>>(gridView());
84

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

89
    void init(std::string const&, tag::unknown) {}
90

91 92 93
    /// Implements \ref FileWriterInterface::writeFiles
    virtual void writeFiles(AdaptInfo& adaptInfo, bool force) override
    {
94 95
      if (!filesystem::exists(dir_))
        error_exit("Output directory '",dir_,"' does not exist!");
96 97
      if (vtkSeqWriter_)
        vtkSeqWriter_->write(adaptInfo.getTime(), mode_);
98 99 100
    }

  protected:
Praetorius, Simon's avatar
Praetorius, Simon committed
101
    GridView const& gridView() const
102
    {
Praetorius, Simon's avatar
Praetorius, Simon committed
103
      return dofvector_.basis().gridView();
104 105
    }

106
  private:
Praetorius, Simon's avatar
Praetorius, Simon committed
107
    Vector dofvector_;
108 109 110

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

Praetorius, Simon's avatar
Praetorius, Simon committed
112 113
    // represents VTK::OutputType: ascii, appendedraw
    Dune::VTK::OutputType mode_;
114
  };
115

116

117
  template <class Traits, class TreePath>
118 119
  std::shared_ptr<FileWriter<Traits,TreePath>> makeFileWriterPtr(
      std::string baseName,
120
      DOFVectorConstView<Traits,TreePath> const& dofvector)
121
  {
Praetorius, Simon's avatar
Praetorius, Simon committed
122
    return std::make_shared<FileWriter<Traits,TreePath>>(baseName, dofvector);
123 124
  }

125
} // end namespace AMDiS