18#if defined(DEAL_II_WITH_ADOLC) || defined(DEAL_II_TRILINOS_WITH_SACADO)
33# ifdef DEAL_II_WITH_ADOLC
34# include <adolc/adolc_fatalerror.h>
35# include <adolc/drivers/drivers.h>
36# include <adolc/taping.h>
52 template <
typename ADNumberType,
typename ScalarType,
typename T>
61 template <
typename ADNumberType,
typename ScalarType,
typename T>
70 template <
typename ADNumberType,
typename ScalarType,
typename T>
80 template <
typename ADNumberType,
typename ScalarType,
typename T>
89 template <
typename ADNumberType,
typename ScalarType,
typename T>
101 template <
typename ADNumberType,
typename ScalarType,
typename T>
111 template <
typename ADNumberType,
typename ScalarType,
typename T>
121 template <
typename ADNumberType,
typename ScalarType,
typename T>
122 std::vector<typename Types<ADNumberType>::tape_index>
127 return std::vector<typename Types<ADNumberType>::tape_index>();
131 template <
typename ADNumberType,
typename ScalarType,
typename T>
140 template <
typename ADNumberType,
typename ScalarType,
typename T>
150 template <
typename ADNumberType,
typename ScalarType,
typename T>
160 template <
typename ADNumberType,
typename ScalarType,
typename T>
169 template <
typename ADNumberType,
typename ScalarType,
typename T>
177 template <
typename ADNumberType,
typename ScalarType,
typename T>
185 template <
typename ADNumberType,
typename ScalarType,
typename T>
189 std::ostream &)
const
195 template <
typename ADNumberType,
typename ScalarType,
typename T>
199 const std::vector<ScalarType> &)
const
202 return ScalarType(0.0);
206 template <
typename ADNumberType,
typename ScalarType,
typename T>
210 const std::vector<ScalarType> &,
217 template <
typename ADNumberType,
typename ScalarType,
typename T>
221 const std::vector<ScalarType> &,
228 template <
typename ADNumberType,
typename ScalarType,
typename T>
233 const std::vector<ScalarType> &,
240 template <
typename ADNumberType,
typename ScalarType,
typename T>
245 const std::vector<ScalarType> &,
253# ifdef DEAL_II_WITH_ADOLC
258 template <
typename ADNumberType>
261 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
263 : active_tape(
Numbers<ADNumberType>::invalid_tape_index)
265 , is_recording_flag(false)
266 , use_stored_taped_buffer_sizes(false)
275 template <
typename ADNumberType>
279 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
280 NumberTypes::adolc_taped>>::is_recording()
283 return is_recording_flag;
287 template <
typename ADNumberType>
292 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
293 NumberTypes::adolc_taped>>::active_tape_index()
const
299 template <
typename ADNumberType>
301 TapedDrivers<ADNumberType,
303 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
304 NumberTypes::adolc_taped>>::
305 keep_independent_values()
const
311 template <
typename ADNumberType>
315 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
316 NumberTypes::adolc_taped>>::
362 const std::vector<typename Types<ADNumberType>::tape_index>
363 registered_tape_indices = get_registered_tape_indices();
364 const auto it = std::find(registered_tape_indices.begin(),
365 registered_tape_indices.end(),
367 if (it == registered_tape_indices.end())
373 std::vector<std::size_t> counts(STAT_SIZE);
374 ::tapestats(tape_index, counts.data());
377 catch (const ::FatalError &exc)
384 template <
typename ADNumberType>
388 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
389 NumberTypes::adolc_taped>>::
390 set_tape_buffer_sizes(
398 obufsize = in_obufsize;
399 lbufsize = in_lbufsize;
400 vbufsize = in_vbufsize;
401 tbufsize = in_tbufsize;
402 use_stored_taped_buffer_sizes =
true;
406 template <
typename ADNumberType>
410 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
411 NumberTypes::adolc_taped>>::
413 const bool keep_independent_values)
415 if (use_stored_taped_buffer_sizes)
417 keep_independent_values,
423 trace_on(tape_index, keep_independent_values);
426 keep_values = keep_independent_values;
427 is_recording_flag =
true;
431 template <
typename ADNumberType>
435 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
436 NumberTypes::adolc_taped>>::
439 const bool write_tapes_to_file)
441 if (write_tapes_to_file)
442 trace_off(active_tape_index);
448 is_recording_flag =
false;
454 if (keep_independent_values() ==
false)
459 template <
typename ADNumberType>
460 std::vector<typename Types<ADNumberType>::tape_index>
463 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
464 NumberTypes::adolc_taped>>::
465 get_registered_tape_indices()
const
471 std::vector<short> registered_tape_indices_s;
472 cachedTraceTags(registered_tape_indices_s);
474 return std::vector<typename Types<ADNumberType>::tape_index>(
475 registered_tape_indices_s.begin(), registered_tape_indices_s.end());
479 template <
typename ADNumberType>
483 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
484 NumberTypes::adolc_taped>>::
487 active_tape = tape_index;
491 template <
typename ADNumberType>
495 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
496 NumberTypes::adolc_taped>>::
501 is_registered_tape(tape_index) ==
true,
503 "Cannot ask for the status of a tape that has not yet been recorded and used."));
505 const auto it_status_tape = status.find(tape_index);
517 if (it_status_tape == status.end())
520 const auto status_tape = it_status_tape->second;
525 status_tape < 4 && status_tape >= -2,
527 "The tape status is not within the range specified within the ADOL-C documentation."));
528 return (status_tape < 0);
532 template <
typename ADNumberType>
536 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
537 NumberTypes::adolc_taped>>::
538 last_action_requires_retaping()
const
540 return requires_retaping(active_tape);
544 template <
typename ADNumberType>
548 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
549 NumberTypes::adolc_taped>>::
552 Assert(is_registered_tape(tape_index),
554 "This tape does not exist, and therefore cannot be cleared."));
555 removeTape(tape_index, TapeRemovalType::ADOLC_REMOVE_COMPLETELY);
559 template <
typename ADNumberType>
563 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
564 NumberTypes::adolc_taped>>::
565 reset(
const bool clear_registered_tapes)
568 is_recording_flag =
false;
570 if (clear_registered_tapes)
572 const std::vector<typename Types<ADNumberType>::tape_index>
573 registered_tape_indices = get_registered_tape_indices();
574 for (
const auto &tape_index : registered_tape_indices)
575 remove_tape(tape_index);
580 template <
typename ADNumberType>
582 TapedDrivers<ADNumberType,
584 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
585 NumberTypes::adolc_taped>>::print(std::ostream
589 const std::vector<typename Types<ADNumberType>::tape_index>
590 registered_tape_indices = get_registered_tape_indices();
591 stream <<
"Registered tapes and their status: ";
592 auto it_registered_tape = registered_tape_indices.begin();
593 for (
unsigned int i = 0; i < registered_tape_indices.size();
594 ++i, ++it_registered_tape)
596 const auto tape_index = *it_registered_tape;
597 const auto it_status_tape = status.find(tape_index);
598 Assert(it_status_tape != status.end(),
600 "This tape's status has not been found in the map."));
601 const auto status_tape = it_status_tape->second;
603 stream << tape_index <<
"->" << status_tape
604 << (i < (registered_tape_indices.size() - 1) ?
"," :
"");
608 stream <<
"Keep values? " << keep_independent_values() <<
'\n';
609 stream <<
"Use stored tape buffer sizes? "
610 << use_stored_taped_buffer_sizes <<
'\n';
614 template <
typename ADNumberType>
618 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
619 NumberTypes::adolc_taped>>::
622 std::ostream & stream)
const
626 std::vector<std::size_t> counts(STAT_SIZE);
627 ::tapestats(tape_index, counts.data());
630 <<
"Tape index: " << tape_index <<
'\n'
631 <<
"Number of independent variables: " << counts[0] <<
'\n'
632 <<
"Number of dependent variables: " << counts[1] <<
'\n'
633 <<
"Max number of live, active variables: " << counts[2] <<
'\n'
634 <<
"Size of taylor stack (number of overwrites): " << counts[3] <<
'\n'
635 <<
"Operations buffer size: " << counts[4] <<
'\n'
636 <<
"Total number of recorded operations: " << counts[5] <<
'\n'
637 <<
"Operations file written or not: " << counts[6] <<
'\n'
638 <<
"Overall number of locations: " << counts[7] <<
'\n'
639 <<
"Locations file written or not: " << counts[8] <<
'\n'
640 <<
"Overall number of values: " << counts[9] <<
'\n'
641 <<
"Values file written or not: " << counts[10] <<
'\n'
642 <<
"Locations buffer size: " << counts[11] <<
'\n'
643 <<
"Values buffer size: " << counts[12] <<
'\n'
644 <<
"Taylor buffer size: " << counts[13] <<
'\n'
645 <<
"Number of eq_*_prod for sparsity pattern: " << counts[14] <<
'\n'
646 <<
"Use of 'min_op', deferred to 'abs_op' for piecewise calculations: "
647 << counts[15] <<
'\n'
648 <<
"Number of 'abs' calls that can switch branch: " << counts[16]
650 <<
"Number of parameters (doubles) interchangeable without retaping: "
651 << counts[17] <<
'\n'
656 template <
typename ADNumberType>
660 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
661 NumberTypes::adolc_taped>>::scalar_type
664 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
665 NumberTypes::adolc_taped>>::
667 const std::vector<scalar_type> &independent_variables)
const
669 Assert(is_registered_tape(active_tape_index),
670 ExcMessage(
"This tape has not yet been recorded."));
672 scalar_type value = 0.0;
674 status[active_tape_index] =
675 ::function(active_tape_index,
677 independent_variables.size(),
678 const_cast<double *
>(independent_variables.data()),
685 template <
typename ADNumberType>
689 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
690 NumberTypes::adolc_taped>>::
692 const std::vector<scalar_type> &independent_variables,
697 ExcSupportedDerivativeLevels(
700 Assert(gradient.size() == independent_variables.size(),
702 independent_variables.size()));
703 Assert(is_registered_tape(active_tape_index),
704 ExcMessage(
"This tape has not yet been recorded."));
709 status[active_tape_index] =
710 ::gradient(active_tape_index,
711 independent_variables.size(),
712 const_cast<scalar_type *
>(independent_variables.data()),
717 template <
typename ADNumberType>
719 TapedDrivers<ADNumberType,
721 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
722 NumberTypes::adolc_taped>>::
723 hessian(
const typename Types<ADNumberType>::tape_index active_tape_index,
724 const std::vector<scalar_type> &independent_variables,
729 ExcSupportedDerivativeLevels(
732 Assert(hessian.m() == independent_variables.size(),
734 Assert(hessian.n() == independent_variables.size(),
736 Assert(is_registered_tape(active_tape_index),
737 ExcMessage(
"This tape has not yet been recorded."));
739 const unsigned int n_independent_variables = independent_variables.size();
740 std::vector<scalar_type *> H(n_independent_variables);
741 for (
unsigned int i = 0; i < n_independent_variables; ++i)
742 H[i] = &hessian[i][0];
744 status[active_tape_index] =
745 ::hessian(active_tape_index,
746 n_independent_variables,
747 const_cast<scalar_type *
>(independent_variables.data()),
753 for (
unsigned int i = 0; i < n_independent_variables; ++i)
754 for (
unsigned int j = 0; j < i; ++j)
755 hessian[j][i] = hessian[i][j];
759 template <
typename ADNumberType>
761 TapedDrivers<ADNumberType,
763 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
764 NumberTypes::adolc_taped>>::
765 values(
const typename Types<ADNumberType>::tape_index active_tape_index,
766 const unsigned int n_dependent_variables,
767 const std::vector<scalar_type> &independent_variables,
770 Assert(values.size() == n_dependent_variables,
772 Assert(is_registered_tape(active_tape_index),
773 ExcMessage(
"This tape has not yet been recorded."));
778 status[active_tape_index] =
779 ::function(active_tape_index,
780 n_dependent_variables,
781 independent_variables.size(),
782 const_cast<scalar_type *
>(independent_variables.data()),
787 template <
typename ADNumberType>
789 TapedDrivers<ADNumberType,
791 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
792 NumberTypes::adolc_taped>>::
793 jacobian(
const typename Types<ADNumberType>::tape_index active_tape_index,
794 const unsigned int n_dependent_variables,
795 const std::vector<scalar_type> &independent_variables,
798 Assert(AD::ADNumberTraits<ADNumberType>::n_supported_derivative_levels >=
800 ExcSupportedDerivativeLevels(
801 AD::ADNumberTraits<ADNumberType>::n_supported_derivative_levels,
803 Assert(jacobian.
m() == n_dependent_variables,
805 Assert(jacobian.
n() == independent_variables.size(),
807 Assert(is_registered_tape(active_tape_index),
808 ExcMessage(
"This tape has not yet been recorded."));
810 std::vector<scalar_type *> J(n_dependent_variables);
811 for (
unsigned int i = 0; i < n_dependent_variables; ++i)
812 J[i] = &jacobian[i][0];
814 status[active_tape_index] = ::jacobian(active_tape_index,
815 n_dependent_variables,
816 independent_variables.size(),
817 independent_variables.data(),
825 template <
typename ADNumberType>
827 TapedDrivers<ADNumberType,
829 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
830 NumberTypes::adolc_taped>>::is_recording()
838 template <
typename ADNumberType>
839 typename Types<ADNumberType>::tape_index
843 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
844 NumberTypes::adolc_taped>>::active_tape_index()
const
847 return Numbers<ADNumberType>::invalid_tape_index;
851 template <
typename ADNumberType>
853 TapedDrivers<ADNumberType,
855 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
856 NumberTypes::adolc_taped>>::
857 keep_independent_values()
const
864 template <
typename ADNumberType>
866 TapedDrivers<ADNumberType,
868 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
869 NumberTypes::adolc_taped>>::
870 is_registered_tape(
const typename Types<ADNumberType>::tape_index)
const
877 template <
typename ADNumberType>
879 TapedDrivers<ADNumberType,
881 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
882 NumberTypes::adolc_taped>>::
883 set_tape_buffer_sizes(
884 const typename Types<ADNumberType>::tape_buffer_sizes,
885 const typename Types<ADNumberType>::tape_buffer_sizes,
886 const typename Types<ADNumberType>::tape_buffer_sizes,
887 const typename Types<ADNumberType>::tape_buffer_sizes)
893 template <
typename ADNumberType>
895 TapedDrivers<ADNumberType,
897 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
898 NumberTypes::adolc_taped>>::
899 start_taping(
const typename Types<ADNumberType>::tape_index,
const bool)
905 template <
typename ADNumberType>
907 TapedDrivers<ADNumberType,
909 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
910 NumberTypes::adolc_taped>>::
911 stop_taping(
const typename Types<ADNumberType>::tape_index,
const bool)
917 template <
typename ADNumberType>
918 std::vector<typename Types<ADNumberType>::tape_index>
919 TapedDrivers<ADNumberType,
921 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
922 NumberTypes::adolc_taped>>::
923 get_registered_tape_indices()
const
926 return std::vector<typename Types<ADNumberType>::tape_index>();
930 template <
typename ADNumberType>
932 TapedDrivers<ADNumberType,
934 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
935 NumberTypes::adolc_taped>>::
936 activate_tape(
const typename Types<ADNumberType>::tape_index)
942 template <
typename ADNumberType>
944 TapedDrivers<ADNumberType,
946 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
947 NumberTypes::adolc_taped>>::
948 requires_retaping(
const typename Types<ADNumberType>::tape_index)
const
955 template <
typename ADNumberType>
957 TapedDrivers<ADNumberType,
959 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
960 NumberTypes::adolc_taped>>::
961 last_action_requires_retaping()
const
968 template <
typename ADNumberType>
970 TapedDrivers<ADNumberType,
972 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
973 NumberTypes::adolc_taped>>::
974 remove_tape(
const typename Types<ADNumberType>::tape_index)
980 template <
typename ADNumberType>
982 TapedDrivers<ADNumberType,
984 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
985 NumberTypes::adolc_taped>>::reset(
const bool)
991 template <
typename ADNumberType>
993 TapedDrivers<ADNumberType,
995 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
996 NumberTypes::adolc_taped>>::print(std::ostream
1003 template <
typename ADNumberType>
1005 TapedDrivers<ADNumberType,
1007 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
1008 NumberTypes::adolc_taped>>::
1009 print_tape_stats(
const typename Types<ADNumberType>::tape_index,
1010 std::ostream &)
const
1016 template <
typename ADNumberType>
1017 typename TapedDrivers<
1020 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
1021 NumberTypes::adolc_taped>>::scalar_type
1022 TapedDrivers<ADNumberType,
1024 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
1025 NumberTypes::adolc_taped>>::
1026 value(
const typename Types<ADNumberType>::tape_index,
1027 const std::vector<scalar_type> &)
const
1034 template <
typename ADNumberType>
1036 TapedDrivers<ADNumberType,
1038 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
1039 NumberTypes::adolc_taped>>::
1040 gradient(
const typename Types<ADNumberType>::tape_index,
1041 const std::vector<scalar_type> &,
1048 template <
typename ADNumberType>
1050 TapedDrivers<ADNumberType,
1052 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
1053 NumberTypes::adolc_taped>>::
1054 hessian(
const typename Types<ADNumberType>::tape_index,
1055 const std::vector<scalar_type> &,
1062 template <
typename ADNumberType>
1064 TapedDrivers<ADNumberType,
1066 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
1067 NumberTypes::adolc_taped>>::
1068 values(
const typename Types<ADNumberType>::tape_index,
1070 const std::vector<scalar_type> &,
1077 template <
typename ADNumberType>
1079 TapedDrivers<ADNumberType,
1081 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
1082 NumberTypes::adolc_taped>>::
1083 jacobian(
const typename Types<ADNumberType>::tape_index,
1085 const std::vector<scalar_type> &,
1097 template <
typename ADNumberType>
1099 TapedDrivers<ADNumberType,
1101 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
1102 NumberTypes::adolc_taped>>::is_recording()
1107 return taped_driver.is_recording();
1111 template <
typename ADNumberType>
1112 typename Types<ADNumberType>::tape_index
1116 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
1117 NumberTypes::adolc_taped>>::active_tape_index()
const
1121 return taped_driver.active_tape_index();
1125 template <
typename ADNumberType>
1127 TapedDrivers<ADNumberType,
1129 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
1130 NumberTypes::adolc_taped>>::
1131 keep_independent_values()
const
1133 return taped_driver.keep_independent_values();
1137 template <
typename ADNumberType>
1139 TapedDrivers<ADNumberType,
1141 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
1142 NumberTypes::adolc_taped>>::
1144 const typename Types<ADNumberType>::tape_index tape_index)
const
1148 return taped_driver.is_registered_tape(tape_index);
1152 template <
typename ADNumberType>
1154 TapedDrivers<ADNumberType,
1156 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
1157 NumberTypes::adolc_taped>>::
1158 set_tape_buffer_sizes(
1159 const typename Types<ADNumberType>::tape_buffer_sizes obufsize,
1160 const typename Types<ADNumberType>::tape_buffer_sizes lbufsize,
1161 const typename Types<ADNumberType>::tape_buffer_sizes vbufsize,
1162 const typename Types<ADNumberType>::tape_buffer_sizes tbufsize)
1166 taped_driver.set_tape_buffer_sizes(obufsize,
1173 template <
typename ADNumberType>
1175 TapedDrivers<ADNumberType,
1177 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
1178 NumberTypes::adolc_taped>>::
1179 start_taping(
const typename Types<ADNumberType>::tape_index tape_index,
1180 const bool keep_independent_values)
1184 taped_driver.start_taping(tape_index, keep_independent_values);
1188 template <
typename ADNumberType>
1190 TapedDrivers<ADNumberType,
1192 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
1193 NumberTypes::adolc_taped>>::
1195 const typename Types<ADNumberType>::tape_index active_tape_index,
1196 const bool write_tapes_to_file)
1200 taped_driver.stop_taping(active_tape_index, write_tapes_to_file);
1204 template <
typename ADNumberType>
1205 std::vector<typename Types<ADNumberType>::tape_index>
1206 TapedDrivers<ADNumberType,
1208 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
1209 NumberTypes::adolc_taped>>::
1210 get_registered_tape_indices()
const
1212 return taped_driver.get_registered_tape_indices();
1216 template <
typename ADNumberType>
1218 TapedDrivers<ADNumberType,
1220 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
1221 NumberTypes::adolc_taped>>::
1222 activate_tape(
const typename Types<ADNumberType>::tape_index tape_index)
1224 taped_driver.activate_tape(tape_index);
1228 template <
typename ADNumberType>
1230 TapedDrivers<ADNumberType,
1232 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
1233 NumberTypes::adolc_taped>>::
1235 const typename Types<ADNumberType>::tape_index tape_index)
const
1237 return taped_driver.requires_retaping(tape_index);
1241 template <
typename ADNumberType>
1243 TapedDrivers<ADNumberType,
1245 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
1246 NumberTypes::adolc_taped>>::
1247 last_action_requires_retaping()
const
1249 return taped_driver.last_action_requires_retaping();
1253 template <
typename ADNumberType>
1255 TapedDrivers<ADNumberType,
1257 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
1258 NumberTypes::adolc_taped>>::
1259 remove_tape(
const typename Types<ADNumberType>::tape_index tape_index)
1261 taped_driver.remove_tape(tape_index);
1265 template <
typename ADNumberType>
1267 TapedDrivers<ADNumberType,
1269 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
1270 NumberTypes::adolc_taped>>::
1271 reset(
const bool clear_registered_tapes)
1273 taped_driver.reset(clear_registered_tapes);
1277 template <
typename ADNumberType>
1279 TapedDrivers<ADNumberType,
1281 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
1282 NumberTypes::adolc_taped>>::print(std::ostream
1286 taped_driver.print(stream);
1290 template <
typename ADNumberType>
1292 TapedDrivers<ADNumberType,
1294 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
1295 NumberTypes::adolc_taped>>::
1297 const typename Types<ADNumberType>::tape_index tape_index,
1298 std::ostream & stream)
const
1302 taped_driver.print_tape_stats(tape_index, stream);
1306 template <
typename ADNumberType>
1307 typename TapedDrivers<
1310 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
1311 NumberTypes::adolc_taped>>::scalar_type
1312 TapedDrivers<ADNumberType,
1314 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
1315 NumberTypes::adolc_taped>>::
1316 value(
const typename Types<ADNumberType>::tape_index active_tape_index,
1317 const std::vector<scalar_type> &independent_variables)
const
1321 return taped_driver.value(active_tape_index,
1322 vector_float_to_double(independent_variables));
1326 template <
typename ADNumberType>
1328 TapedDrivers<ADNumberType,
1330 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
1331 NumberTypes::adolc_taped>>::
1332 gradient(
const typename Types<ADNumberType>::tape_index active_tape_index,
1333 const std::vector<scalar_type> &independent_variables,
1339 taped_driver.gradient(active_tape_index,
1340 vector_float_to_double(independent_variables),
1346 template <
typename ADNumberType>
1348 TapedDrivers<ADNumberType,
1350 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
1351 NumberTypes::adolc_taped>>::
1352 hessian(
const typename Types<ADNumberType>::tape_index active_tape_index,
1353 const std::vector<scalar_type> &independent_variables,
1359 taped_driver.hessian(active_tape_index,
1360 vector_float_to_double(independent_variables),
1366 template <
typename ADNumberType>
1368 TapedDrivers<ADNumberType,
1370 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
1371 NumberTypes::adolc_taped>>::
1372 values(
const typename Types<ADNumberType>::tape_index active_tape_index,
1373 const unsigned int n_dependent_variables,
1374 const std::vector<scalar_type> &independent_variables,
1380 taped_driver.values(active_tape_index,
1381 n_dependent_variables,
1382 vector_float_to_double(independent_variables),
1388 template <
typename ADNumberType>
1390 TapedDrivers<ADNumberType,
1392 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
1393 NumberTypes::adolc_taped>>::
1394 jacobian(
const typename Types<ADNumberType>::tape_index active_tape_index,
1395 const unsigned int n_dependent_variables,
1396 const std::vector<scalar_type> &independent_variables,
1402 taped_driver.jacobian(active_tape_index,
1403 n_dependent_variables,
1404 vector_float_to_double(independent_variables),
1406 jacobian = jacobian_double;
1411 template <
typename ADNumberType>
1413 TapedDrivers<ADNumberType,
1415 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
1416 NumberTypes::adolc_taped>>::
1417 vector_float_to_double(
const std::vector<float> &in)
const
1419 std::vector<double> out(in.size());
1420 std::copy(in.begin(), in.end(), out.begin());
1429 template <
typename ADNumberType,
typename ScalarType,
typename T>
1434 AssertThrow(
false, ExcRequiresADNumberSpecialization());
1437 template <
typename ADNumberType,
typename ScalarType,
typename T>
1442 AssertThrow(
false, ExcRequiresADNumberSpecialization());
1445 template <
typename ADNumberType,
typename ScalarType,
typename T>
1450 AssertThrow(
false, ExcRequiresADNumberSpecialization());
1453 template <
typename ADNumberType,
typename ScalarType,
typename T>
1458 AssertThrow(
false, ExcRequiresADNumberSpecialization());
1463 template <
typename ADNumberType,
typename ScalarType,
typename T>
1466 const std::vector<ADNumberType> &)
const
1468 AssertThrow(
false, ExcRequiresADNumberSpecialization());
1469 return ScalarType(0.0);
1473 template <
typename ADNumberType,
typename ScalarType,
typename T>
1476 const std::vector<ADNumberType> &,
1477 const std::vector<ADNumberType> &,
1480 AssertThrow(
false, ExcRequiresADNumberSpecialization());
1484 template <
typename ADNumberType,
typename ScalarType,
typename T>
1487 const std::vector<ADNumberType> &,
1488 const std::vector<ADNumberType> &,
1491 AssertThrow(
false, ExcRequiresADNumberSpecialization());
1495 template <
typename ADNumberType,
typename ScalarType,
typename T>
1498 const std::vector<ADNumberType> &,
1501 AssertThrow(
false, ExcRequiresADNumberSpecialization());
1505 template <
typename ADNumberType,
typename ScalarType,
typename T>
1508 const std::vector<ADNumberType> &,
1509 const std::vector<ADNumberType> &,
1512 AssertThrow(
false, ExcRequiresADNumberSpecialization());
1522 template <
typename ADNumberType>
1524 NumberTypes::sacado_rad ||
1526 NumberTypes::sacado_rad_dfad)>
1530# ifdef DEAL_II_TRILINOS_WITH_SACADO
1541 template <
typename ADNumberType>
1546 ADNumberType &dependent_variable)
1556 ADNumberType::Outvar_Gradcomp(dependent_variable);
1566 template <
typename ADNumberType>
1568 NumberTypes::adolc_tapeless)>
1573# ifdef DEAL_II_WITH_ADOLC
1583 template <
typename ADNumberType>
1584 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
1585 NumberTypes::adolc_tapeless>
1588# ifdef DEAL_II_ADOLC_WITH_TAPELESS_REFCOUNTING
1603 const std::size_t n_live_variables = adtl::refcounter::getNumLiveVar();
1604 if (n_live_variables == 0)
1606 adtl::setNumDir(n_directional_derivatives);
1615 const std::size_t n_set_directional_derivatives = adtl::getNumDir();
1616 if (n_directional_derivatives > n_set_directional_derivatives)
1618 n_live_variables == 0,
1620 "There are currently " + std::to_string(n_live_variables) +
1622 "adtl::adouble variables in existence. They currently "
1624 std::to_string(n_set_directional_derivatives) +
1625 " directional derivatives "
1626 "but you wish to increase this to " +
1627 std::to_string(n_directional_derivatives) +
1629 "To safely change (or more specifically in this case, "
1630 "increase) the number of directional derivatives, there "
1631 "must be no tapeless doubles in local/global scope."));
1637 adtl::setNumDir(n_directional_derivatives);
1643 template <
typename ADNumberType>
1644 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
1645 NumberTypes::adolc_tapeless>
1646 configure_tapeless_mode(
const unsigned int )
1662 template <
typename ADNumberType,
typename ScalarType>
1666 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
1667 NumberTypes::sacado_rad ||
1668 ADNumberTraits<ADNumberType>::type_code ==
1669 NumberTypes::sacado_rad_dfad>>::TapelessDrivers()
1670 : dependent_variable_marking_safe(false)
1675 template <
typename ADNumberType,
typename ScalarType>
1677 TapelessDrivers<ADNumberType,
1679 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
1681 ADNumberTraits<ADNumberType>::type_code ==
1683 initialize_global_environment(
const unsigned int n_independent_variables)
1685 internal::configure_tapeless_mode<ADNumberType>(n_independent_variables);
1689 template <
typename ADNumberType,
typename ScalarType>
1696 ADNumberTraits<ADNumberType>::type_code ==
1699 dependent_variable_marking_safe =
true;
1703 template <
typename ADNumberType,
typename ScalarType>
1710 ADNumberTraits<ADNumberType>::type_code ==
1713 dependent_variable_marking_safe =
false;
1717 template <
typename ADNumberType,
typename ScalarType>
1719 TapelessDrivers<ADNumberType,
1721 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
1723 ADNumberTraits<ADNumberType>::type_code ==
1725 is_dependent_variable_marking_allowed()
const
1727 return dependent_variable_marking_safe;
1731 template <
typename ADNumberType,
typename ScalarType>
1733 TapelessDrivers<ADNumberType,
1735 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
1737 ADNumberTraits<ADNumberType>::type_code ==
1739 value(
const std::vector<ADNumberType> &dependent_variables)
const
1741 Assert(dependent_variables.size() == 1,
1743 return ADNumberTraits<ADNumberType>::get_scalar_value(
1744 dependent_variables[0]);
1748 template <
typename ADNumberType,
typename ScalarType>
1750 TapelessDrivers<ADNumberType,
1752 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
1754 ADNumberTraits<ADNumberType>::type_code ==
1756 gradient(
const std::vector<ADNumberType> &independent_variables,
1757 const std::vector<ADNumberType> &dependent_variables,
1760 Assert(AD::ADNumberTraits<ADNumberType>::n_supported_derivative_levels >=
1763 AD::ADNumberTraits<ADNumberType>::n_supported_derivative_levels,
1765 Assert(dependent_variables.size() == 1,
1769 independent_variables.size()));
1774 const_cast<ADNumberType &
>(dependent_variables[0]));
1775 const std::size_t n_independent_variables = independent_variables.size();
1776 for (
unsigned int i = 0; i < n_independent_variables; ++i)
1778 ADNumberTraits<ADNumberType>::get_directional_derivative(
1779 independent_variables[i], 0 ));
1783 template <
typename ADNumberType,
typename ScalarType>
1785 TapelessDrivers<ADNumberType,
1787 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
1789 ADNumberTraits<ADNumberType>::type_code ==
1791 hessian(
const std::vector<ADNumberType> &independent_variables,
1792 const std::vector<ADNumberType> &dependent_variables,
1795 Assert(AD::ADNumberTraits<ADNumberType>::n_supported_derivative_levels >=
1798 AD::ADNumberTraits<ADNumberType>::n_supported_derivative_levels,
1800 Assert(dependent_variables.size() == 1,
1810 const_cast<ADNumberType &
>(dependent_variables[0]));
1811 const std::size_t n_independent_variables = independent_variables.size();
1812 for (
unsigned int i = 0; i < n_independent_variables; ++i)
1814 using derivative_type =
1815 typename ADNumberTraits<ADNumberType>::derivative_type;
1816 const derivative_type gradient_i =
1817 ADNumberTraits<ADNumberType>::get_directional_derivative(
1818 independent_variables[i], i);
1820 for (
unsigned int j = 0; j <= i; ++j)
1825 const ScalarType hessian_ij =
1827 ADNumberTraits<derivative_type>::get_directional_derivative(
1837 template <
typename ADNumberType,
typename ScalarType>
1839 TapelessDrivers<ADNumberType,
1841 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
1843 ADNumberTraits<ADNumberType>::type_code ==
1845 values(
const std::vector<ADNumberType> &dependent_variables,
1851 const std::size_t n_dependent_variables = dependent_variables.size();
1852 for (
unsigned int i = 0; i < n_dependent_variables; ++i)
1853 values[i] = ADNumberTraits<ADNumberType>::get_scalar_value(
1854 dependent_variables[i]);
1858 template <
typename ADNumberType,
typename ScalarType>
1860 TapelessDrivers<ADNumberType,
1862 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
1864 ADNumberTraits<ADNumberType>::type_code ==
1866 jacobian(
const std::vector<ADNumberType> &independent_variables,
1867 const std::vector<ADNumberType> &dependent_variables,
1870 Assert(AD::ADNumberTraits<ADNumberType>::n_supported_derivative_levels >=
1873 AD::ADNumberTraits<ADNumberType>::n_supported_derivative_levels,
1875 Assert(jacobian.
m() == dependent_variables.size(),
1877 Assert(jacobian.
n() == independent_variables.size(),
1880 const std::size_t n_independent_variables = independent_variables.size();
1881 const std::size_t n_dependent_variables = dependent_variables.size();
1892 using accumulation_type =
1893 typename ADNumberTraits<ADNumberType>::derivative_type;
1894 std::vector<accumulation_type> rad_accumulation(
1895 n_independent_variables,
1898 for (
unsigned int i = 0; i < n_dependent_variables; ++i)
1901 const_cast<ADNumberType &
>(dependent_variables[i]));
1902 for (
unsigned int j = 0; j < n_independent_variables; ++j)
1904 const accumulation_type df_i_dx_j =
1905 ADNumberTraits<ADNumberType>::get_directional_derivative(
1906 independent_variables[j],
1908 rad_accumulation[j];
1911 rad_accumulation[j] += df_i_dx_j;
1923 template <
typename ADNumberType,
typename ScalarType>
1927 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
1929 ADNumberTraits<ADNumberType>::type_code ==
1931 ADNumberTraits<ADNumberType>::type_code ==
1933 : dependent_variable_marking_safe(false)
1938 template <
typename ADNumberType,
typename ScalarType>
1940 TapelessDrivers<ADNumberType,
1942 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
1944 ADNumberTraits<ADNumberType>::type_code ==
1946 ADNumberTraits<ADNumberType>::type_code ==
1948 initialize_global_environment(
const unsigned int n_independent_variables)
1950 internal::configure_tapeless_mode<ADNumberType>(n_independent_variables);
1954 template <
typename ADNumberType,
typename ScalarType>
1960 ADNumberTraits<ADNumberType>::type_code ==
1963 ADNumberTraits<ADNumberType>::type_code ==
1966 dependent_variable_marking_safe =
true;
1970 template <
typename ADNumberType,
typename ScalarType>
1976 ADNumberTraits<ADNumberType>::type_code ==
1979 ADNumberTraits<ADNumberType>::type_code ==
1982 dependent_variable_marking_safe =
false;
1986 template <
typename ADNumberType,
typename ScalarType>
1988 TapelessDrivers<ADNumberType,
1990 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
1992 ADNumberTraits<ADNumberType>::type_code ==
1994 ADNumberTraits<ADNumberType>::type_code ==
1996 is_dependent_variable_marking_allowed()
const
1998 return dependent_variable_marking_safe;
2002 template <
typename ADNumberType,
typename ScalarType>
2004 TapelessDrivers<ADNumberType,
2006 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
2008 ADNumberTraits<ADNumberType>::type_code ==
2010 ADNumberTraits<ADNumberType>::type_code ==
2012 value(
const std::vector<ADNumberType> &dependent_variables)
const
2014 Assert(dependent_variables.size() == 1,
2016 return ADNumberTraits<ADNumberType>::get_scalar_value(
2017 dependent_variables[0]);
2021 template <
typename ADNumberType,
typename ScalarType>
2023 TapelessDrivers<ADNumberType,
2025 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
2027 ADNumberTraits<ADNumberType>::type_code ==
2029 ADNumberTraits<ADNumberType>::type_code ==
2031 gradient(
const std::vector<ADNumberType> &independent_variables,
2032 const std::vector<ADNumberType> &dependent_variables,
2035 Assert(AD::ADNumberTraits<ADNumberType>::n_supported_derivative_levels >=
2038 AD::ADNumberTraits<ADNumberType>::n_supported_derivative_levels,
2040 Assert(dependent_variables.size() == 1,
2044 independent_variables.size()));
2048 const std::size_t n_independent_variables = independent_variables.size();
2049 for (
unsigned int i = 0; i < n_independent_variables; ++i)
2051 ADNumberTraits<ADNumberType>::get_directional_derivative(
2052 dependent_variables[0], i));
2056 template <
typename ADNumberType,
typename ScalarType>
2058 TapelessDrivers<ADNumberType,
2060 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
2062 ADNumberTraits<ADNumberType>::type_code ==
2064 ADNumberTraits<ADNumberType>::type_code ==
2066 hessian(
const std::vector<ADNumberType> &independent_variables,
2067 const std::vector<ADNumberType> &dependent_variables,
2070 Assert(AD::ADNumberTraits<ADNumberType>::n_supported_derivative_levels >=
2073 AD::ADNumberTraits<ADNumberType>::n_supported_derivative_levels,
2075 Assert(dependent_variables.size() == 1,
2084 const std::size_t n_independent_variables = independent_variables.size();
2085 for (
unsigned int i = 0; i < n_independent_variables; ++i)
2087 using derivative_type =
2088 typename ADNumberTraits<ADNumberType>::derivative_type;
2089 const derivative_type gradient_i =
2090 ADNumberTraits<ADNumberType>::get_directional_derivative(
2091 dependent_variables[0], i);
2093 for (
unsigned int j = 0; j <= i; ++j)
2098 const ScalarType hessian_ij =
2100 ADNumberTraits<derivative_type>::get_directional_derivative(
2110 template <
typename ADNumberType,
typename ScalarType>
2112 TapelessDrivers<ADNumberType,
2114 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
2116 ADNumberTraits<ADNumberType>::type_code ==
2118 ADNumberTraits<ADNumberType>::type_code ==
2120 values(
const std::vector<ADNumberType> &dependent_variables,
2126 const std::size_t n_dependent_variables = dependent_variables.size();
2127 for (
unsigned int i = 0; i < n_dependent_variables; ++i)
2128 values[i] = ADNumberTraits<ADNumberType>::get_scalar_value(
2129 dependent_variables[i]);
2133 template <
typename ADNumberType,
typename ScalarType>
2135 TapelessDrivers<ADNumberType,
2137 std::enable_if_t<ADNumberTraits<ADNumberType>::type_code ==
2139 ADNumberTraits<ADNumberType>::type_code ==
2141 ADNumberTraits<ADNumberType>::type_code ==
2143 jacobian(
const std::vector<ADNumberType> &independent_variables,
2144 const std::vector<ADNumberType> &dependent_variables,
2147 Assert(AD::ADNumberTraits<ADNumberType>::n_supported_derivative_levels >=
2150 AD::ADNumberTraits<ADNumberType>::n_supported_derivative_levels,
2152 Assert(jacobian.
m() == dependent_variables.size(),
2154 Assert(jacobian.
n() == independent_variables.size(),
2157 const std::size_t n_independent_variables = independent_variables.size();
2158 const std::size_t n_dependent_variables = dependent_variables.size();
2162 for (
unsigned int i = 0; i < n_dependent_variables; ++i)
2163 for (
unsigned int j = 0; j < n_independent_variables; ++j)
2165 ADNumberTraits<ADNumberType>::get_directional_derivative(
2166 dependent_variables[i], j));
2175# include "ad_drivers.inst"
2176# ifdef DEAL_II_WITH_ADOLC
2177# include "ad_drivers.inst1"
2179# ifdef DEAL_II_TRILINOS_WITH_SACADO
2180# include "ad_drivers.inst2"
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_CLOSE
static ::ExceptionBase & ExcSupportedDerivativeLevels(std::size_t arg1, std::size_t arg2)
static ::ExceptionBase & ExcRequiresADNumberSpecialization()
#define Assert(cond, exc)
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
std::enable_if_t<!(ADNumberTraits< ADNumberType >::type_code==NumberTypes::sacado_rad||ADNumberTraits< ADNumberType >::type_code==NumberTypes::sacado_rad_dfad)> reverse_mode_dependent_variable_activation(ADNumberType &)
std::enable_if_t<!(ADNumberTraits< ADNumberType >::type_code==NumberTypes::adolc_tapeless)> configure_tapeless_mode(const unsigned int)
void gradient(const typename Types< ADNumberType >::tape_index active_tape_index, const std::vector< ScalarType > &independent_variables, Vector< ScalarType > &gradient) const
bool is_recording() const
bool is_registered_tape(const typename Types< ADNumberType >::tape_index tape_index) const
void stop_taping(const typename Types< ADNumberType >::tape_index active_tape_index, const bool write_tapes_to_file)
void remove_tape(const typename Types< ADNumberType >::tape_index tape_index)
void start_taping(const typename Types< ADNumberType >::tape_index tape_index, const bool keep_independent_values)
void hessian(const typename Types< ADNumberType >::tape_index active_tape_index, const std::vector< ScalarType > &independent_variables, FullMatrix< ScalarType > &hessian) const
void set_tape_buffer_sizes(const typename Types< ADNumberType >::tape_buffer_sizes obufsize=64 *1024 *1024, const typename Types< ADNumberType >::tape_buffer_sizes lbufsize=64 *1024 *1024, const typename Types< ADNumberType >::tape_buffer_sizes vbufsize=64 *1024 *1024, const typename Types< ADNumberType >::tape_buffer_sizes tbufsize=64 *1024 *1024)
void activate_tape(const typename Types< ADNumberType >::tape_index tape_index)
bool last_action_requires_retaping() const
bool requires_retaping(const typename Types< ADNumberType >::tape_index tape_index) const
void print(std::ostream &stream) const
void print_tape_stats(const typename Types< ADNumberType >::tape_index tape_index, std::ostream &stream) const
ScalarType value(const typename Types< ADNumberType >::tape_index active_tape_index, const std::vector< ScalarType > &independent_variables) const
bool keep_independent_values() const
Types< ADNumberType >::tape_index active_tape_index() const
void reset(const bool clear_registered_tapes)
std::vector< typename Types< ADNumberType >::tape_index > get_registered_tape_indices() const
void values(const typename Types< ADNumberType >::tape_index active_tape_index, const unsigned int n_dependent_variables, const std::vector< ScalarType > &independent_variables, Vector< ScalarType > &values) const
void jacobian(const typename Types< ADNumberType >::tape_index active_tape_index, const unsigned int n_dependent_variables, const std::vector< ScalarType > &independent_variables, FullMatrix< ScalarType > &jacobian) const
static constexpr DEAL_II_HOST_DEVICE_ALWAYS_INLINE const T & value(const T &t)