40#include "../expressions/lhsExpressionInterface.hpp"
41#include "../misc/binomial.hpp"
42#include "../misc/compileTimeLoop.hpp"
43#include "../misc/exceptions.hpp"
45#include "../traits/realTraits.hpp"
50#ifndef DOXYGEN_DISABLE
55 namespace DerivativeAccessImpl {
56 CODI_INLINE size_t constexpr maximumDerivatives(
size_t selectionDepth,
size_t order) {
57 return binomial(selectionDepth, order);
60 CODI_INLINE size_t constexpr maximumDerivativesPrimalBranch(
size_t selectionDepth,
size_t order) {
61 return binomial(selectionDepth - 1, order);
64 CODI_INLINE size_t constexpr isPrimalBranch(
size_t selectionDepth,
size_t order,
size_t l) {
65 return l < maximumDerivativesPrimalBranch(selectionDepth, order);
68 template<
typename Type,
size_t selectionDepth,
size_t order,
size_t l>
69 struct CheckCompileTimeValues {
72 "Selection depth must not be higher than the maximum derivative order.");
73 CODI_STATIC_ASSERT(order <= selectionDepth,
"Derivative order must not be higher than the selection depth.");
75 "Selected derivative must not be greater than the number of available derivatives for that"
78 static bool constexpr isValid =
true;
93 template<
typename Type,
bool constant,
size_t selectionDepth,
size_t order,
size_t l,
94 bool primalBranch = isPrimalBranch(selectionDepth, order, l)>
95 struct SelectCompileTime;
98 template<
typename T_Type,
bool constant,
size_t selectionDepth,
size_t order,
size_t l>
99 struct SelectCompileTime<T_Type, constant, selectionDepth, order, l, true> {
101 using Type =
CODI_DD(T_Type, CODI_DEFAULT_LHS_EXPRESSION);
103 using Inner = SelectCompileTime<
typename Type::Real, constant, selectionDepth - 1, order, l>;
104 using ArgType =
typename std::conditional<constant, Type const, Type>::type;
105 using RType =
typename Inner::RType;
108 "Checks inside of type.");
110 static RType& select(ArgType& value) {
111 return Inner::select(value.value());
116 template<
typename T_Type,
bool constant,
size_t selectionDepth,
size_t order,
size_t l>
117 struct SelectCompileTime<T_Type, constant, selectionDepth, order, l, false> {
119 using Type =
CODI_DD(T_Type, CODI_DEFAULT_LHS_EXPRESSION);
121 using Inner = SelectCompileTime<
typename Type::Gradient, constant, selectionDepth - 1, order - 1,
122 l - maximumDerivativesPrimalBranch(selectionDepth, order)>;
123 using ArgType =
typename std::conditional<constant, Type const, Type>::type;
124 using RType =
typename Inner::RType;
127 "Checks inside of type.");
129 static RType& select(ArgType& value) {
130 return Inner::select(value.gradient());
135 template<
typename Type,
bool constant>
136 struct SelectCompileTime<Type, constant, 0, 0, 0, true> {
138 using ArgType =
typename std::conditional<constant, Type const, Type>::type;
139 using RType = ArgType;
141 static RType& select(ArgType& value) {
155 template<
typename T_Type,
bool constant,
size_t T_selectionDepth>
156 struct SelectRunTime {
158 using Type =
CODI_DD(T_Type, CODI_DEFAULT_LHS_EXPRESSION);
162 "CoDiPack type needs to have the same real and gradient value for run time derivative "
165 "Selection depth must not be higher than the maximum derivative order");
167 using Inner = SelectRunTime<
typename Type::Real, constant, selectionDepth - 1>;
168 using ArgType =
typename std::conditional<constant, Type const, Type>::type;
169 using RType =
typename Inner::RType;
171 static RType& select(ArgType& v,
size_t order,
size_t l) {
172 size_t const maxDerivativesPrimalBranch =
binomial(selectionDepth - 1, order);
173 if (l < maxDerivativesPrimalBranch) {
174 return Inner::select(v.value(), order, l);
176 return Inner::select(v.gradient(), order - 1, l - maxDerivativesPrimalBranch);
182 template<
typename T_Type,
bool constant>
183 struct SelectRunTime<T_Type, constant, 0> {
185 using Type =
CODI_DD(T_Type,
double);
186 using ArgType =
typename std::conditional<constant, Type const, Type>::type;
187 using RType = ArgType;
189 static RType& select(ArgType& v,
size_t order,
size_t l) {
266 template<
typename T_Type>
274 template<
bool constant,
size_t selectionDepth>
275 using SelectRunTime = DerivativeAccessImpl::SelectRunTime<Type, constant, selectionDepth>;
278 template<
bool constant,
size_t selectionDepth,
size_t order,
size_t l>
279 using SelectCompileTime = DerivativeAccessImpl::SelectCompileTime<Type, constant, selectionDepth, order, l>;
283 template<size_t selectionDepth = RealTraits::MaxDerivativeOrder<Type>()>
286 checkRuntimeSelection<selectionDepth>(order, l);
293 template<size_t selectionDepth = RealTraits::MaxDerivativeOrder<Type>()>
295 checkRuntimeSelection<selectionDepth>(order, l);
301 template<typename Derivative, size_t selectionDepth = RealTraits::MaxDerivativeOrder<Type>()>
303 size_t const maxDerivatives =
binomial(selectionDepth, order);
304 for (
size_t i = 0; i < maxDerivatives; i += 1) {
305 derivative<selectionDepth>(v, order, i) = d;
311 template<typename Derivative, size_t selectionDepth = RealTraits::MaxDerivativeOrder<Type>()>
319 template<typename Derivative, size_t selectionDepth = RealTraits::MaxDerivativeOrder<Type>()>
322 v.gradient(), order - 1, d);
327 template<size_t order, size_t l, size_t selectionDepth = RealTraits::MaxDerivativeOrder<Type>()>
334 template<size_t order, size_t l, size_t selectionDepth = RealTraits::MaxDerivativeOrder<Type>()>
340 template<size_t order, typename Derivative, size_t selectionDepth = RealTraits::MaxDerivativeOrder<Type>()>
342 CompileTimeLoop<DerivativeAccessImpl::maximumDerivatives(selectionDepth, order)>::eval(
343 CallSetDerivative<order, Derivative, selectionDepth>{}, v, d);
348 template<size_t order, typename Derivative, size_t selectionDepth = RealTraits::MaxDerivativeOrder<Type>()>
356 template<size_t order, typename Derivative, size_t selectionDepth = RealTraits::MaxDerivativeOrder<Type>()>
359 selectionDepth - 1>(v.gradient(), d);
364 template<
size_t selectionDepth>
365 static void checkRuntimeSelection(
size_t order,
size_t l) {
366 if (order > selectionDepth) {
368 "The derivative order must be smaller or equal than the maximum possible derivative. order: %d, max "
370 order, selectionDepth);
373 size_t numberDerivatives =
binomial(selectionDepth, order);
374 if (l >= numberDerivatives) {
376 "The selected derivative must be smaller than the maximum number of derivatives. selected: %d, number "
378 l, numberDerivatives);
382 template<
size_t order,
typename Derivative,
size_t selectionDepth>
383 struct CallSetDerivative {
386 void operator()(std::integral_constant<size_t, pos>,
Type& v, Derivative
const& d) {
#define CODI_INLINE
See codi::Config::ForcedInlines.
Definition config.h:457
#define CODI_DD(Type, Default)
Abbreviation for CODI_DECLARE_DEFAULT.
Definition macros.hpp:94
#define CODI_UNDEFINED_VALUE
Used in interface declarations for variables that have to be defined in the specializations.
Definition macros.hpp:117
#define CODI_STATIC_ASSERT(cond, message)
Static assert in CoDiPack.
Definition macros.hpp:123
#define CODI_T(...)
Abbreviation for CODI_TEMPLATE.
Definition macros.hpp:111
CoDiPack - Code Differentiation Package.
Definition codi.hpp:90
void CODI_UNUSED(Args const &...)
Disable unused warnings for an arbitrary number of arguments.
Definition macros.hpp:46
size_t constexpr binomial(size_t n, size_t k)
Binomial coefficient computation.
Definition binomial.hpp:56
Compile time loop evaluation.
Definition compileTimeLoop.hpp:54
A helper class for the access of the various derivatives in higher order AD types.
Definition derivativeAccess.hpp:267
static SelectCompileTime< false, selectionDepth, order, l >::RType & derivative(Type &v)
Definition derivativeAccess.hpp:335
static void setAllDerivativesForward(Type &v, size_t order, Derivative const &d)
Definition derivativeAccess.hpp:312
static void setAllDerivatives(Type &v, Derivative const &d)
Compile time set of all derivatives of the same order. .
Definition derivativeAccess.hpp:341
static void setAllDerivativesReverse(Type &v, Derivative const &d)
Definition derivativeAccess.hpp:357
static void setAllDerivativesForward(Type &v, Derivative const &d)
Definition derivativeAccess.hpp:349
static SelectRunTime< true, selectionDepth >::RType const & derivative(Type const &v, size_t order, size_t l)
Definition derivativeAccess.hpp:284
static SelectRunTime< false, selectionDepth >::RType & derivative(Type &v, size_t order, size_t l)
Definition derivativeAccess.hpp:294
static void setAllDerivatives(Type &v, size_t order, Derivative const &d)
Run time set of all derivatives of the same order. .
Definition derivativeAccess.hpp:302
static SelectCompileTime< true, selectionDepth, order, l >::RType const & derivative(Type const &v)
Definition derivativeAccess.hpp:328
DerivativeAccessImpl::SelectRunTime< Type, constant, selectionDepth > SelectRunTime
Helper for the run time selection of derivatives.
Definition derivativeAccess.hpp:275
static void setAllDerivativesReverse(Type &v, size_t order, Derivative const &d)
Definition derivativeAccess.hpp:320
DerivativeAccessImpl::SelectCompileTime< Type, constant, selectionDepth, order, l > SelectCompileTime
Helper for the compile time selection of derivatives.
Definition derivativeAccess.hpp:279
T_Type Type
See DerivativeAccess.
Definition derivativeAccess.hpp:271