From 796aa51e79314908af47905bf09a75fc745220c7 Mon Sep 17 00:00:00 2001 From: Arne Morten Kvarving Date: Wed, 19 Oct 2022 09:55:14 +0200 Subject: [PATCH] changed: move calculateBhpFromThp to WellBhpThpCalculator --- opm/simulators/wells/MultisegmentWellEval.cpp | 14 +++- opm/simulators/wells/StandardWellEval.cpp | 14 +++- opm/simulators/wells/WellBhpThpCalculator.cpp | 79 +++++++++++++++++++ opm/simulators/wells/WellBhpThpCalculator.hpp | 9 +++ opm/simulators/wells/WellInterfaceEval.cpp | 66 +--------------- opm/simulators/wells/WellInterfaceEval.hpp | 8 -- opm/simulators/wells/WellInterface_impl.hpp | 15 +++- 7 files changed, 126 insertions(+), 79 deletions(-) diff --git a/opm/simulators/wells/MultisegmentWellEval.cpp b/opm/simulators/wells/MultisegmentWellEval.cpp index b766ca798..6edb9cd84 100644 --- a/opm/simulators/wells/MultisegmentWellEval.cpp +++ b/opm/simulators/wells/MultisegmentWellEval.cpp @@ -1229,7 +1229,12 @@ assembleControlEq(const WellState& well_state, // Setup function for evaluation of BHP from THP (used only if needed). auto bhp_from_thp = [&]() { const auto rates = getRates(); - return baseif_.calculateBhpFromThp(well_state, rates, well, summaryState, rho, deferred_logger); + return WellBhpThpCalculator(baseif_).calculateBhpFromThp(well_state, + rates, + well, + summaryState, + rho, + deferred_logger); }; // Call generic implementation. baseif_.assembleControlEqInj(well_state, @@ -1247,7 +1252,12 @@ assembleControlEq(const WellState& well_state, const auto rates = getRates(); // Setup function for evaluation of BHP from THP (used only if needed). auto bhp_from_thp = [&]() { - return baseif_.calculateBhpFromThp(well_state, rates, well, summaryState, rho, deferred_logger); + return WellBhpThpCalculator(baseif_).calculateBhpFromThp(well_state, + rates, + well, + summaryState, + rho, + deferred_logger); }; // Call generic implementation. baseif_.assembleControlEqProd(well_state, diff --git a/opm/simulators/wells/StandardWellEval.cpp b/opm/simulators/wells/StandardWellEval.cpp index 040998ebf..197cf14c5 100644 --- a/opm/simulators/wells/StandardWellEval.cpp +++ b/opm/simulators/wells/StandardWellEval.cpp @@ -394,7 +394,12 @@ assembleControlEq(const WellState& well_state, // Setup function for evaluation of BHP from THP (used only if needed). auto bhp_from_thp = [&]() { const auto rates = getRates(); - return baseif_.calculateBhpFromThp(well_state, rates, well, summaryState, this->getRho(), deferred_logger); + return WellBhpThpCalculator(baseif_).calculateBhpFromThp(well_state, + rates, + well, + summaryState, + this->getRho(), + deferred_logger); }; // Call generic implementation. const auto& inj_controls = well.injectionControls(summaryState); @@ -413,7 +418,12 @@ assembleControlEq(const WellState& well_state, const auto rates = getRates(); // Setup function for evaluation of BHP from THP (used only if needed). auto bhp_from_thp = [&]() { - return baseif_.calculateBhpFromThp(well_state, rates, well, summaryState, this->getRho(), deferred_logger); + return WellBhpThpCalculator(baseif_).calculateBhpFromThp(well_state, + rates, + well, + summaryState, + this->getRho(), + deferred_logger); }; // Call generic implementation. const auto& prod_controls = well.productionControls(summaryState); diff --git a/opm/simulators/wells/WellBhpThpCalculator.cpp b/opm/simulators/wells/WellBhpThpCalculator.cpp index 960c4aea3..2c419a950 100644 --- a/opm/simulators/wells/WellBhpThpCalculator.cpp +++ b/opm/simulators/wells/WellBhpThpCalculator.cpp @@ -29,6 +29,8 @@ #include #include +#include + #include #include @@ -259,6 +261,58 @@ void WellBhpThpCalculator::updateThp(const double rho, ws.thp = this->calculateThpFromBhp(rates, ws.bhp, rho, alq_value(), deferred_logger); } +template +EvalWell WellBhpThpCalculator:: +calculateBhpFromThp(const WellState& well_state, + const std::vector& rates, + const Well& well, + const SummaryState& summaryState, + const double rho, + DeferredLogger& deferred_logger) const +{ + // TODO: when well is under THP control, the BHP is dependent on the rates, + // the well rates is also dependent on the BHP, so it might need to do some iteration. + // However, when group control is involved, change of the rates might impacts other wells + // so iterations on a higher level will be required. Some investigation might be needed when + // we face problems under THP control. + + assert(int(rates.size()) == 3); // the vfp related only supports three phases now. + + static constexpr int Gas = BlackoilPhases::Vapour; + static constexpr int Oil = BlackoilPhases::Liquid; + static constexpr int Water = BlackoilPhases::Aqua; + + const EvalWell aqua = rates[Water]; + const EvalWell liquid = rates[Oil]; + const EvalWell vapour = rates[Gas]; + + // pick the reference density + // typically the reference in the top layer + if (well_.isInjector() ) + { + const auto& controls = well.injectionControls(summaryState); + const double vfp_ref_depth = well_.vfpProperties()->getInj()->getTable(controls.vfp_table_number).getDatumDepth(); + const double dp = wellhelpers::computeHydrostaticCorrection(well_.refDepth(), vfp_ref_depth, rho, well_.gravity()); + return well_.vfpProperties()->getInj()->bhp(controls.vfp_table_number, aqua, liquid, vapour, well_.getTHPConstraint(summaryState)) - dp; + } + else if (well_.isProducer()) { + const auto& controls = well.productionControls(summaryState); + const double vfp_ref_depth = well_.vfpProperties()->getProd()->getTable(controls.vfp_table_number).getDatumDepth(); + const double dp = wellhelpers::computeHydrostaticCorrection(well_.refDepth(), vfp_ref_depth, rho, well_.gravity()); + const auto& wfr = well_.vfpProperties()->getExplicitWFR(controls.vfp_table_number, well_.indexOfWell()); + const auto& gfr = well_.vfpProperties()->getExplicitGFR(controls.vfp_table_number, well_.indexOfWell()); + const bool use_vfpexplicit = well_.useVfpExplicit(); + return well_.vfpProperties()->getProd()->bhp(controls.vfp_table_number, + aqua, liquid, vapour, + well_.getTHPConstraint(summaryState), + well_.getALQ(well_state), + wfr, gfr, use_vfpexplicit) - dp; + } + else { + OPM_DEFLOG_THROW(std::logic_error, "Expected INJECTOR or PRODUCER for well " + well_.name(), deferred_logger); + } +} + template std::optional WellBhpThpCalculator:: @@ -704,4 +758,29 @@ bruteForceBracket(const std::function& eq, return finding_bracket; } +#define INSTANCE(...) \ +template __VA_ARGS__ WellBhpThpCalculator:: \ +calculateBhpFromThp<__VA_ARGS__>(const WellState&, \ + const std::vector<__VA_ARGS__>&, \ + const Well&, \ + const SummaryState&, \ + const double, \ + DeferredLogger&) const; + +INSTANCE(double) +INSTANCE(DenseAd::Evaluation) +INSTANCE(DenseAd::Evaluation) +INSTANCE(DenseAd::Evaluation) +INSTANCE(DenseAd::Evaluation) +INSTANCE(DenseAd::Evaluation) +INSTANCE(DenseAd::Evaluation) +INSTANCE(DenseAd::Evaluation) +INSTANCE(DenseAd::Evaluation) +INSTANCE(DenseAd::Evaluation) +INSTANCE(DenseAd::Evaluation) +INSTANCE(DenseAd::Evaluation) +INSTANCE(DenseAd::Evaluation) +INSTANCE(DenseAd::Evaluation) +INSTANCE(DenseAd::Evaluation) + } // namespace Opm diff --git a/opm/simulators/wells/WellBhpThpCalculator.hpp b/opm/simulators/wells/WellBhpThpCalculator.hpp index da1ed8b7f..5c35c670a 100644 --- a/opm/simulators/wells/WellBhpThpCalculator.hpp +++ b/opm/simulators/wells/WellBhpThpCalculator.hpp @@ -34,6 +34,7 @@ namespace Opm class DeferredLogger; class SummaryState; +class Well; class WellInterfaceGeneric; class WellState; @@ -85,6 +86,14 @@ public: WellState& well_state, DeferredLogger& deferred_logger) const; + template + EvalWell calculateBhpFromThp(const WellState& well_state, + const std::vector& rates, + const Well& well, + const SummaryState& summaryState, + const double rho, + DeferredLogger& deferred_logger) const; + private: //! \brief Compute BHP from THP limit for an injector - implementation. template diff --git a/opm/simulators/wells/WellInterfaceEval.cpp b/opm/simulators/wells/WellInterfaceEval.cpp index 7be74d96a..25473ca58 100644 --- a/opm/simulators/wells/WellInterfaceEval.cpp +++ b/opm/simulators/wells/WellInterfaceEval.cpp @@ -260,52 +260,6 @@ assembleControlEqInj_(const WellState& well_state, } } -template -template -EvalWell -WellInterfaceEval:: -calculateBhpFromThp(const WellState& well_state, - const std::vector& rates, - const Well& well, - const SummaryState& summaryState, - const double rho, - DeferredLogger& deferred_logger) const -{ - // TODO: when well is under THP control, the BHP is dependent on the rates, - // the well rates is also dependent on the BHP, so it might need to do some iteration. - // However, when group control is involved, change of the rates might impacts other wells - // so iterations on a higher level will be required. Some investigation might be needed when - // we face problems under THP control. - - assert(int(rates.size()) == 3); // the vfp related only supports three phases now. - - const EvalWell aqua = rates[Water]; - const EvalWell liquid = rates[Oil]; - const EvalWell vapour = rates[Gas]; - - // pick the reference density - // typically the reference in the top layer - if (baseif_.isInjector() ) - { - const auto& controls = well.injectionControls(summaryState); - const double vfp_ref_depth = baseif_.vfpProperties()->getInj()->getTable(controls.vfp_table_number).getDatumDepth(); - const double dp = wellhelpers::computeHydrostaticCorrection(baseif_.refDepth(), vfp_ref_depth, rho, baseif_.gravity()); - return baseif_.vfpProperties()->getInj()->bhp(controls.vfp_table_number, aqua, liquid, vapour, baseif_.getTHPConstraint(summaryState)) - dp; - } - else if (baseif_.isProducer()) { - const auto& controls = well.productionControls(summaryState); - const double vfp_ref_depth = baseif_.vfpProperties()->getProd()->getTable(controls.vfp_table_number).getDatumDepth(); - const double dp = wellhelpers::computeHydrostaticCorrection(baseif_.refDepth(), vfp_ref_depth, rho, baseif_.gravity()); - const auto& wfr = baseif_.vfpProperties()->getExplicitWFR(controls.vfp_table_number, baseif_.indexOfWell()); - const auto& gfr = baseif_.vfpProperties()->getExplicitGFR(controls.vfp_table_number, baseif_.indexOfWell()); - const bool use_vfpexplicit = baseif_.useVfpExplicit(); - return baseif_.vfpProperties()->getProd()->bhp(controls.vfp_table_number, aqua, liquid, vapour, baseif_.getTHPConstraint(summaryState), baseif_.getALQ(well_state), wfr, gfr, use_vfpexplicit) - dp; - } - else { - OPM_DEFLOG_THROW(std::logic_error, "Expected INJECTOR or PRODUCER for well " + baseif_.name(), deferred_logger); - } -} - #define INSTANCE_METHODS(A,...) \ template void WellInterfaceEval:: \ assembleControlEqProd_<__VA_ARGS__>(const WellState&, \ @@ -328,14 +282,7 @@ assembleControlEqInj_<__VA_ARGS__>(const WellState&, \ const __VA_ARGS__&, \ const std::function<__VA_ARGS__()>&, \ __VA_ARGS__&, \ - DeferredLogger&) const; \ -template __VA_ARGS__ WellInterfaceEval:: \ -calculateBhpFromThp<__VA_ARGS__>(const WellState&, \ - const std::vector<__VA_ARGS__>&, \ - const Well&, \ - const SummaryState&, \ - const double, \ - DeferredLogger&) const; + DeferredLogger&) const; using FluidSys = BlackOilFluidSystem; @@ -356,15 +303,4 @@ INSTANCE_METHODS(FluidSys, DenseAd::Evaluation) INSTANCE_METHODS(FluidSys, DenseAd::Evaluation) INSTANCE_METHODS(FluidSys, DenseAd::Evaluation) -#define INSTANCE_BHP(...) \ -template double WellInterfaceEval<__VA_ARGS__>:: \ -calculateBhpFromThp(const WellState&, \ - const std::vector&, \ - const Well&, \ - const SummaryState&, \ - const double, \ - DeferredLogger&) const; - -INSTANCE_BHP(FluidSys) - } // namespace Opm diff --git a/opm/simulators/wells/WellInterfaceEval.hpp b/opm/simulators/wells/WellInterfaceEval.hpp index 33159024f..7b4fabd10 100644 --- a/opm/simulators/wells/WellInterfaceEval.hpp +++ b/opm/simulators/wells/WellInterfaceEval.hpp @@ -49,14 +49,6 @@ class WellInterfaceEval { static constexpr int Gas = BlackoilPhases::Vapour; public: - template - EvalWell calculateBhpFromThp(const WellState& well_state, - const std::vector& rates, - const Well& well, - const SummaryState& summaryState, - const double rho, - DeferredLogger& deferred_logger) const; - template void assembleControlEqProd(const WellState& well_state, const GroupState& group_state, diff --git a/opm/simulators/wells/WellInterface_impl.hpp b/opm/simulators/wells/WellInterface_impl.hpp index 755a59467..66de98a7f 100644 --- a/opm/simulators/wells/WellInterface_impl.hpp +++ b/opm/simulators/wells/WellInterface_impl.hpp @@ -23,6 +23,7 @@ #include #include #include +#include #include @@ -811,7 +812,12 @@ namespace Opm case Well::InjectorCMode::THP: { auto rates = ws.surface_rates; - double bhp = this->calculateBhpFromThp(well_state, rates, well, summaryState, this->getRefDensity(), deferred_logger); + double bhp = WellBhpThpCalculator(*this).calculateBhpFromThp(well_state, + rates, + well, + summaryState, + this->getRefDensity(), + deferred_logger); ws.bhp = bhp; ws.thp = this->getTHPConstraint(summaryState); @@ -1036,7 +1042,12 @@ namespace Opm { auto rates = ws.surface_rates; this->adaptRatesForVFP(rates); - double bhp = this->calculateBhpFromThp(well_state, rates, well, summaryState, this->getRefDensity(), deferred_logger); + double bhp = WellBhpThpCalculator(*this).calculateBhpFromThp(well_state, + rates, + well, + summaryState, + this->getRefDensity(), + deferred_logger); ws.bhp = bhp; ws.thp = this->getTHPConstraint(summaryState);