From 0f12977b17ed22d9861bb154732f58917c02e8b4 Mon Sep 17 00:00:00 2001 From: Tor Harald Sandve Date: Tue, 14 Nov 2023 12:45:25 +0100 Subject: [PATCH 1/2] only use d factor for gas wells --- opm/simulators/wells/MultisegmentWell.hpp | 4 +-- .../wells/MultisegmentWell_impl.hpp | 25 +++++++--------- opm/simulators/wells/StandardWell.hpp | 4 +-- opm/simulators/wells/StandardWell_impl.hpp | 29 +++++++++---------- opm/simulators/wells/WellInterface.hpp | 2 +- opm/simulators/wells/WellInterface_impl.hpp | 23 +++++++++++---- 6 files changed, 47 insertions(+), 40 deletions(-) diff --git a/opm/simulators/wells/MultisegmentWell.hpp b/opm/simulators/wells/MultisegmentWell.hpp index 154984526..09a019e3d 100644 --- a/opm/simulators/wells/MultisegmentWell.hpp +++ b/opm/simulators/wells/MultisegmentWell.hpp @@ -188,7 +188,7 @@ namespace Opm template void computePerfRate(const IntensiveQuantities& int_quants, const std::vector& mob_perfcells, - const double Tw, + const std::vector& Tw, const int seg, const int perf, const Value& segment_pressure, @@ -204,7 +204,7 @@ namespace Opm const Value& rv, const std::vector& b_perfcells, const std::vector& mob_perfcells, - const double Tw, + const std::vector& Tw, const int perf, const Value& segment_pressure, const Value& segment_density, diff --git a/opm/simulators/wells/MultisegmentWell_impl.hpp b/opm/simulators/wells/MultisegmentWell_impl.hpp index 881246ae0..fd032bd23 100644 --- a/opm/simulators/wells/MultisegmentWell_impl.hpp +++ b/opm/simulators/wells/MultisegmentWell_impl.hpp @@ -349,7 +349,7 @@ namespace Opm getMobility(ebosSimulator, perf, mob, deferred_logger); const double trans_mult = ebosSimulator.problem().template wellTransMultiplier(intQuants, cell_idx); const auto& wellstate_nupcol = ebosSimulator.problem().wellModel().nupcolWellState().well(this->index_of_well_); - const double Tw = this->wellIndex(perf, intQuants, trans_mult, wellstate_nupcol); + const std::vector Tw = this->wellIndex(perf, intQuants, trans_mult, wellstate_nupcol); const Scalar seg_pressure = segment_pressure[seg]; std::vector cq_s(this->num_components_, 0.); Scalar perf_press = 0.0; @@ -768,7 +768,7 @@ namespace Opm const Value& rv, const std::vector& b_perfcells, const std::vector& mob_perfcells, - const double Tw, + const std::vector& Tw, const int perf, const Value& segment_pressure, const Value& segment_density, @@ -804,7 +804,7 @@ namespace Opm // compute component volumetric rates at standard conditions for (int comp_idx = 0; comp_idx < this->numComponents(); ++comp_idx) { - const Value cq_p = - Tw * (mob_perfcells[comp_idx] * drawdown); + const Value cq_p = - Tw[comp_idx] * (mob_perfcells[comp_idx] * drawdown); cq_s[comp_idx] = b_perfcells[comp_idx] * cq_p; } @@ -828,9 +828,6 @@ namespace Opm total_mob += mob_perfcells[comp_idx]; } - // injection perforations total volume rates - const Value cqt_i = - Tw * (total_mob * drawdown); - // compute volume ratio between connection and at standard conditions Value volume_ratio = 0.0; if (FluidSystem::phaseIsActive(FluidSystem::waterPhaseIdx)) { @@ -871,9 +868,9 @@ namespace Opm } } // injecting connections total volumerates at standard conditions - Value cqt_is = cqt_i / volume_ratio; - for (int comp_idx = 0; comp_idx < this->numComponents(); ++comp_idx) { - cq_s[comp_idx] = cmix_s[comp_idx] * cqt_is; + const Value factor = total_mob / volume_ratio; + for (int componentIdx = 0; componentIdx < this->numComponents(); ++componentIdx) { + cq_s[componentIdx] = - Tw[componentIdx] * cmix_s[componentIdx] * factor * drawdown; } } // end for injection perforations @@ -907,7 +904,7 @@ namespace Opm MultisegmentWell:: computePerfRate(const IntensiveQuantities& int_quants, const std::vector& mob_perfcells, - const double Tw, + const std::vector& Tw, const int seg, const int perf, const Value& segment_pressure, @@ -1182,11 +1179,11 @@ namespace Opm // the well index associated with the connection const double trans_mult = ebos_simulator.problem().template wellTransMultiplier(int_quantities, cell_idx); const auto& wellstate_nupcol = ebos_simulator.problem().wellModel().nupcolWellState().well(this->index_of_well_); - const double tw_perf = this->wellIndex(perf, int_quantities, trans_mult, wellstate_nupcol); + const std::vector tw_perf = this->wellIndex(perf, int_quantities, trans_mult, wellstate_nupcol); std::vector ipr_a_perf(this->ipr_a_.size()); std::vector ipr_b_perf(this->ipr_b_.size()); for (int comp_idx = 0; comp_idx < this->num_components_; ++comp_idx) { - const double tw_mob = tw_perf * mob[comp_idx] * b_perf[comp_idx]; + const double tw_mob = tw_perf[comp_idx] * mob[comp_idx] * b_perf[comp_idx]; ipr_a_perf[comp_idx] += tw_mob * pressure_diff; ipr_b_perf[comp_idx] += tw_mob; } @@ -1682,7 +1679,7 @@ namespace Opm getMobility(ebosSimulator, perf, mob, deferred_logger); const double trans_mult = ebosSimulator.problem().template wellTransMultiplier(int_quants, cell_idx); const auto& wellstate_nupcol = ebosSimulator.problem().wellModel().nupcolWellState().well(this->index_of_well_); - const double Tw = this->wellIndex(perf, int_quants, trans_mult, wellstate_nupcol); + const std::vector Tw = this->wellIndex(perf, int_quants, trans_mult, wellstate_nupcol); std::vector cq_s(this->num_components_, 0.0); EvalWell perf_press; PerforationRates perfRates; @@ -1995,7 +1992,7 @@ namespace Opm getMobility(ebosSimulator, perf, mob, deferred_logger); const double trans_mult = ebosSimulator.problem().template wellTransMultiplier(int_quants, cell_idx); const auto& wellstate_nupcol = ebosSimulator.problem().wellModel().nupcolWellState().well(this->index_of_well_); - const double Tw = this->wellIndex(perf, int_quants, trans_mult, wellstate_nupcol); + const std::vector Tw = this->wellIndex(perf, int_quants, trans_mult, wellstate_nupcol); std::vector cq_s(this->num_components_, 0.0); Scalar perf_press = 0.0; PerforationRates perf_rates; diff --git a/opm/simulators/wells/StandardWell.hpp b/opm/simulators/wells/StandardWell.hpp index 728ecb8d9..a19379b7b 100644 --- a/opm/simulators/wells/StandardWell.hpp +++ b/opm/simulators/wells/StandardWell.hpp @@ -286,7 +286,7 @@ namespace Opm void computePerfRate(const IntensiveQuantities& intQuants, const std::vector& mob, const Value& bhp, - const double Tw, + const std::vector& Tw, const int perf, const bool allow_cf, std::vector& cq_s, @@ -302,7 +302,7 @@ namespace Opm const Value& rvw, const Value& rsw, std::vector& b_perfcells_dense, - const double Tw, + const std::vector& Tw, const int perf, const bool allow_cf, const Value& skin_pressure, diff --git a/opm/simulators/wells/StandardWell_impl.hpp b/opm/simulators/wells/StandardWell_impl.hpp index 4cb54f2ef..8bb99b2fe 100644 --- a/opm/simulators/wells/StandardWell_impl.hpp +++ b/opm/simulators/wells/StandardWell_impl.hpp @@ -119,7 +119,7 @@ namespace Opm computePerfRate(const IntensiveQuantities& intQuants, const std::vector& mob, const Value& bhp, - const double Tw, + const std::vector& Tw, const int perf, const bool allow_cf, std::vector& cq_s, @@ -224,7 +224,7 @@ namespace Opm const Value& rvw, const Value& rsw, std::vector& b_perfcells_dense, - const double Tw, + const std::vector& Tw, const int perf, const bool allow_cf, const Value& skin_pressure, @@ -249,7 +249,7 @@ namespace Opm // compute component volumetric rates at standard conditions for (int componentIdx = 0; componentIdx < this->numComponents(); ++componentIdx) { - const Value cq_p = - Tw * (mob[componentIdx] * drawdown); + const Value cq_p = - Tw[componentIdx] * (mob[componentIdx] * drawdown); cq_s[componentIdx] = b_perfcells_dense[componentIdx] * cq_p; } @@ -270,9 +270,6 @@ namespace Opm total_mob_dense += mob[componentIdx]; } - // injection perforations total volume rates - const Value cqt_i = - Tw * (total_mob_dense * drawdown); - // compute volume ratio between connection at standard conditions Value volumeRatio = bhp * 0.0; // initialize it with the correct type ; @@ -306,9 +303,9 @@ namespace Opm } // injecting connections total volumerates at standard conditions - Value cqt_is = cqt_i / volumeRatio; + const Value factor = total_mob_dense / volumeRatio; for (int componentIdx = 0; componentIdx < this->numComponents(); ++componentIdx) { - cq_s[componentIdx] = cmix_s[componentIdx] * cqt_is; + cq_s[componentIdx] = - Tw[componentIdx] * cmix_s[componentIdx] * factor * drawdown; } // calculating the perforation solution gas rate and solution oil rates @@ -496,7 +493,7 @@ namespace Opm PerforationRates perf_rates; double trans_mult = ebosSimulator.problem().template wellTransMultiplier(intQuants, cell_idx); const auto& wellstate_nupcol = ebosSimulator.problem().wellModel().nupcolWellState().well(this->index_of_well_); - const double Tw = this->wellIndex(perf, intQuants, trans_mult, wellstate_nupcol); + const std::vector Tw = this->wellIndex(perf, intQuants, trans_mult, wellstate_nupcol); computePerfRate(intQuants, mob, bhp, Tw, perf, allow_cf, cq_s, perf_rates, deferred_logger); @@ -790,12 +787,13 @@ namespace Opm } // the well index associated with the connection - const double tw_perf = this->well_index_[perf]*ebos_simulator.problem().template wellTransMultiplier(int_quantities, cell_idx); - + double trans_mult = ebos_simulator.problem().template wellTransMultiplier(int_quantities, cell_idx); + const auto& wellstate_nupcol = ebos_simulator.problem().wellModel().nupcolWellState().well(this->index_of_well_); + const std::vector tw_perf = this->wellIndex(perf, int_quantities, trans_mult, wellstate_nupcol); std::vector ipr_a_perf(this->ipr_a_.size()); std::vector ipr_b_perf(this->ipr_b_.size()); for (int comp_idx = 0; comp_idx < this->num_components_; ++comp_idx) { - const double tw_mob = tw_perf * mob[comp_idx] * b_perf[comp_idx]; + const double tw_mob = tw_perf[comp_idx] * mob[comp_idx] * b_perf[comp_idx]; ipr_a_perf[comp_idx] += tw_mob * pressure_diff; ipr_b_perf[comp_idx] += tw_mob; } @@ -1366,7 +1364,7 @@ namespace Opm getMobility(ebosSimulator, perf, mob, deferred_logger); double trans_mult = ebosSimulator.problem().template wellTransMultiplier(intQuants, cell_idx); const auto& wellstate_nupcol = ebosSimulator.problem().wellModel().nupcolWellState().well(this->index_of_well_); - const double Tw = this->wellIndex(perf, intQuants, trans_mult, wellstate_nupcol); + const std::vector Tw = this->wellIndex(perf, intQuants, trans_mult, wellstate_nupcol); std::vector cq_s(this->num_components_, 0.); PerforationRates perf_rates; @@ -1669,7 +1667,8 @@ namespace Opm std::vector cq_s(this->num_components_, {this->primary_variables_.numWellEq() + Indices::numEq, 0.}); PerforationRates perf_rates; double trans_mult = ebos_simulator.problem().template wellTransMultiplier(int_quant, cell_idx); - const double Tw = this->well_index_[perf] * trans_mult; + const auto& wellstate_nupcol = ebos_simulator.problem().wellModel().nupcolWellState().well(this->index_of_well_); + const std::vector Tw = this->wellIndex(perf, int_quant, trans_mult, wellstate_nupcol); computePerfRate(int_quant, mob, bhp, Tw, perf, allow_cf, cq_s, perf_rates, deferred_logger); // TODO: make area a member @@ -2274,7 +2273,7 @@ namespace Opm std::vector cq_s(this->num_components_, 0.); double trans_mult = ebosSimulator.problem().template wellTransMultiplier(intQuants, cell_idx); const auto& wellstate_nupcol = ebosSimulator.problem().wellModel().nupcolWellState().well(this->index_of_well_); - const double Tw = this->wellIndex(perf, intQuants, trans_mult, wellstate_nupcol); + const std::vector Tw = this->wellIndex(perf, intQuants, trans_mult, wellstate_nupcol); PerforationRates perf_rates; computePerfRate(intQuants, mob, bhp.value(), Tw, perf, allow_cf, cq_s, perf_rates, deferred_logger); diff --git a/opm/simulators/wells/WellInterface.hpp b/opm/simulators/wells/WellInterface.hpp index af325b6c7..9010e775c 100644 --- a/opm/simulators/wells/WellInterface.hpp +++ b/opm/simulators/wells/WellInterface.hpp @@ -349,7 +349,7 @@ public: return 0; } - double wellIndex(const int perf, const IntensiveQuantities& intQuants, const double trans_mult, const SingleWellState& ws) const; + std::vector wellIndex(const int perf, const IntensiveQuantities& intQuants, const double trans_mult, const SingleWellState& ws) const; void updateConnectionDFactor(const Simulator& simulator, SingleWellState& ws) const; diff --git a/opm/simulators/wells/WellInterface_impl.hpp b/opm/simulators/wells/WellInterface_impl.hpp index 247e53b01..d4d548077 100644 --- a/opm/simulators/wells/WellInterface_impl.hpp +++ b/opm/simulators/wells/WellInterface_impl.hpp @@ -1324,28 +1324,35 @@ namespace Opm } template - double + std::vector WellInterface:: wellIndex(const int perf, const IntensiveQuantities& intQuants, const double trans_mult, const SingleWellState& ws) const { + std::vector wi(this->num_components_, this->well_index_[perf] * trans_mult); const auto& wdfac = this->well_ecl_.getWDFAC(); if (!wdfac.useDFactor()) { - return this->well_index_[perf] * trans_mult; + return wi; } if constexpr (! Indices::gasEnabled) { - return this->well_index_[perf] * trans_mult; + return wi; } // closed connection are still closed if (this->well_index_[perf] == 0) - return 0.0; + return std::vector(this->num_components_, 0.0); // for gas wells we may want to add a Forchheimer term if the WDFAC or WDFACCOR keyword is used const auto& connection = this->well_ecl_.getConnections()[ws.perf_data.ecl_index[perf]]; // viscosity is evaluated at connection pressure const double connection_pressure = ws.perf_data.pressure[perf]; - const double mu = FluidSystem::gasPvt().viscosity(this->pvtRegionIdx(), ws.temperature, connection_pressure, getValue(intQuants.fluidState().Rv()), getValue(intQuants.fluidState().Rvw())); + + const auto& rv = getValue(intQuants.fluidState().Rv()); + const double psat = FluidSystem::gasPvt().saturationPressure(this->pvtRegionIdx(), ws.temperature, rv); + const double mu = connection_pressure < psat ? + FluidSystem::gasPvt().saturatedViscosity(this->pvtRegionIdx(), ws.temperature, connection_pressure) : + FluidSystem::gasPvt().viscosity(this->pvtRegionIdx(), ws.temperature, connection_pressure, rv, getValue(intQuants.fluidState().Rvw())); + const double phi = getValue(intQuants.porosity()); //double k = connection.Kh()/h * trans_mult; double Kh = connection.Kh()* trans_mult; @@ -1357,7 +1364,11 @@ namespace Opm double d = wdfac.useConnectionDFactor()? connection.dFactor() : wdfac.getDFactor(rho, mu, Ke, phi, rw, h); const PhaseUsage& pu = this->phaseUsage(); double Q = std::abs(ws.perf_data.phase_rates[perf*pu.num_phases + pu.phase_pos[Gas]]); - return 1.0/(1.0/(trans_mult * this->well_index_[perf]) + (Q/2 * d / scaling)); + + const unsigned gas_comp_idx = Indices::canonicalToActiveComponentIndex(FluidSystem::gasCompIdx); + wi[gas_comp_idx] = 1.0/(1.0/(trans_mult * this->well_index_[perf]) + (Q/2 * d / scaling)); + // solvent??? + return wi; } template From 82cb3c3b53e92293002916ff7ce0921dd9000836 Mon Sep 17 00:00:00 2001 From: Tor Harald Sandve Date: Tue, 14 Nov 2023 14:06:23 +0100 Subject: [PATCH 2/2] Fix connection and well dfactor The well dfactor is scaled by the well index If postive the connection dfactor is threated as a well factor and also scaled. If negative the connection dfactor is not scaled --- .../wells/MultisegmentWell_impl.hpp | 5 +- opm/simulators/wells/StandardWell_impl.hpp | 5 +- opm/simulators/wells/WellInterface.hpp | 2 + opm/simulators/wells/WellInterface_impl.hpp | 85 +++++++++++-------- 4 files changed, 57 insertions(+), 40 deletions(-) diff --git a/opm/simulators/wells/MultisegmentWell_impl.hpp b/opm/simulators/wells/MultisegmentWell_impl.hpp index fd032bd23..838df75f3 100644 --- a/opm/simulators/wells/MultisegmentWell_impl.hpp +++ b/opm/simulators/wells/MultisegmentWell_impl.hpp @@ -868,9 +868,10 @@ namespace Opm } } // injecting connections total volumerates at standard conditions - const Value factor = total_mob / volume_ratio; for (int componentIdx = 0; componentIdx < this->numComponents(); ++componentIdx) { - cq_s[componentIdx] = - Tw[componentIdx] * cmix_s[componentIdx] * factor * drawdown; + const Value cqt_i = - Tw[componentIdx] * (total_mob * drawdown); + Value cqt_is = cqt_i / volume_ratio; + cq_s[componentIdx] = cmix_s[componentIdx] * cqt_is; } } // end for injection perforations diff --git a/opm/simulators/wells/StandardWell_impl.hpp b/opm/simulators/wells/StandardWell_impl.hpp index 8bb99b2fe..320a4e26a 100644 --- a/opm/simulators/wells/StandardWell_impl.hpp +++ b/opm/simulators/wells/StandardWell_impl.hpp @@ -303,9 +303,10 @@ namespace Opm } // injecting connections total volumerates at standard conditions - const Value factor = total_mob_dense / volumeRatio; for (int componentIdx = 0; componentIdx < this->numComponents(); ++componentIdx) { - cq_s[componentIdx] = - Tw[componentIdx] * cmix_s[componentIdx] * factor * drawdown; + const Value cqt_i = - Tw[componentIdx] * (total_mob_dense * drawdown); + Value cqt_is = cqt_i / volumeRatio; + cq_s[componentIdx] = cmix_s[componentIdx] * cqt_is; } // calculating the perforation solution gas rate and solution oil rates diff --git a/opm/simulators/wells/WellInterface.hpp b/opm/simulators/wells/WellInterface.hpp index 9010e775c..e13c0cdb5 100644 --- a/opm/simulators/wells/WellInterface.hpp +++ b/opm/simulators/wells/WellInterface.hpp @@ -447,6 +447,8 @@ protected: double* connII, DeferredLogger& deferred_logger) const; + double computeConnectionDFactor(const int perf, const IntensiveQuantities& intQuants, const double trans_mult, const double total_tw, const SingleWellState& ws) const; + }; diff --git a/opm/simulators/wells/WellInterface_impl.hpp b/opm/simulators/wells/WellInterface_impl.hpp index d4d548077..29fb091e0 100644 --- a/opm/simulators/wells/WellInterface_impl.hpp +++ b/opm/simulators/wells/WellInterface_impl.hpp @@ -1333,41 +1333,27 @@ namespace Opm if (!wdfac.useDFactor()) { return wi; } - + // for gas wells we may want to add a Forchheimer term if the WDFAC or WDFACCOR keyword is used if constexpr (! Indices::gasEnabled) { return wi; } - // closed connection are still closed if (this->well_index_[perf] == 0) return std::vector(this->num_components_, 0.0); - // for gas wells we may want to add a Forchheimer term if the WDFAC or WDFACCOR keyword is used - const auto& connection = this->well_ecl_.getConnections()[ws.perf_data.ecl_index[perf]]; - // viscosity is evaluated at connection pressure - const double connection_pressure = ws.perf_data.pressure[perf]; + double tot_tw = 0.0; + for (const auto& c : this->well_ecl_.getConnections()) { + tot_tw += c.CF(); + } - const auto& rv = getValue(intQuants.fluidState().Rv()); - const double psat = FluidSystem::gasPvt().saturationPressure(this->pvtRegionIdx(), ws.temperature, rv); - const double mu = connection_pressure < psat ? - FluidSystem::gasPvt().saturatedViscosity(this->pvtRegionIdx(), ws.temperature, connection_pressure) : - FluidSystem::gasPvt().viscosity(this->pvtRegionIdx(), ws.temperature, connection_pressure, rv, getValue(intQuants.fluidState().Rvw())); - - const double phi = getValue(intQuants.porosity()); - //double k = connection.Kh()/h * trans_mult; - double Kh = connection.Kh()* trans_mult; - double Ke = connection.Ke()* trans_mult; - double h = Kh / Ke; - double rw = connection.rw(); - double rho = FluidSystem::referenceDensity(FluidSystem::gasPhaseIdx, this->pvtRegionIdx()); - double scaling = 3.141592653589 * Kh; - double d = wdfac.useConnectionDFactor()? connection.dFactor() : wdfac.getDFactor(rho, mu, Ke, phi, rw, h); + double d = computeConnectionDFactor(perf, intQuants, trans_mult, tot_tw, ws); const PhaseUsage& pu = this->phaseUsage(); double Q = std::abs(ws.perf_data.phase_rates[perf*pu.num_phases + pu.phase_pos[Gas]]); - + const auto& connection = this->well_ecl_.getConnections()[ws.perf_data.ecl_index[perf]]; + double Kh = connection.Kh()* trans_mult; + double scaling = 3.141592653589 * Kh; const unsigned gas_comp_idx = Indices::canonicalToActiveComponentIndex(FluidSystem::gasCompIdx); wi[gas_comp_idx] = 1.0/(1.0/(trans_mult * this->well_index_[perf]) + (Q/2 * d / scaling)); - // solvent??? return wi; } @@ -1380,27 +1366,54 @@ namespace Opm if (!wdfac.useDFactor()) { return; } - double rho = FluidSystem::referenceDensity(FluidSystem::gasPhaseIdx, this->pvtRegionIdx()); auto& perf_data = ws.perf_data; - + double tot_tw = 0.0; + for (const auto& c : this->well_ecl_.getConnections()) { + tot_tw += c.CF(); + } for (int perf = 0; perf < this->number_of_perforations_; ++perf) { const int cell_idx = this->well_cells_[perf]; const auto& intQuants = simulator.model().intensiveQuantities(cell_idx, /*timeIdx=*/ 0); const double trans_mult = simulator.problem().template rockCompTransMultiplier(intQuants, cell_idx); - // viscosity is evaluated at connection pressure - const double connection_pressure = ws.perf_data.pressure[perf]; - const double mu = FluidSystem::gasPvt().viscosity(this->pvtRegionIdx(), ws.temperature, connection_pressure, getValue(intQuants.fluidState().Rv()), getValue(intQuants.fluidState().Rvw())); - const double phi = getValue(intQuants.porosity()); - const auto& connection = this->well_ecl_.getConnections()[perf_data.ecl_index[perf]]; - double Kh = connection.Kh()* trans_mult; - double Ke = connection.Ke()* trans_mult; - double h = Kh / Ke; - double rw = connection.rw(); - double d = wdfac.useConnectionDFactor()? connection.dFactor() : wdfac.getDFactor(rho, mu, Ke, phi, rw, h); - perf_data.connection_d_factor[perf] = d; + perf_data.connection_d_factor[perf] = computeConnectionDFactor(perf, intQuants, trans_mult, tot_tw, ws); } } + template + double + WellInterface:: + computeConnectionDFactor(const int perf, const IntensiveQuantities& intQuants, const double trans_mult, const double total_tw, const SingleWellState& ws) const { + const double connection_pressure = ws.perf_data.pressure[perf]; + // viscosity is evaluated at connection pressure + const auto& rv = getValue(intQuants.fluidState().Rv()); + const double psat = FluidSystem::gasPvt().saturationPressure(this->pvtRegionIdx(), ws.temperature, rv); + const double mu = connection_pressure < psat ? + FluidSystem::gasPvt().saturatedViscosity(this->pvtRegionIdx(), ws.temperature, connection_pressure) : + FluidSystem::gasPvt().viscosity(this->pvtRegionIdx(), ws.temperature, connection_pressure, rv, getValue(intQuants.fluidState().Rvw())); + double rho = FluidSystem::referenceDensity(FluidSystem::gasPhaseIdx, this->pvtRegionIdx()); + const double phi = getValue(intQuants.porosity()); + const auto& connection = this->well_ecl_.getConnections()[ws.perf_data.ecl_index[perf]]; + double Kh = connection.Kh()* trans_mult; + double Ke = connection.Ke()* trans_mult; + double h = Kh / Ke; + double rw = connection.rw(); + const auto& wdfac = this->well_ecl_.getWDFAC(); + if (wdfac.useConnectionDFactor()) { + double d = connection.dFactor(); + // If a negative d factor is set in COMPDAT individual connection d factors should be used directly. + if (d < 0) + return -d; + // If a positive d factor is set in COMPDAT the connection d factors is treated like a well d factor. + // and thus scaled with the well index + return d * total_tw / connection.CF(); + } else { + + double d = wdfac.getDFactor(rho, mu, Ke, phi, rw, h); + return d * total_tw / connection.CF(); + } + } + + template void WellInterface::