AMDiS  0.3
The Adaptive Multi-Dimensional Simulation Toolbox
MatrixBackend.hpp
1 #pragma once
2 
3 #include <list>
4 #include <string>
5 #include <memory>
6 
7 #include <dune/istl/bcrsmatrix.hh>
8 #include <dune/istl/matrixindexset.hh>
9 
10 #include <amdis/Output.hpp>
11 #include <amdis/linearalgebra/SymmetryStructure.hpp>
12 
13 namespace AMDiS
14 {
15  template <class T, class = void>
17  {
18  using type = Dune::FieldMatrix<T,1,1>;
19  };
20 
21  template <class T>
22  struct BlockMatrixType<T, typename T::field_type>
23  {
24  using type = T;
25  };
26 
27  template <class T, class C>
29  {
30  public:
32  using BaseMatrix = Dune::BCRSMatrix<typename BlockMatrixType<T>::type>;
33 
35  using Comm = C;
36 
38  using value_type = typename BaseMatrix::block_type;
39 
41  using size_type = typename BaseMatrix::size_type;
42 
43  public:
45  template <class Basis>
46  explicit ISTLBCRSMatrix(Basis const& rowBasis, Basis const&)
47  : comm_(&rowBasis.communicator())
48  {}
49 
51  BaseMatrix const& matrix() const
52  {
53  return matrix_;
54  }
55 
58  {
59  return matrix_;
60  }
61 
62  Comm const& comm() const { return *comm_; }
63 
65  template <class Pattern>
66  void init(Pattern const& pattern)
67  {
68  pattern.applyTo(matrix_);
69  initialized_ = true;
70  }
71 
73  void init()
74  {
75  matrix_ = value_type(0);
76  initialized_ = true;
77  }
78 
79  void finish()
80  {
81  initialized_ = false;
82  }
83 
84 
86  void insert(size_type r, size_type c, value_type const& value)
87  {
88  test_exit_dbg( initialized_, "Occupation pattern not initialized!");
89  test_exit_dbg( r < matrix_.N() && c < matrix_.M() ,
90  "Indices out of range [0,{})x[0,{})", matrix_.N(), matrix_.M() );
91  matrix_[r][c] += value;
92  }
93 
94  template <class Ind, class LocalMat>
95  void scatter(Ind const& idx, LocalMat const& mat)
96  {
97  scatter(idx, idx, mat);
98  }
99 
100  template <class RowInd, class ColInd, class LocalMat>
101  void scatter(RowInd const& rows, ColInd const& cols, LocalMat const& mat)
102  {
103  test_exit_dbg( initialized_, "Occupation pattern not initialized!");
104  for (size_type i = 0; i < size_type(rows.size()); ++i)
105  for (size_type j = 0; j < size_type(cols.size()); ++j)
106  matrix_[rows[i]][cols[j]] += mat[i][j];
107  }
108 
109  std::size_t nnz() const
110  {
111  return matrix_.nonzeroes();
112  }
113 
114  private:
115  BaseMatrix matrix_;
116  Comm const* comm_;
117 
118  bool initialized_ = false;
119  };
120 
121 } // end namespace AMDiS
BaseMatrix const & matrix() const
Return the data-vector vector.
Definition: MatrixBackend.hpp:51
void insert(size_type r, size_type c, value_type const &value)
Insert a single value into the matrix (add to existing value)
Definition: MatrixBackend.hpp:86
ISTLBCRSMatrix(Basis const &rowBasis, Basis const &)
Constructor. Constructs new BaseVector.
Definition: MatrixBackend.hpp:46
void test_exit_dbg(bool condition, Args &&... args)
call assert_msg, in debug mode only
Definition: Output.hpp:205
Contains all classes needed for solving linear and non linear equation systems.
Definition: AdaptBase.hpp:6
Dune::BCRSMatrix< typename BlockMatrixType< T >::type > BaseMatrix
The matrix type of the underlying base matrix.
Definition: MatrixBackend.hpp:32
C Comm
Communication type.
Definition: MatrixBackend.hpp:35
Definition: MatrixBackend.hpp:28
typename BaseMatrix::size_type size_type
The index/size - type.
Definition: MatrixBackend.hpp:41
void init(Pattern const &pattern)
Create occupation pattern and apply it to the matrix.
Definition: MatrixBackend.hpp:66
BaseMatrix & matrix()
Return the data-vector vector.
Definition: MatrixBackend.hpp:57
void init()
Set all entries to zero while keeping the occupation pattern intact.
Definition: MatrixBackend.hpp:73
Definition: MatrixBackend.hpp:16
typename BaseMatrix::block_type value_type
The type of the elements of the DOFMatrix.
Definition: MatrixBackend.hpp:38