dune-assembler 2.12
Loading...
Searching...
No Matches
eigenbackend.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 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_ASSEMBLER_BACKENDS_EIGENBACKEND_HH
8#define DUNE_ASSEMBLER_BACKENDS_EIGENBACKEND_HH
9
10#include <type_traits>
11#include <utility>
12#include <vector>
13#include <variant>
14
17
23
24#include <Eigen/Dense>
25#include <Eigen/Sparse>
26
27
28
29namespace Dune::Assembler {
30
31
32
36template <class V>
38
39
40
46template <class T>
47class EigenVectorBackend<typename Eigen::Matrix<T, Eigen::Dynamic, 1>>
48{
49public:
50 using value_type = T;
51
52public:
53
54 using Vector = typename Eigen::Matrix<T, Eigen::Dynamic, 1>;
56
58 : vector_(vector)
59 {}
60
61 template <class SizeInfo>
62 void resize (const SizeInfo& sizeInfo)
63 {
64 vector_.resize(sizeInfo.size());
65 }
66
67 template <class MultiIndex>
68 const T& operator[] (const MultiIndex& index) const
69 {
70 return vector_[index];
71 }
72
73 template <class MultiIndex>
75 {
76 return vector_[index];
77 }
78
79 template <class LocalView>
80 void scatter (const LocalView& localView, const LocalVector& localVector)
81 {
82 for (std::size_t i = 0; i < localView.size(); ++i)
83 (*this)[localView.index(i)] += localVector[i];
84 }
85
86 template <class TT>
89 {
90 vector_.array() = value;
91 return *this;
92 }
93
94 void setZero ()
95 {
96 vector_.setZero();
97 }
98
99 const Vector& vector () const
100 {
101 return *vector_;
102 }
103
105 {
106 return *vector_;
107 }
108
109private:
110 Vector& vector_;
111};
112
113
114
115template <class T>
116EigenVectorBackend(Eigen::Matrix<T, Eigen::Dynamic, 1>&)
118
119
120
121template <class M>
123
124
125
126template <class T>
128 : public Impl::MatrixPatternBackendMixIn<EigenTripletMatrixPatternBackend<typename Eigen::SparseMatrix<T, Eigen::RowMajor>>>
129{
130 using M = Eigen::SparseMatrix<T, Eigen::RowMajor>;
131 using Base = Impl::MatrixPatternBackendMixIn<EigenTripletMatrixPatternBackend<M>>;
132 using Scalar = typename M::Scalar;
133
134public:
135
136 using Matrix = Eigen::SparseMatrix<T, Eigen::RowMajor>;
138
140 : matrix_(matrix)
141 {}
142
143 template <class RowSizeInfo, class ColSizeInfo>
145 {
146 rows_ = rowSizeInfo.size();
147 cols_ = colSizeInfo.size();
148 }
149
150 template <class RowIndex, class ColIndex>
151 void add (const RowIndex& rowIndex, const ColIndex& colIndex)
152 {
153 indexTriplets_.emplace_back(rowIndex[0], colIndex[0], 0);
154 }
155
157 {
158 matrix_.resize(rows_, cols_);
159 matrix_.setFromTriplets(indexTriplets_.begin(), indexTriplets_.end());
160 }
161
162private:
164 std::size_t rows_;
165 std::size_t cols_;
166 Matrix& matrix_;
167};
168
169
170
171template <class M>
173
174
175
176template <class T>
178 : public Impl::MatrixPatternBackendMixIn<EigenMatrixPatternBackend<typename Eigen::SparseMatrix<T, Eigen::RowMajor>>>
179{
180 using M = Eigen::SparseMatrix<T, Eigen::RowMajor>;
181 using Base = Impl::MatrixPatternBackendMixIn<EigenMatrixPatternBackend<M>>;
182
183 // Hack: Eigen::SparseMatrix::reserve(rowSizes) tries to detect
184 // a range by the presence of decltype(rowSizes)::value_type.
185 // Thus we have to augment Dune::TransformedRangeView by a suitable
186 // typedef.
187 template <class BaseRange, class V>
188 class WithValueType
189 : public BaseRange
190 {
191 public:
192 using value_type = V;
193
194 WithValueType (BaseRange&& baseRange)
195 : BaseRange(std::move(baseRange))
196 {}
197 };
198
199 template <class V, class BaseRange>
200 static auto withValueType (BaseRange&& baseRange)
201 {
202 return WithValueType<std::decay_t<BaseRange>, V> (std::move(baseRange));
203 }
204
205public:
206
207 using Matrix = Eigen::SparseMatrix<T, Eigen::RowMajor>;
209
211 : matrix_(matrix)
212 {}
213
214 template <class RowBasis, class ColBasis>
215 void resize (const RowBasis& rowBasis, const ColBasis& colBasis)
216 {
217 indexSet_.resize(rowBasis.size(), colBasis.size());
218 }
219
220 template <class RowIndex, class ColIndex>
221 void add (const RowIndex& rowIndex, const ColIndex& colIndex)
222 {
223 indexSet_.add(rowIndex[0], colIndex[0]);
224 }
225
227 {
228 std::size_t rows = indexSet_.rows();
229 std::size_t cols = indexSet_.cols();
230
231 matrix_.resize(rows, cols);
232
233 auto rowSizes = withValueType<std::size_t>(Dune::transformedRangeView(Dune::range(rows), [&](auto row) { return indexSet_.rowsize(row); }));
234 matrix_.reserve(rowSizes);
235
236 for (auto row : Dune::range(rows))
237 std::visit([&](const auto& colIndices) {
238 for (auto col : colIndices)
239 matrix_.insert(row, col) = 0;
240 }, indexSet_.columnIndices(row));
241
242 matrix_.finalize();
243 }
244
245private:
246 Matrix& matrix_;
247 Dune::MatrixIndexSet indexSet_;
248};
249
250
251
252
256template <class M, bool useTriplets=false>
258
259
260
266template <class T, bool useTriplets>
268{
269
270public:
271
272 using Matrix = Eigen::SparseMatrix<T, Eigen::RowMajor>;
273 using Entry = typename Matrix::Scalar;
277
279 : matrix_(&matrix)
280 {}
281
283 : matrix_(&matrix)
284 {}
285
287 {
288 if constexpr (useTriplets)
290 else
291 return EigenMatrixPatternBackend<Matrix>(*matrix_);
292 }
293
294 template <class RowMultiIndex, class ColMultiIndex>
295 const Entry& operator() (const RowMultiIndex& row, const ColMultiIndex& col) const
296 {
297 return matrix_->coeffRef(row, col);
298 }
299
300 template <class RowMultiIndex, class ColMultiIndex>
302 {
303 return matrix_->coeffRef(row, col);
304 }
305
306 template <class RowLocalView, class ColLocalView>
307 void scatter (const RowLocalView& rowLocalView, const ColLocalView& colLocalView,
309 {
310 auto rowIndices = Dune::Assembler::Impl::localIndices(rowLocalView.tree());
311 auto colIndices = Dune::Assembler::Impl::localIndices(colLocalView.tree());
312 localPattern.visitEntries(rowIndices, colIndices, [&](const auto& i, const auto&j) {
313 (*this)(rowLocalView.index(i), colLocalView.index(j)) += localMatrix[i][j];
314 });
315 }
316
317 void setZero ()
318 {
319 for (auto i: Dune::range(matrix_->rows()))
320 for (auto it = Matrix::InnerIterator(*matrix_, i); it; ++it)
321 it.valueRef() = 0.0;
322 }
323
327 const Matrix& matrix () const
328 {
329 return *matrix_;
330 }
331
336 {
337 return *matrix_;
338 }
339
343 template <class Value>
344 void assign (const Value& value)
345 {
346 auto& data = matrix_->data();
347 for (auto k : Dune::range(data.size()))
348 data.value(k) = value;
349 }
350
351protected:
352
354};
355
356
357
358template <class T>
359EigenMatrixBackend(Eigen::SparseMatrix<T, Eigen::RowMajor>&)
361
362
363
364} // namespace Dune::Assembler
365
366
367
368#endif // DUNE_ASSEMBLER_BACKENDS_EIGENBACKEND_HH
Definition localboundaryintersectionmatrixassembler.hh:9
Col col
reference operator[](size_type i)
SLList< T, A > & operator=(const SLList< T, A > &other)
auto transformedRangeView(R &&range, F &&f)
static constexpr IntegralRange< T > range(T from, T to) noexcept
size_type rowIndex() const
std::ptrdiff_t index() const
virtual void operator()()=0
VectorBackend for Eigen vectors.
Definition eigenbackend.hh:37
void resize(const SizeInfo &sizeInfo)
Definition eigenbackend.hh:62
void scatter(const LocalView &localView, const LocalVector &localVector)
Definition eigenbackend.hh:80
typename Eigen::Matrix< T, Eigen::Dynamic, 1 > Vector
Definition eigenbackend.hh:54
void add(const RowIndex &rowIndex, const ColIndex &colIndex)
Definition eigenbackend.hh:151
Eigen::SparseMatrix< T, Eigen::RowMajor > Matrix
Definition eigenbackend.hh:136
void resize(const RowSizeInfo &rowSizeInfo, const ColSizeInfo &colSizeInfo)
Definition eigenbackend.hh:144
Definition eigenbackend.hh:172
void resize(const RowBasis &rowBasis, const ColBasis &colBasis)
Definition eigenbackend.hh:215
void add(const RowIndex &rowIndex, const ColIndex &colIndex)
Definition eigenbackend.hh:221
Eigen::SparseMatrix< T, Eigen::RowMajor > Matrix
Definition eigenbackend.hh:207
MatrixBackend for Eigen matrices.
Definition eigenbackend.hh:257
void scatter(const RowLocalView &rowLocalView, const ColLocalView &colLocalView, const LocalMatrix &localMatrix, const LocalPattern &localPattern)
Definition eigenbackend.hh:307
EigenMatrixBackend(Matrix &matrix, std::bool_constant< useTriplets >)
Definition eigenbackend.hh:282
Eigen::SparseMatrix< T, Eigen::RowMajor > Matrix
Definition eigenbackend.hh:272
void assign(const Value &value)
Assign value to wrapped matrix.
Definition eigenbackend.hh:344
Matrix & matrix()
Mutable access to wrapped matrix.
Definition eigenbackend.hh:335
const Matrix & matrix() const
Const access to wrapped matrix.
Definition eigenbackend.hh:327
Definition flatmatrix.hh:17
Definition flatvector.hh:17
Definition localpattern.hh:70
T visit(T... args)