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 
86  template < size_t order >
87  autodiff::HigherOrderDual< order - 1, double > decreaseDualOrder( 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 );
98  }
99 
100  return out;
101  }
102 
103  template < size_t order >
104  autodiff::HigherOrderDual< order - 1, double > decreaseDualOrderWithShift(
105  autodiff::HigherOrderDual< order, double >& in )
106  {
107  using out_scalar_type = autodiff::HigherOrderDual< order - 1, double >;
108  using namespace autodiff::detail;
109 
110  out_scalar_type out( 0.0 );
111  double* in_point = &valnode< order >( in );
112  double* out_point = &valnode< order - 1 >( out );
113 
114  for ( size_t i = 0; i < size_t( std::pow( 2, order - 1 ) ); i++ ) {
115  *( out_point + i ) = *( in_point + i + size_t( std::pow( 2, order - 1 ) ) );
116  }
117 
118  return out;
119  }
120 
121  template < size_t order >
122  Vector< HigherOrderDual< order + 1, double >, -1 > increaseDualOrderWithShift(
123  const Vector< HigherOrderDual< order, double >, -1 >& in )
124  {
125  using in_scalar_type = HigherOrderDual< order, double >;
126  using out_scalar_type = HigherOrderDual< order + 1, double >;
127 
128  Vector< out_scalar_type, -1 > out = Vector< out_scalar_type, -1 >( in.size() );
129  out_scalar_type* out_data = out.data();
130  const in_scalar_type* in_data = in.data();
131 
132  for ( int i = 0; i < in.size(); i++ ) {
133  out_data[i] = increaseDualOrderWithShift< order >( in_data[i] );
134  }
135  return out;
136  }
137 
138  using scalar_to_scalar_function_type = std::function< dual( const dual& ) >;
139  double df_dx( const scalar_to_scalar_function_type& f, const double& x );
140 
141  using scalar_to_scalar_function_type_2nd = std::function< dual2nd( const dual2nd& ) >;
142  dual df_dx( const scalar_to_scalar_function_type_2nd& f, const dual& x );
143 
144  using vector_to_vector_function_type_dual = std::function< VectorXdual( const VectorXdual& X ) >;
145  std::pair< VectorXd, MatrixXd > dF_dX( const vector_to_vector_function_type_dual& F, const VectorXd& X );
146 
147  using vector_to_vector_function_type_dual2nd = std::function< VectorXdual2nd( const VectorXdual2nd& X ) >;
148  std::pair< VectorXdual, MatrixXdual > dF_dX_2nd( const vector_to_vector_function_type_dual2nd& F,
149  const VectorXdual& X );
150  } // namespace AutomaticDifferentiation
151 
152 } // namespace Marmot
Marmot::FastorIndices::i
Fastor::Index< i_ > i
Definition: MarmotFastorTensorBasics.h:137
Marmot::AutomaticDifferentiation::increaseDualOrderWithShift
autodiff::HigherOrderDual< order+1, double > increaseDualOrderWithShift(const autodiff::HigherOrderDual< order, double > &in)
Definition: MarmotAutomaticDifferentiation.h:69
Marmot::AutomaticDifferentiation::dF_dX
std::pair< VectorXd, MatrixXd > dF_dX(const vector_to_vector_function_type_dual &F, const VectorXd &X)
Definition: MarmotAutomaticDifferentiation.cpp:57
Marmot::AutomaticDifferentiation::vector_to_vector_function_type_dual
std::function< VectorXdual(const VectorXdual &X) > vector_to_vector_function_type_dual
Definition: MarmotAutomaticDifferentiation.h:144
Marmot::AutomaticDifferentiation::scalar_to_scalar_function_type
std::function< dual(const dual &) > scalar_to_scalar_function_type
Definition: MarmotAutomaticDifferentiation.h:138
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:141
Marmot::FastorIndices::in
Fastor::Index< i_, n_ > in
Definition: MarmotFastorTensorBasics.h:179
Marmot::AutomaticDifferentiation::vector_to_vector_function_type_dual2nd
std::function< VectorXdual2nd(const VectorXdual2nd &X) > vector_to_vector_function_type_dual2nd
Definition: MarmotAutomaticDifferentiation.h:147
Marmot
This file includes functions needed for calculations with stress and strain tensors written in voigt ...
Definition: MarmotTesting.h:37
Marmot::AutomaticDifferentiation::shiftTo2ndOrderDual
dual2nd shiftTo2ndOrderDual(const dual &x)
Definition: MarmotAutomaticDifferentiation.cpp:12
Marmot::AutomaticDifferentiation::decreaseDualOrder
autodiff::HigherOrderDual< order - 1, double > decreaseDualOrder(autodiff::HigherOrderDual< order, double > &in)
Definition: MarmotAutomaticDifferentiation.h:87
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:104
Marmot::FiniteElement::EAS::F
Eigen::MatrixXd F(const Eigen::MatrixXd &J)
Marmot::AutomaticDifferentiation::dF_dX_2nd
std::pair< VectorXdual, MatrixXdual > dF_dX_2nd(const vector_to_vector_function_type_dual2nd &F, const VectorXdual &X)
Definition: MarmotAutomaticDifferentiation.cpp:81
Marmot::FiniteElement::Spatial1D::Bar2::N
NSized N(double xi)
Definition: MarmotFiniteElement1D.cpp:15