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); }
146 inline std::array<double, 6> toStressArray() const { return {d11_, d22_, d33_, d12_, d13_, d23_}; }
147 inline std::array<double, 6> toStrainArray() const { return {d11_, d22_, d33_, 2.0*d12_, 2.0*d13_, 2.0*d23_}; }
148 private:
149 double d11_ = 0.0;
150 double d22_ = 0.0;
151 double d33_ = 0.0;
152 double d12_ = 0.0;
153 double d13_ = 0.0;
154 double d23_ = 0.0;
155};
156POPWARNING
157
166public:
167 friend class SymTensor;
169 BASE_EXPORT SymTensorInfo(): type_(Type::ThreeDCube) { }
175 BASE_EXPORT Axes3D getAxes() const;
177 BASE_EXPORT SymTensor resolve(const DVect3 &prin) const;
178private:
179 enum class Type { ThreeDCube, ThreeDJacobi, ZMax, ZMid, ZMin };
180 Type type_;
181 Axes3D axes_;
182};
183
184inline bool SymTensor::operator==(const SymTensor &s) const {
185 return (d11_ == s.d11_ && d22_ == s.d22_ &&
186 d33_ == s.d33_ && d12_ == s.d12_ && d13_ == s.d13_ &&
187 d23_ == s.d23_);
188}
189
190inline double SymTensor::operator[](unsigned int i) const {
191 assert(i<6);
192 switch (i) {
193 case 0: return d11_;
194 case 1: return d22_;
195 case 2: return d33_;
196 case 3: return d12_;
197 case 4: return d13_;
198 case 5: return d23_;
199 }
200 return d11_;
201}
202
203inline double &SymTensor::operator[](unsigned int i) {
204 assert(i<6);
205 switch (i) {
206 case 0: return d11_;
207 case 1: return d22_;
208 case 2: return d33_;
209 case 3: return d12_;
210 case 4: return d13_;
211 case 5: return d23_;
212 }
213 return d11_;
214}
215
216inline double SymTensor::getTotalMeasure() const {
217 double I1 = getTrace();
218 double J2 = getJ2();
219 return sqrt(I1*I1/3.0 + 2.0*J2);
220}
221
222inline DVect3 SymTensor::operator*(const DVect3 &normal) const {
223 return DVect3(normal.x()*s11() + normal.y()*s12() + normal.z()*s13(),
224 normal.x()*s21() + normal.y()*s22() + normal.z()*s23(),
225 normal.x()*s31() + normal.y()*s32() + normal.z()*s33());
226}
227
228inline DVect2 SymTensor::operator*(const DVect2 &normal) const {
229 return DVect2(normal.x()*s11() + normal.y()*s12(),
230 normal.x()*s21() + normal.y()*s22());
231}
232
233inline SymTensor SymTensor::operator*(const double &mul) const {
234 SymTensor ret;
235 ret.d11_ = d11_ * mul;
236 ret.d22_ = d22_ * mul;
237 ret.d33_ = d33_ * mul;
238 ret.d12_ = d12_ * mul;
239 ret.d13_ = d13_ * mul;
240 ret.d23_ = d23_ * mul;
241 return ret;
242}
243
244inline const SymTensor &SymTensor::operator*=(const double &mul) {
245 d11_ *= mul;
246 d22_ *= mul;
247 d33_ *= mul;
248 d12_ *= mul;
249 d13_ *= mul;
250 d23_ *= mul;
251 return *this;
252}
253
254inline SymTensor SymTensor::operator/(const double &mul) const {
255 SymTensor ret;
256 ret.d11_ = d11_ / mul;
257 ret.d22_ = d22_ / mul;
258 ret.d33_ = d33_ / mul;
259 ret.d12_ = d12_ / mul;
260 ret.d13_ = d13_ / mul;
261 ret.d23_ = d23_ / mul;
262 return ret;
263}
264
265inline const SymTensor &SymTensor::operator/=(const double &mul) {
266 d11_ /= mul;
267 d22_ /= mul;
268 d33_ /= mul;
269 d12_ /= mul;
270 d13_ /= mul;
271 d23_ /= mul;
272 return *this;
273}
274
275inline SymTensor SymTensor::operator+(const SymTensor &s) const {
276 SymTensor ret;
277 ret.d11_ = d11_ + s.d11_;
278 ret.d22_ = d22_ + s.d22_;
279 ret.d33_ = d33_ + s.d33_;
280 ret.d12_ = d12_ + s.d12_;
281 ret.d13_ = d13_ + s.d13_;
282 ret.d23_ = d23_ + s.d23_;
283 return ret;
284}
285
286inline SymTensor SymTensor::operator-(const SymTensor &s) const {
287 SymTensor ret;
288 ret.d11_ = d11_ - s.d11_;
289 ret.d22_ = d22_ - s.d22_;
290 ret.d33_ = d33_ - s.d33_;
291 ret.d12_ = d12_ - s.d12_;
292 ret.d13_ = d13_ - s.d13_;
293 ret.d23_ = d23_ - s.d23_;
294 return ret;
295}
296
297inline bool SymTensor::isIsotropic(double tol) const {
298 double dtol = std::max(std::max(std::abs(s11()),std::abs(s22())),std::abs(s33())) * tol;
299 if (std::abs(s11()-s22()) > dtol) return false;
300 if (std::abs(s11()-s33()) > dtol) return false;
301 if (std::abs(s12()) > dtol) return false;
302 if (std::abs(s13()) > dtol) return false;
303 if (std::abs(s23()) > dtol) return false;
304 return true;
305}
306
307inline void SymTensor::adjustTrace(const double newTrace) {
308 static constexpr double d1d3 = 1.0 / 3.0;
309 /* --- strain differences --- */
310 double dx = s11() - s22();
311 double dy = s22() - s33();
312 double dz = s33() - s11();
313 rs11() = (newTrace + dx - dz) * d1d3;
314 rs22() = (newTrace + dy - dx) * d1d3;
315 rs33() = (newTrace + dz - dy) * d1d3;
316}
317
318inline void SymTensor::rotate(const DVect3 &rot) {
319 SymTensor copy(*this);
320 d11_ += 2.0*( copy.s12()*rot.x() + copy.s13()*rot.y());
321 d22_ += 2.0*(-copy.s12()*rot.x() + copy.s23()*rot.z());
322 d33_ += -2.0*( copy.s13()*rot.y() + copy.s23()*rot.z());
323 d12_ += ((copy.s22() - copy.s11())*rot.x() + copy.s23()*rot.x() + copy.s13()*rot.z());
324 d13_ += ( copy.s23()*rot.x() + (copy.s33() - copy.s11())*rot.x() - copy.s12()*rot.z());
325 d23_ += ( -copy.s13()*rot.x() - copy.s12()*rot.x() + (copy.s33() - copy.s22())*rot.z());
326}
327
328// 11 0
329// 22 1
330// 33 2
331// 12 3
332// 13 4
333// 23 5
334uint32 SymTensor::doubleToSingleComponent(uint32 dof1,uint32 dof2) {
335 dof1 = std::clamp<uint32>(dof1,0,2);
336 dof2 = std::clamp<uint32>(dof2,0,2);
337 switch (dof1) {
338 case 0:
339 switch (dof2) {
340 case 0: return 0;
341 case 1: return 3;
342 case 2: return 4;
343 }
344 break;
345 case 1:
346 switch (dof2) {
347 case 0: return 3;
348 case 1: return 1;
349 case 2: return 5;
350 }
351 break;
352 case 2:
353 switch (dof2) {
354 case 0: return 4;
355 case 1: return 5;
356 case 2: return 3;
357 }
358 break;
359 }
360 return 0;
361}
362
363namespace base {
364 BASE_EXPORT string ts(const SymTensor &s, int width=0, char notation = '\0', int precision = -1, char fill = ' ');
365}
366
368// 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:165
BASE_EXPORT SymTensorInfo()
Default constructor.
Definition symtensor.h:169
BASE_EXPORT Axes3D getAxes() const
Returns eigen directions (minimum, intermediate, maximum).
Definition symtensor.cpp:569
BASE_EXPORT SymTensor resolve(const DVect3 &prin) const
Regenerates full tensor from info + principal directions.
Definition symtensor.cpp:576
BASE_EXPORT const SymTensorInfo & operator=(const SymTensorInfo &si)
Equality operator.
Definition symtensor.cpp:563
double getTotalMeasure() const
Definition symtensor.h:216
bool operator==(const SymTensor &s) const
Equality operator.
Definition symtensor.h:184
#define BASE_EXPORT
Definition basedef.h:25
double operator[](unsigned int i) const
Allows Index access to tensor components.
Definition symtensor.h:190
DVect3 operator*(const DVect3 &input) const
Performs the linear mapping represented by the tensor on the vector input.
Definition symtensor.h:222
constexpr Vector2< T > max(const Vector2< T > &v1, const Vector2< T > &v2)
Template specialization for max, min.
Definition vect.h:404