GEOS
AccumulationKernels.hpp
Go to the documentation of this file.
1 /*
2  * ------------------------------------------------------------------------------------------------------------
3  * SPDX-License-Identifier: LGPL-2.1-only
4  *
5  * Copyright (c) 2016-2024 Lawrence Livermore National Security LLC
6  * Copyright (c) 2018-2024 TotalEnergies
7  * Copyright (c) 2018-2024 The Board of Trustees of the Leland Stanford Junior University
8  * Copyright (c) 2023-2024 Chevron
9  * Copyright (c) 2019- GEOS/GEOSX Contributors
10  * All rights reserved
11  *
12  * See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
13  * ------------------------------------------------------------------------------------------------------------
14  */
15 
20 #ifndef GEOS_PHYSICSSOLVERS_FLUIDFLOW_SINGLEPHASE_ACCUMULATIONKERNELS_HPP
21 #define GEOS_PHYSICSSOLVERS_FLUIDFLOW_SINGLEPHASE_ACCUMULATIONKERNELS_HPP
22 
23 #include "common/DataTypes.hpp"
24 #include "common/GEOS_RAJA_Interface.hpp"
25 #include "constitutive/fluid/singlefluid/SingleFluidBase.hpp"
26 #include "constitutive/fluid/singlefluid/SingleFluidUtils.hpp"
27 #include "constitutive/fluid/singlefluid/SingleFluidUtils.hpp"
28 #include "constitutive/solid/CoupledSolidBase.hpp"
31 #include "codingUtilities/Utilities.hpp"
32 
33 namespace geos
34 {
35 
36 namespace singlePhaseBaseKernels
37 {
38 
39 /******************************** AccumulationKernel ********************************/
40 
45 template< typename SUBREGION_TYPE, integer NUM_DOF >
47 {
48 
49 public:
50  using SingleFluidProp = constitutive::SingleFluidVar< real64, 2, constitutive::singlefluid::LAYOUT_FLUID, constitutive::singlefluid::LAYOUT_FLUID_DER >;
51 
53  static constexpr integer numDof = NUM_DOF;
54 
56  static constexpr integer numEqn = NUM_DOF;
57 
58 
60  static constexpr integer isThermal = NUM_DOF-1;
61  using DerivOffset = constitutive::singlefluid::DerivativeOffsetC< isThermal >;
62 
71  AccumulationKernel( globalIndex const rankOffset,
72  string const dofKey,
73  SUBREGION_TYPE const & subRegion,
75  arrayView1d< real64 > const & localRhs )
76  :
77  m_rankOffset( rankOffset ),
78  m_dofNumber( subRegion.template getReference< array1d< globalIndex > >( dofKey ) ),
79  m_elemGhostRank( subRegion.ghostRank() ),
80  m_mass( subRegion.template getField< fields::flow::mass >() ),
81  m_mass_n( subRegion.template getField< fields::flow::mass_n >() ),
82  m_dMass( subRegion.template getField< fields::flow::dMass >() ),
83  m_localMatrix( localMatrix ),
84  m_localRhs( localRhs )
85  {}
86 
92  {
93 public:
94 
95  // Residual information
96 
99 
102 
105 
108 
109  };
110 
117  integer elemGhostRank( localIndex const ei ) const
118  { return m_elemGhostRank( ei ); }
119 
120 
127  void setup( localIndex const ei,
128  StackVariables & stack ) const
129  {
130  // set row index and degrees of freedom indices for this element
131  stack.localRow = m_dofNumber[ei] - m_rankOffset;
132  for( integer idof = 0; idof < numDof; ++idof )
133  {
134  stack.dofIndices[idof] = m_dofNumber[ei] + idof;
135  }
136  }
137 
145  template< typename FUNC = NoOpFunc >
148  StackVariables & stack,
149  FUNC && kernelOp = NoOpFunc{} ) const
150  {
151  // Residual contribution is mass conservation in the cell
152  stack.localResidual[0] = m_mass[ei] - m_mass_n[ei];
153 
154  // Derivative of residual wrt to pressure in the cell
155  stack.localJacobian[0][0] = m_dMass[ei][DerivOffset::dP];
156 
157  // Customize the kernel with this lambda
158  kernelOp();
159  }
160 
168  StackVariables & stack ) const
169  {
170  // add contribution to global residual and jacobian (no need for atomics here)
171  m_localMatrix.template addToRow< serialAtomic >( stack.localRow,
172  stack.dofIndices,
173  stack.localJacobian[0],
174  numDof );
175  m_localRhs[stack.localRow] += stack.localResidual[0];
176 
177  }
178 
186  template< typename POLICY, typename KERNEL_TYPE >
187  static void
188  launch( localIndex const numElems,
189  KERNEL_TYPE const & kernelComponent )
190  {
192 
193  forAll< POLICY >( numElems, [=] GEOS_HOST_DEVICE ( localIndex const ei )
194  {
195  if( kernelComponent.elemGhostRank( ei ) >= 0 )
196  {
197  return;
198  }
199 
200  typename KERNEL_TYPE::StackVariables stack;
201 
202  kernelComponent.setup( ei, stack );
203  kernelComponent.computeAccumulation( ei, stack );
204  kernelComponent.complete( ei, stack );
205  } );
206  }
207 
208 protected:
209 
212 
215 
218 
221  arrayView1d< real64 const > const m_mass_n;
223 
228 
229 };
230 
235 class SurfaceElementAccumulationKernel : public AccumulationKernel< SurfaceElementSubRegion, 1 >
236 {
237 
238 public:
239 
241 
253  string const dofKey,
254  SurfaceElementSubRegion const & subRegion,
255  CRSMatrixView< real64, globalIndex const > const & localMatrix,
256  arrayView1d< real64 > const & localRhs )
257  : Base( rankOffset, dofKey, subRegion, localMatrix, localRhs )
258  , m_creationMass( subRegion.getField< fields::flow::massCreated >() )
259  {}
260 
269  Base::StackVariables & stack ) const
270  {
271  Base::computeAccumulation( ei, stack );
272  if( Base::m_mass_n[ei] > 1.1 * m_creationMass[ei] )
273  {
274  stack.localResidual[0] += m_creationMass[ei] * 0.25;
275  }
276  }
277 
278 protected:
279 
280  arrayView1d< real64 const > const m_creationMass;
281 
282 };
283 
288 {
289 public:
290 
302  template< typename POLICY, typename SUBREGION_TYPE >
303  static void
304  createAndLaunch( globalIndex const rankOffset,
305  string const dofKey,
306  SUBREGION_TYPE const & subRegion,
307  CRSMatrixView< real64, globalIndex const > const & localMatrix,
308  arrayView1d< real64 > const & localRhs )
309  {
310  if constexpr ( std::is_base_of_v< CellElementSubRegion, SUBREGION_TYPE > )
311  {
312  integer constexpr NUM_DOF = 1;
313  AccumulationKernel< CellElementSubRegion, NUM_DOF > kernel( rankOffset, dofKey, subRegion, localMatrix, localRhs );
314  AccumulationKernel< CellElementSubRegion, NUM_DOF >::template launch< POLICY >( subRegion.size(), kernel );
315  }
316  else if constexpr ( std::is_base_of_v< SurfaceElementSubRegion, SUBREGION_TYPE > )
317  {
318  SurfaceElementAccumulationKernel kernel( rankOffset, dofKey, subRegion, localMatrix, localRhs );
319  SurfaceElementAccumulationKernel::launch< POLICY >( subRegion.size(), kernel );
320  }
321  else
322  {
323  GEOS_UNUSED_VAR( rankOffset, dofKey, subRegion, localMatrix, localRhs );
324  GEOS_ERROR( "Unsupported subregion type: " << typeid(SUBREGION_TYPE).name() );
325  }
326  }
327 
328 };
329 
330 } // namespace singlePhaseBaseKernels
331 
332 } // namespace geos
333 
334 #endif //GEOS_PHYSICSSOLVERS_FLUIDFLOW_SINGLEPHASE_ACCUMULATIONKERNELS_HPP
#define GEOS_HOST_DEVICE
Marks a host-device function.
Definition: GeosxMacros.hpp:49
#define GEOS_UNUSED_VAR(...)
Mark an unused variable and silence compiler warnings.
Definition: GeosxMacros.hpp:84
#define GEOS_UNUSED_PARAM(X)
Mark an unused argument and silence compiler warnings.
Definition: GeosxMacros.hpp:72
#define GEOS_ERROR(msg)
Raise a hard error and terminate the program.
Definition: Logger.hpp:157
#define GEOS_MARK_FUNCTION
Mark function with both Caliper and NVTX if enabled.
static void createAndLaunch(globalIndex const rankOffset, string const dofKey, SUBREGION_TYPE const &subRegion, CRSMatrixView< real64, globalIndex const > const &localMatrix, arrayView1d< real64 > const &localRhs)
Create a new kernel and launch.
Define the interface for the assembly kernel in charge of accumulation.
static constexpr integer isThermal
Note: Derivative lineup only supports dP & dT, not component terms.
AccumulationKernel(globalIndex const rankOffset, string const dofKey, SUBREGION_TYPE const &subRegion, CRSMatrixView< real64, globalIndex const > const &localMatrix, arrayView1d< real64 > const &localRhs)
Constructor.
static void launch(localIndex const numElems, KERNEL_TYPE const &kernelComponent)
Performs the kernel launch.
GEOS_HOST_DEVICE void computeAccumulation(localIndex const ei, StackVariables &stack, FUNC &&kernelOp=NoOpFunc{}) const
Compute the local accumulation contributions to the residual and Jacobian.
arrayView1d< globalIndex const > const m_dofNumber
View on the dof numbers.
static constexpr integer numDof
Compute time value for the number of degrees of freedom.
GEOS_HOST_DEVICE void complete(localIndex const GEOS_UNUSED_PARAM(ei), StackVariables &stack) const
Performs the complete phase for the kernel.
arrayView1d< real64 const > const m_mass
View on mass.
arrayView1d< real64 > const m_localRhs
View on the local RHS.
GEOS_HOST_DEVICE integer elemGhostRank(localIndex const ei) const
Getter for the ghost rank of an element.
CRSMatrixView< real64, globalIndex const > const m_localMatrix
View on the local CRS matrix.
GEOS_HOST_DEVICE void setup(localIndex const ei, StackVariables &stack) const
Performs the setup phase for the kernel.
arrayView1d< integer const > const m_elemGhostRank
View on the ghost ranks.
static constexpr integer numEqn
Compute time value for the number of equations.
globalIndex const m_rankOffset
Offset for my MPI rank.
Define the interface for the assembly kernel in charge of accumulation in SurfaceElementSubRegion.
SurfaceElementAccumulationKernel(globalIndex const rankOffset, string const dofKey, SurfaceElementSubRegion const &subRegion, CRSMatrixView< real64, globalIndex const > const &localMatrix, arrayView1d< real64 > const &localRhs)
Constructor.
GEOS_HOST_DEVICE void computeAccumulation(localIndex const ei, Base::StackVariables &stack) const
Compute the local accumulation contributions to the residual and Jacobian.
ArrayView< T, 1 > arrayView1d
Alias for 1D array view.
Definition: DataTypes.hpp:188
LvArray::CRSMatrixView< T, COL_INDEX, localIndex const, LvArray::ChaiBuffer > CRSMatrixView
Alias for CRS Matrix View.
Definition: DataTypes.hpp:318
GEOS_GLOBALINDEX_TYPE globalIndex
Global index type (for indexing objects across MPI partitions).
Definition: DataTypes.hpp:87
double real64
64-bit floating point type.
Definition: DataTypes.hpp:98
GEOS_LOCALINDEX_TYPE localIndex
Local index type (for indexing objects within an MPI partition).
Definition: DataTypes.hpp:84
std::int32_t integer
Signed integer type.
Definition: DataTypes.hpp:81
ArrayView< T, 2, USD > arrayView2d
Alias for 2D array view.
Definition: DataTypes.hpp:204
Array< T, 1 > array1d
Alias for 1D array.
Definition: DataTypes.hpp:184
Kernel variables (dof numbers, jacobian and residual) located on the stack.
real64 localJacobian[numEqn][numDof]
Storage for the element local Jacobian matrix.
localIndex localRow
Index of the local row corresponding to this element.
globalIndex dofIndices[numDof]
Index of the matrix row/column corresponding to the dof in this element.
real64 localResidual[numEqn]
Storage for the element local residual vector.