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, const int pressureVarIndex, const bool transpose, Vector& weights, bool enable_thread_parallel)
62 {
63 using VectorBlockType = typename Vector::block_type;
64 using MatrixBlockType = typename Matrix::block_type;
65 const Matrix& A = matrix;
66
67 VectorBlockType rhs(0.0);
68 rhs[pressureVarIndex] = 1.0;
69
70 // Declare variables outside the loop to avoid repetitive allocation
71 MatrixBlockType diag_block;
72 VectorBlockType bweights;
73 MatrixBlockType diag_block_transpose;
74
75 // Use OpenMP to parallelize over matrix rows (runtime controlled via if clause)
76#ifdef _OPENMP
77#pragma omp parallel for private(diag_block, bweights, diag_block_transpose) if(enable_thread_parallel)
78#endif
79 for (int row_idx = 0; row_idx < static_cast<int>(A.N()); ++row_idx) {
80 diag_block = MatrixBlockType(0.0);
81 // Find diagonal block for this row
82 const auto row_it = A.begin() + row_idx;
83 const auto endj = (*row_it).end();
84 for (auto j = (*row_it).begin(); j != endj; ++j) {
85 if (row_it.index() == j.index()) {
86 diag_block = (*j);
87 break;
88 }
89 }
90 if (transpose) {
91 diag_block.solve(bweights, rhs);
92 } else {
93 diag_block_transpose = Details::transposeDenseMatrix(diag_block);
94 diag_block_transpose.solve(bweights, rhs);
95 }
96
97 double abs_max = *std::max_element(
98 bweights.begin(), bweights.end(), [](double a, double b) { return std::fabs(a) < std::fabs(b); });
99 bweights /= std::fabs(abs_max);
100 weights[row_idx] = bweights;
101 }
102 }
103
104 template <class Matrix, class Vector>
105 Vector getQuasiImpesWeights(const Matrix& matrix, const int pressureVarIndex, const bool transpose, bool enable_thread_parallel)
106 {
107 Vector weights(matrix.N());
108 getQuasiImpesWeights(matrix, pressureVarIndex, transpose, weights, enable_thread_parallel);
109 return weights;
110 }
111
112#if HAVE_CUDA
113 template <typename T>
114 std::vector<int> precomputeDiagonalIndices(const gpuistl::GpuSparseMatrixWrapper<T>& matrix) {
115 std::vector<int> diagonalIndices(matrix.N(), -1);
116 const auto rowIndices = matrix.getRowIndices().asStdVector();
117 const auto colIndices = matrix.getColumnIndices().asStdVector();
118
119 for (auto row = 0; row < Opm::gpuistl::detail::to_int(matrix.N()); ++row) {
120 for (auto i = rowIndices[row]; i < rowIndices[row+1]; ++i) {
121 if (colIndices[i] == row) {
122 diagonalIndices[row] = i;
123 break;
124 }
125 }
126 }
127 return diagonalIndices;
128 }
129
130 // GPU version that delegates to the GPU implementation
131 template <typename T, bool transpose>
132 void getQuasiImpesWeights(const gpuistl::GpuSparseMatrixWrapper<T>& matrix,
133 const int pressureVarIndex,
134 gpuistl::GpuVector<T>& weights,
135 const gpuistl::GpuVector<int>& diagonalIndices)
136 {
137 gpuistl::detail::getQuasiImpesWeights<T, transpose>(matrix, pressureVarIndex, weights, diagonalIndices);
138 }
139
140 template <typename T, bool transpose>
141 gpuistl::GpuVector<T> getQuasiImpesWeights(const gpuistl::GpuSparseMatrixWrapper<T>& matrix,
142 const int pressureVarIndex,
143 const gpuistl::GpuVector<int>& diagonalIndices)
144 {
145 gpuistl::GpuVector<T> weights(matrix.N() * matrix.blockSize());
146 getQuasiImpesWeights<T, transpose>(matrix, pressureVarIndex, weights, diagonalIndices);
147 return weights;
148 }
149#endif
150
151 template<class Vector, class ElementContext, class Model, class ElementChunksType>
152 void getTrueImpesWeights(int pressureVarIndex, Vector& weights,
153 const ElementContext& elemCtx,
154 const Model& model,
155 const ElementChunksType& element_chunks,
156 bool enable_thread_parallel)
157 {
158 using VectorBlockType = typename Vector::block_type;
159 using Matrix = typename std::decay_t<decltype(model.linearizer().jacobian())>;
160 using MatrixBlockType = typename Matrix::MatrixBlock;
161 constexpr int numEq = VectorBlockType::size();
162 using Evaluation = typename std::decay_t<decltype(model.localLinearizer(ThreadManager::threadId()).localResidual().residual(0))>
163 ::block_type;
164
165 VectorBlockType rhs(0.0);
166 rhs[pressureVarIndex] = 1.0;
167
168 // Declare variables outside the loop to avoid repetitive allocation
169 MatrixBlockType block;
170 VectorBlockType bweights;
171 MatrixBlockType block_transpose;
172 Dune::FieldVector<Evaluation, numEq> storage;
173
175#ifdef _OPENMP
176#pragma omp parallel for private(block, bweights, block_transpose, storage) if(enable_thread_parallel)
177#endif
178 for (const auto& chunk : element_chunks) {
179 const std::size_t thread_id = ThreadManager::threadId();
180 ElementContext localElemCtx(elemCtx.simulator());
181
182 for (const auto& elem : chunk) {
183 localElemCtx.updatePrimaryStencil(elem);
184 localElemCtx.updatePrimaryIntensiveQuantities(/*timeIdx=*/0);
185
186 model.localLinearizer(thread_id).localResidual().computeStorage(storage, localElemCtx, /*spaceIdx=*/0, /*timeIdx=*/0);
187
188 auto extrusionFactor = localElemCtx.intensiveQuantities(0, /*timeIdx=*/0).extrusionFactor();
189 auto scvVolume = localElemCtx.stencil(/*timeIdx=*/0).subControlVolume(0).volume() * extrusionFactor;
190 auto storage_scale = scvVolume / localElemCtx.simulator().timeStepSize();
191 const double pressure_scale = 50e5;
192
193 // Build the transposed matrix directly to avoid separate transpose step
194 for (int ii = 0; ii < numEq; ++ii) {
195 for (int jj = 0; jj < numEq; ++jj) {
196 block_transpose[jj][ii] = storage[ii].derivative(jj)/storage_scale;
197 if (jj == pressureVarIndex) {
198 block_transpose[jj][ii] *= pressure_scale;
199 }
200 }
201 }
202 block_transpose.solve(bweights, rhs);
203
204 double abs_max = *std::max_element(
205 bweights.begin(), bweights.end(), [](double a, double b) { return std::fabs(a) < std::fabs(b); });
206 // probably a scaling which could give approximately total compressibility would be better
207 bweights /= std::fabs(abs_max); // given normal densities this scales weights to about 1.
208
209 const auto index = localElemCtx.globalSpaceIndex(/*spaceIdx=*/0, /*timeIdx=*/0);
210 weights[index] = bweights;
211 }
212 }
213 OPM_END_PARALLEL_TRY_CATCH("getTrueImpesWeights() failed: ", elemCtx.simulator().vanguard().grid().comm());
214 }
215
216 template <class Vector, class ElementContext, class Model, class ElementChunksType>
217 void getTrueImpesWeightsAnalytic(int /*pressureVarIndex*/,
218 Vector& weights,
219 const ElementContext& elemCtx,
220 const Model& model,
221 const ElementChunksType& element_chunks,
222 bool enable_thread_parallel)
223 {
224 // The sequential residual is a linear combination of the
225 // mass balance residuals, with coefficients equal to (for
226 // water, oil, gas):
227 // 1/bw,
228 // (1/bo - rs/bg)/(1-rs*rv)
229 // (1/bg - rv/bo)/(1-rs*rv)
230 // These coefficients must be applied for both the residual and
231 // Jacobian.
232 using FluidSystem = typename Model::FluidSystem;
233 using LhsEval = double;
234
235 using PrimaryVariables = typename Model::PrimaryVariables;
236 using VectorBlockType = typename Vector::block_type;
237 using Evaluation =
238 typename std::decay_t<decltype(model.localLinearizer(ThreadManager::threadId()).localResidual().residual(0))>::block_type;
239 using Toolbox = MathToolbox<Evaluation>;
240
241 const auto& solution = model.solution(/*timeIdx*/ 0);
242 VectorBlockType bweights;
243
244 // Use OpenMP to parallelize over element chunks (runtime controlled via if clause)
246#ifdef _OPENMP
247#pragma omp parallel for private(bweights) if(enable_thread_parallel)
248#endif
249 for (const auto& chunk : element_chunks) {
250
251 // Each thread gets a unique copy of elemCtx
252 ElementContext localElemCtx(elemCtx.simulator());
253
254 for (const auto& elem : chunk) {
255 localElemCtx.updatePrimaryStencil(elem);
256 localElemCtx.updatePrimaryIntensiveQuantities(/*timeIdx=*/0);
257
258 const auto index = localElemCtx.globalSpaceIndex(/*spaceIdx=*/0, /*timeIdx=*/0);
259 const auto& intQuants = localElemCtx.intensiveQuantities(/*spaceIdx=*/0, /*timeIdx=*/0);
260 const auto& fs = intQuants.fluidState();
261
262 if (FluidSystem::phaseIsActive(FluidSystem::waterPhaseIdx)) {
263 const unsigned activeCompIdx = FluidSystem::canonicalToActiveCompIdx(
264 FluidSystem::solventComponentIndex(FluidSystem::waterPhaseIdx));
265 bweights[activeCompIdx]
266 = Toolbox::template decay<LhsEval>(1 / fs.invB(FluidSystem::waterPhaseIdx));
267 }
268
269 double denominator = 1.0;
270 double rs = Toolbox::template decay<double>(fs.Rs());
271 double rv = Toolbox::template decay<double>(fs.Rv());
272 const auto& priVars = solution[index];
273 if (priVars.primaryVarsMeaningGas() == PrimaryVariables::GasMeaning::Rv) {
274 rs = 0.0;
275 }
276 if (priVars.primaryVarsMeaningGas() == PrimaryVariables::GasMeaning::Rs) {
277 rv = 0.0;
278 }
279 if (FluidSystem::phaseIsActive(FluidSystem::oilPhaseIdx)
280 && FluidSystem::phaseIsActive(FluidSystem::gasPhaseIdx)) {
281 denominator = Toolbox::template decay<LhsEval>(1 - rs * rv);
282 }
283
284 if (FluidSystem::phaseIsActive(FluidSystem::oilPhaseIdx)) {
285 const unsigned activeCompIdx = FluidSystem::canonicalToActiveCompIdx(
286 FluidSystem::solventComponentIndex(FluidSystem::oilPhaseIdx));
287 bweights[activeCompIdx] = Toolbox::template decay<LhsEval>(
288 (1 / fs.invB(FluidSystem::oilPhaseIdx) - rs / fs.invB(FluidSystem::gasPhaseIdx))
289 / denominator);
290 }
291 if (FluidSystem::phaseIsActive(FluidSystem::gasPhaseIdx)) {
292 const unsigned activeCompIdx = FluidSystem::canonicalToActiveCompIdx(
293 FluidSystem::solventComponentIndex(FluidSystem::gasPhaseIdx));
294 bweights[activeCompIdx] = Toolbox::template decay<LhsEval>(
295 (1 / fs.invB(FluidSystem::gasPhaseIdx) - rv / fs.invB(FluidSystem::oilPhaseIdx))
296 / denominator);
297 }
298
299 weights[index] = bweights;
300 }
301 }
302 OPM_END_PARALLEL_TRY_CATCH("getTrueImpesAnalyticWeights() failed: ", elemCtx.simulator().vanguard().grid().comm());
303 }
304} // namespace Amg
305
306} // namespace Opm
307
308#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:152
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:217
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