From fbd6c03dd6762ea69b911b2967406f32de767638 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20H=C3=A6gland?= Date: Thu, 24 Mar 2022 12:42:46 +0100 Subject: [PATCH 1/2] Cleanup glift debugging output code --- .../wells/BlackoilWellModelGeneric.cpp | 3 +- .../wells/BlackoilWellModel_impl.hpp | 5 +- opm/simulators/wells/GasLiftCommon.cpp | 47 +++++++++++++++++++ opm/simulators/wells/GasLiftCommon.hpp | 14 ++++++ opm/simulators/wells/GasLiftGroupInfo.cpp | 25 +++++----- opm/simulators/wells/GasLiftGroupInfo.hpp | 1 - opm/simulators/wells/GasLiftSingleWell.hpp | 1 + .../wells/GasLiftSingleWellGeneric.cpp | 13 +++-- .../wells/GasLiftSingleWellGeneric.hpp | 1 + .../wells/GasLiftSingleWell_impl.hpp | 2 + opm/simulators/wells/GasLiftStage2.cpp | 40 +++++++--------- opm/simulators/wells/GasLiftStage2.hpp | 1 - tests/test_glift1.cpp | 5 +- 13 files changed, 105 insertions(+), 53 deletions(-) diff --git a/opm/simulators/wells/BlackoilWellModelGeneric.cpp b/opm/simulators/wells/BlackoilWellModelGeneric.cpp index 310475754..bd3b5e610 100644 --- a/opm/simulators/wells/BlackoilWellModelGeneric.cpp +++ b/opm/simulators/wells/BlackoilWellModelGeneric.cpp @@ -2189,13 +2189,14 @@ BlackoilWellModelGeneric:: gliftDebug(const std::string& msg, DeferredLogger& deferred_logger) const { - if (this->glift_debug) { + if (this->glift_debug && this->terminal_output_) { const std::string message = fmt::format( " GLIFT (DEBUG) : BlackoilWellModel : {}", msg); deferred_logger.info(message); } } + void BlackoilWellModelGeneric:: gliftDebugShowALQ(DeferredLogger& deferred_logger) diff --git a/opm/simulators/wells/BlackoilWellModel_impl.hpp b/opm/simulators/wells/BlackoilWellModel_impl.hpp index 3b1b7168b..d5a180491 100644 --- a/opm/simulators/wells/BlackoilWellModel_impl.hpp +++ b/opm/simulators/wells/BlackoilWellModel_impl.hpp @@ -909,8 +909,7 @@ namespace Opm { if (this->glift_debug) gliftDebugShowALQ(deferred_logger); num_wells_changed = glift_wells.size(); } - auto comm = ebosSimulator_.vanguard().grid().comm(); - num_wells_changed = comm.sum(num_wells_changed); + num_wells_changed = this->comm_.sum(num_wells_changed); return num_wells_changed > 0; } @@ -1043,7 +1042,7 @@ namespace Opm { = std::make_unique( *well, ebosSimulator_, summary_state, deferred_logger, this->wellState(), this->groupState(), - group_info, sync_groups, this->glift_debug); + group_info, sync_groups, this->comm_, this->glift_debug); auto state = glift->runOptimize( ebosSimulator_.model().newtonMethod().numIterations()); if (state) { diff --git a/opm/simulators/wells/GasLiftCommon.cpp b/opm/simulators/wells/GasLiftCommon.cpp index 88071979b..b9cf16c25 100644 --- a/opm/simulators/wells/GasLiftCommon.cpp +++ b/opm/simulators/wells/GasLiftCommon.cpp @@ -26,10 +26,12 @@ GasLiftCommon:: GasLiftCommon( WellState &well_state, DeferredLogger &deferred_logger, + const Parallel::Communication& comm, bool glift_debug ) : well_state_{well_state}, deferred_logger_{deferred_logger}, + comm_{comm}, debug{glift_debug} { @@ -49,6 +51,51 @@ debugUpdateGlobalCounter_() const return count; } +void +GasLiftCommon:: +displayDebugMessageOnRank0_(const std::string &msg) const +{ + // This output should be identical for all ranks. + + if ( (!this->debug_output_only_on_rank0) + || (this->debug_output_only_on_rank0 && this->comm_.rank() == 0) ) { + displayDebugMessage_(msg); + } +} + +void +GasLiftCommon:: +logMessage_( + const std::string& prefix, const std::string& msg, MessageType msg_type) const +{ + std::string rank = ""; + if (this->comm_.size() > 1) { + rank = fmt::format(" Rank #{} :", this->comm_.rank()); + } + std::string type_str; + switch (msg_type) { + case MessageType::INFO: + type_str = "DEBUG"; + break; + case MessageType::WARNING: + type_str = "WARNING"; + break; + default: + throw std::runtime_error("This should not happen"); + } + const std::string message = fmt::format( + " {} ({}) :{} {}", prefix, type_str, rank, msg); + switch (msg_type) { + case MessageType::INFO: + this->deferred_logger_.info(message); + break; + case MessageType::WARNING: + this->deferred_logger_.info(message); + break; + default: + throw std::runtime_error("This should not happen"); + } +} /**************************************** * Private methods in alphabetical order diff --git a/opm/simulators/wells/GasLiftCommon.hpp b/opm/simulators/wells/GasLiftCommon.hpp index 62857360a..deb74b8ff 100644 --- a/opm/simulators/wells/GasLiftCommon.hpp +++ b/opm/simulators/wells/GasLiftCommon.hpp @@ -37,14 +37,28 @@ protected: GasLiftCommon( WellState &well_state, DeferredLogger &deferred_logger, + const Parallel::Communication& comm, bool debug ); + enum class MessageType { INFO, WARNING }; + int debugUpdateGlobalCounter_() const; virtual void displayDebugMessage_(const std::string& msg) const = 0; + void displayDebugMessageOnRank0_(const std::string &msg) const; + void logMessage_( + const std::string& prefix, + const std::string& msg, + MessageType msg_type = MessageType::INFO) const; WellState &well_state_; DeferredLogger &deferred_logger_; + const Parallel::Communication& comm_; bool debug; + // By setting this variable to true we restrict some debug output + // to only be printed for rank 0. By setting this variable to false we keep + // the output on all ranks. This can in some cases be helpful as a debugging + // aid to check that the output is in fact identical over all ranks + bool debug_output_only_on_rank0 = false; }; } // namespace Opm diff --git a/opm/simulators/wells/GasLiftGroupInfo.cpp b/opm/simulators/wells/GasLiftGroupInfo.cpp index 300860091..4f07da7cb 100644 --- a/opm/simulators/wells/GasLiftGroupInfo.cpp +++ b/opm/simulators/wells/GasLiftGroupInfo.cpp @@ -35,14 +35,13 @@ GasLiftGroupInfo( const Communication &comm, bool glift_debug ) : - GasLiftCommon(well_state, deferred_logger, glift_debug) + GasLiftCommon(well_state, deferred_logger, comm, glift_debug) , ecl_wells_{ecl_wells} , schedule_{schedule} , summary_state_{summary_state} , report_step_idx_{report_step_idx} , iteration_idx_{iteration_idx} , phase_usage_{phase_usage} - , comm_{comm} , glo_{schedule_.glo(report_step_idx_)} { @@ -294,7 +293,7 @@ checkDoGasLiftOptimization_(const std::string &well_name) auto itr = this->ecl_wells_.find(well_name); if (itr == this->ecl_wells_.end()) { // well_name is not present in the well_model's well container - displayDebugMessage_("Could find well ecl_wells. Skipping.", well_name); + displayDebugMessage_("Could not find well in ecl_wells. Skipping.", well_name); return false; } const Well *well = (itr->second).first; @@ -368,6 +367,8 @@ checkNewtonIterationIdxOk_(const std::string &well_name) } } +// This is called by each rank, but the value of "well_name" should be unique +// across ranks void GasLiftGroupInfo:: debugDisplayWellContribution_( @@ -393,11 +394,10 @@ debugDisplayUpdatedGroupRates( const std::string& name, double oil_rate, double gas_rate, double water_rate, double alq) const { - const std::string msg = fmt::format("Updated group info for {} : " "oil_rate = {}, gas_rate = {}, water_rate = {}, alq = {}", name, oil_rate, gas_rate, water_rate, alq); - displayDebugMessage_(msg); + displayDebugMessageOnRank0_(msg); } void @@ -405,7 +405,7 @@ GasLiftGroupInfo:: debugEndInitializeGroup(const std::string& name) const { const std::string msg = fmt::format("Finished with group {} ...", name); - displayDebugMessage_(msg); + displayDebugMessageOnRank0_(msg); } void @@ -413,7 +413,7 @@ GasLiftGroupInfo:: debugStartInitializeGroup(const std::string& name) const { const std::string msg = fmt::format("Initializing group {} ...", name); - displayDebugMessage_(msg); + displayDebugMessageOnRank0_(msg); } void @@ -421,9 +421,8 @@ GasLiftGroupInfo:: displayDebugMessage_(const std::string &msg) const { if (this->debug) { - const std::string message = fmt::format( - " GLIFT (DEBUG) : Init group info : {}", msg); - this->deferred_logger_.info(message); + const std::string message = fmt::format("Init group info : {}", msg); + logMessage_(/*prefix=*/"GLIFT", message); } } @@ -432,10 +431,8 @@ GasLiftGroupInfo:: displayDebugMessage_(const std::string &msg, const std::string &well_name) { if (this->debug) { - const std::string message = fmt::format( - " GLIFT (DEBUG) : Init group info : Well {} : {}", - well_name, msg); - this->deferred_logger_.info(message); + const std::string message = fmt::format("Well {} : {}", well_name, msg); + displayDebugMessage_(message); } } diff --git a/opm/simulators/wells/GasLiftGroupInfo.hpp b/opm/simulators/wells/GasLiftGroupInfo.hpp index de60eda58..910108c8f 100644 --- a/opm/simulators/wells/GasLiftGroupInfo.hpp +++ b/opm/simulators/wells/GasLiftGroupInfo.hpp @@ -202,7 +202,6 @@ protected: const int report_step_idx_; const int iteration_idx_; const PhaseUsage &phase_usage_; - const Parallel::Communication &comm_; const GasLiftOpt& glo_; GroupRateMap group_rate_map_; Well2GroupMap well_group_map_; diff --git a/opm/simulators/wells/GasLiftSingleWell.hpp b/opm/simulators/wells/GasLiftSingleWell.hpp index 48420eba9..163351a69 100644 --- a/opm/simulators/wells/GasLiftSingleWell.hpp +++ b/opm/simulators/wells/GasLiftSingleWell.hpp @@ -50,6 +50,7 @@ namespace Opm const GroupState& group_state, GasLiftGroupInfo &group_info, GLiftSyncGroups &sync_groups, + const Parallel::Communication& comm, bool glift_debug ); const WellInterfaceGeneric &getWell() const override { return well_; } diff --git a/opm/simulators/wells/GasLiftSingleWellGeneric.cpp b/opm/simulators/wells/GasLiftSingleWellGeneric.cpp index c180ebc75..d54198448 100644 --- a/opm/simulators/wells/GasLiftSingleWellGeneric.cpp +++ b/opm/simulators/wells/GasLiftSingleWellGeneric.cpp @@ -48,9 +48,10 @@ GasLiftSingleWellGeneric::GasLiftSingleWellGeneric( const Schedule& schedule, const int report_step_idx, GLiftSyncGroups& sync_groups, + const Parallel::Communication& comm, bool glift_debug ) : - GasLiftCommon(well_state, deferred_logger, glift_debug) + GasLiftCommon(well_state, deferred_logger, comm, glift_debug) , group_state_{group_state} , ecl_well_{ecl_well} , summary_state_{summary_state} @@ -495,9 +496,8 @@ displayDebugMessage_(const std::string& msg) const { if (this->debug) { - const std::string message = fmt::format( - " GLIFT (DEBUG) : Well {} : {}", this->well_name_, msg); - this->deferred_logger_.info(message); + const std::string message = fmt::format("Well {} : {}", this->well_name_, msg); + logMessage_(/*prefix=*/"GLIFT", message); } } @@ -505,9 +505,8 @@ void GasLiftSingleWellGeneric:: displayWarning_(const std::string& msg) { - const std::string message = fmt::format( - "GAS LIFT OPTIMIZATION, WELL {} : {}", this->well_name_, msg); - this->deferred_logger_.warning("WARNING", message); + const std::string message = fmt::format("WELL {} : {}", this->well_name_, msg); + logMessage_(/*prefix=*/"GLIFT", msg, MessageType::WARNING); } std::pair diff --git a/opm/simulators/wells/GasLiftSingleWellGeneric.hpp b/opm/simulators/wells/GasLiftSingleWellGeneric.hpp index 2cab756c5..79f1a376c 100644 --- a/opm/simulators/wells/GasLiftSingleWellGeneric.hpp +++ b/opm/simulators/wells/GasLiftSingleWellGeneric.hpp @@ -111,6 +111,7 @@ protected: const Schedule& schedule, const int report_step_idx, GLiftSyncGroups& sync_groups, + const Parallel::Communication& comm, bool glift_debug ); diff --git a/opm/simulators/wells/GasLiftSingleWell_impl.hpp b/opm/simulators/wells/GasLiftSingleWell_impl.hpp index 1611f8c43..8fcf78e04 100644 --- a/opm/simulators/wells/GasLiftSingleWell_impl.hpp +++ b/opm/simulators/wells/GasLiftSingleWell_impl.hpp @@ -29,6 +29,7 @@ GasLiftSingleWell(const WellInterface &well, const GroupState &group_state, GasLiftGroupInfo &group_info, GLiftSyncGroups &sync_groups, + const Parallel::Communication& comm, bool glift_debug ) // The parent class GasLiftSingleWellGeneric contains all stuff @@ -44,6 +45,7 @@ GasLiftSingleWell(const WellInterface &well, ebos_simulator.vanguard().schedule(), ebos_simulator.episodeIndex(), sync_groups, + comm, glift_debug ) , ebos_simulator_{ebos_simulator} diff --git a/opm/simulators/wells/GasLiftStage2.cpp b/opm/simulators/wells/GasLiftStage2.cpp index 6571cddfe..422f7e134 100644 --- a/opm/simulators/wells/GasLiftStage2.cpp +++ b/opm/simulators/wells/GasLiftStage2.cpp @@ -49,7 +49,7 @@ GasLiftStage2::GasLiftStage2( GLiftWellStateMap &state_map, bool glift_debug ) : - GasLiftCommon(well_state, deferred_logger, glift_debug) + GasLiftCommon(well_state, deferred_logger, comm, glift_debug) , prod_wells_{prod_wells} , stage1_wells_{glift_wells} , well_state_map_{state_map} @@ -57,7 +57,6 @@ GasLiftStage2::GasLiftStage2( , summary_state_{summary_state} , schedule_{schedule} , glo_{schedule_.glo(report_step_idx_)} - , comm_{comm} { // this->time_step_idx_ // = this->ebos_simulator_.model().newtonMethod().currentTimeStep(); @@ -233,18 +232,15 @@ void GasLiftStage2:: displayWarning_(const std::string &msg, const std::string &group_name) { - const std::string message = fmt::format( - "GAS LIFT OPTIMIZATION (STAGE2), GROUP: {} : {}", group_name, msg); - this->deferred_logger_.warning("WARNING", message); + const std::string message = fmt::format("GROUP: {} : {}", group_name, msg); + displayWarning_(message); } void GasLiftStage2:: displayWarning_(const std::string &msg) { - const std::string message = fmt::format( - "GAS LIFT OPTIMIZATION (STAGE2) : {}", msg); - this->deferred_logger_.warning("WARNING", message); + logMessage_(/*prefix=*/"GLIFT2", msg, MessageType::WARNING); } void @@ -252,9 +248,7 @@ GasLiftStage2:: displayDebugMessage_(const std::string &msg) const { if (this->debug) { - const std::string message = fmt::format( - " GLIFT2 (DEBUG) : {}", msg); - this->deferred_logger_.info(message); + logMessage_(/*prefix=*/"GLIFT2", msg); } } @@ -263,9 +257,7 @@ GasLiftStage2:: displayDebugMessage2B_(const std::string &msg) { if (this->debug) { - const std::string message = fmt::format( - "Stage 2B : {}", msg); - displayDebugMessage_(message); + logMessage_(/*prefix=*/"GLIFT2B", msg); } } @@ -291,7 +283,7 @@ getCurrentGroupRates_(const Group &group) const std::string msg = fmt::format( "Current group rates for {} : oil: {}, gas: {}, alq: {}", group.name(), oil_rate, gas_rate, alq); - displayDebugMessage2B_(msg); + displayDebugMessageOnRank0_(msg); } return {oil_rate, gas_rate, alq}; @@ -722,7 +714,7 @@ removeSurplusALQ_(const Group &group, std::vector &inc_grads, std::vector &dec_grads) { if (dec_grads.empty()) { - displayDebugMessage2B_("no wells to remove ALQ from. Skipping"); + displayDebugMessage_("no wells to remove ALQ from. Skipping"); return; } assert(!dec_grads.empty()); @@ -740,7 +732,7 @@ removeSurplusALQ_(const Group &group, "oil_rate = {}, oil_target = {}, gas_rate = {}, gas_target = {}, " "alq = {}, max_alq = {}", group.name(), oil_rate, controls.oil_target, gas_rate, controls.gas_target, alq, max_glift_str); - displayDebugMessage2B_(msg); + displayDebugMessage_(msg); } SurplusState state {*this, group, oil_rate, gas_rate, alq, min_eco_grad, controls.oil_target, controls.gas_target, max_glift }; @@ -790,11 +782,11 @@ removeSurplusALQ_(const Group &group, "Finished after {} iterations for group: {}." " oil_rate = {}, gas_rate = {}, alq = {}", state.it, group.name(), oil_rate2, gas_rate2, alq2); - displayDebugMessage2B_(msg); + displayDebugMessage_(msg); } } else { - displayDebugMessage2B_("Finished after 0 iterations"); + displayDebugMessage_("Finished after 0 iterations"); } } @@ -1022,7 +1014,7 @@ addOrRemoveALQincrement(GradMap &grad_map, const std::string& well_name, bool ad if (this->parent.debug) { const std::string msg = fmt::format("group: {} : well {} : {} ALQ increment", this->group.name(), well_name, (add ? "adding" : "subtracting")); - this->parent.displayDebugMessage2B_(msg); + this->parent.displayDebugMessage_(msg); } this->parent.addOrRemoveALQincrement_(grad_map, well_name, add); } @@ -1040,7 +1032,7 @@ checkALQlimit() const std::string msg = fmt::format("group: {} : " "ALQ rate {} is greater than ALQ limit {}", this->group.name(), this->alq, max_alq); - this->parent.displayDebugMessage2B_(msg); + this->parent.displayDebugMessage_(msg); } return true; } @@ -1057,7 +1049,7 @@ checkEcoGradient(const std::string &well_name, double eco_grad) const std::string msg = fmt::format("group: {}, well: {} : " "economic gradient {} less than minimum ({})", this->group.name(), well_name, eco_grad, this->min_eco_grad); - this->parent.displayDebugMessage2B_(msg); + this->parent.displayDebugMessage_(msg); } return true; } @@ -1076,7 +1068,7 @@ checkGasTarget() const std::string msg = fmt::format("group: {} : " "gas rate {} is greater than gas target {}", this->group.name(), this->gas_rate, this->gas_target); - this->parent.displayDebugMessage2B_(msg); + this->parent.displayDebugMessage_(msg); } return true; } @@ -1094,7 +1086,7 @@ checkOilTarget() const std::string msg = fmt::format("group: {} : " "oil rate {} is greater than oil target {}", this->group.name(), this->oil_rate, this->oil_target); - this->parent.displayDebugMessage2B_(msg); + this->parent.displayDebugMessage_(msg); } return true; } diff --git a/opm/simulators/wells/GasLiftStage2.hpp b/opm/simulators/wells/GasLiftStage2.hpp index 63562aaa0..e2ce7bcbb 100644 --- a/opm/simulators/wells/GasLiftStage2.hpp +++ b/opm/simulators/wells/GasLiftStage2.hpp @@ -130,7 +130,6 @@ protected: const SummaryState& summary_state_; const Schedule& schedule_; const GasLiftOpt& glo_; - const Parallel::Communication& comm_; GradMap inc_grads_; GradMap dec_grads_; int max_iterations_ = 1000; diff --git a/tests/test_glift1.cpp b/tests/test_glift1.cpp index 77d09724f..3c445107c 100644 --- a/tests/test_glift1.cpp +++ b/tests/test_glift1.cpp @@ -172,6 +172,7 @@ BOOST_AUTO_TEST_CASE(G1) GLiftEclWells ecl_well_map; well_model.initGliftEclWellMap(ecl_well_map); const int iteration_idx = simulator->model().newtonMethod().numIterations(); + const auto& comm = simulator->vanguard().grid().comm(); GasLiftGroupInfo group_info { ecl_well_map, schedule, @@ -181,13 +182,13 @@ BOOST_AUTO_TEST_CASE(G1) well_model.phaseUsage(), deferred_logger, well_state, - simulator->vanguard().grid().comm(), + comm, /*glift_debug=*/false }; GLiftSyncGroups sync_groups; GasLiftSingleWell glift {*std_well, *(simulator.get()), summary_state, deferred_logger, well_state, group_state, group_info, sync_groups, - /*glift_debug=*/false + comm, /*glift_debug=*/false }; group_info.initialize(); auto state = glift.runOptimize(iteration_idx); From a3c131955f3b4c13d9182f631f4ab5c38d43d0f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20H=C3=A6gland?= Date: Thu, 24 Mar 2022 13:47:57 +0100 Subject: [PATCH 2/2] Omit one redundant debugging message --- opm/simulators/wells/GasLiftGroupInfo.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opm/simulators/wells/GasLiftGroupInfo.cpp b/opm/simulators/wells/GasLiftGroupInfo.cpp index 4f07da7cb..9a50025dd 100644 --- a/opm/simulators/wells/GasLiftGroupInfo.cpp +++ b/opm/simulators/wells/GasLiftGroupInfo.cpp @@ -293,7 +293,7 @@ checkDoGasLiftOptimization_(const std::string &well_name) auto itr = this->ecl_wells_.find(well_name); if (itr == this->ecl_wells_.end()) { // well_name is not present in the well_model's well container - displayDebugMessage_("Could not find well in ecl_wells. Skipping.", well_name); + //displayDebugMessage_("Could not find well in ecl_wells. Skipping.", well_name); return false; } const Well *well = (itr->second).first;