Dune-Fufem 2.11-git
Loading...
Searching...
No Matches
istlmatrixbackend.hh
Go to the documentation of this file.
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3
4// SPDX-FileCopyrightText: Copyright © DUNE-FUFEM Project contributors, see file AUTHORS.md
5// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception OR LGPL-3.0-or-later
6
7#ifndef DUNE_FUFEM_BACKENDS_ISTLMATRIXBACKEND_HH
8#define DUNE_FUFEM_BACKENDS_ISTLMATRIXBACKEND_HH
9
10#ifndef DUNE_FUFEM_DISABLE_HEADER_DEPRECATION
11#warning This header is deprecated and will be removed after 2.11. Use the ISTLMatrixBackend from dune-assembler instead.
12#endif
13
22#include <type_traits>
23#include <utility>
24
29
31
32#include <dune/assembler/backends/singlerowcolmatrix.hh>
33
35
36
37
39
40// Concept for a ConstMatrixBackend
41// This class is deprecated and will be removed after 2.11.
42template<class RowBasis, class ColBasis=RowBasis>
43struct
45{
46 using RowMultiIndex = typename RowBasis::MultiIndex;
47 using ColMultiIndex = typename ColBasis::MultiIndex;
48
49 template<class V>
50 auto require(const V& v) -> decltype(
52 );
53};
54
55// Concept for a MatrixBackend
56template<class RowBasis, class ColBasis=RowBasis>
57struct
58[[deprecated("This class is deprecated and will be removed after 2.11.")]]
60{
61 using RowMultiIndex = typename RowBasis::MultiIndex;
62 using ColMultiIndex = typename ColBasis::MultiIndex;
63
64 template<class V>
65 auto require(const V& v) -> decltype(
66 const_cast<V&>(v).patternBuilder(),
68 );
69};
70
71} // namespace Dune::Fufem::Concept
72
73
74
75namespace Dune::Fufem {
76
77
78
79namespace Impl {
80
81
82
83 // The following is essentially a copy of Dune::Functions::hybridIndexAccess
84 // But since matrices do not provide size(), we have to use N() instead.
85 template<class C, class I, class F,
87 static auto hybridRowIndexAccess(C&& c, const I& i, F&& f)
88 -> decltype(f(c[i]))
89 {
90 return f(c[i]);
91 }
92
93 template<class C, class I, class F,
95 static decltype(auto) hybridRowIndexAccess(C&& c, const I& i, F&& f)
96 {
98 [&](const auto& ii) -> decltype(auto){
99 return f(c[ii]);
100 }, [&]() -> decltype(auto){
101 return f(c[Dune::Indices::_0]);
102 });
103 }
104
105
106
107 // Call f(matrix[i][j]) and return the result.
108 // This works with dynamic indices i,j, even for a multi-type matrix.
109 // However, it requires that f() has a unique return type.
110 template<class Matrix, class RowIndex, class ColIndex, class F>
111 decltype(auto) visitMatrixEntry(Matrix&& matrix, const RowIndex& i, const ColIndex& j, F&& f)
112 {
113 using namespace Dune::Functions;
114 return hybridRowIndexAccess(matrix, i, [&](auto&& matrix_i) -> decltype(auto) {
115 return hybridIndexAccess(matrix_i, j, f);
116 });
117 }
118
119
120
121 // Call f(matrix[i0][j0]...[in][jm]), by recursively resolving row- and column-multi-indices.
122 // Whenenver a SingleRowMatrix or SingleColumnMatrix is encountered, a zero row- or column-index
123 // is inserted, respectively. The recursion ends, whenever f(matrixEntry) can be called.
124 // This works with dynamic indices i,j, even for a multi-type matrix.
125 // However, it requires that f() has a unique return type.
126 template<class Matrix, class RowIndex, class ColIndex, class F>
127 static decltype(auto) visitMatrixEntryRecursive(Matrix& matrix, const RowIndex& iii, const ColIndex& jjj, F&& f)
128 {
129 using namespace Dune::Indices;
130 using namespace Dune::Functions::Imp;
131 static constexpr bool isSingleRow = Dune::Assembler::IsSingleRowMatrix<Matrix>::value;
132 static constexpr bool isSingleCol = Dune::Assembler::IsSingleColumnMatrix<Matrix>::value;
133 auto splitIndex = [] (auto&& multiIndex) { return std::make_tuple(multiIndex[_0], shiftedDynamicMultiIndex<1>(multiIndex)) ;};
135 {
136 return f(matrix);
137 }
138 else if constexpr (isSingleRow)
139 {
140 assert(jjj.size()>0);
141 auto [j, jj] = splitIndex(jjj);
142 // We have to capture jj explicitly by value, because capturing structured bindings
143 // by reference is not allowed before C++20
144 return visitMatrixEntry(matrix, _0, j, [&, jj=jj](auto&& matrix_0j) -> decltype(auto) {
145 return visitMatrixEntryRecursive(matrix_0j, iii, jj, f);
146 });
147 }
148 else if constexpr (isSingleCol)
149 {
150 assert(iii.size()>0);
151 auto [i, ii] = splitIndex(iii);
152 // We have to capture ii explicitly by value, because capturing structured bindings
153 // by reference is not allowed before C++20
154 return visitMatrixEntry(matrix, i, _0, [&, ii=ii](auto&& matrix_i0) -> decltype(auto) {
155 return visitMatrixEntryRecursive(matrix_i0, ii, jjj, f);
156 });
157 }
158 else
159 {
160 assert(iii.size()>0);
161 assert(jjj.size()>0);
162 auto [i, ii] = splitIndex(iii);
163 auto [j, jj] = splitIndex(jjj);
164 // We have to capture ii and jj explicitly by value, because capturing structured bindings
165 // by reference is not allowed before C++20
166 return visitMatrixEntry(matrix, i, j, [&, ii=ii, jj=jj](auto&& matrix_ij) -> decltype(auto) {
167 return visitMatrixEntryRecursive(matrix_ij, ii, jj, f);
168 });
169 }
170 }
171
172
173
174} // namespace Impl
175
176
177
182template<class M, class E=typename M::field_type>
183class
184[[deprecated("This class is deprecated and will be removed after 2.11. Use the ISTLMatrixBackend from dune-assembler instead.")]]
186{
187
188 template<class Result>
189 struct ToScalar;
190
191 template<class R>
192 struct ToScalar<R&>
193 {
194 template<class Matrix,
196 R& operator()(Matrix& matrix) {
197 return matrix;
198 }
199
201 return matrix[0][0];
202 }
203 };
204
205 template<class R>
206 struct ToScalar<const R&>
207 {
208 template<class Matrix,
210 const R& operator()(Matrix& matrix) {
211 return matrix;
212 }
213
214 const R& operator()(const Dune::FieldMatrix<R, 1, 1>& matrix) {
215 return matrix[0][0];
216 }
217 };
218
219
220
221public:
222
223 using Matrix = M;
224 using Entry = E;
226
228 matrix_(&matrix)
229 {}
230
232 {
233 return {*matrix_};
234 }
235
236 template<class RowMultiIndex, class ColMultiIndex>
237 const Entry& operator()(const RowMultiIndex& row, const ColMultiIndex& col) const
238 {
239 return Impl::visitMatrixEntryRecursive(*matrix_, row, col, ToScalar<const Entry&>());
240 }
241
242 template<class RowMultiIndex, class ColMultiIndex>
243 Entry& operator()(const RowMultiIndex& row, const ColMultiIndex& col)
244 {
245 return Impl::visitMatrixEntryRecursive(*matrix_, row, col, ToScalar<Entry&>());
246 }
247
251 const Matrix& matrix() const
252 {
253 return *matrix_;
254 }
255
260 {
261 return *matrix_;
262 }
263
267 template<class Value>
268 void assign(const Value& value)
269 {
270 matrix() = value;
271 }
272
273protected:
274
276};
277
278
279
284template<class Entry, class Matrix>
285[[deprecated("This function is deprecated and will be removed after 2.11. Use the ISTLMatrixBackend from dune-assembler instead.")]]
292
297template<class Matrix>
298[[deprecated("This function is deprecated and will be removed after 2.11. Use the ISTLMatrixBackend from dune-assembler instead.")]]
305
306
307
330template<class RowBasis, class ColBasis, class M>
331[[deprecated("This function is deprecated and will be removed after 2.11. Use the ISTLMatrixBackend from dune-assembler instead.")]]
332decltype(auto) toConstMatrixBackend(M& m) {
334 if constexpr (Dune::models<Dune::Fufem::Concept::ConstMatrixBackend<RowBasis,ColBasis>, M>()) {
335 return m;
336 } else {
338 }
340}
341
342
343
366template<class RowBasis, class ColBasis, class M>
367[[deprecated("This function is deprecated and will be removed after 2.11. Use the ISTLMatrixBackend from dune-assembler instead.")]]
368decltype(auto) toMatrixBackend(M& m) {
370 if constexpr (Dune::models<Dune::Fufem::Concept::MatrixBackend<RowBasis, ColBasis>, M>()) {
371 return m;
372 } else {
374 }
376}
377
378
379
380} // namespace Dune::Fufem
381
382
383
384#endif // DUNE_FUFEM_BACKENDS_ISTLMATRIXBACKEND_HH
Col col
auto hybridIndexAccess(C &&c, const I &i, F &&f) -> decltype(f(c[i]))
constexpr decltype(auto) switchCases(const Cases &cases, const Value &value, Branches &&branches, ElseBranch &&elseBranch)
#define DUNE_NO_DEPRECATED_END
#define DUNE_NO_DEPRECATED_BEGIN
static constexpr size_type M()
virtual void operator()()=0
constexpr index_constant< 0 > _0
Definition dunefunctionsboundaryfunctionalassembler.hh:29
decltype(auto) toMatrixBackend(M &m)
Return a MatrixBackend for given matrix.
Definition istlmatrixbackend.hh:368
decltype(auto) toConstMatrixBackend(M &m)
Return a ConstMatrixBackend for given matrix.
Definition istlmatrixbackend.hh:332
auto istlMatrixBackend(Matrix &matrix)
Definition istlmatrixbackend.hh:286
Definition istlmatrixbackend.hh:38
Definition istlmatrixbackend.hh:45
auto require(const V &v) -> decltype(v(std::declval< RowMultiIndex >(), std::declval< ColMultiIndex >()))
typename RowBasis::MultiIndex RowMultiIndex
Definition istlmatrixbackend.hh:46
typename ColBasis::MultiIndex ColMultiIndex
Definition istlmatrixbackend.hh:47
Definition istlmatrixbackend.hh:60
typename ColBasis::MultiIndex ColMultiIndex
Definition istlmatrixbackend.hh:62
typename RowBasis::MultiIndex RowMultiIndex
Definition istlmatrixbackend.hh:61
auto require(const V &v) -> decltype(const_cast< V & >(v).patternBuilder(), const_cast< V & >(v)(std::declval< RowMultiIndex >(), std::declval< ColMultiIndex >())=v(std::declval< RowMultiIndex >(), std::declval< ColMultiIndex >()))
Definition istlmatrixbackend.hh:186
Matrix * matrix_
Definition istlmatrixbackend.hh:275
M Matrix
Definition istlmatrixbackend.hh:223
const Entry & operator()(const RowMultiIndex &row, const ColMultiIndex &col) const
Definition istlmatrixbackend.hh:237
Matrix & matrix()
Mutable access to wrapped matrix.
Definition istlmatrixbackend.hh:259
Entry & operator()(const RowMultiIndex &row, const ColMultiIndex &col)
Definition istlmatrixbackend.hh:243
ISTLMatrixBackend(Matrix &matrix)
Definition istlmatrixbackend.hh:227
MatrixBuilder< Matrix > patternBuilder()
Definition istlmatrixbackend.hh:231
const Matrix & matrix() const
Const access to wrapped matrix.
Definition istlmatrixbackend.hh:251
E Entry
Definition istlmatrixbackend.hh:224
Entry value_type
Definition istlmatrixbackend.hh:225
void assign(const Value &value)
Assign value to wrapped matrix.
Definition istlmatrixbackend.hh:268
Helper class for building matrix pattern.
Definition matrixbuilder.hh:51
T forward(T... args)
T make_tuple(T... args)