HierarchicWrapper.hpp 2.51 KB
Newer Older
1
2
#pragma once

3
#include <boost/numeric/mtl/mtl_fwd.hpp>
4
5
#include <dune/functions/functionspacebases/sizeinfo.hh>

6
#include <amdis/utility/MultiIndex.hpp>
7

8
9
10
11
12
13
namespace AMDiS
{
  template <class Matrix>
  class HierarchicMatrixWrapper
  {
  public:
14
    explicit HierarchicMatrixWrapper(Matrix& matrix)
15
16
17
      : matrix_(matrix)
    {}

18
19
    template <class RowIndex, class ColIndex>
    auto& operator()(RowIndex const& row, ColIndex const& col)
20
    {
21
      return matrix_(flatMultiIndex(row), flatMultiIndex(col));
22
23
    }

24
25
    template <class RowIndex, class ColIndex>
    auto const& operator()(RowIndex const& row, ColIndex const& col) const
26
    {
27
      return matrix_(flatMultiIndex(row), flatMultiIndex(col));
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
    }


  private:
    Matrix& matrix_;
  };

  template <class Matrix>
  HierarchicMatrixWrapper<Matrix> hierarchicMatrixWrapper(Matrix& matrix)
  {
    return {matrix};
  }


  template <class Vector>
  class HierarchicVectorWrapper
  {
  public:
46
    explicit HierarchicVectorWrapper(Vector& vector)
47
48
49
      : vector_(vector)
    {}

50
51
    template <class Index>
    auto& operator[](Index const& idx)
52
    {
53
      return vector_[flatMultiIndex(idx)];
54
55
    }

56
57
    template <class Index>
    auto const& operator[](Index const& idx) const
58
    {
59
      return vector_[flatMultiIndex(idx)];
60
61
    }

62
63
64
65
66
67
    template <class Basis>
    void resize(Dune::Functions::SizeInfo<Basis> const& s)
    {
      resizeImpl(vector_, s);
    }

68
69
70
71
72
    std::size_t size() const
    {
      return sizeImpl(vector_);
    }

73
74
75
  protected:

    template <class Vec, class Basis>
Praetorius, Simon's avatar
Praetorius, Simon committed
76
    void_t<decltype( std::declval<Vec>().resize(std::size_t(0)) )>
77
    resizeImpl(Vec& vec, Dune::Functions::SizeInfo<Basis> const& s)
78
    {
79
      vec.resize(std::size_t(s));
80
81
    }

82
    template <class Vec, class Basis>
Praetorius, Simon's avatar
Praetorius, Simon committed
83
    void_t<decltype( std::declval<Vec>().change_dim(std::size_t(0)) )>
84
    resizeImpl(Vec& vec, Dune::Functions::SizeInfo<Basis> const& s)
85
    {
86
      vec.change_dim(std::size_t(s));
87
88
    }

89
    template <class... Params>
90
    std::size_t sizeImpl(mtl::vec::dense_vector<Params...> const& v) const
91
92
93
94
95
96
97
98
99
100
101
102
103
104
    {
      return mtl::size(v);
    }

    template <class V>
    using HasSizeMember = decltype(std::declval<V>().size());

    template <class V,
      std::enable_if_t<Dune::Std::is_detected<HasSizeMember, V>::value, int> = 0>
    std::size_t sizeImpl(V const& v) const
    {
      return v.size();
    }

105
106
107
108
109
  private:
    Vector& vector_;
  };

  template <class Vector>
110
  auto hierarchicVectorWrapper(Vector& vector)
111
  {
112
    return HierarchicVectorWrapper<Vector>{vector};
113
114
115
  }

} // end namespace AMDiS