From 11eaa3d7d44b5bd974c8101a287e2c1aeb2bd067 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20H=C3=A6gland?= Date: Mon, 2 Mar 2020 11:56:50 +0100 Subject: [PATCH] Implement Python step_init() method. The step_init() method initializes the simulation. It is required for the Python script to run step_init() before calling the step() method (which will be implemented in the next commit). --- flow/flow_ebos_blackoil.cpp | 4 +-- flow/flow_ebos_blackoil.hpp | 3 ++ opm/simulators/flow/FlowMainEbos.hpp | 54 ++++++++++++++++++---------- python/simulators/simulators.cpp | 25 ++++++++++++- 4 files changed, 64 insertions(+), 22 deletions(-) diff --git a/flow/flow_ebos_blackoil.cpp b/flow/flow_ebos_blackoil.cpp index f9d986807..e75a9bccc 100644 --- a/flow/flow_ebos_blackoil.cpp +++ b/flow/flow_ebos_blackoil.cpp @@ -47,7 +47,7 @@ void flowEbosBlackoilSetDeck(double setupTime, Deck &deck, EclipseState& eclStat } std::unique_ptr> -flowEbosBlackoilMain_init(int argc, char** argv, bool outputCout, bool outputFiles) +flowEbosBlackoilMainInit(int argc, char** argv, bool outputCout, bool outputFiles) { // we always want to use the default locale, and thus spare us the trouble // with incorrect locale settings. @@ -65,7 +65,7 @@ flowEbosBlackoilMain_init(int argc, char** argv, bool outputCout, bool outputFil // ----------------- Main program ----------------- int flowEbosBlackoilMain(int argc, char** argv, bool outputCout, bool outputFiles) { - auto mainfunc = flowEbosBlackoilMain_init(argc, argv, outputCout, outputFiles); + auto mainfunc = flowEbosBlackoilMainInit(argc, argv, outputCout, outputFiles); return mainfunc->execute(argc, argv, outputCout, outputFiles); } diff --git a/flow/flow_ebos_blackoil.hpp b/flow/flow_ebos_blackoil.hpp index 703326fc8..6e8e2a297 100644 --- a/flow/flow_ebos_blackoil.hpp +++ b/flow/flow_ebos_blackoil.hpp @@ -21,10 +21,13 @@ #include #include #include +#include namespace Opm { void flowEbosBlackoilSetDeck(double setupTime, Deck &deck, EclipseState& eclState, Schedule& schedule, SummaryConfig& summaryConfig); int flowEbosBlackoilMain(int argc, char** argv, bool outputCout, bool outputFiles); +std::unique_ptr> + flowEbosBlackoilMainInit(int argc, char** argv, bool outputCout, bool outputFiles); } #endif // FLOW_EBOS_BLACKOIL_HPP diff --git a/opm/simulators/flow/FlowMainEbos.hpp b/opm/simulators/flow/FlowMainEbos.hpp index e3c2dd351..4dd4f959c 100644 --- a/opm/simulators/flow/FlowMainEbos.hpp +++ b/opm/simulators/flow/FlowMainEbos.hpp @@ -243,12 +243,17 @@ namespace Opm /// This is the main function of Flow. It runs a complete simulation with the /// given grid and simulator classes, based on the user-specified command-line /// input. - int execute(int argc, char** argv, bool output_cout, bool output_to_files) + int execute(int argc, char** argv, bool outputCout, bool outputToFiles) { - return execute_(argc, argv, output_cout, output_to_files, + return execute_(argc, argv, outputCout, outputToFiles, &FlowMainEbos::runSimulator); } + int executeInitStep(int argc, char** argv, bool outputCout, bool outputToFiles) + { + return execute_(argc, argv, outputCout, outputToFiles, + &FlowMainEbos::runSimulatorInit); + } // Print an ASCII-art header to the PRT and DEBUG files. // \return Whether unkown keywords were seen during parsing. @@ -296,7 +301,7 @@ namespace Opm private: // called by execute() or executeInitStep() - int execute_(int argc, char** argv, bool output_cout, bool output_to_files, + int execute_(int argc, char** argv, bool outputCout, bool outputToFiles, void (FlowMainEbos::* runOrInitFunc)(bool)) { try { @@ -307,15 +312,15 @@ namespace Opm return status; setupParallelism(); - setupEbosSimulator(output_cout); - runDiagnostics(output_cout); + setupEbosSimulator(outputCout); + runDiagnostics(outputCout); createSimulator(); // if run, do the actual work, else just initialize - (this->*runOrInitFunc)(output_cout); + (this->*runOrInitFunc)(outputCout); // clean up - mergeParallelLogFiles(output_to_files); + mergeParallelLogFiles(outputToFiles); return EXIT_SUCCESS; } @@ -323,7 +328,7 @@ namespace Opm std::ostringstream message; message << "Program threw an exception: " << e.what(); - if (output_cout) { + if (outputCout) { // in some cases exceptions are thrown before the logging system is set // up. if (OpmLog::hasBackend("STREAMLOG")) { @@ -467,24 +472,35 @@ namespace Opm } // Run the simulator. - void runSimulator(bool output_cout) + void runSimulator(bool outputCout) { - runSimulatorInitOrRun_(output_cout, &FlowMainEbos::runSimulatorRunCallback_); + runSimulatorInitOrRun_(outputCout, &FlowMainEbos::runSimulatorRunCallback_); + } + + void runSimulatorInit(bool outputCout) + { + runSimulatorInitOrRun_(outputCout, &FlowMainEbos::runSimulatorInitCallback_); } private: // Callback that will be called from runSimulatorInitOrRun_(). - void runSimulatorRunCallback_(bool output_cout) + void runSimulatorRunCallback_(bool outputCout) { SimulatorReport successReport = simulator_->run(*simtimer_); - runSimulatorAfterSim_(output_cout, successReport); + runSimulatorAfterSim_(outputCout, successReport); + } + + // Callback that will be called from runSimulatorInitOrRun_(). + void runSimulatorInitCallback_(bool outputCout) + { + simulator_->runInit(*simtimer_); } // Output summary after simulation has completed - void runSimulatorAfterSim_(bool output_cout, SimulatorReport &successReport) + void runSimulatorAfterSim_(bool outputCout, SimulatorReport &successReport) { SimulatorReport failureReport = simulator_->failureReport(); - if (output_cout) { + if (outputCout) { std::ostringstream ss; ss << "\n\n================ End of simulation ===============\n\n"; ss << "Number of MPI processes: " << std::setw(6) << mpi_size_ << "\n"; @@ -501,7 +517,7 @@ namespace Opm // Run the simulator. void runSimulatorInitOrRun_( - bool output_cout, void (FlowMainEbos::* initOrRunFunc)(bool)) + bool outputCout, void (FlowMainEbos::* initOrRunFunc)(bool)) { const auto& schedule = this->schedule(); const auto& timeMap = schedule.getTimeMap(); @@ -512,7 +528,7 @@ namespace Opm const auto& initConfig = eclState().getInitConfig(); simtimer_->init(timeMap, (size_t)initConfig.getRestartStep()); - if (output_cout) { + if (outputCout) { std::ostringstream oss; // This allows a user to catch typos and misunderstandings in the @@ -525,15 +541,15 @@ namespace Opm } if (!ioConfig.initOnly()) { - if (output_cout) { + if (outputCout) { std::string msg; msg = "\n\n================ Starting main simulation loop ===============\n"; OpmLog::info(msg); } - (this->*initOrRunFunc)(output_cout); + (this->*initOrRunFunc)(outputCout); } else { - if (output_cout) { + if (outputCout) { std::cout << "\n\n================ Simulation turned off ===============\n" << std::flush; } } diff --git a/python/simulators/simulators.cpp b/python/simulators/simulators.cpp index 454b05718..d4248295d 100644 --- a/python/simulators/simulators.cpp +++ b/python/simulators/simulators.cpp @@ -276,6 +276,25 @@ public: } } + int step_init() + { + if (hasRunInit_) { + // Running step_init() multiple times is not implemented yet, + // currently we just do nothing and return + return EXIT_SUCCESS; + } + if (prepareRun_()) { + mainfunc_ = Opm::flowEbosBlackoilMainInit( + argc_, argv_, outputCout_, outputFiles_); + int result = mainfunc_->executeInitStep( + argc_, argv_, outputCout_, outputFiles_); + hasRunInit_ = true; + return result; + } + else { + return EXIT_FAILURE; + } + } private: bool prepareRun_() @@ -433,11 +452,14 @@ private: char **argv_; bool outputCout_; // copy of EWOMS parameter "EnableTerminalOutput" bool outputFiles_; // output files? + bool hasRunInit_{false}; std::shared_ptr deck_; std::shared_ptr eclipseState_; std::shared_ptr schedule_; std::shared_ptr summaryConfig_; + + std::unique_ptr> mainfunc_; }; PYBIND11_MODULE(simulators, m) @@ -449,5 +471,6 @@ PYBIND11_MODULE(simulators, m) .def("set_deck", &BlackOilSimulator::setDeck) .def("set_eclipse_state", &BlackOilSimulator::setEclipseState) .def("set_schedule", &BlackOilSimulator::setSchedule) - .def("set_summary_config", &BlackOilSimulator::setSummaryConfig); + .def("set_summary_config", &BlackOilSimulator::setSummaryConfig) + .def("step_init", &BlackOilSimulator::step_init); }