version 0.4.1
assembler/interface.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 <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 auto& finiteElements() const { return Dune::resolveRef(feContainer_); }
90
95 const auto& dirichletValues() const { return Dune::resolveRef(dirichletValues_); }
96
101 const auto& gridView() const { return Dune::resolveRef(dirichletValues_.basis().gridView()); }
102
109 [[nodiscard]] size_t constraintsBelow(SizeType i) const { return constraintsBelow_[i]; }
110
117 [[nodiscard]] bool isConstrained(SizeType i) const { return dirichletValues_.isConstrained(i); }
118
125 [[nodiscard]] size_t estimateOfConnectivity() const {
126 return dirichletValues_.basis().gridView().size(GridView::dimension) * 8;
127 }
128
136 DBCOption dbcOption = DBCOption::Full) {
137 req_ = std::make_optional<FERequirement>(req);
138 affordances_ = std::make_optional<AffordanceCollectionType>(affordanceCollection);
139 dBCOption_ = std::make_optional<DBCOption>(dbcOption);
140 }
141
147 void bind(const FERequirement& req) { req_ = std::make_optional<FERequirement>(req); }
148
155 affordances_ = std::make_optional<AffordanceCollectionType>(affordanceCollection);
156 }
157
163 void bind(DBCOption dbcOption) { dBCOption_ = std::make_optional<DBCOption>(dbcOption); }
164
170 [[nodiscard]]
171 bool bound() const {
173 return true;
174 else
175 DUNE_THROW(Dune::InvalidStateException, "The assembler is not bound to a requirement, affordance or dBCOption.");
176 }
177
183 [[nodiscard]]
184 bool boundToRequirement() const {
185 return req_.has_value();
186 }
187
193 [[nodiscard]]
195 return affordances_.has_value();
196 }
197
203 [[nodiscard]]
204 bool boundToDBCOption() const {
205 return dBCOption_.has_value();
206 }
207
213 if (req_.has_value())
214 return req_.value();
215 else
216 DUNE_THROW(Dune::InvalidStateException, "The requirement can only be obtained after binding");
217 }
218
224 if (affordances_.has_value())
225 return affordances_.value();
226 else
227 DUNE_THROW(Dune::InvalidStateException, "The affordance can only be obtained after binding");
228 }
229
235 if (dBCOption_.has_value())
236 return dBCOption_.value();
237 else
238 DUNE_THROW(Dune::InvalidStateException, "The dBCOption can only be obtained after binding");
239 }
240
241private:
242 FEContainer feContainer_;
243 DirichletValuesType dirichletValues_;
244 std::optional<FERequirement> req_;
245 std::optional<AffordanceCollectionType> affordances_;
246 std::vector<size_t> constraintsBelow_{};
247 size_t fixedDofs_{};
248 std::optional<DBCOption> dBCOption_;
249};
250
251#ifndef DOXYGEN
253template <class FEV, class DirichletValuesType>
254FlatAssemblerBase(const FEV& fes,
255 const DirichletValuesType& dirichletValues) -> FlatAssemblerBase<FEV, DirichletValuesType>;
256#endif
257
265template <typename SA, typename FEC, typename DV, typename ST>
267{
268public:
270 using ScalarType = ST;
272 using FEContainer = FEC;
273 using FEContainerRaw = std::remove_cvref_t<FEC>;
274 using FERequirement = typename FEContainerRaw::value_type::Requirement;
275
283 const ScalarType& scalar(const FERequirement& feRequirements, ScalarAffordance affordance) {
284 return underlying().getScalarImpl(feRequirements, affordance);
285 }
286
293 return underlying().getScalarImpl(underlying().requirement(),
294 underlying().affordanceCollection().scalarAffordance());
295 }
296
297private:
298 //> CRTP
299 const auto& underlying() const { return static_cast<const ScalarAssemblerType&>(*this); }
300 auto& underlying() { return static_cast<ScalarAssemblerType&>(*this); }
301};
302
310template <typename VA, typename FEC, typename DV, typename VT>
312{
313public:
315 using VectorType = VT;
316
317 using FEContainerRaw = std::remove_cvref_t<FEC>;
318 using FERequirement = typename FEContainerRaw::value_type::Requirement;
320 using GlobalIndex = typename FEContainerRaw::value_type::GlobalIndex;
321
323 using FEContainer = FEC;
324
338 const VectorType& vector(const FERequirement& feRequirements, VectorAffordance affordance,
339 DBCOption dbcOption = DBCOption::Full) {
340 if (dbcOption == DBCOption::Raw) {
341 return underlying().getRawVectorImpl(feRequirements, affordance);
342 } else if (dbcOption == DBCOption::Reduced) {
343 return underlying().getReducedVectorImpl(feRequirements, affordance);
344 } else if (dbcOption == DBCOption::Full) {
345 return underlying().getVectorImpl(feRequirements, affordance);
346 }
347 __builtin_unreachable();
348 }
349
360 const VectorType& vector(DBCOption dbcOption) {
361 return vector(underlying().requirement(), underlying().affordanceCollection().vectorAffordance(), dbcOption);
362 }
363
372 const VectorType& vector() { return vector(underlying().dBCOption()); }
373
374private:
375 //> CRTP
376 const auto& underlying() const { return static_cast<const VectorAssemblerType&>(*this); }
377 auto& underlying() { return static_cast<VectorAssemblerType&>(*this); }
378};
379
387template <typename MA, typename FEC, typename DV, typename MT>
389{
390public:
392 using MatrixType = MT;
393
394 using FEContainerRaw = std::remove_cvref_t<FEC>;
395 using FERequirement = typename FEContainerRaw::value_type::Requirement;
397 using GlobalIndex = typename FEContainerRaw::value_type::GlobalIndex;
398
400 using FEContainer = FEC;
401
413 const MatrixType& matrix(const FERequirement& feRequirements, MatrixAffordance affordance,
414 DBCOption dbcOption = DBCOption::Full) {
415 if (dbcOption == DBCOption::Raw) {
416 return underlying().getRawMatrixImpl(feRequirements, affordance);
417 } else if (dbcOption == DBCOption::Reduced) {
418 return underlying().getReducedMatrixImpl(feRequirements, affordance);
419 } else if (dbcOption == DBCOption::Full) {
420 return underlying().getMatrixImpl(feRequirements, affordance);
421 }
422 __builtin_unreachable();
423 }
424
433 const MatrixType& matrix(DBCOption dbcOption) {
434 return matrix(underlying().requirement(), underlying().affordanceCollection().matrixAffordance(), dbcOption);
435 }
436
445 const MatrixType& matrix() { return matrix(underlying().dBCOption()); }
446
447private:
448 //> CRTP
449 const auto& underlying() const { return static_cast<const MatrixAssemblerType&>(*this); }
450 auto& underlying() { return static_cast<MatrixAssemblerType&>(*this); }
451};
452} // namespace Ikarus
Definition of the LinearElastic class for finite element mechanics computations.
STL namespace.
Definition: assemblermanipulatorbuildingblocks.hh:22
auto vectorAffordance(MatrixAffordance affordanceM)
Definition: ferequirements.hh:176
MatrixAffordance
A strongly typed enum class representing the matrix affordance.
Definition: ferequirements.hh:63
auto scalarAffordance(MatrixAffordance affordanceM)
Definition: ferequirements.hh:185
DBCOption
Definition: dirichletbcenforcement.hh:7
VectorAffordance
A strongly typed enum class representing the vector affordance.
Definition: ferequirements.hh:48
ScalarAffordance
A strongly typed enum class representing the scalar affordance.
Definition: ferequirements.hh:37
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:154
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:135
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:163
void bind(const FERequirement &req)
Binds the assembler to a finite element requirement.
Definition: assembler/interface.hh:147
bool boundToAffordanceCollection() const
Returns true if the assembler is bound to an affordance collection.
Definition: assembler/interface.hh:194
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:95
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:212
bool boundToRequirement() const
Returns true if the assembler is bound to a finite element requirement.
Definition: assembler/interface.hh:184
bool boundToDBCOption() const
Returns true if the assembler is bound to an affordance collection.
Definition: assembler/interface.hh:204
size_t constraintsBelow(SizeType i) const
Returns the number of constraints below a given degrees of freedom index.
Definition: assembler/interface.hh:109
DBCOption dBCOption() const
Returns the dirichlet boundary condition enforcement option.
Definition: assembler/interface.hh:234
bool isConstrained(SizeType i) const
Returns true if a given degree of freedom is fixed by a Dirichlet boundary condition.
Definition: assembler/interface.hh:117
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
auto & finiteElements() const
Returns the container of finite elements.
Definition: assembler/interface.hh:89
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:171
size_t estimateOfConnectivity() const
Coarse estimate of node connectivity, i.e., this relates to the bandwidth of a sparse matrix....
Definition: assembler/interface.hh:125
AffordanceCollectionType affordanceCollection() const
Returns the affordance.
Definition: assembler/interface.hh:223
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:101
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:267
const ScalarType & scalar()
Calculates the scalar quantity requested by the bound feRequirements and returns a reference.
Definition: assembler/interface.hh:292
typename FEContainerRaw::value_type::Requirement FERequirement
Definition: assembler/interface.hh:274
ST ScalarType
Definition: assembler/interface.hh:270
std::remove_cvref_t< FEC > FEContainerRaw
Type of the raw finite element container.
Definition: assembler/interface.hh:273
SA ScalarAssemblerType
Definition: assembler/interface.hh:269
DV DirichletValuesType
Definition: assembler/interface.hh:271
FEC FEContainer
Definition: assembler/interface.hh:272
const ScalarType & scalar(const FERequirement &feRequirements, ScalarAffordance affordance)
Calculates the scalar quantity requested by feRequirements and affordance.
Definition: assembler/interface.hh:283
The VectorAssembler provides an interface for an assembler that assembles vector quantities.
Definition: assembler/interface.hh:312
const VectorType & vector(DBCOption dbcOption)
Calculates the vectorial quantity requested by the bound feRequirements and the affordance....
Definition: assembler/interface.hh:360
DV DirichletValuesType
Definition: assembler/interface.hh:322
VA VectorAssemblerType
Definition: assembler/interface.hh:314
typename FEContainerRaw::value_type::Requirement FERequirement
Type of the finite element requirement.
Definition: assembler/interface.hh:319
VT VectorType
Definition: assembler/interface.hh:315
std::remove_cvref_t< FEC > FEContainerRaw
Type of the raw finite element container.
Definition: assembler/interface.hh:317
const 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:338
FEC FEContainer
Definition: assembler/interface.hh:323
const VectorType & vector()
Calculates the vectorial quantity requested by the bound feRequirements, the affordance and the dBCOp...
Definition: assembler/interface.hh:372
typename FEContainerRaw::value_type::GlobalIndex GlobalIndex
Type of the global index.
Definition: assembler/interface.hh:320
The MatrixAssembler provides an interface for an assembler that assembles matrix quantities.
Definition: assembler/interface.hh:389
DV DirichletValuesType
Definition: assembler/interface.hh:399
const MatrixType & matrix(DBCOption dbcOption)
Calculates the matrix quantity requested by the bound feRequirements and the affordance.
Definition: assembler/interface.hh:433
typename FEContainerRaw::value_type::GlobalIndex GlobalIndex
Type of the global index.
Definition: assembler/interface.hh:397
typename FEContainerRaw::value_type::Requirement FERequirement
Type of the finite element requirement.
Definition: assembler/interface.hh:396
const 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:413
std::remove_cvref_t< FEC > FEContainerRaw
Type of the raw finite element container.
Definition: assembler/interface.hh:394
MT MatrixType
Definition: assembler/interface.hh:392
FEC FEContainer
Definition: assembler/interface.hh:400
MA MatrixAssemblerType
Definition: assembler/interface.hh:391
const MatrixType & matrix()
Calculates the matrix quantity requested by the bound feRequirements, the affordance and the dBCOptio...
Definition: assembler/interface.hh:445
Struct representing a collection of affordances.
Definition: ferequirements.hh:105
Definition of DirichletValues class for handling Dirichlet boundary conditions.