MarmotAutomaticDifferentiation.h
Go to the documentation of this file.
1 /* ---------------------------------------------------------------------
2  * _
3  * _ __ ___ __ _ _ __ _ __ ___ ___ | |_
4  * | '_ ` _ \ / _` | '__| '_ ` _ \ / _ \| __|
5  * | | | | | | (_| | | | | | | | | (_) | |_
6  * |_| |_| |_|\__,_|_| |_| |_| |_|\___/ \__|
7  *
8  * Unit of Strength of Materials and Structural Analysis
9  * University of Innsbruck,
10  * 2020 - today
11  *
12  * festigkeitslehre@uibk.ac.at
13  *
14  * Alexander Dummer alexander.dummer@uibk.ac.at
15  *
16  * This file is part of the MAteRialMOdellingToolbox (marmot).
17  *
18  * This library is free software; you can redistribute it and/or
19  * modify it under the terms of the GNU Lesser General Public
20  * License as published by the Free Software Foundation; either
21  * version 2.1 of the License, or (at your option) any later version.
22  *
23  * The full text of the license can be found in the file LICENSE.md at
24  * the top level directory of marmot.
25  * ---------------------------------------------------------------------
26  */
27 
28 #pragma once
29 #include "autodiff/forward/dual/eigen.hpp"
30 #include <autodiff/forward/dual/dual.hpp>
31 #include <functional>
32 
33 namespace Marmot {
34 
35  namespace AutomaticDifferentiation {
36 
37  using namespace autodiff;
38  using namespace Eigen;
39 
40  dual2nd shiftTo2ndOrderDual( const dual& x );
41  VectorXdual2nd shiftTo2ndOrderDual( const VectorXdual& X );
42 
43  template < size_t order, typename T, typename G >
44  auto& valnode( const Dual< T, G >& dual )
45  {
46  constexpr auto N = detail::Order< Dual< T, G > >;
47  static_assert( order <= N );
48  if constexpr ( order == 0 )
49  return dual.val;
50  else if constexpr ( order == 1 )
51  return dual.val;
52  else
53  return valnode< order - 1 >( dual.val );
54  }
55  template < size_t order, typename T, typename G >
56  auto& valnode( Dual< T, G >& dual )
57  {
58  constexpr auto N = detail::Order< Dual< T, G > >;
59  static_assert( order <= N );
60  if constexpr ( order == 0 )
61  return dual.val;
62  else if constexpr ( order == 1 )
63  return dual.val;
64  else
65  return valnode< order - 1 >( dual.val );
66  }
67 
68  template < size_t order >
69  autodiff::HigherOrderDual< order + 1, double > increaseDualOrderWithShift(
70  const autodiff::HigherOrderDual< order, double >& in )
71  {
72  using out_scalar_type = autodiff::HigherOrderDual< order + 1, double >;
73  using namespace autodiff::detail;
74 
75  out_scalar_type out( 0.0 );
76  const double* in_point = &valnode< order >( in );
77  double* out_point = &valnode< order + 1 >( out );
78 
79  for ( size_t i = 0; i < size_t( std::pow( 2, order ) ); i++ ) {
80  *( out_point + i ) = *( in_point + i );
81  }
82 
83  return out;
84  }
85  template < size_t order >
86  autodiff::HigherOrderDual< order - 1, double > decreaseDualOrderWithShift(
87  autodiff::HigherOrderDual< order, double >& in )
88  {
89  using out_scalar_type = autodiff::HigherOrderDual< order - 1, double >;
90  using namespace autodiff::detail;
91 
92  out_scalar_type out( 0.0 );
93  double* in_point = &valnode< order >( in );
94  double* out_point = &valnode< order - 1 >( out );
95 
96  for ( size_t i = 0; i < size_t( std::pow( 2, order - 1 ) ); i++ ) {
97  *( out_point + i ) = *( in_point + i + size_t( std::pow( 2, order - 1 ) ) );
98  }
99 
100  return out;
101  }
102 
103  template < size_t order >
104  Vector< HigherOrderDual< order + 1, double >, -1 > increaseDualOrderWithShift(
105  const Vector< HigherOrderDual< order, double >, -1 >& in )
106  {
107  using in_scalar_type = HigherOrderDual< order, double >;
108  using out_scalar_type = HigherOrderDual< order + 1, double >;
109 
110  Vector< out_scalar_type, -1 > out = Vector< out_scalar_type, -1 >( in.size() );
111  out_scalar_type* out_data = out.data();
112  const in_scalar_type* in_data = in.data();
113 
114  for ( int i = 0; i < in.size(); i++ ) {
115  out_data[i] = increaseDualOrderWithShift< order >( in_data[i] );
116  }
117  return out;
118  }
119 
120  using scalar_to_scalar_function_type = std::function< dual( const dual& ) >;
121  double df_dx( const scalar_to_scalar_function_type& f, const double& x );
122 
123  using scalar_to_scalar_function_type_2nd = std::function< dual2nd( const dual2nd& ) >;
124  dual df_dx( const scalar_to_scalar_function_type_2nd& f, const dual& x );
125 
126  using vector_to_vector_function_type = std::function< VectorXdual( const VectorXdual& X ) >;
127  MatrixXd forwardMode( const vector_to_vector_function_type& F, const VectorXd& X );
128 
129  /* using vector_to_scalar_function_type = std::function< dual( const VectorXdual& ) >; */
130  /* std::pair< double, VectorXd > df_dVector( const vector_to_scalar_function_type& f, const VectorXd& X ); */
131 
132  /* using vector_to_scalar_function_type_2nd = std::function< dual2nd( const VectorXdual2nd& ) >; */
133  /* std::pair< dual, VectorXdual > df_dVector( const vector_to_scalar_function_type_2nd& f, const VectorXdual& X );
134  */
135 
136  using vector_to_vector_function_type_dual = std::function< VectorXdual( const VectorXdual& X ) >;
137  std::pair< VectorXd, MatrixXd > jacobian( const vector_to_vector_function_type_dual& F, const VectorXd& X );
138 
139  using vector_to_vector_function_type_dual2nd = std::function< VectorXdual2nd( const VectorXdual2nd& X ) >;
140  std::pair< VectorXdual, MatrixXdual > jacobian2nd( const vector_to_vector_function_type_dual2nd& F,
141  const VectorXdual& X );
142  } // namespace AutomaticDifferentiation
143 
144 } // namespace Marmot
Marmot::AutomaticDifferentiation::forwardMode
MatrixXd forwardMode(const vector_to_vector_function_type &F, const VectorXd &X)
Definition: MarmotAutomaticDifferentiation.cpp:56
Marmot::AutomaticDifferentiation::increaseDualOrderWithShift
autodiff::HigherOrderDual< order+1, double > increaseDualOrderWithShift(const autodiff::HigherOrderDual< order, double > &in)
Definition: MarmotAutomaticDifferentiation.h:69
Marmot::AutomaticDifferentiation::vector_to_vector_function_type_dual
std::function< VectorXdual(const VectorXdual &X) > vector_to_vector_function_type_dual
Definition: MarmotAutomaticDifferentiation.h:136
Marmot::AutomaticDifferentiation::scalar_to_scalar_function_type
std::function< dual(const dual &) > scalar_to_scalar_function_type
Definition: MarmotAutomaticDifferentiation.h:120
Marmot::AutomaticDifferentiation::jacobian2nd
std::pair< VectorXdual, MatrixXdual > jacobian2nd(const vector_to_vector_function_type_dual2nd &F, const VectorXdual &X)
Definition: MarmotAutomaticDifferentiation.cpp:88
Marmot::AutomaticDifferentiation::valnode
auto & valnode(const Dual< T, G > &dual)
Definition: MarmotAutomaticDifferentiation.h:44
Marmot::AutomaticDifferentiation::scalar_to_scalar_function_type_2nd
std::function< dual2nd(const dual2nd &) > scalar_to_scalar_function_type_2nd
Definition: MarmotAutomaticDifferentiation.h:123
Marmot::AutomaticDifferentiation::vector_to_vector_function_type_dual2nd
std::function< VectorXdual2nd(const VectorXdual2nd &X) > vector_to_vector_function_type_dual2nd
Definition: MarmotAutomaticDifferentiation.h:139
Marmot
This file includes functions needed for calculations with stress and strain tensors written in voigt ...
Definition: MarmotTesting.h:32
Marmot::AutomaticDifferentiation::shiftTo2ndOrderDual
dual2nd shiftTo2ndOrderDual(const dual &x)
Definition: MarmotAutomaticDifferentiation.cpp:12
Marmot::AutomaticDifferentiation::jacobian
std::pair< VectorXd, MatrixXd > jacobian(const vector_to_vector_function_type_dual &F, const VectorXd &X)
Definition: MarmotAutomaticDifferentiation.cpp:64
Marmot::AutomaticDifferentiation::vector_to_vector_function_type
std::function< VectorXdual(const VectorXdual &X) > vector_to_vector_function_type
Definition: MarmotAutomaticDifferentiation.h:126
Marmot::AutomaticDifferentiation::df_dx
double df_dx(const scalar_to_scalar_function_type &f, const double &x)
Definition: MarmotAutomaticDifferentiation.cpp:32
Marmot::AutomaticDifferentiation::decreaseDualOrderWithShift
autodiff::HigherOrderDual< order - 1, double > decreaseDualOrderWithShift(autodiff::HigherOrderDual< order, double > &in)
Definition: MarmotAutomaticDifferentiation.h:86
Marmot::FiniteElement::EAS::F
Eigen::MatrixXd F(const Eigen::MatrixXd &J)
Marmot::FiniteElement::Spatial1D::Bar2::N
NSized N(double xi)
Definition: MarmotFiniteElement1D.cpp:15