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) {
78 typename Traits::template VectorType<> g) {
108 template <
typename... Args>
114 typename Traits::template MatrixType<> h)
const {
117 if constexpr (
requires(Eigen::VectorXd v) {
118 static_cast<const Mixin&
>(std::declval<AutoDiffFE>())
119 .
template calculateMatrixImpl<double>(req, affordance, h, v);
120 } and not forceAutoDiff) {
121 return Mixin::template calculateMatrixImpl<double>(req, affordance, h);
122 }
else if constexpr (
requires(Eigen::VectorXdual v) {
123 static_cast<const Mixin&
>(std::declval<AutoDiffFE>())
124 .
template calculateVectorImpl<autodiff::dual>(
126 std::declval<typename Traits::template VectorType<autodiff::dual>>(), v);
129 Eigen::VectorXdual dx(this->localView().size());
130 Eigen::VectorXdual g(this->localView().size());
132 auto f = [
this, &req, affordance, &g](
auto& x) ->
auto& {
136 Eigen::VectorXdual& gref =
const_cast<Eigen::VectorXdual&
>(g);
138 Mixin::template calculateVectorImpl<autodiff::dual>(req,
vectorAffordance(affordance), gref, x);
141 jacobian(f, autodiff::wrt(dx), at(dx), g, h);
142 }
else if constexpr (
requires(
typename Traits::template VectorType<autodiff::dual2nd> v) {
143 static_cast<const Mixin&
>(std::declval<AutoDiffFE>())
144 .
template calculateScalarImpl<autodiff::dual2nd>(req,
scalarAffordance(affordance), v);
147 Eigen::VectorXdual2nd dx(this->localView().size());
151 auto f = [
this, &req, affordance](
auto& x) {
152 return Mixin::template calculateScalarImpl<autodiff::dual2nd>(req,
scalarAffordance(affordance), x);
154 hessian(f, autodiff::wrt(dx), at(dx), e, g, h);
156 static_assert(Dune::AlwaysFalse<AutoDiffFE>::value,
157 "Appropriate calculateScalarImpl or calculateVectorImpl functions are not implemented for the "
162 typename Traits::template VectorType<> g)
const {
164 if constexpr (
requires {
165 static_cast<const Mixin&
>(std::declval<AutoDiffFE>())
166 .
template calculateVectorImpl<double>(
167 req, affordance, std::declval<
typename Traits::template VectorType<double>>(),
168 std::declval<const Eigen::VectorXd&>());
169 } and not forceAutoDiff) {
170 return Mixin::template calculateVectorImpl<double>(req, affordance, g);
171 }
else if constexpr (
requires {
172 static_cast<const Mixin&
>(std::declval<AutoDiffFE>())
173 .
template calculateScalarImpl<autodiff::dual>(req,
scalarAffordance(affordance),
174 std::declval<const Eigen::VectorXdual&>());
178 Eigen::VectorXdual dx(this->localView().size());
181 auto f = [
this, &req, affordance](
auto& x) {
182 return Mixin::template calculateScalarImpl<autodiff::dual>(req,
scalarAffordance(affordance), x);
184 gradient(f, autodiff::wrt(dx), at(dx), e, g);
186 static_assert(Dune::AlwaysFalse<AutoDiffFE>::value,
187 "Appropriate calculateScalarImpl function is not implemented for the "
193 if constexpr (
requires {
194 static_cast<const Mixin&
>(std::declval<AutoDiffFE>())
195 .
template calculateScalarImpl<double>(par, affordance);
198 return Mixin::template calculateScalarImpl<double>(par, affordance);
200 static_assert(Dune::AlwaysFalse<AutoDiffFE>::value,
201 "Appropriate calculateScalar and calculateScalarImpl functions are not implemented for the "
207 typename Traits::template MatrixType<> h,
typename Traits::template VectorType<> g)
const {
209 Eigen::VectorXdual2nd dx(this->localView().size());
211 auto f = [&](
auto& x) {
return Mixin::calculateScalarImpl(req,
scalarAffordance(affordanceV), x); };
212 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:176
MatrixAffordance
A strongly typed enum class representing the matrix affordance.
Definition: ferequirements.hh:63
auto scalarAffordance(MatrixAffordance affordanceM)
Definition: ferequirements.hh:185
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
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:109
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:90
const RealFE & realFE() const
Get the reference to the base finite element.
Definition: autodifffe.hh:99
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:76
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