https://mooseframework.inl.gov
KokkosFESystem.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 "KokkosSystem.h"
13 #include "KokkosAssembly.h"
14 
15 class MooseMesh;
16 class SystemBase;
17 
18 namespace Moose::Kokkos
19 {
20 
21 class NodalBCBase;
22 
27 class FESystem : public System, public AssemblyHolder
28 {
29 public:
34  FESystem(SystemBase & system);
35 
41  FESystem(System & base, SystemBase & system);
42 
43 #ifdef MOOSE_KOKKOS_SCOPE
44 
49  void reinit();
50 
56  KOKKOS_FUNCTION const auto & getCoupling(unsigned int var) const { return _coupling[var]; }
57 
64  KOKKOS_FUNCTION bool hasNodalBCMatrixTag(dof_id_type dof, TagID tag) const
65  {
66  return _nbc_matrix_tag_dof[tag].isAlloc() && _nbc_matrix_tag_dof[tag][dof];
67  }
68 
74  KOKKOS_FUNCTION unsigned int getFETypeID(unsigned int var) const { return _var_fe_types[var]; }
75 
84  unsigned int i,
85  unsigned int var) const
86  {
87  return _local_node_dof_index[var][node] + i;
88  }
89 
98  unsigned int i,
99  unsigned int var) const
100  {
101  return _local_to_global_dof_index[getNodeLocalDofIndex(node, i, var)];
102  }
103 
110  KOKKOS_FUNCTION bool isNodalDefined(ContiguousNodeID node, unsigned int var) const
111  {
113  }
114 
122  KOKKOS_FUNCTION ADReal getVectorDofADValue(const dof_id_type dof,
123  const TagID tag,
124  const Real seed) const;
133  KOKKOS_FUNCTION Real & getVectorQpValue(const ElementInfo info,
134  const dof_id_type qp,
135  const unsigned int var,
136  const TagID tag) const
137  {
138  return _qp_solutions[tag](info.subdomain, var)[qp];
139  }
151  KOKKOS_FUNCTION ADReal getVectorQpADValue(const ElementInfo info,
152  const dof_id_type offset,
153  const dof_id_type qp,
154  const unsigned int var,
155  const TagID tag,
156  const Real seed) const;
165  KOKKOS_FUNCTION Real3 & getVectorQpGrad(const ElementInfo info,
166  const dof_id_type qp,
167  const unsigned int var,
168  const TagID tag) const
169  {
170  return _qp_solutions_grad[tag](info.subdomain, var)[qp];
171  }
180  KOKKOS_FUNCTION Real3 & getVectorQpVectorValue(const ElementInfo info,
181  const dof_id_type qp,
182  const unsigned int var,
183  const TagID tag) const
184  {
185  return _qp_vector_solutions[tag](info.subdomain, var)[qp];
186  }
195  KOKKOS_FUNCTION Real33 & getVectorQpVectorGrad(const ElementInfo info,
196  const dof_id_type qp,
197  const unsigned int var,
198  const TagID tag) const
199  {
200  return _qp_vector_solutions_grad[tag](info.subdomain, var)[qp];
201  }
210  KOKKOS_FUNCTION Real3 & getVectorQpVectorCurl(const ElementInfo info,
211  const dof_id_type qp,
212  const unsigned int var,
213  const TagID tag) const
214  {
215  return _qp_vector_solutions_curl[tag](info.subdomain, var)[qp];
216  }
229  KOKKOS_FUNCTION ADReal3 getVectorQpADGrad(const ElementInfo info,
230  const Real33 jacobian,
231  const dof_id_type offset,
232  const dof_id_type qp,
233  const unsigned int var,
234  const TagID tag,
235  const Real seed) const;
245  KOKKOS_FUNCTION Real getVectorQpValueFace(const ElementInfo info,
246  const unsigned int side,
247  const unsigned int qp,
248  const unsigned int var,
249  const TagID tag) const;
259  KOKKOS_FUNCTION Real3 getVectorQpVectorValueFace(const ElementInfo info,
260  const unsigned int side,
261  const unsigned int qp,
262  const unsigned int var,
263  const TagID tag) const;
275  KOKKOS_FUNCTION ADReal getVectorQpADValueFace(const ElementInfo info,
276  const unsigned int side,
277  const unsigned int qp,
278  const unsigned int var,
279  const TagID tag,
280  const Real seed) const;
291  KOKKOS_FUNCTION Real3 getVectorQpGradFace(const ElementInfo info,
292  const unsigned int side,
293  const Real33 jacobian,
294  const unsigned int qp,
295  const unsigned int var,
296  const TagID tag) const;
307  KOKKOS_FUNCTION Real33 getVectorQpVectorGradFace(const ElementInfo info,
308  const unsigned int side,
309  const Real33 jacobian,
310  const unsigned int qp,
311  const unsigned int var,
312  const TagID tag) const;
313 
326  KOKKOS_FUNCTION ADReal3 getVectorQpADGradFace(const ElementInfo info,
327  const unsigned int side,
328  const Real33 jacobian,
329  const unsigned int qp,
330  const unsigned int var,
331  const TagID tag,
332  const Real seed) const;
333 
337  KOKKOS_FUNCTION void operator()(const ThreadID tid) const;
338 #endif
339 
340 private:
344  void setupVariables();
345 
349  void setupDofs();
350 
354  void setupCoupling();
355 
359  void setupNodalBCDofs();
360 
366  void getNodalBCDofs(const NodalBCBase * nbc, Array<bool> & dofs);
367 
372 
383 
388 
393 
398 
403 
408 };
409 
410 #ifdef MOOSE_KOKKOS_SCOPE
411 
412 KOKKOS_FUNCTION inline ADReal
413 FESystem::getVectorDofADValue(const dof_id_type dof, TagID tag, const Real seed) const
414 {
415  ADReal value = _vectors[tag][dof];
416 
417  if (seed != 0)
418  value.derivatives().insert(_local_to_global_dof_index[dof]) = seed;
419 
420  return value;
421 }
422 
423 KOKKOS_FUNCTION inline ADReal
425  const dof_id_type offset,
426  const dof_id_type qp,
427  const unsigned int var,
428  const TagID tag,
429  const Real seed) const
430 {
431  ADReal value = 0;
432 
433  if (seed == 0)
434  value = getVectorQpValue(info, offset + qp, var, tag);
435  else
436  {
437  auto fe = _var_fe_types[var];
438  auto n_dofs = kokkosAssembly().getNumDofs(info.type, fe);
439  auto & phi = kokkosAssembly().getPhi(info.subdomain, info.type, fe);
440 
441  for (unsigned int i = 0; i < n_dofs; ++i)
442  value += getVectorDofADValue(getElemLocalDofIndex(info.id, i, var), tag, seed) * phi(i, qp);
443  }
444 
445  return value;
446 }
447 
448 KOKKOS_FUNCTION inline ADReal3
450  const Real33 jacobian,
451  const dof_id_type offset,
452  const dof_id_type qp,
453  const unsigned int var,
454  const TagID tag,
455  const Real seed) const
456 {
457  ADReal3 grad;
458 
459  if (seed == 0)
460  grad = getVectorQpGrad(info, offset + qp, var, tag);
461  else
462  {
463  auto fe = _var_fe_types[var];
464  auto n_dofs = kokkosAssembly().getNumDofs(info.type, fe);
465  auto & grad_phi = kokkosAssembly().getGradPhi(info.subdomain, info.type, fe);
466 
467  for (unsigned int i = 0; i < n_dofs; ++i)
468  grad +=
469  getVectorDofADValue(getElemLocalDofIndex(info.id, i, var), tag, seed) * grad_phi(i, qp);
470 
471  grad = jacobian * grad;
472  }
473 
474  return grad;
475 }
476 
477 KOKKOS_FUNCTION inline Real
479  const unsigned int side,
480  const unsigned int qp,
481  const unsigned int var,
482  const TagID tag) const
483 {
484  auto fe = _var_fe_types[var];
485  auto n_dofs = kokkosAssembly().getNumDofs(info.type, fe);
486  auto & phi = kokkosAssembly().getPhiFace(info.subdomain, info.type, fe)(side);
487 
488  Real value = 0;
489 
490  for (unsigned int i = 0; i < n_dofs; ++i)
491  value += getVectorDofValue(getElemLocalDofIndex(info.id, i, var), tag) * phi(i, qp);
492 
493  return value;
494 }
495 
496 KOKKOS_FUNCTION inline Real3
498  const unsigned int side,
499  const unsigned int qp,
500  const unsigned int var,
501  const TagID tag) const
502 {
503  auto fe = _var_fe_types[var];
504  auto n_dofs = kokkosAssembly().getNumDofs(info.type, fe);
505  auto & phi = kokkosAssembly().getVectorPhiFace(info.subdomain, info.type, fe)(side);
506 
507  Real3 value = 0;
508 
509  for (unsigned int i = 0; i < n_dofs; ++i)
510  value += getVectorDofValue(getElemLocalDofIndex(info.id, i, var), tag) * phi(i, qp);
511 
512  return value;
513 }
514 
515 KOKKOS_FUNCTION inline ADReal
517  const unsigned int side,
518  const unsigned int qp,
519  const unsigned int var,
520  const TagID tag,
521  const Real seed) const
522 {
523  auto fe = _var_fe_types[var];
524  auto n_dofs = kokkosAssembly().getNumDofs(info.type, fe);
525  auto & phi = kokkosAssembly().getPhiFace(info.subdomain, info.type, fe)(side);
526 
527  ADReal value = 0;
528 
529  for (unsigned int i = 0; i < n_dofs; ++i)
530  value += getVectorDofADValue(getElemLocalDofIndex(info.id, i, var), tag, seed) * phi(i, qp);
531 
532  return value;
533 }
534 
535 KOKKOS_FUNCTION inline Real3
537  const unsigned int side,
538  const Real33 jacobian,
539  const unsigned int qp,
540  const unsigned int var,
541  const TagID tag) const
542 {
543  auto fe = _var_fe_types[var];
544  auto n_dofs = kokkosAssembly().getNumDofs(info.type, fe);
545  auto & grad_phi = kokkosAssembly().getGradPhiFace(info.subdomain, info.type, fe)(side);
546 
547  Real3 grad = 0;
548 
549  for (unsigned int i = 0; i < n_dofs; ++i)
550  grad += getVectorDofValue(getElemLocalDofIndex(info.id, i, var), tag) * grad_phi(i, qp);
551 
552  grad = jacobian * grad;
553 
554  return grad;
555 }
556 
557 KOKKOS_FUNCTION inline Real33
559  const unsigned int side,
560  const Real33 jacobian,
561  const unsigned int qp,
562  const unsigned int var,
563  const TagID tag) const
564 {
565  auto fe = _var_fe_types[var];
566  auto n_dofs = kokkosAssembly().getNumDofs(info.type, fe);
567  auto & grad_phi = kokkosAssembly().getVectorGradPhiFace(info.subdomain, info.type, fe)(side);
568 
569  Real33 grad = 0;
570 
571  for (unsigned int i = 0; i < n_dofs; ++i)
572  grad += getVectorDofValue(getElemLocalDofIndex(info.id, i, var), tag) * grad_phi(i, qp);
573 
574  grad = grad * jacobian.transpose();
575 
576  return grad;
577 }
578 
579 KOKKOS_FUNCTION inline ADReal3
581  const unsigned int side,
582  const Real33 jacobian,
583  const unsigned int qp,
584  const unsigned int var,
585  const TagID tag,
586  const Real seed) const
587 {
588  auto fe = _var_fe_types[var];
589  auto n_dofs = kokkosAssembly().getNumDofs(info.type, fe);
590  auto & grad_phi = kokkosAssembly().getGradPhiFace(info.subdomain, info.type, fe)(side);
591 
592  ADReal3 grad = ADReal(0);
593 
594  for (unsigned int i = 0; i < n_dofs; ++i)
595  grad += getVectorDofADValue(getElemLocalDofIndex(info.id, i, var), tag, seed) * grad_phi(i, qp);
596 
597  grad = jacobian * grad;
598 
599  return grad;
600 }
601 #endif
602 
604 } // 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.
Array< Array2D< Array< Real3 > > > _qp_vector_solutions_curl
void reinit()
Allocate the quadrature point vectors for active variable and tags and cache quadrature point values...
KOKKOS_FUNCTION Real3 & getVectorQpVectorCurl(const ElementInfo info, const dof_id_type qp, const unsigned int var, const TagID tag) const
Get the quadrature point curl of a vector variable from a tagged vector.
KOKKOS_FUNCTION dof_id_type getElemLocalDofIndex(ContiguousElementID elem, unsigned int i, unsigned int var) const
Get the local DOF index of a variable for an element.
Definition: KokkosSystem.h:207
void setupDofs()
Setup DOF data.
The Kokkos object that contains the information of an element The IDs used in Kokkos are different fr...
Definition: KokkosMesh.h:33
Array< Array< bool > > _nbc_matrix_tag_dof
Per-matrix-tag local-plus-ghost DOF masks for nodal BC coverage.
Array< dof_id_type > _local_to_global_dof_index
Map from local DOF index to global DOF index.
Definition: KokkosSystem.h:329
KOKKOS_FUNCTION const Assembly & kokkosAssembly() const
Get the const reference of the Kokkos assembly.
unsigned int TagID
Definition: MooseTypes.h:238
MPI_Info info
void setupCoupling()
Setup coupling data between variables.
KOKKOS_FUNCTION Real3 & getVectorQpGrad(const ElementInfo info, const dof_id_type qp, const unsigned int var, const TagID tag) const
Get the quadrature point gradient of a variable from a tagged vector.
KOKKOS_FUNCTION Real getVectorQpValueFace(const ElementInfo info, const unsigned int side, const unsigned int qp, const unsigned int var, const TagID tag) const
Get the face quadrature point value of a variable from a tagged vector.
KOKKOS_FUNCTION const auto & getVectorPhiFace(ContiguousSubdomainID subdomain, unsigned int elem_type, unsigned int fe_type) const
Get the face vector shape functions of a FE type for an element type and subdomain.
KOKKOS_FUNCTION unsigned int getFETypeID(unsigned int var) const
Get the FE type ID of a variable.
KOKKOS_FUNCTION Real3 getVectorQpGradFace(const ElementInfo info, const unsigned int side, const Real33 jacobian, const unsigned int qp, const unsigned int var, const TagID tag) const
Get the face quadrature point gradient of a variable from a tagged vector.
KOKKOS_FUNCTION ADReal3 getVectorQpADGrad(const ElementInfo info, const Real33 jacobian, const dof_id_type offset, const dof_id_type qp, const unsigned int var, const TagID tag, const Real seed) const
Get the quadrature point gradient of a variable from a tagged vector for automatic differentiation (A...
KOKKOS_FUNCTION ADReal3 getVectorQpADGradFace(const ElementInfo info, const unsigned int side, const Real33 jacobian, const unsigned int qp, const unsigned int var, const TagID tag, const Real seed) const
Get the face quadrature point gradient of a variable from a tagged vector for automatic differentiati...
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 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.
KOKKOS_FUNCTION unsigned int getNumDofs(unsigned int elem_type, unsigned int fe_type) const
Get the number of DOFs of a FE type for an element type.
Array< Array2D< Array< Real > > > _qp_solutions
Cached elemental quadrature values and gradients.
Base class for a system (of equations)
Definition: SystemBase.h:85
The Kokkos base system class.
Definition: KokkosSystem.h:30
KOKKOS_FUNCTION const auto & getCoupling(unsigned int var) const
Get the list of off-diagonal coupled variable numbers of a variable.
KOKKOS_FUNCTION Real & getVectorDofValue(const dof_id_type dof, const TagID tag) const
Get the DOF value of a tagged vector.
Definition: KokkosSystem.h:258
DualNumber< Real, DNDerivativeType, false > ADReal
Definition: KokkosADReal.h:28
void setupNodalBCDofs()
Mark the DOFs covered by nodal BCs.
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.
void getNodalBCDofs(const NodalBCBase *nbc, Array< bool > &dofs)
Get the list of DOFs covered by a nodal BC.
void setupVariables()
Setup variable data.
KOKKOS_FUNCTION Real3 & getVectorQpVectorValue(const ElementInfo info, const dof_id_type qp, const unsigned int var, const TagID tag) const
Get the quadrature point value of a vector variable from a tagged vector.
Array< Vector > _vectors
Kokkos vectors and matrices on device.
Definition: KokkosSystem.h:317
MOOSE_KOKKOS_INDEX_TYPE ThreadID
Definition: KokkosThread.h:22
KOKKOS_FUNCTION Real & getVectorQpValue(const ElementInfo info, const dof_id_type qp, const unsigned int var, const TagID tag) const
Get the quadrature point value of a variable from a tagged vector.
Array< bool > _var_is_vector
Whether each variable is vector-valued.
KOKKOS_FUNCTION const auto & getVectorGradPhiFace(ContiguousSubdomainID subdomain, unsigned int elem_type, unsigned int fe_type) const
Get the gradient of face vector shape functions of a FE type for an element type and subdomain...
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
static constexpr dof_id_type invalid_id
dof_id_type ContiguousNodeID
Definition: KokkosMesh.h:21
MooseMesh wraps a libMesh::Mesh object and enhances its capabilities by caching additional data and s...
Definition: MooseMesh.h:94
The Kokkos interface that holds the host reference of the Kokkos assembly and copies it to device dur...
The Kokkos FE system class.
KOKKOS_INLINE_FUNCTION Real33 transpose() const
Definition: KokkosTypes.h:448
KOKKOS_FUNCTION dof_id_type getNodeGlobalDofIndex(ContiguousNodeID node, unsigned int i, unsigned int var) const
Get the global DOF index of a variable for a node.
KOKKOS_FUNCTION Real33 & getVectorQpVectorGrad(const ElementInfo info, const dof_id_type qp, const unsigned int var, const TagID tag) const
Get the quadrature point gradient of a vector variable from a tagged vector.
The base class for Kokkos nodal boundary conditions.
Base class for deriving any boundary condition that works at nodes.
Definition: NodalBCBase.h:26
KOKKOS_FUNCTION ADReal getVectorDofADValue(const dof_id_type dof, const TagID tag, const Real seed) const
Get the DOF value of a tagged vector for automatic differentiation (AD)
Array< Array< unsigned int > > _coupling
Off-diagonal coupled variable numbers of each variable.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
KOKKOS_FUNCTION ADReal getVectorQpADValueFace(const ElementInfo info, const unsigned int side, const unsigned int qp, const unsigned int var, const TagID tag, const Real seed) const
Get the face quadrature point value of a variable from a tagged vector for automatic differentiation ...
MakeSystemHolder(FESystem)
Array< Array2D< Array< Real3 > > > _qp_solutions_grad
Thread _thread
Kokkos thread object.
Array< unsigned int > _var_fe_types
FE type ID of each variable.
Array< Array< dof_id_type > > _local_node_dof_index
Local nodal DOF indices of each variable.
KOKKOS_FUNCTION Real3 getVectorQpVectorValueFace(const ElementInfo info, const unsigned int side, const unsigned int qp, const unsigned int var, const TagID tag) const
Get the face quadrature point value of a vector variable from a tagged vector.
Array< Array2D< Array< Real33 > > > _qp_vector_solutions_grad
KOKKOS_FUNCTION ADReal getVectorQpADValue(const ElementInfo info, const dof_id_type offset, const dof_id_type qp, const unsigned int var, const TagID tag, const Real seed) const
Get the quadrature point value of a variable from a tagged vector for automatic differentiation (AD) ...
KOKKOS_FUNCTION bool hasNodalBCMatrixTag(dof_id_type dof, TagID tag) const
Check whether a local DOF index is associated with a nodal BC for an extra matrix tag...
KOKKOS_FUNCTION bool isNodalDefined(ContiguousNodeID node, unsigned int var) const
Get whether a variable is defined on a node.
FESystem(SystemBase &system)
Constructor for standalone use (pure FE simulations)
KOKKOS_FUNCTION void operator()(const ThreadID tid) const
Kokkos function for caching variable values on element quadrature points.
The Kokkos thread object that aids in converting the one-dimensional thread index into multi-dimensio...
Definition: KokkosThread.h:29
KOKKOS_FUNCTION dof_id_type getNodeLocalDofIndex(ContiguousNodeID node, unsigned int i, unsigned int var) const
Get the local DOF index of a variable for a node.
KOKKOS_FUNCTION Real33 getVectorQpVectorGradFace(const ElementInfo info, const unsigned int side, const Real33 jacobian, const unsigned int qp, const unsigned int var, const TagID tag) const
Get the face quadrature point gradient of a vector variable from a tagged vector. ...
uint8_t dof_id_type
Array< Array2D< Array< Real3 > > > _qp_vector_solutions