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;
48 typename Traits::template MatrixType<> h) {
60 typename Traits::template VectorType<double> g) {
73 typename Traits::template MatrixType<> h,
typename Traits::template VectorType<> g) {
102 template <
typename... Args>
104 :
RealFE{std::forward<Args>(args)...} {}
110 if constexpr (
requires(Eigen::VectorXd v) {
111 static_cast<const Mixin&
>(std::declval<AutoDiffFE>())
112 .
template calculateMatrixImpl<double>(req, h, v);
113 } and not forceAutoDiff) {
114 return Mixin::template calculateMatrixImpl<double>(req, h);
115 }
else if constexpr (
requires(Eigen::VectorXdual v) {
116 static_cast<const Mixin&
>(std::declval<AutoDiffFE>())
117 .
template calculateVectorImpl<autodiff::dual>(
118 req, std::declval<
typename Traits::template VectorType<autodiff::dual>>(), v);
121 Eigen::VectorXdual dx(this->localView().size());
122 Eigen::VectorXdual g(this->localView().size());
124 auto f = [
this, &req, &g](
auto& x) ->
auto& {
128 Eigen::VectorXdual& gref =
const_cast<Eigen::VectorXdual&
>(g);
130 Mixin::template calculateVectorImpl<autodiff::dual>(req, gref, x);
133 jacobian(f, autodiff::wrt(dx), at(dx), g, h);
134 }
else if constexpr (
requires(
typename Traits::template VectorType<autodiff::dual2nd> v) {
135 static_cast<const Mixin&
>(std::declval<AutoDiffFE>())
136 .
template calculateScalarImpl<autodiff::dual2nd>(req, v);
139 Eigen::VectorXdual2nd dx(this->localView().size());
143 auto f = [
this, &req](
auto& x) {
return Mixin::template calculateScalarImpl<autodiff::dual2nd>(req, x); };
144 hessian(f, autodiff::wrt(dx), at(dx), e, g, h);
146 static_assert(Dune::AlwaysFalse<AutoDiffFE>::value,
147 "Appropriate calculateScalarImpl or calculateVectorImpl functions are not implemented for the "
153 if constexpr (
requires {
154 static_cast<const Mixin&
>(std::declval<AutoDiffFE>())
155 .
template calculateVectorImpl<double>(
156 req, std::declval<
typename Traits::template VectorType<double>>(),
157 std::declval<const Eigen::VectorXd&>());
158 } and not forceAutoDiff) {
159 return Mixin::template calculateVectorImpl<double>(req, g);
160 }
else if constexpr (
requires {
161 static_cast<const Mixin&
>(std::declval<AutoDiffFE>())
162 .
template calculateScalarImpl<autodiff::dual>(req,
163 std::declval<const Eigen::VectorXdual&>());
167 Eigen::VectorXdual dx(this->localView().size());
170 auto f = [
this, &req](
auto& x) {
return Mixin::template calculateScalarImpl<autodiff::dual>(req, x); };
171 gradient(f, autodiff::wrt(dx), at(dx), e, g);
173 static_assert(Dune::AlwaysFalse<AutoDiffFE>::value,
174 "Appropriate calculateScalarImpl function is not implemented for the "
180 if constexpr (
requires {
181 static_cast<const Mixin&
>(std::declval<AutoDiffFE>()).
template calculateScalarImpl<double>(par);
183 Mixin::template calculateScalarImpl<double>(par);
185 return Mixin::template calculateScalarImpl<double>(par);
187 static_assert(Dune::AlwaysFalse<AutoDiffFE>::value,
188 "Appropriate calculateScalar and calculateScalarImpl functions are not implemented for the "
194 typename Traits::template VectorType<> g)
const {
195 Eigen::VectorXdual2nd dx(this->localView().size());
197 auto f = [&](
auto& x) {
return Mixin::calculateScalarImpl(req, x); };
198 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: simpleassemblers.hh:22
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:103
typename Traits::FERequirementType FERequirementType
Type of the Finite Element Requirements.
Definition: autodifffe.hh:35
typename RealFE::Traits Traits
Type traits for local view.
Definition: autodifffe.hh:32
friend void calculateMatrix(const AutoDiffFE &self, const FERequirementType &par, typename Traits::template MatrixType<> h)
Calculate the matrix associated with the finite element.
Definition: autodifffe.hh:47
friend auto calculateScalar(const AutoDiffFE &self, const FERequirementType &par)
Calculate the scalar value associated with the finite element.
Definition: autodifffe.hh:84
const RealFE & realFE() const
Get the reference to the base finite element.
Definition: autodifffe.hh:93
friend void calculateLocalSystem(const AutoDiffFE &self, const FERequirementType &par, typename Traits::template MatrixType<> h, typename Traits::template VectorType<> g)
Calculate the local system associated with the finite element.
Definition: autodifffe.hh:72
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
friend void calculateVector(const AutoDiffFE &self, const FERequirementType &par, typename Traits::template VectorType< double > g)
Calculate the vector associated with the finite element.
Definition: autodifffe.hh:59
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