https://mooseframework.inl.gov
KokkosLinearFVFluxKernel.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 "KokkosLinearFVKernel.h"
14 #include "KokkosDatum.h"
15 
16 namespace Moose::Kokkos
17 {
18 
25 {
26 public:
28 
30  LinearFVFluxKernel(const LinearFVFluxKernel & object);
31 
32  virtual void computeRightHandSide() override;
33  virtual void computeMatrix() override;
34  virtual void initialSetup() override;
35 
37  virtual bool needsBoundaryValueData() const { return false; }
39  virtual bool needsBoundaryNormalGradientData() const { return false; }
41  virtual bool hasInternalRightHandSideContribution() const { return true; }
42 
45  {
46  };
49  {
50  };
53  {
54  };
57  {
58  };
59 
60  template <typename Derived>
61  KOKKOS_FUNCTION void
62  operator()(InternalRightHandSideLoop, const ThreadID tid, const Derived & kernel) const;
63 
64  template <typename Derived>
65  KOKKOS_FUNCTION void
66  operator()(BoundaryRightHandSideLoop, const ThreadID tid, const Derived & kernel) const;
67 
68  template <typename Derived>
69  KOKKOS_FUNCTION void
70  operator()(InternalMatrixLoop, const ThreadID tid, const Derived & kernel) const;
71 
72  template <typename Derived>
73  KOKKOS_FUNCTION void
74  operator()(BoundaryMatrixLoop, const ThreadID tid, const Derived & kernel) const;
75 
80  template <typename Derived>
82  KOKKOS_FUNCTION Real computeInternalMatrixContribution(const FVDatum & datum) const;
83  template <typename Derived>
84  KOKKOS_FUNCTION Real computeInternalNeighborMatrixContribution(const FVDatum & datum) const;
85  template <typename Derived>
87  const FVDatum & datum, const int bc_index, const MOOSE_KOKKOS_INDEX_TYPE bc_face_index) const;
88  template <typename Derived>
89  KOKKOS_FUNCTION Real computeInternalRightHandSideContribution(const FVDatum & datum) const;
90  template <typename Derived>
92  const FVDatum & datum, const int bc_index, const MOOSE_KOKKOS_INDEX_TYPE bc_face_index) const;
94 
100  template <typename Derived>
103  {
104  return &LinearFVFluxKernel::computeInternalMatrixContribution<Derived>;
105  }
106 
107  template <typename Derived>
109  {
110  return &LinearFVFluxKernel::computeInternalNeighborMatrixContribution<Derived>;
111  }
112 
113  template <typename Derived>
115  {
116  return &LinearFVFluxKernel::computeBoundaryMatrixContribution<Derived>;
117  }
119 
120 protected:
125  {
127  unsigned int side;
128  int bc_index;
129  MOOSE_KOKKOS_INDEX_TYPE bc_face_index;
130 
131  bool operator<(const BoundaryFaceID & other) const
132  {
133  if (bc_index != other.bc_index)
134  return bc_index < other.bc_index;
135  if (elem != other.elem)
136  return elem < other.elem;
137  return side < other.side;
138  }
139  };
140 
141 #ifndef NDEBUG
142 
147  KOKKOS_FUNCTION bool hasFaceNeighbor(const FVDatum & datum) const;
148 
150  KOKKOS_FUNCTION bool hasBoundaryData(const FVDatum & datum, const int bc_index) const;
151 #endif
152 
154  KOKKOS_FUNCTION Real boundaryValueCoefficient(const int bc_index,
155  const MOOSE_KOKKOS_INDEX_TYPE bc_face_index) const;
157  KOKKOS_FUNCTION Real boundaryValueSource(const int bc_index,
158  const MOOSE_KOKKOS_INDEX_TYPE bc_face_index) const;
160  KOKKOS_FUNCTION Real boundaryNormalGradientCoefficient(
161  const int bc_index, const MOOSE_KOKKOS_INDEX_TYPE bc_face_index) const;
163  KOKKOS_FUNCTION Real boundaryNormalGradientSource(
164  const int bc_index, const MOOSE_KOKKOS_INDEX_TYPE bc_face_index) const;
165 
174 
175  std::unique_ptr<DispatcherBase> _internal_rhs_dispatcher;
176  std::unique_ptr<DispatcherBase> _boundary_rhs_dispatcher;
177  std::unique_ptr<DispatcherBase> _internal_matrix_dispatcher;
178  std::unique_ptr<DispatcherBase> _boundary_matrix_dispatcher;
179 };
180 
181 #ifndef NDEBUG
182 KOKKOS_FUNCTION inline bool
184 {
185  if (!datum.hasNeighbor())
186  return false;
187 
188  const auto & sys = kokkosSystem(_var.sys());
189  return sys.isVariableActive(_var.var(), datum.neighborSubdomain());
190 }
191 
192 KOKKOS_FUNCTION inline bool
193 LinearFVFluxKernel::hasBoundaryData(const FVDatum & datum, const int bc_index) const
194 {
195  return bc_index >= 0 && _bc_index.isAlloc() &&
196  _bc_index(datum.side(), datum.elemID()) == bc_index;
197 }
198 #endif
199 
200 KOKKOS_FUNCTION inline Real
202  const MOOSE_KOKKOS_INDEX_TYPE bc_face_index) const
203 {
204  KOKKOS_ASSERT(bc_index >= 0);
205  return _bc_data[bc_index].value.coefficient[bc_face_index];
206 }
207 
208 KOKKOS_FUNCTION inline Real
210  const MOOSE_KOKKOS_INDEX_TYPE bc_face_index) const
211 {
212  KOKKOS_ASSERT(bc_index >= 0);
213  return _bc_data[bc_index].value.source[bc_face_index];
214 }
215 
216 KOKKOS_FUNCTION inline Real
218  const int bc_index, const MOOSE_KOKKOS_INDEX_TYPE bc_face_index) const
219 {
220  KOKKOS_ASSERT(bc_index >= 0);
221  return _bc_data[bc_index].normal_gradient.coefficient[bc_face_index];
222 }
223 
224 KOKKOS_FUNCTION inline Real
226  const MOOSE_KOKKOS_INDEX_TYPE bc_face_index) const
227 {
228  KOKKOS_ASSERT(bc_index >= 0);
229  return _bc_data[bc_index].normal_gradient.source[bc_face_index];
230 }
231 
232 template <typename Derived>
233 KOKKOS_FUNCTION Real
235 {
236  ::Kokkos::abort("Default computeInternalMatrixContribution() should never be called. Make sure "
237  "you properly redefined this method in your class without typos.");
238 
239  return 0;
240 }
241 
242 template <typename Derived>
243 KOKKOS_FUNCTION Real
245 {
246  ::Kokkos::abort("Default computeInternalNeighborMatrixContribution() should never be called. "
247  "Make sure you properly redefined this method in your class without typos.");
248 
249  return 0;
250 }
251 
252 template <typename Derived>
253 KOKKOS_FUNCTION Real
255  const int,
256  const MOOSE_KOKKOS_INDEX_TYPE) const
257 {
258  ::Kokkos::abort("Default computeBoundaryMatrixContribution() should never be called. Make sure "
259  "you properly redefined this method in your class without typos.");
260 
261  return 0;
262 }
263 
264 template <typename Derived>
265 KOKKOS_FUNCTION Real
267 {
268  ::Kokkos::abort("Default computeInternalRightHandSideContribution() should never be called. Make "
269  "sure you properly redefined this method in your class without typos.");
270 
271  return 0;
272 }
273 
274 template <typename Derived>
275 KOKKOS_FUNCTION Real
277  const int,
278  const MOOSE_KOKKOS_INDEX_TYPE) const
279 {
280  ::Kokkos::abort("Default computeBoundaryRightHandSideContribution() should never be called. Make "
281  "sure you properly redefined this method in your class without typos.");
282 
283  return 0;
284 }
285 
286 template <typename Derived>
287 KOKKOS_FUNCTION void
289  const ThreadID tid,
290  const Derived & kernel) const
291 {
292  const auto [elem, side] = _internal_face_ids[tid];
293  FVDatum datum(elem, side, kokkosMesh());
294  KOKKOS_ASSERT(hasFaceNeighbor(datum));
295 
296  KOKKOS_ASSERT(_var.components() == 1);
297  const auto & sys = kokkosSystem(_var.sys());
298  kernel.accumulateTaggedVector(
299  kernel.template computeInternalRightHandSideContribution<Derived>(datum),
300  sys.getElemLocalDofIndex(elem, 0, _var.var()));
301 }
302 
303 template <typename Derived>
304 KOKKOS_FUNCTION void
306  const ThreadID tid,
307  const Derived & kernel) const
308 {
309  const auto [elem, side, bc_index, bc_face_index] = _boundary_face_ids[tid];
310  FVDatum datum(elem, side, kokkosMesh());
311  KOKKOS_ASSERT(hasBoundaryData(datum, bc_index));
312 
313  KOKKOS_ASSERT(_var.components() == 1);
314  const auto & sys = kokkosSystem(_var.sys());
315  kernel.accumulateTaggedVector(kernel.template computeBoundaryRightHandSideContribution<Derived>(
316  datum, bc_index, bc_face_index),
317  sys.getElemLocalDofIndex(elem, 0, _var.var()));
318 }
319 
320 template <typename Derived>
321 KOKKOS_FUNCTION void
322 LinearFVFluxKernel::operator()(InternalMatrixLoop, const ThreadID tid, const Derived & kernel) const
323 {
324  const auto [elem, side] = _internal_face_ids[tid];
325  FVDatum datum(elem, side, kokkosMesh());
326  KOKKOS_ASSERT(hasFaceNeighbor(datum));
327 
328  KOKKOS_ASSERT(_var.components() == 1);
329  const auto var_num = _var.var();
330  const auto & sys = kokkosSystem(_var.sys());
331  const auto row = sys.getElemLocalDofIndex(elem, 0, var_num);
332  kernel.accumulateTaggedMatrix(kernel.template computeInternalMatrixContribution<Derived>(datum),
333  row,
334  sys.getElemGlobalDofIndex(elem, 0, var_num));
335  kernel.accumulateTaggedMatrix(
336  kernel.template computeInternalNeighborMatrixContribution<Derived>(datum),
337  row,
338  sys.getElemGlobalDofIndex(datum.neighborID(), 0, var_num));
339 }
340 
341 template <typename Derived>
342 KOKKOS_FUNCTION void
343 LinearFVFluxKernel::operator()(BoundaryMatrixLoop, const ThreadID tid, const Derived & kernel) const
344 {
345  const auto [elem, side, bc_index, bc_face_index] = _boundary_face_ids[tid];
346  FVDatum datum(elem, side, kokkosMesh());
347  KOKKOS_ASSERT(hasBoundaryData(datum, bc_index));
348 
349  KOKKOS_ASSERT(_var.components() == 1);
350  const auto var_num = _var.var();
351  const auto & sys = kokkosSystem(_var.sys());
352  const auto row = sys.getElemLocalDofIndex(elem, 0, var_num);
353  kernel.accumulateTaggedMatrix(
354  kernel.template computeBoundaryMatrixContribution<Derived>(datum, bc_index, bc_face_index),
355  row,
356  sys.getElemGlobalDofIndex(elem, 0, var_num));
357 }
358 
359 } // namespace Moose::Kokkos
KOKKOS_FUNCTION bool hasBoundaryData(const FVDatum &datum, const int bc_index) const
Whether this boundary face has a boundary condition data provider.
The Kokkos array class.
Definition: KokkosArray.h:65
KOKKOS_FUNCTION unsigned int sys(unsigned int comp=0) const
Get the system number of a component.
LinearFVFluxKernel(const InputParameters &parameters)
virtual void computeRightHandSide() override
Compute the right-hand side contributions of this object.
Tag dispatch type for boundary-face right-hand side computation.
dof_id_type ContiguousElementID
Definition: KokkosMesh.h:20
KOKKOS_FUNCTION Real computeBoundaryRightHandSideContribution(const FVDatum &datum, const int bc_index, const MOOSE_KOKKOS_INDEX_TYPE bc_face_index) const
std::unique_ptr< DispatcherBase > _internal_rhs_dispatcher
KOKKOS_FUNCTION ContiguousElementID elemID() const
Get the contiguous element ID.
Definition: KokkosDatum.h:52
static InputParameters validParams()
const InputParameters & parameters() const
Get the parameters of the object.
Definition: MooseBase.h:131
KOKKOS_FUNCTION bool hasFaceNeighbor(const FVDatum &datum) const
Whether this face has a mesh neighbor on a subdomain where this kernel&#39;s variable is active...
static auto defaultInternalMatrixContribution()
Functions used to check if users have overriden the matrix hook methods, whose calculations can be sk...
KOKKOS_FUNCTION Real computeInternalRightHandSideContribution(const FVDatum &datum) const
virtual bool hasInternalRightHandSideContribution() const
Whether this kernel has nonzero internal-face RHS contributions.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
KOKKOS_FUNCTION Real computeInternalMatrixContribution(const FVDatum &datum) const
Default methods to prevent compile errors when matrix/RHS contributions are not defined in the derive...
KOKKOS_FUNCTION Real boundaryValueCoefficient(const int bc_index, const MOOSE_KOKKOS_INDEX_TYPE bc_face_index) const
Boundary value coefficient for this boundary face.
KOKKOS_FUNCTION Real boundaryNormalGradientCoefficient(const int bc_index, const MOOSE_KOKKOS_INDEX_TYPE bc_face_index) const
Boundary normal gradient coefficient for this boundary face.
KOKKOS_FUNCTION bool hasNeighbor() const
Get whether the current side has a neighbor.
Definition: KokkosDatum.h:149
std::unique_ptr< DispatcherBase > _boundary_matrix_dispatcher
Directed boundary side and its compact boundary condition and BC-local face indices.
KOKKOS_FUNCTION unsigned int components() const
Get the number of components.
KOKKOS_FUNCTION unsigned int side() const
Get the side index.
Definition: KokkosDatum.h:79
Array< LinearFVBoundaryCondition::BoundaryData > _bc_data
Boundary condition data handles indexed by compact boundary condition index.
Array< FaceID > _internal_face_ids
Directed internal element sides with an active variable on the neighbor element.
MOOSE_KOKKOS_INDEX_TYPE ThreadID
Definition: KokkosThread.h:22
Base class for Kokkos linear finite volume kernels that contribute on faces (flux terms)...
Tag dispatch type for internal-face right-hand side computation.
Array< BoundaryFaceID > _boundary_face_ids
Directed boundary element sides with active Kokkos boundary data.
bool operator<(const BoundaryFaceID &other) const
virtual bool needsBoundaryNormalGradientData() const
Whether this kernel needs boundary normal gradient data from its boundary conditions.
Base class for Kokkos linear finite volume kernels that contribute to the linear system.
KOKKOS_FUNCTION Real computeBoundaryMatrixContribution(const FVDatum &datum, const int bc_index, const MOOSE_KOKKOS_INDEX_TYPE bc_face_index) const
virtual void computeMatrix() override
Compute the matrix contributions of this object.
KOKKOS_FUNCTION const Mesh & kokkosMesh() const
Get the const reference of the Kokkos mesh.
Definition: KokkosMesh.h:651
Tag dispatch type for boundary-face matrix computation.
std::unique_ptr< DispatcherBase > _internal_matrix_dispatcher
KOKKOS_FUNCTION ContiguousSubdomainID neighborSubdomain() const
Get the contiguous neighbor subdomain ID.
Definition: KokkosDatum.h:97
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
Tag dispatch type for internal-face matrix computation.
KOKKOS_FUNCTION void operator()(InternalRightHandSideLoop, const ThreadID tid, const Derived &kernel) const
Device-side geometric context for finite volume kernels and boundary conditions.
Definition: KokkosDatum.h:167
virtual bool needsBoundaryValueData() const
Whether this kernel needs boundary value data from its boundary conditions.
Array2D< int > _bc_index
Setup/debug map from (side, element) to compact boundary condition index.
KOKKOS_FUNCTION unsigned int var(unsigned int comp=0) const
Get the variable number of a component.
std::unique_ptr< DispatcherBase > _boundary_rhs_dispatcher
KOKKOS_FUNCTION Real boundaryNormalGradientSource(const int bc_index, const MOOSE_KOKKOS_INDEX_TYPE bc_face_index) const
Boundary normal gradient source for this boundary face.
virtual void initialSetup() override
Gets called at the beginning of the simulation before this object is asked to do its job...
KOKKOS_FUNCTION Real boundaryValueSource(const int bc_index, const MOOSE_KOKKOS_INDEX_TYPE bc_face_index) const
Boundary value source for this boundary face.
KOKKOS_FUNCTION Real computeInternalNeighborMatrixContribution(const FVDatum &datum) const
KOKKOS_FUNCTION ContiguousElementID neighborID() const
Get the contiguous neighbor element ID.
Definition: KokkosDatum.h:73