18 #if defined(DEAL_II_WITH_ADOLC) || defined(DEAL_II_TRILINOS_WITH_SACADO) 23 # include <type_traits> 37 template <enum AD::NumberTypes ADNumberTypeCode,
typename ScalarType>
39 const unsigned int n_independent_variables,
40 const unsigned int n_dependent_variables)
41 : independent_variable_values(
42 n_independent_variables,
44 , registered_independent_variable_values(n_independent_variables, false)
45 , registered_marked_independent_variables(n_independent_variables, false)
46 , registered_marked_dependent_variables(n_dependent_variables, false)
55 "Floating point/arithmetic numbers have no derivatives."));
59 "The AD number type does not support the calculation of any derivatives."));
79 template <enum AD::NumberTypes ADNumberTypeCode,
typename ScalarType>
91 template <enum AD::NumberTypes ADNumberTypeCode,
typename ScalarType>
103 template <enum AD::NumberTypes ADNumberTypeCode,
typename ScalarType>
106 const unsigned int index,
119 "Cannot change the value of an independent variable " 120 "of the tapeless variety while this class is not set " 121 "in recording operations."));
132 "Trying to set the value of a non-existent independent variable."));
140 template <enum AD::NumberTypes ADNumberTypeCode,
typename ScalarType>
143 const unsigned int index,
155 "Need to extract sensitivities in the order they're created."));
164 "The marking of independent variables is only valid " 165 "during recording."));
178 template <enum AD::NumberTypes ADNumberTypeCode,
typename ScalarType>
185 ExcMessage(
"Not all values of sensitivities have been recorded!"));
202 template <enum AD::NumberTypes ADNumberTypeCode,
typename ScalarType>
215 "The initialization of non-sensitive independent variables is " 216 "only valid outside of recording operations."));
227 template <enum AD::NumberTypes ADNumberTypeCode,
typename ScalarType>
239 template <enum AD::NumberTypes ADNumberTypeCode,
typename ScalarType>
248 template <enum AD::NumberTypes ADNumberTypeCode,
typename ScalarType>
260 template <enum AD::NumberTypes ADNumberTypeCode,
typename ScalarType>
269 template <enum AD::NumberTypes ADNumberTypeCode,
typename ScalarType>
281 template <enum AD::NumberTypes ADNumberTypeCode,
typename ScalarType>
294 template <enum AD::NumberTypes ADNumberTypeCode,
typename ScalarType>
307 template <enum AD::NumberTypes ADNumberTypeCode,
typename ScalarType>
312 const std::ios_base::fmtflags stream_flags(stream.flags());
314 stream.setf(std::ios_base::boolalpha);
318 stream << std::flush;
323 stream <<
"Registered independent variables: " 330 stream <<
"Independent variable values: " 334 stream <<
"Registered marked independent variables: " 342 stream <<
"Dependent variable values: " 349 stream <<
"Registered dependent variables: " 357 stream.flags(stream_flags);
362 template <enum AD::NumberTypes ADNumberTypeCode,
typename ScalarType>
365 std::ostream &stream)
const 376 template <enum AD::NumberTypes ADNumberTypeCode,
typename ScalarType>
380 std::ostream & stream)
const 393 template <enum AD::NumberTypes ADNumberTypeCode,
typename ScalarType>
398 const bool clear_registered_tapes)
400 const unsigned int new_n_independent_variables =
404 const unsigned int new_n_dependent_variables =
436 new_n_independent_variables,
439 std::vector<bool>(new_n_independent_variables,
false);
441 std::vector<bool>(new_n_independent_variables,
false);
443 std::vector<ad_type>(new_n_dependent_variables,
446 std::vector<bool>(new_n_dependent_variables,
false);
451 template <enum AD::NumberTypes ADNumberTypeCode,
typename ScalarType>
455 const bool ensure_persistent_setting)
462 n_independent_variables);
464 if (ensure_persistent_setting ==
true)
477 template <enum AD::NumberTypes ADNumberTypeCode,
typename ScalarType>
487 template <enum AD::NumberTypes ADNumberTypeCode,
typename ScalarType>
500 template <enum AD::NumberTypes ADNumberTypeCode,
typename ScalarType>
513 template <enum AD::NumberTypes ADNumberTypeCode,
typename ScalarType>
525 template <enum AD::NumberTypes ADNumberTypeCode,
typename ScalarType>
529 const bool read_mode)
536 ExcMessage(
"Tape index exceeds maximum allowable value"));
543 if (read_mode ==
true)
550 ExcMessage(
"Not all dependent variables have been set!"));
562 template <enum AD::NumberTypes ADNumberTypeCode,
typename ScalarType>
581 template <enum AD::NumberTypes ADNumberTypeCode,
typename ScalarType>
585 const bool overwrite_tape,
586 const bool keep_independent_values)
589 const bool read_mode =
false;
593 if (overwrite_tape !=
true)
608 keep_independent_values);
619 "Tape recording is unexpectedly still enabled."));
646 template <enum AD::NumberTypes ADNumberTypeCode,
typename ScalarType>
649 const bool write_tapes_to_file)
655 ExcMessage(
"Not all values of sensitivities have been recorded!"));
668 ExcMessage(
"Not all dependent variables have been set!"));
682 template <enum AD::NumberTypes ADNumberTypeCode,
typename ScalarType>
685 const unsigned int index,
691 "This dependent variable has already been registered."));
699 "Must be recording when registering dependent variables."));
714 template <enum AD::NumberTypes ADNumberTypeCode,
typename ScalarType>
718 :
HelperBase<ADNumberTypeCode, ScalarType>(n_independent_variables,
719 n_dependent_variables)
724 template <enum AD::NumberTypes ADNumberTypeCode,
typename ScalarType>
727 const std::vector<scalar_type> &dof_values)
735 "Vector size does not match number of independent variables"));
739 ExcMessage(
"Independent variable value already registered."));
746 template <enum AD::NumberTypes ADNumberTypeCode,
typename ScalarType>
772 template <enum AD::NumberTypes ADNumberTypeCode,
typename ScalarType>
775 const std::vector<scalar_type> &values)
785 "Vector size does not match number of independent variables"));
797 template <enum AD::NumberTypes ADNumberTypeCode,
typename ScalarType>
800 :
CellLevelBase<ADNumberTypeCode, ScalarType>(n_independent_variables, 1)
805 template <enum AD::NumberTypes ADNumberTypeCode,
typename ScalarType>
817 template <enum AD::NumberTypes ADNumberTypeCode,
typename ScalarType>
829 "Not all values of sensitivities have been registered or subsequently set!"));
833 ExcMessage(
"Not all dependent variables have been registered."));
838 "The EnergyFunctional class expects there to be only one dependent variable."));
847 "Cannot compute value while tape is being recorded."));
871 template <enum AD::NumberTypes ADNumberTypeCode,
typename ScalarType>
874 Vector<scalar_type> &gradient)
const 884 "Not all values of sensitivities have been registered or subsequently set!"));
888 ExcMessage(
"Not all dependent variables have been registered."));
893 "The EnergyFunctional class expects there to be only one dependent variable."));
899 gradient.reinit(this->n_independent_variables(),
909 "Cannot compute gradient while tape is being recorded."));
936 template <enum AD::NumberTypes ADNumberTypeCode,
typename ScalarType>
943 "Cannot computed function Hessian: AD number type does " 944 "not support the calculation of second order derivatives."));
953 "Not all values of sensitivities have been registered or subsequently set!"));
957 ExcMessage(
"Not all dependent variables have been registered."));
962 "The EnergyFunctional class expects there to be only one dependent variable."));
969 hessian.
reinit({this->n_independent_variables(),
970 this->n_independent_variables()},
980 "Cannot compute hessian while tape is being recorded."));
1010 template <enum AD::NumberTypes ADNumberTypeCode,
typename ScalarType>
1014 :
CellLevelBase<ADNumberTypeCode, ScalarType>(n_independent_variables,
1015 n_dependent_variables)
1020 template <enum AD::NumberTypes ADNumberTypeCode,
typename ScalarType>
1027 "Vector size does not match number of dependent variables"));
1035 template <enum AD::NumberTypes ADNumberTypeCode,
typename ScalarType>
1038 Vector<scalar_type> &values)
const 1048 "Not all values of sensitivities have been registered or subsequently set!"));
1052 ExcMessage(
"Not all dependent variables have been registered."));
1058 values.reinit(this->n_dependent_variables(),
1068 "Cannot compute values while tape is being recorded."));
1089 template <enum AD::NumberTypes ADNumberTypeCode,
typename ScalarType>
1102 "Not all values of sensitivities have been registered or subsequently set!"));
1106 ExcMessage(
"Not all dependent variables have been registered."));
1113 jacobian.
reinit({this->n_dependent_variables(),
1114 this->n_independent_variables()},
1124 "Cannot compute hessian while tape is being recorded."));
1158 typename ScalarType>
1162 :
HelperBase<ADNumberTypeCode, ScalarType>(n_independent_variables,
1163 n_dependent_variables)
1164 , symmetric_independent_variables(n_independent_variables, false)
1171 typename ScalarType>
1176 const bool clear_registered_tapes)
1179 n_dependent_variables,
1180 clear_registered_tapes);
1182 const unsigned int new_n_independent_variables =
1187 std::vector<bool>(new_n_independent_variables,
false);
1194 typename ScalarType>
1208 typename ScalarType>
1222 typename ScalarType>
1233 "Vector size does not match number of independent variables"));
1237 ExcMessage(
"Independent variable value already registered."));
1246 typename ScalarType>
1276 typename ScalarType>
1280 const bool symmetric_component,
1288 "Trying to set the symmetry flag of a non-existent independent variable."));
1298 typename ScalarType>
1311 "Vector size does not match number of independent variables"));
1325 typename ScalarType>
1329 n_independent_variables,
1337 typename ScalarType>
1351 typename ScalarType>
1363 "Not all values of sensitivities have been registered or subsequently set!"));
1367 ExcMessage(
"Not all dependent variables have been registered."));
1372 "The ScalarFunction class expects there to be only one dependent variable."));
1381 "Cannot compute values while tape is being recorded."));
1401 typename ScalarType>
1404 Vector<scalar_type> &gradient)
const 1414 "Not all values of sensitivities have been registered or subsequently set!"));
1418 ExcMessage(
"Not all dependent variables have been registered."));
1423 "The ScalarFunction class expects there to be only one dependent variable."));
1429 gradient.reinit(this->n_independent_variables(),
1439 "Cannot compute gradient while tape is being recorded."));
1475 typename ScalarType>
1482 "Cannot computed function Hessian: AD number type does " 1483 "not support the calculation of second order derivatives."));
1492 "Not all values of sensitivities have been registered or subsequently set!"));
1496 ExcMessage(
"Not all dependent variables have been registered."));
1501 "The ScalarFunction class expects there to be only one dependent variable."));
1508 hessian.
reinit({this->n_independent_variables(),
1509 this->n_independent_variables()},
1519 "Cannot compute Hessian while tape is being recorded."));
1545 for (
unsigned int j = 0; j < i + 1; j++)
1550 hessian[i][j] *= 0.25;
1552 hessian[j][i] *= 0.25;
1559 hessian[i][j] *= 0.5;
1561 hessian[j][i] *= 0.5;
1570 typename ScalarType>
1589 const std::vector<unsigned int> row_index_set(
1590 internal::extract_field_component_indices<dim>(extractor_row));
1591 const std::vector<unsigned int> col_index_set(
1592 internal::extract_field_component_indices<dim>(extractor_col));
1598 hessian[row_index_set[0]][col_index_set[0]]);
1607 typename ScalarType>
1627 const std::vector<unsigned int> row_index_set(
1628 internal::extract_field_component_indices<dim>(extractor_row));
1629 const std::vector<unsigned int> col_index_set(
1630 internal::extract_field_component_indices<dim>(extractor_col));
1632 for (
unsigned int r = 0; r < row_index_set.size(); ++r)
1633 for (
unsigned int c = 0; c < col_index_set.size(); ++c)
1636 out, r, c, hessian[row_index_set[r]][col_index_set[c]]);
1650 typename ScalarType>
1655 n_independent_variables,
1656 n_dependent_variables)
1663 typename ScalarType>
1670 "Vector size does not match number of dependent variables"));
1680 typename ScalarType>
1683 Vector<scalar_type> &values)
const 1693 "Not all values of sensitivities have been registered or subsequently set!"));
1697 ExcMessage(
"Not all dependent variables have been registered."));
1703 values.reinit(this->n_dependent_variables(),
1713 "Cannot compute values while tape is being recorded."));
1736 typename ScalarType>
1749 "Not all values of sensitivities have been registered or subsequently set!"));
1753 ExcMessage(
"Not all dependent variables have been registered."));
1760 jacobian.
reinit({this->n_dependent_variables(),
1761 this->n_independent_variables()},
1771 "Cannot compute Jacobian while tape is being recorded."));
1803 jacobian[i][j] *= 0.5;
1811 typename ScalarType>
1831 const std::vector<unsigned int> row_index_set(
1832 internal::extract_field_component_indices<dim>(extractor_row));
1833 const std::vector<unsigned int> col_index_set(
1834 internal::extract_field_component_indices<dim>(extractor_col));
1840 jacobian[row_index_set[0]][col_index_set[0]]);
1849 typename ScalarType>
1869 const std::vector<unsigned int> row_index_set(
1870 internal::extract_field_component_indices<dim>(extractor_row));
1871 const std::vector<unsigned int> col_index_set(
1872 internal::extract_field_component_indices<dim>(extractor_col));
1874 for (
unsigned int r = 0; r < row_index_set.size(); ++r)
1875 for (
unsigned int c = 0; c < col_index_set.size(); ++c)
1878 out, r, c, jacobian[row_index_set[r]][col_index_set[c]]);
1890 # include "ad_helpers.inst" 1892 # ifdef DEAL_II_WITH_ADOLC 1893 # include "ad_helpers.inst1" 1895 # ifdef DEAL_II_TRILINOS_WITH_SACADO 1896 # include "ad_helpers.inst2" 1902 #endif // defined(DEAL_II_WITH_ADOLC) || defined(DEAL_II_TRILINOS_WITH_SACADO) 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
TapedDrivers< ad_type, scalar_type > taped_driver
virtual void reset(const unsigned int n_independent_variables=::numbers::invalid_unsigned_int, const unsigned int n_dependent_variables=::numbers::invalid_unsigned_int, const bool clear_registered_tapes=true)
bool is_recording() const
static const unsigned int invalid_unsigned_int
HelperBase(const unsigned int n_independent_variables, const unsigned int n_dependent_variables)
void values(const std::vector< ADNumberType > &dependent_variables, Vector< ScalarType > &values) const
PointLevelFunctionsBase(const unsigned int n_independent_variables, const unsigned int n_dependent_variables)
void compute_jacobian(FullMatrix< scalar_type > &jacobian) const
VectorFunction(const unsigned int n_independent_variables, const unsigned int n_dependent_variables)
void set_sensitivity_value(const unsigned int index, const scalar_type &value)
Types< ADNumberType >::tape_index active_tape_index() const
scalar_type compute_energy() const
static constexpr const T & value(const T &t)
void mark_independent_variable(const unsigned int index, ad_type &out) const
void compute_residual(Vector< scalar_type > &residual) const override
CellLevelBase(const unsigned int n_independent_variables, const unsigned int n_dependent_variables)
void register_independent_variables(const std::vector< scalar_type > &values)
typename HelperBase< ADNumberTypeCode, ScalarType >::scalar_type scalar_type
static internal::VectorFieldJacobian< dim, scalar_type, ExtractorType_Row, ExtractorType_Col >::type extract_jacobian_component(const FullMatrix< scalar_type > &jacobian, const ExtractorType_Row &extractor_row, const ExtractorType_Col &extractor_col)
static internal::ScalarFieldHessian< dim, scalar_type, ExtractorType_Row, ExtractorType_Col >::type extract_hessian_component(const FullMatrix< scalar_type > &hessian, const ExtractorType_Row &extractor_row, const ExtractorType_Col &extractor_col)
virtual void compute_residual(Vector< scalar_type > &residual) const override
void set_tape_buffer_sizes(const typename Types< ad_type >::tape_buffer_sizes obufsize=64 *1024 *1024, const typename Types< ad_type >::tape_buffer_sizes lbufsize=64 *1024 *1024, const typename Types< ad_type >::tape_buffer_sizes vbufsize=64 *1024 *1024, const typename Types< ad_type >::tape_buffer_sizes tbufsize=64 *1024 *1024)
ScalarType value(const typename Types< ADNumberType >::tape_index active_tape_index, const std::vector< ScalarType > &independent_variables) const
void jacobian(const std::vector< ADNumberType > &independent_variables, const std::vector< ADNumberType > &dependent_variables, FullMatrix< ScalarType > &jacobian) const
const std::vector< ad_type > & get_sensitive_dof_values() const
void activate_tape(const typename Types< ADNumberType >::tape_index tape_index)
void register_dependent_variable(const ad_type &func)
void stop_recording_operations(const bool write_tapes_to_file=false)
bool is_dependent_variable_marking_allowed() const
void compute_gradient(Vector< scalar_type > &gradient) 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 void configure_tapeless_mode(const unsigned int n_independent_variables, const bool ensure_persistent_setting=true)
std::vector< scalar_type > independent_variable_values
Types< ad_type >::tape_index active_tape_index() const
std::vector< bool > symmetric_independent_variables
void set_sensitivity_value(const unsigned int index, const bool symmetric_component, const scalar_type &value)
typename HelperBase< ADNumberTypeCode, ScalarType >::ad_type ad_type
std::vector< bool > registered_marked_independent_variables
void hessian(const typename Types< ADNumberType >::tape_index active_tape_index, const std::vector< ScalarType > &independent_variables, FullMatrix< ScalarType > &hessian) const
bool keep_independent_values() const
ResidualLinearization(const unsigned int n_independent_variables, const unsigned int n_dependent_variables)
bool start_recording_operations(const typename Types< ad_type >::tape_index tape_index, const bool overwrite_tape=false, const bool keep_independent_values=true)
void compute_values(Vector< scalar_type > &values) const
unsigned int n_symmetric_independent_variables() const
void reset_registered_dependent_variables(const bool flag=false)
void register_dependent_variable(const unsigned int index, const ad_type &func)
bool requires_retaping(const typename Types< ADNumberType >::tape_index tape_index) const
void print_values(std::ostream &stream) const
static ::ExceptionBase & ExcMessage(std::string arg1)
std::vector< ad_type > dependent_variables
typename AD::NumberTraits< ScalarType, ADNumberTypeCode >::ad_type ad_type
virtual void compute_linearization(FullMatrix< scalar_type > &linearization) const override
void compute_hessian(FullMatrix< scalar_type > &hessian) const
std::size_t n_independent_variables() const
void set_tensor_entry(TensorType &t, const unsigned int unrolled_index, const NumberType &value)
void reinit(const TableIndices< N > &new_size, const bool omit_default_initialization=false)
typename AD::NumberTraits< ScalarType, ADNumberTypeCode >::scalar_type scalar_type
#define Assert(cond, exc)
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
void reset_registered_independent_variables()
std::vector< bool > registered_marked_dependent_variables
ScalarFunction(const unsigned int n_independent_variables)
scalar_type compute_value() const
void print_tape_stats(const typename Types< ADNumberType >::tape_index tape_index, std::ostream &stream) const
void start_taping(const typename Types< ADNumberType >::tape_index tape_index, const bool keep_independent_values)
ScalarType value(const std::vector< ADNumberType > &dependent_variables) const
unsigned int n_registered_dependent_variables() const
bool is_recording() const
#define DEAL_II_NAMESPACE_CLOSE
virtual void reset(const unsigned int n_independent_variables=::numbers::invalid_unsigned_int, const unsigned int n_dependent_variables=::numbers::invalid_unsigned_int, const bool clear_registered_tapes=true) override
void register_energy_functional(const ad_type &energy)
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 print(std::ostream &stream) const
void print(std::ostream &stream) const
void finalize_sensitive_independent_variables() const
virtual void compute_linearization(FullMatrix< scalar_type > &linearization) const override
void gradient(const typename Types< ADNumberType >::tape_index active_tape_index, const std::vector< ScalarType > &independent_variables, Vector< ScalarType > &gradient) const
bool is_symmetric_independent_variable(const unsigned int index) const
typename HelperBase< ADNumberTypeCode, ScalarType >::scalar_type scalar_type
bool is_registered_tape(const typename Types< ADNumberType >::tape_index tape_index) const
void register_dof_values(const std::vector< scalar_type > &dof_values)
void swap(MemorySpaceData< Number, MemorySpace > &, MemorySpaceData< Number, MemorySpace > &)
static void initialize_global_environment(const unsigned int n_independent_variables)
bool active_tape_requires_retaping() const
void reset(const bool clear_registered_tapes)
void register_dependent_variables(const std::vector< ad_type > &funcs)
std::vector< bool > registered_independent_variable_values
#define DEAL_II_NAMESPACE_OPEN
void initialize_non_sensitive_independent_variable(const unsigned int index, ad_type &out) const
bool recorded_tape_requires_retaping(const typename Types< ad_type >::tape_index tape_index) const
void prevent_dependent_variable_marking()
void register_residual_vector(const std::vector< ad_type > &residual)
void print_tape_stats(const typename Types< ad_type >::tape_index tape_index, std::ostream &stream) const
TapelessDrivers< ad_type, scalar_type > tapeless_driver
bool is_registered_tape(const typename Types< ad_type >::tape_index tape_index) const
std::vector< ad_type > independent_variables
std::size_t n_dependent_variables() const
void gradient(const std::vector< ADNumberType > &independent_variables, const std::vector< ADNumberType > &dependent_variables, Vector< ScalarType > &gradient) const
void activate_recorded_tape(const typename Types< ad_type >::tape_index tape_index)
void stop_taping(const typename Types< ADNumberType >::tape_index active_tape_index, const bool write_tapes_to_file)
void hessian(const std::vector< ADNumberType > &independent_variables, const std::vector< ADNumberType > &dependent_variables, FullMatrix< ScalarType > &hessian) const
bool last_action_requires_retaping() const
typename HelperBase< ADNumberTypeCode, ScalarType >::scalar_type scalar_type
void set_independent_variables(const std::vector< scalar_type > &values)
EnergyFunctional(const unsigned int n_independent_variables)
const std::vector< ad_type > & get_sensitive_variables() const
void set_dof_values(const std::vector< scalar_type > &dof_values)
void allow_dependent_variable_marking()
unsigned int n_registered_independent_variables() const
void remove_tape(const typename Types< ADNumberType >::tape_index tape_index)
static ::ExceptionBase & ExcInternalError()
void activate_tape(const typename Types< ad_type >::tape_index tape_index, const bool read_mode)