From 06c5c17c9687c4936bb47094b2d4da3560601222 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Atgeirr=20Fl=C3=B8=20Rasmussen?= Date: Fri, 14 Feb 2025 09:25:30 +0100 Subject: [PATCH 1/2] Remove explicit updating of well AD vars. Now that the AD (Evaluation) primary variables in the well classes are always updated whenever the value primary variables are, we no longer need to remember to explicitly update them, simplifying the code and enabling removal of functions. The init() function of the primary variable classes have been renamed to setEvaluationsFromValues() and made private. --- opm/simulators/wells/BlackoilWellModel.hpp | 5 ++--- opm/simulators/wells/BlackoilWellModelNldd.hpp | 2 -- .../wells/BlackoilWellModelNldd_impl.hpp | 13 ------------- opm/simulators/wells/BlackoilWellModel_impl.hpp | 16 ---------------- opm/simulators/wells/MultisegmentWell.hpp | 2 -- .../wells/MultisegmentWellPrimaryVariables.cpp | 6 +++--- .../wells/MultisegmentWellPrimaryVariables.hpp | 6 +++--- opm/simulators/wells/MultisegmentWell_impl.hpp | 15 --------------- opm/simulators/wells/StandardWell.hpp | 2 -- .../wells/StandardWellPrimaryVariables.cpp | 8 ++++---- .../wells/StandardWellPrimaryVariables.hpp | 6 +++--- opm/simulators/wells/StandardWell_impl.hpp | 16 ---------------- opm/simulators/wells/WellInterface.hpp | 2 -- opm/simulators/wells/WellInterface_impl.hpp | 1 - tests/test_glift1.cpp | 1 - 15 files changed, 15 insertions(+), 86 deletions(-) diff --git a/opm/simulators/wells/BlackoilWellModel.hpp b/opm/simulators/wells/BlackoilWellModel.hpp index c5b665ff6..45fc3fffb 100644 --- a/opm/simulators/wells/BlackoilWellModel.hpp +++ b/opm/simulators/wells/BlackoilWellModel.hpp @@ -257,8 +257,8 @@ template class WellContributions; // called at the beginning of a report step void beginReportStep(const int time_step); - // it should be able to go to prepareTimeStep(), however, the updateWellControls() and initPrimaryVariablesEvaluation() - // makes it a little more difficult. unless we introduce if (iterationIdx != 0) to avoid doing the above functions + // it should be able to go to prepareTimeStep(), however, the updateWellControls() + // makes it a little more difficult. unless we introduce if (iterationIdx != 0) to avoid doing the above function // twice at the beginning of the time step /// Calculating the explict quantities used in the well calculation. By explicit, we mean they are cacluated /// at the beginning of the time step and no derivatives are included in these quantities @@ -266,7 +266,6 @@ template class WellContributions; // some preparation work, mostly related to group control and RESV, // at the beginning of each time step (Not report step) void prepareTimeStep(DeferredLogger& deferred_logger); - void initPrimaryVariablesEvaluation() const; std::pair updateWellControls(const bool mandatory_network_balance, DeferredLogger& deferred_logger, const bool relax_network_tolerance = false); diff --git a/opm/simulators/wells/BlackoilWellModelNldd.hpp b/opm/simulators/wells/BlackoilWellModelNldd.hpp index fe0025cf8..5975624f6 100644 --- a/opm/simulators/wells/BlackoilWellModelNldd.hpp +++ b/opm/simulators/wells/BlackoilWellModelNldd.hpp @@ -91,8 +91,6 @@ public: , wellModel_(model) {} - void initPrimaryVariablesEvaluation(const Domain& domain) const; - void addWellPressureEquations(PressureMatrix& jacobian, const BVector& weights, const bool use_well_weights, diff --git a/opm/simulators/wells/BlackoilWellModelNldd_impl.hpp b/opm/simulators/wells/BlackoilWellModelNldd_impl.hpp index 6ae4a5d9b..42f8fe909 100644 --- a/opm/simulators/wells/BlackoilWellModelNldd_impl.hpp +++ b/opm/simulators/wells/BlackoilWellModelNldd_impl.hpp @@ -47,7 +47,6 @@ assemble(const int /*iterationIdx*/, DeferredLogger local_deferredLogger; this->updateWellControls(local_deferredLogger, domain); - this->initPrimaryVariablesEvaluation(domain); this->assembleWellEq(dt, domain, local_deferredLogger); } @@ -125,18 +124,6 @@ recoverWellSolutionAndUpdateWellState(const BVector& x, } } -template -void -BlackoilWellModelNldd:: -initPrimaryVariablesEvaluation(const Domain& domain) const -{ - for (auto& well : wellModel_.localNonshutWells()) { - if (this->well_domain().at(well->name()) == domain.index) { - well->initPrimaryVariablesEvaluation(); - } - } -} - template ConvergenceReport BlackoilWellModelNldd:: diff --git a/opm/simulators/wells/BlackoilWellModel_impl.hpp b/opm/simulators/wells/BlackoilWellModel_impl.hpp index 7883d0b16..4a49e298f 100644 --- a/opm/simulators/wells/BlackoilWellModel_impl.hpp +++ b/opm/simulators/wells/BlackoilWellModel_impl.hpp @@ -1103,7 +1103,6 @@ namespace Opm { } OPM_END_PARALLEL_TRY_CATCH("BlackoilWellModel::doPreStepNetworkRebalance() failed: ", this->simulator_.vanguard().grid().comm()); - this->initPrimaryVariablesEvaluation(); } while (iter < max_iter); if (!converged) { @@ -1239,9 +1238,6 @@ namespace Opm { bool alq_updated = false; OPM_BEGIN_PARALLEL_TRY_CATCH(); { - // Set the well primary variables based on the value of well solutions - initPrimaryVariablesEvaluation(); - alq_updated = gaslift_.maybeDoGasLiftOptimize(simulator_, well_container_, this->wellState(), @@ -1661,17 +1657,6 @@ namespace Opm { } - template - void - BlackoilWellModel:: - initPrimaryVariablesEvaluation() const - { - for (auto& well : well_container_) { - well->initPrimaryVariablesEvaluation(); - } - } - - template ConvergenceReport BlackoilWellModel:: @@ -2084,7 +2069,6 @@ namespace Opm { if (events.hasEvent(WellState::event_mask)) { well->updateWellStateWithTarget(simulator_, this->groupState(), this->wellState(), deferred_logger); well->updatePrimaryVariables(simulator_, this->wellState(), deferred_logger); - well->initPrimaryVariablesEvaluation(); // There is no new well control change input within a report step, // so next time step, the well does not consider to have effective events anymore. events.clearEvent(WellState::event_mask); diff --git a/opm/simulators/wells/MultisegmentWell.hpp b/opm/simulators/wells/MultisegmentWell.hpp index 10f444caa..9d56f1446 100644 --- a/opm/simulators/wells/MultisegmentWell.hpp +++ b/opm/simulators/wells/MultisegmentWell.hpp @@ -86,8 +86,6 @@ namespace Opm { const std::vector& B_avg, const bool changed_to_open_this_step) override; - void initPrimaryVariablesEvaluation() override; - /// updating the well state based the current control mode void updateWellStateWithTarget(const Simulator& simulator, const GroupState& group_state, diff --git a/opm/simulators/wells/MultisegmentWellPrimaryVariables.cpp b/opm/simulators/wells/MultisegmentWellPrimaryVariables.cpp index 233d9c48c..6ceb1b493 100644 --- a/opm/simulators/wells/MultisegmentWellPrimaryVariables.cpp +++ b/opm/simulators/wells/MultisegmentWellPrimaryVariables.cpp @@ -54,7 +54,7 @@ resize(const int numSegments) template void MultisegmentWellPrimaryVariables:: -init() +setEvaluationsFromValues() { for (std::size_t seg = 0; seg < value_.size(); ++seg) { for (int eq_idx = 0; eq_idx < numWellEq; ++eq_idx) { @@ -152,7 +152,7 @@ update(const WellState& well_state, } } } - init(); + setEvaluationsFromValues(); } template @@ -209,7 +209,7 @@ updateNewton(const BVectorWell& dwells, if (stop_or_zero_rate_target) { value_[0][WQTotal] = 0.; } - init(); + setEvaluationsFromValues(); } template diff --git a/opm/simulators/wells/MultisegmentWellPrimaryVariables.hpp b/opm/simulators/wells/MultisegmentWellPrimaryVariables.hpp index 951d34b4c..9a94302b0 100644 --- a/opm/simulators/wells/MultisegmentWellPrimaryVariables.hpp +++ b/opm/simulators/wells/MultisegmentWellPrimaryVariables.hpp @@ -81,9 +81,6 @@ public: //! \brief Resize values and evaluations. void resize(const int numSegments); - //! \brief Initialize evaluations from values. - void init(); - //! \brief Copy values from well state. void update(const WellState& well_state, const bool stop_or_zero_rate_target); @@ -149,6 +146,9 @@ public: void outputLowLimitPressureSegments(DeferredLogger& deferred_logger) const; private: + //! \brief Initialize evaluations from values. + void setEvaluationsFromValues(); + //! \brief Handle non-reasonable fractions due to numerical overshoot. void processFractions(const int seg); diff --git a/opm/simulators/wells/MultisegmentWell_impl.hpp b/opm/simulators/wells/MultisegmentWell_impl.hpp index 95e65dd88..44f93b7dc 100644 --- a/opm/simulators/wells/MultisegmentWell_impl.hpp +++ b/opm/simulators/wells/MultisegmentWell_impl.hpp @@ -147,18 +147,6 @@ namespace Opm - template - void - MultisegmentWell:: - initPrimaryVariablesEvaluation() - { - this->primary_variables_.init(); - } - - - - - template void MultisegmentWell:: @@ -732,7 +720,6 @@ namespace Opm DeferredLogger& deferred_logger) { updatePrimaryVariables(simulator, well_state, deferred_logger); - initPrimaryVariablesEvaluation(); computePerfCellPressDiffs(simulator); computeInitialSegmentFluids(simulator); } @@ -1582,7 +1569,6 @@ namespace Opm deferred_logger.debug(sstr.str()); } updateWellState(simulator, dx_well, well_state, deferred_logger, relaxation_factor); - initPrimaryVariablesEvaluation(); } // TODO: we should decide whether to keep the updated well_state, or recover to use the old well_state @@ -1774,7 +1760,6 @@ namespace Opm } } updateWellState(simulator, dx_well, well_state, deferred_logger, relaxation_factor); - initPrimaryVariablesEvaluation(); } if (converged) { diff --git a/opm/simulators/wells/StandardWell.hpp b/opm/simulators/wells/StandardWell.hpp index e33fb1a83..2efebea76 100644 --- a/opm/simulators/wells/StandardWell.hpp +++ b/opm/simulators/wells/StandardWell.hpp @@ -137,8 +137,6 @@ namespace Opm const std::vector& B_avg, const bool changed_to_open_this_step) override; - void initPrimaryVariablesEvaluation() override; - /// check whether the well equations get converged for this well virtual ConvergenceReport getWellConvergence(const Simulator& simulator, const WellState& well_state, diff --git a/opm/simulators/wells/StandardWellPrimaryVariables.cpp b/opm/simulators/wells/StandardWellPrimaryVariables.cpp index b6b13bf01..eb007641e 100644 --- a/opm/simulators/wells/StandardWellPrimaryVariables.cpp +++ b/opm/simulators/wells/StandardWellPrimaryVariables.cpp @@ -97,7 +97,7 @@ namespace Opm { template void StandardWellPrimaryVariables:: -init() +setEvaluationsFromValues() { for (int eqIdx = 0; eqIdx < numWellEq_; ++eqIdx) { evaluation_[eqIdx] = @@ -223,7 +223,7 @@ update(const WellState& well_state, // BHP value_[Bhp] = ws.bhp; - init(); + setEvaluationsFromValues(); } template @@ -302,7 +302,7 @@ updateNewton(const BVectorWell& dwells, // so that bhp constaint can be an active control when needed. constexpr Scalar bhp_lower_limit = 1. * unit::barsa - 1. * unit::Pascal; value_[Bhp] = std::max(value_[Bhp] - dx1_limited, bhp_lower_limit); - init(); + setEvaluationsFromValues(); } template @@ -322,7 +322,7 @@ updateNewtonPolyMW(const BVectorWell& dwells) value_[pskin_index] -= relaxation_factor * dx_pskin; } } - init(); + setEvaluationsFromValues(); } template diff --git a/opm/simulators/wells/StandardWellPrimaryVariables.hpp b/opm/simulators/wells/StandardWellPrimaryVariables.hpp index 7c0c551ac..ac7d4877d 100644 --- a/opm/simulators/wells/StandardWellPrimaryVariables.hpp +++ b/opm/simulators/wells/StandardWellPrimaryVariables.hpp @@ -92,9 +92,6 @@ public: : well_(well) {} - //! \brief Initialize evaluations from values. - void init(); - //! \brief Resize values and evaluations. void resize(const int numWellEq); @@ -151,6 +148,9 @@ public: { value_[idx] = val; } private: + //! \brief Initialize evaluations from values. + void setEvaluationsFromValues(); + //! \brief Calculate a relaxation factor for producers. //! \details To avoid overshoot of the fractions which might result in negative rates. Scalar relaxationFactorFractionsProducer(const BVectorWell& dwells, diff --git a/opm/simulators/wells/StandardWell_impl.hpp b/opm/simulators/wells/StandardWell_impl.hpp index 809b70f8f..b0f4cb0f2 100644 --- a/opm/simulators/wells/StandardWell_impl.hpp +++ b/opm/simulators/wells/StandardWell_impl.hpp @@ -87,17 +87,6 @@ namespace Opm - template - void StandardWell:: - initPrimaryVariablesEvaluation() - { - this->primary_variables_.init(); - } - - - - - template template void @@ -1412,7 +1401,6 @@ namespace Opm DeferredLogger& deferred_logger) { updatePrimaryVariables(simulator, well_state, deferred_logger); - initPrimaryVariablesEvaluation(); computeWellConnectionPressures(simulator, well_state, deferred_logger); this->computeAccumWell(); } @@ -1564,7 +1552,6 @@ namespace Opm = sign * ws.well_potentials[phase]; } well_copy.updatePrimaryVariables(simulator, well_state_copy, deferred_logger); - well_copy.initPrimaryVariablesEvaluation(); well_copy.computeAccumWell(); const double dt = simulator.timeStepSize(); @@ -1576,7 +1563,6 @@ namespace Opm } well_copy.updatePrimaryVariables(simulator, well_state_copy, deferred_logger); well_copy.computeWellConnectionPressures(simulator, well_state_copy, deferred_logger); - well_copy.initPrimaryVariablesEvaluation(); well_copy.computeWellRatesWithBhp(simulator, bhp, well_flux, deferred_logger); } @@ -2382,7 +2368,6 @@ namespace Opm // this function or we use different functions for the well testing purposes. // We don't allow for switching well controls while computing well potentials and testing wells // updateWellControl(simulator, well_state, deferred_logger); - initPrimaryVariablesEvaluation(); } while (it < max_iter); return converged; @@ -2478,7 +2463,6 @@ namespace Opm ++it; solveEqAndUpdateWellState(simulator, well_state, deferred_logger); - initPrimaryVariablesEvaluation(); } while (it < max_iter); diff --git a/opm/simulators/wells/WellInterface.hpp b/opm/simulators/wells/WellInterface.hpp index 60ea9ee70..9def675f0 100644 --- a/opm/simulators/wells/WellInterface.hpp +++ b/opm/simulators/wells/WellInterface.hpp @@ -151,8 +151,6 @@ public: const std::vector& B_avg, const bool changed_to_open_this_step); - virtual void initPrimaryVariablesEvaluation() = 0; - virtual ConvergenceReport getWellConvergence(const Simulator& simulator, const WellState& well_state, const std::vector& B_avg, diff --git a/opm/simulators/wells/WellInterface_impl.hpp b/opm/simulators/wells/WellInterface_impl.hpp index bf01c5abd..afac48a19 100644 --- a/opm/simulators/wells/WellInterface_impl.hpp +++ b/opm/simulators/wells/WellInterface_impl.hpp @@ -400,7 +400,6 @@ namespace Opm updateWellStateWithTarget(simulator, group_state, well_state_copy, deferred_logger); calculateExplicitQuantities(simulator, well_state_copy, deferred_logger); updatePrimaryVariables(simulator, well_state_copy, deferred_logger); - initPrimaryVariablesEvaluation(); if (this->isProducer()) { const auto& schedule = simulator.vanguard().schedule(); diff --git a/tests/test_glift1.cpp b/tests/test_glift1.cpp index 9fc229502..b0ea4b036 100644 --- a/tests/test_glift1.cpp +++ b/tests/test_glift1.cpp @@ -153,7 +153,6 @@ BOOST_AUTO_TEST_CASE(G1) well_model.calculateExplicitQuantities(deferred_logger); well_model.prepareTimeStep(deferred_logger); well_model.updateWellControls(false, deferred_logger); - well_model.initPrimaryVariablesEvaluation(); Opm::WellInterface *well_ptr = well_model.getWell("B-1H").get(); StdWell *std_well = dynamic_cast(well_ptr); From 40043b0dc6c0b9971b267838ee447507edc7d534 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Atgeirr=20Fl=C3=B8=20Rasmussen?= Date: Fri, 14 Feb 2025 12:44:44 +0100 Subject: [PATCH 2/2] Bugfix: add call to setEvaluationsFromValues() in updatePolyMW(). --- opm/simulators/wells/StandardWellPrimaryVariables.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/opm/simulators/wells/StandardWellPrimaryVariables.cpp b/opm/simulators/wells/StandardWellPrimaryVariables.cpp index eb007641e..5f0e04675 100644 --- a/opm/simulators/wells/StandardWellPrimaryVariables.cpp +++ b/opm/simulators/wells/StandardWellPrimaryVariables.cpp @@ -240,6 +240,7 @@ updatePolyMW(const WellState& well_state) value_[Bhp + 1 + well_.numPerfs() + perf] = skin_pressure[perf]; } } + setEvaluationsFromValues(); } template