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_solventMassFraction( fluid.solventMassFraction() ),
78  m_primarySpeciesAggregateConcentration( fluid.primarySpeciesAggregateConcentration() ),
79  m_dPrimarySpeciesAggregateConcentration_dLogPrimarySpeciesConcentrations( fluid.dPrimarySpeciesAggregateConcentration_dLogPrimarySpeciesConcentrations() ),
80  m_density( fluid.density() ),
81  m_dDensity( fluid.dDensity() ),
82  m_localMatrix( localMatrix ),
83  m_localRhs( localRhs ),
84  m_massProd( massProd )
85  {}
86 
92  {
93 public:
94 
97 
100 
103 
106 
107  real64 totalInflowMass = 0.0;
108 
109  };
110 
117  integer elemGhostRank( localIndex const ei ) const
118  { return m_elemGhostRank( ei ); }
119 
127  void setup( localIndex const ei,
128  localIndex const a,
129  StackVariables & stack ) const
130  {
131  // set row index and degrees of freedom indices for this element
132  stack.massRowIndex = m_dofNumber[ei] - m_rankOffset;
133  for( integer idof = 0; idof < numDof; ++idof )
134  {
135  stack.dofIndices[idof] = m_dofNumber[ei] + idof - m_rankOffset;
136  }
137 
138  stack.totalInflowMass = m_rhsContributionArrayView[a];
139  }
140 
149  StackVariables & stack ) const
150  {
151  real64 const scaledInflowMass = stack.totalInflowMass / m_sizeScalingFactor;
152 
153  // the inflow mass carries molality * solvent mass fraction moles per kg of solution
154  for( integer i = 0; i < numSpecies; ++i )
155  {
156  stack.localSpeciesRhs[i] += m_primarySpeciesAggregateConcentration[ei][0][i] * m_solventMassFraction * scaledInflowMass;
157 
158  for( integer j = 0; j < numSpecies; ++j )
159  {
160  stack.localSpeciesJacobian[i][j+numDof-numSpecies] += m_dPrimarySpeciesAggregateConcentration_dLogPrimarySpeciesConcentrations[ei][0][i][j] * m_solventMassFraction *
161  scaledInflowMass;
162  }
163  }
164  }
165 
173  StackVariables & stack ) const
174  {
175  m_massProd += stack.totalInflowMass / m_sizeScalingFactor;
176  m_localRhs[stack.massRowIndex] += stack.totalInflowMass / m_sizeScalingFactor;
177 
178  if( stack.totalInflowMass > 0.0 )
179  {
180  for( integer i = 0; i < numSpecies; ++i )
181  {
182  globalIndex const speciesRowBeginIndex = stack.massRowIndex + numEqn - numSpecies;
183  m_localRhs[speciesRowBeginIndex + i] += stack.localSpeciesRhs[i];
184 
185  // add contribution to global residual and jacobian (no need for atomics here)
186  m_localMatrix.template addToRow< serialAtomic >( speciesRowBeginIndex+i,
187  stack.dofIndices,
188  stack.localSpeciesJacobian[i],
189  numDof );
190  }
191  }
192  }
193 
201  template< typename POLICY, typename KERNEL_TYPE >
202  static void
204  KERNEL_TYPE const & kernelComponent )
205  {
207 
208  forAll< POLICY >( targetSet.size(), [=] GEOS_HOST_DEVICE ( localIndex const a )
209  {
210  // we need to filter out ghosts here, because targetSet may contain them
211  localIndex const ei = targetSet[a];
212 
213  if( kernelComponent.elemGhostRank( ei ) >= 0 )
214  {
215  return;
216  }
217 
218  typename KERNEL_TYPE::StackVariables stack;
219 
220  kernelComponent.setup( ei, a, stack );
221  kernelComponent.computeSourceFlux( ei, stack );
222  kernelComponent.complete( ei, stack );
223  } );
224  }
225 
226 protected:
227 
230 
233 
236 
241 
244 
245  // View on the total concentration of ions that contain the primary species
246  arrayView3d< real64 const, constitutive::reactivefluid::USD_SPECIES > const m_primarySpeciesAggregateConcentration;
247  // View on the derivatives of total ion concentration for the primary species wrt log of primary species concentration
248  arrayView4d< real64 const, constitutive::reactivefluid::USD_SPECIES_DC > const m_dPrimarySpeciesAggregateConcentration_dLogPrimarySpeciesConcentrations;
249 
250  // View on the fluid density
252  // View on the derivatives of fluid density
254 
259 
260  RAJA::ReduceSum< parallelDeviceReduce, real64 > m_massProd;
261 
262 };
263 
268 {
269 public:
270 
286  template< typename POLICY, typename BASE_FLUID_TYPE >
287  static void
288  createAndLaunch( integer const numSpecies,
289  globalIndex const rankOffset,
290  arrayView1d< globalIndex const > const dofNumber,
291  arrayView1d< integer const > const elemGhostRank,
292  SortedArrayView< localIndex const > const targetSet,
293  arrayView1d< real64 const > const rhsContributionArrayView,
294  real64 const sizeScalingFactor,
295  constitutive::reactivefluid::ReactiveSinglePhaseFluid< BASE_FLUID_TYPE > const & fluid,
296  CRSMatrixView< real64, globalIndex const > const & localMatrix,
297  arrayView1d< real64 > const & localRhs,
298  RAJA::ReduceSum< parallelDeviceReduce, real64 > massProd )
299  {
300  internal::kernelLaunchSelectorCompSwitch( numSpecies, [&] ( auto NS )
301  {
302  integer constexpr NUM_SPECIES = NS();
303  integer constexpr NUM_DOF = 1+NS();
304 
305  SourceFluxComputeKernel< NUM_DOF, NUM_SPECIES, BASE_FLUID_TYPE > kernel( rankOffset, dofNumber, elemGhostRank, rhsContributionArrayView, sizeScalingFactor, fluid, localMatrix, localRhs,
306  massProd );
308  } );
309  }
310 };
311 } // namespace singlePhaseReactiveBaseKernels
312 
313 } // namespace geos
314 
315 #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:97
#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.
real64 const m_solventMassFraction
Mass fraction of solvent in the solution [-]; molality times this fraction is the amount per kg of so...
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.