DataTransfer.hpp 3.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#pragma once

#include <memory>
#include <type_traits>
#include <utility>

#include <dune/functions/functionspacebases/subspacebasis.hh>

#include <amdis/Output.hpp>
#include <amdis/utility/TreeData.hpp>
#include <amdis/utility/Visitor.hpp>

namespace AMDiS
{
  typedef enum {
    NO_OPERATION = 0,
    INTERPOLATE  = 1
  } DataTransferOperation;

  template <class Node, class RangeType>
  class NodeDataTransfer
  {
  public:
    template <class Basis, class Container>
    void preAdapt(Basis const& basis, Container const& coeff, bool mightCoarsen) {}

    template <class Basis, class Container>
    void postAdapt(Basis const& basis, Container& coeff, bool refined) const {}
  };


  template <class Container>
  class DataTransferInterface
  {
  public:
    /// Virtual destructor
    virtual ~DataTransferInterface() = default;

    /// Collect data that is needed before grid adaption
    virtual void preAdapt(Container const& container, bool mightCoarsen) = 0;

    /// Interpolate data to new grid after grid adaption
    virtual void postAdapt(Container& container, bool refined) const = 0;
  };


  /// Implementation of \ref DataTransferInterface that does not interpolation, but
  /// just resizes the containers to the dimension of the basis
  template <class Container>
  class NoDataTransfer
      : public DataTransferInterface<Container>
  {
  public:
    virtual void preAdapt(Container const& container, bool) override {}

    virtual void postAdapt(Container& container, bool) const override
    {
      container.compress();
    }
  };


  template <class Container, class Basis>
  class DataTransfer
      : public DataTransferInterface<Container>
  {
    template <class Node>
    using NDT = NodeDataTransfer<std::decay_t<Node>, typename Container::value_type>;

  public:
    DataTransfer(Basis const& basis)
      : basis_(&basis)
    {}

    /// Calls \ref NodeDataTransfer::preAdapt() on each basis node
    virtual void preAdapt(Container const& container, bool mightCoarsen) override
    {
      nodeDataTransfer_.init(*basis_);
      AMDiS::forEachLeafNode_(basis_->localView().tree(), [&](auto const& node, auto const& treePath)
      {
        auto subBasis = Dune::Functions::subspaceBasis(*basis_, treePath);
        nodeDataTransfer_[node].preAdapt(subBasis, container.vector(), mightCoarsen);
      });
    }

    /// Calls \ref NodeDataTransfer::postAdapt() on each basis node after compressing the
    /// Container the dimension of the basis
    virtual void postAdapt(Container& container, bool refined) const override
    {
      container.compress();
      AMDiS::forEachLeafNode_(basis_->localView().tree(), [&](auto const& node, auto const& treePath)
      {
        auto subBasis = Dune::Functions::subspaceBasis(*basis_, treePath);
        nodeDataTransfer_[node].postAdapt(subBasis, container.vector(), refined);
      });
    }

  private:
    Basis const* basis_;
    TreeData<Basis, NDT, true> nodeDataTransfer_;
  };


  /// Factory to create DataTransfer objects based on the \ref DataTransferOperation
  template <class Container>
  class DataTransferFactory
  {
    using Interface = DataTransferInterface<Container>;

  public:
    template <class Basis>
    static std::unique_ptr<Interface> create(DataTransferOperation op, Basis const& basis)
    {
      switch (op)
      {
        case NO_OPERATION:
          return std::make_unique<NoDataTransfer<Container>>();
        case INTERPOLATE:
          return std::make_unique<DataTransfer<Container, Basis>>(basis);
        default:
          error_exit("Invalid data transfer\n");
          return nullptr; // avoid warnings
      }
    }
  };

} // end namespace AMDiS