version 0.4.1
simpleassemblers.hh
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2021-2024 The Ikarus Developers mueller@ibb.uni-stuttgart.de
2// SPDX-License-Identifier: LGPL-3.0-or-later
3
9#pragma once
10#include <mutex>
11#include <ranges>
12#include <utility>
13
14#include <dune/functions/backends/istlvectorbackend.hh>
15
16#include <Eigen/Core>
17#include <Eigen/Sparse>
18
21
22namespace Ikarus {
31template <typename FEC, typename DV>
33{
34public:
35 using FEContainerRaw = std::remove_cvref_t<FEC>;
36 using FERequirementType = typename FEContainerRaw::value_type::FERequirementType;
38 using GlobalIndex = typename FEContainerRaw::value_type::GlobalIndex;
39 using Basis = typename DV::Basis;
40 using GridView = typename Basis::GridView;
41 using FEContainer = FEC;
42 using FEContainerType = std::conditional_t<std::is_reference_v<FEContainer>, const FEContainer, FEContainer>;
45
53 : feContainer_{std::forward<FEContainer>(fes)},
54 dirichletValues_{&dirichletValues} {
55 constraintsBelow_.reserve(dirichletValues_->size());
56 size_t counter = 0;
57 for (auto iv : std::ranges::iota_view{decltype(dirichletValues_->size())(0), dirichletValues_->size()}) {
58 constraintsBelow_.emplace_back(counter);
59 if (dirichletValues_->isConstrained(iv))
60 ++counter;
61 }
62 fixedDofs_ = dirichletValues_->fixedDOFsize();
63 }
64
69 size_t reducedSize() { return dirichletValues_->size() - fixedDofs_; }
70
75 size_t size() { return dirichletValues_->size(); }
76
84 Eigen::VectorXd createFullVector(Eigen::Ref<const Eigen::VectorXd> reducedVector);
85
90 auto& finiteElements() const { return feContainer_; }
91
98 [[nodiscard]] size_t constraintsBelow(size_t i) const { return constraintsBelow_[i]; }
99
106 [[nodiscard]] bool isConstrained(size_t i) const { return dirichletValues_->isConstrained(i); }
107
114 [[nodiscard]] size_t estimateOfConnectivity() const {
115 return dirichletValues_->basis().gridView().size(GridView::dimension) * 8;
116 }
117
118private:
119 FEContainerType feContainer_;
120 const DirichletValuesType* dirichletValues_;
121 std::vector<size_t> constraintsBelow_{};
122 size_t fixedDofs_{};
123};
124
125#ifndef DOXYGEN
126template <class T, class DirichletValuesType>
127FlatAssemblerBase(T&& fes, const DirichletValuesType& dirichletValues) -> FlatAssemblerBase<T, DirichletValuesType>;
128#endif
129
137template <typename FEC, typename DV>
138class ScalarAssembler : public FlatAssemblerBase<FEC, DV>
139{
140 using FEContainerRaw = std::remove_cvref_t<FEC>;
142
143public:
144 using typename Base::Basis;
145 using typename Base::DirichletValuesType;
146 using typename Base::FEContainer;
147 using typename Base::FERequirementType;
148 using typename Base::GlobalIndex;
149
158
165 const double& getScalar(const FERequirementType& feRequirements) { return getScalarImpl(feRequirements); }
166
167private:
174 double& getScalarImpl(const FERequirementType& feRequirements) {
175 scal_ = 0.0;
176 for (auto& fe : this->finiteElements()) {
177 scal_ += calculateScalar(fe, feRequirements);
178 }
179 return scal_;
180 }
181
182 double scal_{0.0};
183};
184
185#ifndef DOXYGEN
186template <class T, class DirichletValuesType>
187ScalarAssembler(T&& fes, const DirichletValuesType& dirichletValues) -> ScalarAssembler<T, DirichletValuesType>;
188#endif
189
197template <typename FEC, typename DV>
199{
200 using FEContainerRaw = std::remove_cvref_t<FEC>;
202
203public:
204 using typename Base::Basis;
205 using typename Base::DirichletValuesType;
206 using typename Base::FEContainer;
207 using typename Base::FERequirementType;
208 using typename Base::GlobalIndex;
209
210public:
219
226 const Eigen::VectorXd& getRawVector(const FERequirementType& feRequirements) {
227 return getRawVectorImpl(feRequirements);
228 }
229
237 const Eigen::VectorXd& getVector(const FERequirementType& feRequirements) { return getVectorImpl(feRequirements); }
238
246 const Eigen::VectorXd& getReducedVector(const FERequirementType& feRequirements) {
247 return getReducedVectorImpl(feRequirements);
248 }
249
250private:
251 void assembleRawVectorImpl(const FERequirementType& feRequirements, Eigen::VectorXd& assemblyVec);
252 Eigen::VectorXd& getRawVectorImpl(const FERequirementType& feRequirements);
253 Eigen::VectorXd& getVectorImpl(const FERequirementType& feRequirements);
254
255 Eigen::VectorXd& getReducedVectorImpl(const FERequirementType& feRequirements);
256
257 Eigen::VectorXd vecRaw_{};
258 Eigen::VectorXd vec_{};
259 Eigen::VectorXd vecRed_{};
260};
261
262#ifndef DOXYGEN
263template <class T, class DirichletValuesType>
264VectorFlatAssembler(T&& fes, const DirichletValuesType& dirichletValues) -> VectorFlatAssembler<T, DirichletValuesType>;
265#endif
266
275template <typename FEC, typename DV>
277{
278public:
279 using FEContainerRaw = std::remove_cvref_t<FEC>;
281
282 using typename Base::Basis;
283 using typename Base::DirichletValuesType;
284 using typename Base::FEContainer;
285 using typename Base::FERequirementType;
286 using typename Base::GlobalIndex;
287
296
297 using GridView = typename Basis::GridView;
298
305 const Eigen::SparseMatrix<double>& getRawMatrix(const FERequirementType& feRequirements) {
306 return getRawMatrixImpl(feRequirements);
307 }
308
316 const Eigen::SparseMatrix<double>& getMatrix(const FERequirementType& feRequirements) {
317 return getMatrixImpl(feRequirements);
318 }
319
327 const Eigen::SparseMatrix<double>& getReducedMatrix(const FERequirementType& feRequirements) {
328 return getReducedMatrixImpl(feRequirements);
329 }
330
331private:
332 void assembleRawMatrixImpl(const FERequirementType& feRequirements, Eigen::SparseMatrix<double>& assemblyMat);
333 Eigen::SparseMatrix<double>& getRawMatrixImpl(const FERequirementType& feRequirements);
334 Eigen::SparseMatrix<double>& getMatrixImpl(const FERequirementType& feRequirements);
335 Eigen::SparseMatrix<double>& getReducedMatrixImpl(const FERequirementType& feRequirements);
336
339 void createOccupationPattern(Eigen::SparseMatrix<double>& assemblyMat);
340
343 void createReducedOccupationPattern(Eigen::SparseMatrix<double>& assemblyMat);
344
346 void createLinearDOFsPerElement(Eigen::SparseMatrix<double>& assemblyMat);
347
350 void createLinearDOFsPerElementReduced(Eigen::SparseMatrix<double>& assemblyMat);
351
353 void preProcessSparseMatrix(Eigen::SparseMatrix<double>& assemblyMat);
354
356 void preProcessSparseMatrixReduced(Eigen::SparseMatrix<double>& assemblyMat);
357
358 Eigen::SparseMatrix<double> spMatRaw_;
359 Eigen::SparseMatrix<double> spMat_;
360 Eigen::SparseMatrix<double> spMatReduced_;
361 std::vector<std::vector<Eigen::Index>>
362 elementLinearIndices_;
363 std::vector<std::vector<Eigen::Index>>
364 elementLinearReducedIndices_;
365 std::once_flag sparsePreProcessorRaw_, sparsePreProcessor_,
366 sparsePreProcessorReduced_;
367};
368
369#ifndef DOXYGEN
370template <class FEC, class DV>
371SparseFlatAssembler(FEC&& fes, const DV& dirichletValues) -> SparseFlatAssembler<FEC, DV>;
372#endif
373
383template <typename FEC, typename DV>
385{
386public:
387 using FEContainerRaw = std::remove_cvref_t<FEC>;
389
390 using typename Base::Basis;
391 using typename Base::DirichletValuesType;
392 using typename Base::FEContainer;
393 using typename Base::FERequirementType;
394 using typename Base::GlobalIndex;
395
404
411 const Eigen::MatrixXd& getRawMatrix(const FERequirementType& feRequirements) {
412 return getRawMatrixImpl(feRequirements);
413 }
414
422 const Eigen::MatrixXd& getMatrix(const FERequirementType& feRequirements) { return getMatrixImpl(feRequirements); }
423
431 const Eigen::MatrixXd& getReducedMatrix(const FERequirementType& feRequirements) {
432 return getReducedMatrixImpl(feRequirements);
433 }
434
435private:
436 void assembleRawMatrixImpl(const FERequirementType& feRequirements, Eigen::MatrixXd& assemblyMat);
437 Eigen::MatrixXd& getRawMatrixImpl(const FERequirementType& feRequirements);
438 Eigen::MatrixXd& getMatrixImpl(const FERequirementType& feRequirements);
439 Eigen::MatrixXd& getReducedMatrixImpl(const FERequirementType& feRequirements);
440
441 Eigen::MatrixXd matRaw_{};
442 Eigen::MatrixXd mat_{};
443 Eigen::MatrixXd matRed_{};
444};
445
446#ifndef DOXYGEN
447// https://en.cppreference.com/w/cpp/language/class_template_argument_deduction
448template <class FEC, class DV>
449DenseFlatAssembler(FEC&& fes, const DV& dirichletValues) -> DenseFlatAssembler<FEC, DV>;
450#endif
451} // namespace Ikarus
452
453#include "simpleassemblers.inl"
Implementation of assembler functions.
Definition: simpleassemblers.hh:22
def dirichletValues(basis)
Definition: dirichlet_values.py:7
The FlatAssemblerBase takes care of common subtasks done by flat assemblers.
Definition: simpleassemblers.hh:33
std::remove_cvref_t< FEC > FEContainerRaw
Type of the raw finite element container.
Definition: simpleassemblers.hh:35
FlatAssemblerBase(FEContainer &&fes, const DirichletValuesType &dirichletValues)
Constructor for FlatAssemblerBase.
Definition: simpleassemblers.hh:52
FEC FEContainer
Type of the finite element container.
Definition: simpleassemblers.hh:41
Eigen::VectorXd createFullVector(Eigen::Ref< const Eigen::VectorXd > reducedVector)
Creates the full-sized vector of size Dof and inserts the values of a reduced vector at the "free" de...
Definition: simpleassemblers.inl:15
typename FEContainerRaw::value_type::FERequirementType FERequirementType
Type of the finite element requirement.
Definition: simpleassemblers.hh:37
bool isConstrained(size_t i) const
Returns true if a given degree of freedom is fixed by a Dirichlet boundary condition.
Definition: simpleassemblers.hh:106
size_t reducedSize()
Returns the size of the free degrees of freedom, which are not fixed by a Dirichlet boundary conditio...
Definition: simpleassemblers.hh:69
typename FEContainerRaw::value_type::GlobalIndex GlobalIndex
Type of the global index.
Definition: simpleassemblers.hh:38
size_t constraintsBelow(size_t i) const
Returns the number of constraints below a given degrees of freedom index.
Definition: simpleassemblers.hh:98
auto & finiteElements() const
Returns the container of finite elements.
Definition: simpleassemblers.hh:90
DV DirichletValuesType
Type of the Dirichlet values.
Definition: simpleassemblers.hh:44
typename Basis::GridView GridView
Type of the grid view.
Definition: simpleassemblers.hh:40
std::conditional_t< std::is_reference_v< FEContainer >, const FEContainer, FEContainer > FEContainerType
Type of the finite element container (reference or by value).
Definition: simpleassemblers.hh:43
size_t estimateOfConnectivity() const
Coarse estimate of node connectivity, i.e., this relates to the bandwidth of a sparse matrix....
Definition: simpleassemblers.hh:114
size_t size()
Returns the size of nodes, i.e., the number of degrees of freedom.
Definition: simpleassemblers.hh:75
typename DV::Basis Basis
Type of the basis.
Definition: simpleassemblers.hh:39
ScalarAssembler assembles scalar quantities.
Definition: simpleassemblers.hh:139
const double & getScalar(const FERequirementType &feRequirements)
Calculates the scalar quantity requested by feRequirements and returns a reference.
Definition: simpleassemblers.hh:165
FEC FEContainer
Type of the finite element container.
Definition: simpleassemblers.hh:41
typename FEContainerRaw::value_type::FERequirementType FERequirementType
Type of the finite element requirement.
Definition: simpleassemblers.hh:37
typename FEContainerRaw::value_type::GlobalIndex GlobalIndex
Type of the global index.
Definition: simpleassemblers.hh:38
DV DirichletValuesType
Type of the Dirichlet values.
Definition: simpleassemblers.hh:44
ScalarAssembler(FEContainer &&fes, const DirichletValuesType &dirichletValues)
Constructor for ScalarAssembler.
Definition: simpleassemblers.hh:156
typename DV::Basis Basis
Type of the basis.
Definition: simpleassemblers.hh:39
VectorFlatAssembler assembles vector quantities using a flat basis Indexing strategy.
Definition: simpleassemblers.hh:199
const Eigen::VectorXd & getVector(const FERequirementType &feRequirements)
Calculates the vectorial quantity requested by feRequirements and returns a reference....
Definition: simpleassemblers.hh:237
const Eigen::VectorXd & getRawVector(const FERequirementType &feRequirements)
Calculates the vectorial quantity requested by feRequirements and returns a reference.
Definition: simpleassemblers.hh:226
FEC FEContainer
Type of the finite element container.
Definition: simpleassemblers.hh:41
const Eigen::VectorXd & getReducedVector(const FERequirementType &feRequirements)
Calculates the vectorial quantity requested by feRequirements and returns a reference....
Definition: simpleassemblers.hh:246
typename FEContainerRaw::value_type::FERequirementType FERequirementType
Type of the finite element requirement.
Definition: simpleassemblers.hh:37
typename FEContainerRaw::value_type::GlobalIndex GlobalIndex
Type of the global index.
Definition: simpleassemblers.hh:38
DV DirichletValuesType
Type of the Dirichlet values.
Definition: simpleassemblers.hh:44
VectorFlatAssembler(FEContainer &&fes, const DirichletValuesType &dirichletValues)
Constructor for VectorFlatAssembler.
Definition: simpleassemblers.hh:217
typename DV::Basis Basis
Type of the basis.
Definition: simpleassemblers.hh:39
SparseFlatAssembler assembles matrix quantities using a flat basis Indexing strategy....
Definition: simpleassemblers.hh:277
typename FEContainerRaw::value_type::FERequirementType FERequirementType
Type of the finite element requirement.
Definition: simpleassemblers.hh:37
const Eigen::SparseMatrix< double > & getRawMatrix(const FERequirementType &feRequirements)
Calculates the matrix quantity requested by feRequirements and returns a reference.
Definition: simpleassemblers.hh:305
const Eigen::SparseMatrix< double > & getReducedMatrix(const FERequirementType &feRequirements)
Calculates the matrix quantity requested by feRequirements and returns a reference....
Definition: simpleassemblers.hh:327
SparseFlatAssembler(FEContainer &&fes, const DirichletValuesType &dirichletValues)
Constructor for SparseFlatAssembler.
Definition: simpleassemblers.hh:294
const Eigen::SparseMatrix< double > & getMatrix(const FERequirementType &feRequirements)
Calculates the matrix quantity requested by feRequirements and returns a reference....
Definition: simpleassemblers.hh:316
DenseFlatAssembler assembles matrix quantities using a flat basis Indexing strategy....
Definition: simpleassemblers.hh:385
const Eigen::MatrixXd & getRawMatrix(const FERequirementType &feRequirements)
Calculates the matrix quantity requested by feRequirements and returns a reference.
Definition: simpleassemblers.hh:411
const Eigen::MatrixXd & getMatrix(const FERequirementType &feRequirements)
Calculates the matrix quantity requested by feRequirements and returns a reference....
Definition: simpleassemblers.hh:422
const Eigen::MatrixXd & getReducedMatrix(const FERequirementType &feRequirements)
Calculates the matrix quantity requested by feRequirements and returns a reference....
Definition: simpleassemblers.hh:431
typename FEContainerRaw::value_type::FERequirementType FERequirementType
Type of the finite element requirement.
Definition: simpleassemblers.hh:37
DenseFlatAssembler(FEContainer &&fes, const DirichletValuesType &dirichletValues)
< Type of the global index.
Definition: simpleassemblers.hh:402
Definition of DirichletValues class for handling Dirichlet boundary conditions.