From e334f28cc310ddffcbd19166e7528bf625dd6613 Mon Sep 17 00:00:00 2001 From: Arne Morten Kvarving Date: Fri, 24 Nov 2023 10:21:09 +0100 Subject: [PATCH] HandlerContext: make target_wellpi private add a method getWellPI to obtain value for a given well. protects the potential null pointer --- .../input/eclipse/Schedule/HandlerContext.cpp | 16 ++++++++++++++++ .../input/eclipse/Schedule/HandlerContext.hpp | 7 +++++-- .../input/eclipse/Schedule/KeywordHandlers.cpp | 8 ++------ 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/opm/input/eclipse/Schedule/HandlerContext.cpp b/src/opm/input/eclipse/Schedule/HandlerContext.cpp index a1e1aef3c..b07752be0 100644 --- a/src/opm/input/eclipse/Schedule/HandlerContext.cpp +++ b/src/opm/input/eclipse/Schedule/HandlerContext.cpp @@ -25,6 +25,8 @@ #include "MSW/WelSegsSet.hpp" +#include + namespace Opm { void HandlerContext::affected_well(const std::string& well_name) @@ -62,4 +64,18 @@ void HandlerContext::compsegs_handled(const std::string& well_name) } } +double HandlerContext::getWellPI(const std::string& well_name) const +{ + if (!target_wellpi) { + throw std::logic_error("Lookup of well PI with no map available"); + } + + auto wellpi_iter = target_wellpi->find(well_name); + if (wellpi_iter == target_wellpi->end()) { + throw std::logic_error("Missing current PI for well " + well_name); + } + + return wellpi_iter->second; +} + } diff --git a/src/opm/input/eclipse/Schedule/HandlerContext.hpp b/src/opm/input/eclipse/Schedule/HandlerContext.hpp index 5be769112..b6f4a82d1 100644 --- a/src/opm/input/eclipse/Schedule/HandlerContext.hpp +++ b/src/opm/input/eclipse/Schedule/HandlerContext.hpp @@ -63,9 +63,9 @@ public: , actionx_mode(actionx_mode_) , parseContext(parseContext_) , errors(errors_) - , target_wellpi(target_wellpi_) , wpimult_global_factor(wpimult_global_factor_) , grid(grid_) + , target_wellpi(target_wellpi_) , welsegs_wells(welsegs_wells_) , compsegs_wells(compsegs_wells_) , sim_update(sim_update_) @@ -86,6 +86,9 @@ public: /// \brief Mark that the well occured in a COMPSEGS keyword. void compsegs_handled(const std::string& well_name); + //! \brief Obtain PI for a well. + double getWellPI(const std::string& well_name) const; + const ScheduleBlock& block; const DeckKeyword& keyword; const std::size_t currentStep; @@ -93,11 +96,11 @@ public: const bool actionx_mode; const ParseContext& parseContext; ErrorGuard& errors; - const std::unordered_map* target_wellpi{nullptr}; std::unordered_map* wpimult_global_factor{nullptr}; const ScheduleGrid& grid; private: + const std::unordered_map* target_wellpi{nullptr}; WelSegsSet* welsegs_wells{nullptr}; std::set* compsegs_wells{nullptr}; SimulatorUpdate* sim_update{nullptr}; diff --git a/src/opm/input/eclipse/Schedule/KeywordHandlers.cpp b/src/opm/input/eclipse/Schedule/KeywordHandlers.cpp index dc050eed3..be07c943a 100644 --- a/src/opm/input/eclipse/Schedule/KeywordHandlers.cpp +++ b/src/opm/input/eclipse/Schedule/KeywordHandlers.cpp @@ -1773,14 +1773,10 @@ File {} line {}.)", wname, location.keyword, location.filename, location.lineno) const auto targetPI = record.getItem().get(0); std::vector scalingApplicable; - const auto& current_wellpi = *handlerContext.target_wellpi; for (const auto& well_name : well_names) { - auto wellpi_iter = current_wellpi.find(well_name); - if (wellpi_iter == current_wellpi.end()) - throw std::logic_error(fmt::format("Missing current PI for well {}", well_name)); - auto new_well = this->getWell(well_name, report_step); - auto scalingFactor = new_well.convertDeckPI(targetPI) / wellpi_iter->second; + auto scalingFactor = new_well.convertDeckPI(targetPI) / + handlerContext.getWellPI(well_name); new_well.updateWellProductivityIndex(); new_well.applyWellProdIndexScaling(scalingFactor, scalingApplicable); this->snapshots.back().wells.update( std::move(new_well) );