From 7bb3dba3101e047f8e637257154a2e9965d128dd Mon Sep 17 00:00:00 2001 From: Joakim Hove Date: Tue, 31 Aug 2021 17:31:45 +0200 Subject: [PATCH] Special case wells with no open connections in ParallelWellInfo --- opm/simulators/wells/ParallelWellInfo.cpp | 3 ++- opm/simulators/wells/ParallelWellInfo.hpp | 20 +++++++++++++------- tests/test_parallelwellinfo.cpp | 11 +++++++++++ 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/opm/simulators/wells/ParallelWellInfo.cpp b/opm/simulators/wells/ParallelWellInfo.cpp index 0f08924a0..5ae05b9f4 100644 --- a/opm/simulators/wells/ParallelWellInfo.cpp +++ b/opm/simulators/wells/ParallelWellInfo.cpp @@ -381,7 +381,8 @@ void ParallelWellInfo::communicateFirstPerforation(bool hasFirst) comm_->allgather(&first, 1, firstVec.data()); auto found = std::find_if(firstVec.begin(), firstVec.end(), [](int i) -> bool{ return i;}); - rankWithFirstPerf_ = found - firstVec.begin(); + if (found != firstVec.end()) + rankWithFirstPerf_ = found - firstVec.begin(); } void ParallelWellInfo::pushBackEclIndex(int above, int current) diff --git a/opm/simulators/wells/ParallelWellInfo.hpp b/opm/simulators/wells/ParallelWellInfo.hpp index 6ffc7dd56..c8767f5bd 100644 --- a/opm/simulators/wells/ParallelWellInfo.hpp +++ b/opm/simulators/wells/ParallelWellInfo.hpp @@ -279,20 +279,26 @@ public: /// \brief Collectively decide which rank has first perforation. void communicateFirstPerforation(bool hasFirst); + + /// If the well does not have any open connections the member rankWithFirstPerf + /// is not initialized, and no broadcast is performed. In this case the argument + /// is returned unmodified. template T broadcastFirstPerforationValue(const T& t) const { T res = t; + if (rankWithFirstPerf_ >= 0) { #ifndef NDEBUG - assert(rankWithFirstPerf_ >= 0 && rankWithFirstPerf_ < comm_->size()); - // At least on some OpenMPI version this might broadcast might interfere - // with other communication if there are bugs - comm_->barrier(); + assert(rankWithFirstPerf_ < comm_->size()); + // At least on some OpenMPI version this might broadcast might interfere + // with other communication if there are bugs + comm_->barrier(); #endif - comm_->broadcast(&res, 1, rankWithFirstPerf_); + comm_->broadcast(&res, 1, rankWithFirstPerf_); #ifndef NDEBUG - comm_->barrier(); + comm_->barrier(); #endif + } return res; } @@ -406,7 +412,7 @@ private: bool hasLocalCells_; /// \brief Whether we own the well and should do reports etc. bool isOwner_; - /// \brief Rank with the first perforation on it. + /// \brief Rank with the first perforation on it, -1 for wells with no open connections. int rankWithFirstPerf_; /// \brief Communication object for the well /// diff --git a/tests/test_parallelwellinfo.cpp b/tests/test_parallelwellinfo.cpp index 660fa4eaf..2fb75f5b7 100644 --- a/tests/test_parallelwellinfo.cpp +++ b/tests/test_parallelwellinfo.cpp @@ -468,3 +468,14 @@ BOOST_AUTO_TEST_CASE(GlobalPerfFactoryParallel1) testGlobalPerfFactoryParallel(1); testGlobalPerfFactoryParallel(3); } + + +BOOST_AUTO_TEST_CASE(EmptyWell) { + auto comm = Communication(Dune::MPIHelper::getCommunicator()); + Opm::ParallelWellInfo pw({"WELL1", true}, comm); + pw.communicateFirstPerforation(false); + double local_p = 1; + auto global_p = pw.broadcastFirstPerforationValue(local_p); + + BOOST_CHECK_EQUAL(local_p, global_p); +}