6#include "eigen3/Eigen/Core"
7#include "eigen3/Eigen/Dense"
8#include "eigen3/unsupported/Eigen/CXX11/Tensor"
29 using Matrix = Eigen::Matrix<double, nsd, nsd>;
35 using Tensor = Eigen::TensorFixedSize<double, Eigen::Sizes<nsd, nsd, nsd, nsd>>;
42 using NodalMatrix = Eigen::Matrix<double, nsd, Eigen::Dynamic, 0, nsd, consts::maxNoN>;
47 using NodalVector = Eigen::Matrix<double, Eigen::Dynamic, 1, 0, consts::maxNoN, 1>;
56 Eigen::Map<const Eigen::Matrix<double, rows, Eigen::Dynamic>>
58 if (A.nrows() != rows) {
59 svmp::raise<svmp::FE::InvalidArgumentException>(
60 "A view of " + std::to_string(rows) +
" rows was requested for an array with " +
61 std::to_string(A.nrows()) +
" rows.");
63 return {A.data(), rows, A.ncols()};
67 inline Eigen::Map<const Eigen::MatrixXd>
69 return {A.data(), A.nrows(), A.ncols()};
86 Eigen::Map<const Eigen::Matrix<double, rows, Eigen::Dynamic>, 0, Eigen::OuterStride<>>
88 if (first < 0 || first + rows > A.nrows()) {
89 svmp::raise<svmp::FE::InvalidArgumentException>(
90 "A view of " + std::to_string(rows) +
" rows starting at row " + std::to_string(first) +
91 " was requested for an array with " + std::to_string(A.nrows()) +
" rows.");
93 return {A.data() + first, rows, A.ncols(), Eigen::OuterStride<>(A.nrows())};
97 inline Eigen::Map<Eigen::MatrixXd>
99 return {A.data(), A.nrows(), A.ncols()};
103 inline Eigen::Map<const Eigen::VectorXd>
105 return {v.data(), v.size()};
112 Eigen::Map<const Eigen::Matrix<double, rows, 1>>
114 if (v.size() != rows) {
115 svmp::raise<svmp::FE::InvalidArgumentException>(
116 "A view of " + std::to_string(rows) +
" entries was requested for a vector with " +
117 std::to_string(v.size()) +
" entries.");
119 return Eigen::Map<const Eigen::Matrix<double, rows, 1>>(v.data());
123 template <
typename MatrixType>
124 MatrixType convert_to_eigen_matrix(
const Array<double>& src) {
126 for (
int i = 0; i < mat.rows(); ++i)
127 for (
int j = 0; j < mat.cols(); ++j)
128 mat(i, j) = src(i, j);
133 template <
typename MatrixType>
134 void convert_to_array(
const MatrixType& mat, Array<double>& dest) {
135 for (
int i = 0; i < mat.rows(); ++i)
136 for (
int j = 0; j < mat.cols(); ++j)
137 dest(i, j) = mat(i, j);
141 template <
typename MatrixType>
142 void copy_Dm(
const MatrixType& mat, Array<double>& dest) {
143 if ((mat.rows() != dest.nrows()) || (mat.cols() != dest.ncols())) {
144 const std::string mat_dims =
"(" + std::to_string(mat.rows()) +
"x" + std::to_string(mat.cols()) +
")";
145 const std::string dest_dims =
"(" + std::to_string(dest.nrows()) +
"x" + std::to_string(dest.ncols()) +
")";
146 svmp::raise<svmp::FE::InvalidArgumentException>(
147 "The 'mat" + mat_dims +
"' and 'dest" + dest_dims +
148 "' arrays have incompatible sizes.");
151 for (
int i = 0; i < mat.rows(); ++i) {
152 for (
int j = 0; j < mat.cols(); ++j) {
153 dest(i, j) = mat(i, j);
159 Eigen::Matrix<double, nsd, 1> cross_product(
const Eigen::Matrix<double, nsd, 1>& u,
const Eigen::Matrix<double, nsd, 1>& v) {
160 if constexpr (nsd == 2) {
161 return Eigen::Matrix<double, 2, 1>(v(1), - v(0));
163 else if constexpr (nsd == 3) {
167 throw std::runtime_error(
"[cross_product] Invalid number of spatial dimensions '" + std::to_string(nsd) +
"'. Valid dimensions are 2 or 3.");
171 double mat_ddot(
const Array<double>& A,
const Array<double>& B,
const int nd);
175 return A.cwiseProduct(B).sum();
178 double mat_det(
const Array<double>& A,
const int nd);
179 Array<double> mat_dev(
const Array<double>& A,
const int nd);
181 Array<double>
mat_dyad_prod(
const Vector<double>& u,
const Vector<double>& v,
const int nd);
183 Array<double> mat_id(
const int nsd);
184 Array<double>
mat_inv(
const Array<double>& A,
const int nd,
bool debug =
false);
185 Array<double>
mat_inv_ge(
const Array<double>& A,
const int nd,
bool debug =
false);
186 Array<double>
mat_inv_lp(
const Array<double>& A,
const int nd);
195 Vector<double>
mat_mul(
const Array<double>& A,
const Vector<double>& v);
206 Array<double>
mat_mul(
const Array<double>& A,
const Array<double>& B);
217 void mat_mul(
const Array<double>& A,
const Array<double>& B, Array<double>& result);
235 template <
int M,
int K,
int N>
236 void mat_mul(
const Array<double>& A,
const Array<double>& B,
239 Eigen::Map<const Eigen::Matrix<double, M, K>> a(A.data());
240 Eigen::Map<const Eigen::Matrix<double, K, N>> b(B.data());
241 Eigen::Map<Eigen::Matrix<double, M, N>> c(C.data());
251 template <
int M,
int K>
252 void mat_mul(
const Array<double>& A,
const Array<double>& B,
255 Eigen::Map<const Eigen::Matrix<double, M, K>> a(A.data());
256 Eigen::Map<const Eigen::Matrix<double, K, Eigen::Dynamic>> b(B.data(), K, B.ncols());
257 Eigen::Map<Eigen::Matrix<double, M, Eigen::Dynamic>> c(C.data(), M, C.ncols());
262 Array<double>
mat_symm(
const Array<double>& A,
const int nd);
265 double mat_trace(
const Array<double>& A,
const int nd);
285 double_dot_product(
const Tensor<nsd>& A,
const std::array<int, 2>& dimsA,
286 const Tensor<nsd>& B,
const std::array<int, 2>& dimsB) {
289 if (dimsA[0] == 2 && dimsA[1] == 3 && dimsB[0] == 2 && dimsB[1] == 3) {
290 constexpr int N = nsd * nsd;
292 Eigen::Map<const Eigen::Matrix<double, N, N>> a(A.data());
293 Eigen::Map<const Eigen::Matrix<double, N, N>> b(B.data());
294 Eigen::Map<Eigen::Matrix<double, N, N>> c(C.data());
295 c.noalias() = a * b.transpose();
300 Eigen::array<Eigen::IndexPair<int>, 2> contractionDims = {
301 Eigen::IndexPair<int>(dimsA[0], dimsB[0]),
302 Eigen::IndexPair<int>(dimsA[1], dimsB[1])
306 return A.contract(B, contractionDims);
324 constexpr int N = nsd * nsd;
327 Eigen::Map<const Eigen::Matrix<double, N, 1>> a(A.data());
328 Eigen::Map<const Eigen::Matrix<double, N, 1>> b(B.data());
329 Eigen::Map<Eigen::Matrix<double, N, N>> c(C.data());
330 c.noalias() = a * b.transpose();
351 for (
int i = 0; i < nsd; ++i) {
352 for (
int j = 0; j < nsd; ++j) {
378 for (
int l = 0; l < nsd; ++l) {
379 for (
int k = 0; k < nsd; ++k) {
381 Eigen::Map<Eigen::Matrix<double, nsd, nsd>> blk(C.data() + nsd * nsd * (k + nsd * l));
382 blk.noalias() = 0.5 * (A.col(k) * B.col(l).transpose()
383 + A.col(l) * B.col(k).transpose());
408 for (
int i = 0; i < nsd; ++i) {
409 for (
int j = 0; j < nsd; ++j) {
410 for (
int k = 0; k < nsd; ++k) {
411 for (
int l = 0; l < nsd; ++l) {
412 B(i,j,k,l) = A(k,l,i,j);
421 Array<double>
transpose(
const Array<double>& A);
Exception hierarchy for error handling in the FE library.
The Tensor4 template class implements a simple interface to 4th order tensors.
Definition Tensor4.h:20
The Vector template class is used for storing int and double data.
Definition Vector.h:26
The classes defined here duplicate the data structures in the Fortran MATFUN module defined in MATFUN...
Definition mat_fun.cpp:15
Eigen::Matrix< double, nsd, nsd > Matrix
A 2nd order tensor, nsd x nsd, fixed size and stack allocated. Used for the deformation gradient,...
Definition mat_fun.h:29
Tensor< nsd > symmetric_dyadic_product(const Matrix< nsd > &A, const Matrix< nsd > &B)
Create a 4th order tensor from symmetric outer product of two matrices: C_ijkl = 0....
Definition mat_fun.h:372
Eigen::Map< Eigen::MatrixXd > eigen_view_mutable(Array< double > &A)
Writable Eigen view of a whole Array, sharing its storage.
Definition mat_fun.h:98
void ten_init(const int nd)
Initialize tensor index pointer.
Definition mat_fun.cpp:747
Tensor4< double > ten_symm_prod(const Array< double > &A, const Array< double > &B, const int nd)
Create a 4th order tensor from symmetric outer product of two matrices.
Definition mat_fun.cpp:839
Eigen::Map< const Eigen::Matrix< double, rows, Eigen::Dynamic > > eigen_view(const Array< double > &A)
Read-only Eigen view of an Array, sharing its storage.
Definition mat_fun.h:57
Array< double > ten_mddot(const Tensor4< double > &A, const Array< double > &B, const int nd)
Double dot product of a 4th order tensor and a 2nd order tensor.
Definition mat_fun.cpp:809
Eigen::Matrix< double, Eigen::Dynamic, 1, 0, consts::maxNoN, 1 > NodalVector
One scalar per element node, so eNoN entries. Dynamic length bounded by consts::maxNoN to stay stack ...
Definition mat_fun.h:47
Array< double > mat_symm(const Array< double > &A, const int nd)
Symmetric part of a matrix, S = (A + A.T)/2.
Definition mat_fun.cpp:542
Tensor4< double > ten_ids(const int nd)
Create a 4th order order symmetric identity tensor.
Definition mat_fun.cpp:790
Array< double > transpose(const Array< double > &A)
Reproduces Fortran TRANSPOSE.
Definition mat_fun.cpp:875
Vector< double > mat_mul(const Array< double > &A, const Vector< double > &v)
Multiply a matrix by a vector.
Definition mat_fun.cpp:433
double mat_trace(const Array< double > &A, const int nd)
Trace of second order matrix of rank nd.
Definition mat_fun.cpp:574
Tensor4< double > ten_ddot(const Tensor4< double > &A, const Tensor4< double > &B, const int nd)
Double dot product of 2 4th order tensors T_ijkl = A_ijmn * B_klmn.
Definition mat_fun.cpp:613
Tensor4< double > ten_dyad_prod(const Array< double > &A, const Array< double > &B, const int nd)
Create a 4th order tensor from outer product of two matrices.
Definition mat_fun.cpp:771
Array< double > mat_inv_lp(const Array< double > &A, const int nd)
This function computes inverse of a square matrix using Lapack functions (DGETRF + DGETRI).
Definition mat_fun.cpp:397
Array< double > mat_inv(const Array< double > &A, const int nd, bool debug)
This function computes inverse of a square matrix.
Definition mat_fun.cpp:110
Array< double > mat_inv_ge(const Array< double > &Ain, const int n, bool debug)
This function computes inverse of a square matrix using Gauss Elimination method.
Definition mat_fun.cpp:167
Eigen::Map< const Eigen::Matrix< double, rows, Eigen::Dynamic >, 0, Eigen::OuterStride<> > eigen_view_rows(const Array< double > &A, const int first)
Read-only Eigen view of a band of rows of an Array, sharing its storage.
Definition mat_fun.h:87
Array< double > mat_symm_prod(const Vector< double > &u, const Vector< double > &v, const int nd)
Create a matrix from symmetric product of two vectors.
Definition mat_fun.cpp:559
Array< double > mat_dyad_prod(const Vector< double > &u, const Vector< double > &v, const int nd)
Create a matrix from outer product of two vectors.
Definition mat_fun.cpp:82
Tensor< nsd > fourth_order_identity()
Create a 4th order identity tensor: I_ijkl = 0.5 * (δ_ik * δ_jl + δ_il * δ_jk).
Definition mat_fun.h:345
double mat_ddot(const Array< double > &A, const Array< double > &B, const int nd)
Double dot product of 2 square matrices.
Definition mat_fun.cpp:21
Tensor4< double > ten_ddot_2412(const Tensor4< double > &A, const Tensor4< double > &B, const int nd)
T_ijkl = A_imjn * B_mnkl.
Definition mat_fun.cpp:658
Eigen::TensorFixedSize< double, Eigen::Sizes< nsd, nsd, nsd, nsd > > Tensor
A 4th order tensor, nsd x nsd x nsd x nsd, fixed size and stack allocated. Used for the material elas...
Definition mat_fun.h:35
Tensor4< double > ten_asym_prod12(const Array< double > &A, const Array< double > &B, const int nd)
Create a 4th order tensor from antisymmetric outer product of two matrices.
Definition mat_fun.cpp:591
Eigen::Matrix< double, nsd, Eigen::Dynamic, 0, nsd, consts::maxNoN > NodalMatrix
One nsd-vector per element node, so nsd x eNoN. Row count is fixed at compile time while column count...
Definition mat_fun.h:42
Tensor< nsd > dyadic_product(const Matrix< nsd > &A, const Matrix< nsd > &B)
Compute the dyadic product of two 2nd order tensors A and B, C_ijkl = A_ij * B_kl.
Definition mat_fun.h:321