20#ifndef OPM_GET_QUASI_IMPES_WEIGHTS_HEADER_INCLUDED
21#define OPM_GET_QUASI_IMPES_WEIGHTS_HEADER_INCLUDED
23#include <dune/common/fvector.hh>
25#include <opm/grid/utility/ElementChunks.hpp>
27#include <opm/material/common/MathToolbox.hpp>
37 template <
class DenseMatrix>
41 for (
int i = 0; i < M.rows; ++i)
42 for (
int j = 0; j < M.cols; ++j)
51 template <
class Matrix,
class Vector>
52 void getQuasiImpesWeights(
const Matrix& matrix,
const int pressureVarIndex,
const bool transpose, Vector& weights)
54 using VectorBlockType =
typename Vector::block_type;
55 using MatrixBlockType =
typename Matrix::block_type;
56 const Matrix& A = matrix;
58 VectorBlockType rhs(0.0);
59 rhs[pressureVarIndex] = 1.0;
62 MatrixBlockType diag_block;
63 VectorBlockType bweights;
64 MatrixBlockType diag_block_transpose;
68 #pragma omp parallel for private(diag_block, bweights, diag_block_transpose)
70 for (
int row_idx = 0; row_idx < static_cast<int>(A.N()); ++row_idx) {
71 diag_block = MatrixBlockType(0.0);
73 const auto row_it = A.begin() + row_idx;
74 const auto endj = (*row_it).end();
75 for (
auto j = (*row_it).begin(); j != endj; ++j) {
76 if (row_it.index() == j.index()) {
82 diag_block.solve(bweights, rhs);
85 diag_block_transpose.solve(bweights, rhs);
88 double abs_max = *std::max_element(
89 bweights.begin(), bweights.end(), [](
double a,
double b) { return std::fabs(a) < std::fabs(b); });
90 bweights /= std::fabs(abs_max);
91 weights[row_idx] = bweights;
95 template <
class Matrix,
class Vector>
98 Vector weights(matrix.N());
103 template<
class Vector,
class ElementContext,
class Model,
class ElementChunksType>
105 const ElementContext& elemCtx,
107 const ElementChunksType& element_chunks)
109 using VectorBlockType =
typename Vector::block_type;
110 using Matrix =
typename std::decay_t<
decltype(model.linearizer().jacobian())>;
111 using MatrixBlockType =
typename Matrix::MatrixBlock;
112 constexpr int numEq = VectorBlockType::size();
113 using Evaluation =
typename std::decay_t<
decltype(model.localLinearizer(
ThreadManager::threadId()).localResidual().residual(0))>
116 VectorBlockType rhs(0.0);
117 rhs[pressureVarIndex] = 1.0;
120 MatrixBlockType block;
121 VectorBlockType bweights;
122 MatrixBlockType block_transpose;
123 Dune::FieldVector<Evaluation, numEq> storage;
127 #pragma omp parallel for private(block, bweights, block_transpose, storage)
129 for (
const auto& chunk : element_chunks) {
131 ElementContext localElemCtx(elemCtx.simulator());
133 for (
const auto& elem : chunk) {
134 localElemCtx.updatePrimaryStencil(elem);
135 localElemCtx.updatePrimaryIntensiveQuantities(0);
137 model.localLinearizer(thread_id).localResidual().computeStorage(storage, localElemCtx, 0, 0);
139 auto extrusionFactor = localElemCtx.intensiveQuantities(0, 0).extrusionFactor();
140 auto scvVolume = localElemCtx.stencil(0).subControlVolume(0).volume() * extrusionFactor;
141 auto storage_scale = scvVolume / localElemCtx.simulator().timeStepSize();
142 const double pressure_scale = 50e5;
145 for (
int ii = 0; ii < numEq; ++ii) {
146 for (
int jj = 0; jj < numEq; ++jj) {
147 block_transpose[jj][ii] = storage[ii].derivative(jj)/storage_scale;
148 if (jj == pressureVarIndex) {
149 block_transpose[jj][ii] *= pressure_scale;
153 block_transpose.solve(bweights, rhs);
155 double abs_max = *std::max_element(
156 bweights.begin(), bweights.end(), [](
double a,
double b) { return std::fabs(a) < std::fabs(b); });
158 bweights /= std::fabs(abs_max);
160 const auto index = localElemCtx.globalSpaceIndex(0, 0);
161 weights[index] = bweights;
167 template <
class Vector,
class ElementContext,
class Model,
class ElementChunksType>
170 const ElementContext& elemCtx,
172 const ElementChunksType& element_chunks)
182 using FluidSystem =
typename Model::FluidSystem;
183 using LhsEval = double;
185 using PrimaryVariables =
typename Model::PrimaryVariables;
186 using VectorBlockType =
typename Vector::block_type;
188 typename std::decay_t<
decltype(model.localLinearizer(
ThreadManager::threadId()).localResidual().residual(0))>::block_type;
189 using Toolbox = MathToolbox<Evaluation>;
191 const auto& solution = model.solution( 0);
192 VectorBlockType bweights;
197 #pragma omp parallel for private(bweights)
199 for (
const auto& chunk : element_chunks) {
202 ElementContext localElemCtx(elemCtx.simulator());
204 for (
const auto& elem : chunk) {
205 localElemCtx.updatePrimaryStencil(elem);
206 localElemCtx.updatePrimaryIntensiveQuantities(0);
208 const auto index = localElemCtx.globalSpaceIndex(0, 0);
209 const auto& intQuants = localElemCtx.intensiveQuantities(0, 0);
210 const auto& fs = intQuants.fluidState();
212 if (FluidSystem::phaseIsActive(FluidSystem::waterPhaseIdx)) {
213 const unsigned activeCompIdx = FluidSystem::canonicalToActiveCompIdx(
214 FluidSystem::solventComponentIndex(FluidSystem::waterPhaseIdx));
215 bweights[activeCompIdx]
216 = Toolbox::template decay<LhsEval>(1 / fs.invB(FluidSystem::waterPhaseIdx));
219 double denominator = 1.0;
220 double rs = Toolbox::template decay<double>(fs.Rs());
221 double rv = Toolbox::template decay<double>(fs.Rv());
222 const auto& priVars = solution[index];
223 if (priVars.primaryVarsMeaningGas() == PrimaryVariables::GasMeaning::Rv) {
226 if (priVars.primaryVarsMeaningGas() == PrimaryVariables::GasMeaning::Rs) {
229 if (FluidSystem::phaseIsActive(FluidSystem::oilPhaseIdx)
230 && FluidSystem::phaseIsActive(FluidSystem::gasPhaseIdx)) {
231 denominator = Toolbox::template decay<LhsEval>(1 - rs * rv);
234 if (FluidSystem::phaseIsActive(FluidSystem::oilPhaseIdx)) {
235 const unsigned activeCompIdx = FluidSystem::canonicalToActiveCompIdx(
236 FluidSystem::solventComponentIndex(FluidSystem::oilPhaseIdx));
237 bweights[activeCompIdx] = Toolbox::template decay<LhsEval>(
238 (1 / fs.invB(FluidSystem::oilPhaseIdx) - rs / fs.invB(FluidSystem::gasPhaseIdx))
241 if (FluidSystem::phaseIsActive(FluidSystem::gasPhaseIdx)) {
242 const unsigned activeCompIdx = FluidSystem::canonicalToActiveCompIdx(
243 FluidSystem::solventComponentIndex(FluidSystem::gasPhaseIdx));
244 bweights[activeCompIdx] = Toolbox::template decay<LhsEval>(
245 (1 / fs.invB(FluidSystem::gasPhaseIdx) - rv / fs.invB(FluidSystem::oilPhaseIdx))
249 weights[index] = bweights;
#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.
void getQuasiImpesWeights(const Matrix &matrix, const int pressureVarIndex, const bool transpose, Vector &weights)
Definition: getQuasiImpesWeights.hpp:52
void getTrueImpesWeightsAnalytic(int, Vector &weights, const ElementContext &elemCtx, const Model &model, const ElementChunksType &element_chunks)
Definition: getQuasiImpesWeights.hpp:168
void getTrueImpesWeights(int pressureVarIndex, Vector &weights, const ElementContext &elemCtx, const Model &model, const ElementChunksType &element_chunks)
Definition: getQuasiImpesWeights.hpp:104
DenseMatrix transposeDenseMatrix(const DenseMatrix &M)
Definition: getQuasiImpesWeights.hpp:38
Definition: blackoilboundaryratevector.hh:39