12#ifndef BELFEM_CL_TENSOR_HPP
13#define BELFEM_CL_TENSOR_HPP
41 template<
typename T >
76 mOffsetK( aSizeI * aSizeJ ),
77 mOffsetL( aSizeI * aSizeJ * aSizeK ),
79 mCapacity( aSizeI * aSizeJ * aSizeK * aSizeL )
81 BELFEM_ASSERT( mCapacity > 0,
"Tensor sizes must not be zero" );
83 mData = ( T * ) malloc( ( mCapacity ) *
sizeof( T ) );
95 mOffsetK( aSizeI * aSizeJ ),
96 mOffsetL( aSizeI * aSizeJ * aSizeK ),
98 mCapacity( aSizeI * aSizeJ * aSizeK )
100 BELFEM_ASSERT( mCapacity > 0,
"Tensor sizes must not be zero" );
102 mData = ( T * ) malloc( ( mCapacity ) *
sizeof( T ) );
110 const real aValue ) :
111 Tensor( aSizeI, aSizeJ, aSizeK, aSizeL )
113 this->
fill( aValue );
121 && aElasticityMatrix.
n_cols() == 6,
122 "Matrix must be allocated as 6x6" );
131 mSizeI( aOther.
size_i() ),
132 mSizeJ( aOther.
size_j() ),
133 mSizeK( aOther.
size_k() ),
134 mSizeL( aOther.
size_l() ),
135 mOffsetJ( aOther.mOffsetJ ),
136 mOffsetK( aOther.mOffsetK ),
137 mOffsetL( aOther.mOffsetL ),
138 mOrder( aOther.mOrder ),
139 mCapacity( aOther.mCapacity )
141 mData = ( T * ) malloc( mCapacity *
sizeof( T ) );
142 std::memcpy( mData, aOther.mData, mCapacity *
sizeof( T ) );
149 mSizeI( aOther.
size_i() ),
150 mSizeJ( aOther.
size_j() ),
151 mSizeK( aOther.
size_k() ),
152 mSizeL( aOther.
size_l() ),
153 mOffsetJ( aOther.mOffsetJ ),
154 mOffsetK( aOther.mOffsetK ),
155 mOffsetL( aOther.mOffsetL ),
156 mOrder( aOther.mOrder ),
157 mCapacity( aOther.mCapacity )
159 mData = aOther.mData ;
160 aOther.mData = nullptr ;
238 std::fill_n( mData, mCapacity, aValue );
264 T K =
E / ( 3. * ( 1.0 - 2.0 *
nu ) );
267 T G =
E / ( 2.0 * ( 1.0 +
nu ) );
293 aPoisson23, aPoisson31, aPoisson12,
294 aShear23, aShear31, aShear12, tS );
322 return mSizeI == 3 && mSizeJ == 3 && mSizeK == 3 && mSizeL == 3 ;
328 print(
const string aLabel=
"Tensor")
331 fprintf( stdout,
" %s:\n", aLabel.c_str() );
333 for(
index_t l=0; l<mSizeL; ++l )
335 for(
index_t k=0; k<mSizeK; ++k )
337 for(
index_t j=0; j<mSizeJ; ++j )
339 for(
index_t i=0; i<mSizeI; ++i )
341 fprintf( stdout,
" %u : ( %u, %u, %u, %u ) = %12.3f\n",
342 (
unsigned int ) tCount,
347 (
double ) mData[ tCount ] );
368 BELFEM_ASSERT( mOrder == 3,
"Tensor order must be of order 3" );
370 BELFEM_ASSERT( I < mSizeI,
"Index i out of bounds ( %u vs %u )",
372 (
unsigned int ) mSizeI );
374 BELFEM_ASSERT( J < mSizeJ,
"Index j out of bounds ( %u vs %u )",
376 (
unsigned int ) mSizeJ );
378 BELFEM_ASSERT( K < mSizeK,
"Index k out of bounds ( %u vs %u )",
380 (
unsigned int ) mSizeK );
382 return mData[ K * mOffsetK + J * mOffsetJ + I ] ;
396 BELFEM_ASSERT( mOrder == 3,
"Tensor order must be of order 3" );
398 BELFEM_ASSERT( I < mSizeI,
"Index i out of bounds ( %u vs %u )",
400 (
unsigned int ) mSizeI );
402 BELFEM_ASSERT( J < mSizeJ,
"Index j out of bounds ( %u vs %u )",
404 (
unsigned int ) mSizeJ );
406 BELFEM_ASSERT( K < mSizeK,
"Index k out of bounds ( %u vs %u )",
408 (
unsigned int ) mSizeK );
410 return mData[ K * mOffsetK + J * mOffsetJ + I ] ;
425 BELFEM_ASSERT( mOrder == 4,
"Tensor order must be of order 4" );
427 BELFEM_ASSERT( I < mSizeI,
"Index i out of bounds ( %u vs %u )",
429 (
unsigned int ) mSizeI );
431 BELFEM_ASSERT( J < mSizeJ,
"Index j out of bounds ( %u vs %u )",
433 (
unsigned int ) mSizeJ );
435 BELFEM_ASSERT( K < mSizeK,
"Index k out of bounds ( %u vs %u )",
437 (
unsigned int ) mSizeK );
439 BELFEM_ASSERT( L < mSizeL,
"Index l out of bounds ( %u vs %u )",
441 (
unsigned int ) mSizeL );
443 return mData[ L * mOffsetL + K * mOffsetK + J * mOffsetJ + I ] ;
458 BELFEM_ASSERT( mOrder == 4,
"Tensor order must be of order 4" );
460 BELFEM_ASSERT( I < mSizeI,
"Index i out of bounds ( %u vs %u )",
462 (
unsigned int ) mSizeI );
464 BELFEM_ASSERT( J < mSizeJ,
"Index j out of bounds ( %u vs %u )",
466 (
unsigned int ) mSizeJ );
468 BELFEM_ASSERT( K < mSizeK,
"Index k out of bounds ( %u vs %u )",
470 (
unsigned int ) mSizeK );
472 BELFEM_ASSERT( L < mSizeL,
"Index l out of bounds ( %u vs %u )",
474 (
unsigned int ) mSizeL );
476 return mData[ L * mOffsetL + K * mOffsetK + J * mOffsetJ + I ] ;
489 if(
this == &aTensor )
return *
this;
492 "Tensor orders must match for copy assignment" );
495 "Tensor dimensions must match for copy assignment" );
498 "Tensor dimensions must match for copy assignment" );
501 "Tensor dimensions must match for copy assignment" );
504 "Tensor dimensions must match for copy assignment" );
506 std::memcpy( mData, aTensor.
data(), mCapacity *
sizeof( T ) );
518 if(
this == &aTensor )
return *
this;
521 "Tensor orders must match for move assignment" );
524 "Tensor dimensions must match for move assignment" );
527 "Tensor dimensions must match for move assignment" );
530 "Tensor dimensions must match for move assignment" );
533 "Tensor dimensions must match for move assignment" );
536 mData = aTensor.mData ;
537 aTensor.mData = nullptr ;
546 this->
fill( aScalar );
569 std::for_each( mData, mData + mCapacity,
570 [ aScalar ]( T & tVal )
571 { tVal += aScalar; } );
581 "Tensor orders must match for addition operator" );
584 "Tensor dimensions must match for addition operator" );
587 "Tensor dimensions must match for addition operator" );
590 "Tensor dimensions must match for addition operator" );
593 "Tensor dimensions must match for addition operator" );
595 std::transform( mData, mData + mCapacity, aTensor.
data(),
596 mData, std::plus< T >() );
607 std::for_each( mData, mData + mCapacity,
608 [ aScalar ]( T & tVal )
609 { tVal -= aScalar; } );
619 "Tensor orders must match for subtraction operator" );
622 "Tensor dimensions must match for subtraction operator" );
625 "Tensor dimensions must match for subtraction operator" );
628 "Tensor dimensions must match for subtraction operator" );
631 "Tensor dimensions must match for subtraction operator" );
633 std::transform( mData, mData + mCapacity, aTensor.
data(),
634 mData, std::minus< T >() );
645 std::for_each( mData, mData + mCapacity,
646 [ aScalar ]( T & tVal )
647 { tVal *= aScalar; } );
658 std::for_each( mData, mData + mCapacity,
659 [ aScalar ]( T & tVal )
660 { tVal /= aScalar; } );
696 "operating tensor must be 3x3x3x3" );
700 "argument matrix must be allocated as 3x3" );
704 "target matrix must be allocated as 3x3" );
722 "Matrix must be allocated as 6x6" );
731 template<
typename T >
737 "Tensor orders must match for addition operator" );
740 "Tensor dimensions must match for addition operator" );
743 "Tensor dimensions must match for addition operator" );
746 "Tensor dimensions must match for addition operator" );
749 "Tensor dimensions must match for addition operator" );
759 template<
typename T >
765 "Tensor orders must match for subtraction operator" );
768 "Tensor dimensions must match for subtraction operator" );
771 "Tensor dimensions must match for subtraction operator" );
774 "Tensor dimensions must match for subtraction operator" );
777 "Tensor dimensions must match for subtraction operator" );
788 template<
typename T >
794 "Both tensors must be of size 3x3x3x3" );
803 template<
typename T >
809 "Tensor A must be of size 3x3x3x3" );
812 "when contracting a 3x3x3x3 tensor with a matrix, latter one must be 3x3" );
821 template<
typename T >
#define BELFEM_ASSERT(aCheck,...)
Definition assert.hpp:244
size_t n_rows() const
Definition cl_AR_Matrix.hpp:187
size_t n_cols() const
Definition cl_AR_Matrix.hpp:195
Third- or fourth-order tensor container; the constitutive helpers (contraction, rotation,...
Definition cl_Tensor.hpp:43
Tensor(Tensor< T > &&aOther)
Definition cl_Tensor.hpp:148
Tensor< T > & operator=(const T &aScalar)
Definition cl_Tensor.hpp:544
Tensor(const Tensor< T > &aOther)
Definition cl_Tensor.hpp:130
Tensor< T > & operator+=(const Tensor< T > &aTensor)
Definition cl_Tensor.hpp:578
void fill(const T aA, const T aB)
fill tensor in an isotropic way
Definition cl_Tensor.hpp:247
Tensor< T > & operator+=(const T &aScalar)
Definition cl_Tensor.hpp:567
index_t order() const
Definition cl_Tensor.hpp:198
void to_matrix(Matrix< real > &aMatrix)
converts a tensor to the elastitity matrix in Voigt notation
Definition cl_Tensor.hpp:718
void ddot(const Matrix< T > &aB, Matrix< T > &aC)
contract with 3x3 matrox
Definition cl_Tensor.hpp:693
Tensor< T > & operator*=(const T &aScalar)
Definition cl_Tensor.hpp:643
Tensor< T > & operator=(Tensor< T > &&aTensor)
move assignment
Definition cl_Tensor.hpp:516
T * data()
expose the underlying raw pointer
Definition cl_Tensor.hpp:211
Tensor< T > & operator=(const Matrix< T > &aElasticityMatrix)
Definition cl_Tensor.hpp:553
void fill_orthotropic_elasticity(const T aYoung1, const T aYoung2, const T aYoung3, const T aPoisson23, const T aPoisson31, const T aPoisson12, const T aShear23, const T aShear31, const T aShear12)
Definition cl_Tensor.hpp:276
index_t size_j() const
Definition cl_Tensor.hpp:181
Tensor(const index_t aSizeI, const index_t aSizeJ, const index_t aSizeK, const index_t aSizeL, const real aValue)
Definition cl_Tensor.hpp:106
~Tensor()
Definition cl_Tensor.hpp:165
void ddot(const Tensor< T > &aB, Tensor< T > &aC)
contract with other tensor
Definition cl_Tensor.hpp:675
bool is_3333() const
returns true if this is a 3x3x3x3 tensor
Definition cl_Tensor.hpp:320
T & operator()(const index_t I, const index_t J, const index_t K)
access operator ( writable version )
Definition cl_Tensor.hpp:363
void print(const string aLabel="Tensor")
Definition cl_Tensor.hpp:328
index_t size_l() const
Definition cl_Tensor.hpp:193
index_t capacity() const
memory size
Definition cl_Tensor.hpp:309
Tensor< T > & operator=(const Tensor< T > &aTensor)
copy assignment
Definition cl_Tensor.hpp:487
const T & operator()(const index_t I, const index_t J, const index_t K, const index_t L) const
access operator ( const version )
Definition cl_Tensor.hpp:452
Tensor< T > & operator-=(const T &aScalar)
Definition cl_Tensor.hpp:605
Tensor< T > & operator-=(const Tensor< T > &aTensor)
Definition cl_Tensor.hpp:616
Tensor(const index_t aSizeI, const index_t aSizeJ, const index_t aSizeK, const index_t aSizeL)
Definition cl_Tensor.hpp:67
Tensor(const Matrix< real > &aElasticityMatrix)
Definition cl_Tensor.hpp:117
const T & operator()(const index_t I, const index_t J, const index_t K) const
access operator ( const version )
Definition cl_Tensor.hpp:391
T & operator()(const index_t I, const index_t J, const index_t K, const index_t L)
access operator ( writable version )
Definition cl_Tensor.hpp:419
void fill(const T aValue)
fill all values
Definition cl_Tensor.hpp:235
Tensor(const index_t aSizeI, const index_t aSizeJ, const index_t aSizeK)
Definition cl_Tensor.hpp:87
Tensor< T > & operator/=(const T &aScalar)
Definition cl_Tensor.hpp:656
void fill_isotropic_elasticity(const T E, const T nu)
special funcition to create an isotropic elasticity tensor
Definition cl_Tensor.hpp:259
index_t size_k() const
Definition cl_Tensor.hpp:187
const T * data() const
expose the underlying raw pointer ( const version )
Definition cl_Tensor.hpp:222
index_t size_i() const
Definition cl_Tensor.hpp:175
Inverse of a square matrix.
void fill(T *A, const T a, const T b)
Definition fn_TR_fill.hpp:29
void contract42(const T *A, const T *B, T *C)
tensor contraction A_ijkl * B_kl = C_ij
Definition fn_TR_contract42_arma.hpp:28
void mat_to_ten(const T *C, T *A)
elasticity matrix conversion elasticity matrix C => tensor A
Definition fn_TR_mat_to_ten.hpp:27
void contract44(const T *A, const T *B, T *C)
tensor contraction A_ijmn * B_mnkl = C_ijkl
Definition fn_TR_contract44.hpp:26
bool equal_equal(const T *A, const T *B, const index_t aCapacity)
Definition fn_TR_equal_equal.hpp:23
void ten_to_mat(const T *A, T *C)
elasticity matrix conversion Elasticity tensor A => elasticity matrix C
Definition fn_TR_ten_to_mat.hpp:28
USER GUIDES:
Definition cl_Capacitor.cpp:16
auto operator+(const Matrix< T > &aA, const Matrix< T > &aB) -> decltype(aA.matrix_data()+aB.matrix_data())
Definition op_MatrixPlus.hpp:23
void compliance_matrix(const T &aE1, const T &aE2, const T &aE3, const T &aNu23, const T &aNu31, const T &aNu12, const T &aG23, const T &aG31, const T &aG12, Matrix< T > &aS)
orthotropic compliance matrix in Voigt order 11, 22, 33, 23, 31, 12.
Definition fn_compliance_matrix.hpp:33
@ nu
Definition cl_Material.hpp:156
@ E
Definition cl_Material.hpp:155
auto operator-(const Matrix< T > &aA, const Matrix< T > &aB) -> decltype(aA.matrix_data() - aB.matrix_data())
Definition op_MatrixMinus.hpp:23
uint32_t index_t
Definition typedefs.hpp:52
auto inv(const T &aExpression) -> decltype(arma::inv(aExpression))
Definition fn_AR_inv.hpp:22
auto operator%(const Vector< T > &aA, const Vector< T > &aB) -> decltype(aA.vector_data() % aB.vector_data())
Definition op_AR_VectorElementwiseMultiplication.hpp:22
double real
Definition typedefs.hpp:36
bool operator==(const Matrix< T > &aA, const Matrix< T > &aB)
Definition op_AR_MatrixEqualEqual.hpp:23