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>
29#include <opm/models/utils/start.hh>
36#include <dune/fem/misc/mpimanager.hh>
38#include <dune/common/parallel/mpihelper.hh>
46template<
class TypeTag,
class MyTypeTag>
48 using type = UndefinedProperty;
50template<
class TypeTag,
class MyTypeTag>
52 using type = UndefinedProperty;
54template<
class TypeTag,
class MyTypeTag>
56 using type = UndefinedProperty;
60template<
class TypeTag>
62 static constexpr auto value =
"auto";
65template<
class TypeTag>
67 static constexpr bool value =
false;
69template<
class TypeTag>
71 static constexpr int value = 1;
81 template <
class TypeTag>
85 using MaterialLawManager =
typename GetProp<TypeTag, Properties::MaterialLaw>::EclMaterialLawManager;
87 using Grid = GetPropType<TypeTag, Properties::Grid>;
88 using GridView = GetPropType<TypeTag, Properties::GridView>;
89 using Problem = GetPropType<TypeTag, Properties::Problem>;
90 using Scalar = GetPropType<TypeTag, Properties::Scalar>;
91 using FluidSystem = GetPropType<TypeTag, Properties::FluidSystem>;
95 FlowMain(
int argc,
char **argv,
bool output_cout,
bool output_files )
96 : argc_{argc}, argv_{argv},
97 output_cout_{output_cout}, output_files_{output_files}
105 using ParamsMeta = GetProp<TypeTag, Properties::ParameterMetaData>;
106 if (!ParamsMeta::registrationOpen()) {
117 Parameters::registerParam<TypeTag, Properties::EnableDryRun>
118 (
"Specify if the simulation ought to be actually run, or just pretended to be");
119 Parameters::registerParam<TypeTag, Properties::OutputInterval>
120 (
"Specify the number of report steps between two consecutive writes of restart data");
121 Parameters::registerParam<TypeTag, Properties::EnableLoggingFalloutWarning>
122 (
"Developer option to see whether logging was on non-root processors. "
123 "In that case it will be appended to the *.DBG or *.PRT files");
128 registerAllParameters_<TypeTag>(
false);
131 Parameters::hideParam<TypeTag, Properties::EnableGravity>();
132 Parameters::hideParam<TypeTag, Properties::EnableGridAdaptation>();
137 Parameters::hideParam<TypeTag, Properties::EnableIntensiveQuantityCache>();
141 Parameters::hideParam<TypeTag, Properties::EnableThermodynamicHints>();
144 Parameters::hideParam<TypeTag, Properties::EndTime>();
147 Parameters::hideParam<TypeTag, Properties::InitialTimeStepSize>();
148 Parameters::hideParam<TypeTag, Properties::MaxTimeStepDivisions>();
149 Parameters::hideParam<TypeTag, Properties::MaxTimeStepSize>();
150 Parameters::hideParam<TypeTag, Properties::MinTimeStepSize>();
151 Parameters::hideParam<TypeTag, Properties::PredeterminedTimeStepsFile>();
154 Parameters::hideParam<TypeTag, Properties::NewtonMaxError>();
155 Parameters::hideParam<TypeTag, Properties::NewtonTolerance>();
156 Parameters::hideParam<TypeTag, Properties::NewtonTargetIterations>();
157 Parameters::hideParam<TypeTag, Properties::NewtonVerbose>();
158 Parameters::hideParam<TypeTag, Properties::NewtonWriteConvergence>();
161 Parameters::hideParam<TypeTag, Properties::RestartTime>();
162 Parameters::hideParam<TypeTag, Properties::RestartWritingInterval>();
165 Parameters::hideParam<TypeTag, Properties::VtkWriteOilFormationVolumeFactor>();
166 Parameters::hideParam<TypeTag, Properties::VtkWriteOilSaturationPressure>();
167 Parameters::hideParam<TypeTag, Properties::VtkWriteOilVaporizationFactor>();
168 Parameters::hideParam<TypeTag, Properties::VtkWritePorosity>();
169 Parameters::hideParam<TypeTag, Properties::VtkWritePotentialGradients>();
170 Parameters::hideParam<TypeTag, Properties::VtkWritePressures>();
171 Parameters::hideParam<TypeTag, Properties::VtkWritePrimaryVars>();
172 Parameters::hideParam<TypeTag, Properties::VtkWritePrimaryVarsMeaning>();
173 Parameters::hideParam<TypeTag, Properties::VtkWriteProcessRank>();
174 Parameters::hideParam<TypeTag, Properties::VtkWriteRelativePermeabilities>();
175 Parameters::hideParam<TypeTag, Properties::VtkWriteSaturatedGasOilVaporizationFactor>();
176 Parameters::hideParam<TypeTag, Properties::VtkWriteSaturatedOilGasDissolutionFactor>();
177 Parameters::hideParam<TypeTag, Properties::VtkWriteSaturationRatios>();
178 Parameters::hideParam<TypeTag, Properties::VtkWriteSaturations>();
179 Parameters::hideParam<TypeTag, Properties::VtkWriteTemperature>();
180 Parameters::hideParam<TypeTag, Properties::VtkWriteViscosities>();
181 Parameters::hideParam<TypeTag, Properties::VtkWriteWaterFormationVolumeFactor>();
182 Parameters::hideParam<TypeTag, Properties::VtkWriteGasDissolutionFactor>();
183 Parameters::hideParam<TypeTag, Properties::VtkWriteGasFormationVolumeFactor>();
184 Parameters::hideParam<TypeTag, Properties::VtkWriteGasSaturationPressure>();
185 Parameters::hideParam<TypeTag, Properties::VtkWriteIntrinsicPermeabilities>();
186 Parameters::hideParam<TypeTag, Properties::VtkWriteTracerConcentration>();
187 Parameters::hideParam<TypeTag, Properties::VtkWriteExtrusionFactor>();
188 Parameters::hideParam<TypeTag, Properties::VtkWriteFilterVelocities>();
189 Parameters::hideParam<TypeTag, Properties::VtkWriteDensities>();
190 Parameters::hideParam<TypeTag, Properties::VtkWriteDofIndex>();
191 Parameters::hideParam<TypeTag, Properties::VtkWriteMobilities>();
193 Parameters::hideParam<TypeTag, Properties::VtkWriteAverageMolarMasses>();
194 Parameters::hideParam<TypeTag, Properties::VtkWriteFugacities>();
195 Parameters::hideParam<TypeTag, Properties::VtkWriteFugacityCoeffs>();
196 Parameters::hideParam<TypeTag, Properties::VtkWriteMassFractions>();
197 Parameters::hideParam<TypeTag, Properties::VtkWriteMolarities>();
198 Parameters::hideParam<TypeTag, Properties::VtkWriteMoleFractions>();
199 Parameters::hideParam<TypeTag, Properties::VtkWriteTotalMassFractions>();
200 Parameters::hideParam<TypeTag, Properties::VtkWriteTotalMoleFractions>();
202 Parameters::hideParam<TypeTag, Properties::VtkWriteTortuosities>();
203 Parameters::hideParam<TypeTag, Properties::VtkWriteDiffusionCoefficients>();
204 Parameters::hideParam<TypeTag, Properties::VtkWriteEffectiveDiffusionCoefficients>();
207 Parameters::hideParam<TypeTag, Properties::UseAverageDensityMsWells>();
209 Parameters::endParamRegistration<TypeTag>();
211 int mpiRank = comm.rank();
214 int status = ::Opm::setupParameters_<TypeTag>(argc,
const_cast<const char**
>(argv),
false,
true, (mpiRank==0));
219 int unknownKeyWords = 0;
221 unknownKeyWords = Parameters::printUnused<TypeTag>(std::cerr);
223 int globalUnknownKeyWords = comm.sum(unknownKeyWords);
224 unknownKeyWords = globalUnknownKeyWords;
225 if ( unknownKeyWords )
229 std::string msg =
"Aborting simulation due to unknown "
230 "parameters. Please query \"flow --help\" for "
231 "supported command line parameters.";
232 if (OpmLog::hasBackend(
"STREAMLOG"))
237 std::cerr << msg << std::endl;
247 if (Parameters::get<TypeTag, Properties::PrintProperties>() == 1) {
250 Properties::printValues<TypeTag>(std::cout);
253 if (Parameters::get<TypeTag, Properties::PrintParameters>() == 1) {
256 Parameters::printValues<TypeTag>();
283 return simulator_->runStep(*simtimer_);
291 runSimulatorAfterSim_(report);
297 return modelSimulator_.get();
302 return simtimer_.get();
308 return simtimer_->stepLengthTaken();
313 int execute_(
int (
FlowMain::* runOrInitFunc)(),
bool cleanup)
315 auto logger = [
this](
const std::exception& e,
const std::string& message_start) {
316 std::ostringstream message;
317 message << message_start << e.what();
319 if (this->output_cout_) {
322 if (OpmLog::hasBackend(
"STREAMLOG")) {
323 OpmLog::error(message.str());
326 std::cout << message.str() <<
"\n";
336 Dune::Timer setupTimerAfterReadingDeck;
337 setupTimerAfterReadingDeck.start();
348 this->deck_read_time_ = modelSimulator_->vanguard().setupTime();
349 this->total_setup_time_ = setupTimerAfterReadingDeck.elapsed() + this->deck_read_time_;
352 int exitCode = (this->*runOrInitFunc)();
358 catch (
const TimeSteppingBreakdown& e) {
359 auto exitCode = logger(e,
"Simulation aborted: ");
363 catch (
const std::exception& e) {
364 auto exitCode = logger(e,
"Simulation aborted as program threw an unexpected exception: ");
370 void executeCleanup_() {
382 mpi_rank_ = comm.rank();
383 mpi_size_ = comm.size();
388 if (!getenv(
"OMP_NUM_THREADS"))
391 const int requested_threads = Parameters::get<TypeTag, Properties::ThreadsPerProcess>();
392 if (requested_threads > 0)
393 threads = requested_threads;
398 omp_set_num_threads(threads);
402 using ThreadManager = GetPropType<TypeTag, Properties::ThreadManager>;
403 ThreadManager::init(
false);
409 OpmLog::removeAllBackends();
411 if (mpi_rank_ != 0 || mpi_size_ < 2 || !this->output_files_ || !modelSimulator_) {
416 Parameters::get<TypeTag, Properties::EclDeckFileName>(),
417 Parameters::get<TypeTag, Properties::EnableLoggingFalloutWarning>());
423 modelSimulator_->executionTimer().start();
424 modelSimulator_->model().applyInitialSolution();
428 const std::string& dryRunString = Parameters::get<TypeTag, Properties::EnableDryRun>();
429 if (dryRunString !=
"" && dryRunString !=
"auto") {
431 if (dryRunString ==
"true"
432 || dryRunString ==
"t"
433 || dryRunString ==
"1")
435 else if (dryRunString ==
"false"
436 || dryRunString ==
"f"
437 || dryRunString ==
"0")
440 throw std::invalid_argument(
"Invalid value for parameter EnableDryRun: '"
442 auto& ioConfig =
eclState().getIOConfig();
443 ioConfig.overrideNOSIM(yesno);
446 catch (
const std::invalid_argument& e) {
447 std::cerr <<
"Failed to create valid EclipseState object" << std::endl;
448 std::cerr <<
"Exception caught: " << e.what() << std::endl;
454 {
return modelSimulator_->vanguard().eclState(); }
457 {
return modelSimulator_->vanguard().eclState(); }
460 {
return modelSimulator_->vanguard().schedule(); }
465 return runSimulatorInitOrRun_(&FlowMain::runSimulatorRunCallback_);
470 return runSimulatorInitOrRun_(&FlowMain::runSimulatorInitCallback_);
475 int runSimulatorRunCallback_()
478 runSimulatorAfterSim_(report);
483 int runSimulatorInitCallback_()
485 simulator_->init(*simtimer_);
490 void runSimulatorAfterSim_(SimulatorReport &report)
492 if (! this->output_cout_) {
497#if !defined(_OPENMP) || !_OPENMP
500 = omp_get_max_threads();
503 printFlowTrailer(mpi_size_, threads, total_setup_time_, deck_read_time_, report, simulator_->model().localAccumulatedReports());
506 Parameters::get<TypeTag, Properties::OutputExtraConvergenceInfo>(),
507 R
"(OutputExtraConvergenceInfo (--output-extra-convergence-info))",
508 eclState().getIOConfig().getOutputDir(),
509 eclState().getIOConfig().getBaseName());
513 int runSimulatorInitOrRun_(
int (
FlowMain::* initOrRunFunc)())
517 auto& ioConfig =
eclState().getIOConfig();
518 simtimer_ = std::make_unique<SimulatorTimer>();
521 const auto& initConfig =
eclState().getInitConfig();
522 simtimer_->init(
schedule,
static_cast<std::size_t
>(initConfig.getRestartStep()));
524 if (this->output_cout_) {
525 std::ostringstream oss;
529 if (Parameters::printUnused<TypeTag>(oss)) {
530 std::cout <<
"----------------- Unrecognized parameters: -----------------\n";
531 std::cout << oss.str();
532 std::cout <<
"----------------------------------------------------------------" << std::endl;
536 if (!ioConfig.initOnly()) {
537 if (this->output_cout_) {
539 msg =
"\n\n================ Starting main simulation loop ===============\n";
543 return (this->*initOrRunFunc)();
546 if (this->output_cout_) {
547 std::cout <<
"\n\n================ Simulation turned off ===============\n" << std::flush;
562 simulator_ = std::make_unique<Simulator>(*modelSimulator_);
566 {
return modelSimulator_->vanguard().grid(); }
569 std::unique_ptr<ModelSimulator> modelSimulator_;
572 std::any parallel_information_;
573 std::unique_ptr<Simulator> simulator_;
574 std::unique_ptr<SimulatorTimer> simtimer_;
579 double total_setup_time_ = 0.0;
580 double deck_read_time_ = 0.0;
static Parallel::Communication & comm()
Obtain global communicator.
Definition: FlowGenericVanguard.hpp:255
Definition: FlowMain.hpp:83
int executeStep()
Definition: FlowMain.hpp:281
const Schedule & schedule() const
Definition: FlowMain.hpp:459
FlowMain(int argc, char **argv, bool output_cout, bool output_files)
Definition: FlowMain.hpp:95
EclipseState & eclState()
Definition: FlowMain.hpp:456
int executeInitStep()
Definition: FlowMain.hpp:274
Grid & grid()
Definition: FlowMain.hpp:565
static int setupParameters_(int argc, char **argv, Parallel::Communication comm)
Definition: FlowMain.hpp:103
int runSimulatorInit()
Definition: FlowMain.hpp:468
GetPropType< TypeTag, Properties::Simulator > ModelSimulator
Definition: FlowMain.hpp:86
int executeStepsCleanup()
Definition: FlowMain.hpp:288
GetPropType< TypeTag, Properties::Grid > Grid
Definition: FlowMain.hpp:87
void setupModelSimulator()
Definition: FlowMain.hpp:420
void mergeParallelLogFiles()
Definition: FlowMain.hpp:406
GetPropType< TypeTag, Properties::Problem > Problem
Definition: FlowMain.hpp:89
double getPreviousReportStepSize()
Get the size of the previous report step.
Definition: FlowMain.hpp:306
ModelSimulator * getSimulatorPtr()
Definition: FlowMain.hpp:295
const EclipseState & eclState() const
Definition: FlowMain.hpp:453
int execute()
Definition: FlowMain.hpp:269
GetPropType< TypeTag, Properties::FluidSystem > FluidSystem
Definition: FlowMain.hpp:91
void createSimulator()
This is the main function of Flow.
Definition: FlowMain.hpp:559
GetPropType< TypeTag, Properties::Scalar > Scalar
Definition: FlowMain.hpp:90
GetPropType< TypeTag, Properties::GridView > GridView
Definition: FlowMain.hpp:88
typename GetProp< TypeTag, Properties::MaterialLaw >::EclMaterialLawManager MaterialLawManager
Definition: FlowMain.hpp:85
void setupParallelism()
Definition: FlowMain.hpp:376
int runSimulator()
Definition: FlowMain.hpp:463
SimulatorTimer * getSimTimer()
Definition: FlowMain.hpp:300
This problem simulates an input file given in the data format used by the commercial ECLiPSE simulato...
Definition: FlowProblem.hpp:112
a simulator for the blackoil model
Definition: SimulatorFullyImplicitBlackoil.hpp:143
static void registerParameters()
Definition: SimulatorFullyImplicitBlackoil.hpp:215
Definition: SimulatorTimer.hpp:39
Dune::Communication< MPIComm > Communication
Definition: ParallelCommunication.hpp:30
Definition: AluGridVanguard.hpp:57
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: BlackoilPhases.hpp:27
void printFlowTrailer(int nprocs, int nthreads, const double total_setup_time, const double deck_read_time, const SimulatorReport &report, const SimulatorReportSingle &localsolves_report)
Definition: FlowMain.hpp:47
UndefinedProperty type
Definition: FlowMain.hpp:48
Definition: FlowMain.hpp:55
UndefinedProperty type
Definition: FlowMain.hpp:56
Definition: FlowMain.hpp:51
UndefinedProperty type
Definition: FlowMain.hpp:52
Definition: SimulatorReport.hpp:100
SimulatorReportSingle success
Definition: SimulatorReport.hpp:101
int exit_status
Definition: SimulatorReport.hpp:56