12#include <autodiff/forward/dual.hpp>
13#include <autodiff/forward/dual/dual.hpp>
14#include <autodiff/forward/dual/eigen.hpp>
15#include <autodiff/forward/utils/derivative.hpp>
33template <
typename RealMAT,
bool forceAutoDiffV = false,
bool forceAutoDiffS = false>
43 static constexpr int dim = RealMAT::dim;
46 static constexpr auto strainTag = RealMAT::strainTag;
47 static constexpr auto stressTag = RealMAT::stressTag;
63 template <
typename... Args>
65 : RealMAT{
std::forward<Args>(args)...} {}
67 [[nodiscard]]
constexpr static std::string
nameImpl() noexcept {
return "AutoDiff: " + RealMAT::name(); }
80 template <StrainTags tag,
typename Derived>
82 if constexpr (
requires {
realMAT().template storedEnergy<tag>(E); }) {
83 auto mat_ad =
realMAT().template rebind<autodiff::dual>();
84 return mat_ad.template storedEnergy<tag>(E);
86 static_assert(Dune::AlwaysFalse<AutoDiffMAT>::value,
87 "Appropriate storedEnergy function not is implemented for the chosen material model.");
98 template <StrainTags tag,
bool voigt = true,
typename Derived>
99 auto stresses(
const Eigen::MatrixBase<Derived>& E)
const {
100 if constexpr (
requires {
realMAT().template stresses<tag>(E); } and not(forceAutoDiffV or forceAutoDiffS)) {
101 return realMAT().template stresses<tag>(E);
102 }
else if constexpr (
requires {
realMAT().template storedEnergy<tag>(E); }) {
104 "The strain measure used for autodiff has to be in matrix notation.");
105 auto mat_ad =
realMAT().template rebind<autodiff::dual>();
107 auto f = [&](
const auto& x) {
return mat_ad.template storedEnergy<tag>(x); };
109 Eigen::Vector<autodiff::dual, nVoigtIndices> dx =
toVoigt(E.derived());
111 Eigen::Vector<double, nVoigtIndices> g;
113 gradient(f, autodiff::wrt(dx), autodiff::at(dx), e, g);
117 static_assert(Dune::AlwaysFalse<AutoDiffMAT>::value,
118 "Appropriate storedEnergy function not is implemented for the chosen material model.");
129 template <StrainTags tag,
bool voigt = true,
typename Derived>
131 if constexpr (
requires {
realMAT().template tangentModuli<tag>(E); } and not(forceAutoDiffV or forceAutoDiffS)) {
132 return realMAT().template tangentModuli<tag>(E);
133 }
else if constexpr (
requires {
realMAT().template stresses<tag>(E); } and forceAutoDiffV and not forceAutoDiffS) {
135 "The strain measure used for autodiff has to be in matrix notation.");
136 auto mat_ad =
realMAT().template rebind<autodiff::dual>();
138 auto f = [&](
const auto& x) {
return mat_ad.template stresses<tag>(x); };
140 auto dx = Eigen::Vector<autodiff::dual, nVoigtIndices>{};
145 auto h = Eigen::Matrix<double, nVoigtIndices, nVoigtIndices>{};
146 jacobian(f, autodiff::wrt(dx), autodiff::at(dx), g, h);
149 }
else if constexpr (
requires {
realMAT().template storedEnergy<tag>(E); }) {
151 "The strain measure used for autodiff has to be in matrix notation.");
152 auto mat_ad =
realMAT().template rebind<autodiff::dual2nd>();
154 auto f = [&](
const auto& x) {
return mat_ad.template storedEnergy<tag>(x); };
156 Eigen::Matrix<autodiff::dual2nd, nVoigtIndices, 1> dx =
toVoigt(E.derived());
159 Eigen::Matrix<double, nVoigtIndices, 1> g;
160 Eigen::Matrix<double, nVoigtIndices, nVoigtIndices> h;
162 h = autodiff::hessian(f, autodiff::wrt(dx), autodiff::at(dx), e, g);
167 Dune::AlwaysFalse<AutoDiffMAT>::value,
168 "Appropriate storedEnergy and stresses functions are not implemented for the chosen material model.");
177 const RealMAT&
realMAT()
const {
return *
this; }
Helper for the Eigen::Tensor types.
helper functions used by material model implementations.
constexpr Eigen::Index toVoigt(Eigen::Index i, Eigen::Index j) noexcept
Converts 2D indices to Voigt notation index.
Definition: tensorutils.hh:179
Definition: autodiffmat.hh:21
Implementation of a AutoDiff-based material model.
Definition: autodiffmat.hh:35
static constexpr bool moduliToVoigt
Definition: autodiffmat.hh:52
typename RealMAT::StrainMatrix StrainMatrix
Definition: autodiffmat.hh:37
static constexpr auto strainTag
Definition: autodiffmat.hh:46
static constexpr double derivativeFactorImpl
Definition: autodiffmat.hh:54
MaterialParameters materialParametersImpl() const
Returns the material parameters stored in the material.
Definition: autodiffmat.hh:72
typename RealMAT::StressMatrix StressMatrix
Definition: autodiffmat.hh:38
static constexpr std::string nameImpl() noexcept
Definition: autodiffmat.hh:67
typename RealMAT::MaterialTensor MaterialTensor
Definition: autodiffmat.hh:39
static constexpr int dim
Definition: autodiffmat.hh:43
auto storedEnergy(const Eigen::MatrixBase< Derived > &E) const
Computes the stored energy in the underlying material model.
Definition: autodiffmat.hh:81
static constexpr bool energyAcceptsVoigt
Definition: autodiffmat.hh:49
const RealMAT & realMAT() const
Get the reference to the base material.
Definition: autodiffmat.hh:177
static constexpr auto stressTag
Definition: autodiffmat.hh:47
static constexpr bool moduliAcceptsVoigt
Definition: autodiffmat.hh:53
static constexpr int nVoigtIndices
Definition: autodiffmat.hh:44
typename RealMAT::MaterialParameters MaterialParameters
Definition: autodiffmat.hh:41
static constexpr bool stressToVoigt
Definition: autodiffmat.hh:50
auto stresses(const Eigen::MatrixBase< Derived > &E) const
Computes the stresses in the underlying material model.
Definition: autodiffmat.hh:99
static constexpr auto tangentModuliTag
Definition: autodiffmat.hh:48
typename RealMAT::ScalarType ScalarType
Definition: autodiffmat.hh:36
auto tangentModuli(const Eigen::MatrixBase< Derived > &E) const
Computes the tangent moduli in the underlying material model.
Definition: autodiffmat.hh:130
AutoDiffMAT(Args &&... args)
Constructor for the AutoDiffMAT class. Forward the construction to the underlying element.
Definition: autodiffmat.hh:64
static constexpr bool stressAcceptsVoigt
Definition: autodiffmat.hh:51
Concept defining the requirements for Eigen vectors.
Definition: utils/concepts.hh:353
Contains the Material interface class and related template functions for material properties.