16 #ifndef dealii_differentiation_ad_sacado_number_types_h 17 #define dealii_differentiation_ad_sacado_number_types_h 19 #include <deal.II/base/config.h> 21 #include <type_traits> 24 DEAL_II_NAMESPACE_OPEN
38 template <
typename NumberType,
typename =
void>
50 template <
typename NumberType,
typename =
void>
62 template <
typename NumberType,
typename =
void>
70 DEAL_II_NAMESPACE_CLOSE
74 #ifdef DEAL_II_TRILINOS_WITH_SACADO 76 DEAL_II_DISABLE_EXTRA_DIAGNOSTICS
77 # include <Sacado.hpp> 82 # include <Sacado_Fad_DFad.hpp> 84 # include <Sacado_trad.hpp> 85 DEAL_II_ENABLE_EXTRA_DIAGNOSTICS
87 # include <deal.II/base/exceptions.h> 88 # include <deal.II/base/numbers.h> 90 # include <deal.II/differentiation/ad/ad_number_traits.h> 91 # include <deal.II/differentiation/ad/ad_number_types.h> 94 # include <type_traits> 96 DEAL_II_NAMESPACE_OPEN
113 template <
typename SacadoNumber,
typename =
void>
156 template <
typename SacadoNumber>
157 struct SacadoNumberInfo<
159 typename
std::enable_if<std::is_same<
161 Sacado::Fad::DFad<typename SacadoNumber::value_type>>::value>::type>
163 using ad_type = SacadoNumber;
164 using scalar_type =
typename ad_type::scalar_type;
165 using value_type =
typename ad_type::value_type;
166 using derivative_type =
typename ad_type::value_type;
168 static const unsigned int n_supported_derivative_levels =
169 1 + SacadoNumberInfo<derivative_type>::n_supported_derivative_levels;
176 template <
typename SacadoNumber>
177 struct SacadoNumberInfo<
179 typename
std::enable_if<std::is_same<
181 Sacado::Rad::ADvar<typename SacadoNumber::value_type>>::value>::type>
183 using ad_type = SacadoNumber;
184 using scalar_type =
typename ad_type::ADVari::scalar_type;
185 using value_type =
typename ad_type::ADVari::value_type;
186 using derivative_type =
typename ad_type::ADVari::value_type;
188 static const unsigned int n_supported_derivative_levels =
189 1 + SacadoNumberInfo<derivative_type>::n_supported_derivative_levels;
199 template <
typename Number>
200 struct SacadoNumberInfo<
202 typename
std::enable_if<
203 std::is_arithmetic<typename std::decay<Number>::type>::value>::type>
205 static const unsigned int n_supported_derivative_levels = 0;
213 template <
typename ScalarType>
214 struct ADNumberInfoFromEnum<
217 typename std::enable_if<
218 std::is_floating_point<ScalarType>::value>::type>
220 static const bool is_taped =
false;
221 using real_type = Sacado::Fad::DFad<ScalarType>;
222 using derivative_type =
223 typename SacadoNumberInfo<real_type>::derivative_type;
224 static const unsigned int n_supported_derivative_levels =
225 SacadoNumberInfo<real_type>::n_supported_derivative_levels;
233 template <
typename ScalarType>
234 struct ADNumberInfoFromEnum<
237 typename std::enable_if<
238 std::is_floating_point<ScalarType>::value>::type>
240 static const bool is_taped =
false;
241 using real_type = Sacado::Fad::DFad<Sacado::Fad::DFad<ScalarType>>;
242 using derivative_type =
243 typename SacadoNumberInfo<real_type>::derivative_type;
244 static const unsigned int n_supported_derivative_levels =
245 SacadoNumberInfo<real_type>::n_supported_derivative_levels;
253 template <
typename ScalarType>
254 struct ADNumberInfoFromEnum<
257 typename std::enable_if<
258 std::is_floating_point<ScalarType>::value>::type>
260 static const bool is_taped =
false;
261 using real_type = Sacado::Rad::ADvar<ScalarType>;
262 using derivative_type =
263 typename SacadoNumberInfo<real_type>::derivative_type;
264 static const unsigned int n_supported_derivative_levels =
265 SacadoNumberInfo<real_type>::n_supported_derivative_levels;
273 template <
typename ScalarType>
274 struct ADNumberInfoFromEnum<
277 typename std::enable_if<
278 std::is_floating_point<ScalarType>::value>::type>
280 static const bool is_taped =
false;
281 using real_type = Sacado::Rad::ADvar<Sacado::Fad::DFad<ScalarType>>;
282 using derivative_type =
283 typename SacadoNumberInfo<real_type>::derivative_type;
284 static const unsigned int n_supported_derivative_levels =
285 SacadoNumberInfo<real_type>::n_supported_derivative_levels;
293 template <
typename NumberType>
294 struct Marking<Sacado::Fad::DFad<NumberType>>
297 typename SacadoNumberInfo<Sacado::Fad::DFad<NumberType>>::ad_type;
298 using derivative_type =
typename SacadoNumberInfo<
299 Sacado::Fad::DFad<NumberType>>::derivative_type;
301 typename SacadoNumberInfo<Sacado::Fad::DFad<NumberType>>::scalar_type;
307 independent_variable(
const scalar_type &in,
308 const unsigned int index,
309 const unsigned int n_independent_variables,
314 out = ad_type(n_independent_variables, index, in);
317 Marking<derivative_type>::independent_variable(
318 in, index, n_independent_variables, out.val());
325 dependent_variable(ad_type &out,
const ad_type &func)
336 template <
typename NumberType>
337 struct Marking<Sacado::Rad::ADvar<NumberType>>
340 typename SacadoNumberInfo<Sacado::Rad::ADvar<NumberType>>::ad_type;
341 using derivative_type =
typename SacadoNumberInfo<
342 Sacado::Rad::ADvar<NumberType>>::derivative_type;
343 using scalar_type =
typename SacadoNumberInfo<
344 Sacado::Rad::ADvar<NumberType>>::scalar_type;
350 independent_variable(
const scalar_type &in,
351 const unsigned int index,
352 const unsigned int n_independent_variables,
361 derivative_type derivative_initializer;
362 Marking<derivative_type>::independent_variable(
363 in, index, n_independent_variables, derivative_initializer);
366 out = derivative_initializer;
373 dependent_variable(ad_type &out,
const ad_type &func)
387 template <
typename NumberType>
388 struct ExtractData<Sacado::Fad::DFad<NumberType>>
390 using derivative_type =
typename SacadoNumberInfo<
391 Sacado::Fad::DFad<NumberType>>::derivative_type;
393 typename SacadoNumberInfo<Sacado::Fad::DFad<NumberType>>::scalar_type;
395 typename SacadoNumberInfo<Sacado::Fad::DFad<NumberType>>::value_type;
401 value(
const Sacado::Fad::DFad<NumberType> &x)
403 return ExtractData<value_type>::value(x.val());
411 n_directional_derivatives(
const Sacado::Fad::DFad<NumberType> &x)
420 static derivative_type
421 directional_derivative(
const Sacado::Fad::DFad<NumberType> &x,
422 const unsigned int direction)
424 if (x.hasFastAccess())
425 return x.fastAccessDx(direction);
427 return x.dx(direction);
439 template <
typename NumberType>
440 struct ExtractData<Sacado::Rad::ADvar<NumberType>>
442 using derivative_type =
typename SacadoNumberInfo<
443 Sacado::Rad::ADvar<NumberType>>::derivative_type;
444 using scalar_type =
typename SacadoNumberInfo<
445 Sacado::Rad::ADvar<NumberType>>::scalar_type;
447 typename SacadoNumberInfo<Sacado::Rad::ADvar<NumberType>>::value_type;
453 value(
const Sacado::Rad::ADvar<NumberType> &x)
455 return ExtractData<value_type>::value(x.val());
463 n_directional_derivatives(
const Sacado::Rad::ADvar<NumberType> &)
479 static derivative_type
480 directional_derivative(
const Sacado::Rad::ADvar<NumberType> &x,
499 template <
typename ADNumberType>
500 struct ADNumberTraits<
502 typename
std::enable_if<std::is_same<
504 Sacado::Fad::DFad<typename ADNumberType::scalar_type>>::value>::type>
505 : NumberTraits<typename ADNumberType::scalar_type,
506 NumberTypes::sacado_dfad>
516 template <
typename ADNumberType>
517 struct ADNumberTraits<
519 typename
std::enable_if<std::is_same<
521 std::complex<Sacado::Fad::DFad<
522 typename ADNumberType::value_type::scalar_type>>>::value>::type>
524 std::complex<typename ADNumberType::value_type::scalar_type>,
525 NumberTypes::sacado_dfad>
536 typename ADNumberTraits<Sacado::Fad::DFad<float>>::scalar_type,
537 NumberTypes::sacado_dfad>
546 struct NumberTraits<
std::complex<Sacado::Fad::DFad<float>>,
548 : NumberTraits<typename ADNumberTraits<
549 std::complex<Sacado::Fad::DFad<float>>>::scalar_type,
550 NumberTypes::sacado_dfad>
561 typename ADNumberTraits<Sacado::Fad::DFad<double>>::scalar_type,
562 NumberTypes::sacado_dfad>
571 struct NumberTraits<
std::complex<Sacado::Fad::DFad<double>>,
573 : NumberTraits<typename ADNumberTraits<
574 std::complex<Sacado::Fad::DFad<double>>>::scalar_type,
575 NumberTypes::sacado_dfad>
588 template <
typename ADNumberType>
589 struct ADNumberTraits<
591 typename
std::enable_if<std::is_same<
593 Sacado::Rad::ADvar<typename ADNumberType::ADVari::scalar_type>>::
595 : NumberTraits<typename ADNumberType::ADVari::scalar_type,
596 NumberTypes::sacado_rad>
607 typename ADNumberTraits<Sacado::Rad::ADvar<float>>::scalar_type,
608 NumberTypes::sacado_rad>
619 typename ADNumberTraits<Sacado::Rad::ADvar<double>>::scalar_type,
620 NumberTypes::sacado_rad>
624 # ifdef DEAL_II_TRILINOS_CXX_SUPPORTS_SACADO_COMPLEX_RAD 633 template <
typename ADNumberType>
634 struct ADNumberTraits<
636 typename
std::enable_if<std::is_same<
638 std::complex<Sacado::Rad::ADvar<typename ADNumberType::value_type::
639 ADVari::scalar_type>>>::value>::type>
641 std::complex<typename ADNumberType::value_type::ADVari::scalar_type>,
642 NumberTypes::sacado_rad>
651 struct NumberTraits<
std::complex<Sacado::Rad::ADvar<float>>,
653 : NumberTraits<typename ADNumberTraits<
654 std::complex<Sacado::Rad::ADvar<float>>>::scalar_type,
655 NumberTypes::sacado_rad>
664 struct NumberTraits<
std::complex<Sacado::Rad::ADvar<double>>,
666 : NumberTraits<typename ADNumberTraits<
667 std::complex<Sacado::Rad::ADvar<double>>>::scalar_type,
668 NumberTypes::sacado_rad>
684 template <
typename ADNumberType>
685 struct ADNumberTraits<
687 typename
std::enable_if<
688 std::is_same<ADNumberType,
689 Sacado::Fad::DFad<Sacado::Fad::DFad<
690 typename ADNumberType::scalar_type>>>::value>::type>
691 : NumberTraits<typename ADNumberType::scalar_type,
692 NumberTypes::sacado_dfad_dfad>
703 template <
typename ADNumberType>
704 struct ADNumberTraits<
706 typename
std::enable_if<std::is_same<
708 std::complex<Sacado::Fad::DFad<Sacado::Fad::DFad<
709 typename ADNumberType::value_type::scalar_type>>>>::value>::type>
711 std::complex<typename ADNumberType::value_type::scalar_type>,
712 NumberTypes::sacado_dfad_dfad>
721 struct NumberTraits<Sacado::Fad::DFad<Sacado::Fad::DFad<float>>,
723 : NumberTraits<typename ADNumberTraits<Sacado::Fad::DFad<
724 Sacado::Fad::DFad<float>>>::scalar_type,
725 NumberTypes::sacado_dfad_dfad>
735 std::complex<Sacado::Fad::DFad<Sacado::Fad::DFad<float>>>,
737 : NumberTraits<typename ADNumberTraits<std::complex<Sacado::Fad::DFad<
738 Sacado::Fad::DFad<float>>>>::scalar_type,
739 NumberTypes::sacado_dfad_dfad>
748 struct NumberTraits<Sacado::Fad::DFad<Sacado::Fad::DFad<double>>,
750 : NumberTraits<typename ADNumberTraits<Sacado::Fad::DFad<
751 Sacado::Fad::DFad<double>>>::scalar_type,
752 NumberTypes::sacado_dfad_dfad>
762 std::complex<Sacado::Fad::DFad<Sacado::Fad::DFad<double>>>,
764 : NumberTraits<typename ADNumberTraits<std::complex<Sacado::Fad::DFad<
765 Sacado::Fad::DFad<double>>>>::scalar_type,
766 NumberTypes::sacado_dfad_dfad>
779 template <
typename ADNumberType>
780 struct ADNumberTraits<
782 typename
std::enable_if<std::is_same<
784 Sacado::Rad::ADvar<Sacado::Fad::DFad<
785 typename ADNumberType::ADVari::scalar_type>>>::value>::type>
786 : NumberTraits<typename ADNumberType::ADVari::scalar_type,
787 NumberTypes::sacado_rad_dfad>
796 struct NumberTraits<Sacado::Rad::ADvar<Sacado::Fad::DFad<float>>,
798 : NumberTraits<typename ADNumberTraits<Sacado::Rad::ADvar<
799 Sacado::Fad::DFad<float>>>::scalar_type,
800 NumberTypes::sacado_rad_dfad>
809 struct NumberTraits<Sacado::Rad::ADvar<Sacado::Fad::DFad<double>>,
811 : NumberTraits<typename ADNumberTraits<Sacado::Rad::ADvar<
812 Sacado::Fad::DFad<double>>>::scalar_type,
813 NumberTypes::sacado_rad_dfad>
817 # ifdef DEAL_II_TRILINOS_CXX_SUPPORTS_SACADO_COMPLEX_RAD 827 template <
typename ADNumberType>
828 struct ADNumberTraits<
830 typename
std::enable_if<std::is_same<
832 std::complex<Sacado::Rad::ADvar<Sacado::Fad::DFad<
833 typename ADNumberType::value_type::ADVari::scalar_type>>>>::value>::
836 std::complex<typename ADNumberType::value_type::ADVari::scalar_type>,
837 NumberTypes::sacado_rad_dfad>
847 std::complex<Sacado::Rad::ADvar<Sacado::Fad::DFad<float>>>,
849 : NumberTraits<typename ADNumberTraits<std::complex<Sacado::Rad::ADvar<
850 Sacado::Fad::DFad<float>>>>::scalar_type,
851 NumberTypes::sacado_rad_dfad>
861 std::complex<Sacado::Rad::ADvar<Sacado::Fad::DFad<double>>>,
863 : NumberTraits<typename ADNumberTraits<std::complex<Sacado::Rad::ADvar<
864 Sacado::Fad::DFad<double>>>>::scalar_type,
865 NumberTypes::sacado_rad_dfad>
875 template <
typename NumberType>
876 struct is_sacado_dfad_number<
878 typename
std::enable_if<
879 ADNumberTraits<typename std::decay<NumberType>::type>::type_code ==
880 NumberTypes::sacado_dfad ||
881 ADNumberTraits<typename std::decay<NumberType>::type>::type_code ==
882 NumberTypes::sacado_dfad_dfad>::type> : std::true_type
886 template <
typename NumberType>
887 struct is_sacado_dfad_number<
889 typename
std::enable_if<std::is_same<
891 Sacado::Fad::Expr<typename NumberType::value_type>>::value>::type>
896 template <
typename NumberType>
897 struct is_sacado_rad_number<
899 typename
std::enable_if<
900 ADNumberTraits<typename std::decay<NumberType>::type>::type_code ==
901 NumberTypes::sacado_rad ||
902 ADNumberTraits<typename std::decay<NumberType>::type>::type_code ==
903 NumberTypes::sacado_rad_dfad>::type> : std::true_type
907 template <
typename NumberType>
908 struct is_sacado_rad_number<
910 typename
std::enable_if<std::is_same<
912 Sacado::Rad::ADvari<Sacado::Fad::DFad<
913 typename NumberType::ADVari::scalar_type>>>::value>::type>
918 template <
typename NumberType>
919 struct is_sacado_number<
921 typename
std::enable_if<is_sacado_dfad_number<NumberType>::value ||
922 is_sacado_rad_number<NumberType>::value>::type>
932 template <
typename NumberType>
933 struct is_cuda_compatible<
935 typename
std::enable_if<::Differentiation::AD::is_sacado_rad_number<
936 NumberType>::value>::type> : std::false_type
944 DEAL_II_NAMESPACE_CLOSE
947 #endif // DEAL_II_TRILINOS_WITH_SACADO