suanPan
Loading...
Searching...
No Matches
tensor.h
Go to the documentation of this file.
1/*******************************************************************************
2 * Copyright (C) 2017-2025 Theodore Chang
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 ******************************************************************************/
29#ifndef TENSOR_H
30#define TENSOR_H
31
32#include <Toolbox/utility.h>
33
34template<typename T> class Quaternion;
35
36namespace tensor {
37 mat isotropic_stiffness(double, double);
38 mat orthotropic_stiffness(const vec&, const vec&);
39
43
44 static const vec unit_tensor2{1., 1., 1., 0., 0., 0.};
45
46 namespace stress {
47 // applies to 3D tensor only, either principal or not
48 double invariant1(const vec&);
49 // applies to 3D tensor only, either principal or not
50 double invariant2(const vec&);
51 // applies to 3D tensor only, either principal or not
52 double invariant3(const vec&);
53
54 // compute lode angle based on input stress
55 double lode(vec);
56 // compute derivative of lode angle based on input stress
57 vec lode_der(vec);
58
59 static const vec norm_weight{1., 1., 1., 2., 2., 2.};
60 } // namespace stress
61 namespace strain {
62 // applies to 3D tensor only, either principal or not
63 double invariant1(const vec&);
64 // applies to 3D tensor only, either principal or not
65 double invariant2(const vec&);
66 // applies to 3D tensor only, either principal or not
67 double invariant3(const vec&);
68
69 // compute lode angle based on input deviatoric strain
70 double lode(vec);
71
72 static const vec norm_weight{1., 1., 1., .5, .5, .5};
73 } // namespace strain
74 double trace2(const vec&);
75 double trace3(const vec&);
76 double mean3(const vec&);
77 vec dev(const vec&);
78 vec dev(vec&&);
79
80 mat dev(const mat&);
81 mat dev(mat&&);
82
83 namespace strain {
84 mat to_green(mat&&);
85 mat to_green(const mat&);
86 mat to_tensor(const vec&);
87 vec to_voigt(const mat&);
88 double norm(const vec&);
89 double norm(vec&&);
90 double double_contraction(const vec&);
91 double double_contraction(const vec&, const vec&);
92 double double_contraction(vec&&, vec&&);
93 } // namespace strain
94 namespace stress {
95 mat to_tensor(const vec&);
96 vec to_voigt(const mat&);
97 double norm(const vec&);
98 double norm(vec&&);
99 double double_contraction(const vec&);
100 double double_contraction(const vec&, const vec&);
101 double double_contraction(vec&&, vec&&);
102 } // namespace stress
103
104 namespace base {
105 class Base3D {
106 const vec3 g1, g2, g3;
107 mat33 g;
108
109 public:
110 Base3D(const vec3&, const vec3&, const vec3&);
111 [[nodiscard]] std::tuple<vec3, vec3, vec3> to_inverse() const;
112 };
113
114 vec3 unit_norm(const vec3&, const vec3&);
115 } // namespace base
116
117 mat diff_unit(const vec&);
118
119 mat diff_triad(const vec3&, const vec3&, const vec3&);
120} // namespace tensor
121
122namespace transform {
123 void hoffman_projection(const vec&, mat&, mat&);
124 mat hill_projection(double, double, double, double, double, double);
125
126 double atan2(const vec&);
129
130 mat eigen_to_tensor_base(const mat&);
131 mat eigen_to_tensile_stress(const vec&, const mat&);
132 mat eigen_to_tensile_derivative(const vec&, const mat&);
133
134 template<typename T> Mat<T> skew_symm(const Mat<T>& R) {
135 suanpan_assert([&] { if(R.n_elem != 3) throw invalid_argument("need 3 element vector"); });
136
137 Mat<T> S(3, 3, fill::zeros);
138
139 S(0, 1) = -(S(1, 0) = R(2));
140 S(2, 0) = -(S(0, 2) = R(1));
141 S(1, 2) = -(S(2, 1) = R(0));
142
143 return S;
144 }
145
146 template<typename T> concept HasEval = requires(const T& x) { { x.eval() } -> std::convertible_to<mat>; };
147
148 template<HasEval T> mat skew_symm(const T& R) { return skew_symm(R.eval()); }
149
150 template<typename T> Mat<T> rodrigues(const Mat<T>& R) { return arma::expmat(transform::skew_symm(R)); }
151
152 template<typename T> Quaternion<T> to_quaternion(const Mat<T>& R) {
153 if(3 == R.n_elem) {
154 const auto angle = arma::norm(R);
155
156 if(suanpan::approx_equal(angle, 0.)) return {0., 0., 0., 0.};
157
158 return {std::cos(.5 * angle), std::sin(.5 * angle) / angle * R};
159 }
160
161 if(9 == R.n_elem) {
162 const auto tr_r = arma::trace(R);
163 const auto max_r = arma::max(R.diag());
164 if(tr_r >= max_r) {
165 const auto q0 = .5 * std::sqrt(tr_r + 1.);
166 const auto q1 = .25 / q0 * (R(2, 1) - R(1, 2));
167 const auto q2 = .25 / q0 * (R(0, 2) - R(2, 0));
168 const auto q3 = .25 / q0 * (R(1, 0) - R(0, 1));
169 return {q0, q1, q2, q3};
170 }
171
172 for(auto I = 0; I < 3; ++I)
173 if(suanpan::approx_equal(R(I, I), max_r)) {
174 const auto J = (I + 1) % 3;
175 const auto K = (J + 1) % 3;
176 vec q(3, fill::none);
177 q(I) = std::sqrt(.5 * max_r + .25 * (1. - tr_r));
178 const double q0 = .25 / q(I) * (R(K, J) - R(J, K));
179 q(J) = .25 / q(I) * (R(J, I) + R(I, J));
180 q(K) = .25 / q(I) * (R(K, I) + R(I, K));
181
182 return {q0, std::move(q)};
183 }
184 }
185
186 throw invalid_argument("need either rotation vector or matrix");
187 }
188
189 template<typename T> Col<T> to_pseudo(const Mat<T>& R) {
190 const Mat<T> S = arma::real(arma::logmat(R));
191
192 return {S(2, 1), S(0, 2), S(1, 0)};
193 }
194
195 namespace strain {
196 double angle(const vec&);
197 mat trans(double);
198 vec principal(const vec&);
199 vec rotate(const vec&, double);
200 } // namespace strain
201 namespace stress {
202 double angle(const vec&);
203 mat trans(double);
204 vec principal(const vec&);
205 vec rotate(const vec&, double);
206 } // namespace stress
207 namespace beam {
208 mat global_to_local(double, double, double);
209 mat global_to_local(const vec&, double);
210 } // namespace beam
211 namespace triangle {
212 vec to_area_coordinate(const vec&, const mat&);
213 }
214} // namespace transform
215
216namespace suanpan {
217 template<typename T> T ramp(const T in) { return in > T(0) ? in : T(0); }
218} // namespace suanpan
219
220#endif
221
An Quaternion class.
Definition Quaternion.hpp:34
Definition tensor.h:105
std::tuple< vec3, vec3, vec3 > to_inverse() const
Definition tensor.cpp:283
Definition tensor.h:146
Definition MatrixModifier.hpp:36
T ramp(const T in)
Definition tensor.h:217
bool approx_equal(T x, T y, int ulp=2)
Definition utility.h:60
vec3 unit_norm(const vec3 &, const vec3 &)
Definition tensor.cpp:289
mat to_green(mat &&)
Definition tensor.cpp:292
mat to_tensor(const vec &)
Definition tensor.cpp:316
double invariant3(const vec &)
compute the third invariant of the given 3D strain tensor, could be either normal or deviatoric strai...
Definition tensor.cpp:112
double norm(const vec &)
Definition tensor.cpp:370
double invariant2(const vec &)
compute the second invariant of the given 3D strain tensor, could be either normal or deviatoric stra...
Definition tensor.cpp:100
double invariant1(const vec &)
compute the first invariant of the given 3D strain tensor, could be either normal or deviatoric strai...
Definition tensor.cpp:89
double double_contraction(const vec &)
Definition tensor.cpp:382
vec to_voigt(const mat &)
Definition tensor.cpp:341
double lode(vec)
Definition tensor.cpp:154
vec to_voigt(const mat &)
Definition tensor.cpp:413
double invariant2(const vec &)
compute the second invariant of the given 3D stress tensor, could be either normal or deviatoric stre...
Definition tensor.cpp:135
vec lode_der(vec)
Definition tensor.cpp:172
double lode(vec)
Definition tensor.cpp:163
double invariant3(const vec &)
compute the third invariant of the given 3D stress tensor, could be either normal or deviatoric stres...
Definition tensor.cpp:147
double invariant1(const vec &)
compute the first invariant of the given 3D stress tensor, could be either normal or deviatoric stres...
Definition tensor.cpp:124
double norm(const vec &)
Definition tensor.cpp:442
mat to_tensor(const vec &)
Definition tensor.cpp:388
double double_contraction(const vec &)
Definition tensor.cpp:454
Definition tensor.h:36
double trace3(const vec &)
Only accepts 3D tensor!
Definition tensor.cpp:196
mat diff_unit(const vec &)
Definition tensor.cpp:231
mat unit_deviatoric_tensor4v2()
Definition tensor.cpp:67
mat unit_deviatoric_tensor4()
Definition tensor.cpp:57
double mean3(const vec &)
Definition tensor.cpp:202
mat diff_triad(const vec3 &, const vec3 &, const vec3 &)
Definition tensor.cpp:236
vec dev(const vec &)
Definition tensor.cpp:204
mat unit_symmetric_tensor4()
Definition tensor.cpp:75
mat isotropic_stiffness(double, double)
Definition tensor.cpp:20
mat orthotropic_stiffness(const vec &, const vec &)
Definition tensor.cpp:34
double trace2(const vec &)
Only accepts 2D tensor!
Definition tensor.cpp:185
mat global_to_local(double, double, double)
Definition tensor.cpp:712
vec principal(const vec &)
Definition tensor.cpp:664
mat trans(double)
Definition tensor.cpp:650
double angle(const vec &)
Definition tensor.cpp:644
vec rotate(const vec &, double)
Definition tensor.cpp:676
vec principal(const vec &)
Definition tensor.cpp:698
mat trans(double)
Definition tensor.cpp:684
double angle(const vec &)
Definition tensor.cpp:682
vec rotate(const vec &, double)
Definition tensor.cpp:710
vec to_area_coordinate(const vec &, const mat &)
Definition tensor.cpp:629
Definition tensor.h:122
mat compute_jacobian_nominal_to_principal(const mat &)
Definition tensor.cpp:515
Mat< T > skew_symm(const Mat< T > &R)
Definition tensor.h:134
mat hill_projection(double, double, double, double, double, double)
Definition tensor.cpp:492
Col< T > to_pseudo(const Mat< T > &R)
Definition tensor.h:189
double atan2(const vec &)
Definition tensor.cpp:513
mat compute_jacobian_principal_to_nominal(const mat &)
Definition tensor.cpp:549
mat eigen_to_tensile_stress(const vec &, const mat &)
Definition tensor.cpp:600
void hoffman_projection(const vec &, mat &, mat &)
Generate two projection matrix based on the given yield stress according to the Hoffman yielding crit...
Definition tensor.cpp:466
mat eigen_to_tensor_base(const mat &)
Definition tensor.cpp:583
mat eigen_to_tensile_derivative(const vec &, const mat &)
Definition tensor.cpp:607
Quaternion< T > to_quaternion(const Mat< T > &R)
Definition tensor.h:152
Mat< T > rodrigues(const Mat< T > &R)
Definition tensor.h:150
void suanpan_assert(const std::function< void()> &F)
Definition suanPan.h:296