AMDiS 2.10
The Adaptive Multi-Dimensional Simulation Toolbox
MatrixFacade.hpp
1#pragma once
2
3#include <type_traits>
4
5#include <dune/common/shared_ptr.hh>
6
7#include <amdis/common/Concepts.hpp>
8#include <amdis/common/ConceptsBase.hpp>
9#include <amdis/common/TypeTraits.hpp>
10#include <amdis/functions/NodeIndices.hpp>
11#include <amdis/typetree/MultiIndex.hpp>
12
13namespace AMDiS
14{
23 template <class T, template <class> class MatrixImpl>
25 {
26 using Self = MatrixFacade;
27 using Impl = MatrixImpl<T>;
28
29 public:
32 template <class RowBasis, class ColBasis>
33 MatrixFacade(RowBasis const& rowBasis, ColBasis const& colBasis)
34 : impl_(rowBasis.indexDistribution(), colBasis.indexDistribution())
35 {}
36
38 Impl const& impl() const { return impl_; }
39 Impl& impl() { return impl_; }
40
42 template <class SparsityPattern>
43 void init(SparsityPattern const& pattern)
44 {
45 impl_.init(pattern);
46 }
47
49 void init()
50 {
51 impl_.init();
52 }
53
55 void finish()
56 {
57 impl_.finish();
58 }
59
61 template <class RowIndex, class ColIndex,
62 REQUIRES(Concepts::MultiIndex<RowIndex>),
63 REQUIRES(Concepts::MultiIndex<ColIndex>)>
64 void insert(RowIndex const& row, ColIndex const& col, typename Impl::value_type const& value)
65 {
66 impl_.insert(row, col, value);
67 }
68
71 template <class RowLocalView, class ColLocalView, class LocalMatrix,
72 REQUIRES(Concepts::LocalView<RowLocalView>),
73 REQUIRES(Concepts::LocalView<ColLocalView>)>
74 void scatter(RowLocalView const& r, ColLocalView const& c, LocalMatrix const& localMatrix)
75 {
76 assert(r.size() * c.size() == localMatrix.size());
77 assert(r.size() == localMatrix.rows());
78 assert(c.size() == localMatrix.cols());
79
80 const bool optimized = std::is_same_v<RowLocalView,ColLocalView>
81 && std::uintptr_t(&r) == std::uintptr_t(&c);
82
83 if (optimized)
84 impl_.scatter(nodeIndices(r), localMatrix);
85 else
86 impl_.scatter(nodeIndices(r), nodeIndices(c), localMatrix);
87 }
88
90 template <class Idx,
91 REQUIRES(Concepts::MultiIndex<Idx>)>
92 void zeroRows(std::vector<Idx> const& ind, bool diag)
93 {
94 impl_.zeroRows(ind, diag);
95 }
96
98 template <class RowIdx, class ColIdx,
99 REQUIRES(Concepts::MultiIndex<RowIdx>),
100 REQUIRES(Concepts::MultiIndex<ColIdx>)>
101 void zeroRows(std::vector<RowIdx> const& rowInd, std::vector<ColIdx> const& colInd, bool diag)
102 {
103 assert(rowInd.size() == colInd.size());
104 impl_.zeroRows(rowInd, colInd, diag);
105 }
106
108 template <class Idx, class VecX, class VecB,
109 REQUIRES(Concepts::MultiIndex<Idx>)>
110 void zeroRowsColumns(std::vector<Idx> const& ind, bool diag, VecX const& x, VecB& b)
111 {
112 impl_.zeroRowsColumns(ind, diag, x.impl(), b.impl());
113 }
114
116 template <class Idx,
117 REQUIRES(Concepts::MultiIndex<Idx>)>
118 void zeroRowsColumns(std::vector<Idx> const& ind, bool diag)
119 {
120 impl_.zeroRowsColumns(ind, diag);
121 }
122
124 template <class RowIdx, class ColIdx, class VecX, class VecB,
125 REQUIRES(Concepts::MultiIndex<RowIdx>),
126 REQUIRES(Concepts::MultiIndex<ColIdx>)>
127 void zeroRowsColumns(std::vector<RowIdx> const& rowInd, std::vector<ColIdx> const& colInd, bool diag, VecX const& x, VecB& b)
128 {
129 assert(rowInd.size() == colInd.size());
130 impl_.zeroRowsColumns(rowInd, colInd, diag, x.impl(), b.impl());
131 }
132
134 template <class RowIdx, class ColIdx,
135 REQUIRES(Concepts::MultiIndex<RowIdx>),
136 REQUIRES(Concepts::MultiIndex<ColIdx>)>
137 void zeroRowsColumns(std::vector<RowIdx> const& rowInd, std::vector<ColIdx> const& colInd, bool diag)
138 {
139 assert(rowInd.size() == colInd.size());
140 impl_.zeroRowsColumns(rowInd, colInd, diag);
141 }
142
144 std::size_t nnz() const
145 {
146 return impl_.nnz();
147 }
148
149 protected:
152 };
153
154} // end namespace AMDiS
Definition: MatrixFacade.hpp:25
void zeroRowsColumns(std::vector< RowIdx > const &rowInd, std::vector< ColIdx > const &colInd, bool diag, VecX const &x, VecB &b)
Set all entries of the specified rows and columns to zero and the diagonal elements defined as pairs ...
Definition: MatrixFacade.hpp:127
void init()
Initialize the matrix for insertion while keeping the pattern unchanged.
Definition: MatrixFacade.hpp:49
std::size_t nnz() const
Number of nonzeros in the matrix.
Definition: MatrixFacade.hpp:144
void init(SparsityPattern const &pattern)
Initialize the matrix for insertion and allocate the non-zero pattern.
Definition: MatrixFacade.hpp:43
MatrixFacade(RowBasis const &rowBasis, ColBasis const &colBasis)
Definition: MatrixFacade.hpp:33
Impl impl_
The matrix backend.
Definition: MatrixFacade.hpp:151
void zeroRowsColumns(std::vector< RowIdx > const &rowInd, std::vector< ColIdx > const &colInd, bool diag)
Set all entries of the specified rows and columns to zero and the diagonal elements defined as pairs ...
Definition: MatrixFacade.hpp:137
void zeroRowsColumns(std::vector< Idx > const &ind, bool diag)
Set all entries of the specified rows and columns==rows to zero and the main diagonal element to 1 if...
Definition: MatrixFacade.hpp:118
void scatter(RowLocalView const &r, ColLocalView const &c, LocalMatrix const &localMatrix)
Definition: MatrixFacade.hpp:74
void finish()
Finish the matrix insertion, e.g. cleanup or final insertion.
Definition: MatrixFacade.hpp:55
void zeroRowsColumns(std::vector< Idx > const &ind, bool diag, VecX const &x, VecB &b)
Set all entries of the specified rows and columns==rows to zero and the diagonal element to 1 if diag...
Definition: MatrixFacade.hpp:110
void zeroRows(std::vector< Idx > const &ind, bool diag)
Set all entries of the specified rows to zero and the diagonal element to diag
Definition: MatrixFacade.hpp:92
void insert(RowIndex const &row, ColIndex const &col, typename Impl::value_type const &value)
Insert a single value into the matrix (add to existing value)
Definition: MatrixFacade.hpp:64
void zeroRows(std::vector< RowIdx > const &rowInd, std::vector< ColIdx > const &colInd, bool diag)
Set all entries of the specified rows to zero and the diagonal element to diag
Definition: MatrixFacade.hpp:101
Impl const & impl() const
Return the underlying matrix backend.
Definition: MatrixFacade.hpp:38
A general sparsity pattern implementation using the full pattern of the basis by adding all local ind...
Definition: SparsityPattern.hpp:15