https://mooseframework.inl.gov
KokkosAssembly.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 "KokkosTypes.h"
13 
14 #include "MooseMesh.h"
15 
16 #include "libmesh/elem_range.h"
17 #include "libmesh/fe_base.h"
18 #include "libmesh/fe_type.h"
19 
20 class FEProblemBase;
21 
22 namespace Moose
23 {
24 namespace Kokkos
25 {
26 
30 class Assembly : public MeshHolder
31 {
32 public:
37  Assembly(FEProblemBase & problem);
41  void init();
42 
43 #ifdef MOOSE_KOKKOS_SCOPE
44 
49  unsigned int getFETypeID(FEType type) const { return libmesh_map_find(_fe_type_map, type); }
54  KOKKOS_FUNCTION unsigned int getMaxQpsPerElem() const { return _max_qps_per_elem; }
60  KOKKOS_FUNCTION dof_id_type getNumQps(ContiguousSubdomainID subdomain) const
61  {
62  return _n_subdomain_qps[subdomain];
63  }
69  KOKKOS_FUNCTION unsigned int getNumQps(ElementInfo info) const { return _n_qps[info.id]; }
77  KOKKOS_FUNCTION dof_id_type getNumFaceQps(ContiguousSubdomainID subdomain) const
78  {
79  return _n_subdomain_qps_face[subdomain];
80  }
87  KOKKOS_FUNCTION unsigned int getNumFaceQps(ElementInfo info, unsigned int side) const
88  {
89  return _n_qps_face(side, info.id);
90  }
97  KOKKOS_FUNCTION dof_id_type getQpOffset(ElementInfo info) const { return _qp_offset[info.id]; }
105  KOKKOS_FUNCTION dof_id_type getQpFaceOffset(ElementInfo info, unsigned int side) const
106  {
107  return _qp_offset_face(side, info.id);
108  }
115  KOKKOS_FUNCTION unsigned int getNumDofs(unsigned int elem_type, unsigned int fe_type) const
116  {
117  return _n_dofs(elem_type, fe_type);
118  }
126  KOKKOS_FUNCTION const auto &
127  getPhi(ContiguousSubdomainID subdomain, unsigned int elem_type, unsigned int fe_type) const
128  {
129  return _phi(subdomain, elem_type, fe_type);
130  }
138  KOKKOS_FUNCTION const auto &
139  getPhiFace(ContiguousSubdomainID subdomain, unsigned int elem_type, unsigned int fe_type) const
140  {
141  return _phi_face(subdomain, elem_type, fe_type);
142  }
150  KOKKOS_FUNCTION const auto &
151  getGradPhi(ContiguousSubdomainID subdomain, unsigned int elem_type, unsigned int fe_type) const
152  {
153  return _grad_phi(subdomain, elem_type, fe_type);
154  }
162  KOKKOS_FUNCTION const auto & getGradPhiFace(ContiguousSubdomainID subdomain,
163  unsigned int elem_type,
164  unsigned int fe_type) const
165  {
166  return _grad_phi_face(subdomain, elem_type, fe_type);
167  }
174  KOKKOS_FUNCTION Real33 getJacobian(ElementInfo info, unsigned int qp) const
175  {
176  return _jacobian[info.subdomain][getQpOffset(info) + qp];
177  }
184  KOKKOS_FUNCTION Real getJxW(ElementInfo info, unsigned int qp) const
185  {
186  return _jxw[info.subdomain][getQpOffset(info) + qp];
187  }
194  KOKKOS_FUNCTION Real3 getQPoint(ElementInfo info, unsigned int qp) const
195  {
196  return _xyz[info.subdomain][getQpOffset(info) + qp];
197  }
198 
205  KOKKOS_FUNCTION Real coordTransformFactor(const ContiguousSubdomainID subdomain,
206  const Real3 point) const;
215  KOKKOS_FUNCTION void computePhysicalMap(const ElementInfo info,
216  const unsigned int qp,
217  Real33 * const jacobian,
218  Real * const JxW,
219  Real3 * const q_points) const;
229  KOKKOS_FUNCTION void computePhysicalMap(const ElementInfo info,
230  const unsigned int side,
231  const unsigned int qp,
232  Real33 * const jacobian,
233  Real * const JxW,
234  Real3 * const q_points) const;
235 
239  KOKKOS_FUNCTION void operator()(const ThreadID tid) const;
240 
245  const auto & getMaterialBoundaries() const { return _material_boundaries; }
246 #endif
247 
248 private:
252  void initQuadrature();
256  void initShape();
260  void cachePhysicalMap();
261 
273  std::map<FEType, unsigned int> _fe_type_map;
274 
278  const unsigned int _dimension;
291 
299 
305 
306  unsigned int _max_qps_per_elem = 0;
307 
311 
320 
330 
341 
349 
353  std::set<BoundaryID> _material_boundaries;
354 };
355 
356 #ifdef MOOSE_KOKKOS_SCOPE
357 KOKKOS_FUNCTION inline Real
359 {
360  switch (_coord_type[subdomain])
361  {
362  case Moose::COORD_XYZ:
363  return 1;
364  case Moose::COORD_RZ:
366  return 2 * M_PI *
367  (point - _rz_axis[subdomain].first).cross_product(_rz_axis[subdomain].second).norm();
368  else
369  return 2 * M_PI * point(_rz_radial_coord);
371  return 4 * M_PI * point(0) * point(0);
372  default:
373  return 0;
374  }
375 }
376 
377 KOKKOS_FUNCTION inline void
379  const unsigned int qp,
380  Real33 * const jacobian,
381  Real * const JxW,
382  Real3 * const q_points) const
383 {
384  auto sid = info.subdomain;
385  auto eid = info.id;
386  auto elem_type = info.type;
387  auto num_nodes = kokkosMesh().getNumNodes(elem_type);
388 
389  auto & phi = _map_phi(sid, elem_type);
390  auto & grad_phi = _map_grad_phi(sid, elem_type);
391 
392  Real33 J;
393  Real3 xyz;
394 
395  for (unsigned int node = 0; node < num_nodes; ++node)
396  {
397  auto points = kokkosMesh().getNodePoint(kokkosMesh().getContiguousNodeID(eid, node));
398 
399  if (jacobian || JxW)
400  J += grad_phi(node, qp).cartesian_product(points);
401 
402  xyz += phi(node, qp) * points;
403  }
404 
405  if (jacobian)
406  *jacobian = J.inverse(_dimension);
407  if (JxW)
408  *JxW =
409  J.determinant(_dimension) * _weights(sid, elem_type)[qp] * coordTransformFactor(sid, xyz);
410  if (q_points)
411  *q_points = xyz;
412 }
413 
414 KOKKOS_FUNCTION inline void
416  const unsigned int side,
417  const unsigned int qp,
418  Real33 * const jacobian,
419  Real * const JxW,
420  Real3 * const q_points) const
421 {
422  auto sid = info.subdomain;
423  auto eid = info.id;
424  auto elem_type = info.type;
425  auto num_nodes = kokkosMesh().getNumNodes(elem_type);
426  auto num_side_nodes = kokkosMesh().getNumNodes(elem_type, side);
427 
428  auto & phi = _map_phi_face(sid, elem_type)(side);
429  auto & grad_phi = _map_grad_phi_face(sid, elem_type)(side);
430 
431  Real33 J;
432  Real3 xyz;
433 
434  for (unsigned int node = 0; node < num_nodes; ++node)
435  {
436  auto points = kokkosMesh().getNodePoint(kokkosMesh().getContiguousNodeID(eid, node));
437 
438  if (jacobian)
439  J += grad_phi(node, qp).cartesian_product(points);
440 
441  if (JxW || q_points)
442  xyz += phi(node, qp) * points;
443  }
444 
445  if (jacobian)
446  *jacobian = J.inverse(_dimension);
447  if (q_points)
448  *q_points = xyz;
449 
450  if (JxW)
451  {
452  Real33 J;
453 
454  auto & grad_psi = _map_grad_psi_face(sid, elem_type)(side);
455 
456  for (unsigned int node = 0; node < num_side_nodes; ++node)
457  {
458  auto points = kokkosMesh().getNodePoint(kokkosMesh().getContiguousNodeID(eid, side, node));
459 
460  J += grad_psi(node, qp).cartesian_product(points);
461  }
462 
463  *JxW = ::Kokkos::sqrt((J * J.transpose()).determinant(_dimension - 1)) *
464  _weights_face(sid, elem_type)[side][qp] * coordTransformFactor(sid, xyz);
465  }
466 }
467 #endif
468 
476 {
477 public:
482  AssemblyHolder(const Assembly & assembly) : _assembly_host(assembly), _assembly_device(assembly)
483  {
484  }
490  {
491  }
492 
493 #ifdef MOOSE_KOKKOS_SCOPE
494 
499  KOKKOS_FUNCTION const Assembly & kokkosAssembly() const
500  {
501  KOKKOS_IF_ON_HOST(return _assembly_host;)
502 
503  return _assembly_device;
504  }
505 #endif
506 
507 private:
516 };
517 
518 } // namespace Kokkos
519 } // namespace Moose
Array2D< Array< Real > > _weights
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.
std::map< FEType, unsigned int > _fe_type_map
FE type ID map.
AssemblyHolder(const AssemblyHolder &holder)
Copy constructor.
Array2D< Array< Array2D< Real3 > > > _map_grad_psi_face
unsigned int _rz_radial_coord
Radial coordinate index in cylindrical coordinate system.
Array2D< Array< Array2D< Real > > > _map_phi_face
The Kokkos assembly class.
The Kokkos object that contains the information of an element The IDs used in Kokkos are different fr...
Definition: KokkosMesh.h:35
Assembly(FEProblemBase &problem)
Constructor.
Array2D< unsigned int > _n_qps_face
const unsigned int invalid_uint
KOKKOS_FUNCTION dof_id_type getQpOffset(ElementInfo info) const
Get the starting offset of quadrature points of an element into the global quadrature point index...
KOKKOS_FUNCTION const Assembly & kokkosAssembly() const
Get the const reference of the Kokkos assembly.
FEProblemBase & _problem
Reference of the MOOSE problem.
KOKKOS_FUNCTION unsigned int getNumQps(ElementInfo info) const
Get the number of quadrature points of an element.
MPI_Info info
Array3D< Array< Array2D< Real > > > _phi_face
dof_id_type ThreadID
Definition: KokkosThread.h:18
Array< Array< Real > > _jxw
Array2D< dof_id_type > _qp_offset_face
Array< dof_id_type > _n_subdomain_qps_face
KOKKOS_FUNCTION void operator()(const ThreadID tid) const
Kokkos function for caching physical maps on element quadrature points.
const unsigned int _dimension
Mesh dimension.
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.
unsigned int getFETypeID(FEType type) const
Get the FE type ID.
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.
Array2D< Array< Array< Real > > > _weights_face
The Kokkos interface that holds the host reference of the Kokkos mesh and copies it to device during ...
Definition: KokkosMesh.h:338
KOKKOS_FUNCTION Real coordTransformFactor(const ContiguousSubdomainID subdomain, const Real3 point) const
Get the coordinate transform factor for a point in a subdomain.
Array< unsigned int > _n_qps
Number of quadrature points.
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
Array2D< unsigned int > _n_dofs
Array2D< Array2D< Real > > _map_phi
Shape functions for computing reference-to-physical maps.
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 cachePhysicalMap()
Cache physical maps on element quadrature points.
Array2D< Array< Array2D< Real3 > > > _map_grad_phi_face
Array< Pair< Real3, Real3 > > _rz_axis
General axisymmetric axis of each subdomain in cylindrical coordinate system.
KOKKOS_FUNCTION unsigned int getNumNodes(unsigned int elem_type) const
Get the number of nodes of an element type.
Definition: KokkosMesh.h:172
KOKKOS_FUNCTION unsigned int getMaxQpsPerElem() const
Get the maximum number of quadrature points per element in the current partition. ...
Array< Array< Real33 > > _jacobian
Cached physical maps on element quadrature points.
Array2D< Array< Real3 > > _q_points
Quadrature points and weights for reference elements.
Array3D< Array< Array2D< Real3 > > > _grad_phi_face
MooseMesh wraps a libMesh::Mesh object and enhances its capabilities by caching additional data and s...
Definition: MooseMesh.h:92
The Kokkos interface that holds the host reference of the Kokkos assembly and copies it to device dur...
Array3D< Array2D< Real > > _phi
Shape functions for reference elements.
KOKKOS_FUNCTION dof_id_type getNumFaceQps(ContiguousSubdomainID subdomain) const
Get the total number of facial quadrature points in a subdomain Note: this number does not represent ...
unsigned int _max_qps_per_elem
Array2D< Array< Array< Real3 > > > _q_points_face
void initQuadrature()
Initialize quadrature data.
KOKKOS_INLINE_FUNCTION Real33 transpose()
Definition: KokkosTypes.h:106
KOKKOS_FUNCTION Real3 getNodePoint(ContiguousNodeID node) const
Get the coordinate of a node.
Definition: KokkosMesh.h:215
Array3D< Array2D< Real3 > > _grad_phi
KOKKOS_FUNCTION Real getJxW(ElementInfo info, unsigned int qp) const
Get the transformed Jacobian weight of an element quadrature point.
KOKKOS_FUNCTION const Mesh & kokkosMesh() const
Get the const reference of the Kokkos mesh.
Definition: KokkosMesh.h:360
MooseMesh & _mesh
Reference of the MOOSE mesh.
const auto & getMaterialBoundaries() const
Get the list of boundaries to cache face material properties.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
KOKKOS_FUNCTION dof_id_type getQpFaceOffset(ElementInfo info, unsigned int side) const
Get the starting offset of quadrature points of a side of an element into the global quadrature point...
KOKKOS_INLINE_FUNCTION Real determinant(const unsigned int dim=3)
Definition: KokkosTypes.h:58
void initShape()
Initialize shape data.
CTSub CT_OPERATOR_BINARY CTMul CTCompareLess CTCompareGreater CTCompareEqual _arg template * sqrt(_arg)) *_arg.template D< dtag >()) CT_SIMPLE_UNARY_FUNCTION(tanh
Array2D< Array2D< Real3 > > _map_grad_phi
Array< dof_id_type > _qp_offset
Starting offset into the global quadrature point index.
KOKKOS_FUNCTION dof_id_type getNumQps(ContiguousSubdomainID subdomain) const
Get the total number of elemental quadrature points in a subdomain.
std::set< BoundaryID > _material_boundaries
Boundaries to cache face material properties.
Array< Moose::CoordinateSystemType > _coord_type
Coordinate system type of each subdomain.
Array< Array< Real3 > > _xyz
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...
Array< dof_id_type > _n_subdomain_qps
Array2D< Array< Array2D< Real > > > _map_psi_face
void init()
Initialize assembly.
AssemblyHolder(const Assembly &assembly)
Constructor.
const Assembly _assembly_device
Device copy of the Kokkos assembly.
const Assembly & _assembly_host
Host reference of the Kokkos assembly.
KOKKOS_INLINE_FUNCTION Real33 inverse(const unsigned int dim=3)
Definition: KokkosTypes.h:75
KOKKOS_FUNCTION Real33 getJacobian(ElementInfo info, unsigned int qp) const
Get the inverse of Jacobian matrix of an element quadrature point.
uint8_t dof_id_type
KOKKOS_FUNCTION void computePhysicalMap(const ElementInfo info, const unsigned int qp, Real33 *const jacobian, Real *const JxW, Real3 *const q_points) const
Compute physical transformation data for an element.
KOKKOS_FUNCTION unsigned int getNumFaceQps(ElementInfo info, unsigned int side) const
Get the number of quadrature points of a side of an element.
KOKKOS_FUNCTION Real3 getQPoint(ElementInfo info, unsigned int qp) const
Get the coordinate of an element quadrature point.