https://mooseframework.inl.gov
KokkosVariableValue.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 "KokkosDatum.h"
13 
14 #include "MooseVariableFieldBase.h"
15 
16 namespace Moose::Kokkos
17 {
18 
22 class VariablePhiValue
24 {
25 public:
33  KOKKOS_FUNCTION Real operator()(AssemblyDatum & datum, unsigned int i, unsigned int qp) const
34  {
35  auto & elem = datum.elem();
36  auto side = datum.side();
37  auto fe = datum.jfe();
38 
39  return side == libMesh::invalid_uint
40  ? datum.assembly().getPhi(elem.subdomain, elem.type, fe)(i, qp)
41  : datum.assembly().getPhiFace(elem.subdomain, elem.type, fe)(side)(i, qp);
42  }
43 };
44 
46 {
47 public:
55  KOKKOS_FUNCTION Real3 operator()(AssemblyDatum & datum, unsigned int i, unsigned int qp) const
56  {
57  auto & elem = datum.elem();
58  auto side = datum.side();
59  auto fe = datum.jfe();
60 
61  return datum.J(qp) *
62  (side == libMesh::invalid_uint
63  ? datum.assembly().getGradPhi(elem.subdomain, elem.type, fe)(i, qp)
64  : datum.assembly().getGradPhiFace(elem.subdomain, elem.type, fe)(side)(i, qp));
65  }
66 };
67 
69 {
70 public:
78  KOKKOS_FUNCTION Real operator()(AssemblyDatum & datum, unsigned int i, unsigned int qp) const
79  {
80  auto & elem = datum.elem();
81  auto side = datum.side();
82  auto fe = datum.ife();
83 
84  return side == libMesh::invalid_uint
85  ? datum.assembly().getPhi(elem.subdomain, elem.type, fe)(i, qp)
86  : datum.assembly().getPhiFace(elem.subdomain, elem.type, fe)(side)(i, qp);
87  }
88 };
89 
91 {
92 public:
100  KOKKOS_FUNCTION Real3 operator()(AssemblyDatum & datum, unsigned int i, unsigned int qp) const
101  {
102  auto & elem = datum.elem();
103  auto side = datum.side();
104  auto fe = datum.ife();
105 
106  return datum.J(qp) *
107  (side == libMesh::invalid_uint
108  ? datum.assembly().getGradPhi(elem.subdomain, elem.type, fe)(i, qp)
109  : datum.assembly().getGradPhiFace(elem.subdomain, elem.type, fe)(side)(i, qp));
110  }
111 };
112 
117 
119 
123 template <bool is_ad>
126 {
127  using real_type = std::conditional_t<is_ad, ADReal, Real>;
128 
129 public:
133  VariableValueTempl() = default;
139  VariableValueTempl(Variable var, bool dof = false) : _var(var), _dof(dof) {}
147  const TagName & tag = Moose::SOLUTION_TAG,
148  bool dof = false)
149  : _var(var, tag), _dof(dof)
150  {
151  }
158  VariableValueTempl(const std::vector<const MooseVariableFieldBase *> & vars,
160  const TagName & tag = Moose::SOLUTION_TAG,
161  bool dof = false)
162  : _var(vars, tag), _dof(dof)
163  {
164  }
165  VariableValueTempl(const std::vector<MooseVariableFieldBase *> & vars,
166  const TagName & tag = Moose::SOLUTION_TAG,
167  bool dof = false)
168  : _var(vars, tag), _dof(dof)
169  {
170  }
172 
181 
186  KOKKOS_FUNCTION operator bool() const { return _var.coupled(); }
187 
195  KOKKOS_FUNCTION auto operator()(Datum & datum, unsigned int idx, unsigned int comp = 0) const
196  {
197  return get(datum, idx, comp);
198  }
199 
207  KOKKOS_FUNCTION auto
208  operator()(AssemblyDatum & datum, unsigned int idx, unsigned int comp = 0) const;
209 
214  KOKKOS_FUNCTION const Variable & variable() const { return _var; }
215 
216 private:
225  KOKKOS_FUNCTION auto
226  get(Datum & datum, unsigned int idx, unsigned int comp = 0, Real seed = 0) const;
227 
239  bool _dof = false;
240 };
241 
242 template <bool is_ad>
244  : _var(object._var), _seed(object._seed), _dof(object._dof)
245 {
246  if constexpr (is_ad)
247  if (_var.coupled())
248  {
249  if (!_seed.isAlloc())
251 
252  for (unsigned int comp = 0; comp < _var.components(); ++comp)
253  _seed[comp] =
254  _var.dot() ? _var.mooseVar(comp)->sys().duDotDu(_var.var(comp)) : (_var.old() ? 0 : 1);
255 
257  }
258 }
259 
260 template <bool is_ad>
263 {
264  _var = object._var;
265  _dof = object._dof;
266 
267  return *this;
268 }
269 
270 template <bool is_ad>
271 KOKKOS_FUNCTION auto
273  unsigned int idx,
274  unsigned int comp) const
275 {
276  if constexpr (is_ad)
277  {
278  Real seed =
279  datum.do_derivatives() && _var.coupled() && _var.sys(comp) == datum.sys() ? _seed[comp] : 0;
280 
281  return get(datum, idx, comp, seed);
282  }
283  else
284  return get(datum, idx, comp);
285 }
286 
287 template <bool is_ad>
288 KOKKOS_FUNCTION auto
290  unsigned int idx,
291  unsigned int comp,
292  [[maybe_unused]] Real seed) const
293 {
294  KOKKOS_ASSERT(_var.initialized());
295 
297 
298  if (_var.coupled())
299  {
300  auto & sys = datum.system(_var.sys(comp));
301  auto var = _var.var(comp);
302  auto tag = _var.tag();
303 
304  if (_dof)
305  {
306  unsigned int dof;
307 
308  if (datum.isNodal())
309  {
310  auto node = datum.node();
311  dof = sys.getNodeLocalDofIndex(node, 0, var);
312  }
313  else
314  {
315  auto elem = datum.elem().id;
316  dof = sys.getElemLocalDofIndex(elem, idx, var);
317  }
318 
319  if constexpr (is_ad)
320  value = sys.getVectorDofADValue(dof, tag, seed);
321  else
322  value = sys.getVectorDofValue(dof, tag);
323  }
324  else
325  {
326  auto & elem = datum.elem();
327  auto side = datum.side();
328 
329  if constexpr (is_ad)
330  value = side == libMesh::invalid_uint
331  ? sys.getVectorQpADValue(elem, datum.qpOffset(), idx, var, tag, seed)
332  : sys.getVectorQpADValueFace(elem, side, idx, var, tag, seed);
333  else
334  value = side == libMesh::invalid_uint
335  ? sys.getVectorQpValue(elem, datum.qpOffset() + idx, var, tag)
336  : sys.getVectorQpValueFace(elem, side, idx, var, tag);
337  }
338  }
339  else
340  value = _var.value(comp);
341 
342  return value;
343 }
344 
345 template <bool is_ad>
347 {
348  using real3_type = std::conditional_t<is_ad, ADReal3, Real3>;
349 
350 public:
354  VariableGradientTempl() = default;
366  const TagName & tag = Moose::SOLUTION_TAG)
367  : _var(var, tag)
368  {
369  }
375  VariableGradientTempl(const std::vector<const MooseVariableFieldBase *> & vars,
377  const TagName & tag = Moose::SOLUTION_TAG)
378  : _var(vars, tag)
379  {
380  }
381  VariableGradientTempl(const std::vector<MooseVariableFieldBase *> & vars,
382  const TagName & tag = Moose::SOLUTION_TAG)
383  : _var(vars, tag)
384  {
385  }
387 
396 
401  KOKKOS_FUNCTION operator bool() const { return _var.coupled(); }
402 
410  KOKKOS_FUNCTION auto operator()(Datum & datum, unsigned int qp, unsigned int comp = 0) const
411  {
412  return get(datum, qp, comp);
413  }
414 
422  KOKKOS_FUNCTION auto
423  operator()(AssemblyDatum & datum, unsigned int qp, unsigned int comp = 0) const;
424 
429  KOKKOS_FUNCTION const Variable & variable() const { return _var; }
430 
431 private:
440  KOKKOS_FUNCTION auto
441  get(Datum & datum, unsigned int qp, unsigned int comp = 0, Real seed = 0) const;
442 
451 };
452 
453 template <bool is_ad>
455  : _var(object._var), _seed(object._seed)
456 {
457  if constexpr (is_ad)
458  if (_var.coupled())
459  {
460  if (!_seed.isAlloc())
462 
463  for (unsigned int comp = 0; comp < _var.components(); ++comp)
464  _seed[comp] =
465  _var.dot() ? _var.mooseVar(comp)->sys().duDotDu(_var.var(comp)) : (_var.old() ? 0 : 1);
466 
468  }
469 }
470 
471 template <bool is_ad>
474 {
475  _var = object._var;
476 
477  return *this;
478 }
479 
480 template <bool is_ad>
481 KOKKOS_FUNCTION auto
483  unsigned int qp,
484  unsigned int comp) const
485 {
486  if constexpr (is_ad)
487  {
488  Real seed =
489  datum.do_derivatives() && _var.coupled() && _var.sys(comp) == datum.sys() ? _seed[comp] : 0;
490 
491  return get(datum, qp, comp, seed);
492  }
493  else
494  return get(datum, qp, comp);
495 }
496 
497 template <bool is_ad>
498 KOKKOS_FUNCTION auto
500  unsigned int qp,
501  unsigned int comp,
502  [[maybe_unused]] Real seed) const
503 {
504  KOKKOS_ASSERT(_var.initialized());
505 
506  real3_type grad;
507 
508  if (_var.coupled())
509  {
510  KOKKOS_ASSERT(!datum.isNodal());
511 
512  auto & elem = datum.elem();
513  auto side = datum.side();
514 
515  if constexpr (is_ad)
516  grad =
517  side == libMesh::invalid_uint
518  ? datum.system(_var.sys(comp))
519  .getVectorQpADGrad(
520  elem, datum.J(qp), datum.qpOffset(), qp, _var.var(comp), _var.tag(), seed)
521  : datum.system(_var.sys(comp))
522  .getVectorQpADGradFace(
523  elem, side, datum.J(qp), qp, _var.var(comp), _var.tag(), seed);
524  else
525  grad =
526  side == libMesh::invalid_uint
527  ? datum.system(_var.sys(comp))
528  .getVectorQpGrad(elem, datum.qpOffset() + qp, _var.var(comp), _var.tag())
529  : datum.system(_var.sys(comp))
530  .getVectorQpGradFace(elem, side, datum.J(qp), qp, _var.var(comp), _var.tag());
531  }
532 
533  return grad;
534 }
535 
540 
541 template <>
543 {
544  static constexpr bool value = true;
545 };
546 
547 template <>
549 {
550  static constexpr bool value = true;
551 };
553 
554 } // namespace Moose::Kokkos
KOKKOS_FUNCTION const auto & getPhi(ContiguousSubdomainID subdomain, unsigned int elem_type, unsigned int fe_type) const
Get the shape functions of a FE type for an element type and subdomain.
KOKKOS_FUNCTION bool old() const
Get whether the tag is old/older value.
KOKKOS_FUNCTION auto get(Datum &datum, unsigned int qp, unsigned int comp=0, Real seed=0) const
Get the current variable gradient.
The Kokkos object that holds thread-private data in the parallel operations of any Kokkos object...
Definition: KokkosDatum.h:23
bool _dof
Flag whether DOF values are requested.
const unsigned int invalid_uint
VariableGradientTempl()=default
Default constructor.
VariableGradientTempl(const std::vector< MooseVariableFieldBase *> &vars, const TagName &tag=Moose::SOLUTION_TAG)
Array< Real > _seed
Derivative seed of each component for AD.
VariableValueTempl(const std::vector< MooseVariableFieldBase *> &vars, const TagName &tag=Moose::SOLUTION_TAG, bool dof=false)
KOKKOS_FUNCTION auto operator()(Datum &datum, unsigned int idx, unsigned int comp=0) const
Get the current variable value.
Variable _var
Coupled Kokkos variable.
char ** vars
KOKKOS_FUNCTION void do_derivatives(const bool flag)
Set whether to compute derivatives for automatic differentiation (AD)
Definition: KokkosDatum.h:476
KOKKOS_FUNCTION unsigned int sys() const
Get the system number of variable.
Definition: KokkosDatum.h:441
OutputTools< Real >::VariablePhiValue VariablePhiValue
Definition: MooseTypes.h:348
KOKKOS_FUNCTION const auto & getGradPhiFace(ContiguousSubdomainID subdomain, unsigned int elem_type, unsigned int fe_type) const
Get the gradient of face shape functions of a FE type for an element type and subdomain.
KOKKOS_FUNCTION const Real33 & J(const unsigned int qp)
Get the inverse of Jacobian matrix | dxi/dx deta/dx dzeta/dx | | dxi/dy deta/dy dzeta/dy | | dxi/dz d...
Definition: KokkosDatum.h:300
KOKKOS_FUNCTION const auto & getGradPhi(ContiguousSubdomainID subdomain, unsigned int elem_type, unsigned int fe_type) const
Get the gradient of shape functions of a FE type for an element type and subdomain.
This class provides an interface for common operations on field variables of both FE and FV types wit...
KOKKOS_FUNCTION Real3 operator()(AssemblyDatum &datum, unsigned int i, unsigned int qp) const
Get the gradient of the current shape function.
The Kokkos wrapper classes for MOOSE-like shape function access.
KOKKOS_FUNCTION bool dot() const
Get whether the tag is time derivative.
KOKKOS_FUNCTION const auto & getPhiFace(ContiguousSubdomainID subdomain, unsigned int elem_type, unsigned int fe_type) const
Get the face shape functions of a FE type for an element type and subdomain.
Array< Real > _seed
Derivative seed of each component for AD.
KOKKOS_FUNCTION bool coupled() const
Get whether the variable is coupled.
std::conditional_t< is_ad, ADReal3, Real3 > real3_type
KOKKOS_FUNCTION bool isAlloc() const
Get whether the array was allocated either on host or device.
Definition: KokkosArray.h:180
KOKKOS_FUNCTION unsigned int side() const
Get the side index.
Definition: KokkosDatum.h:108
virtual const Number & duDotDu(unsigned int var_num=0) const
Definition: SystemBase.C:1690
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
KOKKOS_FUNCTION unsigned int jfe() const
Get the coupled variable FE type ID.
Definition: KokkosDatum.h:471
OutputTools< Real >::VariableTestValue VariableTestValue
Definition: MooseTypes.h:353
VariableGradientTempl< is_ad > & operator=(const VariableGradientTempl< is_ad > &object)
Copy assignment operator.
void create(const std::vector< index_type > &n)
Allocate array on host and device.
Definition: KokkosArray.h:303
KOKKOS_FUNCTION const Assembly & assembly() const
Get the Kokkos assembly.
Definition: KokkosDatum.h:67
VariableValueTempl()=default
Default constructor.
The Kokkos wrapper classes for MOOSE-like variable value access.
void copyToDevice()
Copy data from host to device.
Definition: KokkosArray.h:947
KOKKOS_FUNCTION unsigned int components()
Get the number of components.
std::conditional_t< is_ad, ADReal, Real > real_type
KOKKOS_FUNCTION Real operator()(AssemblyDatum &datum, unsigned int i, unsigned int qp) const
Get the current shape function.
KOKKOS_FUNCTION Real3 operator()(AssemblyDatum &datum, unsigned int i, unsigned int qp) const
Get the gradient of the current test function.
The type trait that determines the default behavior of copy constructor and deepCopy() If this type t...
Definition: KokkosArray.h:90
KOKKOS_FUNCTION const Variable & variable() const
Get the Kokkos variable.
OutputTools< Real >::VariablePhiGradient VariablePhiGradient
Definition: MooseTypes.h:349
KOKKOS_FUNCTION ContiguousNodeID node() const
Get the contiguous node ID.
Definition: KokkosDatum.h:113
VariableValueTempl(const MooseVariableFieldBase &var, const TagName &tag=Moose::SOLUTION_TAG, bool dof=false)
Constructor.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
VariableValueTempl(Variable var, bool dof=false)
Constructor.
Variable _var
Coupled Kokkos variable.
VariableValueTempl< is_ad > & operator=(const VariableValueTempl< is_ad > &object)
Copy assignment operator.
KOKKOS_FUNCTION bool isNodal() const
Get whether the current datum is on a node.
Definition: KokkosDatum.h:146
The Kokkos variable object that carries the coupled variable and tag information. ...
KOKKOS_FUNCTION const ElementInfo & elem() const
Get the element information object.
Definition: KokkosDatum.h:84
const MooseVariableFieldBase * mooseVar(unsigned int comp=0)
Get the MOOSE variable of a component.
KOKKOS_FUNCTION unsigned int ife() const
Get the variable FE type ID.
Definition: KokkosDatum.h:466
KOKKOS_FUNCTION auto get(Datum &datum, unsigned int idx, unsigned int comp=0, Real seed=0) const
Get the current variable value.
VariableGradientTempl(Variable var)
Constructor.
The Kokkos object that holds thread-private data in the parallel operations of Kokkos kernels...
Definition: KokkosDatum.h:364
const TagName SOLUTION_TAG
Definition: MooseTypes.C:25
KOKKOS_FUNCTION const Variable & variable() const
Get the Kokkos variable.
KOKKOS_FUNCTION unsigned int var(unsigned int comp=0) const
Get the variable number of a component.
OutputTools< Real >::VariableTestGradient VariableTestGradient
Definition: MooseTypes.h:354
KOKKOS_FUNCTION const System & system(unsigned int sys) const
Get the Kokkos system.
Definition: KokkosDatum.h:73
KOKKOS_FUNCTION dof_id_type qpOffset() const
Get the starting offset into the global quadrature point index.
Definition: KokkosDatum.h:123
SystemBase & sys()
Get the system this variable is part of.
KOKKOS_FUNCTION auto operator()(Datum &datum, unsigned int qp, unsigned int comp=0) const
Get the current variable gradient.
KOKKOS_FUNCTION Real operator()(AssemblyDatum &datum, unsigned int i, unsigned int qp) const
Get the current test function.
static constexpr bool value
Definition: KokkosArray.h:92
ContiguousElementID id
Contiguous element ID.
Definition: KokkosMesh.h:42
unsigned int idx(const ElemType type, const unsigned int nx, const unsigned int i, const unsigned int j)
VariableGradientTempl(const MooseVariableFieldBase &var, const TagName &tag=Moose::SOLUTION_TAG)
Constructor.