22#ifndef OPM_FLOW_MAIN_HEADER_INCLUDED
23#define OPM_FLOW_MAIN_HEADER_INCLUDED
25#include <opm/input/eclipse/EclipseState/EclipseState.hpp>
26#include <opm/input/eclipse/EclipseState/IOConfig/IOConfig.hpp>
27#include <opm/input/eclipse/EclipseState/InitConfig/InitConfig.hpp>
36#include <dune/fem/misc/mpimanager.hh>
38#include <dune/common/parallel/mpihelper.hh>
52struct EnableLoggingFalloutWarning {
static constexpr bool value =
false; };
54struct OutputInterval {
static constexpr int value = 1; };
63 template <
class TypeTag>
67 using MaterialLawManager =
typename GetProp<TypeTag, Properties::MaterialLaw>::EclMaterialLawManager;
68 using ModelSimulator = GetPropType<TypeTag, Properties::Simulator>;
69 using Grid = GetPropType<TypeTag, Properties::Grid>;
70 using GridView = GetPropType<TypeTag, Properties::GridView>;
71 using Problem = GetPropType<TypeTag, Properties::Problem>;
72 using Scalar = GetPropType<TypeTag, Properties::Scalar>;
73 using FluidSystem = GetPropType<TypeTag, Properties::FluidSystem>;
75 using Simulator = SimulatorFullyImplicitBlackoil<TypeTag>;
77 FlowMain(
int argc,
char **argv,
bool output_cout,
bool output_files )
78 : argc_{argc}, argv_{argv},
79 output_cout_{output_cout}, output_files_{output_files}
98 Parameters::Register<Parameters::OutputInterval>
99 (
"Specify the number of report steps between two consecutive writes of restart data");
100 Parameters::Register<Parameters::EnableLoggingFalloutWarning>
101 (
"Developer option to see whether logging was on non-root processors. "
102 "In that case it will be appended to the *.DBG or *.PRT files");
108 registerAllParameters_<TypeTag>(
false);
110 detail::hideUnusedParameters<Scalar>();
114 int mpiRank = comm.rank();
117 int status = ::Opm::setupParameters_<TypeTag>(argc,
118 const_cast<const char**
>(argv),
126 int unknownKeyWords = 0;
130 int globalUnknownKeyWords = comm.sum(unknownKeyWords);
131 unknownKeyWords = globalUnknownKeyWords;
132 if ( unknownKeyWords )
136 std::string msg =
"Aborting simulation due to unknown "
137 "parameters. Please query \"flow --help\" for "
138 "supported command line parameters.";
139 if (OpmLog::hasBackend(
"STREAMLOG"))
144 std::cerr << msg << std::endl;
151 if (Parameters::Get<Parameters::PrintParameters>() == 1) {
167 return execute_(&FlowMain::runSimulator,
true);
170 int executeInitStep()
172 return execute_(&FlowMain::runSimulatorInit,
false);
179 return simulator_->runStep(*simtimer_);
184 int executeStepsCleanup()
186 SimulatorReport report = simulator_->finalize();
187 runSimulatorAfterSim_(report);
188 return report.success.exit_status;
191 ModelSimulator* getSimulatorPtr()
193 return modelSimulator_.get();
196 SimulatorTimer* getSimTimer()
198 return simtimer_.get();
202 double getPreviousReportStepSize()
204 return simtimer_->stepLengthTaken();
209 int execute_(
int (FlowMain::* runOrInitFunc)(),
bool cleanup)
211 auto logger = [
this](
const std::exception& e,
const std::string& message_start) {
212 std::ostringstream message;
213 message << message_start << e.what();
215 if (this->output_cout_) {
218 if (OpmLog::hasBackend(
"STREAMLOG")) {
219 OpmLog::error(message.str());
222 std::cout << message.str() <<
"\n";
232 Dune::Timer setupTimerAfterReadingDeck;
233 setupTimerAfterReadingDeck.start();
241 setupModelSimulator();
244 this->deck_read_time_ = modelSimulator_->vanguard().setupTime();
245 this->total_setup_time_ = setupTimerAfterReadingDeck.elapsed() + this->deck_read_time_;
248 int exitCode = (this->*runOrInitFunc)();
254 catch (
const TimeSteppingBreakdown& e) {
255 auto exitCode = logger(e,
"Simulation aborted: ");
259 catch (
const std::exception& e) {
260 auto exitCode = logger(e,
"Simulation aborted as program threw an unexpected exception: ");
266 void executeCleanup_() {
272 void setupParallelism()
278 mpi_rank_ = comm.rank();
279 mpi_size_ = comm.size();
286 constexpr int default_threads = 2;
287 const int requested_threads = Parameters::Get<Parameters::ThreadsPerProcess>();
288 int threads = requested_threads > 0 ? requested_threads : default_threads;
290 const char* env_var = getenv(
"OMP_NUM_THREADS");
292 int omp_num_threads = -1;
293 auto result = std::from_chars(env_var, env_var + std::strlen(env_var), omp_num_threads);
294 if (result.ec == std::errc() && omp_num_threads > 0) {
296 threads = omp_num_threads;
302 OpmLog::warning(
"Invalid value for OMP_NUM_THREADS environment variable.");
309 omp_set_num_threads(threads);
312 using TM = GetPropType<TypeTag, Properties::ThreadManager>;
319 OpmLog::removeAllBackends();
321 if (mpi_rank_ != 0 || mpi_size_ < 2 || !this->output_files_ || !modelSimulator_) {
326 Parameters::Get<Parameters::EclDeckFileName>(),
327 Parameters::Get<Parameters::EnableLoggingFalloutWarning>());
330 void setupModelSimulator()
333 modelSimulator_->executionTimer().start();
334 modelSimulator_->model().applyInitialSolution();
337 const EclipseState& eclState()
const
338 {
return modelSimulator_->vanguard().eclState(); }
340 EclipseState& eclState()
341 {
return modelSimulator_->vanguard().eclState(); }
343 const Schedule& schedule()
const
344 {
return modelSimulator_->vanguard().schedule(); }
349 return runSimulatorInitOrRun_(&FlowMain::runSimulatorRunCallback_);
352 int runSimulatorInit()
354 return runSimulatorInitOrRun_(&FlowMain::runSimulatorInitCallback_);
359 int runSimulatorRunCallback_()
361 SimulatorReport report = simulator_->run(*simtimer_);
362 runSimulatorAfterSim_(report);
363 return report.success.exit_status;
367 int runSimulatorInitCallback_()
369 simulator_->init(*simtimer_);
374 void runSimulatorAfterSim_(SimulatorReport &report)
376 if (! this->output_cout_) {
381#if !defined(_OPENMP) || !_OPENMP
384 = omp_get_max_threads();
387 printFlowTrailer(mpi_size_, threads, total_setup_time_, deck_read_time_, report, simulator_->model().localAccumulatedReports());
390 Parameters::Get<Parameters::OutputExtraConvergenceInfo>(),
391 R
"(OutputExtraConvergenceInfo (--output-extra-convergence-info))",
392 eclState().getIOConfig().getOutputDir(),
393 eclState().getIOConfig().getBaseName());
397 int runSimulatorInitOrRun_(
int (FlowMain::* initOrRunFunc)())
400 const auto& schedule = this->schedule();
401 auto& ioConfig = eclState().getIOConfig();
402 simtimer_ = std::make_unique<SimulatorTimer>();
405 const auto& initConfig = eclState().getInitConfig();
406 simtimer_->init(schedule,
static_cast<std::size_t
>(initConfig.getRestartStep()));
408 if (this->output_cout_) {
409 std::ostringstream oss;
414 std::cout <<
"----------------- Unrecognized parameters: -----------------\n";
415 std::cout << oss.str();
416 std::cout <<
"----------------------------------------------------------------" << std::endl;
420 if (!ioConfig.initOnly()) {
421 if (this->output_cout_) {
423 msg =
"\n\n================ Starting main simulation loop ===============\n";
427 return (this->*initOrRunFunc)();
430 if (this->output_cout_) {
431 std::cout <<
"\n\n================ Simulation turned off ===============\n" << std::flush;
443 void createSimulator()
446 simulator_ = std::make_unique<Simulator>(*modelSimulator_);
450 {
return modelSimulator_->vanguard().grid(); }
453 std::unique_ptr<ModelSimulator> modelSimulator_;
456 std::any parallel_information_;
457 std::unique_ptr<Simulator> simulator_;
458 std::unique_ptr<SimulatorTimer> simtimer_;
463 double total_setup_time_ = 0.0;
464 double deck_read_time_ = 0.0;
static Parallel::Communication & comm()
Obtain global communicator.
Definition: FlowGenericVanguard.hpp:306
static void registerParameters()
Registers all runtime parameters used by the simulation.
Definition: simulator.hh:240
static void registerParameters()
Register all run-time parameters of the thread manager.
Dune::Communication< MPIComm > Communication
Definition: ParallelCommunication.hpp:30
Definition: blackoilnewtonmethodparams.hpp:31
void printValues(std::ostream &os)
Print values of the run-time parameters.
bool IsRegistrationOpen()
Query whether parameter registration is open or not.
void endRegistration()
Indicate that all parameters are registered for a given type tag.
bool printUnused(std::ostream &os)
Print the list of unused run-time parameters.
void mergeParallelLogFiles(std::string_view output_dir, std::string_view deck_filename, bool enableLoggingFalloutWarning)
void checkAllMPIProcesses()
void handleExtraConvergenceOutput(SimulatorReport &report, std::string_view option, std::string_view optionName, std::string_view output_dir, std::string_view base_name)
Definition: blackoilboundaryratevector.hh:37
void printFlowTrailer(int nprocs, int nthreads, const double total_setup_time, const double deck_read_time, const SimulatorReport &report, const SimulatorReportSingle &localsolves_report)
Provides convenience routines to bring up the simulation at runtime.