MarmotTensor.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  *
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 "Marmot/MarmotJournal.h"
30 #include "Marmot/MarmotTypedefs.h"
31 #include "Marmot/MarmotVoigt.h"
32 #include <iostream>
33 #include <utility>
34 
35 namespace Marmot {
36  namespace ContinuumMechanics::CommonTensors {
37  extern const EigenTensors::Tensor3333d I2xI2;
38  extern const EigenTensors::Tensor3333d Isym;
39  extern const EigenTensors::Tensor3333d Iskew;
43 
46 
47  constexpr int getNumberOfDofForRotation( int nDim )
48  {
49  if ( nDim == 2 )
50  return 1;
51  else
52  return 3;
53  }
54 
55  template < int sizeI, int sizeJ >
56  Eigen::Matrix< double, sizeI * sizeJ, sizeI * sizeJ > makeIndexSwapTensor()
57  {
58  // Aux. Matrix, which helps to swap indices in Eigen::Matrices abused as higher order Tensors by
59  // multiplication ,
60  //
61  // T_(ij)(kl) * IndexSwapTensor_(kl)(lk) = T_(ij)(lk)
62  //
63  // For instance:
64  //
65  // Matrix3d t;
66  // t<<1,2,3,4,5,6,7,8,9;
67  // const auto P = makeIndexSwapTensor<3,3>();
68  // std::cout << t.reshaped().transpose() << std::endl;
69  // std::cout << t.reshaped().transpose() * P << std::endl;
70  //
71  // Output:
72  //
73  // 1,4,7,2,5,8,3,6,9
74  // 1,2,3,4,5,6,7,8,9
75  //
76 
77  Eigen::Matrix< double, sizeI * sizeJ, sizeI * sizeJ > P;
78 
79  auto d = []( int a, int b ) -> double { return a == b ? 1.0 : 0.0; };
80 
81  for ( int i = 0; i < sizeI; i++ )
82  for ( int j = 0; j < sizeJ; j++ )
83  for ( int l = 0; l < sizeI; l++ )
84  for ( int k = 0; k < sizeJ; k++ )
85  P( i + j * sizeI, l * sizeJ + k ) = d( i, l ) * d( k, j );
86 
87  return P;
88  }
89 
90  template < int nDim >
91  constexpr Eigen::TensorFixedSize< double, Eigen::Sizes< getNumberOfDofForRotation( nDim ), nDim, nDim > > getReferenceToCorrectLeviCivita()
92  // template <int nDim>
93  // template <int nDim=2>
94  // constexpr Eigen::TensorFixedSize< double, Eigen::Sizes<getNumberOfDofForRotation (nDim), nDim, nDim> >
95  // getReferenceToCorrectLeviCivita()
96  {
97  if constexpr ( nDim == 2 )
98  return LeviCivita2D;
99  else
100  return LeviCivita3D;
101  }
102 
103  } // namespace ContinuumMechanics::CommonTensors
104 
105  namespace ContinuumMechanics::TensorUtility {
106 
107  constexpr int d( int a, int b )
108  {
109  return a == b ? 1 : 0;
110  }
111 
112  template < int x,
113  int y,
114  typename T,
115  typename = std::enable_if< !std::is_const< std::remove_reference< T > >::value > >
116  auto as( T& t )
117  {
118  return Eigen::Map< Eigen::Matrix< typename T::Scalar, x, y > >( t.data() );
119  }
120 
121  template < int x, int y, typename T, typename = void >
122  auto as( const T& t )
123  {
124  return Eigen::Map< const Eigen::Matrix< typename T::Scalar, x, y > >( t.data() );
125  }
126 
127  template < typename Derived,
128  typename = std::enable_if< !std::is_const< std::remove_reference< Derived > >::value > >
129  auto flatten( Derived& t )
130  {
131  return Eigen::Map<
132  Eigen::Matrix< typename Derived::Scalar, Derived::RowsAtCompileTime * Derived::ColsAtCompileTime, 1 > >(
133  t.data() );
134  }
135 
136  template < typename Derived, typename = void >
137  auto flatten( const Derived& t )
138  {
139  return Eigen::Map<
140  const Eigen::Matrix< typename Derived::Scalar, Derived::RowsAtCompileTime * Derived::ColsAtCompileTime, 1 > >(
141  t.data() );
142  }
143 
144  Eigen::Matrix3d dyadicProduct( const Eigen::Vector3d& vector1, const Eigen::Vector3d& vector2 );
145 
146  namespace IndexNotation {
147  template < int nDim >
148  constexpr std::pair< int, int > fromVoigt( int ij )
149  {
150  if constexpr ( nDim == 1 )
151  return std::pair< int, int >( 0, 0 );
152  else if ( nDim == 2 )
153  switch ( ij ) {
154  case 0: return std::pair< int, int >( 0, 0 );
155  case 1: return std::pair< int, int >( 1, 1 );
156  case 2: return std::pair< int, int >( 0, 1 );
157  }
158 
159  else if ( nDim == 3 ) {
160  switch ( ij ) {
161  case 0: return std::pair< int, int >( 0, 0 );
162  case 1: return std::pair< int, int >( 1, 1 );
163  case 2: return std::pair< int, int >( 2, 2 );
164  case 3: return std::pair< int, int >( 0, 1 );
165  case 4: return std::pair< int, int >( 0, 2 );
166  case 5: return std::pair< int, int >( 1, 2 );
167  }
168  }
169 
170  throw std::invalid_argument( MakeString()
171  << __PRETTY_FUNCTION__ << ": invalid dimension / voigt index specified" );
172  }
173 
174  template < int nDim >
175  constexpr int toVoigt( int i, int j )
176  {
177  if constexpr ( nDim == 1 )
178  return 0;
179  else if ( nDim == 2 )
180  return ( i == j ) ? ( i == 0 ? 0 : 1 ) : 2;
181 
182  else if ( nDim == 3 ) {
183  constexpr int tensor2VoigtNotationIndicesMapping[3][3] = { { 0, 3, 4 }, { 3, 1, 5 }, { 4, 5, 2 } };
184  return tensor2VoigtNotationIndicesMapping[i][j];
185  }
186 
187  throw std::invalid_argument( MakeString() << __PRETTY_FUNCTION__ << ": invalid dimension specified" );
188  }
189 
190  template < int nDim >
191  Eigen::TensorFixedSize< double, Eigen::Sizes< VOIGTFROMDIM( nDim ), nDim, nDim > > voigtMap()
192  {
193  using namespace Eigen;
194  Eigen::TensorFixedSize< double, Eigen::Sizes< VOIGTFROMDIM( nDim ), nDim, nDim > > result;
195  result.setZero();
196  for ( int i = 0; i < nDim; i++ )
197  for ( int j = 0; j < nDim; j++ )
198  result( toVoigt< nDim >( i, j ), i, j ) = 1;
199  return result;
200  }
201 
202  } // namespace IndexNotation
203 
204  // namespace ContinuumMechanics::VoigtNotation
205 
206  } // namespace ContinuumMechanics::TensorUtility
207 
208 } // namespace Marmot
Marmot::ContinuumMechanics::CommonTensors::LeviCivita2D
const EigenTensors::Tensor122d LeviCivita2D
Definition: MarmotTensor.cpp:121
Marmot::ContinuumMechanics::CommonTensors::getReferenceToCorrectLeviCivita
constexpr Eigen::TensorFixedSize< double, Eigen::Sizes< getNumberOfDofForRotation(nDim), nDim, nDim > > getReferenceToCorrectLeviCivita()
Definition: MarmotTensor.h:91
Marmot::ContinuumMechanics::TensorUtility::IndexNotation::voigtMap
Eigen::TensorFixedSize< double, Eigen::Sizes< VOIGTFROMDIM(nDim), nDim, nDim > > voigtMap()
Definition: MarmotTensor.h:191
MarmotJournal.h
MarmotTypedefs.h
Marmot::ContinuumMechanics::CommonTensors::dDeviatoricStress_dStress
const EigenTensors::Tensor3333d dDeviatoricStress_dStress
Definition: MarmotTensor.cpp:91
Marmot::ContinuumMechanics::CommonTensors::I2xI2
const EigenTensors::Tensor3333d I2xI2
Definition: MarmotTensor.cpp:48
Marmot::ContinuumMechanics::CommonTensors::IFourthOrder
const EigenTensors::Tensor3333d IFourthOrder
Definition: MarmotTensor.cpp:20
Marmot::ContinuumMechanics::CommonTensors::IFourthOrderTranspose
const EigenTensors::Tensor3333d IFourthOrderTranspose
Definition: MarmotTensor.cpp:34
MarmotVoigt.h
Marmot::ContinuumMechanics::CommonTensors::Isym
const EigenTensors::Tensor3333d Isym
Definition: MarmotTensor.cpp:62
Marmot::FiniteElement::Spatial2D::nDim
constexpr int nDim
Definition: MarmotFiniteElement.h:112
Marmot::Vector3d
Eigen::Matrix< double, 3, 1 > Vector3d
Definition: MarmotTypedefs.h:42
VOIGTFROMDIM
#define VOIGTFROMDIM(x)
Definition: MarmotVoigt.h:35
Marmot::ContinuumMechanics::TensorUtility::d
constexpr int d(int a, int b)
Definition: MarmotTensor.h:107
Marmot::ContinuumMechanics::TensorUtility::dyadicProduct
Eigen::Matrix3d dyadicProduct(const Eigen::Vector3d &vector1, const Eigen::Vector3d &vector2)
Definition: MarmotTensor.cpp:126
Marmot::Matrix3d
Eigen::Matrix< double, 3, 3 > Matrix3d
Definition: MarmotTypedefs.h:40
Marmot::EigenTensors::Tensor3333d
Eigen::TensorFixedSize< double, Eigen::Sizes< 3, 3, 3, 3 > > Tensor3333d
Definition: MarmotTypedefs.h:68
Marmot::ContinuumMechanics::TensorUtility::IndexNotation::toVoigt
constexpr int toVoigt(int i, int j)
Definition: MarmotTensor.h:175
Marmot
This file includes functions needed for calculations with stress and strain tensors written in voigt ...
Definition: MarmotTesting.h:30
Marmot::ContinuumMechanics::TensorUtility::IndexNotation::fromVoigt
constexpr std::pair< int, int > fromVoigt(int ij)
Definition: MarmotTensor.h:148
Marmot::ContinuumMechanics::CommonTensors::LeviCivita3D
const EigenTensors::Tensor333d LeviCivita3D
Definition: MarmotTensor.cpp:120
Marmot::ContinuumMechanics::TensorUtility::as
auto as(T &t)
Definition: MarmotTensor.h:116
Marmot::ContinuumMechanics::VoigtNotation::P
const Marmot::Vector6d P
Definition: MarmotVoigt.cpp:16
Marmot::ContinuumMechanics::TensorUtility::flatten
auto flatten(Derived &t)
Definition: MarmotTensor.h:129
Marmot::ContinuumMechanics::CommonTensors::Iskew
const EigenTensors::Tensor3333d Iskew
Definition: MarmotTensor.cpp:76
Marmot::ContinuumMechanics::CommonTensors::makeIndexSwapTensor
Eigen::Matrix< double, sizeI *sizeJ, sizeI *sizeJ > makeIndexSwapTensor()
Definition: MarmotTensor.h:56
Marmot::EigenTensors::Tensor122d
Eigen::TensorFixedSize< double, Eigen::Sizes< 1, 2, 2 > > Tensor122d
Definition: MarmotTypedefs.h:70
Marmot::EigenTensors::Tensor333d
Eigen::TensorFixedSize< double, Eigen::Sizes< 3, 3, 3 > > Tensor333d
Definition: MarmotTypedefs.h:69
Marmot::ContinuumMechanics::CommonTensors::getNumberOfDofForRotation
constexpr int getNumberOfDofForRotation(int nDim)
Definition: MarmotTensor.h:47
MakeString
Definition: MarmotJournal.h:32