svMultiPhysics
Loading...
Searching...
No Matches
mat_fun.h
1// SPDX-FileCopyrightText: Copyright (c) Stanford University, The Regents of the University of California, and others.
2// SPDX-License-Identifier: BSD-3-Clause
3
4#ifndef MAT_FUN_H
5#define MAT_FUN_H
6#include "eigen3/Eigen/Core"
7#include "eigen3/Eigen/Dense"
8#include "eigen3/unsupported/Eigen/CXX11/Tensor"
9#include <stdexcept>
10
11#include "Array.h"
12#include "Tensor4.h"
13#include "Vector.h"
14
15/// @brief The classes defined here duplicate the data structures in the
16/// Fortran MATFUN module defined in MATFUN.f.
17///
18/// This module defines data structures for generally performed matrix and tensor operations.
19///
20/// \todo [TODO:DaveP] this should just be a namespace?
21//
22namespace mat_fun {
23 // Define templated type aliases for Eigen matrices and tensors for convenience
24 template<size_t nsd>
25 using Matrix = Eigen::Matrix<double, nsd, nsd>;
26
27 template<size_t nsd>
28 using Tensor = Eigen::TensorFixedSize<double, Eigen::Sizes<nsd, nsd, nsd, nsd>>;
29
30 // Function to convert Array<double> to Eigen::Matrix
31 template <typename MatrixType>
32 MatrixType convert_to_eigen_matrix(const Array<double>& src) {
33 MatrixType mat;
34 for (int i = 0; i < mat.rows(); ++i)
35 for (int j = 0; j < mat.cols(); ++j)
36 mat(i, j) = src(i, j);
37 return mat;
38 }
39
40 // Function to convert Eigen::Matrix to Array<double>
41 template <typename MatrixType>
42 void convert_to_array(const MatrixType& mat, Array<double>& dest) {
43 for (int i = 0; i < mat.rows(); ++i)
44 for (int j = 0; j < mat.cols(); ++j)
45 dest(i, j) = mat(i, j);
46 }
47
48 // Function to convert a higher-dimensional array like Dm
49 template <typename MatrixType>
50 void copy_Dm(const MatrixType& mat, Array<double>& dest, int rows, int cols) {
51 for (int i = 0; i < rows; ++i)
52 for (int j = 0; j < cols; ++j)
53 dest(i, j) = mat(i, j);
54 }
55
56 template <int nsd>
57 Eigen::Matrix<double, nsd, 1> cross_product(const Eigen::Matrix<double, nsd, 1>& u, const Eigen::Matrix<double, nsd, 1>& v) {
58 if constexpr (nsd == 2) {
59 return Eigen::Matrix<double, 2, 1>(v(1), - v(0));
60 }
61 else if constexpr (nsd == 3) {
62 return u.cross(v);
63 }
64 else {
65 throw std::runtime_error("[cross_product] Invalid number of spatial dimensions '" + std::to_string(nsd) + "'. Valid dimensions are 2 or 3.");
66 }
67 }
68
69 double mat_ddot(const Array<double>& A, const Array<double>& B, const int nd);
70
71 template <int nsd>
72 double double_dot_product(const Matrix<nsd>& A, const Matrix<nsd>& B) {
73 return A.cwiseProduct(B).sum();
74 }
75
76 double mat_det(const Array<double>& A, const int nd);
77 Array<double> mat_dev(const Array<double>& A, const int nd);
78
79 Array<double> mat_dyad_prod(const Vector<double>& u, const Vector<double>& v, const int nd);
80
81 Array<double> mat_id(const int nsd);
82 Array<double> mat_inv(const Array<double>& A, const int nd, bool debug = false);
83 Array<double> mat_inv_ge(const Array<double>& A, const int nd, bool debug = false);
84 Array<double> mat_inv_lp(const Array<double>& A, const int nd);
85
86 Vector<double> mat_mul(const Array<double>& A, const Vector<double>& v);
87 Array<double> mat_mul(const Array<double>& A, const Array<double>& B);
88 void mat_mul(const Array<double>& A, const Array<double>& B, Array<double>& result);
89 void mat_mul6x3(const Array<double>& A, const Array<double>& B, Array<double>& C);
90
91 Array<double> mat_symm(const Array<double>& A, const int nd);
92 Array<double> mat_symm_prod(const Vector<double>& u, const Vector<double>& v, const int nd);
93
94 double mat_trace(const Array<double>& A, const int nd);
95
96 Tensor4<double> ten_asym_prod12(const Array<double>& A, const Array<double>& B, const int nd);
97 Tensor4<double> ten_ddot(const Tensor4<double>& A, const Tensor4<double>& B, const int nd);
98 Tensor4<double> ten_ddot_2412(const Tensor4<double>& A, const Tensor4<double>& B, const int nd);
99 Tensor4<double> ten_ddot_3424(const Tensor4<double>& A, const Tensor4<double>& B, const int nd);
100
101 /**
102 * @brief Contracts two 4th order tensors A and B over two dimensions,
103 *
104 */
105 template <int nsd>
106 Tensor<nsd>
107 double_dot_product(const Tensor<nsd>& A, const std::array<int, 2>& dimsA,
108 const Tensor<nsd>& B, const std::array<int, 2>& dimsB) {
109
110 // Define the contraction dimensions
111 Eigen::array<Eigen::IndexPair<int>, 2> contractionDims = {
112 Eigen::IndexPair<int>(dimsA[0], dimsB[0]), // Contract A's dimsA[0] with B's dimsB[0]
113 Eigen::IndexPair<int>(dimsA[1], dimsB[1]) // Contract A's dimsA[1] with B's dimsB[1]
114 };
115
116 // Return the double dot product
117 return A.contract(B, contractionDims);
118
119 // For some reason, in this case the Eigen::Tensor contract function is
120 // faster than a for loop implementation.
121 }
122
123 Tensor4<double> ten_dyad_prod(const Array<double>& A, const Array<double>& B, const int nd);
124
125 /**
126 * @brief Compute the dyadic product of two 2nd order tensors A and B, C_ijkl = A_ij * B_kl
127 *
128 * @tparam nsd, the number of spatial dimensions
129 * @param A, the first 2nd order tensor
130 * @param B, the second 2nd order tensor
131 * @return Tensor<nsd>
132 */
133 template <int nsd>
134 Tensor<nsd>
135 dyadic_product(const Matrix<nsd>& A, const Matrix<nsd>& B) {
136 // Initialize the result tensor
137 Tensor<nsd> C;
138
139 // Compute the dyadic product: C_ijkl = A_ij * B_kl
140 for (int i = 0; i < nsd; ++i) {
141 for (int j = 0; j < nsd; ++j) {
142 for (int k = 0; k < nsd; ++k) {
143 for (int l = 0; l < nsd; ++l) {
144 C(i,j,k,l) = A(i,j) * B(k,l);
145 }
146 }
147 }
148 }
149 // For some reason, in this case the Eigen::Tensor contract function is
150 // slower than the for loop implementation
151
152 return C;
153 }
154
155 Tensor4<double> ten_ids(const int nd);
156
157 /**
158 * @brief Create a 4th order identity tensor:
159 * I_ijkl = 0.5 * (δ_ik * δ_jl + δ_il * δ_jk)
160 *
161 * @tparam nsd, the number of spatial dimensions
162 * @return Tensor<nsd>
163 */
164 template <int nsd>
165 Tensor<nsd>
167 // Initialize as zero
168 Tensor<nsd> I;
169 I.setZero();
170
171 // Set only non-zero entries
172 for (int i = 0; i < nsd; ++i) {
173 for (int j = 0; j < nsd; ++j) {
174 I(i,j,i,j) += 0.5;
175 I(i,j,j,i) += 0.5;
176 }
177 }
178
179 return I;
180 }
181
182 Array<double> ten_mddot(const Tensor4<double>& A, const Array<double>& B, const int nd);
183
184 Tensor4<double> ten_symm_prod(const Array<double>& A, const Array<double>& B, const int nd);
185
186 /// @brief Create a 4th order tensor from symmetric outer product of two matrices: C_ijkl = 0.5 * (A_ik * B_jl + A_il * B_jk)
187 ///
188 /// Reproduces 'FUNCTION TEN_SYMMPROD(A, B, nd) RESULT(C)'.
189 //
190 template <int nsd>
191 Tensor<nsd>
192 symmetric_dyadic_product(const Matrix<nsd>& A, const Matrix<nsd>& B) {
193
194 // Initialize the result tensor
195 Tensor<nsd> C;
196
197 // Compute the symmetric product: C_ijkl = 0.5 * (A_ik * B_jl + A_il * B_jk)
198 for (int i = 0; i < nsd; ++i) {
199 for (int j = 0; j < nsd; ++j) {
200 for (int k = 0; k < nsd; ++k) {
201 for (int l = 0; l < nsd; ++l) {
202 C(i,j,k,l) = 0.5 * (A(i,k) * B(j,l) + A(i,l) * B(j,k));
203 }
204 }
205 }
206 }
207 // For some reason, in this case the for loop implementation is faster
208 // than the Eigen::Tensor contract method
209
210 // Return the symmetric product
211 return C;
212 }
213
214 Tensor4<double> ten_transpose(const Tensor4<double>& A, const int nd);
215
216 /**
217 * @brief Performs a tensor transpose operation on a 4th order tensor A, B_ijkl = A_klij
218 *
219 * @tparam nsd, the number of spatial dimensions
220 * @param A, the input 4th order tensor
221 * @return Tensor<nsd>
222 */
223 template <int nsd>
224 Tensor<nsd>
225 transpose(const Tensor<nsd>& A) {
226
227 // Initialize the result tensor
228 Tensor<nsd> B;
229
230 // Permute the tensor indices to perform the transpose operation
231 for (int i = 0; i < nsd; ++i) {
232 for (int j = 0; j < nsd; ++j) {
233 for (int k = 0; k < nsd; ++k) {
234 for (int l = 0; l < nsd; ++l) {
235 B(i,j,k,l) = A(k,l,i,j);
236 }
237 }
238 }
239 }
240
241 return B;
242 }
243
244 Array<double> transpose(const Array<double>& A);
245
246 void ten_init(const int nd);
247
248};
249
250#endif
251
The Tensor4 template class implements a simple interface to 4th order tensors.
Definition Tensor4.h:18
The Vector template class is used for storing int and double data.
Definition Vector.h:23
The classes defined here duplicate the data structures in the Fortran MATFUN module defined in MATFUN...
Definition mat_fun.cpp:14
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:192
void ten_init(const int nd)
Initialize tensor index pointer.
Definition mat_fun.cpp:760
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:852
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:822
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:555
Tensor4< double > ten_ids(const int nd)
Create a 4th order order symmetric identity tensor.
Definition mat_fun.cpp:803
Array< double > transpose(const Array< double > &A)
Reproduces Fortran TRANSPOSE.
Definition mat_fun.cpp:888
Vector< double > mat_mul(const Array< double > &A, const Vector< double > &v)
Multiply a matrix by a vector.
Definition mat_fun.cpp:466
double mat_trace(const Array< double > &A, const int nd)
Trace of second order matrix of rank nd.
Definition mat_fun.cpp:587
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:626
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:784
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:396
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:109
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:166
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:572
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:81
Tensor< nsd > fourth_order_identity()
Create a 4th order identity tensor: I_ijkl = 0.5 * (δ_ik * δ_jl + δ_il * δ_jk)
Definition mat_fun.h:166
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:20
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:671
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:604
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:135