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).
This commit is contained in:
Håkon Hægland 2020-03-02 11:56:50 +01:00
parent b125ad415d
commit 11eaa3d7d4
4 changed files with 64 additions and 22 deletions

View File

@ -47,7 +47,7 @@ void flowEbosBlackoilSetDeck(double setupTime, Deck &deck, EclipseState& eclStat
}
std::unique_ptr<Opm::FlowMainEbos<TTAG(EclFlowProblem)>>
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);
}

View File

@ -21,10 +21,13 @@
#include <opm/parser/eclipse/EclipseState/EclipseState.hpp>
#include <opm/parser/eclipse/EclipseState/Schedule/Schedule.hpp>
#include <opm/parser/eclipse/EclipseState/SummaryConfig/SummaryConfig.hpp>
#include <opm/simulators/flow/FlowMainEbos.hpp>
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<Opm::FlowMainEbos<TTAG(EclFlowProblem)>>
flowEbosBlackoilMainInit(int argc, char** argv, bool outputCout, bool outputFiles);
}
#endif // FLOW_EBOS_BLACKOIL_HPP

View File

@ -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;
}
}

View File

@ -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<Opm::Deck> deck_;
std::shared_ptr<Opm::EclipseState> eclipseState_;
std::shared_ptr<Opm::Schedule> schedule_;
std::shared_ptr<Opm::SummaryConfig> summaryConfig_;
std::unique_ptr<Opm::FlowMainEbos<TTAG(EclFlowProblem)>> 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);
}