From 389db91839dcb14a0968ca55649ea3da943a8fc3 Mon Sep 17 00:00:00 2001 From: Vegard Kippe Date: Fri, 22 Sep 2023 20:10:52 +0200 Subject: [PATCH] Slight restructuring/renaming for (possibly) improved clarity. --- opm/simulators/wells/BlackoilWellModel.hpp | 5 +-- .../wells/BlackoilWellModelGeneric.cpp | 39 ++++++++++++------- .../wells/BlackoilWellModelGeneric.hpp | 10 +++-- .../wells/BlackoilWellModel_impl.hpp | 20 ++++++---- 4 files changed, 45 insertions(+), 29 deletions(-) diff --git a/opm/simulators/wells/BlackoilWellModel.hpp b/opm/simulators/wells/BlackoilWellModel.hpp index 0a055746e..22301b072 100644 --- a/opm/simulators/wells/BlackoilWellModel.hpp +++ b/opm/simulators/wells/BlackoilWellModel.hpp @@ -406,9 +406,8 @@ namespace Opm { SimulatorReportSingle last_report_{}; - // solve to get a good network solution, group and well states might be updated during the process. - // the reservoir should stay static during this solution procedure. - void balanceNetwork(DeferredLogger& deferred_logger); + // Pre-step network solve at static reservoir conditions (group and well states might be updated) + void doPreStepNetworkRebalance(DeferredLogger& deferred_logger); // used to better efficiency of calcuation mutable BVector scaleAddRes_{}; diff --git a/opm/simulators/wells/BlackoilWellModelGeneric.cpp b/opm/simulators/wells/BlackoilWellModelGeneric.cpp index 4c754bdfe..36a6265a2 100644 --- a/opm/simulators/wells/BlackoilWellModelGeneric.cpp +++ b/opm/simulators/wells/BlackoilWellModelGeneric.cpp @@ -985,35 +985,46 @@ hasTHPConstraints() const return BlackoilWellModelConstraints(*this).hasTHPConstraints(); } -std::pair +void BlackoilWellModelGeneric:: -needRebalanceNetwork(const int report_step) const -{ +updateNetworkActiveState(const int report_step) { const auto& network = schedule()[report_step].network(); if (!network.active()) { - return {false, false}; + this->network_active_ = false; + return; } bool network_active = false; - bool network_rebalance_necessary = false; for (const auto& well : well_container_generic_) { const bool is_partof_network = network.has_node(well->wellEcl().groupName()); const bool prediction_mode = well->wellEcl().predictionMode(); if (is_partof_network && prediction_mode) { network_active = true; + break; } - // TODO: we might find more relevant events to be included here + } + this->network_active_ = comm_.max(network_active); +} + +bool +BlackoilWellModelGeneric:: +needPreStepNetworkRebalance(const int report_step) const +{ + assert(this->networkActive()); + + const auto& network = schedule()[report_step].network(); + bool network_rebalance_necessary = false; + for (const auto& well : well_container_generic_) { + const bool is_partof_network = network.has_node(well->wellEcl().groupName()); + // TODO: we might find more relevant events to be included here (including network change events?) const auto& events = this->wellState().well(well->indexOfWell()).events; if (is_partof_network && events.hasEvent(ScheduleEvents::WELL_STATUS_CHANGE)) { network_rebalance_necessary = true; - } - if (network_active && network_rebalance_necessary) break; + } } network_rebalance_necessary = comm_.max(network_rebalance_necessary); - network_active = comm_.max(network_active); - - return {network_active, network_rebalance_necessary}; + return network_rebalance_necessary; } bool @@ -1383,11 +1394,9 @@ bool BlackoilWellModelGeneric:: shouldBalanceNetwork(const int reportStepIdx, const int iterationIdx) const { - // if network is not active, we do not need to balance the network - const auto& network = schedule()[reportStepIdx].network(); - if (!network.active()) { + // If the network is not active now, we do not need to balance it. + if (!this->networkActive()) return false; - } const auto& balance = schedule()[reportStepIdx].network_balance(); if (balance.mode() == Network::Balance::CalcMode::TimeStepStart) { diff --git a/opm/simulators/wells/BlackoilWellModelGeneric.hpp b/opm/simulators/wells/BlackoilWellModelGeneric.hpp index bb6ab8303..a03497508 100644 --- a/opm/simulators/wells/BlackoilWellModelGeneric.hpp +++ b/opm/simulators/wells/BlackoilWellModelGeneric.hpp @@ -172,9 +172,13 @@ public: /// Return true if any well has a THP constraint. bool hasTHPConstraints() const; - /// Checks if network is active (at least one network well on prediction), - /// and whether it is necessary to re-balance the network - std::pair needRebalanceNetwork(const int report_step) const; + /// Checks if network is active (at least one network well on prediction). + void updateNetworkActiveState(const int report_step); + + /// Checks if there are reasons to perform a pre-step network re-balance. + /// (Currently, the only reasons are network well status changes.) + /// (TODO: Consider if adding network change events would be helpful.) + bool needPreStepNetworkRebalance(const int report_step) const; /// Shut down any single well /// Returns true if the well was actually found and shut. diff --git a/opm/simulators/wells/BlackoilWellModel_impl.hpp b/opm/simulators/wells/BlackoilWellModel_impl.hpp index 32951cbc7..9151c45e3 100644 --- a/opm/simulators/wells/BlackoilWellModel_impl.hpp +++ b/opm/simulators/wells/BlackoilWellModel_impl.hpp @@ -1006,7 +1006,7 @@ namespace Opm { template void BlackoilWellModel:: - balanceNetwork(DeferredLogger& deferred_logger) { + doPreStepNetworkRebalance(DeferredLogger& deferred_logger) { const double dt = this->ebosSimulator_.timeStepSize(); // TODO: should we also have the group and network backed-up here in case the solution did not get converged? auto& well_state = this->wellState(); @@ -2239,8 +2239,14 @@ namespace Opm { BlackoilWellModel:: prepareTimeStep(DeferredLogger& deferred_logger) { - bool network_rebalance_necessary{false}; - std::tie(this->network_active_, network_rebalance_necessary) = this->needRebalanceNetwork(ebosSimulator_.episodeIndex()); + // Check if there is a network with active prediction wells at this time step. + const auto episodeIdx = ebosSimulator_.episodeIndex(); + this->updateNetworkActiveState(episodeIdx); + + // Rebalance the network initially if any wells in the network have status changes + // (Need to check this before clearing events) + bool do_prestep_network_rebalance{false}; + if (this->networkActive()) do_prestep_network_rebalance = this->needPreStepNetworkRebalance(episodeIdx); for (const auto& well : well_container_) { auto& events = this->wellState().well(well->indexOfWell()).events; @@ -2258,7 +2264,7 @@ namespace Opm { try { well->solveWellEquation(ebosSimulator_, this->wellState(), this->groupState(), deferred_logger); } catch (const std::exception& e) { - const std::string msg = "Compute initial well solution for " + well->name() + " initially failed. Continue with the privious rates"; + const std::string msg = "Compute initial well solution for " + well->name() + " initially failed. Continue with the previous rates"; deferred_logger.warning("WELL_INITIAL_SOLVE_FAILED", msg); } } @@ -2268,10 +2274,8 @@ namespace Opm { } updatePrimaryVariables(deferred_logger); - if (network_rebalance_necessary) { - // this is to obtain good network solution - balanceNetwork(deferred_logger); - } + // Actually do the pre-step network rebalance, using the updated well states and initial solutions + if (do_prestep_network_rebalance) doPreStepNetworkRebalance(deferred_logger); } template