getQuasiImpesWeights.hpp
Go to the documentation of this file.
1/*
2 Copyright 2019 SINTEF Digital, Mathematics and Cybernetics.
3
4 This file is part of the Open Porous Media project (OPM).
5
6 OPM is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 OPM is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with OPM. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#ifndef OPM_GET_QUASI_IMPES_WEIGHTS_HEADER_INCLUDED
21#define OPM_GET_QUASI_IMPES_WEIGHTS_HEADER_INCLUDED
22
23#include <dune/common/fvector.hh>
24
25#include <opm/grid/utility/ElementChunks.hpp>
27#include <opm/material/common/MathToolbox.hpp>
29#include <algorithm>
30#include <cmath>
31
32#if HAVE_CUDA
33#if USE_HIP
34#include <opm/simulators/linalg/gpuistl_hip/detail/cpr_amg_operations.hpp>
35#else
37#endif
38#endif
39
40
41namespace Opm
42{
43
44namespace Details
45{
46 template <class DenseMatrix>
47 DenseMatrix transposeDenseMatrix(const DenseMatrix& M)
48 {
49 DenseMatrix tmp;
50 for (int i = 0; i < M.rows; ++i)
51 for (int j = 0; j < M.cols; ++j)
52 tmp[j][i] = M[i][j];
53
54 return tmp;
55 }
56} // namespace Details
57
58namespace Amg
59{
60 template <class Matrix, class Vector>
61 void getQuasiImpesWeights(const Matrix& matrix,
62 const int pressureVarIndex,
63 const bool transpose,
64 Vector& weights,
65 [[maybe_unused]] bool enable_thread_parallel)
66 {
67 using VectorBlockType = typename Vector::block_type;
68 using MatrixBlockType = typename Matrix::block_type;
69 const Matrix& A = matrix;
70
71 VectorBlockType rhs(0.0);
72 rhs[pressureVarIndex] = 1.0;
73
74 // Declare variables outside the loop to avoid repetitive allocation
75 MatrixBlockType diag_block;
76 VectorBlockType bweights;
77 MatrixBlockType diag_block_transpose;
78
79 // Use OpenMP to parallelize over matrix rows (runtime controlled via if clause)
80#ifdef _OPENMP
81#pragma omp parallel for private(diag_block, bweights, diag_block_transpose) if(enable_thread_parallel)
82#endif
83 for (int row_idx = 0; row_idx < static_cast<int>(A.N()); ++row_idx) {
84 diag_block = MatrixBlockType(0.0);
85 // Find diagonal block for this row
86 const auto row_it = A.begin() + row_idx;
87 const auto endj = (*row_it).end();
88 for (auto j = (*row_it).begin(); j != endj; ++j) {
89 if (row_it.index() == j.index()) {
90 diag_block = (*j);
91 break;
92 }
93 }
94 if (transpose) {
95 diag_block.solve(bweights, rhs);
96 } else {
97 diag_block_transpose = Details::transposeDenseMatrix(diag_block);
98 diag_block_transpose.solve(bweights, rhs);
99 }
100
101 double abs_max = *std::max_element(
102 bweights.begin(), bweights.end(), [](double a, double b) { return std::fabs(a) < std::fabs(b); });
103 bweights /= std::fabs(abs_max);
104 weights[row_idx] = bweights;
105 }
106 }
107
108 template <class Matrix, class Vector>
109 Vector getQuasiImpesWeights(const Matrix& matrix,
110 const int pressureVarIndex,
111 const bool transpose,
112 bool enable_thread_parallel)
113 {
114 Vector weights(matrix.N());
115 getQuasiImpesWeights(matrix, pressureVarIndex, transpose, weights, enable_thread_parallel);
116 return weights;
117 }
118
119#if HAVE_CUDA
120 template <typename T>
121 std::vector<int> precomputeDiagonalIndices(const gpuistl::GpuSparseMatrixWrapper<T>& matrix) {
122 std::vector<int> diagonalIndices(matrix.N(), -1);
123 const auto rowIndices = matrix.getRowIndices().asStdVector();
124 const auto colIndices = matrix.getColumnIndices().asStdVector();
125
126 for (auto row = 0; row < Opm::gpuistl::detail::to_int(matrix.N()); ++row) {
127 for (auto i = rowIndices[row]; i < rowIndices[row+1]; ++i) {
128 if (colIndices[i] == row) {
129 diagonalIndices[row] = i;
130 break;
131 }
132 }
133 }
134 return diagonalIndices;
135 }
136
137 // GPU version that delegates to the GPU implementation
138 template <typename T, bool transpose>
139 void getQuasiImpesWeights(const gpuistl::GpuSparseMatrixWrapper<T>& matrix,
140 const int pressureVarIndex,
141 gpuistl::GpuVector<T>& weights,
142 const gpuistl::GpuVector<int>& diagonalIndices)
143 {
144 gpuistl::detail::getQuasiImpesWeights<T, transpose>(matrix, pressureVarIndex, weights, diagonalIndices);
145 }
146
147 template <typename T, bool transpose>
148 gpuistl::GpuVector<T> getQuasiImpesWeights(const gpuistl::GpuSparseMatrixWrapper<T>& matrix,
149 const int pressureVarIndex,
150 const gpuistl::GpuVector<int>& diagonalIndices)
151 {
152 gpuistl::GpuVector<T> weights(matrix.N() * matrix.blockSize());
153 getQuasiImpesWeights<T, transpose>(matrix, pressureVarIndex, weights, diagonalIndices);
154 return weights;
155 }
156#endif
157
158 template<class Vector, class ElementContext, class Model, class ElementChunksType>
159 void getTrueImpesWeights(int pressureVarIndex, Vector& weights,
160 const ElementContext& elemCtx,
161 const Model& model,
162 const ElementChunksType& element_chunks,
163 [[maybe_unused]] bool enable_thread_parallel)
164 {
165 using VectorBlockType = typename Vector::block_type;
166 using Matrix = typename std::decay_t<decltype(model.linearizer().jacobian())>;
167 using MatrixBlockType = typename Matrix::MatrixBlock;
168 constexpr int numEq = VectorBlockType::size();
169 using Evaluation = typename std::decay_t<decltype(model.localLinearizer(ThreadManager::threadId()).localResidual().residual(0))>
170 ::block_type;
171
172 VectorBlockType rhs(0.0);
173 rhs[pressureVarIndex] = 1.0;
174
175 // Declare variables outside the loop to avoid repetitive allocation
176 MatrixBlockType block;
177 VectorBlockType bweights;
178 MatrixBlockType block_transpose;
179 Dune::FieldVector<Evaluation, numEq> storage;
180
182#ifdef _OPENMP
183#pragma omp parallel for private(block, bweights, block_transpose, storage) if(enable_thread_parallel)
184#endif
185 for (const auto& chunk : element_chunks) {
186 const std::size_t thread_id = ThreadManager::threadId();
187 ElementContext localElemCtx(elemCtx.simulator());
188
189 for (const auto& elem : chunk) {
190 localElemCtx.updatePrimaryStencil(elem);
191 localElemCtx.updatePrimaryIntensiveQuantities(/*timeIdx=*/0);
192
193 model.localLinearizer(thread_id).localResidual().computeStorage(storage, localElemCtx, /*spaceIdx=*/0, /*timeIdx=*/0);
194
195 auto extrusionFactor = localElemCtx.intensiveQuantities(0, /*timeIdx=*/0).extrusionFactor();
196 auto scvVolume = localElemCtx.stencil(/*timeIdx=*/0).subControlVolume(0).volume() * extrusionFactor;
197 auto storage_scale = scvVolume / localElemCtx.simulator().timeStepSize();
198 const double pressure_scale = 50e5;
199
200 // Build the transposed matrix directly to avoid separate transpose step
201 for (int ii = 0; ii < numEq; ++ii) {
202 for (int jj = 0; jj < numEq; ++jj) {
203 block_transpose[jj][ii] = storage[ii].derivative(jj)/storage_scale;
204 if (jj == pressureVarIndex) {
205 block_transpose[jj][ii] *= pressure_scale;
206 }
207 }
208 }
209 block_transpose.solve(bweights, rhs);
210
211 double abs_max = *std::max_element(
212 bweights.begin(), bweights.end(), [](double a, double b) { return std::fabs(a) < std::fabs(b); });
213 // probably a scaling which could give approximately total compressibility would be better
214 bweights /= std::fabs(abs_max); // given normal densities this scales weights to about 1.
215
216 const auto index = localElemCtx.globalSpaceIndex(/*spaceIdx=*/0, /*timeIdx=*/0);
217 weights[index] = bweights;
218 }
219 }
220 OPM_END_PARALLEL_TRY_CATCH("getTrueImpesWeights() failed: ", elemCtx.simulator().vanguard().grid().comm());
221 }
222
223 template <class Vector, class ElementContext, class Model, class ElementChunksType>
224 void getTrueImpesWeightsAnalytic(int /*pressureVarIndex*/,
225 Vector& weights,
226 const ElementContext& elemCtx,
227 const Model& model,
228 const ElementChunksType& element_chunks,
229 [[maybe_unused]] bool enable_thread_parallel)
230 {
231 // The sequential residual is a linear combination of the
232 // mass balance residuals, with coefficients equal to (for
233 // water, oil, gas):
234 // 1/bw,
235 // (1/bo - rs/bg)/(1-rs*rv)
236 // (1/bg - rv/bo)/(1-rs*rv)
237 // These coefficients must be applied for both the residual and
238 // Jacobian.
239 using FluidSystem = typename Model::FluidSystem;
240 using LhsEval = double;
241
242 using PrimaryVariables = typename Model::PrimaryVariables;
243 using VectorBlockType = typename Vector::block_type;
244 using Evaluation =
245 typename std::decay_t<decltype(model.localLinearizer(ThreadManager::threadId()).localResidual().residual(0))>::block_type;
246 using Toolbox = MathToolbox<Evaluation>;
247
248 const auto& solution = model.solution(/*timeIdx*/ 0);
249 VectorBlockType bweights;
250
251 // Use OpenMP to parallelize over element chunks (runtime controlled via if clause)
253#ifdef _OPENMP
254#pragma omp parallel for private(bweights) if(enable_thread_parallel)
255#endif
256 for (const auto& chunk : element_chunks) {
257
258 // Each thread gets a unique copy of elemCtx
259 ElementContext localElemCtx(elemCtx.simulator());
260
261 for (const auto& elem : chunk) {
262 localElemCtx.updatePrimaryStencil(elem);
263 localElemCtx.updatePrimaryIntensiveQuantities(/*timeIdx=*/0);
264
265 const auto index = localElemCtx.globalSpaceIndex(/*spaceIdx=*/0, /*timeIdx=*/0);
266 const auto& intQuants = localElemCtx.intensiveQuantities(/*spaceIdx=*/0, /*timeIdx=*/0);
267 const auto& fs = intQuants.fluidState();
268
269 if (FluidSystem::phaseIsActive(FluidSystem::waterPhaseIdx)) {
270 const unsigned activeCompIdx = FluidSystem::canonicalToActiveCompIdx(
271 FluidSystem::solventComponentIndex(FluidSystem::waterPhaseIdx));
272 bweights[activeCompIdx]
273 = Toolbox::template decay<LhsEval>(1 / fs.invB(FluidSystem::waterPhaseIdx));
274 }
275
276 double denominator = 1.0;
277 double rs = Toolbox::template decay<double>(fs.Rs());
278 double rv = Toolbox::template decay<double>(fs.Rv());
279 const auto& priVars = solution[index];
280 if (priVars.primaryVarsMeaningGas() == PrimaryVariables::GasMeaning::Rv) {
281 rs = 0.0;
282 }
283 if (priVars.primaryVarsMeaningGas() == PrimaryVariables::GasMeaning::Rs) {
284 rv = 0.0;
285 }
286 if (FluidSystem::phaseIsActive(FluidSystem::oilPhaseIdx)
287 && FluidSystem::phaseIsActive(FluidSystem::gasPhaseIdx)) {
288 denominator = Toolbox::template decay<LhsEval>(1 - rs * rv);
289 }
290
291 if (FluidSystem::phaseIsActive(FluidSystem::oilPhaseIdx)) {
292 const unsigned activeCompIdx = FluidSystem::canonicalToActiveCompIdx(
293 FluidSystem::solventComponentIndex(FluidSystem::oilPhaseIdx));
294 bweights[activeCompIdx] = Toolbox::template decay<LhsEval>(
295 (1 / fs.invB(FluidSystem::oilPhaseIdx) - rs / fs.invB(FluidSystem::gasPhaseIdx))
296 / denominator);
297 }
298 if (FluidSystem::phaseIsActive(FluidSystem::gasPhaseIdx)) {
299 const unsigned activeCompIdx = FluidSystem::canonicalToActiveCompIdx(
300 FluidSystem::solventComponentIndex(FluidSystem::gasPhaseIdx));
301 bweights[activeCompIdx] = Toolbox::template decay<LhsEval>(
302 (1 / fs.invB(FluidSystem::gasPhaseIdx) - rv / fs.invB(FluidSystem::oilPhaseIdx))
303 / denominator);
304 }
305
306 weights[index] = bweights;
307 }
308 }
309 OPM_END_PARALLEL_TRY_CATCH("getTrueImpesAnalyticWeights() failed: ", elemCtx.simulator().vanguard().grid().comm());
310 }
311} // namespace Amg
312
313} // namespace Opm
314
315#endif // OPM_GET_QUASI_IMPES_WEIGHTS_HEADER_INCLUDED
#define OPM_END_PARALLEL_TRY_CATCH(prefix, comm)
Catch exception and throw in a parallel try-catch clause.
Definition: DeferredLoggingErrorHelpers.hpp:192
#define OPM_BEGIN_PARALLEL_TRY_CATCH()
Macro to setup the try of a parallel try-catch.
Definition: DeferredLoggingErrorHelpers.hpp:158
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:61
GpuVector< int > & getRowIndices()
getRowIndices returns the row indices used to represent the BSR structure.
Definition: GpuSparseMatrixWrapper.hpp:267
GpuVector< int > & getColumnIndices()
getColumnIndices returns the column indices used to represent the BSR structure.
Definition: GpuSparseMatrixWrapper.hpp:287
size_t N() const
N returns the number of rows (which is equal to the number of columns)
Definition: GpuSparseMatrixWrapper.hpp:228
void getTrueImpesWeights(int pressureVarIndex, Vector &weights, const ElementContext &elemCtx, const Model &model, const ElementChunksType &element_chunks, bool enable_thread_parallel)
Definition: getQuasiImpesWeights.hpp:159
void getQuasiImpesWeights(const Matrix &matrix, const int pressureVarIndex, const bool transpose, Vector &weights, bool enable_thread_parallel)
Definition: getQuasiImpesWeights.hpp:61
void getTrueImpesWeightsAnalytic(int, Vector &weights, const ElementContext &elemCtx, const Model &model, const ElementChunksType &element_chunks, bool enable_thread_parallel)
Definition: getQuasiImpesWeights.hpp:224
DenseMatrix transposeDenseMatrix(const DenseMatrix &M)
Definition: getQuasiImpesWeights.hpp:47
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:52
Definition: blackoilbioeffectsmodules.hh:43