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

Praetorius, Simon's avatar
Praetorius, Simon committed
3
4
5
#include <dune/istl/bvector.hh>
#include <dune/functions/backends/istlvectorbackend.hh>

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

namespace AMDiS
{
Praetorius, Simon's avatar
Praetorius, Simon committed
13
14
15
16
17
18
19
20
21
22
23
24
  template <class T, class = void>
  struct BlockVectorType
  {
    using type = Dune::FieldVector<T,1>;
  };

  template <class T>
  struct BlockVectorType<T, typename T::field_type>
  {
    using type = T;
  };

25
	template <class T>
Praetorius, Simon's avatar
Praetorius, Simon committed
26
27
	class ISTLBlockVector
      : public VectorBase<ISTLBlockVector<T>>
28
29
30
	{
	public:
    /// The vector type of the underlying base vector
Praetorius, Simon's avatar
Praetorius, Simon committed
31
	  using BaseVector = Dune::BlockVector<typename BlockVectorType<T>::type>;
32
33
34
35
36

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

    /// The type of the elements of the DOFVector
Praetorius, Simon's avatar
Praetorius, Simon committed
37
    using value_type = T;
38
39
40
41
42
43
44
45
46

    /// The underlying field type
    using field_type = typename block_type::field_type;

    /// The index/size - type
	  using size_type  = typename BaseVector::size_type;

  public:
    /// Constructor. Constructs new BaseVector.
47
    template <class Basis>
Praetorius, Simon's avatar
Praetorius, Simon committed
48
    explicit ISTLBlockVector(Basis const&) {}
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

    /// 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 vector_.size();
    }

Praetorius, Simon's avatar
Praetorius, Simon committed
68
69
    /// Resize the \ref vector_ to the size given by the \p sizeProvider
    // NOTE: this resize works recursively on the hierarchy of the vector
70
    template <class SizeInfo>
Praetorius, Simon's avatar
Praetorius, Simon committed
71
    void init(SizeInfo const& sizeInfo, bool clear)
72
    {
Praetorius, Simon's avatar
Praetorius, Simon committed
73
      Dune::Functions::istlVectorBackend(vector_).resize(sizeInfo);
74
75
76
77
      if (clear)
        vector_ = 0;
    }

Praetorius, Simon's avatar
Praetorius, Simon committed
78
79
80
    /// Access the entry \p idx of the \ref vector_ with write-access.
    template <class MultiIndex>
    value_type& at(MultiIndex const& idx)
81
    {
Praetorius, Simon's avatar
Praetorius, Simon committed
82
      return Dune::Functions::istlVectorBackend(vector_)[idx];
83
84
    }

Praetorius, Simon's avatar
Praetorius, Simon committed
85
86
87
    /// Access the entry \p idx of the \ref vector_ with read-access.
    template <class MultiIndex>
    value_type const& at(MultiIndex const& idx) const
88
    {
Praetorius, Simon's avatar
Praetorius, Simon committed
89
      return Dune::Functions::istlVectorBackend(vector_)[idx];
90
91
92
93
94
95
96
    }

	private:
    BaseVector vector_;
	};

} // end namespace AMDiS