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 
20 class Vec3;
21 
22 
27 class Tensor
28 {
29 protected:
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 
39 private:
41  void define3Dtransform(const Vec3& v1, const Vec3& v2, const Vec3& v3);
42 
43 public:
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.front(); }
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);
96  Tensor& operator+=(Real val);
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 
182 class SymmTensor : public Tensor
183 {
191  bool redim(const t_ind nsd, bool with33 = false);
192 
193 protected:
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 
209 public:
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 
225  SymmTensor& operator=(Real val);
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 
254 
256  SymmTensor& outerProd(const Vec3& u);
257 
259  Real L2norm(bool doSqrt = true) const;
261  Real vonMises(bool doSqrt = true) const;
263  bool principal(Vec3& p, bool sorted = true) const;
265  bool principal(Vec3& p, Tensor& pdir) const;
267  bool principal(Vec3& p, Vec3* pdir, int ndir = 0) const;
269  bool principal(Vec3& p, SymmTensor* M) const;
270 
272  std::ostream& print(std::ostream& os, int prec = 0) const override;
273 
274  // Global operators
275 
277  friend SymmTensor operator+(const SymmTensor& T, Real a);
279  friend SymmTensor operator-(const SymmTensor& T, Real a);
280 
282  friend SymmTensor operator*(Real a, const SymmTensor& T);
284  friend SymmTensor operator*(const SymmTensor& A, const SymmTensor& B);
285 };
286 
287 
289 inline SymmTensor operator+(const SymmTensor& A, const SymmTensor& B)
290 {
291  SymmTensor C(A); C += B; return C;
292 }
293 
295 inline SymmTensor operator-(const SymmTensor& A, const SymmTensor& B)
296 {
297  SymmTensor C(A); C -= B; return C;
298 }
299 
301 inline Real ddot(const SymmTensor& A, const SymmTensor& B)
302 {
303  return A.innerProd(B);
304 }
305 
306 
312 {
313 protected:
316 
317 public:
319  virtual ~LocalSystem() {}
320 
322  virtual const Tensor& getTmat(const Vec3& X) const = 0;
323 
324  static int patch;
325 };
326 
327 #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:301
SymmTensor operator+(const SymmTensor &A, const SymmTensor &B)
Adding two symmetric tensors.
Definition: Tensor.h:289
SymmTensor operator-(const SymmTensor &A, const SymmTensor &B)
Subtracting two symmetric tensors.
Definition: Tensor.h:295
Abstract interface to problem-specific local coordinate systems.
Definition: Tensor.h:312
virtual ~LocalSystem()
Empty default destructor.
Definition: Tensor.h:319
LocalSystem()
Protected default constructor since this is an interface class.
Definition: Tensor.h:315
static int patch
Counter used to establish multi-patch local systems.
Definition: Tensor.h:324
virtual const Tensor & getTmat(const Vec3 &X) const =0
Computes the global-to-local transformation at the point X.
Simple class for representing a symmetric second-order tensor.
Definition: Tensor.h:183
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:1391
Tensor & symmetrize() override
Makes the symmetric tensor symmetric (i.e., does nothing).
Definition: Tensor.h:236
Tensor & transpose() override
Transposes the symmetric tensor (i.e., does nothing).
Definition: Tensor.h:234
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:1419
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:1060
SymmTensor & outerProd(const Vec3 &u)
Dyadic (outer) product between two identical vectors.
Definition: Tensor.C:1030
Real vonMises(bool doSqrt=true) const
Returns the von Mises value of the symmetric tensor.
Definition: Tensor.C:1079
void copy(const SymmTensor &T)
Copies a symmetric tensor, possibly with dimension change.
Definition: Tensor.C:843
SymmTensor(const t_ind nsd, bool with33=false)
Constructor creating a zero tensor.
Definition: Tensor.C:802
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:1437
bool principal(Vec3 &p, bool sorted=true) const
Computes the principal values of the symmetric tensor.
Definition: Tensor.C:1108
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:1455
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
Real & operator()(t_ind i, t_ind j)
Index-1 based component access.
Definition: Tensor.h:82
Tensor & postMult(const Tensor &B)
Post-multiplication with another Tensor.
Definition: Tensor.C:366
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
virtual std::ostream & print(std::ostream &os, int prec=0) const
Prints out the tensor to an output stream.
Definition: Tensor.C:690
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
Tensor(const t_ind nsd, bool identity=false)
Constructor creating a zero or identity tesnor.
Definition: Tensor.C:30
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
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
const Real * ptr() const
Reference through a pointer.
Definition: Tensor.h:77
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
Tensor & operator*=(const Tensor &B)
Post-multiplication with another Tensor.
Definition: Tensor.h:106
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
const Real & operator()(t_ind i, t_ind j) const
Index-1 based component reference.
Definition: Tensor.h:80
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 std::ostream & operator<<(std::ostream &os, const Tensor &T)
Output stream operator.
Definition: Tensor.h:171
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