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 void identity(const unsigned int dim = 3)
59  {
60  *this = 0;
61 
62  for (unsigned int i = 0; i < dim; ++i)
63  a[i][i] = 1;
64  }
65  KOKKOS_INLINE_FUNCTION Real determinant(const unsigned int dim = 3)
66  {
67  Real det = 0;
68 
69  if (dim == 0)
70  det = 1;
71  else if (dim == 1)
72  det = a[0][0];
73  else if (dim == 2)
74  det = a[0][0] * a[1][1] - a[0][1] * a[1][0];
75  else if (dim == 3)
76  det = a[0][0] * (a[1][1] * a[2][2] - a[1][2] * a[2][1]) -
77  a[0][1] * (a[1][0] * a[2][2] - a[1][2] * a[2][0]) +
78  a[0][2] * (a[1][0] * a[2][1] - a[1][1] * a[2][0]);
79 
80  return det;
81  }
82  KOKKOS_INLINE_FUNCTION Real33 inverse(const unsigned int dim = 3)
83  {
84  Real inv_det = 1.0 / determinant(dim);
85  Real33 inv_mat;
86 
87  if (dim == 1)
88  {
89  inv_mat(0, 0) = inv_det;
90  }
91  else if (dim == 2)
92  {
93  inv_mat(0, 0) = a[1][1] * inv_det;
94  inv_mat(0, 1) = -a[0][1] * inv_det;
95  inv_mat(1, 0) = -a[1][0] * inv_det;
96  inv_mat(1, 1) = a[0][0] * inv_det;
97  }
98  else if (dim == 3)
99  {
100  inv_mat(0, 0) = (a[1][1] * a[2][2] - a[1][2] * a[2][1]) * inv_det;
101  inv_mat(0, 1) = (a[0][2] * a[2][1] - a[0][1] * a[2][2]) * inv_det;
102  inv_mat(0, 2) = (a[0][1] * a[1][2] - a[0][2] * a[1][1]) * inv_det;
103  inv_mat(1, 0) = (a[1][2] * a[2][0] - a[1][0] * a[2][2]) * inv_det;
104  inv_mat(1, 1) = (a[0][0] * a[2][2] - a[0][2] * a[2][0]) * inv_det;
105  inv_mat(1, 2) = (a[0][2] * a[1][0] - a[0][0] * a[1][2]) * inv_det;
106  inv_mat(2, 0) = (a[1][0] * a[2][1] - a[1][1] * a[2][0]) * inv_det;
107  inv_mat(2, 1) = (a[0][1] * a[2][0] - a[0][0] * a[2][1]) * inv_det;
108  inv_mat(2, 2) = (a[0][0] * a[1][1] - a[0][1] * a[1][0]) * inv_det;
109  }
110 
111  return inv_mat;
112  }
113  KOKKOS_INLINE_FUNCTION Real33 transpose()
114  {
115  Real33 tr_mat;
116 
117  for (unsigned int i = 0; i < 3; ++i)
118  for (unsigned int j = 0; j < 3; ++j)
119  tr_mat(i, j) = a[j][i];
120 
121  return tr_mat;
122  }
123 #endif
124 };
125 
126 struct Real3
127 {
128  Real v[3];
129 
130 #ifdef MOOSE_KOKKOS_SCOPE
131  KOKKOS_INLINE_FUNCTION Real3()
132  {
133  v[0] = 0;
134  v[1] = 0;
135  v[2] = 0;
136  }
137  KOKKOS_INLINE_FUNCTION Real3(const Real & scalar)
138  {
139  v[0] = scalar;
140  v[1] = scalar;
141  v[2] = scalar;
142  }
143  KOKKOS_INLINE_FUNCTION Real3(const Real3 & vector)
144  {
145  v[0] = vector.v[0];
146  v[1] = vector.v[1];
147  v[2] = vector.v[2];
148  }
149  KOKKOS_INLINE_FUNCTION Real3(const Real & x, const Real & y, const Real & z)
150  {
151  v[0] = x;
152  v[1] = y;
153  v[2] = z;
154  }
156  {
157  v[0] = vector(0);
158  v[1] = vector(1);
159  v[2] = vector(2);
160  }
161 
162  KOKKOS_INLINE_FUNCTION Real & operator()(unsigned int i) { return v[i]; }
163  KOKKOS_INLINE_FUNCTION Real operator()(unsigned int i) const { return v[i]; }
164 
165  KOKKOS_INLINE_FUNCTION Real3 & operator=(const Real3 & vector)
166  {
167  v[0] = vector.v[0];
168  v[1] = vector.v[1];
169  v[2] = vector.v[2];
170 
171  return *this;
172  }
173  KOKKOS_INLINE_FUNCTION Real3 & operator=(const Real scalar)
174  {
175  v[0] = scalar;
176  v[1] = scalar;
177  v[2] = scalar;
178 
179  return *this;
180  }
182  {
183  v[0] = vector(0);
184  v[1] = vector(1);
185  v[2] = vector(2);
186 
187  return *this;
188  }
189  KOKKOS_INLINE_FUNCTION void operator+=(const Real scalar)
190  {
191  v[0] += scalar;
192  v[1] += scalar;
193  v[2] += scalar;
194  }
195  KOKKOS_INLINE_FUNCTION void operator+=(const Real3 vector)
196  {
197  v[0] += vector.v[0];
198  v[1] += vector.v[1];
199  v[2] += vector.v[2];
200  }
201  KOKKOS_INLINE_FUNCTION void operator-=(const Real scalar)
202  {
203  v[0] -= scalar;
204  v[1] -= scalar;
205  v[2] -= scalar;
206  }
207  KOKKOS_INLINE_FUNCTION void operator-=(const Real3 vector)
208  {
209  v[0] -= vector.v[0];
210  v[1] -= vector.v[1];
211  v[2] -= vector.v[2];
212  }
213  KOKKOS_INLINE_FUNCTION void operator*=(const Real scalar)
214  {
215  v[0] *= scalar;
216  v[1] *= scalar;
217  v[2] *= scalar;
218  }
219  KOKKOS_INLINE_FUNCTION Real norm() { return std::sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]); }
220  KOKKOS_INLINE_FUNCTION Real dot_product(const Real3 vector)
221  {
222  return v[0] * vector.v[0] + v[1] * vector.v[1] + v[2] * vector.v[2];
223  }
224  KOKKOS_INLINE_FUNCTION Real3 cross_product(const Real3 vector)
225  {
226  Real3 cross;
227 
228  cross.v[0] = v[1] * vector.v[2] - v[2] * vector.v[1];
229  cross.v[1] = v[2] * vector.v[0] - v[0] * vector.v[2];
230  cross.v[2] = v[0] * vector.v[1] - v[1] * vector.v[0];
231 
232  return cross;
233  }
234  KOKKOS_INLINE_FUNCTION Real33 cartesian_product(const Real3 vector)
235  {
236  Real33 tensor;
237 
238  for (unsigned int i = 0; i < 3; ++i)
239  for (unsigned int j = 0; j < 3; ++j)
240  tensor(i, j) = v[i] * vector.v[j];
241 
242  return tensor;
243  }
244 #endif
245 };
246 
247 #ifdef MOOSE_KOKKOS_SCOPE
248 KOKKOS_INLINE_FUNCTION Real3
249 operator*(const Real left, const Real3 right)
250 {
251  return {left * right.v[0], left * right.v[1], left * right.v[2]};
252 }
253 KOKKOS_INLINE_FUNCTION Real3
254 operator*(const Real3 left, const Real right)
255 {
256  return {left.v[0] * right, left.v[1] * right, left.v[2] * right};
257 }
258 KOKKOS_INLINE_FUNCTION Real
259 operator*(const Real3 left, const Real3 right)
260 {
261  return left.v[0] * right.v[0] + left.v[1] * right.v[1] + left.v[2] * right.v[2];
262 }
263 KOKKOS_INLINE_FUNCTION Real3
264 operator*(const Real33 left, const Real3 right)
265 {
266  return {left(0, 0) * right.v[0] + left(0, 1) * right.v[1] + left(0, 2) * right.v[2],
267  left(1, 0) * right.v[0] + left(1, 1) * right.v[1] + left(1, 2) * right.v[2],
268  left(2, 0) * right.v[0] + left(2, 1) * right.v[1] + left(2, 2) * right.v[2]};
269 }
270 KOKKOS_INLINE_FUNCTION Real33
271 operator*(const Real33 left, const Real33 right)
272 {
273  Real33 mul;
274 
275  for (unsigned int i = 0; i < 3; ++i)
276  for (unsigned int j = 0; j < 3; ++j)
277  for (unsigned int k = 0; k < 3; ++k)
278  mul(i, j) += left(i, k) * right(k, j);
279 
280  return mul;
281 }
282 KOKKOS_INLINE_FUNCTION Real3
283 operator+(const Real left, const Real3 right)
284 {
285  return {left + right.v[0], left + right.v[1], left + right.v[2]};
286 }
287 KOKKOS_INLINE_FUNCTION Real3
288 operator+(const Real3 left, const Real right)
289 {
290  return {left.v[0] + right, left.v[1] + right, left.v[2] + right};
291 }
292 KOKKOS_INLINE_FUNCTION Real3
293 operator+(const Real3 left, const Real3 right)
294 {
295  return {left.v[0] + right.v[0], left.v[1] + right.v[1], left.v[2] + right.v[2]};
296 }
297 KOKKOS_INLINE_FUNCTION Real3
298 operator-(const Real left, const Real3 right)
299 {
300  return {left - right.v[0], left - right.v[1], left - right.v[2]};
301 }
302 KOKKOS_INLINE_FUNCTION Real3
303 operator-(const Real3 left, const Real right)
304 {
305  return {left.v[0] - right, left.v[1] - right, left.v[2] - right};
306 }
307 KOKKOS_INLINE_FUNCTION Real3
308 operator-(const Real3 left, const Real3 right)
309 {
310  return {left.v[0] - right.v[0], left.v[1] - right.v[1], left.v[2] - right.v[2]};
311 }
312 #endif
313 
314 template <typename T1, typename T2>
315 struct Pair
316 {
317  T1 first;
318  T2 second;
319 
320  template <typename T3, typename T4>
321  auto & operator=(const std::pair<T3, T4> pair)
322  {
323  first = pair.first;
324  second = pair.second;
325 
326  return *this;
327  }
328 };
329 
330 template <typename T1, typename T2>
331 bool
332 operator<(const Pair<T1, T2> & left, const Pair<T1, T2> & right)
333 {
334  return std::make_pair(left.first, left.second) < std::make_pair(right.first, right.second);
335 }
336 
337 } // namespace Kokkos
338 } // namespace Moose
KOKKOS_INLINE_FUNCTION Real3 operator*(const Real left, const Real3 right)
Definition: KokkosTypes.h:249
KOKKOS_INLINE_FUNCTION Real3(const Real &x, const Real &y, const Real &z)
Definition: KokkosTypes.h:149
KOKKOS_INLINE_FUNCTION Real norm()
Definition: KokkosTypes.h:219
KOKKOS_INLINE_FUNCTION Real3 operator+(const Real left, const Real3 right)
Definition: KokkosTypes.h:283
KOKKOS_INLINE_FUNCTION Real operator()(unsigned int i, unsigned int j) const
Definition: KokkosTypes.h:35
KOKKOS_INLINE_FUNCTION void identity(const unsigned int dim=3)
Definition: KokkosTypes.h:58
KOKKOS_INLINE_FUNCTION Real3(const Real &scalar)
Definition: KokkosTypes.h:137
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:224
KOKKOS_INLINE_FUNCTION void operator+=(const Real scalar)
Definition: KokkosTypes.h:189
KOKKOS_INLINE_FUNCTION Real dot_product(const Real3 vector)
Definition: KokkosTypes.h:220
Real3 & operator=(const libMesh::TypeVector< Real > &vector)
Definition: KokkosTypes.h:181
auto & operator=(const std::pair< T3, T4 > pair)
Definition: KokkosTypes.h:321
KOKKOS_INLINE_FUNCTION Real33 cartesian_product(const Real3 vector)
Definition: KokkosTypes.h:234
KOKKOS_INLINE_FUNCTION Real operator()(unsigned int i) const
Definition: KokkosTypes.h:163
KOKKOS_INLINE_FUNCTION Real3 & operator=(const Real3 &vector)
Definition: KokkosTypes.h:165
KOKKOS_INLINE_FUNCTION void operator+=(const Real33 tensor)
Definition: KokkosTypes.h:52
KOKKOS_INLINE_FUNCTION void operator-=(const Real3 vector)
Definition: KokkosTypes.h:207
KOKKOS_INLINE_FUNCTION Real33 transpose()
Definition: KokkosTypes.h:113
KOKKOS_INLINE_FUNCTION Real3(const Real3 &vector)
Definition: KokkosTypes.h:143
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:298
Real3(const libMesh::TypeVector< Real > &vector)
Definition: KokkosTypes.h:155
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
KOKKOS_INLINE_FUNCTION Real determinant(const unsigned int dim=3)
Definition: KokkosTypes.h:65
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:173
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:131
KOKKOS_INLINE_FUNCTION void operator*=(const Real scalar)
Definition: KokkosTypes.h:213
KOKKOS_INLINE_FUNCTION void operator-=(const Real scalar)
Definition: KokkosTypes.h:201
KOKKOS_INLINE_FUNCTION Real & operator()(unsigned int i)
Definition: KokkosTypes.h:162
KOKKOS_INLINE_FUNCTION void operator+=(const Real3 vector)
Definition: KokkosTypes.h:195
KOKKOS_INLINE_FUNCTION Real33 inverse(const unsigned int dim=3)
Definition: KokkosTypes.h:82
KOKKOS_INLINE_FUNCTION Real33(const Real &scalar)
Definition: KokkosTypes.h:32