GEOS
SourceFluxComputeKernel.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_SINGLEPHASEREACTIVE_SOURCEFLUXCOMPUTEKERNELS_HPP
21 #define GEOS_PHYSICSSOLVERS_FLUIDFLOW_SINGLEPHASEREACTIVE_SOURCEFLUXCOMPUTEKERNELS_HPP
22 
23 #include "common/DataTypes.hpp"
24 #include "common/GEOS_RAJA_Interface.hpp"
25 #include "constitutive/fluid/reactivefluid/ReactiveSinglePhaseFluid.hpp"
26 #include "constitutive/fluid/reactivefluid/ReactiveFluidLayouts.hpp"
27 #include "constitutive/fluid/singlefluid/SingleFluidBase.hpp"
28 #include "constitutive/fluid/singlefluid/SingleFluidUtils.hpp"
29 #include "codingUtilities/Utilities.hpp"
30 
31 #include "physicsSolvers/fluidFlow/kernels/singlePhase/reactive/KernelLaunchSelectors.hpp"
32 
33 namespace geos
34 {
35 
36 namespace singlePhaseReactiveBaseKernels
37 {
38 
39 /******************************** SourceFluxComputeKernel ********************************/
40 
45 template< integer NUM_DOF, integer NUM_SPECIES, typename BASE_FLUID_TYPE >
47 {
48 
49 public:
50 
52  static constexpr integer numDof = NUM_DOF;
53 
55  static constexpr integer numEqn = NUM_DOF;
56 
58  static constexpr integer numSpecies = NUM_SPECIES;
59 
60  using DerivOffset = constitutive::singlefluid::DerivativeOffsetC< 1 >;
61 
62  SourceFluxComputeKernel( globalIndex const rankOffset,
63  arrayView1d< globalIndex const > const dofNumber,
65  arrayView1d< real64 const > const rhsContributionArrayView,
66  real64 const sizeScalingFactor,
67  constitutive::reactivefluid::ReactiveSinglePhaseFluid< BASE_FLUID_TYPE > const & fluid,
69  arrayView1d< real64 > const & localRhs,
70  RAJA::ReduceSum< parallelDeviceReduce, real64 > massProd )
71  :
72  m_rankOffset( rankOffset ),
73  m_dofNumber( dofNumber ),
75  m_rhsContributionArrayView( rhsContributionArrayView ),
76  m_sizeScalingFactor( sizeScalingFactor ),
77  m_primarySpeciesAggregateConcentration( fluid.primarySpeciesAggregateConcentration() ),
78  m_dPrimarySpeciesAggregateConcentration_dLogPrimarySpeciesConcentrations( fluid.dPrimarySpeciesAggregateConcentration_dLogPrimarySpeciesConcentrations() ),
79  m_density( fluid.density() ),
80  m_dDensity( fluid.dDensity() ),
81  m_localMatrix( localMatrix ),
82  m_localRhs( localRhs ),
83  m_massProd( massProd )
84  {}
85 
91  {
92 public:
93 
96 
99 
102 
105 
106  real64 totalInflowMass = 0.0;
107 
108  };
109 
116  integer elemGhostRank( localIndex const ei ) const
117  { return m_elemGhostRank( ei ); }
118 
126  void setup( localIndex const ei,
127  localIndex const a,
128  StackVariables & stack ) const
129  {
130  // set row index and degrees of freedom indices for this element
131  stack.massRowIndex = m_dofNumber[ei] - m_rankOffset;
132  for( integer idof = 0; idof < numDof; ++idof )
133  {
134  stack.dofIndices[idof] = m_dofNumber[ei] + idof - m_rankOffset;
135  }
136 
137  stack.totalInflowMass = m_rhsContributionArrayView[a];
138  }
139 
148  StackVariables & stack ) const
149  {
150  real64 const scaledInflowMass = stack.totalInflowMass / m_sizeScalingFactor;
151 
152  for( integer i = 0; i < numSpecies; ++i )
153  {
154  stack.localSpeciesRhs[i] += m_primarySpeciesAggregateConcentration[ei][0][i] / m_density[ei][0] * scaledInflowMass;
155  stack.localSpeciesJacobian[i][0] += -m_primarySpeciesAggregateConcentration[ei][0][i] * m_dDensity[ei][0][DerivOffset::dP] / (m_density[ei][0] * m_density[ei][0]) * scaledInflowMass;
156 
157  for( integer j = 0; j < numSpecies; ++j )
158  {
159  stack.localSpeciesJacobian[i][j+numDof-numSpecies] += m_dPrimarySpeciesAggregateConcentration_dLogPrimarySpeciesConcentrations[ei][0][i][j] / m_density[ei][0] * scaledInflowMass;
160  }
161  }
162  }
163 
171  StackVariables & stack ) const
172  {
173  m_massProd += stack.totalInflowMass / m_sizeScalingFactor;
174  m_localRhs[stack.massRowIndex] += stack.totalInflowMass / m_sizeScalingFactor;
175 
176  if( stack.totalInflowMass > 0.0 )
177  {
178  for( integer i = 0; i < numSpecies; ++i )
179  {
180  globalIndex const speciesRowBeginIndex = stack.massRowIndex + numEqn - numSpecies;
181  m_localRhs[speciesRowBeginIndex + i] += stack.localSpeciesRhs[i];
182 
183  // add contribution to global residual and jacobian (no need for atomics here)
184  m_localMatrix.template addToRow< serialAtomic >( speciesRowBeginIndex+i,
185  stack.dofIndices,
186  stack.localSpeciesJacobian[i],
187  numDof );
188  }
189  }
190  }
191 
199  template< typename POLICY, typename KERNEL_TYPE >
200  static void
202  KERNEL_TYPE const & kernelComponent )
203  {
205 
206  forAll< POLICY >( targetSet.size(), [=] GEOS_HOST_DEVICE ( localIndex const a )
207  {
208  // we need to filter out ghosts here, because targetSet may contain them
209  localIndex const ei = targetSet[a];
210 
211  if( kernelComponent.elemGhostRank( ei ) >= 0 )
212  {
213  return;
214  }
215 
216  typename KERNEL_TYPE::StackVariables stack;
217 
218  kernelComponent.setup( ei, a, stack );
219  kernelComponent.computeSourceFlux( ei, stack );
220  kernelComponent.complete( ei, stack );
221  } );
222  }
223 
224 protected:
225 
228 
231 
234 
239 
240  // View on the total concentration of ions that contain the primary species
241  arrayView3d< real64 const, constitutive::reactivefluid::USD_SPECIES > const m_primarySpeciesAggregateConcentration;
242  // View on the derivatives of total ion concentration for the primary species wrt log of primary species concentration
243  arrayView4d< real64 const, constitutive::reactivefluid::USD_SPECIES_DC > const m_dPrimarySpeciesAggregateConcentration_dLogPrimarySpeciesConcentrations;
244 
245  // View on the fluid density
247  // View on the derivatives of fluid density
249 
254 
255  RAJA::ReduceSum< parallelDeviceReduce, real64 > m_massProd;
256 
257 };
258 
263 {
264 public:
265 
281  template< typename POLICY, typename BASE_FLUID_TYPE >
282  static void
283  createAndLaunch( integer const numSpecies,
284  globalIndex const rankOffset,
285  arrayView1d< globalIndex const > const dofNumber,
286  arrayView1d< integer const > const elemGhostRank,
287  SortedArrayView< localIndex const > const targetSet,
288  arrayView1d< real64 const > const rhsContributionArrayView,
289  real64 const sizeScalingFactor,
290  constitutive::reactivefluid::ReactiveSinglePhaseFluid< BASE_FLUID_TYPE > const & fluid,
291  CRSMatrixView< real64, globalIndex const > const & localMatrix,
292  arrayView1d< real64 > const & localRhs,
293  RAJA::ReduceSum< parallelDeviceReduce, real64 > massProd )
294  {
295  internal::kernelLaunchSelectorCompSwitch( numSpecies, [&] ( auto NS )
296  {
297  integer constexpr NUM_SPECIES = NS();
298  integer constexpr NUM_DOF = 1+NS();
299 
300  SourceFluxComputeKernel< NUM_DOF, NUM_SPECIES, BASE_FLUID_TYPE > kernel( rankOffset, dofNumber, elemGhostRank, rhsContributionArrayView, sizeScalingFactor, fluid, localMatrix, localRhs,
301  massProd );
303  } );
304  }
305 };
306 } // namespace singlePhaseReactiveBaseKernels
307 
308 } // namespace geos
309 
310 #endif //GEOS_PHYSICSSOLVERS_FLUIDFLOW_SINGLEPHASEREACTIVE_SOURCEFLUXCOMPUTEKERNELS_HPP
#define GEOS_HOST_DEVICE
Marks a host-device function.
Definition: GeosxMacros.hpp:49
#define GEOS_UNUSED_PARAM(X)
Mark an unused argument and silence compiler warnings.
Definition: GeosxMacros.hpp:72
#define GEOS_MARK_FUNCTION
Mark function with both Caliper and NVTX if enabled.
static void createAndLaunch(integer const numSpecies, globalIndex const rankOffset, arrayView1d< globalIndex const > const dofNumber, arrayView1d< integer const > const elemGhostRank, SortedArrayView< localIndex const > const targetSet, arrayView1d< real64 const > const rhsContributionArrayView, real64 const sizeScalingFactor, constitutive::reactivefluid::ReactiveSinglePhaseFluid< BASE_FLUID_TYPE > const &fluid, CRSMatrixView< real64, globalIndex const > const &localMatrix, arrayView1d< real64 > const &localRhs, RAJA::ReduceSum< parallelDeviceReduce, real64 > massProd)
Create a new kernel and launch.
Define the interface for the assembly kernel in charge of source flux.
static void launch(SortedArrayView< localIndex const > const targetSet, KERNEL_TYPE const &kernelComponent)
Performs the kernel launch.
CRSMatrixView< real64, globalIndex const > const m_localMatrix
View on the local CRS matrix.
GEOS_HOST_DEVICE void setup(localIndex const ei, localIndex const a, StackVariables &stack) const
Performs the setup phase for the kernel.
GEOS_HOST_DEVICE void complete(localIndex const GEOS_UNUSED_PARAM(ei), StackVariables &stack) const
Performs the complete phase for the kernel.
GEOS_HOST_DEVICE void computeSourceFlux(localIndex const ei, StackVariables &stack) const
Compute the local source flux contributions to the residual and Jacobian.
arrayView1d< real64 const > const m_rhsContributionArrayView
View on the rhs contribution.
GEOS_HOST_DEVICE integer elemGhostRank(localIndex const ei) const
Getter for the ghost rank of an element.
arrayView1d< integer const > const m_elemGhostRank
View on the ghost ranks.
arrayView1d< globalIndex const > const m_dofNumber
View on the dof numbers.
static constexpr integer numSpecies
Compile time value for the number of primary species.
static constexpr integer numEqn
Compute time value for the number of equations.
static constexpr integer numDof
Compute time value for the number of degrees of freedom.
arrayView1d< real64 > const m_localRhs
View on the local RHS.
ArrayView< T, 1 > arrayView1d
Alias for 1D array view.
Definition: DataTypes.hpp:179
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
LvArray::CRSMatrixView< T, COL_INDEX, INDEX_TYPE const, LvArray::ChaiBuffer > CRSMatrixView
Alias for CRS Matrix View.
Definition: DataTypes.hpp:309
LvArray::SortedArrayView< T, localIndex, LvArray::ChaiBuffer > SortedArrayView
A sorted array view of local indices.
Definition: DataTypes.hpp:270
ArrayView< T, 4, USD > arrayView4d
Alias for 4D array view.
Definition: DataTypes.hpp:227
ArrayView< T, 2, USD > arrayView2d
Alias for 2D array view.
Definition: DataTypes.hpp:195
int integer
Signed integer type.
Definition: DataTypes.hpp:81
ArrayView< T, 3, USD > arrayView3d
Alias for 3D array view.
Definition: DataTypes.hpp:211
Kernel variables (dof numbers, jacobian and residual) located on the stack.
real64 localSpeciesRhs[numSpecies]
Storage for the element local residual vector for species rows.
globalIndex dofIndices[numDof]
Index of the matrix row/column corresponding to the dof in this element.
real64 localSpeciesJacobian[numSpecies][numDof]
Storage for the element local Jacobian matrix for species rows.
localIndex massRowIndex
Index of the local row corresponding to this element.