version 0.4.1
ferequirements.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
10#pragma once
11
12#include <iosfwd>
13#include <map>
14#include <set>
15#include <vector>
16
17#include <dune/common/exceptions.hh>
18
19#include <Eigen/Core>
20
22#include <ikarus/utils/basis.hh>
24
25namespace Ikarus {
26// clang-format off
27
33// cppcheck-suppress MAKE_ENUM
35 noAffordance,
36 mechanicalPotentialEnergy,
37 microMagneticPotentialEnergy
38 );
39
46 noAffordance,
47 forces,
48 microMagneticForces
49 );
50
57 noAffordance,
59 materialstiffness,
60 geometricstiffness,
62 microMagneticHessian,
63 mass
64 );
65
72 noParameter,
73 loadfactor,
74 time
75 );
76
83 noSolution,
84 displacement,
85 velocity,
86 director,
87 magnetizationAndVectorPotential
88 );
89
90// clang-format on
91
95template <typename T>
96concept FEAffordance = std::is_same_v<std::remove_cvref_t<T>, ScalarAffordance> or
97 std::is_same_v<std::remove_cvref_t<T>, VectorAffordance> or
98 std::is_same_v<std::remove_cvref_t<T>, MatrixAffordance>;
99
103template <FEAffordance... Affordances>
104requires(sizeof...(Affordances) <= 3)
105struct AffordanceCollection : public std::tuple<Affordances...>
106{
107 using Base = std::tuple<Affordances...>;
108
110 constexpr AffordanceCollection(Affordances... affordances)
111 : Base(affordances...) {}
112
113 static constexpr bool hasScalarAffordance = traits::hasType<ScalarAffordance, std::tuple<Affordances...>>::value;
114 static constexpr bool hasVectorAffordance = traits::hasType<VectorAffordance, std::tuple<Affordances...>>::value;
115 static constexpr bool hasMatrixAffordance = traits::hasType<MatrixAffordance, std::tuple<Affordances...>>::value;
125 template <FEAffordance Affordance>
126 constexpr bool hasAffordance(Affordance&& affordances) const {
127 using AffordanceRaw = std::remove_cvref_t<Affordance>;
128 if constexpr (std::is_same_v<AffordanceRaw, AffordanceCollection>)
129 return affordances == *this;
130 else {
131 if constexpr (std::is_same_v<AffordanceRaw, ScalarAffordance>) {
132 if constexpr (hasScalarAffordance)
133 return affordances == std::get<ScalarAffordance>(*this);
134 else
135 return false;
136 }
137 if constexpr (std::is_same_v<AffordanceRaw, VectorAffordance>) {
138 if constexpr (hasVectorAffordance)
139 return affordances == std::get<VectorAffordance>(*this);
140 else
141 return false;
142 }
143 if constexpr (std::is_same_v<AffordanceRaw, MatrixAffordance>) {
144 if constexpr (hasMatrixAffordance)
145 return affordances == std::get<MatrixAffordance>(*this);
146 else
147 return false;
148 }
149 }
150 return false;
151 }
152
153 auto scalarAffordance() const
154 requires hasScalarAffordance
155 {
156 return std::get<ScalarAffordance>(*this);
157 }
158 auto vectorAffordance() const
159 requires hasVectorAffordance
160 {
161 return std::get<VectorAffordance>(*this);
162 }
163 auto matrixAffordance() const
164 requires hasMatrixAffordance
165 {
166 return std::get<MatrixAffordance>(*this);
167 }
168};
169
171
176
178 if (affordanceM == MatrixAffordance::stiffness)
180 else if (affordanceM == MatrixAffordance::microMagneticHessian)
182 else
184}
185
187 if (affordanceM == MatrixAffordance::stiffness)
189 else if (affordanceM == MatrixAffordance::microMagneticHessian)
191 else
193}
194
196 if (affordanceV == VectorAffordance::forces)
198 else if (affordanceV == VectorAffordance::microMagneticForces)
200 else
202}
203
204namespace AffordanceCollections {
207} // namespace AffordanceCollections
208
221template <FESolutions sol, FEParameter para, typename SV = Eigen::VectorXd, typename PM = double>
223{
224public:
226 using ParameterType = PM;
227
231 FERequirements() = default;
232
239 template <typename SV2 = SV, typename PM2 = PM>
240 FERequirements(SV2&& solVec, PM2&& parameter)
241 : sol_(std::make_unique<SV>(std::forward<SV2>(solVec))),
242 parameter_(std::make_unique<PM>(std::forward<PM2>(parameter))) {}
243
249 template <typename PB>
251 : sol_(std::make_unique<SV>(SV::Zero(basis.flat().size()))),
252 parameter_(std::make_unique<PM>(0.0)) {}
253
262 : sol_(other.sol_ ? std::make_unique<SV>(*other.sol_) : nullptr),
263 parameter_(other.parameter_ ? std::make_unique<PM>(*other.parameter_) : nullptr) {}
264
274 if (this != &other) {
275 sol_ = other.sol_ ? std::make_unique<SV>(*other.sol_) : nullptr;
276 parameter_ = other.parameter_ ? std::make_unique<PM>(*other.parameter_) : nullptr;
277 }
278 return *this;
279 }
280
290 parameter_ = std::make_unique<PM>(val);
291 return *this;
292 }
293
302 template <typename SV2 = SolutionVectorType>
304 sol_ = std::make_unique<SV>(std::forward<SV2>(solVec));
305 return *this;
306 }
307
314 if (!sol_)
315 DUNE_THROW(Dune::InvalidStateException, "Solution vector is not initialized.");
316 return *sol_;
317 }
318
325 if (!sol_)
326 DUNE_THROW(Dune::InvalidStateException, "Solution vector is not initialized.");
327
328 return *sol_;
329 }
330
336 const PM& parameter() const {
337 if (!parameter_)
338 DUNE_THROW(Dune::InvalidStateException, "Parameter is not initialized.");
339
340 return *parameter_;
341 }
342
348 PM& parameter() {
349 if (!parameter_)
350 DUNE_THROW(Dune::InvalidStateException, "Parameter is not initialized.");
351
352 return *parameter_;
353 }
354
360 bool populated() const { return sol_ and parameter_; }
361
369 template <typename T>
370 SV& operator+=(const T& rhs) {
371 if (!sol_)
372 DUNE_THROW(Dune::InvalidStateException, "Solution vector is not initialized.");
373 *sol_ += rhs;
374 return *sol_;
375 }
376
377private:
378 std::unique_ptr<SV> sol_;
379 std::unique_ptr<PM> parameter_;
380};
381
382} // namespace Ikarus
Implementation of the make enum macro.
Definitions of ResultTypes used for finite element results.
FEParameter
A strongly typed enum class representing the FE parameter.
Definition: ferequirements.hh:75
FESolutions
A strongly typed enum class representing the type of the solutions vectors.
Definition: ferequirements.hh:88
#define MAKE_ENUM(type,...)
Macro to create an enumeration with a string conversion function.The macro creates an enum class with...
Definition: makeenum.hh:40
Definition: assemblermanipulatorbuildingblocks.hh:22
constexpr MatrixAffordance stiffnessdiffBucklingVector
Definition: ferequirements.hh:173
auto vectorAffordance(MatrixAffordance affordanceM)
Definition: ferequirements.hh:177
MatrixAffordance
A strongly typed enum class representing the matrix affordance.
Definition: ferequirements.hh:64
constexpr MatrixAffordance stiffness
Definition: ferequirements.hh:172
constexpr VectorAffordance forces
Definition: ferequirements.hh:170
constexpr ScalarAffordance potentialEnergy
Definition: ferequirements.hh:175
auto scalarAffordance(MatrixAffordance affordanceM)
Definition: ferequirements.hh:186
VectorAffordance
A strongly typed enum class representing the vector affordance.
Definition: ferequirements.hh:49
constexpr MatrixAffordance mass
Definition: ferequirements.hh:174
ScalarAffordance
A strongly typed enum class representing the scalar affordance.
Definition: ferequirements.hh:38
constexpr AffordanceCollection elastoStatics(ScalarAffordance::mechanicalPotentialEnergy, VectorAffordance::forces, MatrixAffordance::stiffness)
def basis(gv, tree)
Definition: basis.py:10
Struct representing a collection of affordances.
Definition: ferequirements.hh:106
std::tuple< Affordances... > Base
Definition: ferequirements.hh:107
auto vectorAffordance() const
Definition: ferequirements.hh:158
constexpr AffordanceCollection(Affordances... affordances)
Definition: ferequirements.hh:110
auto scalarAffordance() const
Definition: ferequirements.hh:153
constexpr bool hasAffordance(Affordance &&affordances) const
Check if a specific affordance is present in the requirements.
Definition: ferequirements.hh:126
auto matrixAffordance() const
Definition: ferequirements.hh:163
Class representing the requirements for finite element calculations.
Definition: ferequirements.hh:223
FERequirements()=default
Default constructor.
bool populated() const
Tells if the class contains all needed values.
Definition: ferequirements.hh:360
FERequirements & operator=(const FERequirements &other)
Copy assignment operator.
Definition: ferequirements.hh:273
FERequirements(const FERequirements &other)
Copy constructor.
Definition: ferequirements.hh:261
SV SolutionVectorType
Type of the solution vector.
Definition: ferequirements.hh:225
FERequirements(SV2 &&solVec, PM2 &&parameter)
Constructor initializing the solution vector and parameter.
Definition: ferequirements.hh:240
PM ParameterType
Type of the parameter.
Definition: ferequirements.hh:226
SV & globalSolution()
Get the global solution vector.
Definition: ferequirements.hh:324
FERequirements & insertGlobalSolution(SV2 &&solVec)
Insert a global solution vector into the requirements.
Definition: ferequirements.hh:303
const SolutionVectorType & globalSolution() const
Get the global solution vector.
Definition: ferequirements.hh:313
FERequirements & insertParameter(const PM &val)
Insert a parameter into the requirements.
Definition: ferequirements.hh:289
const PM & parameter() const
Get the parameter value.
Definition: ferequirements.hh:336
PM & parameter()
Get the parameter value.
Definition: ferequirements.hh:348
FERequirements(const Ikarus::BasisHandler< PB > &basis)
Constructor from a basis.
Definition: ferequirements.hh:250
SV & operator+=(const T &rhs)
Enables the usage of the class as a solution vector.
Definition: ferequirements.hh:370
Wrapper class for a hierarchical basis constructed from a pre-basis.
Definition: utils/basis.hh:30
Type trait to check if a specified type is present in a tuple.
Definition: traits.hh:88
Concept to check if a given type is one of the predefined affordance enums or the AffordanceCollectio...
Definition: ferequirements.hh:96
Wrapper around Dune-functions global basis.