11#include <autodiff/forward/dual/dual.hpp>
12#include <autodiff/forward/dual/eigen.hpp>
26template <
typename FEImpl,
bool forceAutoDiff = false>
32 using Traits =
typename RealFE::Traits;
37 using Mixin = FEImpl::Mixin;
49 typename Traits::template MatrixType<> h) {
62 typename Traits::template VectorType<double> g) {
74 template <
typename MT,
typename BC>
76 return realFE().template subscribeTo<MT>(bc);
91 typename Traits::template VectorType<> g) {
128 template <
typename... Args>
130 :
RealFE{std::forward<Args>(args)...} {}
134 typename Traits::template MatrixType<> h)
const {
137 if constexpr (
requires(Eigen::VectorXd v) {
138 static_cast<const Mixin&
>(std::declval<AutoDiffFE>())
139 .
template calculateMatrixImpl<double>(req, affordance, h, v);
140 } and not forceAutoDiff) {
141 return Mixin::template calculateMatrixImpl<double>(req, affordance, h);
142 }
else if constexpr (
requires(Eigen::VectorXdual v) {
143 static_cast<const Mixin&
>(std::declval<AutoDiffFE>())
144 .
template calculateVectorImpl<autodiff::dual>(
146 std::declval<typename Traits::template VectorType<autodiff::dual>>(), v);
149 Eigen::VectorXdual dx(this->localView().size());
150 Eigen::VectorXdual g(this->localView().size());
152 auto f = [
this, &req, affordance, &g](
auto& x) ->
auto& {
156 Eigen::VectorXdual& gref =
const_cast<Eigen::VectorXdual&
>(g);
158 Mixin::template calculateVectorImpl<autodiff::dual>(req,
vectorAffordance(affordance), gref, x);
161 jacobian(f, autodiff::wrt(dx), at(dx), g, h);
162 }
else if constexpr (
requires(
typename Traits::template VectorType<autodiff::dual2nd> v) {
163 static_cast<const Mixin&
>(std::declval<AutoDiffFE>())
164 .
template calculateScalarImpl<autodiff::dual2nd>(req,
scalarAffordance(affordance), v);
167 Eigen::VectorXdual2nd dx(this->localView().size());
171 auto f = [
this, &req, affordance](
auto& x) {
172 return Mixin::template calculateScalarImpl<autodiff::dual2nd>(req,
scalarAffordance(affordance), x);
174 hessian(f, autodiff::wrt(dx), at(dx), e, g, h);
176 static_assert(Dune::AlwaysFalse<AutoDiffFE>::value,
177 "Appropriate calculateScalarImpl or calculateVectorImpl functions are not implemented for the "
182 typename Traits::template VectorType<> g)
const {
184 if constexpr (
requires {
185 static_cast<const Mixin&
>(std::declval<AutoDiffFE>())
186 .
template calculateVectorImpl<double>(
187 req, affordance, std::declval<
typename Traits::template VectorType<double>>(),
188 std::declval<const Eigen::VectorXd&>());
189 } and not forceAutoDiff) {
190 return Mixin::template calculateVectorImpl<double>(req, affordance, g);
191 }
else if constexpr (
requires {
192 static_cast<const Mixin&
>(std::declval<AutoDiffFE>())
193 .
template calculateScalarImpl<autodiff::dual>(req,
scalarAffordance(affordance),
194 std::declval<const Eigen::VectorXdual&>());
198 Eigen::VectorXdual dx(this->localView().size());
201 auto f = [
this, &req, affordance](
auto& x) {
202 return Mixin::template calculateScalarImpl<autodiff::dual>(req,
scalarAffordance(affordance), x);
204 gradient(f, autodiff::wrt(dx), at(dx), e, g);
206 static_assert(Dune::AlwaysFalse<AutoDiffFE>::value,
207 "Appropriate calculateScalarImpl function is not implemented for the "
213 if constexpr (
requires {
214 static_cast<const Mixin&
>(std::declval<AutoDiffFE>())
215 .
template calculateScalarImpl<double>(par, affordance);
218 return Mixin::template calculateScalarImpl<double>(par, affordance);
220 static_assert(Dune::AlwaysFalse<AutoDiffFE>::value,
221 "Appropriate calculateScalar and calculateScalarImpl functions are not implemented for the "
227 typename Traits::template MatrixType<> h,
typename Traits::template VectorType<> g)
const {
229 Eigen::VectorXdual2nd dx(this->localView().size());
231 auto f = [&](
auto& x) {
return Mixin::calculateScalarImpl(req,
scalarAffordance(affordanceV), x); };
232 hessian(f, autodiff::wrt(dx), at(dx), g, h);
Contains stl-like type traits.
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
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
AutoDiffFE class, an automatic differentiation wrapper for finite elements.
Definition: autodifffe.hh:28
AutoDiffFE(Args &&... args)
Constructor for the AutoDiffFE class. Forward the construction to the underlying element.
Definition: autodifffe.hh:129
typename RealFE::Traits Traits
Type traits for local view.
Definition: autodifffe.hh:32
friend void calculateVector(const AutoDiffFE &self, const Requirement &par, VectorAffordance affordance, typename Traits::template VectorType< double > g)
Calculate the vector associated with the finite element.
Definition: autodifffe.hh:61
friend auto calculateScalar(const AutoDiffFE &self, const Requirement &par, ScalarAffordance affordance)
Calculate the scalar value associated with the finite element.
Definition: autodifffe.hh:103
const RealFE & realFE() const
Get the reference to the base finite element.
Definition: autodifffe.hh:112
RealFE & realFE()
Get the reference to the base finite element.
Definition: autodifffe.hh:119
friend void calculateMatrix(const AutoDiffFE &self, const Requirement &par, const MatrixAffordance &affordance, typename Traits::template MatrixType<> h)
Calculate the matrix associated with the finite element.
Definition: autodifffe.hh:48
friend void calculateLocalSystem(const AutoDiffFE &self, const Requirement &par, const MatrixAffordance &affordanceM, VectorAffordance affordanceV, typename Traits::template MatrixType<> h, typename Traits::template VectorType<> g)
Calculate the local system associated with the finite element.
Definition: autodifffe.hh:89
FEImpl RealFE
Type of the base finite element.
Definition: autodifffe.hh:30
typename RealFE::BasisHandler BasisHandler
Type of the basis handler.
Definition: autodifffe.hh:31
typename Traits::LocalView LocalView
Type of the local view.
Definition: autodifffe.hh:33
typename Traits::Element Element
Type of the element.
Definition: autodifffe.hh:34
typename RealFE::Requirement Requirement
Type of the Finite Element Requirements.
Definition: autodifffe.hh:35
auto subscribeTo(BC &bc)
Subscribes the elements to listen to functions provided from the skills emitted by the given broadcas...
Definition: autodifffe.hh:75