version 0.4
ferequirements.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
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
23namespace Ikarus {
24 // clang-format off
31 noAffordance,
32 mechanicalPotentialEnergy,
33 microMagneticPotentialEnergy
34 );
35
42 noAffordance,
43 forces,
44 microMagneticForces
45 );
46
53 noAffordance,
55 materialstiffness,
56 geometricstiffness,
58 microMagneticHessian,
59 mass
60 );
61
68 noParameter,
69 loadfactor,
70 time
71 );
72
79 noSolution,
80 displacement,
81 velocity,
82 director,
83 magnetizationAndVectorPotential
84 );
85
92 noType,
93 magnetization,
94 gradientNormOfMagnetization,
95 vectorPotential,
96 divergenceOfVectorPotential,
97 BField,
98 HField,
99 cauchyStress,
100 PK2Stress,
101 linearStress,
102 director
103 );
104
105 // clang-format on
106
114 };
115
119 template <typename Type>
121 = std::is_same_v<std::remove_cvref_t<Type>, ScalarAffordances> or std::is_same_v<std::remove_cvref_t<Type>,
122 VectorAffordances> or std::
123 is_same_v<std::remove_cvref_t<Type>, MatrixAffordances> or std::is_same_v<std::remove_cvref_t<Type>,
124 AffordanceCollectionImpl>;
125
127
131
133
134 namespace AffordanceCollections {
137 }
138
139 namespace Impl {
140 template <typename T>
141 struct DeduceRawVectorType {
142 static_assert(!std::is_same<T, T>::value, "You should end up in the provided specializations");
143 };
144
145 template <typename T>
146 struct DeduceRawVectorType<std::reference_wrapper<T>> {
147 using Type = T;
148 };
149
150 template <typename T>
151 struct DeduceRawVectorType<Eigen::Ref<T>> {
152 using Type = Eigen::Ref<T>;
153 };
154 } // namespace Impl
155
168 template <typename SolutionVectorType_ = std::reference_wrapper<Eigen::VectorXd>,
169 typename ParameterType_ = std::reference_wrapper<double>>
171 public:
172 using SolutionVectorType = SolutionVectorType_;
173 using SolutionVectorTypeRaw = typename Impl::DeduceRawVectorType<std::remove_cvref_t<SolutionVectorType_>>::Type;
174 using ParameterType = ParameterType_;
175 using ParameterTypeRaw = typename ParameterType_::type;
176
186 template <FEAffordance Affordance>
187 FERequirements& addAffordance(Affordance&& affordance) {
188 if constexpr (std::is_same_v<Affordance, ScalarAffordances>)
189 affordances.scalarAffordances = affordance;
190 else if constexpr (std::is_same_v<Affordance, VectorAffordances>)
191 affordances.vectorAffordances = affordance;
192 else if constexpr (std::is_same_v<Affordance, MatrixAffordances>)
193 affordances.matrixAffordances = affordance;
194 else if constexpr (std::is_same_v<Affordance, AffordanceCollectionImpl>)
195 affordances = affordance;
196 return *this;
197 }
198
209 parameter.insert_or_assign(key, val);
210 return *this;
211 }
212
223 sols.insert_or_assign(key, sol);
224 return *this;
225 }
226
238 try {
239 if constexpr (std::is_same_v<SolutionVectorType, std::reference_wrapper<Eigen::VectorXd>>)
240 return sols.at(key).get();
241 else
242 return sols.at(key);
243 } catch (std::out_of_range& oor) {
244 DUNE_THROW(Dune::RangeError, std::string("Out of Range error: ") + std::string(oor.what())
245 + " in getGlobalSolution with key" + toString(key));
246 abort();
247 }
248 }
249
261 try {
262 return parameter.at(key).get();
263 } catch (std::out_of_range& oor) {
264 DUNE_THROW(Dune::RangeError, std::string("Out of Range error: ") + std::string(oor.what())
265 + " in getParameter with key" + toString(key));
266 abort();
267 }
268 }
269
279 template <FEAffordance Affordance>
280 bool hasAffordance(Affordance&& affordance) const {
281 if constexpr (std::is_same_v<Affordance, ScalarAffordances>)
282 return affordances.scalarAffordances == affordance;
283 else if constexpr (std::is_same_v<Affordance, VectorAffordances>)
284 return affordances.vectorAffordances == affordance;
285 else if constexpr (std::is_same_v<Affordance, MatrixAffordances>)
286 return affordances.matrixAffordances == affordance;
287 else if constexpr (std::is_same_v<Affordance, AffordanceCollectionImpl>)
288 return affordances == affordance;
289 }
290
291 private:
292 std::map<FESolutions, SolutionVectorType> sols;
293 std::map<FEParameter, ParameterType> parameter;
294 AffordanceCollectionImpl affordances;
295 };
296
310 template <typename SolutionVectorType_ = std::reference_wrapper<Eigen::VectorXd>,
311 typename ParameterType_ = std::reference_wrapper<double>>
312 class [[deprecated(
313 "FErequirements is deprecaded and will be removed after v0.5. Use FERequirements instead.")]] FErequirements
314 : public FERequirements<SolutionVectorType_, ParameterType_> {
316
317 public:
318 FErequirements() = default;
319 FErequirements(Base && base) : Base(std::forward<Base>(base)) {}
320 FErequirements(const Base& base) : Base(base) {}
322 Base::operator=(base);
323 return *this;
324 }
326 Base::operator=(std::forward<Base>(base));
327 return *this;
328 }
329 };
330
341 template <typename ParameterType = double>
343 public:
344 using ResultArray = Eigen::Matrix<ParameterType, Eigen::Dynamic, Eigen::Dynamic, 0, 9, 3>;
345
354 void insertOrAssignResult(ResultType&& resultType, const ResultArray& resultArray) {
355 results.insert_or_assign(resultType, resultArray);
356 }
357
368 ResultArray& getResult(const ResultType& resultType) { return results.at(resultType); }
369
380 if (results.size() != 1)
381 DUNE_THROW(Dune::RangeError, "getSingleResult can only be called when a single result was inserted");
382 else
383 return *(results.begin());
384 }
385
386 private:
387 std::map<ResultType, ResultArray> results;
388 };
389
390 template <typename Type>
391 concept ResultTypeConcept = std::is_same_v<Type, ResultType>;
392
404 template <typename FERequirements = FERequirements<>>
406 public:
410
419 ResultRequirements(FERequirements&& req, std::set<ResultType>&& p_resType)
420 : reqB{req}, resType(std::move(p_resType)) {}
421
429 explicit ResultRequirements(const FERequirements& req) : reqB{req} {}
430
437
446 bool isResultRequested(ResultType&& key) const { return resType.contains(key); }
447
457 template <FEAffordance Affordance>
458 ResultRequirements& addAffordance(Affordance&& affordance) {
459 reqB.addAffordance(std::forward<Affordance>(affordance));
460 return *this;
461 }
462
473 reqB.insertParameter(std::forward<FEParameter>(key), val);
474 return *this;
475 }
476
487 reqB.insertGlobalSolution(std::forward<FESolutions>(key), sol);
488 return *this;
489 }
490
500 template <ResultTypeConcept... ResultTypes>
501 ResultRequirements& addResultRequest(ResultTypes&&... keys) {
502 resType.insert({std::move(keys)...});
503 return *this;
504 }
505
515 return reqB.getGlobalSolution(std::move(key));
516 }
517
526 const ParameterTypeRaw& getParameter(FEParameter&& key) const { return reqB.getParameter(std::move(key)); }
527
535 const FERequirements& getFERequirements() const { return reqB; }
536
546 auto getRequestedResult() const {
547 if (resType.size() == 1)
548 return *(resType.begin());
549 else {
550 DUNE_THROW(Dune::InvalidStateException, "This function can only be called when a single result is requested");
551 }
552 }
553
554 private:
555 std::set<ResultType> resType;
556 FERequirements reqB;
557 };
558} // namespace Ikarus
Implementation of the make enum macro.
VectorAffordances
A strongly typed enum class representing the vector affordance.
Definition: ferequirements.hh:45
MatrixAffordances
A strongly typed enum class representing the matrix affordance.
Definition: ferequirements.hh:60
ScalarAffordances
A strongly typed enum class representing the scalar affordance.
Definition: ferequirements.hh:34
FEParameter
A strongly typed enum class representing the FE parameter.
Definition: ferequirements.hh:71
ResultType
A strongly typed enum class representing the type of the result request.
Definition: ferequirements.hh:103
FESolutions
A strongly typed enum class representing the type of the solutions vectors.
Definition: ferequirements.hh:84
#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: simpleassemblers.hh:21
constexpr std::string toString(ScalarAffordances _e)
Definition: ferequirements.hh:34
constexpr MatrixAffordances stiffness
Definition: ferequirements.hh:128
constexpr ScalarAffordances potentialEnergy
Definition: ferequirements.hh:132
constexpr MatrixAffordances mass
Definition: ferequirements.hh:130
constexpr MatrixAffordances stiffnessdiffBucklingVector
Definition: ferequirements.hh:129
constexpr VectorAffordances forces
Definition: ferequirements.hh:126
constexpr AffordanceCollectionImpl elastoStatics
Definition: ferequirements.hh:136
Definition: truncatedconjugategradient.hh:24
Struct representing a collection of affordances.
Definition: ferequirements.hh:110
VectorAffordances vectorAffordances
Definition: ferequirements.hh:112
ScalarAffordances scalarAffordances
Definition: ferequirements.hh:111
MatrixAffordances matrixAffordances
Definition: ferequirements.hh:113
Class representing the requirements for finite element calculations.
Definition: ferequirements.hh:170
typename Impl::DeduceRawVectorType< std::remove_cvref_t< SolutionVectorType_ > >::Type SolutionVectorTypeRaw
Definition: ferequirements.hh:173
FERequirements & addAffordance(Affordance &&affordance)
Add an affordance to the requirements.
Definition: ferequirements.hh:187
bool hasAffordance(Affordance &&affordance) const
Check if a specific affordance is present in the requirements.
Definition: ferequirements.hh:280
FERequirements & insertParameter(const FEParameter &key, ParameterTypeRaw &val)
Insert a parameter into the requirements.
Definition: ferequirements.hh:208
FERequirements & insertGlobalSolution(const FESolutions &key, SolutionVectorTypeRaw &sol)
Insert a global solution vector into the requirements.
Definition: ferequirements.hh:222
ParameterType_ ParameterType
Definition: ferequirements.hh:174
typename ParameterType_::type ParameterTypeRaw
Definition: ferequirements.hh:175
const SolutionVectorTypeRaw & getGlobalSolution(const FESolutions &key) const
Get the raw global solution vector for a specific type.
Definition: ferequirements.hh:237
const ParameterTypeRaw & getParameter(FEParameter &&key) const
Get the raw parameter value for a specific key.
Definition: ferequirements.hh:260
SolutionVectorType_ SolutionVectorType
Definition: ferequirements.hh:172
Class representing the requirements for finite element calculations.
Definition: ferequirements.hh:314
FErequirements & operator=(Base &&base)
Definition: ferequirements.hh:325
FErequirements(Base &&base)
Definition: ferequirements.hh:319
FErequirements & operator=(const Base &base)
Definition: ferequirements.hh:321
FErequirements(const Base &base)
Definition: ferequirements.hh:320
Class representing a map of result types to result arrays.
Definition: ferequirements.hh:342
auto & getSingleResult()
Get the result array for a single result type.
Definition: ferequirements.hh:379
Eigen::Matrix< ParameterType, Eigen::Dynamic, Eigen::Dynamic, 0, 9, 3 > ResultArray
Definition: ferequirements.hh:344
ResultArray & getResult(const ResultType &resultType)
Get the result array for a specific result type.
Definition: ferequirements.hh:368
void insertOrAssignResult(ResultType &&resultType, const ResultArray &resultArray)
Insert or assign a result to the map.
Definition: ferequirements.hh:354
Class representing the requirements for obtaining specific results.
Definition: ferequirements.hh:405
ResultRequirements & addAffordance(Affordance &&affordance)
Add an affordance to the finite element requirements.
Definition: ferequirements.hh:458
const SolutionVectorTypeRaw & getGlobalSolution(FESolutions &&key) const
Get the global solution for a specific global solution type.
Definition: ferequirements.hh:514
ResultRequirements & addResultRequest(ResultTypes &&... keys)
Add one or more result types to the set of requested results.
Definition: ferequirements.hh:501
auto getRequestedResult() const
Get the requested result type.
Definition: ferequirements.hh:546
ResultRequirements & insertParameter(FEParameter &&key, ParameterTypeRaw &val)
Insert a parameter into the finite element requirements.
Definition: ferequirements.hh:472
const FERequirements & getFERequirements() const
Get the associated finite element requirements.
Definition: ferequirements.hh:535
typename FERequirements::SolutionVectorType SolutionVectorType
Definition: ferequirements.hh:408
ResultRequirements()=default
Default constructor.
ResultRequirements & insertGlobalSolution(FESolutions &&key, SolutionVectorTypeRaw &sol)
Insert a global solution into the finite element requirements.
Definition: ferequirements.hh:486
typename FERequirements::SolutionVectorTypeRaw SolutionVectorTypeRaw
Definition: ferequirements.hh:409
const ParameterTypeRaw & getParameter(FEParameter &&key) const
Get the value of a specific parameter.
Definition: ferequirements.hh:526
bool isResultRequested(ResultType &&key) const
Check if a specific result type is requested.
Definition: ferequirements.hh:446
ResultRequirements(const FERequirements &req)
Constructor with only FERequirements.
Definition: ferequirements.hh:429
typename FERequirements::ParameterTypeRaw ParameterTypeRaw
Definition: ferequirements.hh:407
ResultRequirements(FERequirements &&req, std::set< ResultType > &&p_resType)
Constructor with FERequirements and result types.
Definition: ferequirements.hh:419
Concept to check if a given type is one of the predefined affordance enums or the AffordanceCollectio...
Definition: ferequirements.hh:121
Definition: ferequirements.hh:391