DUNE-ACFEM (unstable)

placeholdertraits.hh
1 #ifndef __DUNE_ACFEM_TENSORS_OPERATIONS_AUTODIFF_PLACEHOLDERTRAITS_HH__
2 #define __DUNE_ACFEM_TENSORS_OPERATIONS_AUTODIFF_PLACEHOLDERTRAITS_HH__
3 
4 #include "../indeterminate.hh"
5 #include "../placeholder.hh"
6 
7 namespace Dune {
8 
9  namespace ACFem {
10 
11  namespace Tensor {
12 
13  template<std::size_t Id, class Signature, class T, class DT>
14  class AutoDiffPlaceholder;
15 
16  template<class T>
17  struct IsAutoDiffPlaceholder
18  : FalseType
19  {};
20 
21  template<class T>
22  struct IsAutoDiffPlaceholder<T&>
23  : IsAutoDiffPlaceholder<std::decay_t<T> >
24  {};
25 
26  template<class T>
27  struct IsAutoDiffPlaceholder<T&&>
28  : IsAutoDiffPlaceholder<std::decay_t<T> >
29  {};
30 
31  template<std::size_t Id, class Signature, class T, class DT>
32  struct IsAutoDiffPlaceholder<AutoDiffPlaceholder<Id, Signature, T, DT> >
33  : TrueType
34  {};
35 
36  template<class T, class SFINAE = void>
37  struct AutoDiffTraits
38  {
39  static constexpr std::size_t depth_ = 0;
40  };
41 
42  template<class T>
43  struct AutoDiffTraits<T, std::enable_if_t<!IsDecay<T>::value> >
44  : AutoDiffTraits<std::decay_t<T> >
45  {};
46 
47  template<std::size_t Id, class Signature, class T, class DT>
48  struct AutoDiffTraits<AutoDiffPlaceholder<Id, Signature, T, DT> >
49  {
50  static constexpr std::size_t id_ = Id;
51  using IdType = IndexConstant<Id>;
52  using IndeterminateSignature = Signature;
53  using ValuesType = T;
54  using DerivativeType = DT;
55  static constexpr std::size_t depth_ = AutoDiffTraits<DT>::depth_ + 1;
56  using DepthType = IndexConstant<depth_>;
57  };
58 
59  namespace {
60  template<std::size_t N, class T, class SFINAE = void>
61  struct AutoDiffOfDepthHelper;
62 
63  template<class T>
64  struct AutoDiffOfDepthHelper<0, T, std::enable_if_t<!IsAutoDiffPlaceholder<T>::value> >
65  {
66  using Type = T;
67  };
68 
69  template<class T>
70  struct AutoDiffOfDepthHelper<0, T, std::enable_if_t<IsAutoDiffPlaceholder<T>::value> >
71  {
72  using Type = typename AutoDiffTraits<T>::ValuesType;
73  };
74 
75  template<std::size_t N, class T>
76  struct AutoDiffOfDepthHelper<N, T, std::enable_if_t<IsAutoDiffPlaceholder<T>::value> >
77  {
78  static_assert(N <= AutoDiffTraits<T>::depth_, "Not so many levels of AD available...");
79  using Type = AutoDiffPlaceholder<
80  AutoDiffTraits<T>::id_,
81  typename AutoDiffTraits<T>::IndeterminateSignature,
82  typename AutoDiffTraits<T>::ValuesType,
83  typename AutoDiffOfDepthHelper<N-1, typename AutoDiffTraits<T>::DerivativeType>::Type>;
84  };
85  }
86 
88  template<std::size_t N, class T>
89  using AutoDiffOfDepth = typename AutoDiffOfDepthHelper<N, T>::Type;
90 
94  template<class T, class Ids, class... Signature>
96  : FalseType
97  {};
98 
99  template<class T, std::size_t Id, class Signature, class SFINAE = void>
100  struct IsSingleMatchingAutoDiffPlaceholder
101  : BoolConstant<(AutoDiffTraits<T>::id_ == Id
102  && typename AutoDiffTraits<T>::IndeterminateSignature{} == Signature{})>
103  {};
104 
105  template<class T, std::size_t Id, class Signature>
106  struct IsSingleMatchingAutoDiffPlaceholder<
107  T, Id, Signature, std::enable_if_t<!IsAutoDiffPlaceholder<T>::value> >
108  : FalseType
109  {};
110 
111  template<class T>
112  struct IsMatchingAutoDiffPlaceholder<T, Seq<> >
113  : TrueType
114  {};
115 
116  template<class T, std::size_t Id, class Signature>
117  struct IsMatchingAutoDiffPlaceholder<T, Seq<Id>, Signature>
118  : IsSingleMatchingAutoDiffPlaceholder<T, Id, Signature>
119  {};
120 
121  template<class T,
122  std::size_t Id0, std::size_t Id1, std::size_t... RestId,
123  class Sig0, class Sig1, class... RestSig>
124  struct IsMatchingAutoDiffPlaceholder<T, Seq<Id0, Id1, RestId...>, Sig0, Sig1, RestSig...>
125  : BoolConstant<(IsSingleMatchingAutoDiffPlaceholder<T, Id0, Sig0>::value
126  && IsMatchingAutoDiffPlaceholder<T, Seq<Id1, RestId...>, Sig1, RestSig...>::value
127  )>
128  {};
129 
130  template<std::size_t Id, class Signature, class T, class DT>
131  struct HasSpecializedExpressionTraits<Tensor::AutoDiffPlaceholder<Id, Signature, T, DT> >
132  : TrueType
133  {};
134 
135  } // NS Tensor
136 
138  template<std::size_t Id, class Signature, class T, class DT>
139  struct ExpressionTraits<Tensor::AutoDiffPlaceholder<Id, Signature, T, DT> >
140  : ExpressionTraits<T>
141  {
142  static constexpr bool isVolatile = true;
143  };
144 
145  } // NS ACFem
146 
147  template<std::size_t Id, class Signature, class T, class DT>
148  struct FieldTraits<ACFem::Tensor::AutoDiffPlaceholder<Id, Signature, T, DT> >
149  : FieldTraits<std::decay_t<T> >
150  {};
151 
152 } // NS Dune
153 
154 #endif // __DUNE_ACFEM_TENSORS_OPERATIONS_AUTODIFF_PLACEHOLDERTRAITS_HH__
Constant< bool, V > BoolConstant
Short-cut for integral constant of type bool.
Definition: types.hh:48
BoolConstant< false > FalseType
Alias for std::false_type.
Definition: types.hh:110
BoolConstant< true > TrueType
Alias for std::true_type.
Definition: types.hh:107
Default expression traits definition is a recursion in order to ease disambiguation.
Definition: expressiontraits.hh:54
Check whehter T is a placeholder which matches the givens indeterminate ids and signature.
Definition: placeholdertraits.hh:97
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.80.0 (May 3, 22:32, 2024)