FileWriter.hpp 4.75 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>
13
#include <dune/amdis/io/FileWriterInterface.hpp>
14
#include <dune/amdis/utility/Filesystem.hpp>
15
16
17

namespace AMDiS
{
18
  template <class Traits, class TreePath>
19
  class FileWriter
20
      : public FileWriterInterface
21
22
  {
  private: // typedefs and static constants
23

24
25
    using GlobalBasis = typename Traits::GlobalBasis;

26
    using GridView = typename GlobalBasis::GridView;
27

28
    /// Dimension of the mesh
29
    static constexpr int dim = GridView::dimension;
30

31
    /// Dimension of the world
32
    static constexpr int dow = GridView::dimensionworld;
33

34
    using Vector = DOFVector<GlobalBasis>;
35
36
37



38
  public:
39
    /// Constructor.
40
41
42
43
44
45
46
47
    FileWriter(std::string baseName,
               std::shared_ptr<GlobalBasis> const& basis,
               TreePath const& tp,
               std::shared_ptr<Vector> const& vector)
      : FileWriterInterface(baseName)
      , basis_(basis)
      , treePath_(tp)
      , vector_(vector)
48
    {
49
      //int subSampling = Parameters::get<int>(baseName + "->subsampling").value_or(0);
50

51
52
53
54
      //if (subSampling > 0)
      //  vtkWriter_ = std::make_shared<Dune::SubsamplingVTKWriter<GridView>>(basis_->gridView(), subSampling);
      //else
      vtkWriter_ = std::make_shared<Dune::VTKWriter<GridView>>(basis_->gridView());
55

56
57
58
      vtkSeqWriter_ = std::make_shared<Dune::VTKSequenceWriter<GridView>>(vtkWriter_, filename_, dir_, "");

      mode_ = Parameters::get<int>(baseName + "->ParaView mode").value_or(0);
59
60
    }

61

62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
    /// Implements \ref FileWriterInterface::writeFiles
    virtual void writeFiles(AdaptInfo& adaptInfo, bool force) override
    {
      using Tree = typename GlobalBasis::LocalView::Tree;
      using Node = typename Dune::TypeTree::ChildForTreePath<Tree, TreePath>;

      writeVertexData(typename Node::NodeTag{}, index_<Node::CHILDREN>, [&,this]()
      {
        if (mode_ == 0)
          vtkSeqWriter_->write(adaptInfo.getTime(), Dune::VTK::ascii);
        else if (mode_ == 1)
          vtkSeqWriter_->write(adaptInfo.getTime(), Dune::VTK::appendedraw);
      });
    }

  protected:
    template <class W>
    void writeVertexData(Dune::TypeTree::LeafNodeTag, index_t<0>, W write)
    {
81
82
      using Dune::Functions::BasisBuilder::makeBasis;
      using Dune::Functions::BasisBuilder::lagrange;
83
      auto fct = makeDiscreteFunctionPtr(basis_,treePath_,vector_);
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98

      auto p1basis = makeBasis(basis_->gridView(), lagrange<1>());
      auto data = makeDOFVector(p1basis, name_);
      interpolate(p1basis, data, *fct);

      auto dataFct = Dune::Functions::makeDiscreteGlobalBasisFunction<double>(p1basis,data);

      vtkWriter_->addVertexData(dataFct, Dune::VTK::FieldInfo(name_, Dune::VTK::FieldInfo::Type::scalar, 1));
      write();
      vtkWriter_->clear();
    }

    template <std::size_t C, class W>
    void writeVertexData(Dune::TypeTree::PowerNodeTag, index_t<C>, W write)
    {
99
100
101
102
103
      using Dune::Functions::BasisBuilder::makeBasis;
      using Dune::Functions::BasisBuilder::lagrange;
      using Dune::Functions::BasisBuilder::power;
      using Dune::Functions::BasisBuilder::flatLexicographic;

104
      assert( C == dow );
105
      auto fct = makeDiscreteFunctionPtr(basis_,treePath_,vector_);
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121

      auto p1basis = makeBasis(basis_->gridView(), power<C>(lagrange<1>(), flatLexicographic()));
      auto data = makeDOFVector(p1basis, name_);
      interpolate(p1basis, data, *fct);

      using Range = Dune::FieldVector<double,C>;
      auto dataFct = Dune::Functions::makeDiscreteGlobalBasisFunction<Range>(p1basis,data);

      vtkWriter_->addVertexData(dataFct, Dune::VTK::FieldInfo(name_, Dune::VTK::FieldInfo::Type::vector, C));
      write();
      vtkWriter_->clear();
    }

    template <class NodeTag, std::size_t C, class W>
    void writeVertexData(NodeTag, index_t<C>, W) {}

122
  private:
123
124
125
126
127
128
    std::shared_ptr<GlobalBasis> basis_;
    TreePath treePath_;
    std::shared_ptr<Vector> vector_;

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

130
    // std::vector<double> data_vector;
131

132
133
    // represents VTK::OutputType: 0...ascii, 1...appendedraw
    int mode_;
134
  };
135

136
137
138
139
140
141
142
143
144
145
146

  template <class Traits, class GlobalBasis, class TreePath, class Vector>
  std::shared_ptr<FileWriter<Traits,TreePath>> makeFileWriterPtr(
      std::string baseName,
      std::shared_ptr<GlobalBasis> const& basis,
      TreePath const& tp,
      std::shared_ptr<Vector> const& vector)
  {
    return std::make_shared<FileWriter<Traits,TreePath>>(baseName, basis, tp, vector);
  }

147
} // end namespace AMDiS