Itasca C++ Interface
Loading...
Searching...
No Matches
symtensor.h
Go to the documentation of this file.
1#pragma once
8#include "axes.h"
9#include <cassert>
10#include <limits>
11#include <algorithm>
12
13class SymTensorInfo;
14
19PUSHWARNING
20VSWARNING(26495) // member init (not initialized on purpose)
21VSWARNING(4702) // Weird unreachable code warnings in release
23public:
25 SymTensor() { }
27 SymTensor(const SymTensor &s)=default;
30 explicit SymTensor(double i11,double i22=0.0,double i33=0.0,double i12=0.0,double i13=0.0,double i23=0.0)
31 : d11_(i11), d22_(i22), d33_(i33), d12_(i12), d13_(i13), d23_(i23) { }
33 SymTensor &operator=(const SymTensor &s)=default;
34
36 bool operator==(const SymTensor &s) const;
37 bool operator<(const SymTensor &s) const;
38
40 double s11() const { return d11_; }
41 double s22() const { return d22_; }
42 double s33() const { return d33_; }
43 double s12() const { return d12_; }
44 double s13() const { return d13_; }
45 double s23() const { return d23_; }
46 double s21() const { return d12_; }
47 double s31() const { return d13_; }
48 double s32() const { return d23_; }
50 double &rs11() { return d11_; }
51 double &rs22() { return d22_; }
52 double &rs33() { return d33_; }
53 double &rs12() { return d12_; }
54 double &rs13() { return d13_; }
55 double &rs23() { return d23_; }
56 double &rs21() { return d12_; }
57 double &rs31() { return d13_; }
58 double &rs32() { return d23_; }
63 inline double operator[](unsigned int i) const;
64 inline double &operator[](unsigned int i);
65
67 double operator()(unsigned int i,unsigned int j) const;
69 double &operator()(unsigned int i,unsigned int j);
70
76 DVect3 getEigenInfo(SymTensorInfo *si=0) const;
78 double getTrace() const { return d11_+d22_+d33_; }
80 SymTensor getDeviatoric() const { double p=getTrace()/3.0; return SymTensor(d11_-p, d22_-p, d33_-p, d12_, d13_, d23_); }
82 double getI1() const { return getTrace(); }
84 double getI2() const { return d11_*d22_ + d22_*d33_ + d11_*d33_ - d12_*d12_ - d23_*d23_ - d13_*d13_; }
86 double getI3() const { return getDeterminate(); }
88 double getJ2() const;
90 double getJ2(SymTensor *dev, double *I1=nullptr);
91 //Keep only getJ2(), Von Mises and Octahedral stress/strain are calculated from J2.
93 //double getVonMises() const { return sqrt(3.0*getJ2()); }
95 //double getVonMisesStrain() const { return sqrt(4.0*getJ2()/9.0); }
97 //double getOctahedralShear() const { return sqrt(2.0*getJ2()/3.0); }
99 //double getOctahedralShearStrain() const { return sqrt(8.0*getJ2()/3.0); }
101 double getJ3() const;
103 double getLode(double *I1 = nullptr, double *J2 = nullptr, double *J3 = nullptr);
105 double getDeterminate() const;
107 double getNorm2() const { return d11_*d11_ + d22_*d22_ + d33_*d33_ + 2.0*(d12_*d12_ + d13_*d13_ + d23_*d23_); }
111 double getTotalMeasure() const;
113 SymTensor toGlobal(const Axes3D &a) const;
118 inline DVect3 operator*(const DVect3 &input) const;
119 inline DVect2 operator*(const DVect2 &input) const;
120 inline SymTensor operator*(const double &mul) const;
121 inline const SymTensor &operator*=(const double &mul);
122 inline SymTensor operator/(const double &mul) const;
123 inline const SymTensor &operator/=(const double &mul);
125 SymTensor mul(const double &d) const { SymTensor ret(d11_*d,d22_*d,d33_*d,d12_*d,d13_*d,d23_*d); return ret; }
127 const SymTensor &operator+=(const SymTensor &s) { d11_+=s.d11_; d22_+=s.d22_; d33_+=s.d33_; d12_+=s.d12_; d13_+=s.d13_; d23_+=s.d23_; return *this; }
128 const SymTensor &operator-=(const SymTensor &s) { d11_-=s.d11_; d22_-=s.d22_; d33_-=s.d33_; d12_-=s.d12_; d13_-=s.d13_; d23_-=s.d23_; return *this; }
129 inline SymTensor operator+(const SymTensor &s) const;
130 inline SymTensor operator-(const SymTensor &s) const;
132 static SymTensor fromPrincipal(const DVect3 &prin,const Axes3D &axes);
136 static SymTensor fromForceNormal(const DVect3 &normal, const DVect3 &force);
137 static inline uint32 doubleToSingleComponent(uint32 dof1,uint32 dof2);
139 bool isDiagonal(double tol = std::numeric_limits<double>::epsilon()*1000.0) const { return (abs(d12_) > tol || abs(d13_) > tol || abs(d23_) > tol) ? false : true; }
140 inline bool isIsotropic(double tol = std::numeric_limits<double>::epsilon()*1000.0) const;
141 inline void adjustTrace(const double newTrace);
142 inline void incrementDiagonal(const double increment) { d11_ += increment; d22_ += increment; d33_ += increment; }
143 inline void rotate(const DVect3 &rot);
144 inline double maxAbs() const { return std::max(std::abs(d11_),std::max(std::abs(d22_),std::max(std::abs(d33_),std::max(std::abs(d12_),std::max(std::abs(d13_),std::abs(d23_)))))); }
145 inline bool zero() const { return (d11_==0.0) and (d22_==0.0) and (d33_==0.0) and (d12_==0.0) and (d13_==0.0) and (d23_==0.0); }
146private:
147 double d11_ = 0.0;
148 double d22_ = 0.0;
149 double d33_ = 0.0;
150 double d12_ = 0.0;
151 double d13_ = 0.0;
152 double d23_ = 0.0;
153};
154POPWARNING
155
164public:
165 friend class SymTensor;
167 BASE_EXPORT SymTensorInfo(): type_(Type::ThreeDCube) { }
173 BASE_EXPORT Axes3D getAxes() const;
175 BASE_EXPORT SymTensor resolve(const DVect3 &prin) const;
176private:
177 enum class Type { ThreeDCube, ThreeDJacobi, ZMax, ZMid, ZMin };
178 Type type_;
179 Axes3D axes_;
180};
181
182inline bool SymTensor::operator==(const SymTensor &s) const {
183 return (d11_ == s.d11_ && d22_ == s.d22_ &&
184 d33_ == s.d33_ && d12_ == s.d12_ && d13_ == s.d13_ &&
185 d23_ == s.d23_);
186}
187
188inline double SymTensor::operator[](unsigned int i) const {
189 assert(i<6);
190 switch (i) {
191 case 0: return d11_;
192 case 1: return d22_;
193 case 2: return d33_;
194 case 3: return d12_;
195 case 4: return d13_;
196 case 5: return d23_;
197 }
198 return d11_;
199}
200
201inline double &SymTensor::operator[](unsigned int i) {
202 assert(i<6);
203 switch (i) {
204 case 0: return d11_;
205 case 1: return d22_;
206 case 2: return d33_;
207 case 3: return d12_;
208 case 4: return d13_;
209 case 5: return d23_;
210 }
211 return d11_;
212}
213
214inline double SymTensor::getTotalMeasure() const {
215 double I1 = getTrace();
216 double J2 = getJ2();
217 return sqrt(I1*I1/3.0 + 2.0*J2);
218}
219
220inline DVect3 SymTensor::operator*(const DVect3 &normal) const {
221 return DVect3(normal.x()*s11() + normal.y()*s12() + normal.z()*s13(),
222 normal.x()*s21() + normal.y()*s22() + normal.z()*s23(),
223 normal.x()*s31() + normal.y()*s32() + normal.z()*s33());
224}
225
226inline DVect2 SymTensor::operator*(const DVect2 &normal) const {
227 return DVect2(normal.x()*s11() + normal.y()*s12(),
228 normal.x()*s21() + normal.y()*s22());
229}
230
231inline SymTensor SymTensor::operator*(const double &mul) const {
232 SymTensor ret;
233 ret.d11_ = d11_ * mul;
234 ret.d22_ = d22_ * mul;
235 ret.d33_ = d33_ * mul;
236 ret.d12_ = d12_ * mul;
237 ret.d13_ = d13_ * mul;
238 ret.d23_ = d23_ * mul;
239 return ret;
240}
241
242inline const SymTensor &SymTensor::operator*=(const double &mul) {
243 d11_ *= mul;
244 d22_ *= mul;
245 d33_ *= mul;
246 d12_ *= mul;
247 d13_ *= mul;
248 d23_ *= mul;
249 return *this;
250}
251
252inline SymTensor SymTensor::operator/(const double &mul) const {
253 SymTensor ret;
254 ret.d11_ = d11_ / mul;
255 ret.d22_ = d22_ / mul;
256 ret.d33_ = d33_ / mul;
257 ret.d12_ = d12_ / mul;
258 ret.d13_ = d13_ / mul;
259 ret.d23_ = d23_ / mul;
260 return ret;
261}
262
263inline const SymTensor &SymTensor::operator/=(const double &mul) {
264 d11_ /= mul;
265 d22_ /= mul;
266 d33_ /= mul;
267 d12_ /= mul;
268 d13_ /= mul;
269 d23_ /= mul;
270 return *this;
271}
272
273inline SymTensor SymTensor::operator+(const SymTensor &s) const {
274 SymTensor ret;
275 ret.d11_ = d11_ + s.d11_;
276 ret.d22_ = d22_ + s.d22_;
277 ret.d33_ = d33_ + s.d33_;
278 ret.d12_ = d12_ + s.d12_;
279 ret.d13_ = d13_ + s.d13_;
280 ret.d23_ = d23_ + s.d23_;
281 return ret;
282}
283
284inline SymTensor SymTensor::operator-(const SymTensor &s) const {
285 SymTensor ret;
286 ret.d11_ = d11_ - s.d11_;
287 ret.d22_ = d22_ - s.d22_;
288 ret.d33_ = d33_ - s.d33_;
289 ret.d12_ = d12_ - s.d12_;
290 ret.d13_ = d13_ - s.d13_;
291 ret.d23_ = d23_ - s.d23_;
292 return ret;
293}
294
295inline bool SymTensor::isIsotropic(double tol) const {
296 double dtol = std::max(std::max(std::abs(s11()),std::abs(s22())),std::abs(s33())) * tol;
297 if (std::abs(s11()-s22()) > dtol) return false;
298 if (std::abs(s11()-s33()) > dtol) return false;
299 if (std::abs(s12()) > dtol) return false;
300 if (std::abs(s13()) > dtol) return false;
301 if (std::abs(s23()) > dtol) return false;
302 return true;
303}
304
305inline void SymTensor::adjustTrace(const double newTrace) {
306 static constexpr double d1d3 = 1.0 / 3.0;
307 /* --- strain differences --- */
308 double dx = s11() - s22();
309 double dy = s22() - s33();
310 double dz = s33() - s11();
311 rs11() = (newTrace + dx - dz) * d1d3;
312 rs22() = (newTrace + dy - dx) * d1d3;
313 rs33() = (newTrace + dz - dy) * d1d3;
314}
315
316inline void SymTensor::rotate(const DVect3 &rot) {
317 SymTensor copy(*this);
318 d11_ += 2.0*( copy.s12()*rot.x() + copy.s13()*rot.y());
319 d22_ += 2.0*(-copy.s12()*rot.x() + copy.s23()*rot.z());
320 d33_ += -2.0*( copy.s13()*rot.y() + copy.s23()*rot.z());
321 d12_ += ((copy.s22() - copy.s11())*rot.x() + copy.s23()*rot.x() + copy.s13()*rot.z());
322 d13_ += ( copy.s23()*rot.x() + (copy.s33() - copy.s11())*rot.x() - copy.s12()*rot.z());
323 d23_ += ( -copy.s13()*rot.x() - copy.s12()*rot.x() + (copy.s33() - copy.s22())*rot.z());
324}
325
326// 11 0
327// 22 1
328// 33 2
329// 12 3
330// 13 4
331// 23 5
332uint32 SymTensor::doubleToSingleComponent(uint32 dof1,uint32 dof2) {
333 dof1 = std::clamp<uint32>(dof1,0,2);
334 dof2 = std::clamp<uint32>(dof2,0,2);
335 switch (dof1) {
336 case 0:
337 switch (dof2) {
338 case 0: return 0;
339 case 1: return 3;
340 case 2: return 4;
341 }
342 break;
343 case 1:
344 switch (dof2) {
345 case 0: return 3;
346 case 1: return 1;
347 case 2: return 5;
348 }
349 break;
350 case 2:
351 switch (dof2) {
352 case 0: return 4;
353 case 1: return 5;
354 case 2: return 3;
355 }
356 break;
357 }
358 return 0;
359}
360
361namespace base {
362 BASE_EXPORT string ts(const SymTensor &s, int width=0, char notation = '\0', int precision = -1, char fill = ' ');
363}
364
366// EoF
2D and 3D cartesian Axes systems.
Class for specifying a particular 3D cartesian axes system, and converting to and from it.
Definition axes.h:121
A symmetric 2nd order tensor.
Definition symtensor.h:22
double & rs31()
Reference access to components, note that s12 is equivalent to s21 (for instance).
Definition symtensor.h:57
double & rs22()
Reference access to components, note that s12 is equivalent to s21 (for instance).
Definition symtensor.h:51
double & rs13()
Reference access to components, note that s12 is equivalent to s21 (for instance).
Definition symtensor.h:54
double & rs11()
Reference access to components, note that s12 is equivalent to s21 (for instance).
Definition symtensor.h:50
double getI3() const
Returns the third invariant, or I3.
Definition symtensor.h:86
const SymTensor & operator+=(const SymTensor &s)
+= operator for a SymTensor
Definition symtensor.h:127
double & rs12()
Reference access to components, note that s12 is equivalent to s21 (for instance).
Definition symtensor.h:53
double s21() const
Component access, note that s12 is equivalent to s21 (for instance).
Definition symtensor.h:46
SymTensor & operator=(const SymTensor &s)=default
Assignment operator.
SymTensor(double i11, double i22=0.0, double i33=0.0, double i12=0.0, double i13=0.0, double i23=0.0)
Definition symtensor.h:30
double getI1() const
Same as getTrace() - returns the first invariant.
Definition symtensor.h:82
double s33() const
Component access, note that s12 is equivalent to s21 (for instance).
Definition symtensor.h:42
double s12() const
Component access, note that s12 is equivalent to s21 (for instance).
Definition symtensor.h:43
double s11() const
Component access, note that s12 is equivalent to s21 (for instance).
Definition symtensor.h:40
SymTensor mul(const double &d) const
Returns a SymTensor with every component multiplied by a scalar value.
Definition symtensor.h:125
double & rs33()
Reference access to components, note that s12 is equivalent to s21 (for instance).
Definition symtensor.h:52
bool isDiagonal(double tol=std::numeric_limits< double >::epsilon() *1000.0) const
Determines whether or not the SymTensor is diagonal.
Definition symtensor.h:139
double s32() const
Component access, note that s12 is equivalent to s21 (for instance).
Definition symtensor.h:48
double s22() const
Component access, note that s12 is equivalent to s21 (for instance).
Definition symtensor.h:41
double getI2() const
Returns the second invariant.
Definition symtensor.h:84
double & rs21()
Reference access to components, note that s12 is equivalent to s21 (for instance).
Definition symtensor.h:56
double getTrace() const
Returns the trace of the tensor (11+22+33). I1.
Definition symtensor.h:78
double & rs23()
Reference access to components, note that s12 is equivalent to s21 (for instance).
Definition symtensor.h:55
double getJ2() const
Returns the second invariant of the deviatoric – J2.
Definition symtensor.cpp:160
SymTensor()
Default constructor, no data initialization.
Definition symtensor.h:25
double & rs32()
Reference access to components, note that s12 is equivalent to s21 (for instance).
Definition symtensor.h:58
double s31() const
Component access, note that s12 is equivalent to s21 (for instance).
Definition symtensor.h:47
double s23() const
Component access, note that s12 is equivalent to s21 (for instance).
Definition symtensor.h:45
double getNorm2() const
Returns a scalar norm (magnitude) value for the tensor, can be used for tolerance checking,...
Definition symtensor.h:107
SymTensor getDeviatoric() const
Returns the deviatoric tensor.
Definition symtensor.h:80
double s13() const
Component access, note that s12 is equivalent to s21 (for instance).
Definition symtensor.h:44
SymTensor(const SymTensor &s)=default
Copy constructor.
SymTensor eigenvalue and direction helper class.
Definition symtensor.h:163
BASE_EXPORT SymTensorInfo()
Default constructor.
Definition symtensor.h:167
double getTotalMeasure() const
Definition symtensor.h:214
bool operator==(const SymTensor &s) const
Equality operator.
Definition symtensor.h:182
#define BASE_EXPORT
Definition basedef.h:25
double operator[](unsigned int i) const
Allows Index access to tensor components.
Definition symtensor.h:188
DVect3 operator*(const DVect3 &input) const
Performs the linear mapping represented by the tensor on the vector input.
Definition symtensor.h:220
constexpr Vector2< T > max(const Vector2< T > &v1, const Vector2< T > &v2)
Template specialization for max, min.
Definition vect.h:404