From ad746440e0f48c61f9ec55b233d862031f9992d6 Mon Sep 17 00:00:00 2001 From: Joakim Hove Date: Thu, 7 May 2020 17:25:11 +0200 Subject: [PATCH] Use string based parent *name* in GTNode instead of pointer --- .../eclipse/EclipseState/Schedule/Group/GTNode.hpp | 9 ++++++--- .../eclipse/EclipseState/Schedule/Schedule.hpp | 3 +-- .../eclipse/EclipseState/Schedule/Group/GTNode.cpp | 14 ++++++++------ .../eclipse/EclipseState/Schedule/Schedule.cpp | 8 ++++---- tests/parser/ScheduleTests.cpp | 4 ++-- 5 files changed, 21 insertions(+), 17 deletions(-) diff --git a/opm/parser/eclipse/EclipseState/Schedule/Group/GTNode.hpp b/opm/parser/eclipse/EclipseState/Schedule/Group/GTNode.hpp index ea14836d256..4245fafc8f9 100644 --- a/opm/parser/eclipse/EclipseState/Schedule/Group/GTNode.hpp +++ b/opm/parser/eclipse/EclipseState/Schedule/Group/GTNode.hpp @@ -17,6 +17,7 @@ along with OPM. If not, see . */ +#include #include #include @@ -29,7 +30,7 @@ namespace Opm { class GTNode { public: - GTNode(const Group& group, const GTNode* parent); + GTNode(const Group& group, std::size_t level, const std::optional& parent_name); void add_group(const GTNode& child_group); void add_well(const Well& well); @@ -37,11 +38,13 @@ class GTNode { const std::vector& wells() const; const std::vector& groups() const; const std::string& name() const; - const GTNode& parent() const; + const std::string& parent_name() const; + const Group& group() const; private: const Group m_group; - const GTNode * m_parent; + std::size_t m_level; + std::optional m_parent_name; /* Class T with a stl container - supposedly undefined behavior before C++17 - but it compiles without warnings. diff --git a/opm/parser/eclipse/EclipseState/Schedule/Schedule.hpp b/opm/parser/eclipse/EclipseState/Schedule/Schedule.hpp index f5a05e87236..0d6658be164 100644 --- a/opm/parser/eclipse/EclipseState/Schedule/Schedule.hpp +++ b/opm/parser/eclipse/EclipseState/Schedule/Schedule.hpp @@ -367,8 +367,7 @@ namespace Opm const UnitSystem& unit_system); DynamicState> rpt_config; - - GTNode groupTree(const std::string& root_node, std::size_t report_step, const GTNode * parent) const; + GTNode groupTree(const std::string& root_node, std::size_t report_step, std::size_t level, const std::optional& parent_name) const; void updateGroup(std::shared_ptr group, size_t reportStep); bool checkGroups(const ParseContext& parseContext, ErrorGuard& errors); void updateUDQActive( std::size_t timeStep, std::shared_ptr udq ); diff --git a/src/opm/parser/eclipse/EclipseState/Schedule/Group/GTNode.cpp b/src/opm/parser/eclipse/EclipseState/Schedule/Group/GTNode.cpp index 8369f443897..6e8d53f84d1 100644 --- a/src/opm/parser/eclipse/EclipseState/Schedule/Group/GTNode.cpp +++ b/src/opm/parser/eclipse/EclipseState/Schedule/Group/GTNode.cpp @@ -21,10 +21,12 @@ namespace Opm { -GTNode::GTNode(const Group& group_arg, const GTNode * parent_arg) : +GTNode::GTNode(const Group& group_arg, std::size_t level, const std::optional& parent_name) : m_group(group_arg), - m_parent(parent_arg) -{} + m_level(level), + m_parent_name(parent_name) +{ +} const std::string& GTNode::name() const { return this->m_group.name(); @@ -34,9 +36,9 @@ const Group& GTNode::group() const { return this->m_group; } -const GTNode& GTNode::parent() const { - if (this->m_parent) - return *this->m_parent; +const std::string& GTNode::parent_name() const { + if (this->m_parent_name.has_value()) + return *this->m_parent_name; throw std::invalid_argument("Tried to access parent of root in GroupTree. Root: " + this->name()); } diff --git a/src/opm/parser/eclipse/EclipseState/Schedule/Schedule.cpp b/src/opm/parser/eclipse/EclipseState/Schedule/Schedule.cpp index 5f03ada434f..787fbb6eb32 100644 --- a/src/opm/parser/eclipse/EclipseState/Schedule/Schedule.cpp +++ b/src/opm/parser/eclipse/EclipseState/Schedule/Schedule.cpp @@ -2218,9 +2218,9 @@ void Schedule::invalidNamePattern( const std::string& namePattern, std::size_t } - GTNode Schedule::groupTree(const std::string& root_node, std::size_t report_step, const GTNode * parent) const { + GTNode Schedule::groupTree(const std::string& root_node, std::size_t report_step, std::size_t level, const std::optional& parent_name) const { auto root_group = this->getGroup(root_node, report_step); - GTNode tree(root_group, parent); + GTNode tree(root_group, level, parent_name); for (const auto& wname : root_group.wells()) { const auto& well = this->getWell(wname, report_step); @@ -2228,7 +2228,7 @@ void Schedule::invalidNamePattern( const std::string& namePattern, std::size_t } for (const auto& gname : root_group.groups()) { - auto child_group = this->groupTree(gname, report_step, std::addressof(tree)); + auto child_group = this->groupTree(gname, report_step, level + 1, root_node); tree.add_group(child_group); } @@ -2236,7 +2236,7 @@ void Schedule::invalidNamePattern( const std::string& namePattern, std::size_t } GTNode Schedule::groupTree(const std::string& root_node, std::size_t report_step) const { - return this->groupTree(root_node, report_step, nullptr); + return this->groupTree(root_node, report_step, 0, {}); } diff --git a/tests/parser/ScheduleTests.cpp b/tests/parser/ScheduleTests.cpp index 1852af1641e..1474c5f5e7f 100644 --- a/tests/parser/ScheduleTests.cpp +++ b/tests/parser/ScheduleTests.cpp @@ -475,13 +475,13 @@ BOOST_AUTO_TEST_CASE(GroupTree2TEST) { auto gt = schedule.groupTree(0); BOOST_CHECK_EQUAL(gt.wells().size(), 0); BOOST_CHECK_EQUAL(gt.group().name(), "FIELD"); - BOOST_CHECK_THROW(gt.parent(), std::invalid_argument); + BOOST_CHECK_THROW(gt.parent_name(), std::invalid_argument); auto cg = gt.groups(); auto pg = cg[0]; BOOST_CHECK_EQUAL(cg.size(), 1); BOOST_CHECK_EQUAL(pg.group().name(), "PLATFORM"); - BOOST_CHECK_EQUAL(pg.parent().name(), "FIELD"); + BOOST_CHECK_EQUAL(pg.parent_name(), "FIELD"); }