From 82b112305e252f0733d0f7c06f7943cc3a8a6637 Mon Sep 17 00:00:00 2001 From: Joakim Hove Date: Wed, 13 Oct 2021 14:22:59 +0200 Subject: [PATCH] Move some code from rst_deck application to FileDeck class --- examples/rst_deck.cpp | 38 +++------------------ opm/parser/eclipse/Deck/FileDeck.hpp | 5 +++ src/opm/parser/eclipse/Deck/FileDeck.cpp | 43 ++++++++++++++++++++++++ tests/parser/ScheduleRestartTests.cpp | 22 ++++++++++++ 4 files changed, 74 insertions(+), 34 deletions(-) diff --git a/examples/rst_deck.cpp b/examples/rst_deck.cpp index 914ae93bc..26acce993 100644 --- a/examples/rst_deck.cpp +++ b/examples/rst_deck.cpp @@ -24,7 +24,6 @@ #include #include -#include #include #include #include @@ -43,7 +42,6 @@ namespace fs = Opm::filesystem; const std::unordered_set remove_from_solution = {"EQUIL", "PRESSURE", "SWAT", "SGAS"}; -const std::unordered_set keep_in_solution = {"ABC"}; void print_help_and_exit(const std::optional error_msg = {}) { @@ -54,7 +52,7 @@ void print_help_and_exit(const std::optional error_msg = {}) { } std::string keep_keywords; - for (const auto& kw : keep_in_solution) + for (const auto& kw : Opm::FileDeck::rst_keep_in_solution) keep_keywords += kw + " "; const std::string help_text = fmt::format(R"( @@ -239,30 +237,7 @@ void update_solution(const options& opt, Opm::FileDeck& file_deck) if (!summary.has_value()) print_help_and_exit(fmt::format("Could not find SUMMARY section in input deck: {}", opt.input_deck)); - auto index = solution.value(); - index++; - { - while (true) { - const auto& keyword = file_deck[index]; - if (keep_in_solution.count(keyword.name()) == 0) { - file_deck.erase(index); - summary.value()--; - } else - index++; - - if (index == summary) - break; - } - } - - { - Opm::UnitSystem units; - std::vector values{ Opm::DeckValue{opt.restart.first}, Opm::DeckValue{opt.restart.second} }; - Opm::DeckKeyword restart(Opm::ParserKeywords::RESTART(), std::vector>{ values }, units, units); - - auto schedule = file_deck.find("SCHEDULE"); - file_deck.insert(++schedule.value(), restart); - } + file_deck.rst_solution(opt.restart.first, opt.restart.second); } @@ -271,13 +246,8 @@ void update_schedule(const options& opt, Opm::FileDeck& file_deck) if (opt.restart.second == 0) return; - const auto schedule = file_deck.find("SCHEDULE"); - if (opt.skiprest) { - Opm::DeckKeyword skiprest( Opm::ParserKeywords::SKIPREST{} ); - - auto index = schedule.value(); - file_deck.insert(++index, skiprest); - } + if (opt.skiprest) + file_deck.insert_skiprest(); } diff --git a/opm/parser/eclipse/Deck/FileDeck.hpp b/opm/parser/eclipse/Deck/FileDeck.hpp index 114780334..9b7688072 100644 --- a/opm/parser/eclipse/Deck/FileDeck.hpp +++ b/opm/parser/eclipse/Deck/FileDeck.hpp @@ -38,6 +38,8 @@ class Deck; class FileDeck { public: + static const std::unordered_set rst_keep_in_solution; + enum class OutputMode { INLINE = 1, @@ -103,6 +105,9 @@ friend FileDeck; const Index start() const; const Index stop() const; + void rst_solution(const std::string& rst_base, int report_step); + void insert_skiprest(); + private: std::vector blocks; std::string input_directory; diff --git a/src/opm/parser/eclipse/Deck/FileDeck.cpp b/src/opm/parser/eclipse/Deck/FileDeck.cpp index e129d67ac..506ebe98e 100644 --- a/src/opm/parser/eclipse/Deck/FileDeck.cpp +++ b/src/opm/parser/eclipse/Deck/FileDeck.cpp @@ -19,8 +19,11 @@ #include #include #include +#include #include #include +#include +#include namespace fs = Opm::filesystem; @@ -355,4 +358,44 @@ void FileDeck::dump_stdout(const std::string& output_dir, OutputMode mode) const else if (mode == OutputMode::SHARE) this->dump_shared(std::cout, output_dir); } + + +void FileDeck::rst_solution(const std::string& rst_base, int report_step) { + auto index = this->find("SOLUTION").value(); + auto summary_index = this->find("SUMMARY").value(); + + index++; + { + while (true) { + const auto& keyword = this->operator[](index); + if (FileDeck::rst_keep_in_solution.count(keyword.name()) == 0) { + this->erase(index); + summary_index--; + } else + index++; + + if (index == summary_index) + break; + } + } + + { + Opm::UnitSystem units; + std::vector values{ DeckValue{rst_base}, DeckValue{report_step} }; + DeckKeyword restart(ParserKeywords::RESTART(), std::vector>{ values }, units, units); + + auto solution = this->find("SOLUTION").value(); + this->insert(++solution, restart); + } +} + +void FileDeck::insert_skiprest() { + DeckKeyword skiprest( ParserKeywords::SKIPREST{} ); + const auto schedule = this->find("SCHEDULE"); + auto index = schedule.value(); + this->insert(++index, skiprest); +} + +const std::unordered_set FileDeck::rst_keep_in_solution = {"ABC"}; + } diff --git a/tests/parser/ScheduleRestartTests.cpp b/tests/parser/ScheduleRestartTests.cpp index b22150b70..1bec1bbe6 100644 --- a/tests/parser/ScheduleRestartTests.cpp +++ b/tests/parser/ScheduleRestartTests.cpp @@ -266,6 +266,28 @@ BOOST_AUTO_TEST_CASE(TestFileDeck) fd.erase(index6.value(), index7.value()); } +BOOST_AUTO_TEST_CASE(RestartTest2) +{ + Parser parser; + auto python = std::make_shared(); + auto deck = parser.parseFile("UDQ_WCONPROD.DATA"); + FileDeck fd(deck); + + fd.rst_solution("RESTART", 77); + fd.insert_skiprest(); + + auto solution_index = fd.find("SOLUTION").value(); + auto restart_index = fd.find("RESTART").value(); + auto summary_index = fd.find("SUMMARY").value(); + auto schedule_index = fd.find("SCHEDULE").value(); + auto skiprest_index = fd.find("SKIPREST").value(); + + BOOST_CHECK(solution_index < restart_index); + BOOST_CHECK(restart_index < summary_index); + BOOST_CHECK(summary_index < schedule_index); + BOOST_CHECK(schedule_index < skiprest_index); +} + BOOST_AUTO_TEST_CASE(RestartTest) {