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 <utility>
33 
34 namespace Marmot {
35  namespace ContinuumMechanics::CommonTensors {
36 
39 
42 
45 
48 
51 
54  ;
55 
58 
61 
64 
65  constexpr int getNumberOfDofForRotation( int nDim )
66  {
67  if ( nDim == 2 )
68  return 1;
69  else
70  return 3;
71  }
72 
73  template < int sizeI, int sizeJ >
74  Eigen::Matrix< double, sizeI * sizeJ, sizeI * sizeJ > makeIndexSwapTensor()
75  {
76  // Aux. Matrix, which helps to swap indices in Eigen::Matrices abused as higher order Tensors by
77  // multiplication ,
78  //
79  // T_(ij)(kl) * IndexSwapTensor_(kl)(lk) = T_(ij)(lk)
80  //
81  // For instance:
82  //
83  // Matrix3d t;
84  // t<<1,2,3,4,5,6,7,8,9;
85  // const auto P = makeIndexSwapTensor<3,3>();
86  // std::cout << t.reshaped().transpose() << std::endl;
87  // std::cout << t.reshaped().transpose() * P << std::endl;
88  //
89  // Output:
90  //
91  // 1,4,7,2,5,8,3,6,9
92  // 1,2,3,4,5,6,7,8,9
93  //
94 
95  Eigen::Matrix< double, sizeI * sizeJ, sizeI * sizeJ > P;
96 
97  auto d = []( int a, int b ) -> double { return a == b ? 1.0 : 0.0; };
98 
99  for ( int i = 0; i < sizeI; i++ )
100  for ( int j = 0; j < sizeJ; j++ )
101  for ( int l = 0; l < sizeI; l++ )
102  for ( int k = 0; k < sizeJ; k++ )
103  P( i + j * sizeI, l * sizeJ + k ) = d( i, l ) * d( k, j );
104 
105  return P;
106  }
107 
108  template < int nDim >
109  constexpr Eigen::TensorFixedSize< double, Eigen::Sizes< getNumberOfDofForRotation( nDim ), nDim, nDim > > getReferenceToCorrectLeviCivita()
110  // template <int nDim>
111  // template <int nDim=2>
112  // constexpr Eigen::TensorFixedSize< double, Eigen::Sizes<getNumberOfDofForRotation (nDim), nDim, nDim> >
113  // getReferenceToCorrectLeviCivita()
114  {
115  if constexpr ( nDim == 2 )
116  return LeviCivita2D;
117  else
118  return LeviCivita3D;
119  }
120 
121  } // namespace ContinuumMechanics::CommonTensors
122 
123  namespace ContinuumMechanics::TensorUtility {
124 
125  constexpr int d( int a, int b )
126  {
127  return a == b ? 1 : 0;
128  }
129 
130  template < int x,
131  int y,
132  typename T,
133  typename = std::enable_if< !std::is_const< std::remove_reference< T > >::value > >
134  auto as( T& t )
135  {
136  return Eigen::Map< Eigen::Matrix< typename T::Scalar, x, y > >( t.data() );
137  }
138 
139  template < int x, int y, typename T, typename = void >
140  auto as( const T& t )
141  {
142  return Eigen::Map< const Eigen::Matrix< typename T::Scalar, x, y > >( t.data() );
143  }
144 
145  template < typename Derived,
146  typename = std::enable_if< !std::is_const< std::remove_reference< Derived > >::value > >
147  auto flatten( Derived& t )
148  {
149  return Eigen::Map<
150  Eigen::Matrix< typename Derived::Scalar, Derived::RowsAtCompileTime * Derived::ColsAtCompileTime, 1 > >(
151  t.data() );
152  }
153 
154  template < typename Derived, typename = void >
155  auto flatten( const Derived& t )
156  {
157  return Eigen::Map<
158  const Eigen::Matrix< typename Derived::Scalar, Derived::RowsAtCompileTime * Derived::ColsAtCompileTime, 1 > >(
159  t.data() );
160  }
161 
162  template < int... Pairs >
163  constexpr auto contractionDims() // should be evaluated at compile time
164  {
165  static_assert( sizeof...( Pairs ) % 2 == 0, "Pairs must contain an even number of elements." );
166 
167  constexpr int numPairs = sizeof...( Pairs ) / 2;
168 
169  Eigen::array< Eigen::IndexPair< int >, numPairs > result{};
170 
171  if constexpr ( numPairs > 0 ) { // return empty array if no pairs are given
172  constexpr int values[] = { Pairs... }; // Pairs... expands the parameter pack
173 
174  // Fill the result array at compile-time
175  for ( int i = 0; i < numPairs; ++i ) {
176  result[i] = { values[2 * i], values[2 * i + 1] };
177  }
178  }
179  return result;
180  }
181 
182  Eigen::Matrix3d dyadicProduct( const Eigen::Vector3d& vector1, const Eigen::Vector3d& vector2 );
183 
184  namespace IndexNotation {
185  template < int nDim >
186  constexpr std::pair< int, int > fromVoigt( int ij )
187  {
188  if constexpr ( nDim == 1 )
189  return std::pair< int, int >( 0, 0 );
190  else if ( nDim == 2 )
191  switch ( ij ) {
192  case 0: return std::pair< int, int >( 0, 0 );
193  case 1: return std::pair< int, int >( 1, 1 );
194  case 2: return std::pair< int, int >( 0, 1 );
195  }
196 
197  else if ( nDim == 3 ) {
198  switch ( ij ) {
199  case 0: return std::pair< int, int >( 0, 0 );
200  case 1: return std::pair< int, int >( 1, 1 );
201  case 2: return std::pair< int, int >( 2, 2 );
202  case 3: return std::pair< int, int >( 0, 1 );
203  case 4: return std::pair< int, int >( 0, 2 );
204  case 5: return std::pair< int, int >( 1, 2 );
205  }
206  }
207 
208  throw std::invalid_argument( MakeString()
209  << __PRETTY_FUNCTION__ << ": invalid dimension / voigt index specified" );
210  }
211 
212  template < int nDim >
213  constexpr int toVoigt( int i, int j )
214  {
215  if constexpr ( nDim == 1 )
216  return 0;
217  else if ( nDim == 2 )
218  return ( i == j ) ? ( i == 0 ? 0 : 1 ) : 2;
219 
220  else if ( nDim == 3 ) {
221  constexpr int tensor2VoigtNotationIndicesMapping[3][3] = { { 0, 3, 4 }, { 3, 1, 5 }, { 4, 5, 2 } };
222  return tensor2VoigtNotationIndicesMapping[i][j];
223  }
224 
225  throw std::invalid_argument( MakeString() << __PRETTY_FUNCTION__ << ": invalid dimension specified" );
226  }
227 
228  template < int nDim >
229  Eigen::TensorFixedSize< double, Eigen::Sizes< VOIGTFROMDIM( nDim ), nDim, nDim > > voigtMap()
230  {
231  using namespace Eigen;
232  Eigen::TensorFixedSize< double, Eigen::Sizes< VOIGTFROMDIM( nDim ), nDim, nDim > > result;
233  result.setZero();
234  for ( int i = 0; i < nDim; i++ )
235  for ( int j = 0; j < nDim; j++ )
236  result( toVoigt< nDim >( i, j ), i, j ) = 1;
237  return result;
238  }
239 
240  } // namespace IndexNotation
241 
242  // namespace ContinuumMechanics::VoigtNotation
243 
244  } // namespace ContinuumMechanics::TensorUtility
245 
246 } // namespace Marmot
Marmot::ContinuumMechanics::CommonTensors::Initialize_I2xI2
EigenTensors::Tensor3333d Initialize_I2xI2()
Definition: MarmotTensor.cpp:34
Marmot::ContinuumMechanics::CommonTensors::Initialize_Iskew
EigenTensors::Tensor3333d Initialize_Iskew()
Definition: MarmotTensor.cpp:71
Marmot::FastorIndices::i
Fastor::Index< i_ > i
Definition: MarmotFastorTensorBasics.h:137
Marmot::ContinuumMechanics::CommonTensors::LeviCivita2D
const EigenTensors::Tensor122d LeviCivita2D
Definition: MarmotTensor.h:60
Marmot::ContinuumMechanics::CommonTensors::Initialize_dDeviatoricStress_dStress
EigenTensors::Tensor3333d Initialize_dDeviatoricStress_dStress()
Definition: MarmotTensor.cpp:84
Marmot::ContinuumMechanics::CommonTensors::getReferenceToCorrectLeviCivita
constexpr Eigen::TensorFixedSize< double, Eigen::Sizes< getNumberOfDofForRotation(nDim), nDim, nDim > > getReferenceToCorrectLeviCivita()
Definition: MarmotTensor.h:109
Marmot::ContinuumMechanics::TensorUtility::IndexNotation::voigtMap
Eigen::TensorFixedSize< double, Eigen::Sizes< VOIGTFROMDIM(nDim), nDim, nDim > > voigtMap()
Definition: MarmotTensor.h:229
Marmot::ContinuumMechanics::TensorUtility::contractionDims
constexpr auto contractionDims()
Definition: MarmotTensor.h:163
MarmotJournal.h
MarmotTypedefs.h
Marmot::ContinuumMechanics::CommonTensors::dDeviatoricStress_dStress
const EigenTensors::Tensor3333d dDeviatoricStress_dStress
Definition: MarmotTensor.h:53
Marmot::FastorIndices::l
Fastor::Index< l_ > l
Definition: MarmotFastorTensorBasics.h:207
Marmot::ContinuumMechanics::CommonTensors::Initialize_IFourthOrder
EigenTensors::Tensor3333d Initialize_IFourthOrder()
Definition: MarmotTensor.cpp:8
Marmot::ContinuumMechanics::CommonTensors::I2xI2
const EigenTensors::Tensor3333d I2xI2
Definition: MarmotTensor.h:38
Marmot::ContinuumMechanics::CommonTensors::IFourthOrder
const EigenTensors::Tensor3333d IFourthOrder
Definition: MarmotTensor.h:47
Marmot::ContinuumMechanics::CommonTensors::IFourthOrderTranspose
const EigenTensors::Tensor3333d IFourthOrderTranspose
Definition: MarmotTensor.h:50
MarmotVoigt.h
Marmot::ContinuumMechanics::CommonTensors::Initialize_LeviCivita2D
EigenTensors::Tensor122d Initialize_LeviCivita2D()
Definition: MarmotTensor.cpp:113
Marmot::ContinuumMechanics::CommonTensors::Isym
const EigenTensors::Tensor3333d Isym
Definition: MarmotTensor.h:41
Marmot::ContinuumMechanics::CommonTensors::Initialize_IFourthOrderTranspose
EigenTensors::Tensor3333d Initialize_IFourthOrderTranspose()
Definition: MarmotTensor.cpp:21
Marmot::FiniteElement::Spatial2D::nDim
constexpr int nDim
Definition: MarmotFiniteElement.h:112
Marmot::ContinuumMechanics::CommonTensors::Initialize_Isym
EigenTensors::Tensor3333d Initialize_Isym()
Definition: MarmotTensor.cpp:58
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:125
Marmot::ContinuumMechanics::TensorUtility::dyadicProduct
Eigen::Matrix3d dyadicProduct(const Eigen::Vector3d &vector1, const Eigen::Vector3d &vector2)
Definition: MarmotTensor.cpp:127
Marmot::EigenTensors::Tensor33d
Eigen::TensorFixedSize< double, Eigen::Sizes< 3, 3 > > Tensor33d
Definition: MarmotTypedefs.h:80
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:73
Marmot::ContinuumMechanics::TensorUtility::IndexNotation::toVoigt
constexpr int toVoigt(int i, int j)
Definition: MarmotTensor.h:213
Marmot::FastorIndices::j
Fastor::Index< j_ > j
Definition: MarmotFastorTensorBasics.h:182
Marmot
This file includes functions needed for calculations with stress and strain tensors written in voigt ...
Definition: MarmotTesting.h:37
Marmot::ContinuumMechanics::TensorUtility::IndexNotation::fromVoigt
constexpr std::pair< int, int > fromVoigt(int ij)
Definition: MarmotTensor.h:186
Marmot::ContinuumMechanics::CommonTensors::LeviCivita3D
const EigenTensors::Tensor333d LeviCivita3D
Definition: MarmotTensor.h:57
Marmot::ContinuumMechanics::TensorUtility::as
auto as(T &t)
Definition: MarmotTensor.h:134
Marmot::ContinuumMechanics::VoigtNotation::P
const Marmot::Vector6d P
Definition: MarmotVoigt.cpp:15
Marmot::ContinuumMechanics::TensorUtility::flatten
auto flatten(Derived &t)
Definition: MarmotTensor.h:147
Marmot::ContinuumMechanics::CommonTensors::I2
const EigenTensors::Tensor33d I2
Definition: MarmotTensor.h:63
Marmot::ContinuumMechanics::CommonTensors::Iskew
const EigenTensors::Tensor3333d Iskew
Definition: MarmotTensor.h:44
Marmot::FastorIndices::ij
Fastor::Index< i_, j_ > ij
Definition: MarmotFastorTensorBasics.h:158
Marmot::FastorIndices::k
Fastor::Index< k_ > k
Definition: MarmotFastorTensorBasics.h:195
Marmot::ContinuumMechanics::CommonTensors::Initialize_LeviCivita3D
EigenTensors::Tensor333d Initialize_LeviCivita3D()
Definition: MarmotTensor.cpp:97
Marmot::ContinuumMechanics::CommonTensors::makeIndexSwapTensor
Eigen::Matrix< double, sizeI *sizeJ, sizeI *sizeJ > makeIndexSwapTensor()
Definition: MarmotTensor.h:74
Marmot::EigenTensors::Tensor122d
Eigen::TensorFixedSize< double, Eigen::Sizes< 1, 2, 2 > > Tensor122d
Definition: MarmotTypedefs.h:75
Marmot::ContinuumMechanics::CommonTensors::Initialize_I2
EigenTensors::Tensor33d Initialize_I2()
Definition: MarmotTensor.cpp:47
Marmot::EigenTensors::Tensor333d
Eigen::TensorFixedSize< double, Eigen::Sizes< 3, 3, 3 > > Tensor333d
Definition: MarmotTypedefs.h:74
Marmot::ContinuumMechanics::CommonTensors::getNumberOfDofForRotation
constexpr int getNumberOfDofForRotation(int nDim)
Definition: MarmotTensor.h:65
MakeString
Definition: MarmotJournal.h:32