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 
96  KOKKOS_FUNCTION dof_id_type getNodeGlobalDofIndex(ContiguousNodeID node, unsigned int var) const
97  {
99  }
100 
107  KOKKOS_FUNCTION bool isNodalDefined(ContiguousNodeID node, unsigned int var) const
108  {
110  }
111 
119  KOKKOS_FUNCTION ADReal getVectorDofADValue(const dof_id_type dof,
120  const TagID tag,
121  const Real seed) const;
130  KOKKOS_FUNCTION Real & getVectorQpValue(const ElementInfo info,
131  const dof_id_type qp,
132  const unsigned int var,
133  const TagID tag) const
134  {
135  return _qp_solutions[tag](info.subdomain, var)[qp];
136  }
148  KOKKOS_FUNCTION ADReal getVectorQpADValue(const ElementInfo info,
149  const dof_id_type offset,
150  const dof_id_type qp,
151  const unsigned int var,
152  const TagID tag,
153  const Real seed) const;
162  KOKKOS_FUNCTION Real3 & getVectorQpGrad(const ElementInfo info,
163  const dof_id_type qp,
164  const unsigned int var,
165  const TagID tag) const
166  {
167  return _qp_solutions_grad[tag](info.subdomain, var)[qp];
168  }
181  KOKKOS_FUNCTION ADReal3 getVectorQpADGrad(const ElementInfo info,
182  const Real33 jacobian,
183  const dof_id_type offset,
184  const dof_id_type qp,
185  const unsigned int var,
186  const TagID tag,
187  const Real seed) const;
197  KOKKOS_FUNCTION Real getVectorQpValueFace(const ElementInfo info,
198  const unsigned int side,
199  const unsigned int qp,
200  const unsigned int var,
201  const TagID tag) const;
213  KOKKOS_FUNCTION ADReal getVectorQpADValueFace(const ElementInfo info,
214  const unsigned int side,
215  const unsigned int qp,
216  const unsigned int var,
217  const TagID tag,
218  const Real seed) const;
229  KOKKOS_FUNCTION Real3 getVectorQpGradFace(const ElementInfo info,
230  const unsigned int side,
231  const Real33 jacobian,
232  const unsigned int qp,
233  const unsigned int var,
234  const TagID tag) const;
235 
248  KOKKOS_FUNCTION ADReal3 getVectorQpADGradFace(const ElementInfo info,
249  const unsigned int side,
250  const Real33 jacobian,
251  const unsigned int qp,
252  const unsigned int var,
253  const TagID tag,
254  const Real seed) const;
255 
259  KOKKOS_FUNCTION void operator()(const ThreadID tid) const;
260 #endif
261 
262 private:
266  void setupVariables();
267 
271  void setupDofs();
272 
276  void setupCoupling();
277 
281  void setupNodalBCDofs();
282 
288  void getNodalBCDofs(const NodalBCBase * nbc, Array<bool> & dofs);
289 
294 
302 
307 
312 
317 
322 };
323 
324 #ifdef MOOSE_KOKKOS_SCOPE
325 
326 KOKKOS_FUNCTION inline ADReal
327 FESystem::getVectorDofADValue(const dof_id_type dof, TagID tag, const Real seed) const
328 {
329  ADReal value = _vectors[tag][dof];
330 
331  if (seed != 0)
332  value.derivatives().insert(_local_to_global_dof_index[dof]) = seed;
333 
334  return value;
335 }
336 
337 KOKKOS_FUNCTION inline ADReal
339  const dof_id_type offset,
340  const dof_id_type qp,
341  const unsigned int var,
342  const TagID tag,
343  const Real seed) const
344 {
345  ADReal value = 0;
346 
347  if (seed == 0)
348  value = getVectorQpValue(info, offset + qp, var, tag);
349  else
350  {
351  auto fe = _var_fe_types[var];
352  auto n_dofs = kokkosAssembly().getNumDofs(info.type, fe);
353  auto & phi = kokkosAssembly().getPhi(info.subdomain, info.type, fe);
354 
355  for (unsigned int i = 0; i < n_dofs; ++i)
356  value += getVectorDofADValue(getElemLocalDofIndex(info.id, i, var), tag, seed) * phi(i, qp);
357  }
358 
359  return value;
360 }
361 
362 KOKKOS_FUNCTION inline ADReal3
364  const Real33 jacobian,
365  const dof_id_type offset,
366  const dof_id_type qp,
367  const unsigned int var,
368  const TagID tag,
369  const Real seed) const
370 {
371  ADReal3 grad;
372 
373  if (seed == 0)
374  grad = getVectorQpGrad(info, offset + qp, var, tag);
375  else
376  {
377  auto fe = _var_fe_types[var];
378  auto n_dofs = kokkosAssembly().getNumDofs(info.type, fe);
379  auto & grad_phi = kokkosAssembly().getGradPhi(info.subdomain, info.type, fe);
380 
381  for (unsigned int i = 0; i < n_dofs; ++i)
382  grad += getVectorDofADValue(getElemLocalDofIndex(info.id, i, var), tag, seed) *
383  (jacobian * grad_phi(i, qp));
384  }
385 
386  return grad;
387 }
388 
389 KOKKOS_FUNCTION inline Real
391  const unsigned int side,
392  const unsigned int qp,
393  const unsigned int var,
394  const TagID tag) const
395 {
396  auto fe = _var_fe_types[var];
397  auto n_dofs = kokkosAssembly().getNumDofs(info.type, fe);
398  auto & phi = kokkosAssembly().getPhiFace(info.subdomain, info.type, fe)(side);
399 
400  Real value = 0;
401 
402  for (unsigned int i = 0; i < n_dofs; ++i)
403  value += getVectorDofValue(getElemLocalDofIndex(info.id, i, var), tag) * phi(i, qp);
404 
405  return value;
406 }
407 
408 KOKKOS_FUNCTION inline ADReal
410  const unsigned int side,
411  const unsigned int qp,
412  const unsigned int var,
413  const TagID tag,
414  const Real seed) const
415 {
416  auto fe = _var_fe_types[var];
417  auto n_dofs = kokkosAssembly().getNumDofs(info.type, fe);
418  auto & phi = kokkosAssembly().getPhiFace(info.subdomain, info.type, fe)(side);
419 
420  ADReal value = 0;
421 
422  for (unsigned int i = 0; i < n_dofs; ++i)
423  value += getVectorDofADValue(getElemLocalDofIndex(info.id, i, var), tag, seed) * phi(i, qp);
424 
425  return value;
426 }
427 
428 KOKKOS_FUNCTION inline Real3
430  const unsigned int side,
431  const Real33 jacobian,
432  const unsigned int qp,
433  const unsigned int var,
434  const TagID tag) const
435 {
436  auto fe = _var_fe_types[var];
437  auto n_dofs = kokkosAssembly().getNumDofs(info.type, fe);
438  auto & grad_phi = kokkosAssembly().getGradPhiFace(info.subdomain, info.type, fe)(side);
439 
440  Real3 grad = 0;
441 
442  for (unsigned int i = 0; i < n_dofs; ++i)
443  grad += getVectorDofValue(getElemLocalDofIndex(info.id, i, var), tag) *
444  (jacobian * grad_phi(i, qp));
445 
446  return grad;
447 }
448 
449 KOKKOS_FUNCTION inline ADReal3
451  const unsigned int side,
452  const Real33 jacobian,
453  const unsigned int qp,
454  const unsigned int var,
455  const TagID tag,
456  const Real seed) const
457 {
458  auto fe = _var_fe_types[var];
459  auto n_dofs = kokkosAssembly().getNumDofs(info.type, fe);
460  auto & grad_phi = kokkosAssembly().getGradPhiFace(info.subdomain, info.type, fe)(side);
461 
462  ADReal3 grad = ADReal(0);
463 
464  for (unsigned int i = 0; i < n_dofs; ++i)
465  grad += getVectorDofADValue(getElemLocalDofIndex(info.id, i, var), tag, seed) *
466  (jacobian * grad_phi(i, qp));
467 
468  return grad;
469 }
470 #endif
471 
473 } // 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.
void reinit()
Allocate the quadrature point vectors for active variable and tags and cache quadrature point values...
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 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.
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.
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:93
The Kokkos interface that holds the host reference of the Kokkos assembly and copies it to device dur...
The Kokkos FE system class.
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 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 dof_id_type getNodeGlobalDofIndex(ContiguousNodeID node, unsigned int var) const
Get the global DOF index of a variable for a node.
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.
uint8_t dof_id_type