16 #ifndef dealii_differentiation_ad_sacado_number_types_h
17 #define dealii_differentiation_ad_sacado_number_types_h
21 #include <type_traits>
38 template <
typename NumberType,
typename =
void>
50 template <
typename NumberType,
typename =
void>
62 template <
typename NumberType,
typename =
void>
74 #ifdef DEAL_II_TRILINOS_WITH_SACADO
77 # include <Sacado.hpp>
82 # include <Sacado_Fad_DFad.hpp>
84 # include <Sacado_trad.hpp>
112 template <
typename SacadoNumber,
typename =
void>
155 template <
typename SacadoNumber>
156 struct SacadoNumberInfo<
158 typename std::enable_if<std::is_same<
160 Sacado::Fad::DFad<typename SacadoNumber::value_type>>::value>::type>
164 using value_type =
typename ad_type::value_type;
165 using derivative_type =
typename ad_type::value_type;
167 static const unsigned int n_supported_derivative_levels =
168 1 + SacadoNumberInfo<derivative_type>::n_supported_derivative_levels;
175 template <
typename SacadoNumber>
176 struct SacadoNumberInfo<
178 typename std::enable_if<std::is_same<
180 Sacado::Rad::ADvar<typename SacadoNumber::value_type>>::value>::type>
183 using scalar_type =
typename ad_type::ADVari::scalar_type;
184 using value_type =
typename ad_type::ADVari::value_type;
185 using derivative_type =
typename ad_type::ADVari::value_type;
187 static const unsigned int n_supported_derivative_levels =
188 1 + SacadoNumberInfo<derivative_type>::n_supported_derivative_levels;
198 template <
typename Number>
199 struct SacadoNumberInfo<
201 typename std::enable_if<
202 std::is_arithmetic<typename std::decay<Number>::type>::value>::type>
204 static const unsigned int n_supported_derivative_levels = 0;
212 template <
typename ScalarType>
213 struct ADNumberInfoFromEnum<
216 typename std::enable_if<
217 std::is_floating_point<ScalarType>::value>::type>
219 static const bool is_taped =
false;
220 using real_type = Sacado::Fad::DFad<ScalarType>;
221 using derivative_type =
222 typename SacadoNumberInfo<real_type>::derivative_type;
223 static const unsigned int n_supported_derivative_levels =
224 SacadoNumberInfo<real_type>::n_supported_derivative_levels;
232 template <
typename ScalarType>
233 struct ADNumberInfoFromEnum<
236 typename std::enable_if<
237 std::is_floating_point<ScalarType>::value>::type>
239 static const bool is_taped =
false;
240 using real_type = Sacado::Fad::DFad<Sacado::Fad::DFad<ScalarType>>;
241 using derivative_type =
242 typename SacadoNumberInfo<real_type>::derivative_type;
243 static const unsigned int n_supported_derivative_levels =
244 SacadoNumberInfo<real_type>::n_supported_derivative_levels;
252 template <
typename ScalarType>
253 struct ADNumberInfoFromEnum<
256 typename std::enable_if<
257 std::is_floating_point<ScalarType>::value>::type>
259 static const bool is_taped =
false;
260 using real_type = Sacado::Rad::ADvar<ScalarType>;
261 using derivative_type =
262 typename SacadoNumberInfo<real_type>::derivative_type;
263 static const unsigned int n_supported_derivative_levels =
264 SacadoNumberInfo<real_type>::n_supported_derivative_levels;
272 template <
typename ScalarType>
273 struct ADNumberInfoFromEnum<
276 typename std::enable_if<
277 std::is_floating_point<ScalarType>::value>::type>
279 static const bool is_taped =
false;
280 using real_type = Sacado::Rad::ADvar<Sacado::Fad::DFad<ScalarType>>;
281 using derivative_type =
282 typename SacadoNumberInfo<real_type>::derivative_type;
283 static const unsigned int n_supported_derivative_levels =
284 SacadoNumberInfo<real_type>::n_supported_derivative_levels;
292 template <
typename NumberType>
293 struct Marking<Sacado::Fad::DFad<NumberType>>
296 typename SacadoNumberInfo<Sacado::Fad::DFad<NumberType>>
::ad_type;
297 using derivative_type =
typename SacadoNumberInfo<
298 Sacado::Fad::DFad<NumberType>>::derivative_type;
300 typename SacadoNumberInfo<Sacado::Fad::DFad<NumberType>>
::scalar_type;
307 const unsigned int index,
308 const unsigned int n_independent_variables,
313 out =
ad_type(n_independent_variables, index, in);
316 Marking<derivative_type>::independent_variable(
317 in, index, n_independent_variables, out.val());
335 template <
typename NumberType>
336 struct Marking<Sacado::Rad::ADvar<NumberType>>
339 typename SacadoNumberInfo<Sacado::Rad::ADvar<NumberType>>
::ad_type;
340 using derivative_type =
typename SacadoNumberInfo<
341 Sacado::Rad::ADvar<NumberType>>::derivative_type;
350 const unsigned int index,
351 const unsigned int n_independent_variables,
360 derivative_type derivative_initializer;
361 Marking<derivative_type>::independent_variable(
362 in, index, n_independent_variables, derivative_initializer);
365 out = derivative_initializer;
386 template <
typename NumberType>
387 struct ExtractData<Sacado::Fad::DFad<NumberType>>
389 using derivative_type =
typename SacadoNumberInfo<
390 Sacado::Fad::DFad<NumberType>>::derivative_type;
392 typename SacadoNumberInfo<Sacado::Fad::DFad<NumberType>>
::scalar_type;
394 typename SacadoNumberInfo<Sacado::Fad::DFad<NumberType>>
::value_type;
400 value(
const Sacado::Fad::DFad<NumberType> &x)
410 n_directional_derivatives(
const Sacado::Fad::DFad<NumberType> &x)
419 static derivative_type
420 directional_derivative(
const Sacado::Fad::DFad<NumberType> &x,
421 const unsigned int direction)
423 if (x.hasFastAccess())
424 return x.fastAccessDx(direction);
426 return x.dx(direction);
438 template <
typename NumberType>
439 struct ExtractData<Sacado::Rad::ADvar<NumberType>>
441 using derivative_type =
typename SacadoNumberInfo<
442 Sacado::Rad::ADvar<NumberType>>::derivative_type;
446 typename SacadoNumberInfo<Sacado::Rad::ADvar<NumberType>>
::value_type;
452 value(
const Sacado::Rad::ADvar<NumberType> &x)
462 n_directional_derivatives(
const Sacado::Rad::ADvar<NumberType> &)
478 static derivative_type
479 directional_derivative(
const Sacado::Rad::ADvar<NumberType> &x,
498 template <
typename ADNumberType>
499 struct ADNumberTraits<
501 typename std::enable_if<std::is_same<
503 Sacado::Fad::DFad<typename ADNumberType::scalar_type>>::value>::type>
504 : NumberTraits<typename ADNumberType::scalar_type,
505 NumberTypes::sacado_dfad>
515 template <
typename ADNumberType>
516 struct ADNumberTraits<
518 typename std::enable_if<std::is_same<
520 std::complex<Sacado::Fad::DFad<
521 typename ADNumberType::value_type::scalar_type>>>::value>::type>
523 std::complex<typename ADNumberType::value_type::scalar_type>,
524 NumberTypes::sacado_dfad>
535 typename ADNumberTraits<Sacado::Fad::DFad<float>>::scalar_type,
536 NumberTypes::sacado_dfad>
545 struct NumberTraits<std::complex<Sacado::Fad::DFad<float>>,
547 : NumberTraits<typename ADNumberTraits<
548 std::complex<Sacado::Fad::DFad<float>>>::scalar_type,
549 NumberTypes::sacado_dfad>
560 typename ADNumberTraits<Sacado::Fad::DFad<double>>::scalar_type,
561 NumberTypes::sacado_dfad>
570 struct NumberTraits<std::complex<Sacado::Fad::DFad<double>>,
572 : NumberTraits<typename ADNumberTraits<
573 std::complex<Sacado::Fad::DFad<double>>>::scalar_type,
574 NumberTypes::sacado_dfad>
587 template <
typename ADNumberType>
588 struct ADNumberTraits<
590 typename std::enable_if<std::is_same<
592 Sacado::Rad::ADvar<typename ADNumberType::ADVari::scalar_type>>::
594 : NumberTraits<typename ADNumberType::ADVari::scalar_type,
595 NumberTypes::sacado_rad>
606 typename ADNumberTraits<Sacado::Rad::ADvar<float>>::scalar_type,
607 NumberTypes::sacado_rad>
618 typename ADNumberTraits<Sacado::Rad::ADvar<double>>::scalar_type,
619 NumberTypes::sacado_rad>
623 # ifdef DEAL_II_TRILINOS_CXX_SUPPORTS_SACADO_COMPLEX_RAD
632 template <
typename ADNumberType>
633 struct ADNumberTraits<
635 typename std::enable_if<std::is_same<
637 std::complex<Sacado::Rad::ADvar<typename ADNumberType::value_type::
638 ADVari::scalar_type>>>::value>::type>
640 std::complex<typename ADNumberType::value_type::ADVari::scalar_type>,
641 NumberTypes::sacado_rad>
650 struct NumberTraits<std::complex<Sacado::Rad::ADvar<float>>,
652 : NumberTraits<typename ADNumberTraits<
653 std::complex<Sacado::Rad::ADvar<float>>>::scalar_type,
654 NumberTypes::sacado_rad>
663 struct NumberTraits<std::complex<Sacado::Rad::ADvar<double>>,
665 : NumberTraits<typename ADNumberTraits<
666 std::complex<Sacado::Rad::ADvar<double>>>::scalar_type,
667 NumberTypes::sacado_rad>
683 template <
typename ADNumberType>
684 struct ADNumberTraits<
686 typename std::enable_if<
687 std::is_same<ADNumberType,
688 Sacado::Fad::DFad<Sacado::Fad::DFad<
689 typename ADNumberType::scalar_type>>>::value>::type>
690 : NumberTraits<typename ADNumberType::scalar_type,
691 NumberTypes::sacado_dfad_dfad>
702 template <
typename ADNumberType>
703 struct ADNumberTraits<
705 typename std::enable_if<std::is_same<
707 std::complex<Sacado::Fad::DFad<Sacado::Fad::DFad<
708 typename ADNumberType::value_type::scalar_type>>>>::value>::type>
710 std::complex<typename ADNumberType::value_type::scalar_type>,
711 NumberTypes::sacado_dfad_dfad>
720 struct NumberTraits<Sacado::Fad::DFad<Sacado::Fad::DFad<float>>,
722 : NumberTraits<typename ADNumberTraits<Sacado::Fad::DFad<
723 Sacado::Fad::DFad<float>>>::scalar_type,
724 NumberTypes::sacado_dfad_dfad>
734 std::complex<Sacado::Fad::DFad<Sacado::Fad::DFad<float>>>,
736 : NumberTraits<typename ADNumberTraits<std::complex<Sacado::Fad::DFad<
737 Sacado::Fad::DFad<float>>>>::scalar_type,
738 NumberTypes::sacado_dfad_dfad>
747 struct NumberTraits<Sacado::Fad::DFad<Sacado::Fad::DFad<double>>,
749 : NumberTraits<typename ADNumberTraits<Sacado::Fad::DFad<
750 Sacado::Fad::DFad<double>>>::scalar_type,
751 NumberTypes::sacado_dfad_dfad>
761 std::complex<Sacado::Fad::DFad<Sacado::Fad::DFad<double>>>,
763 : NumberTraits<typename ADNumberTraits<std::complex<Sacado::Fad::DFad<
764 Sacado::Fad::DFad<double>>>>::scalar_type,
765 NumberTypes::sacado_dfad_dfad>
778 template <
typename ADNumberType>
779 struct ADNumberTraits<
781 typename std::enable_if<std::is_same<
783 Sacado::Rad::ADvar<Sacado::Fad::DFad<
784 typename ADNumberType::ADVari::scalar_type>>>::value>::type>
785 : NumberTraits<typename ADNumberType::ADVari::scalar_type,
786 NumberTypes::sacado_rad_dfad>
795 struct NumberTraits<Sacado::Rad::ADvar<Sacado::Fad::DFad<float>>,
797 : NumberTraits<typename ADNumberTraits<Sacado::Rad::ADvar<
798 Sacado::Fad::DFad<float>>>::scalar_type,
799 NumberTypes::sacado_rad_dfad>
808 struct NumberTraits<Sacado::Rad::ADvar<Sacado::Fad::DFad<double>>,
810 : NumberTraits<typename ADNumberTraits<Sacado::Rad::ADvar<
811 Sacado::Fad::DFad<double>>>::scalar_type,
812 NumberTypes::sacado_rad_dfad>
816 # ifdef DEAL_II_TRILINOS_CXX_SUPPORTS_SACADO_COMPLEX_RAD
826 template <
typename ADNumberType>
827 struct ADNumberTraits<
829 typename std::enable_if<std::is_same<
831 std::complex<Sacado::Rad::ADvar<Sacado::Fad::DFad<
832 typename ADNumberType::value_type::ADVari::scalar_type>>>>::value>::
835 std::complex<typename ADNumberType::value_type::ADVari::scalar_type>,
836 NumberTypes::sacado_rad_dfad>
846 std::complex<Sacado::Rad::ADvar<Sacado::Fad::DFad<float>>>,
848 : NumberTraits<typename ADNumberTraits<std::complex<Sacado::Rad::ADvar<
849 Sacado::Fad::DFad<float>>>>::scalar_type,
850 NumberTypes::sacado_rad_dfad>
860 std::complex<Sacado::Rad::ADvar<Sacado::Fad::DFad<double>>>,
862 : NumberTraits<typename ADNumberTraits<std::complex<Sacado::Rad::ADvar<
863 Sacado::Fad::DFad<double>>>>::scalar_type,
864 NumberTypes::sacado_rad_dfad>
874 template <
typename NumberType>
875 struct is_sacado_dfad_number<
877 typename std::enable_if<
878 ADNumberTraits<typename std::decay<NumberType>::type>::type_code ==
879 NumberTypes::sacado_dfad ||
880 ADNumberTraits<typename std::decay<NumberType>::type>::type_code ==
881 NumberTypes::sacado_dfad_dfad>::type> : std::true_type
885 template <
typename NumberType>
886 struct is_sacado_dfad_number<
888 typename std::enable_if<std::is_same<
890 Sacado::Fad::Expr<typename NumberType::value_type>>::value>::type>
895 template <
typename NumberType>
896 struct is_sacado_rad_number<
898 typename std::enable_if<
899 ADNumberTraits<typename std::decay<NumberType>::type>::type_code ==
900 NumberTypes::sacado_rad ||
901 ADNumberTraits<typename std::decay<NumberType>::type>::type_code ==
902 NumberTypes::sacado_rad_dfad>::type> : std::true_type
906 template <
typename NumberType>
907 struct is_sacado_rad_number<
909 typename std::enable_if<std::is_same<
911 Sacado::Rad::ADvari<Sacado::Fad::DFad<
912 typename NumberType::ADVari::scalar_type>>>::value>::type>
917 template <
typename NumberType>
918 struct is_sacado_number<
920 typename std::enable_if<is_sacado_dfad_number<NumberType>::value ||
921 is_sacado_rad_number<NumberType>::value>::type>
931 template <
typename NumberType>
932 struct is_cuda_compatible<
934 typename std::enable_if<::Differentiation::AD::is_sacado_rad_number<
935 NumberType>::value>::type> : std::false_type
946 #endif // DEAL_II_TRILINOS_WITH_SACADO