VectorBackend.hpp 2.69 KB
Newer Older
1
2
#pragma once

Praetorius, Simon's avatar
Praetorius, Simon committed
3
4
#include <boost/numeric/mtl/vector/dense_vector.hpp>

5
6
7
8
#include <dune/common/ftraits.hh>

#include <amdis/Output.hpp>
#include <amdis/common/FakeContainer.hpp>
Praetorius, Simon's avatar
Praetorius, Simon committed
9
10
#include <amdis/linearalgebra/VectorBase.hpp>
#include <amdis/typetree/MultiIndex.hpp>
11
12
13
14

namespace AMDiS
{
  /// \brief The basic container that stores a base vector data
Praetorius, Simon's avatar
Praetorius, Simon committed
15
16
17
  template <class T>
  class MTLVector
      : public VectorBase<MTLVector<T>>
18
19
20
  {
  public:
    /// The type of the base vector
Praetorius, Simon's avatar
Praetorius, Simon committed
21
    using BaseVector = mtl::dense_vector<T>;
22
23
24
25

    /// The type of the elements of the DOFVector
    using value_type = typename BaseVector::value_type;

Praetorius, Simon's avatar
Praetorius, Simon committed
26
27
28
29
    /// The index/size - type
    using size_type  = typename BaseVector::size_type;

  private:
30
31
32
33
34
35
36
37
    /// The type of the elements of the DOFVector
    using block_type = value_type;

    /// The underlying field type
    using field_type = typename Dune::FieldTraits<value_type>::field_type;

  public:
    /// Constructor. Constructs new BaseVector.
38
    template <class Basis>
Praetorius, Simon's avatar
Praetorius, Simon committed
39
    explicit MTLVector(Basis const&) {}
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

    /// Return the data-vector \ref vector_
    BaseVector const& vector() const
    {
      return vector_;
    }

    /// Return the data-vector \ref vector_
    BaseVector& vector()
    {
      return vector_;
    }

    /// Return the current size of the \ref vector_
    std::size_t size() const
    {
      return mtl::vec::size(vector_);
    }


    /// Resize the \ref vector_ to the size \p s
    template <class SizeInfo>
Praetorius, Simon's avatar
Praetorius, Simon committed
62
    void init(SizeInfo const& sizeInfo, bool clear)
63
    {
Praetorius, Simon's avatar
Praetorius, Simon committed
64
      vector_.change_dim(sizeInfo({}));
65
66
67
68
69
      if (clear)
        set_to_zero(vector_);
    }

    /// Access the entry \p i of the \ref vector with read-access.
Praetorius, Simon's avatar
Praetorius, Simon committed
70
71
    template <class MultiIndex>
    value_type& at(MultiIndex const& idx)
72
    {
Praetorius, Simon's avatar
Praetorius, Simon committed
73
74
      const size_type i = flatMultiIndex(idx);
      test_exit_dbg(i < size(),"Index {} out of range [0,{})", i, size());
75
76
77
      return vector_[i];
    }

Praetorius, Simon's avatar
Praetorius, Simon committed
78
79
80
    /// Access the entry \p i of the \ref vector with read-access.
    template <class MultiIndex>
    value_type const& at(MultiIndex const& idx) const
81
    {
Praetorius, Simon's avatar
Praetorius, Simon committed
82
83
84
      const size_type i = flatMultiIndex(idx);
      test_exit_dbg(i < size(), "Index {} out of range [0,{})", i, size());
      return vector_[i];
85
86
87
88
89
90
91
    }

  private:
    /// The data-vector (can hold a new BaseVector or a pointer to external data
    BaseVector vector_;
  };

92
93
94
95

  namespace Recursive
  {
    template <class T>
Praetorius, Simon's avatar
Praetorius, Simon committed
96
    struct ForEach<MTLVector<T>> : ForEach<VectorBase<MTLVector<T>>> {};
97
98

    template <class T>
Praetorius, Simon's avatar
Praetorius, Simon committed
99
    struct Transform<MTLVector<T>> : Transform<VectorBase<MTLVector<T>>> {};
100
101

    template <class T>
Praetorius, Simon's avatar
Praetorius, Simon committed
102
    struct InnerProduct<MTLVector<T>> : InnerProduct<VectorBase<MTLVector<T>>> {};
103
104

  } // end namespace Recursive
105
} // end namespace AMDiS