MarmotMath.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  * Matthias Neuner matthias.neuner@uibk.ac.at
15  * Magdalena Schreter magdalena.schreter@uibk.ac.at
16  * Alexander Dummer alexander.dummer@uibk.ac.at
17  *
18  * This file is part of the MAteRialMOdellingToolbox (marmot).
19  *
20  * This library is free software; you can redistribute it and/or
21  * modify it under the terms of the GNU Lesser General Public
22  * License as published by the Free Software Foundation; either
23  * version 2.1 of the License, or (at your option) any later version.
24  *
25  * The full text of the license can be found in the file LICENSE.md at
26  * the top level directory of marmot.
27  * ---------------------------------------------------------------------
28  */
29 
30 #pragma once
31 #include "Marmot/MarmotConstants.h"
32 #include "Marmot/MarmotTypedefs.h"
33 #include "autodiff/forward/dual.hpp"
34 #include "autodiff/forward/real.hpp"
35 #include <algorithm>
36 #include <autodiff/forward/dual/dual.hpp>
37 #include <complex>
38 
39 namespace Marmot {
40  namespace Math {
43  template < typename T >
44  bool isNaN( T x )
45  {
46  return x != x;
47  }
48 
51  double linearInterpolation( double x, double x0, double x1, double y0, double y1 );
52 
55  double exp( double x );
56 
59  int getExponentPowerTen( const double x );
60 
63  constexpr double radToDeg( const double alpha )
64  {
65  return alpha * 180 / Marmot::Constants::Pi;
66  }
67 
70  constexpr double degToRad( const double alpha )
71  {
72  return alpha / 180 * Marmot::Constants::Pi;
73  }
74 
77  constexpr double macauly( double scalar )
78  {
79  return scalar >= 0 ? scalar : 0.0;
80  }
81 
84  constexpr int heaviside( double scalar )
85  {
86  return scalar >= 0 ? 1 : 0;
87  }
88 
91  template < typename T >
92  constexpr T sgn( T val )
93  {
94  return val / std::abs( val );
95  }
96 
97  double makeReal( const double& value );
98  double makeReal( const std::complex< double >& value );
99  double makeReal( const autodiff::real& value );
100 
101  template < typename T, typename G >
102  double makeReal( const autodiff::detail::Dual< T, G >& number )
103  {
104  return double( number );
105  }
106 
110  template < int nRows, int nCols >
111  Eigen::Matrix< double, nRows, nCols > macaulyMatrix( const Eigen::Matrix< double, nRows, nCols >& mat )
112  {
113  Eigen::Matrix< double, nRows, nCols > positivePart = Eigen::Matrix< double, nRows, nCols >::Zero();
114  for ( int i = 0; i < nRows; i++ ) {
115  for ( int j = 0; j < nCols; j++ ) {
116  positivePart( i, j ) = macauly( mat( i, j ) );
117  }
118  }
119  return positivePart;
120  }
121 
124  template < typename functionType, typename yType, typename... Args >
125  yType explicitEuler( yType yN, const double dt, functionType fRate, Args&&... fRateArgs )
126  {
127  return yN + fRate( yN, fRateArgs... ) * dt;
128  }
129 
135  template < int ySize, typename functionType, typename... Args >
136  Eigen::Matrix< double, ySize, 1 > semiImplicitEuler( Eigen::Matrix< double, ySize, 1 > yN,
137  const double dt,
138  functionType fRate,
139  Args&&... fRateArgs )
140  {
141  Eigen::Matrix< double, ySize, ySize > fS = Eigen::Matrix< double, ySize, ySize >::Zero();
142  Eigen::Matrix< double, ySize, ySize > Iy = Eigen::Matrix< double, ySize, ySize >::Identity();
143 
144  Eigen::VectorXd leftX( ySize );
145  Eigen::VectorXd rightX( ySize );
146 
147  for ( size_t i = 0; i < ySize; i++ ) {
148  double volatile h = std::max( 1.0, std::abs( yN( i ) ) ) * Constants::cubicRootEps();
149  leftX = yN;
150  leftX( i ) -= h;
151  rightX = yN;
152  rightX( i ) += h;
153  fS.col( i ) = 1. / ( 2. * h ) * ( fRate( rightX, fRateArgs... ) - fRate( leftX, fRateArgs... ) );
154  }
155 
156  return yN + ( Iy - dt * fS ).inverse() * dt * fRate( yN, fRateArgs... );
157  }
158 
162  template < int nRows, int nCols, typename functionType >
163  Eigen::Matrix< double, nRows, nCols > centralDiff( functionType f, const Eigen::Matrix< double, nCols, 1 >& X )
164  {
165  Eigen::Matrix< double, nRows, nCols > dXdY = Eigen::Matrix< double, nRows, nCols >::Zero();
166 
167  Eigen::VectorXd leftX( nCols );
168  Eigen::VectorXd rightX( nCols );
169 
170  for ( size_t i = 0; i < nCols; i++ ) {
171  double volatile h = std::max( 1.0, std::abs( X( i ) ) ) * Constants::cubicRootEps();
172  leftX = X;
173  leftX( i ) -= h;
174  rightX = X;
175  rightX( i ) += h;
176  dXdY.col( i ) = 1. / ( 2. * h ) * ( f( rightX ) - f( leftX ) );
177  }
178 
179  return dXdY;
180  }
181 
186  template < typename functionType, typename yType, typename... Args >
187  yType explicitEulerRichardson( yType yN, const double dt, functionType fRate, Args&&... fRateArgs )
188  {
189  yType fN = fRate( yN, fRateArgs... );
190  yType u = yN + fN * dt;
191  yType v = yN + fN * dt / 2.;
192  yType w = v + fRate( v, fRateArgs... ) * dt / 2.;
193 
194  return 2. * w - u;
195  }
196 
201  template < int ySize, typename functionType, typename... Args >
202  std::tuple< Eigen::Matrix< double, ySize, 1 >, double > explicitEulerRichardsonWithErrorEstimator(
203  Eigen::Matrix< double, ySize, 1 > yN,
204  const double dt,
205  const double TOL,
206  functionType fRate,
207  Args&&... fRateArgs )
208  {
209 
210  typedef Eigen::Matrix< double, ySize, 1 > ySized;
211  ySized fN = fRate( yN, fRateArgs... );
212  ySized u = yN + fN * dt;
213  ySized v = yN + fN * dt / 2.;
214  ySized w = v + fRate( v, fRateArgs... ) * dt / 2.;
215  ySized yNew = 2. * w - u;
216 
217  // error estimator
218  const double AERR = 1.0;
219  const double aI = AERR / TOL;
220  const double rI = 1.0;
221  double scaling = 0;
222  ySized ESTVec = ySized::Zero();
223 
224  for ( int i = 0; i < ySize; i++ ) {
225  scaling = aI + rI * std::max( abs( yNew( i ) ), abs( yN( i ) ) );
226  ESTVec( i ) = abs( w( i ) - u( i ) ) / abs( scaling );
227  }
228 
229  const double EST = ESTVec.maxCoeff();
230  const double tauNew = dt * std::min( 2., std::max( 0.2, 0.9 * std::sqrt( TOL / EST ) ) );
231 
232  return std::make_tuple( yNew, tauNew );
233  }
234 
238  Matrix3d directionCosines( const Matrix3d& transformedCoordinateSystem );
239 
244  } // namespace Math
245 } // namespace Marmot
Marmot::Math::radToDeg
constexpr double radToDeg(const double alpha)
Definition: MarmotMath.h:63
Marmot::Math::explicitEulerRichardsonWithErrorEstimator
std::tuple< Eigen::Matrix< double, ySize, 1 >, double > explicitEulerRichardsonWithErrorEstimator(Eigen::Matrix< double, ySize, 1 > yN, const double dt, const double TOL, functionType fRate, Args &&... fRateArgs)
Definition: MarmotMath.h:202
Marmot::Math::macaulyMatrix
Eigen::Matrix< double, nRows, nCols > macaulyMatrix(const Eigen::Matrix< double, nRows, nCols > &mat)
Definition: MarmotMath.h:111
MarmotConstants.h
Marmot::Math::exp
double exp(double x)
Definition: MarmotMath.cpp:13
Marmot::Math::degToRad
constexpr double degToRad(const double alpha)
Definition: MarmotMath.h:70
Marmot::Math::sgn
constexpr T sgn(T val)
Definition: MarmotMath.h:92
MarmotTypedefs.h
Marmot::Math::isNaN
bool isNaN(T x)
Definition: MarmotMath.h:44
Marmot::Vector3d
Eigen::Matrix< double, 3, 1 > Vector3d
Definition: MarmotTypedefs.h:42
Marmot::Math::makeReal
double makeReal(const double &value)
Definition: MarmotMath.cpp:37
Marmot::Math::linearInterpolation
double linearInterpolation(double x, double x0, double x1, double y0, double y1)
Definition: MarmotMath.cpp:7
Marmot::Matrix3d
Eigen::Matrix< double, 3, 3 > Matrix3d
Definition: MarmotTypedefs.h:40
Marmot
This file includes functions needed for calculations with stress and strain tensors written in voigt ...
Definition: MarmotTesting.h:30
Marmot::Math::explicitEulerRichardson
yType explicitEulerRichardson(yType yN, const double dt, functionType fRate, Args &&... fRateArgs)
Definition: MarmotMath.h:187
Marmot::Math::explicitEuler
yType explicitEuler(yType yN, const double dt, functionType fRate, Args &&... fRateArgs)
Definition: MarmotMath.h:125
Marmot::Math::heaviside
constexpr int heaviside(double scalar)
Definition: MarmotMath.h:84
Marmot::Math::centralDiff
Eigen::Matrix< double, nRows, nCols > centralDiff(functionType f, const Eigen::Matrix< double, nCols, 1 > &X)
Definition: MarmotMath.h:163
Marmot::Math::orthonormalCoordinateSystem
Matrix3d orthonormalCoordinateSystem(Vector3d &normalVector)
Definition: MarmotMath.cpp:53
Marmot::Math::macauly
constexpr double macauly(double scalar)
Definition: MarmotMath.h:77
Marmot::Math::directionCosines
Matrix3d directionCosines(const Matrix3d &transformedCoordinateSystem)
Definition: MarmotMath.cpp:74
Marmot::Constants::cubicRootEps
double cubicRootEps()
Definition: MarmotConstants.h:36
Marmot::Constants::Pi
constexpr double Pi
Definition: MarmotConstants.h:34
Marmot::Math::semiImplicitEuler
Eigen::Matrix< double, ySize, 1 > semiImplicitEuler(Eigen::Matrix< double, ySize, 1 > yN, const double dt, functionType fRate, Args &&... fRateArgs)
Definition: MarmotMath.h:136
Marmot::Math::getExponentPowerTen
int getExponentPowerTen(const double x)
Definition: MarmotMath.cpp:43