https://mooseframework.inl.gov
KokkosTypes.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 "KokkosThread.h"
13 #include "KokkosScalar.h"
14 #include "KokkosArray.h"
15 
16 #include "MooseError.h"
17 #include "MooseUtils.h"
18 
19 #include "libmesh/tensor_tools.h"
20 
21 namespace Moose
22 {
23 namespace Kokkos
24 {
25 
26 struct Real33
27 {
28  Real a[3][3];
29 
30 #ifdef MOOSE_KOKKOS_SCOPE
31  KOKKOS_INLINE_FUNCTION Real33() { *this = 0; }
32  KOKKOS_INLINE_FUNCTION Real33(const Real & scalar) { *this = scalar; }
33  KOKKOS_INLINE_FUNCTION Real33(const Real33 & tensor) { *this = tensor; }
34  KOKKOS_INLINE_FUNCTION Real & operator()(unsigned int i, unsigned int j) { return a[i][j]; }
35  KOKKOS_INLINE_FUNCTION Real operator()(unsigned int i, unsigned int j) const { return a[i][j]; }
36  KOKKOS_INLINE_FUNCTION Real33 & operator=(const Real33 & tensor)
37  {
38  for (unsigned int i = 0; i < 3; ++i)
39  for (unsigned int j = 0; j < 3; ++j)
40  a[i][j] = tensor.a[i][j];
41 
42  return *this;
43  }
44  KOKKOS_INLINE_FUNCTION Real33 & operator=(const Real scalar)
45  {
46  for (unsigned int i = 0; i < 3; ++i)
47  for (unsigned int j = 0; j < 3; ++j)
48  a[i][j] = scalar;
49 
50  return *this;
51  }
52  KOKKOS_INLINE_FUNCTION void operator+=(const Real33 tensor)
53  {
54  for (unsigned int i = 0; i < 3; ++i)
55  for (unsigned int j = 0; j < 3; ++j)
56  a[i][j] += tensor.a[i][j];
57  }
58  KOKKOS_INLINE_FUNCTION Real determinant(const unsigned int dim = 3)
59  {
60  Real det = 0;
61 
62  if (dim == 0)
63  det = 1;
64  else if (dim == 1)
65  det = a[0][0];
66  else if (dim == 2)
67  det = a[0][0] * a[1][1] - a[0][1] * a[1][0];
68  else if (dim == 3)
69  det = a[0][0] * (a[1][1] * a[2][2] - a[1][2] * a[2][1]) -
70  a[0][1] * (a[1][0] * a[2][2] - a[1][2] * a[2][0]) +
71  a[0][2] * (a[1][0] * a[2][1] - a[1][1] * a[2][0]);
72 
73  return det;
74  }
75  KOKKOS_INLINE_FUNCTION Real33 inverse(const unsigned int dim = 3)
76  {
77  Real inv_det = 1.0 / determinant(dim);
78  Real33 inv_mat;
79 
80  if (dim == 1)
81  {
82  inv_mat(0, 0) = inv_det;
83  }
84  else if (dim == 2)
85  {
86  inv_mat(0, 0) = a[1][1] * inv_det;
87  inv_mat(0, 1) = -a[0][1] * inv_det;
88  inv_mat(1, 0) = -a[1][0] * inv_det;
89  inv_mat(1, 1) = a[0][0] * inv_det;
90  }
91  else if (dim == 3)
92  {
93  inv_mat(0, 0) = (a[1][1] * a[2][2] - a[1][2] * a[2][1]) * inv_det;
94  inv_mat(0, 1) = (a[0][2] * a[2][1] - a[0][1] * a[2][2]) * inv_det;
95  inv_mat(0, 2) = (a[0][1] * a[1][2] - a[0][2] * a[1][1]) * inv_det;
96  inv_mat(1, 0) = (a[1][2] * a[2][0] - a[1][0] * a[2][2]) * inv_det;
97  inv_mat(1, 1) = (a[0][0] * a[2][2] - a[0][2] * a[2][0]) * inv_det;
98  inv_mat(1, 2) = (a[0][2] * a[1][0] - a[0][0] * a[1][2]) * inv_det;
99  inv_mat(2, 0) = (a[1][0] * a[2][1] - a[1][1] * a[2][0]) * inv_det;
100  inv_mat(2, 1) = (a[0][1] * a[2][0] - a[0][0] * a[2][1]) * inv_det;
101  inv_mat(2, 2) = (a[0][0] * a[1][1] - a[0][1] * a[1][0]) * inv_det;
102  }
103 
104  return inv_mat;
105  }
106  KOKKOS_INLINE_FUNCTION Real33 transpose()
107  {
108  Real33 tr_mat;
109 
110  for (unsigned int i = 0; i < 3; ++i)
111  for (unsigned int j = 0; j < 3; ++j)
112  tr_mat(i, j) = a[j][i];
113 
114  return tr_mat;
115  }
116 #endif
117 };
118 
119 struct Real3
120 {
121  Real v[3];
122 
123 #ifdef MOOSE_KOKKOS_SCOPE
124  KOKKOS_INLINE_FUNCTION Real3()
125  {
126  v[0] = 0;
127  v[1] = 0;
128  v[2] = 0;
129  }
130  KOKKOS_INLINE_FUNCTION Real3(const Real & scalar)
131  {
132  v[0] = scalar;
133  v[1] = scalar;
134  v[2] = scalar;
135  }
136  KOKKOS_INLINE_FUNCTION Real3(const Real3 & vector)
137  {
138  v[0] = vector.v[0];
139  v[1] = vector.v[1];
140  v[2] = vector.v[2];
141  }
142  KOKKOS_INLINE_FUNCTION Real3(const Real & x, const Real & y, const Real & z)
143  {
144  v[0] = x;
145  v[1] = y;
146  v[2] = z;
147  }
149  {
150  v[0] = vector(0);
151  v[1] = vector(1);
152  v[2] = vector(2);
153  }
154 
155  KOKKOS_INLINE_FUNCTION Real & operator()(unsigned int i) { return v[i]; }
156  KOKKOS_INLINE_FUNCTION Real operator()(unsigned int i) const { return v[i]; }
157 
158  KOKKOS_INLINE_FUNCTION Real3 & operator=(const Real3 & vector)
159  {
160  v[0] = vector.v[0];
161  v[1] = vector.v[1];
162  v[2] = vector.v[2];
163 
164  return *this;
165  }
166  KOKKOS_INLINE_FUNCTION Real3 & operator=(const Real scalar)
167  {
168  v[0] = scalar;
169  v[1] = scalar;
170  v[2] = scalar;
171 
172  return *this;
173  }
175  {
176  v[0] = vector(0);
177  v[1] = vector(1);
178  v[2] = vector(2);
179 
180  return *this;
181  }
182  KOKKOS_INLINE_FUNCTION void operator+=(const Real scalar)
183  {
184  v[0] += scalar;
185  v[1] += scalar;
186  v[2] += scalar;
187  }
188  KOKKOS_INLINE_FUNCTION void operator+=(const Real3 vector)
189  {
190  v[0] += vector.v[0];
191  v[1] += vector.v[1];
192  v[2] += vector.v[2];
193  }
194  KOKKOS_INLINE_FUNCTION void operator-=(const Real scalar)
195  {
196  v[0] -= scalar;
197  v[1] -= scalar;
198  v[2] -= scalar;
199  }
200  KOKKOS_INLINE_FUNCTION void operator-=(const Real3 vector)
201  {
202  v[0] -= vector.v[0];
203  v[1] -= vector.v[1];
204  v[2] -= vector.v[2];
205  }
206  KOKKOS_INLINE_FUNCTION void operator*=(const Real scalar)
207  {
208  v[0] *= scalar;
209  v[1] *= scalar;
210  v[2] *= scalar;
211  }
212  KOKKOS_INLINE_FUNCTION Real norm() { return std::sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]); }
213  KOKKOS_INLINE_FUNCTION Real dot_product(const Real3 vector)
214  {
215  return v[0] * vector.v[0] + v[1] * vector.v[1] + v[2] * vector.v[2];
216  }
217  KOKKOS_INLINE_FUNCTION Real3 cross_product(const Real3 vector)
218  {
219  Real3 cross;
220 
221  cross.v[0] = v[1] * vector.v[2] - v[2] * vector.v[1];
222  cross.v[1] = v[2] * vector.v[0] - v[0] * vector.v[2];
223  cross.v[2] = v[0] * vector.v[1] - v[1] * vector.v[0];
224 
225  return cross;
226  }
227  KOKKOS_INLINE_FUNCTION Real33 cartesian_product(const Real3 vector)
228  {
229  Real33 tensor;
230 
231  for (unsigned int i = 0; i < 3; ++i)
232  for (unsigned int j = 0; j < 3; ++j)
233  tensor(i, j) = v[i] * vector.v[j];
234 
235  return tensor;
236  }
237 #endif
238 };
239 
240 #ifdef MOOSE_KOKKOS_SCOPE
241 KOKKOS_INLINE_FUNCTION Real3
242 operator*(const Real left, const Real3 right)
243 {
244  return {left * right.v[0], left * right.v[1], left * right.v[2]};
245 }
246 KOKKOS_INLINE_FUNCTION Real3
247 operator*(const Real3 left, const Real right)
248 {
249  return {left.v[0] * right, left.v[1] * right, left.v[2] * right};
250 }
251 KOKKOS_INLINE_FUNCTION Real
252 operator*(const Real3 left, const Real3 right)
253 {
254  return left.v[0] * right.v[0] + left.v[1] * right.v[1] + left.v[2] * right.v[2];
255 }
256 KOKKOS_INLINE_FUNCTION Real3
257 operator*(const Real33 left, const Real3 right)
258 {
259  return {left(0, 0) * right.v[0] + left(0, 1) * right.v[1] + left(0, 2) * right.v[2],
260  left(1, 0) * right.v[0] + left(1, 1) * right.v[1] + left(1, 2) * right.v[2],
261  left(2, 0) * right.v[0] + left(2, 1) * right.v[1] + left(2, 2) * right.v[2]};
262 }
263 KOKKOS_INLINE_FUNCTION Real33
264 operator*(const Real33 left, const Real33 right)
265 {
266  Real33 mul;
267 
268  for (unsigned int i = 0; i < 3; ++i)
269  for (unsigned int j = 0; j < 3; ++j)
270  for (unsigned int k = 0; k < 3; ++k)
271  mul(i, j) += left(i, k) * right(k, j);
272 
273  return mul;
274 }
275 KOKKOS_INLINE_FUNCTION Real3
276 operator+(const Real left, const Real3 right)
277 {
278  return {left + right.v[0], left + right.v[1], left + right.v[2]};
279 }
280 KOKKOS_INLINE_FUNCTION Real3
281 operator+(const Real3 left, const Real right)
282 {
283  return {left.v[0] + right, left.v[1] + right, left.v[2] + right};
284 }
285 KOKKOS_INLINE_FUNCTION Real3
286 operator+(const Real3 left, const Real3 right)
287 {
288  return {left.v[0] + right.v[0], left.v[1] + right.v[1], left.v[2] + right.v[2]};
289 }
290 KOKKOS_INLINE_FUNCTION Real3
291 operator-(const Real left, const Real3 right)
292 {
293  return {left - right.v[0], left - right.v[1], left - right.v[2]};
294 }
295 KOKKOS_INLINE_FUNCTION Real3
296 operator-(const Real3 left, const Real right)
297 {
298  return {left.v[0] - right, left.v[1] - right, left.v[2] - right};
299 }
300 KOKKOS_INLINE_FUNCTION Real3
301 operator-(const Real3 left, const Real3 right)
302 {
303  return {left.v[0] - right.v[0], left.v[1] - right.v[1], left.v[2] - right.v[2]};
304 }
305 #endif
306 
307 template <typename T1, typename T2>
308 struct Pair
309 {
310  T1 first;
311  T2 second;
312 
313  template <typename T3, typename T4>
314  auto & operator=(const std::pair<T3, T4> pair)
315  {
316  first = pair.first;
317  second = pair.second;
318 
319  return *this;
320  }
321 };
322 
323 template <typename T1, typename T2>
324 bool
325 operator<(const Pair<T1, T2> & left, const Pair<T1, T2> & right)
326 {
327  return std::make_pair(left.first, left.second) < std::make_pair(right.first, right.second);
328 }
329 
330 } // namespace Kokkos
331 } // namespace Moose
KOKKOS_INLINE_FUNCTION Real3 operator*(const Real left, const Real3 right)
Definition: KokkosTypes.h:242
KOKKOS_INLINE_FUNCTION Real3(const Real &x, const Real &y, const Real &z)
Definition: KokkosTypes.h:142
KOKKOS_INLINE_FUNCTION Real norm()
Definition: KokkosTypes.h:212
KOKKOS_INLINE_FUNCTION Real3 operator+(const Real left, const Real3 right)
Definition: KokkosTypes.h:276
KOKKOS_INLINE_FUNCTION Real operator()(unsigned int i, unsigned int j) const
Definition: KokkosTypes.h:35
KOKKOS_INLINE_FUNCTION Real3(const Real &scalar)
Definition: KokkosTypes.h:130
KOKKOS_INLINE_FUNCTION Real33()
Definition: KokkosTypes.h:31
static constexpr std::size_t dim
This is the dimension of all vector and tensor datastructures used in MOOSE.
Definition: Moose.h:159
KOKKOS_INLINE_FUNCTION Real3 cross_product(const Real3 vector)
Definition: KokkosTypes.h:217
KOKKOS_INLINE_FUNCTION void operator+=(const Real scalar)
Definition: KokkosTypes.h:182
KOKKOS_INLINE_FUNCTION Real dot_product(const Real3 vector)
Definition: KokkosTypes.h:213
Real3 & operator=(const libMesh::TypeVector< Real > &vector)
Definition: KokkosTypes.h:174
auto & operator=(const std::pair< T3, T4 > pair)
Definition: KokkosTypes.h:314
KOKKOS_INLINE_FUNCTION Real33 cartesian_product(const Real3 vector)
Definition: KokkosTypes.h:227
KOKKOS_INLINE_FUNCTION Real operator()(unsigned int i) const
Definition: KokkosTypes.h:156
KOKKOS_INLINE_FUNCTION Real3 & operator=(const Real3 &vector)
Definition: KokkosTypes.h:158
KOKKOS_INLINE_FUNCTION void operator+=(const Real33 tensor)
Definition: KokkosTypes.h:52
KOKKOS_INLINE_FUNCTION void operator-=(const Real3 vector)
Definition: KokkosTypes.h:200
KOKKOS_INLINE_FUNCTION Real33 transpose()
Definition: KokkosTypes.h:106
KOKKOS_INLINE_FUNCTION Real3(const Real3 &vector)
Definition: KokkosTypes.h:136
KOKKOS_INLINE_FUNCTION Real33 & operator=(const Real33 &tensor)
Definition: KokkosTypes.h:36
KOKKOS_INLINE_FUNCTION Real3 operator-(const Real left, const Real3 right)
Definition: KokkosTypes.h:291
Real3(const libMesh::TypeVector< Real > &vector)
Definition: KokkosTypes.h:148
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
KOKKOS_INLINE_FUNCTION Real determinant(const unsigned int dim=3)
Definition: KokkosTypes.h:58
CTSub CT_OPERATOR_BINARY CTMul CTCompareLess CTCompareGreater CTCompareEqual _arg template * sqrt(_arg)) *_arg.template D< dtag >()) CT_SIMPLE_UNARY_FUNCTION(tanh
KOKKOS_INLINE_FUNCTION Real3 & operator=(const Real scalar)
Definition: KokkosTypes.h:166
KOKKOS_INLINE_FUNCTION Real33(const Real33 &tensor)
Definition: KokkosTypes.h:33
KOKKOS_INLINE_FUNCTION Real & operator()(unsigned int i, unsigned int j)
Definition: KokkosTypes.h:34
KOKKOS_INLINE_FUNCTION Real33 & operator=(const Real scalar)
Definition: KokkosTypes.h:44
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...
KOKKOS_INLINE_FUNCTION Real3()
Definition: KokkosTypes.h:124
KOKKOS_INLINE_FUNCTION void operator*=(const Real scalar)
Definition: KokkosTypes.h:206
KOKKOS_INLINE_FUNCTION void operator-=(const Real scalar)
Definition: KokkosTypes.h:194
KOKKOS_INLINE_FUNCTION Real & operator()(unsigned int i)
Definition: KokkosTypes.h:155
KOKKOS_INLINE_FUNCTION void operator+=(const Real3 vector)
Definition: KokkosTypes.h:188
KOKKOS_INLINE_FUNCTION Real33 inverse(const unsigned int dim=3)
Definition: KokkosTypes.h:75
KOKKOS_INLINE_FUNCTION Real33(const Real &scalar)
Definition: KokkosTypes.h:32