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