version 0.4.1
assembler/interface.hh
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2021-2025 The Ikarus Developers mueller@ibb.uni-stuttgart.de
2// SPDX-License-Identifier: LGPL-3.0-or-later
3
9#pragma once
10#include <ranges>
11
12#include <dune/common/referencehelper.hh>
13
18
19namespace Ikarus {
28template <typename FEC, typename DV>
30{
31public:
32 using FEContainerRaw = std::remove_cvref_t<FEC>;
33 using FERequirement = typename FEContainerRaw::value_type::Requirement;
35 using GlobalIndex = typename FEContainerRaw::value_type::GlobalIndex;
36 using Basis = typename DV::Basis;
37 using GridView = typename Basis::GridView;
38 using FEContainer = FEC;
41 using SizeType = typename DirichletValuesType::SizeType;
43
50 template <typename FEContainer_ = FEContainer, typename DirichletValuesType_ = DirichletValuesType>
51 FlatAssemblerBase(FEContainer_&& fes, DirichletValuesType_&& dirichletValues)
52 : feContainer_{std::forward<FEContainer_>(fes)},
53 dirichletValues_{std::forward<DirichletValuesType_>(dirichletValues)} {
54 constraintsBelow_.reserve(dirichletValues_.size());
55 size_t counter = 0;
56 for (auto iv : std::ranges::iota_view{decltype(dirichletValues_.size())(0), dirichletValues_.size()}) {
57 constraintsBelow_.emplace_back(counter);
58 if (dirichletValues_.isConstrained(iv))
59 ++counter;
60 }
61 fixedDofs_ = dirichletValues_.fixedDOFsize();
62 }
63
68 size_t reducedSize() { return dirichletValues_.size() - fixedDofs_; }
69
74 size_t size() { return dirichletValues_.size(); }
75
83 Eigen::VectorXd createFullVector(Eigen::Ref<const Eigen::VectorXd> reducedVector);
84
89 const auto& finiteElements() const { return Dune::resolveRef(feContainer_); }
90
95 auto& finiteElements() { return Dune::resolveRef(feContainer_); }
96
101 const auto& dirichletValues() const { return Dune::resolveRef(dirichletValues_); }
102
107 const auto& gridView() const { return Dune::resolveRef(dirichletValues_.basis().gridView()); }
108
115 [[nodiscard]] size_t constraintsBelow(SizeType i) const { return constraintsBelow_[i]; }
116
123 [[nodiscard]] bool isConstrained(SizeType i) const { return dirichletValues_.isConstrained(i); }
124
131 [[nodiscard]] size_t estimateOfConnectivity() const {
132 return dirichletValues_.basis().gridView().size(GridView::dimension) * 8;
133 }
134
142 DBCOption dbcOption = DBCOption::Full) {
143 req_ = std::make_optional<FERequirement>(req);
144 affordances_ = std::make_optional<AffordanceCollectionType>(affordanceCollection);
145 dBCOption_ = std::make_optional<DBCOption>(dbcOption);
146 }
147
153 void bind(const FERequirement& req) { req_ = std::make_optional<FERequirement>(req); }
154
161 affordances_ = std::make_optional<AffordanceCollectionType>(affordanceCollection);
162 }
163
169 void bind(DBCOption dbcOption) { dBCOption_ = std::make_optional<DBCOption>(dbcOption); }
170
176 [[nodiscard]]
177 bool bound() const {
179 return true;
180 else
181 DUNE_THROW(Dune::InvalidStateException, "The assembler is not bound to a requirement, affordance or dBCOption.");
182 }
183
189 [[nodiscard]]
190 bool boundToRequirement() const {
191 return req_.has_value();
192 }
193
199 [[nodiscard]]
201 return affordances_.has_value();
202 }
203
209 [[nodiscard]]
210 bool boundToDBCOption() const {
211 return dBCOption_.has_value();
212 }
213
219 if (req_.has_value())
220 return req_.value();
221 else
222 DUNE_THROW(Dune::InvalidStateException, "The requirement can only be obtained after binding");
223 }
224
230 if (affordances_.has_value())
231 return affordances_.value();
232 else
233 DUNE_THROW(Dune::InvalidStateException, "The affordance can only be obtained after binding");
234 }
235
241 if (dBCOption_.has_value())
242 return dBCOption_.value();
243 else
244 DUNE_THROW(Dune::InvalidStateException, "The dBCOption can only be obtained after binding");
245 }
246
247private:
248 FEContainer feContainer_;
249 DirichletValuesType dirichletValues_;
250 std::optional<FERequirement> req_;
251 std::optional<AffordanceCollectionType> affordances_;
252 std::vector<size_t> constraintsBelow_{};
253 size_t fixedDofs_{};
254 std::optional<DBCOption> dBCOption_;
255};
256
257#ifndef DOXYGEN
259template <class FEV, class DirichletValuesType>
260FlatAssemblerBase(const FEV& fes,
261 const DirichletValuesType& dirichletValues) -> FlatAssemblerBase<FEV, DirichletValuesType>;
262#endif
263
271template <typename SA, typename FEC, typename DV, typename ST>
273{
274public:
276 using ScalarType = ST;
278 using FEContainer = FEC;
279 using FEContainerRaw = std::remove_cvref_t<FEC>;
280 using FERequirement = typename FEContainerRaw::value_type::Requirement;
281
289 ScalarType& scalar(const FERequirement& feRequirements, ScalarAffordance affordance) {
290 return underlying().getScalarImpl(feRequirements, affordance);
291 }
292
299 return underlying().getScalarImpl(underlying().requirement(),
300 underlying().affordanceCollection().scalarAffordance());
301 }
302
303private:
304 //> CRTP
305 const auto& underlying() const { return static_cast<const ScalarAssemblerType&>(*this); }
306 auto& underlying() { return static_cast<ScalarAssemblerType&>(*this); }
307};
308
316template <typename VA, typename FEC, typename DV, typename VT>
318{
319public:
321 using VectorType = VT;
322
323 using FEContainerRaw = std::remove_cvref_t<FEC>;
324 using FERequirement = typename FEContainerRaw::value_type::Requirement;
326 using GlobalIndex = typename FEContainerRaw::value_type::GlobalIndex;
327
329 using FEContainer = FEC;
330
344 VectorType& vector(const FERequirement& feRequirements, VectorAffordance affordance,
345 DBCOption dbcOption = DBCOption::Full) {
346 if (dbcOption == DBCOption::Raw) {
347 return underlying().getRawVectorImpl(feRequirements, affordance);
348 } else if (dbcOption == DBCOption::Reduced) {
349 return underlying().getReducedVectorImpl(feRequirements, affordance);
350 } else if (dbcOption == DBCOption::Full) {
351 return underlying().getVectorImpl(feRequirements, affordance);
352 }
353 __builtin_unreachable();
354 }
355
367 return vector(underlying().requirement(), underlying().affordanceCollection().vectorAffordance(), dbcOption);
368 }
369
378 VectorType& vector() { return vector(underlying().dBCOption()); }
379
380private:
381 //> CRTP
382 const auto& underlying() const { return static_cast<const VectorAssemblerType&>(*this); }
383 auto& underlying() { return static_cast<VectorAssemblerType&>(*this); }
384};
385
393template <typename MA, typename FEC, typename DV, typename MT>
395{
396public:
398 using MatrixType = MT;
399
400 using FEContainerRaw = std::remove_cvref_t<FEC>;
401 using FERequirement = typename FEContainerRaw::value_type::Requirement;
403 using GlobalIndex = typename FEContainerRaw::value_type::GlobalIndex;
404
406 using FEContainer = FEC;
407
419 MatrixType& matrix(const FERequirement& feRequirements, MatrixAffordance affordance,
420 DBCOption dbcOption = DBCOption::Full) {
421 if (dbcOption == DBCOption::Raw) {
422 return underlying().getRawMatrixImpl(feRequirements, affordance);
423 } else if (dbcOption == DBCOption::Reduced) {
424 return underlying().getReducedMatrixImpl(feRequirements, affordance);
425 } else if (dbcOption == DBCOption::Full) {
426 return underlying().getMatrixImpl(feRequirements, affordance);
427 }
428 __builtin_unreachable();
429 }
430
440 return matrix(underlying().requirement(), underlying().affordanceCollection().matrixAffordance(), dbcOption);
441 }
442
451 MatrixType& matrix() { return matrix(underlying().dBCOption()); }
452
453private:
454 //> CRTP
455 const auto& underlying() const { return static_cast<const MatrixAssemblerType&>(*this); }
456 auto& underlying() { return static_cast<MatrixAssemblerType&>(*this); }
457};
458} // namespace Ikarus
Definition of the LinearElastic class for finite element mechanics computations.
Definition: assemblermanipulatorbuildingblocks.hh:22
auto vectorAffordance(MatrixAffordance affordanceM)
Definition: ferequirements.hh:177
MatrixAffordance
A strongly typed enum class representing the matrix affordance.
Definition: ferequirements.hh:64
auto scalarAffordance(MatrixAffordance affordanceM)
Definition: ferequirements.hh:186
DBCOption
Definition: dirichletbcenforcement.hh:8
VectorAffordance
A strongly typed enum class representing the vector affordance.
Definition: ferequirements.hh:49
ScalarAffordance
A strongly typed enum class representing the scalar affordance.
Definition: ferequirements.hh:38
def dirichletValues(basis)
Definition: dirichlet_values.py:38
The FlatAssemblerBase takes care of common subtasks done by flat assemblers.
Definition: assembler/interface.hh:30
void bind(AffordanceCollectionType affordanceCollection)
Binds the assembler to an affordance collection.
Definition: assembler/interface.hh:160
std::remove_cvref_t< FEC > FEContainerRaw
Type of the raw finite element container.
Definition: assembler/interface.hh:32
void bind(const FERequirement &req, AffordanceCollectionType affordanceCollection, DBCOption dbcOption=DBCOption::Full)
Binds the assembler to a set of finite element requirement and affordance.
Definition: assembler/interface.hh:141
typename FEContainerRaw::value_type::Requirement FERequirement
Type of the finite element requirement.
Definition: assembler/interface.hh:34
typename DirichletValuesType::SizeType SizeType
size_type of the container storing Dirichlet flags
Definition: assembler/interface.hh:41
void bind(DBCOption dbcOption)
Binds the assembler to an affordance collection.
Definition: assembler/interface.hh:169
void bind(const FERequirement &req)
Binds the assembler to a finite element requirement.
Definition: assembler/interface.hh:153
bool boundToAffordanceCollection() const
Returns true if the assembler is bound to an affordance collection.
Definition: assembler/interface.hh:200
FEC FEContainer
Type of the finite element container (reference or by value).
Definition: assembler/interface.hh:39
const auto & dirichletValues() const
Returns the dirichlet value object.
Definition: assembler/interface.hh:101
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:25
FlatAssemblerBase(FEContainer_ &&fes, DirichletValuesType_ &&dirichletValues)
Constructor for FlatAssemblerBase.
Definition: assembler/interface.hh:51
FERequirement & requirement()
Returns the requirement.
Definition: assembler/interface.hh:218
bool boundToRequirement() const
Returns true if the assembler is bound to a finite element requirement.
Definition: assembler/interface.hh:190
const auto & finiteElements() const
Returns the container of finite elements.
Definition: assembler/interface.hh:89
bool boundToDBCOption() const
Returns true if the assembler is bound to an affordance collection.
Definition: assembler/interface.hh:210
size_t constraintsBelow(SizeType i) const
Returns the number of constraints below a given degrees of freedom index.
Definition: assembler/interface.hh:115
DBCOption dBCOption() const
Returns the dirichlet boundary condition enforcement option.
Definition: assembler/interface.hh:240
bool isConstrained(SizeType i) const
Returns true if a given degree of freedom is fixed by a Dirichlet boundary condition.
Definition: assembler/interface.hh:123
size_t reducedSize()
Returns the size of the free degrees of freedom, which are not fixed by a Dirichlet boundary conditio...
Definition: assembler/interface.hh:68
typename FEContainerRaw::value_type::GlobalIndex GlobalIndex
Type of the global index.
Definition: assembler/interface.hh:35
DV DirichletValuesType
Type of the Dirichlet values.
Definition: assembler/interface.hh:40
typename Basis::GridView GridView
Type of the grid view.
Definition: assembler/interface.hh:37
bool bound() const
Returns true if the assembler is bound to a finite element requirement and affordance.
Definition: assembler/interface.hh:177
size_t estimateOfConnectivity() const
Coarse estimate of node connectivity, i.e., this relates to the bandwidth of a sparse matrix....
Definition: assembler/interface.hh:131
AffordanceCollectionType affordanceCollection() const
Returns the affordance.
Definition: assembler/interface.hh:229
size_t size()
Returns the size of nodes, i.e., the number of degrees of freedom.
Definition: assembler/interface.hh:74
const auto & gridView() const
Returns the gridView object.
Definition: assembler/interface.hh:107
auto & finiteElements()
Returns the container of finite elements.
Definition: assembler/interface.hh:95
typename DV::Basis Basis
Type of the basis.
Definition: assembler/interface.hh:36
The ScalarAssembler provides an interface for an assembler that assembles scalar quantities.
Definition: assembler/interface.hh:273
typename FEContainerRaw::value_type::Requirement FERequirement
Definition: assembler/interface.hh:280
ST ScalarType
Definition: assembler/interface.hh:276
std::remove_cvref_t< FEC > FEContainerRaw
Type of the raw finite element container.
Definition: assembler/interface.hh:279
SA ScalarAssemblerType
Definition: assembler/interface.hh:275
ScalarType & scalar()
Calculates the scalar quantity requested by the bound feRequirements and returns a reference.
Definition: assembler/interface.hh:298
DV DirichletValuesType
Definition: assembler/interface.hh:277
ScalarType & scalar(const FERequirement &feRequirements, ScalarAffordance affordance)
Calculates the scalar quantity requested by feRequirements and affordance.
Definition: assembler/interface.hh:289
FEC FEContainer
Definition: assembler/interface.hh:278
The VectorAssembler provides an interface for an assembler that assembles vector quantities.
Definition: assembler/interface.hh:318
DV DirichletValuesType
Definition: assembler/interface.hh:328
VA VectorAssemblerType
Definition: assembler/interface.hh:320
typename FEContainerRaw::value_type::Requirement FERequirement
Type of the finite element requirement.
Definition: assembler/interface.hh:325
VT VectorType
Definition: assembler/interface.hh:321
std::remove_cvref_t< FEC > FEContainerRaw
Type of the raw finite element container.
Definition: assembler/interface.hh:323
VectorType & vector(DBCOption dbcOption)
Calculates the vectorial quantity requested by the bound feRequirements and the affordance....
Definition: assembler/interface.hh:366
VectorType & vector(const FERequirement &feRequirements, VectorAffordance affordance, DBCOption dbcOption=DBCOption::Full)
Calculates the vectorial quantity requested by the feRequirements and the affordance....
Definition: assembler/interface.hh:344
FEC FEContainer
Definition: assembler/interface.hh:329
VectorType & vector()
Calculates the vectorial quantity requested by the bound feRequirements, the affordance and the dBCOp...
Definition: assembler/interface.hh:378
typename FEContainerRaw::value_type::GlobalIndex GlobalIndex
Type of the global index.
Definition: assembler/interface.hh:326
The MatrixAssembler provides an interface for an assembler that assembles matrix quantities.
Definition: assembler/interface.hh:395
MatrixType & matrix()
Calculates the matrix quantity requested by the bound feRequirements, the affordance and the dBCOptio...
Definition: assembler/interface.hh:451
DV DirichletValuesType
Definition: assembler/interface.hh:405
typename FEContainerRaw::value_type::GlobalIndex GlobalIndex
Type of the global index.
Definition: assembler/interface.hh:403
typename FEContainerRaw::value_type::Requirement FERequirement
Type of the finite element requirement.
Definition: assembler/interface.hh:402
std::remove_cvref_t< FEC > FEContainerRaw
Type of the raw finite element container.
Definition: assembler/interface.hh:400
MT MatrixType
Definition: assembler/interface.hh:398
MatrixType & matrix(const FERequirement &feRequirements, MatrixAffordance affordance, DBCOption dbcOption=DBCOption::Full)
Calculates the matrix quantity requested by feRequirements and the affordance. For DBCOption::Full a ...
Definition: assembler/interface.hh:419
FEC FEContainer
Definition: assembler/interface.hh:406
MA MatrixAssemblerType
Definition: assembler/interface.hh:397
MatrixType & matrix(DBCOption dbcOption)
Calculates the matrix quantity requested by the bound feRequirements and the affordance.
Definition: assembler/interface.hh:439
Struct representing a collection of affordances.
Definition: ferequirements.hh:106
Definition of DirichletValues class for handling Dirichlet boundary conditions.