suanPan
tensor.h
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright (C) 2017-2023 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 
34 template<typename T> class Quaternion;
35 
36 namespace 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&, const vec&);
91  double double_contraction(vec&&, vec&&);
92  } // namespace strain
93  namespace stress {
94  mat to_tensor(const vec&);
95  vec to_voigt(const mat&);
96  double norm(const vec&);
97  double norm(vec&&);
98  double double_contraction(const vec&, const vec&);
99  double double_contraction(vec&&, vec&&);
100  } // namespace stress
101 } // namespace tensor
102 
103 namespace transform {
104  double atan2(const vec&);
107 
108  template<typename T> Mat<T> skew_symm(const Mat<T>& R) {
109  suanpan_assert([&] { if(R.n_elem != 3) throw invalid_argument("need 3 element vector"); });
110 
111  Mat<T> S(3, 3, fill::zeros);
112 
113  S(0, 1) = -(S(1, 0) = R(2));
114  S(2, 0) = -(S(0, 2) = R(1));
115  S(1, 2) = -(S(2, 1) = R(0));
116 
117  return S;
118  }
119 
120  template<typename T> Mat<T> skew_symm(const subview_col<T>& R) { return skew_symm(R.eval()); }
121 
122  template<typename T> Mat<T> rodrigues(const Mat<T>& R) { return arma::expmat(transform::skew_symm(R)); }
123 
124  template<typename T> Mat<T> rodrigues(const subview_col<T>& R) { return arma::expmat(transform::skew_symm(R)); }
125 
126  template<typename T> Quaternion<T> to_quaternion(const Mat<T>& R) {
127  if(3 == R.n_elem) {
128  const auto angle = arma::norm(R);
129 
130  if(suanpan::approx_equal(angle, 0.)) return {0., 0., 0., 0.};
131 
132  return {std::cos(.5 * angle), std::sin(.5 * angle) / angle * R};
133  }
134 
135  if(9 == R.n_elem) {
136  const auto tr_r = arma::trace(R);
137  const auto max_r = arma::max(R.diag());
138  if(tr_r >= max_r) {
139  const auto q0 = .5 * std::sqrt(tr_r + 1.);
140  const auto q1 = .25 / q0 * (R(2, 1) - R(1, 2));
141  const auto q2 = .25 / q0 * (R(0, 2) - R(2, 0));
142  const auto q3 = .25 / q0 * (R(1, 0) - R(0, 1));
143  return {q0, q1, q2, q3};
144  }
145 
146  for(auto I = 0; I < 3; ++I)
147  if(suanpan::approx_equal(R(I, I), max_r)) {
148  const auto J = (I + 1) % 3;
149  const auto K = (J + 1) % 3;
150  vec q(3, fill::none);
151  q(I) = std::sqrt(.5 * max_r + .25 * (1. - tr_r));
152  const double q0 = .25 / q(I) * (R(K, J) - R(J, K));
153  q(J) = .25 / q(I) * (R(J, I) + R(I, J));
154  q(K) = .25 / q(I) * (R(K, I) + R(I, K));
155 
156  return {q0, std::move(q)};
157  }
158  }
159 
160  throw invalid_argument("need either rotation vector or matrix");
161  }
162 
163  template<typename T> Col<T> to_pseudo(const Mat<T>& R) {
164  const Mat<T> S = arma::real(arma::logmat(R));
165 
166  return {S(2, 1), S(0, 2), S(1, 0)};
167  }
168 
169  namespace strain {
170  double angle(const vec&);
171  mat trans(double);
172  vec principal(const vec&);
173  vec rotate(const vec&, double);
174  } // namespace strain
175  namespace stress {
176  double angle(const vec&);
177  mat trans(double);
178  vec principal(const vec&);
179  vec rotate(const vec&, double);
180  } // namespace stress
181  namespace beam {
182  mat global_to_local(double, double, double);
183  mat global_to_local(const vec&, double);
184  } // namespace beam
185  namespace triangle {
186  vec to_area_coordinate(const vec&, const mat&);
187  }
188 } // namespace transform
189 
190 namespace suanpan {
191  template<typename T> T ramp(const T in) { return in > T(0) ? in : T(0); }
192 } // namespace suanpan
193 
194 #endif
195 
An Quaternion class.
Definition: Quaternion.hpp:34
Mat< T > stress(T X, T Y, unsigned S)
Definition: shape.h:475
Mat< T > strain(T X, T Y, T V, unsigned S)
Definition: shape.h:523
Col< T > beam(T int_pts, unsigned order, double length)
Definition: shape.h:151
T triangle(const Mat< T > &EC)
Definition: shape.h:107
Definition: MatrixModifier.hpp:36
T ramp(const T in)
Definition: tensor.h:191
std::enable_if_t<!std::numeric_limits< T >::is_integer, bool > approx_equal(T x, T y, int ulp=2)
Definition: utility.h:58
mat to_green(mat &&)
Definition: tensor.cpp:224
mat to_tensor(const vec &)
Definition: tensor.cpp:248
double invariant3(const vec &)
compute the third invariant of the given 3D strain tensor, could be either normal or deviatoric strai...
Definition: tensor.cpp:105
double norm(const vec &)
Definition: tensor.cpp:302
double invariant2(const vec &)
compute the second invariant of the given 3D strain tensor, could be either normal or deviatoric stra...
Definition: tensor.cpp:93
double invariant1(const vec &)
compute the first invariant of the given 3D strain tensor, could be either normal or deviatoric strai...
Definition: tensor.cpp:82
double double_contraction(const vec &, const vec &)
Definition: tensor.cpp:314
vec to_voigt(const mat &)
Definition: tensor.cpp:273
double lode(vec)
Definition: tensor.cpp:147
vec to_voigt(const mat &)
Definition: tensor.cpp:343
double invariant2(const vec &)
compute the second invariant of the given 3D stress tensor, could be either normal or deviatoric stre...
Definition: tensor.cpp:128
double double_contraction(const vec &, const vec &)
Definition: tensor.cpp:384
vec lode_der(vec)
Definition: tensor.cpp:165
double lode(vec)
Definition: tensor.cpp:156
double invariant3(const vec &)
compute the third invariant of the given 3D stress tensor, could be either normal or deviatoric stres...
Definition: tensor.cpp:140
double invariant1(const vec &)
compute the first invariant of the given 3D stress tensor, could be either normal or deviatoric stres...
Definition: tensor.cpp:117
double norm(const vec &)
Definition: tensor.cpp:372
mat to_tensor(const vec &)
Definition: tensor.cpp:318
double norm(vec &&)
Definition: tensor.cpp:378
Definition: tensor.h:36
double trace3(const vec &)
Only accepts 3D tensor!
Definition: tensor.cpp:189
mat unit_deviatoric_tensor4v2()
Definition: tensor.cpp:60
mat unit_deviatoric_tensor4()
Definition: tensor.cpp:50
double mean3(const vec &)
Definition: tensor.cpp:195
vec dev(const vec &)
Definition: tensor.cpp:197
mat unit_symmetric_tensor4()
Definition: tensor.cpp:68
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:178
mat global_to_local(double, double, double)
Definition: tensor.cpp:541
vec principal(const vec &)
Definition: tensor.cpp:493
mat trans(double)
Definition: tensor.cpp:479
double angle(const vec &)
Definition: tensor.cpp:473
vec rotate(const vec &, double)
Definition: tensor.cpp:505
vec principal(const vec &)
Definition: tensor.cpp:527
mat trans(double)
Definition: tensor.cpp:513
double angle(const vec &)
Definition: tensor.cpp:511
vec rotate(const vec &, double)
Definition: tensor.cpp:539
vec to_area_coordinate(const vec &, const mat &)
Definition: tensor.cpp:458
Definition: tensor.h:103
Mat< T > rodrigues(const Mat< T > &R)
Definition: tensor.h:122
mat compute_jacobian_nominal_to_principal(const mat &)
Definition: tensor.cpp:390
Quaternion< T > to_quaternion(const Mat< T > &R)
Definition: tensor.h:126
Col< T > to_pseudo(const Mat< T > &R)
Definition: tensor.h:163
Mat< T > skew_symm(const Mat< T > &R)
Definition: tensor.h:108
double atan2(const vec &)
Definition: tensor.cpp:388
mat compute_jacobian_principal_to_nominal(const mat &)
Definition: tensor.cpp:424
void suanpan_assert(const std::function< void()> &F)
Definition: suanPan.h:284