www.mooseframework.org
SymmTensor.h
Go to the documentation of this file.
1 /****************************************************************/
2 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
3 /* */
4 /* All contents are licensed under LGPL V2.1 */
5 /* See LICENSE for full restrictions */
6 /****************************************************************/
7 #ifndef SYMMTENSOR_H
8 #define SYMMTENSOR_H
9 
10 #include "ColumnMajorMatrix.h"
11 #include "MaterialProperty.h"
12 #include "DataIO.h"
13 #include "MooseRandom.h"
14 
15 #include <cmath>
16 #include "libmesh/libmesh.h"
17 #include "libmesh/point.h"
18 
20 {
21 public:
22  SymmTensor() : _xx(0), _yy(0), _zz(0), _xy(0), _yz(0), _zx(0) {}
23  explicit SymmTensor(Real init) : _xx(init), _yy(init), _zz(init), _xy(init), _yz(init), _zx(init)
24  {
25  }
26  SymmTensor(Real xx, Real yy, Real zz, Real xy, Real yz, Real zx)
27  : _xx(xx), _yy(yy), _zz(zz), _xy(xy), _yz(yz), _zx(zx)
28  {
29  }
30  explicit SymmTensor(const ColumnMajorMatrix & cmm)
31  : _xx(cmm.numEntries() == 9 ? cmm.rawData()[0] : 0),
32  _yy(cmm.numEntries() == 9 ? cmm.rawData()[4] : 0),
33  _zz(cmm.numEntries() == 9 ? cmm.rawData()[8] : 0),
34  _xy(cmm.numEntries() == 9 ? cmm.rawData()[1] : 0),
35  _yz(cmm.numEntries() == 9 ? cmm.rawData()[5] : 0),
36  _zx(cmm.numEntries() == 9 ? cmm.rawData()[2] : 0)
37  {
38  if (cmm.numEntries() != 9)
39  {
40  mooseError("Cannot create SymmTensor from ColumnMajorMatrix. Wrong number of entries.");
41  }
42  }
43 
44  explicit SymmTensor(const std::vector<Real> & init_list)
45  : _xx(init_list[0]),
46  _yy(init_list[1]),
47  _zz(init_list[2]),
48  _xy(init_list[3]),
49  _yz(init_list[4]),
50  _zx(init_list[5])
51  {
52  // test the length to make sure it's 6 long
53  if (init_list.size() != 6)
54  {
55  mooseError("SymmTensor initialization error: please enter a vector with 6 entries.");
56  }
57  }
58 
59  void fillFromInputVector(const std::vector<Real> & input)
60  {
61  if (input.size() != 6)
62  {
63  mooseError("SymmTensor error. Input vector must have six entries.");
64  }
65  _xx = input[0];
66  _yy = input[1];
67  _zz = input[2];
68  _xy = input[3];
69  _yz = input[4];
70  _zx = input[5];
71  }
72 
73  Real rowDot(const unsigned int r, const libMesh::TypeVector<Real> & v) const
74  {
75  mooseAssert(LIBMESH_DIM == 3, "Incompatible sizes");
76  if (0 == r)
77  {
78  return _xx * v(0) + _xy * v(1) + _zx * v(2);
79  }
80  else if (1 == r)
81  {
82  return _xy * v(0) + _yy * v(1) + _yz * v(2);
83  }
84  else if (2 == r)
85  {
86  return _zx * v(0) + _yz * v(1) + _zz * v(2);
87  }
88  else
89  {
90  mooseError("Incorrect row");
91  }
92  return 0;
93  }
94 
95  Real trace() const { return _xx + _yy + _zz; }
96 
97  Real component(unsigned int i) const
98  {
99  if (0 == i)
100  {
101  return _xx;
102  }
103  else if (1 == i)
104  {
105  return _yy;
106  }
107  else if (2 == i)
108  {
109  return _zz;
110  }
111  else if (3 == i)
112  {
113  return _xy;
114  }
115  else if (4 == i)
116  {
117  return _yz;
118  }
119  else if (5 == i)
120  {
121  return _zx;
122  }
123  else
124  {
125  mooseError("Invalid entry requested for SymmTensor");
126  }
127  return 0;
128  }
129  Real xx() const { return _xx; }
130  Real yy() const { return _yy; }
131  Real zz() const { return _zz; }
132  Real xy() const { return _xy; }
133  Real yz() const { return _yz; }
134  Real zx() const { return _zx; }
135  Real yx() const { return _xy; }
136  Real zy() const { return _yz; }
137  Real xz() const { return _zx; }
138  Real & xx() { return _xx; }
139  Real & yy() { return _yy; }
140  Real & zz() { return _zz; }
141  Real & xy() { return _xy; }
142  Real & yz() { return _yz; }
143  Real & zx() { return _zx; }
144  Real & yx() { return _xy; }
145  Real & zy() { return _yz; }
146  Real & xz() { return _zx; }
147  Real & operator()(const unsigned i, const unsigned j)
148  {
149  Real * rVal(NULL);
150  if (0 == i)
151  {
152  if (0 == j)
153  {
154  rVal = &_xx;
155  }
156  else if (1 == j)
157  {
158  rVal = &_xy;
159  }
160  else if (2 == j)
161  {
162  rVal = &_zx;
163  }
164  }
165  else if (1 == i)
166  {
167  if (0 == j)
168  {
169  rVal = &_xy;
170  }
171  else if (1 == j)
172  {
173  rVal = &_yy;
174  }
175  else if (2 == j)
176  {
177  rVal = &_yz;
178  }
179  }
180  else if (2 == i)
181  {
182  if (0 == j)
183  {
184  rVal = &_zx;
185  }
186  else if (1 == j)
187  {
188  rVal = &_yz;
189  }
190  else if (2 == j)
191  {
192  rVal = &_zz;
193  }
194  }
195  if (!rVal)
196  {
197  mooseError("Index must be 0, 1, or 2");
198  }
199  return *rVal;
200  }
201 
202  Real operator()(const unsigned i, const unsigned j) const
203  {
204  const Real * rVal(NULL);
205  if (0 == i)
206  {
207  if (0 == j)
208  {
209  rVal = &_xx;
210  }
211  else if (1 == j)
212  {
213  rVal = &_xy;
214  }
215  else if (2 == j)
216  {
217  rVal = &_zx;
218  }
219  }
220  else if (1 == i)
221  {
222  if (0 == j)
223  {
224  rVal = &_xy;
225  }
226  else if (1 == j)
227  {
228  rVal = &_yy;
229  }
230  else if (2 == j)
231  {
232  rVal = &_yz;
233  }
234  }
235  else if (2 == i)
236  {
237  if (0 == j)
238  {
239  rVal = &_zx;
240  }
241  else if (1 == j)
242  {
243  rVal = &_yz;
244  }
245  else if (2 == j)
246  {
247  rVal = &_zz;
248  }
249  }
250  if (!rVal)
251  {
252  mooseError("Index must be 0, 1, or 2");
253  }
254  return *rVal;
255  }
256 
257  Real doubleContraction(const SymmTensor & rhs) const
258  {
259  return _xx * rhs._xx + _yy * rhs._yy + _zz * rhs._zz +
260  2 * (_xy * rhs._xy + _yz * rhs._yz + _zx * rhs._zx);
261  }
262 
263  void xx(Real xx) { _xx = xx; }
264  void yy(Real yy) { _yy = yy; }
265  void zz(Real zz) { _zz = zz; }
266  void xy(Real xy) { _xy = xy; }
267  void yz(Real yz) { _yz = yz; }
268  void zx(Real zx) { _zx = zx; }
269  void yx(Real yx) { _xy = yx; }
270  void zy(Real zy) { _yz = zy; }
271  void xz(Real xz) { _zx = xz; }
272 
273  void zero() { _xx = _yy = _zz = _xy = _yz = _zx = 0; }
274  void identity()
275  {
276  _xx = _yy = _zz = 1;
277  _xy = _yz = _zx = 0;
278  }
279  void addDiag(Real value)
280  {
281  _xx += value;
282  _yy += value;
283  _zz += value;
284  }
285  bool operator==(const SymmTensor & rhs) const
286  {
287  return _xx == rhs._xx && _yy == rhs._yy && _zz == rhs._zz && _xy == rhs._xy && _yz == rhs._yz &&
288  _zx == rhs._zx;
289  }
290  bool operator!=(const SymmTensor & rhs) const { return !operator==(rhs); }
291 
293  {
294  _xx += t._xx;
295  _yy += t._yy;
296  _zz += t._zz;
297  _xy += t._xy;
298  _yz += t._yz;
299  _zx += t._zx;
300  return *this;
301  }
302 
304  {
305  _xx -= t._xx;
306  _yy -= t._yy;
307  _zz -= t._zz;
308  _xy -= t._xy;
309  _yz -= t._yz;
310  _zx -= t._zx;
311  return *this;
312  }
313 
314  SymmTensor operator+(const SymmTensor & t) const
315  {
316  SymmTensor r_val;
317  r_val._xx = _xx + t._xx;
318  r_val._yy = _yy + t._yy;
319  r_val._zz = _zz + t._zz;
320  r_val._xy = _xy + t._xy;
321  r_val._yz = _yz + t._yz;
322  r_val._zx = _zx + t._zx;
323  return r_val;
324  }
325 
326  SymmTensor operator*(Real t) const
327  {
328  SymmTensor r_val;
329 
330  r_val._xx = _xx * t;
331  r_val._yy = _yy * t;
332  r_val._zz = _zz * t;
333  r_val._xy = _xy * t;
334  r_val._yz = _yz * t;
335  r_val._zx = _zx * t;
336  return r_val;
337  }
338 
339  Point operator*(const Point & p) const
340  {
341  return Point(_xx * p(0) + _xy * p(1) + _zx * p(2),
342  _xy * p(0) + _yy * p(1) + _yz * p(2),
343  _zx * p(0) + _yz * p(1) + _zz * p(2));
344  }
345 
346  SymmTensor operator-(const SymmTensor & t) const
347  {
348  SymmTensor r_val;
349  r_val._xx = _xx - t._xx;
350  r_val._yy = _yy - t._yy;
351  r_val._zz = _zz - t._zz;
352  r_val._xy = _xy - t._xy;
353  r_val._yz = _yz - t._yz;
354  r_val._zx = _zx - t._zx;
355  return r_val;
356  }
357 
358  SymmTensor & operator+=(const ColumnMajorMatrix & cmm)
359  {
360  mooseAssert(cmm.numEntries() == 9,
361  "Cannot add ColumnMajorMatrix to SymmTensor. Wrong number of entries.");
362  const Real * data = cmm.rawData();
363  _xx += data[0];
364  _xy += data[1];
365  _zx += data[2];
366  _yy += data[4];
367  _yz += data[5];
368  _zz += data[8];
369  return *this;
370  }
371 
372  SymmTensor & operator-=(const ColumnMajorMatrix & cmm)
373  {
374  mooseAssert(cmm.numEntries() == 9,
375  "Cannot add ColumnMajorMatrix to SymmTensor. Wrong number of entries.");
376  const Real * data = cmm.rawData();
377 
378  _xx -= data[0];
379  _xy -= data[1];
380  _zx -= data[2];
381  _yy -= data[4];
382  _yz -= data[5];
383  _zz -= data[8];
384  return *this;
385  }
386 
387  SymmTensor & operator=(const ColumnMajorMatrix & cmm)
388  {
389  mooseAssert(cmm.numEntries() == 9,
390  "Cannot set SymmTensor to ColumnMajorMatrix. Wrong number of entries.");
391  const Real * data = cmm.rawData();
392  _xx = data[0];
393  _xy = data[1];
394  _zx = data[2];
395  _yy = data[4];
396  _yz = data[5];
397  _zz = data[8];
398  return *this;
399  }
400 
401  SymmTensor & operator=(Real val)
402  {
403  _xx = val;
404  _xy = val;
405  _zx = val;
406  _yy = val;
407  _yz = val;
408  _zz = val;
409  return *this;
410  }
411 
412  SymmTensor & operator*=(Real val)
413  {
414  _xx *= val;
415  _xy *= val;
416  _zx *= val;
417  _yy *= val;
418  _yz *= val;
419  _zz *= val;
420  return *this;
421  }
422 
423  ColumnMajorMatrix columnMajorMatrix() const
424  {
425  ColumnMajorMatrix cmm(3, 3);
426  cmm(0, 0) = _xx;
427  cmm(1, 0) = _xy;
428  cmm(2, 0) = _zx;
429  cmm(0, 1) = _xy;
430  cmm(1, 1) = _yy;
431  cmm(2, 1) = _yz;
432  cmm(0, 2) = _zx;
433  cmm(1, 2) = _yz;
434  cmm(2, 2) = _zz;
435  return cmm;
436  }
437 
438  friend std::ostream & operator<<(std::ostream & stream, const SymmTensor & obj);
439 
440  static void initRandom()
441  {
442 
443  unsigned int randinit = 2000;
444  MooseRandom::seed(randinit);
445  }
446 
447  static SymmTensor genRandomSymmTensor(Real scalefactor)
448  {
449 
450  SymmTensor tensor;
451 
452  tensor.xx() = (MooseRandom::rand() + 1.0) * scalefactor;
453  tensor.yy() = (MooseRandom::rand() + 1.0) * scalefactor;
454  tensor.zz() = (MooseRandom::rand() + 1.0) * scalefactor;
455  tensor.xy() = (MooseRandom::rand() + 1.0) * scalefactor;
456  tensor.yz() = (MooseRandom::rand() + 1.0) * scalefactor;
457  tensor.zx() = (MooseRandom::rand() + 1.0) * scalefactor;
458 
459  return tensor;
460  }
461 
462 private:
463  Real _xx;
464  Real _yy;
465  Real _zz;
466  Real _xy;
467  Real _yz;
468  Real _zx;
469 };
470 
471 template <>
472 PropertyValue * MaterialProperty<SymmTensor>::init(int size);
473 
474 template <>
475 void dataStore(std::ostream & stream, const SymmTensor & v, void * /*context*/);
476 
477 template <>
478 void dataLoad(std::istream & stream, SymmTensor & v, void * /*context*/);
479 
480 #endif // SYMMTENSOR_H
SymmTensor & operator-=(const SymmTensor &t)
Definition: SymmTensor.h:303
SymmTensor(Real init)
Definition: SymmTensor.h:23
Real & yy()
Definition: SymmTensor.h:139
Real yy() const
Definition: SymmTensor.h:130
SymmTensor & operator+=(const ColumnMajorMatrix &cmm)
Definition: SymmTensor.h:358
void fillFromInputVector(const std::vector< Real > &input)
Definition: SymmTensor.h:59
static void initRandom()
Definition: SymmTensor.h:440
SymmTensor operator+(const SymmTensor &t) const
Definition: SymmTensor.h:314
Real operator()(const unsigned i, const unsigned j) const
Definition: SymmTensor.h:202
Real & xz()
Definition: SymmTensor.h:146
Real rowDot(const unsigned int r, const libMesh::TypeVector< Real > &v) const
Definition: SymmTensor.h:73
Real & zz()
Definition: SymmTensor.h:140
Real xx() const
Definition: SymmTensor.h:129
bool operator!=(const SymmTensor &rhs) const
Definition: SymmTensor.h:290
void xy(Real xy)
Definition: SymmTensor.h:266
SymmTensor & operator=(Real val)
Definition: SymmTensor.h:401
SymmTensor & operator-=(const ColumnMajorMatrix &cmm)
Definition: SymmTensor.h:372
SymmTensor & operator+=(const SymmTensor &t)
Definition: SymmTensor.h:292
ColumnMajorMatrix columnMajorMatrix() const
Definition: SymmTensor.h:423
void xx(Real xx)
Definition: SymmTensor.h:263
static SymmTensor genRandomSymmTensor(Real scalefactor)
Definition: SymmTensor.h:447
Real zz() const
Definition: SymmTensor.h:131
Real & yz()
Definition: SymmTensor.h:142
void zero()
Definition: SymmTensor.h:273
SymmTensor(const ColumnMajorMatrix &cmm)
Definition: SymmTensor.h:30
void yy(Real yy)
Definition: SymmTensor.h:264
void yx(Real yx)
Definition: SymmTensor.h:269
Real & xy()
Definition: SymmTensor.h:141
SymmTensor operator*(Real t) const
Definition: SymmTensor.h:326
Real xz() const
Definition: SymmTensor.h:137
Real trace() const
Definition: SymmTensor.h:95
Real zy() const
Definition: SymmTensor.h:136
Real & zy()
Definition: SymmTensor.h:145
Real & operator()(const unsigned i, const unsigned j)
Definition: SymmTensor.h:147
Real xy() const
Definition: SymmTensor.h:132
Real & xx()
Definition: SymmTensor.h:138
void dataStore(std::ostream &stream, const SymmTensor &v, void *)
Definition: SymmTensor.C:33
Real yz() const
Definition: SymmTensor.h:133
Real zx() const
Definition: SymmTensor.h:134
Real & yx()
Definition: SymmTensor.h:144
SymmTensor operator-(const SymmTensor &t) const
Definition: SymmTensor.h:346
void zz(Real zz)
Definition: SymmTensor.h:265
Real & zx()
Definition: SymmTensor.h:143
void addDiag(Real value)
Definition: SymmTensor.h:279
Real doubleContraction(const SymmTensor &rhs) const
Definition: SymmTensor.h:257
SymmTensor(const std::vector< Real > &init_list)
Definition: SymmTensor.h:44
void identity()
Definition: SymmTensor.h:274
void zy(Real zy)
Definition: SymmTensor.h:270
bool operator==(const SymmTensor &rhs) const
Definition: SymmTensor.h:285
Point operator*(const Point &p) const
Definition: SymmTensor.h:339
void dataLoad(std::istream &stream, SymmTensor &v, void *)
Definition: SymmTensor.C:52
SymmTensor & operator=(const ColumnMajorMatrix &cmm)
Definition: SymmTensor.h:387
void yz(Real yz)
Definition: SymmTensor.h:267
Real component(unsigned int i) const
Definition: SymmTensor.h:97
void zx(Real zx)
Definition: SymmTensor.h:268
SymmTensor & operator*=(Real val)
Definition: SymmTensor.h:412
void xz(Real xz)
Definition: SymmTensor.h:271
Real yx() const
Definition: SymmTensor.h:135
friend std::ostream & operator<<(std::ostream &stream, const SymmTensor &obj)
Definition: SymmTensor.C:10
SymmTensor(Real xx, Real yy, Real zz, Real xy, Real yz, Real zx)
Definition: SymmTensor.h:26