IFEM 90A354
Tensor.h
Go to the documentation of this file.
1// $Id$
2//==============================================================================
12//==============================================================================
13
14#ifndef _TENSOR_H
15#define _TENSOR_H
16
17#include <vector>
18#include <iostream>
19
20class Vec3;
21
22
27class Tensor
28{
29protected:
30 using t_ind = unsigned short int;
31
32 const t_ind n;
33 std::vector<Real> v;
34
37 virtual t_ind index(t_ind i, t_ind j) const { return i-1 + n*(j-1); }
38
39private:
41 void define3Dtransform(const Vec3& v1, const Vec3& v2, const Vec3& v3);
42
43public:
45 explicit Tensor(const t_ind nsd, bool identity = false);
47 explicit Tensor(const Vec3& vn, bool vnIsX = false);
49 Tensor(const Vec3& t1, const Vec3& t2,
50 bool t1isZ = false, bool t2isXZ = false);
52 Tensor(const Vec3& v1, const Vec3& v2, const Vec3& v3);
54 Tensor(Real a1, Real a2, Real a3);
56 Tensor(const Tensor& T, bool transpose = false);
58 Tensor(const std::vector<Real>& a, bool transpose = false);
59
61 virtual ~Tensor() {}
62
64 void zero() { std::fill(v.begin(),v.end(),Real(0)); }
65
67 void diag(Real value = Real(1));
69 void diag(const Vec3& diagonal);
70
72 operator const std::vector<Real>&() const { return v; }
74 operator std::vector<Real>&() { return v; }
75
77 const Real* ptr() const { return v.data(); }
78
80 const Real& operator()(t_ind i, t_ind j) const { return v[this->index(i,j)]; }
82 Real& operator()(t_ind i, t_ind j) { return v[this->index(i,j)]; }
84 Vec3 operator[](t_ind i) const;
85
87 Tensor& operator=(const Tensor& T);
89 Tensor& operator=(const std::vector<Real>& val);
91 Tensor& operator=(Real val);
92
94 Tensor& operator+=(const Tensor& T);
97
99 Tensor& operator-=(const Tensor& T);
101 Tensor& operator-=(Real val);
102
104 Tensor& operator*=(Real val);
106 Tensor& operator*=(const Tensor& B) { return this->postMult(B); }
108 Tensor& postMult(const Tensor& B);
110 Tensor& preMult(const Tensor& A);
112 Tensor& rotate(Real alpha, t_ind axis);
113
115 Tensor& outerProd(const Vec3& a, const Vec3& b);
116
118 Real innerProd(const Tensor& T) const;
119
121 t_ind dim() const { return n; }
122
124 size_t size() const { return v.size(); }
125
127 virtual bool symmetric() const { return false; }
128
130 bool equal(const Tensor& T, Real tol = Real(1.0e-6)) const;
131
133 bool isZero(Real tol = Real(1.0e-6)) const;
134
136 virtual Tensor& transpose();
138 virtual Tensor& symmetrize();
140 Tensor& shift(short int idx = 1);
141
143 virtual Real trace() const;
144
146 virtual Real det() const;
147
151 virtual Real inverse(Real tol = Real(0));
152
154 Vec3 rotVec() const;
155
157 virtual std::ostream& print(std::ostream& os, int prec = 0) const;
158
159 // Global operators
160
162 friend Vec3 operator*(const Tensor& T, const Vec3& v);
164 friend Vec3 operator*(const Vec3& v, const Tensor& T);
166 friend Tensor operator*(const Tensor& A, const Tensor& B);
168 friend Tensor operator*(Real a, const Tensor& T);
169
171 friend std::ostream& operator<<(std::ostream& os, const Tensor& T)
172 {
173 return T.print(os);
174 }
175};
176
177
182class SymmTensor : public Tensor
183{
191 bool redim(const t_ind nsd, bool with33 = false);
192
193protected:
198 t_ind index(t_ind i, t_ind j) const override
199 {
200 if (i == j)
201 return i-1; // diagonal term
202 else if (n == 2)
203 return v.size()-1; // off-diagonal term (2D)
204
205 if (i == j+1 || i+2 == j) std::swap(i,j);
206 return i+2; // upper triangular term (3D)
207 }
208
209public:
218 explicit SymmTensor(const t_ind nsd, bool with33 = false);
220 SymmTensor(const std::vector<Real>& vec);
222 SymmTensor(const SymmTensor& T) : Tensor(0) { this->copy(T); }
223
226
228 void copy(const SymmTensor& T);
229
231 bool symmetric() const override { return true; }
232
234 Tensor& transpose() override { return *this; }
236 Tensor& symmetrize() override { return *this; }
237
239 Real trace() const override;
240
242 Real det() const override;
243
247 Real inverse(Real tol = Real(0)) override;
248
250 SymmTensor& transform(const Tensor& T);
251
256
258 SymmTensor& outerProd(const Vec3& u);
259
261 Real L2norm(bool doSqrt = true) const;
263 Real vonMises(bool doSqrt = true) const;
265 bool principal(Vec3& p, bool sorted = true) const;
267 bool principal(Vec3& p, Tensor& pdir) const;
269 bool principal(Vec3& p, Vec3* pdir, int ndir = 0) const;
271 bool principal(Vec3& p, SymmTensor* M) const;
272
274 std::ostream& print(std::ostream& os, int prec = 0) const override;
275
276 // Global operators
277
279 friend SymmTensor operator+(const SymmTensor& T, Real a);
281 friend SymmTensor operator-(const SymmTensor& T, Real a);
282
284 friend SymmTensor operator*(Real a, const SymmTensor& T);
286 friend SymmTensor operator*(const SymmTensor& A, const SymmTensor& B);
287};
288
289
291inline SymmTensor operator+(const SymmTensor& A, const SymmTensor& B)
292{
293 SymmTensor C(A); C += B; return C;
294}
295
297inline SymmTensor operator-(const SymmTensor& A, const SymmTensor& B)
298{
299 SymmTensor C(A); C -= B; return C;
300}
301
303inline Real ddot(const SymmTensor& A, const SymmTensor& B)
304{
305 return A.innerProd(B);
306}
307
308
314{
315protected:
318
319public:
321 virtual ~LocalSystem() {}
322
324 virtual const Tensor& getTmat(const Vec3& X) const = 0;
325
326 static int patch;
327};
328
329#endif
static SystemMatrix * M
Pointer to coefficient matrix B.
Definition EigSolver.C:92
#define Real
The floating point type to use.
Definition ImmersedBoundaries.h:18
Real ddot(const SymmTensor &A, const SymmTensor &B)
Inner-product (:-operator) of two symmetric tensors.
Definition Tensor.h:303
SymmTensor operator+(const SymmTensor &A, const SymmTensor &B)
Adding two symmetric tensors.
Definition Tensor.h:291
SymmTensor operator-(const SymmTensor &A, const SymmTensor &B)
Subtracting two symmetric tensors.
Definition Tensor.h:297
Abstract interface to problem-specific local coordinate systems.
Definition Tensor.h:314
virtual ~LocalSystem()
Empty default destructor.
Definition Tensor.h:321
virtual const Tensor & getTmat(const Vec3 &X) const =0
Computes the global-to-local transformation at the point X.
LocalSystem()
Protected default constructor since this is an interface class.
Definition Tensor.h:317
static int patch
Counter used to establish multi-patch local systems.
Definition Tensor.h:326
Simple class for representing a symmetric second-order tensor.
Definition Tensor.h:183
Tensor & transpose() override
Transposes the symmetric tensor (i.e., does nothing).
Definition Tensor.h:234
std::ostream & print(std::ostream &os, int prec=0) const override
Prints out the lower triangle of the tensor to an output stream.
Definition Tensor.C:1423
Real inverse(Real tol=Real(0)) override
Inverts the symmetric tensor.
Definition Tensor.C:955
SymmTensor & operator=(Real val)
Assignment operator.
Definition Tensor.C:834
SymmTensor(const SymmTensor &T)
Copy constructor.
Definition Tensor.h:222
friend SymmTensor operator+(const SymmTensor &T, Real a)
Adding a scaled unit tensor to a symmetric tensor.
Definition Tensor.C:1451
Tensor & symmetrize() override
Makes the symmetric tensor symmetric (i.e., does nothing).
Definition Tensor.h:236
SymmTensor & rightCauchyGreen(const Tensor &F)
Constructs the right Cauchy-Green tensor from a deformation tensor.
Definition Tensor.C:998
bool symmetric() const override
Query whether this tensor is symmetric or not.
Definition Tensor.h:231
Real det() const override
Returns the determinant of the symmetric tensor.
Definition Tensor.C:935
Real L2norm(bool doSqrt=true) const
Returns the inner-product (L2-norm) of the symmetric tensor.
Definition Tensor.C:1092
SymmTensor & outerProd(const Vec3 &u)
Dyadic (outer) product between two identical vectors.
Definition Tensor.C:1062
Real vonMises(bool doSqrt=true) const
Returns the von Mises value of the symmetric tensor.
Definition Tensor.C:1111
SymmTensor & leftCauchyGreen(const Tensor &F)
Constructs the left Cauchy-Green tensor from a deformation tensor.
Definition Tensor.C:1030
void copy(const SymmTensor &T)
Copies a symmetric tensor, possibly with dimension change.
Definition Tensor.C:843
Real trace() const override
Returns the trace of the symmetric tensor.
Definition Tensor.C:920
friend SymmTensor operator-(const SymmTensor &T, Real a)
Subtracting a scaled unit tensor from a symmetric tensor.
Definition Tensor.C:1469
bool principal(Vec3 &p, bool sorted=true) const
Computes the principal values of the symmetric tensor.
Definition Tensor.C:1140
t_ind index(t_ind i, t_ind j) const override
Returns a 0-based array index for the given tensor indices.
Definition Tensor.h:198
friend SymmTensor operator*(Real a, const SymmTensor &T)
Multiplication between a scalar and a symmetric tensor.
Definition Tensor.C:1487
SymmTensor & transform(const Tensor &T)
Congruence transformation of a symmetric tensor.
Definition Tensor.C:855
bool redim(const t_ind nsd, bool with33=false)
Resets the number of spatial dimensions of the tensor.
Definition Tensor.C:808
Simple class for representing a non-symmetric second-order tensor.
Definition Tensor.h:28
Tensor & postMult(const Tensor &B)
Post-multiplication with another Tensor.
Definition Tensor.C:366
const Real & operator()(t_ind i, t_ind j) const
Index-1 based component reference.
Definition Tensor.h:80
size_t size() const
Returns the size of this tensor.
Definition Tensor.h:124
Tensor & operator*=(Real val)
Scaling operator.
Definition Tensor.C:357
void diag(Real value=Real(1))
Sets *this to a diagonal tensor with value on the diagonal.
Definition Tensor.C:209
Real & operator()(t_ind i, t_ind j)
Index-1 based component access.
Definition Tensor.h:82
virtual std::ostream & print(std::ostream &os, int prec=0) const
Prints out the tensor to an output stream.
Definition Tensor.C:690
Tensor & operator*=(const Tensor &B)
Post-multiplication with another Tensor.
Definition Tensor.h:106
Tensor & shift(short int idx=1)
Performs a cyclic permutation of the tensor columns.
Definition Tensor.C:572
Tensor & operator=(const Tensor &T)
Assignment operator.
Definition Tensor.C:246
Tensor & operator+=(const Tensor &T)
Incrementation operator.
Definition Tensor.C:303
const t_ind n
Number of spatial dimensions for the tensor.
Definition Tensor.h:32
virtual Real inverse(Real tol=Real(0))
Inverts the tensor.
Definition Tensor.C:650
Tensor & preMult(const Tensor &A)
Pre-multiplication with another Tensor.
Definition Tensor.C:399
Tensor & operator-=(const Tensor &T)
Decrementation operator.
Definition Tensor.C:330
unsigned short int t_ind
Tensor index type (for convenience)
Definition Tensor.h:30
const Real * ptr() const
Reference through a pointer.
Definition Tensor.h:77
friend std::ostream & operator<<(std::ostream &os, const Tensor &T)
Output stream operator.
Definition Tensor.h:171
std::vector< Real > v
The actual tensor component values.
Definition Tensor.h:33
Real innerProd(const Tensor &T) const
Returns the inner-product of *this and the given tensor.
Definition Tensor.C:485
void zero()
Sets *this to the 0-tensor.
Definition Tensor.h:64
virtual Tensor & symmetrize()
Makes the tensor symmetric.
Definition Tensor.C:551
bool equal(const Tensor &T, Real tol=Real(1.0e-6)) const
Query whether this tensor equals another within given tolerance.
Definition Tensor.C:507
bool isZero(Real tol=Real(1.0e-6)) const
Query whether this tensor is zero within given tolerance.
Definition Tensor.C:520
t_ind dim() const
Returns the dimension of this tensor.
Definition Tensor.h:121
Tensor & rotate(Real alpha, t_ind axis)
Rotates the tensor about given coordinate axis.
Definition Tensor.C:432
Vec3 operator[](t_ind i) const
Index-0 based column reference.
Definition Tensor.C:237
Vec3 rotVec() const
Returns the rotation angles corresponding to the tensor.
Definition Tensor.C:609
Tensor & outerProd(const Vec3 &a, const Vec3 &b)
Dyadic (outer) product between two vectors.
Definition Tensor.C:475
friend Vec3 operator*(const Tensor &T, const Vec3 &v)
Multiplication between a tensor and a point vector.
Definition Tensor.C:718
virtual Real det() const
Returns the determinant of the tensor.
Definition Tensor.C:635
virtual bool symmetric() const
Query whether this tensor is symmetric or not.
Definition Tensor.h:127
virtual Tensor & transpose()
Transposes the tensor.
Definition Tensor.C:530
virtual Real trace() const
Returns the trace of the tensor.
Definition Tensor.C:596
void define3Dtransform(const Vec3 &v1, const Vec3 &v2, const Vec3 &v3)
Creates a 3D transformation from three unit vectors.
Definition Tensor.C:225
virtual ~Tensor()
Empty destructor.
Definition Tensor.h:61
virtual t_ind index(t_ind i, t_ind j) const
Returns a 0-based array index for the given tensor indices.
Definition Tensor.h:37
Simple class for representing a point in 3D space.
Definition Vec3.h:27