20#ifndef OPM_GET_QUASI_IMPES_WEIGHTS_HEADER_INCLUDED
21#define OPM_GET_QUASI_IMPES_WEIGHTS_HEADER_INCLUDED
23#include <dune/common/exceptions.hh>
24#include <dune/common/fvector.hh>
26#include <fmt/format.h>
28#include <opm/grid/utility/ElementChunks.hpp>
30#include <opm/material/common/MathToolbox.hpp>
38#include <opm/simulators/linalg/gpuistl_hip/detail/cpr_amg_operations.hpp>
50 template <
class DenseMatrix>
54 for (
int i = 0; i < M.rows; ++i)
55 for (
int j = 0; j < M.cols; ++j)
64 template <
class Matrix,
class Vector>
66 const int pressureVarIndex,
69 [[maybe_unused]]
bool enable_thread_parallel)
71 using VectorBlockType =
typename Vector::block_type;
72 using MatrixBlockType =
typename Matrix::block_type;
73 const Matrix& A = matrix;
75 VectorBlockType rhs(0.0);
76 rhs[pressureVarIndex] = 1.0;
79 MatrixBlockType diag_block;
80 VectorBlockType bweights;
81 MatrixBlockType diag_block_transpose;
85#pragma omp parallel for private(diag_block, bweights, diag_block_transpose) if(enable_thread_parallel)
87 for (
int row_idx = 0; row_idx < static_cast<int>(A.N()); ++row_idx) {
88 diag_block = MatrixBlockType(0.0);
90 const auto row_it = A.begin() + row_idx;
91 const auto endj = (*row_it).end();
92 for (
auto j = (*row_it).begin(); j != endj; ++j) {
93 if (row_it.index() == j.index()) {
99 diag_block.solve(bweights, rhs);
102 diag_block_transpose.solve(bweights, rhs);
105 const double abs_max =
106 *std::ranges::max_element(bweights,
107 [](
double a,
double b)
108 {
return std::fabs(a) < std::fabs(b); });
109 bweights /= std::fabs(abs_max);
110 weights[row_idx] = bweights;
114 template <
class Matrix,
class Vector>
116 const int pressureVarIndex,
117 const bool transpose,
118 bool enable_thread_parallel)
120 Vector weights(matrix.N());
126 template <
typename T>
128 std::vector<int> diagonalIndices(matrix.
N(), -1);
129 const auto rowIndices = matrix.
getRowIndices().asStdVector();
133 for (
auto i = rowIndices[row]; i < rowIndices[row+1]; ++i) {
134 if (colIndices[i] == row) {
135 diagonalIndices[row] = i;
140 return diagonalIndices;
144 template <
typename T,
bool transpose>
146 const int pressureVarIndex,
147 gpuistl::GpuVector<T>& weights,
148 const gpuistl::GpuVector<int>& diagonalIndices)
150 gpuistl::detail::getQuasiImpesWeights<T, transpose>(matrix, pressureVarIndex, weights, diagonalIndices);
153 template <
typename T,
bool transpose>
155 const int pressureVarIndex,
156 const gpuistl::GpuVector<int>& diagonalIndices)
158 gpuistl::GpuVector<T> weights(matrix.N() * matrix.blockSize());
159 getQuasiImpesWeights<T, transpose>(matrix, pressureVarIndex, weights, diagonalIndices);
164 template<
class Vector,
class ElementContext,
class Model,
class ElementChunksType>
166 const ElementContext& elemCtx,
168 const ElementChunksType& element_chunks,
169 [[maybe_unused]]
bool enable_thread_parallel)
171 using VectorBlockType =
typename Vector::block_type;
172 using Matrix =
typename std::decay_t<
decltype(model.linearizer().jacobian())>;
173 using MatrixBlockType =
typename Matrix::MatrixBlock;
174 constexpr int numEq = VectorBlockType::size();
175 using Evaluation =
typename std::decay_t<
decltype(model.localLinearizer(
ThreadManager::threadId()).localResidual().residual(0))>
178 VectorBlockType rhs(0.0);
179 rhs[pressureVarIndex] = 1.0;
182 MatrixBlockType block;
183 VectorBlockType bweights;
184 MatrixBlockType block_transpose;
185 Dune::FieldVector<Evaluation, numEq> storage;
189#pragma omp parallel for private(block, bweights, block_transpose, storage) if(enable_thread_parallel)
191 for (std::size_t ci = 0; ci < element_chunks.size(); ++ci) {
192 const auto& chunk = element_chunks[ci];
194 ElementContext localElemCtx(elemCtx.simulator());
196 for (
const auto& elem : chunk) {
197 localElemCtx.updatePrimaryStencil(elem);
198 localElemCtx.updatePrimaryIntensiveQuantities(0);
200 model.localLinearizer(thread_id).localResidual().computeStorage(storage, localElemCtx, 0, 0);
202 auto extrusionFactor = localElemCtx.intensiveQuantities(0, 0).extrusionFactor();
203 auto scvVolume = localElemCtx.stencil(0).subControlVolume(0).volume() * extrusionFactor;
204 auto storage_scale = scvVolume / localElemCtx.simulator().timeStepSize();
205 const double pressure_scale = 50e5;
208 for (
int ii = 0; ii < numEq; ++ii) {
209 for (
int jj = 0; jj < numEq; ++jj) {
210 block_transpose[jj][ii] = storage[ii].derivative(jj)/storage_scale;
211 if (jj == pressureVarIndex) {
212 block_transpose[jj][ii] *= pressure_scale;
217 block_transpose.solve(bweights, rhs);
219 catch (
const Dune::FMatrixError&) {
231 const auto globalDofIdx =
232 localElemCtx.globalSpaceIndex(0, 0);
234 throw std::runtime_error {
235 fmt::format(
"Singular storage matrix when forming the CPR "
236 "pressure weights for cell {} (Cartesian index "
237 "{}). The storage derivatives of that cell are "
238 "rank deficient, so the pressure equation cannot "
241 localElemCtx.simulator().vanguard()
242 .cartesianIndex(globalDofIdx))
246 const double abs_max =
247 *std::ranges::max_element(bweights,
248 [](
double a,
double b)
249 {
return std::fabs(a) < std::fabs(b); });
251 bweights /= std::fabs(abs_max);
253 const auto index = localElemCtx.globalSpaceIndex(0, 0);
254 weights[index] = bweights;
260 template <
class Vector,
class ElementContext,
class Model,
class ElementChunksType>
263 const ElementContext& elemCtx,
265 const ElementChunksType& element_chunks,
266 [[maybe_unused]]
bool enable_thread_parallel)
276 using FluidSystem =
typename Model::FluidSystem;
277 using LhsEval = double;
279 using PrimaryVariables =
typename Model::PrimaryVariables;
280 using VectorBlockType =
typename Vector::block_type;
282 typename std::decay_t<
decltype(model.localLinearizer(
ThreadManager::threadId()).localResidual().residual(0))>::block_type;
283 using Toolbox = MathToolbox<Evaluation>;
285 const auto& solution = model.solution( 0);
286 VectorBlockType bweights;
291#pragma omp parallel for private(bweights) if(enable_thread_parallel)
293 for (std::size_t ci = 0; ci < element_chunks.size(); ++ci) {
294 const auto& chunk = element_chunks[ci];
297 ElementContext localElemCtx(elemCtx.simulator());
299 for (
const auto& elem : chunk) {
300 localElemCtx.updatePrimaryStencil(elem);
301 localElemCtx.updatePrimaryIntensiveQuantities(0);
303 const auto index = localElemCtx.globalSpaceIndex(0, 0);
304 const auto& intQuants = localElemCtx.intensiveQuantities(0, 0);
305 const auto& fs = intQuants.fluidState();
307 if (FluidSystem::phaseIsActive(FluidSystem::waterPhaseIdx)) {
308 const unsigned activeCompIdx = FluidSystem::canonicalToActiveCompIdx(
309 FluidSystem::solventComponentIndex(FluidSystem::waterPhaseIdx));
310 bweights[activeCompIdx]
311 = Toolbox::template decay<LhsEval>(1 / fs.invB(FluidSystem::waterPhaseIdx));
314 double denominator = 1.0;
315 double rs = Toolbox::template decay<double>(fs.Rs());
316 double rv = Toolbox::template decay<double>(fs.Rv());
317 const auto& priVars = solution[index];
318 if (priVars.primaryVarsMeaningGas() == PrimaryVariables::GasMeaning::Rv) {
321 if (priVars.primaryVarsMeaningGas() == PrimaryVariables::GasMeaning::Rs) {
326 const bool oilAndGas = FluidSystem::phaseIsActive(FluidSystem::oilPhaseIdx)
327 && FluidSystem::phaseIsActive(FluidSystem::gasPhaseIdx);
329 denominator = Toolbox::template decay<LhsEval>(1 - rs * rv);
332 if (FluidSystem::phaseIsActive(FluidSystem::oilPhaseIdx)) {
333 const unsigned activeCompIdx = FluidSystem::canonicalToActiveCompIdx(
334 FluidSystem::solventComponentIndex(FluidSystem::oilPhaseIdx));
335 auto oilWeight = 1 / fs.invB(FluidSystem::oilPhaseIdx);
337 oilWeight -= rs / fs.invB(FluidSystem::gasPhaseIdx);
339 bweights[activeCompIdx] = Toolbox::template decay<LhsEval>(oilWeight / denominator);
341 if (FluidSystem::phaseIsActive(FluidSystem::gasPhaseIdx)) {
342 const unsigned activeCompIdx = FluidSystem::canonicalToActiveCompIdx(
343 FluidSystem::solventComponentIndex(FluidSystem::gasPhaseIdx));
344 auto gasWeight = 1 / fs.invB(FluidSystem::gasPhaseIdx);
346 gasWeight -= rv / fs.invB(FluidSystem::oilPhaseIdx);
348 bweights[activeCompIdx] = Toolbox::template decay<LhsEval>(gasWeight / denominator);
351 weights[index] = bweights;
#define OPM_END_PARALLEL_TRY_CATCH(prefix, comm)
Catch exception and throw in a parallel try-catch clause.
Definition: DeferredLoggingErrorHelpers.hpp:197
#define OPM_BEGIN_PARALLEL_TRY_CATCH()
Macro to setup the try of a parallel try-catch.
Definition: DeferredLoggingErrorHelpers.hpp:160
static unsigned threadId()
Return the index of the current OpenMP thread.
The GpuSparseMatrixWrapper Checks CUDA/HIP version and dispatches a version either using the old or t...
Definition: GpuSparseMatrixWrapper.hpp:63
GpuVector< int > & getRowIndices()
getRowIndices returns the row indices used to represent the BSR structure.
Definition: GpuSparseMatrixWrapper.hpp:268
std::size_t N() const
N returns the number of rows (which is equal to the number of columns)
Definition: GpuSparseMatrixWrapper.hpp:229
GpuVector< int > & getColumnIndices()
getColumnIndices returns the column indices used to represent the BSR structure.
Definition: GpuSparseMatrixWrapper.hpp:288
void getTrueImpesWeights(int pressureVarIndex, Vector &weights, const ElementContext &elemCtx, const Model &model, const ElementChunksType &element_chunks, bool enable_thread_parallel)
Definition: getQuasiImpesWeights.hpp:165
void getQuasiImpesWeights(const Matrix &matrix, const int pressureVarIndex, const bool transpose, Vector &weights, bool enable_thread_parallel)
Definition: getQuasiImpesWeights.hpp:65
void getTrueImpesWeightsAnalytic(int, Vector &weights, const ElementContext &elemCtx, const Model &model, const ElementChunksType &element_chunks, bool enable_thread_parallel)
Definition: getQuasiImpesWeights.hpp:261
DenseMatrix transposeDenseMatrix(const DenseMatrix &M)
Definition: getQuasiImpesWeights.hpp:51
int to_int(std::size_t s)
to_int converts a (on most relevant platforms) 64 bits unsigned size_t to a signed 32 bits signed int
Definition: safe_conversion.hpp:56
Definition: blackoilbioeffectsmodules.hh:45