forked from sandialabs/Albany
/
Albany_SacadoTypes.hpp
139 lines (119 loc) · 4.78 KB
/
Albany_SacadoTypes.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//*****************************************************************//
// Albany 3.0: Copyright 2016 Sandia Corporation //
// This Software is released under the BSD license detailed //
// in the file "license.txt" in the top-level Albany directory //
//*****************************************************************//
#ifndef ALBANY_SACADO_TYPES_HPP
#define ALBANY_SACADO_TYPES_HPP
// Get all Albany configuration macros
#include "Albany_config.h"
// Get Albany's RealType
#include "Albany_ScalarOrdinalTypes.hpp"
// Include all of our AD types
#include "Sacado.hpp"
#include "Sacado_MathFunctions.hpp"
#include "Sacado_ELRFad_DFad.hpp"
#include "Sacado_ELRCacheFad_DFad.hpp"
#include "Sacado_Fad_DFad.hpp"
#include "Sacado_Fad_SLFad.hpp"
#include "Sacado_Fad_SFad.hpp"
#include "Sacado_ELRFad_SLFad.hpp"
#include "Sacado_ELRFad_SFad.hpp"
#include "Sacado_CacheFad_DFad.hpp"
// Include ScalarParameterLibrary to specialize its traits
#include "Sacado_ScalarParameterLibrary.hpp"
#include "Sacado_ScalarParameterVector.hpp"
// ******************************************************************
// Definition of Sacado::ParameterLibrary traits
// ******************************************************************
// Switch between dynamic and static FAD types
#if defined(ALBANY_FAD_TYPE_SFAD)
typedef Sacado::Fad::SFad<RealType, ALBANY_SFAD_SIZE> FadType;
#elif defined(ALBANY_FAD_TYPE_SLFAD)
typedef Sacado::Fad::SLFad<RealType, ALBANY_SLFAD_SIZE> FadType;
#else
typedef Sacado::Fad::DFad<RealType> FadType;
#endif
#if defined(ALBANY_TAN_FAD_TYPE_SFAD)
typedef Sacado::Fad::SFad<RealType, ALBANY_TAN_SFAD_SIZE> TanFadType;
#elif defined(ALBANY_TAN_FAD_TYPE_SLFAD)
typedef Sacado::Fad::SLFad<RealType, ALBANY_TAN_SLFAD_SIZE> TanFadType;
#else
typedef Sacado::Fad::DFad<RealType> TanFadType;
#endif
typedef Sacado::Fad::SFad<RealType, 1> HessianVecInnerFad;
#if defined(ALBANY_HES_VEC_FAD_TYPE_SFAD)
typedef Sacado::Fad::SFad<HessianVecInnerFad, ALBANY_HES_VEC_SFAD_SIZE> HessianVecFad;
#elif defined(ALBANY_HES_VEC_FAD_TYPE_SLFAD)
typedef Sacado::Fad::SLFad<HessianVecInnerFad, ALBANY_HES_VEC_SLFAD_SIZE> HessianVecFad;
#else
typedef Sacado::Fad::DFad<HessianVecInnerFad> HessianVecFad;
#endif
struct SPL_Traits {
template <class T> struct apply {
typedef typename T::ScalarT type;
};
};
// Synonym for the ScalarParameterLibrary/Vector on our traits
typedef Sacado::ScalarParameterLibrary<SPL_Traits> ParamLib;
typedef Sacado::ScalarParameterVector<SPL_Traits> ParamVec;
namespace Albany
{
// Function to get the underlying value out of a scalar type
template <typename T>
typename Sacado::ScalarType<T>::type
KOKKOS_INLINE_FUNCTION
ADValue(const T& x) { return Sacado::ScalarValue<T>::eval(x); }
template <unsigned Size, unsigned Stride>
RealType
KOKKOS_INLINE_FUNCTION
ADValue(const Sacado::Fad::ViewFad<const HessianVecInnerFad, Size, Stride, HessianVecFad>& x) { return x.val().val(); }
// Function to convert a ScalarType to a different one. This is used to convert
// a ScalarT to a ParamScalarT.
// Note, for all Evaluation types but one, ScalarT and ParamScalarT are the same,
// and for those we want to keep the Fad derivatives (if any).
// The only exception can be Jacobian (if mesh/param do not depend on solution),
// where ParamScalarT=RealType and ScalarT=FadType.
// Notice also that if the two scalar types are different, the conversion works
// only if the target type has a constructor from the source type.
template<typename ToST,typename FromST>
struct ScalarConversionHelper
{
KOKKOS_INLINE_FUNCTION
static typename std::enable_if<std::is_constructible<ToST,FromST>::value,ToST>::type
apply (const FromST& x)
{
return ToST(x);
}
};
template<typename FromST>
struct ScalarConversionHelper<typename Sacado::ScalarType<FromST>::type,FromST>
{
KOKKOS_INLINE_FUNCTION
static typename Sacado::ScalarType<FromST>::type apply (const FromST& x)
{
return ADValue(x);
}
};
template<typename ToST,typename FromST>
KOKKOS_INLINE_FUNCTION
ToST convertScalar (const FromST& x)
{
return ScalarConversionHelper<ToST,FromST>::apply(x);
}
// Get the strongest type between ST1 and ST2, meaning the type that can
// be constructed from the other. If no conversion is possible, return void.
template<typename ST1, typename ST2>
struct StrongestScalarType {
private:
static constexpr bool st2_from_st1 = std::is_constructible<ST2,ST1>::value;
static constexpr bool st1_from_st2 = std::is_constructible<ST1,ST2>::value;
public:
using type = typename std::conditional<
st2_from_st1,
ST2,
typename std::conditional<st1_from_st2, ST1, void>::type
>::type;
};
} // namespace Albany
#endif // ALBANY_SACADO_TYPES_HPP