From 0403d4e6e79a0d96efbc4d0f64ad3a806690d1a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20H=C3=A6gland?= Date: Thu, 1 Oct 2020 23:54:48 +0200 Subject: [PATCH 1/2] Implements a Python step_cleanup() method. Continues the work in #2735 on implementing Python bindings for the flow simulator. --- opm/simulators/flow/FlowMainEbos.hpp | 9 +++++++++ opm/simulators/flow/python/simulators.hpp | 7 +++++-- python/simulators/simulators.cpp | 24 ++++++++++++++++++----- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/opm/simulators/flow/FlowMainEbos.hpp b/opm/simulators/flow/FlowMainEbos.hpp index 2618933bf..86026cb7d 100644 --- a/opm/simulators/flow/FlowMainEbos.hpp +++ b/opm/simulators/flow/FlowMainEbos.hpp @@ -335,6 +335,15 @@ namespace Opm return simulator_->runStep(*simtimer_); } + // Called from Python to cleanup after having executed the last + // executeStep() + int executeStepsCleanup() + { + SimulatorReport report = simulator_->finalize(); + runSimulatorAfterSim_(report); + return report.success.exit_status; + } + // Print an ASCII-art header to the PRT and DEBUG files. // \return Whether unkown keywords were seen during parsing. static void printPRTHeader(bool output_cout) diff --git a/opm/simulators/flow/python/simulators.hpp b/opm/simulators/flow/python/simulators.hpp index 2ecd79d5a..588596360 100644 --- a/opm/simulators/flow/python/simulators.hpp +++ b/opm/simulators/flow/python/simulators.hpp @@ -35,13 +35,16 @@ public: BlackOilSimulator( const std::string &deckFilename); int run(); int step(); - int step_init(); + int stepInit(); + int stepCleanup(); private: const std::string deckFilename_; + bool hasRunInit_; + bool hasRunCleanup_; + std::unique_ptr mainEbos_; std::unique_ptr main_; - bool hasRunInit_; }; } // namespace Opm::Python diff --git a/python/simulators/simulators.cpp b/python/simulators/simulators.cpp index e14cd9b17..b552f9e55 100644 --- a/python/simulators/simulators.cpp +++ b/python/simulators/simulators.cpp @@ -18,7 +18,7 @@ namespace py = pybind11; namespace Opm::Pybind { BlackOilSimulator::BlackOilSimulator( const std::string &deckFilename) - : deckFilename_(deckFilename), hasRunInit_(false) + : deckFilename_{deckFilename}, hasRunInit_{false}, hasRunCleanup_{false} { } @@ -33,16 +33,29 @@ int BlackOilSimulator::step() if (!hasRunInit_) { throw std::logic_error("step() called before step_init()"); } + if (hasRunCleanup_) { + throw std::logic_error("step() called after step_cleanup()"); + } return mainEbos_->executeStep(); } -int BlackOilSimulator::step_init() +int BlackOilSimulator::stepCleanup() +{ + hasRunCleanup_ = true; + return mainEbos_->executeStepsCleanup(); +} + +int BlackOilSimulator::stepInit() { if (hasRunInit_) { // Running step_init() multiple times is not implemented yet, - // currently we just do nothing and return - return EXIT_SUCCESS; + if (hasRunCleanup_) { + throw std::logic_error("step_init() called again"); + } + else { + return EXIT_SUCCESS; + } } main_ = std::make_unique( deckFilename_ ); int exitCode = EXIT_SUCCESS; @@ -65,5 +78,6 @@ PYBIND11_MODULE(simulators, m) .def(py::init< const std::string& >()) .def("run", &Opm::Pybind::BlackOilSimulator::run) .def("step", &Opm::Pybind::BlackOilSimulator::step) - .def("step_init", &Opm::Pybind::BlackOilSimulator::step_init); + .def("step_init", &Opm::Pybind::BlackOilSimulator::stepInit) + .def("step_cleanup", &Opm::Pybind::BlackOilSimulator::stepCleanup); } From 53cbe0c57e382c18d9132a8f156837e5f1739cb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20H=C3=A6gland?= Date: Tue, 3 Nov 2020 15:59:53 +0100 Subject: [PATCH 2/2] Initialize boolean variables in-class. Initialize the boolean variables hasRunInit_ and hasRunCleanup_ in the class instead of in the constructor. --- opm/simulators/flow/python/simulators.hpp | 6 +++--- python/simulators/simulators.cpp | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/opm/simulators/flow/python/simulators.hpp b/opm/simulators/flow/python/simulators.hpp index 588596360..07ff65720 100644 --- a/opm/simulators/flow/python/simulators.hpp +++ b/opm/simulators/flow/python/simulators.hpp @@ -40,12 +40,12 @@ public: private: const std::string deckFilename_; - bool hasRunInit_; - bool hasRunCleanup_; + bool hasRunInit_ = false; + bool hasRunCleanup_ = false; std::unique_ptr mainEbos_; std::unique_ptr main_; }; -} // namespace Opm::Python +} // namespace Opm::Pybind #endif // OPM_SIMULATORS_HEADER_INCLUDED diff --git a/python/simulators/simulators.cpp b/python/simulators/simulators.cpp index b552f9e55..38e6c0e0b 100644 --- a/python/simulators/simulators.cpp +++ b/python/simulators/simulators.cpp @@ -18,7 +18,7 @@ namespace py = pybind11; namespace Opm::Pybind { BlackOilSimulator::BlackOilSimulator( const std::string &deckFilename) - : deckFilename_{deckFilename}, hasRunInit_{false}, hasRunCleanup_{false} + : deckFilename_{deckFilename} { }