22 #ifndef OPM_FLOW_MAIN_EBOS_HEADER_INCLUDED
23 #define OPM_FLOW_MAIN_EBOS_HEADER_INCLUDED
26 #include <sys/utsname.h>
28 #include <opm/simulators/flow/SimulatorFullyImplicitBlackoilEbos.hpp>
29 #include <opm/simulators/utils/ParallelFileMerger.hpp>
30 #include <opm/simulators/utils/moduleVersion.hpp>
31 #include <opm/simulators/utils/ParallelEclipseState.hpp>
33 #include <opm/input/eclipse/EclipseState/EclipseState.hpp>
34 #include <opm/input/eclipse/EclipseState/IOConfig/IOConfig.hpp>
35 #include <opm/input/eclipse/EclipseState/InitConfig/InitConfig.hpp>
36 #include <opm/input/eclipse/Schedule/UDQ/UDQState.hpp>
37 #include <opm/common/utility/String.hpp>
39 #include <fmt/format.h>
43 #include <dune/fem/misc/mpimanager.hh>
45 #include <dune/common/parallel/mpihelper.hh>
48 namespace Opm::Properties {
50 template<
class TypeTag,
class MyTypeTag>
52 using type = UndefinedProperty;
54 template<
class TypeTag,
class MyTypeTag>
56 using type = UndefinedProperty;
58 template<
class TypeTag,
class MyTypeTag>
60 using type = UndefinedProperty;
64 template<
class TypeTag>
66 static constexpr
auto value =
"auto";
69 template<
class TypeTag>
71 static constexpr
bool value =
false;
73 template<
class TypeTag>
75 static constexpr
int value = 1;
86 template <
class TypeTag>
90 using MaterialLawManager =
typename GetProp<TypeTag, Properties::MaterialLaw>::EclMaterialLawManager;
91 using EbosSimulator = GetPropType<TypeTag, Properties::Simulator>;
92 using Grid = GetPropType<TypeTag, Properties::Grid>;
93 using GridView = GetPropType<TypeTag, Properties::GridView>;
94 using Problem = GetPropType<TypeTag, Properties::Problem>;
95 using Scalar = GetPropType<TypeTag, Properties::Scalar>;
96 using FluidSystem = GetPropType<TypeTag, Properties::FluidSystem>;
100 FlowMainEbos(
int argc,
char **argv,
bool output_cout,
bool output_files )
101 : argc_{argc}, argv_{argv},
102 output_cout_{output_cout}, output_files_{output_files}
108 static int setupParameters_(
int argc,
char** argv, Parallel::Communication comm)
110 using ParamsMeta = GetProp<TypeTag, Properties::ParameterMetaData>;
111 if (!ParamsMeta::registrationOpen()) {
122 EWOMS_REGISTER_PARAM(TypeTag, std::string, EnableDryRun,
123 "Specify if the simulation ought to be actually run, or just pretended to be");
124 EWOMS_REGISTER_PARAM(TypeTag,
int, OutputInterval,
125 "Specify the number of report steps between two consecutive writes of restart data");
126 EWOMS_REGISTER_PARAM(TypeTag,
bool, EnableLoggingFalloutWarning,
127 "Developer option to see whether logging was on non-root processors. In that case it will be appended to the *.DBG or *.PRT files");
129 Simulator::registerParameters();
132 registerAllParameters_<TypeTag>(
false);
135 EWOMS_HIDE_PARAM(TypeTag, EnableGravity);
136 EWOMS_HIDE_PARAM(TypeTag, EnableGridAdaptation);
141 EWOMS_HIDE_PARAM(TypeTag, EnableIntensiveQuantityCache);
145 EWOMS_HIDE_PARAM(TypeTag, EnableThermodynamicHints);
148 EWOMS_HIDE_PARAM(TypeTag, EndTime);
151 EWOMS_HIDE_PARAM(TypeTag, InitialTimeStepSize);
152 EWOMS_HIDE_PARAM(TypeTag, MaxTimeStepDivisions);
153 EWOMS_HIDE_PARAM(TypeTag, MaxTimeStepSize);
154 EWOMS_HIDE_PARAM(TypeTag, MinTimeStepSize);
155 EWOMS_HIDE_PARAM(TypeTag, PredeterminedTimeStepsFile);
157 EWOMS_HIDE_PARAM(TypeTag, EclMaxTimeStepSizeAfterWellEvent);
158 EWOMS_HIDE_PARAM(TypeTag, EclRestartShrinkFactor);
159 EWOMS_HIDE_PARAM(TypeTag, EclEnableTuning);
162 EWOMS_HIDE_PARAM(TypeTag, NewtonMaxError);
163 EWOMS_HIDE_PARAM(TypeTag, NewtonMaxIterations);
164 EWOMS_HIDE_PARAM(TypeTag, NewtonTolerance);
165 EWOMS_HIDE_PARAM(TypeTag, NewtonTargetIterations);
166 EWOMS_HIDE_PARAM(TypeTag, NewtonVerbose);
167 EWOMS_HIDE_PARAM(TypeTag, NewtonWriteConvergence);
168 EWOMS_HIDE_PARAM(TypeTag, EclNewtonSumTolerance);
169 EWOMS_HIDE_PARAM(TypeTag, EclNewtonSumToleranceExponent);
170 EWOMS_HIDE_PARAM(TypeTag, EclNewtonStrictIterations);
171 EWOMS_HIDE_PARAM(TypeTag, EclNewtonRelaxedVolumeFraction);
172 EWOMS_HIDE_PARAM(TypeTag, EclNewtonRelaxedTolerance);
175 EWOMS_HIDE_PARAM(TypeTag, RestartTime);
176 EWOMS_HIDE_PARAM(TypeTag, RestartWritingInterval);
179 EWOMS_HIDE_PARAM(TypeTag, VtkWriteOilFormationVolumeFactor);
180 EWOMS_HIDE_PARAM(TypeTag, VtkWriteOilSaturationPressure);
181 EWOMS_HIDE_PARAM(TypeTag, VtkWriteOilVaporizationFactor);
182 EWOMS_HIDE_PARAM(TypeTag, VtkWritePorosity);
183 EWOMS_HIDE_PARAM(TypeTag, VtkWritePotentialGradients);
184 EWOMS_HIDE_PARAM(TypeTag, VtkWritePressures);
185 EWOMS_HIDE_PARAM(TypeTag, VtkWritePrimaryVars);
186 EWOMS_HIDE_PARAM(TypeTag, VtkWritePrimaryVarsMeaning);
187 EWOMS_HIDE_PARAM(TypeTag, VtkWriteProcessRank);
188 EWOMS_HIDE_PARAM(TypeTag, VtkWriteRelativePermeabilities);
189 EWOMS_HIDE_PARAM(TypeTag, VtkWriteSaturatedGasOilVaporizationFactor);
190 EWOMS_HIDE_PARAM(TypeTag, VtkWriteSaturatedOilGasDissolutionFactor);
191 EWOMS_HIDE_PARAM(TypeTag, VtkWriteSaturationRatios);
192 EWOMS_HIDE_PARAM(TypeTag, VtkWriteSaturations);
193 EWOMS_HIDE_PARAM(TypeTag, VtkWriteTemperature);
194 EWOMS_HIDE_PARAM(TypeTag, VtkWriteViscosities);
195 EWOMS_HIDE_PARAM(TypeTag, VtkWriteWaterFormationVolumeFactor);
196 EWOMS_HIDE_PARAM(TypeTag, VtkWriteGasDissolutionFactor);
197 EWOMS_HIDE_PARAM(TypeTag, VtkWriteGasFormationVolumeFactor);
198 EWOMS_HIDE_PARAM(TypeTag, VtkWriteGasSaturationPressure);
199 EWOMS_HIDE_PARAM(TypeTag, VtkWriteIntrinsicPermeabilities);
200 EWOMS_HIDE_PARAM(TypeTag, VtkWriteEclTracerConcentration);
201 EWOMS_HIDE_PARAM(TypeTag, VtkWriteExtrusionFactor);
202 EWOMS_HIDE_PARAM(TypeTag, VtkWriteFilterVelocities);
203 EWOMS_HIDE_PARAM(TypeTag, VtkWriteDensities);
204 EWOMS_HIDE_PARAM(TypeTag, VtkWriteDofIndex);
205 EWOMS_HIDE_PARAM(TypeTag, VtkWriteMobilities);
207 EWOMS_HIDE_PARAM(TypeTag, VtkWriteAverageMolarMasses);
208 EWOMS_HIDE_PARAM(TypeTag, VtkWriteFugacities);
209 EWOMS_HIDE_PARAM(TypeTag, VtkWriteFugacityCoeffs);
210 EWOMS_HIDE_PARAM(TypeTag, VtkWriteMassFractions);
211 EWOMS_HIDE_PARAM(TypeTag, VtkWriteMolarities);
212 EWOMS_HIDE_PARAM(TypeTag, VtkWriteMoleFractions);
213 EWOMS_HIDE_PARAM(TypeTag, VtkWriteTotalMassFractions);
214 EWOMS_HIDE_PARAM(TypeTag, VtkWriteTotalMoleFractions);
216 EWOMS_END_PARAM_REGISTRATION(TypeTag);
218 int mpiRank = comm.rank();
221 int status = ::Opm::setupParameters_<TypeTag>(argc,
const_cast<const char**
>(argv),
false,
true, (mpiRank==0));
226 int unknownKeyWords = 0;
228 unknownKeyWords = Parameters::printUnused<TypeTag>(std::cerr);
230 int globalUnknownKeyWords = comm.sum(unknownKeyWords);
231 unknownKeyWords = globalUnknownKeyWords;
232 if ( unknownKeyWords )
236 std::string msg =
"Aborting simulation due to unknown "
237 "parameters. Please query \"flow --help\" for "
238 "supported command line parameters.";
239 if (OpmLog::hasBackend(
"STREAMLOG"))
244 std::cerr << msg << std::endl;
254 if (EWOMS_GET_PARAM(TypeTag,
int, PrintProperties) == 1) {
257 Properties::printValues<TypeTag>();
260 if (EWOMS_GET_PARAM(TypeTag,
int, PrintParameters) == 1) {
263 Parameters::printValues<TypeTag>();
273 static void printBanner(Parallel::Communication comm)
275 const int lineLen = 70;
277 const std::string banner =
"This is flow "+version;
278 const int bannerPreLen = (lineLen - 2 - banner.size())/2;
279 const int bannerPostLen = bannerPreLen + (lineLen - 2 - banner.size())%2;
280 std::cout <<
"**********************************************************************\n";
281 std::cout <<
"* *\n";
282 std::cout <<
"*" << std::string(bannerPreLen,
' ') << banner << std::string(bannerPostLen,
' ') <<
"*\n";
283 std::cout <<
"* *\n";
284 std::cout <<
"* Flow is a simulator for fully implicit three-phase black-oil flow, *\n";
285 std::cout <<
"* including solvent and polymer capabilities. *\n";
286 std::cout <<
"* For more information, see https://opm-project.org *\n";
287 std::cout <<
"* *\n";
288 std::cout <<
"**********************************************************************\n\n";
296 if (getenv(
"OMP_NUM_THREADS"))
297 threads = omp_get_max_threads();
299 threads = std::min(2, omp_get_max_threads());
301 const int input_threads = EWOMS_GET_PARAM(TypeTag,
int, ThreadsPerProcess);
303 if (input_threads > 0)
304 threads = std::min(input_threads, omp_get_max_threads());
307 int mpiSize = comm.size();
309 std::cout <<
"Using "<< mpiSize <<
" MPI processes with "<< threads <<
" OMP threads on each \n\n";
317 return execute_(&FlowMainEbos::runSimulator,
true);
320 int executeInitStep()
322 return execute_(&FlowMainEbos::runSimulatorInit,
false);
329 return simulator_->runStep(*simtimer_);
334 int executeStepsCleanup()
336 SimulatorReport report = simulator_->finalize();
337 runSimulatorAfterSim_(report);
338 return report.success.exit_status;
343 static void printPRTHeader(
bool output_cout)
347 const double megabyte = 1024 * 1024;
348 unsigned num_cpu = std::thread::hardware_concurrency();
350 const char* user = getlogin();
351 time_t now = std::time(0);
354 tstruct = *localtime(&now);
355 strftime(tmstr,
sizeof(tmstr),
"%d-%m-%Y at %X", &tstruct);
356 const double mem_size = getTotalSystemMemory() / megabyte;
357 std::ostringstream ss;
359 ss <<
" ######## # ###### # #\n";
360 ss <<
" # # # # # # \n";
361 ss <<
" ##### # # # # # # \n";
362 ss <<
" # # # # # # # # \n";
363 ss <<
" # ####### ###### # # \n\n";
364 ss <<
"Flow is a simulator for fully implicit three-phase black-oil flow,";
365 ss <<
" and is part of OPM.\nFor more information visit: https://opm-project.org \n\n";
366 ss <<
"Flow Version = " + version +
"\n";
367 if (uname(&arch) == 0) {
368 ss <<
"Machine name = " << arch.nodename <<
" (Number of logical cores: " << num_cpu;
369 ss <<
", Memory size: " << std::fixed << std::setprecision (2) << mem_size <<
" MB) \n";
370 ss <<
"Operating system = " << arch.sysname <<
" " << arch.machine <<
" (Kernel: " << arch.release;
371 ss <<
", " << arch.version <<
" )\n";
375 ss <<
"User = " << user << std::endl;
377 ss <<
"Simulation started on " << tmstr <<
" hrs\n";
379 ss <<
"Parameters used by Flow:\n";
380 Parameters::printValues<TypeTag>(ss);
382 OpmLog::note(ss.str());
386 EbosSimulator *getSimulatorPtr() {
387 return ebosSimulator_.get();
392 int execute_(
int (FlowMainEbos::* runOrInitFunc)(),
bool cleanup)
397 int status = setupParameters_(this->argc_, this->argv_, EclGenericVanguard::comm());
402 setupEbosSimulator();
406 int exitCode = (this->*runOrInitFunc)();
412 catch (
const std::exception& e) {
413 std::ostringstream message;
414 message <<
"Program threw an exception: " << e.what();
416 if (this->output_cout_) {
419 if (OpmLog::hasBackend(
"STREAMLOG")) {
420 OpmLog::error(message.str());
423 std::cout << message.str() <<
"\n";
427 if (this->mpi_size_ > 1)
428 MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
434 void executeCleanup_() {
436 mergeParallelLogFiles();
440 void setupParallelism()
445 auto comm = EclGenericVanguard::comm();
446 mpi_rank_ = comm.rank();
447 mpi_size_ = comm.size();
451 if (!getenv(
"OMP_NUM_THREADS"))
452 omp_set_num_threads(std::min(2, omp_get_num_procs()));
455 using ThreadManager = GetPropType<TypeTag, Properties::ThreadManager>;
456 ThreadManager::init();
461 void mergeParallelLogFiles()
464 OpmLog::removeAllBackends();
466 if (mpi_rank_ != 0 || mpi_size_ < 2 || !this->output_files_) {
470 namespace fs = ::std::filesystem;
471 const std::string& output_dir = eclState().getIOConfig().getOutputDir();
472 fs::path output_path(output_dir);
473 fs::path deck_filename(EWOMS_GET_PARAM(TypeTag, std::string, EclDeckFileName));
474 std::string basename;
476 std::string extension = uppercase(deck_filename.extension().string());
477 if ( extension ==
".DATA" || extension ==
"." )
479 basename = uppercase(deck_filename.stem().string());
483 basename = uppercase(deck_filename.filename().string());
485 std::for_each(fs::directory_iterator(output_path),
486 fs::directory_iterator(),
487 detail::ParallelFileMerger(output_path, basename,
488 EWOMS_GET_PARAM(TypeTag,
bool, EnableLoggingFalloutWarning)));
491 void setupEbosSimulator()
493 ebosSimulator_.reset(
new EbosSimulator(EclGenericVanguard::comm(),
false));
494 ebosSimulator_->executionTimer().start();
495 ebosSimulator_->model().applyInitialSolution();
499 const std::string& dryRunString = EWOMS_GET_PARAM(TypeTag, std::string, EnableDryRun);
500 if (dryRunString !=
"" && dryRunString !=
"auto") {
502 if (dryRunString ==
"true"
503 || dryRunString ==
"t"
504 || dryRunString ==
"1")
506 else if (dryRunString ==
"false"
507 || dryRunString ==
"f"
508 || dryRunString ==
"0")
511 throw std::invalid_argument(
"Invalid value for parameter EnableDryRun: '"
513 auto& ioConfig = eclState().getIOConfig();
514 ioConfig.overrideNOSIM(yesno);
517 catch (
const std::invalid_argument& e) {
518 std::cerr <<
"Failed to create valid EclipseState object" << std::endl;
519 std::cerr <<
"Exception caught: " << e.what() << std::endl;
524 const Deck& deck()
const
525 {
return ebosSimulator_->vanguard().deck(); }
528 {
return ebosSimulator_->vanguard().deck(); }
530 const EclipseState& eclState()
const
531 {
return ebosSimulator_->vanguard().eclState(); }
533 EclipseState& eclState()
534 {
return ebosSimulator_->vanguard().eclState(); }
536 const Schedule& schedule()
const
537 {
return ebosSimulator_->vanguard().schedule(); }
542 return runSimulatorInitOrRun_(&FlowMainEbos::runSimulatorRunCallback_);
545 int runSimulatorInit()
547 return runSimulatorInitOrRun_(&FlowMainEbos::runSimulatorInitCallback_);
552 int runSimulatorRunCallback_()
554 SimulatorReport report = simulator_->run(*simtimer_);
555 runSimulatorAfterSim_(report);
556 return report.success.exit_status;
560 int runSimulatorInitCallback_()
562 simulator_->init(*simtimer_);
567 void runSimulatorAfterSim_(SimulatorReport &report)
569 if (this->output_cout_) {
570 std::ostringstream ss;
571 ss <<
"\n\n================ End of simulation ===============\n\n";
572 ss << fmt::format(
"Number of MPI processes: {:9}\n", mpi_size_ );
574 int threads = omp_get_max_threads();
578 ss << fmt::format(
"Threads per MPI process: {:9}\n", threads);
579 report.reportFullyImplicit(ss);
580 OpmLog::info(ss.str());
581 const std::string dir = eclState().getIOConfig().getOutputDir();
582 namespace fs = ::std::filesystem;
583 fs::path output_dir(dir);
585 std::string filename = eclState().getIOConfig().getBaseName() +
".INFOSTEP";
586 fs::path fullpath = output_dir / filename;
587 std::ofstream os(fullpath.string());
588 report.fullReports(os);
594 int runSimulatorInitOrRun_(
int (FlowMainEbos::* initOrRunFunc)())
597 const auto& schedule = this->schedule();
598 auto& ioConfig = eclState().getIOConfig();
599 simtimer_ = std::make_unique<SimulatorTimer>();
602 const auto& initConfig = eclState().getInitConfig();
603 simtimer_->init(schedule, (
size_t)initConfig.getRestartStep());
605 if (this->output_cout_) {
606 std::ostringstream oss;
610 if (Parameters::printUnused<TypeTag>(oss)) {
611 std::cout <<
"----------------- Unrecognized parameters: -----------------\n";
612 std::cout << oss.str();
613 std::cout <<
"----------------------------------------------------------------" << std::endl;
617 if (!ioConfig.initOnly()) {
618 if (this->output_cout_) {
620 msg =
"\n\n================ Starting main simulation loop ===============\n";
624 return (this->*initOrRunFunc)();
627 if (this->output_cout_) {
628 std::cout <<
"\n\n================ Simulation turned off ===============\n" << std::flush;
643 simulator_.reset(
new Simulator(*ebosSimulator_));
646 static unsigned long long getTotalSystemMemory()
648 long pages = sysconf(_SC_PHYS_PAGES);
649 long page_size = sysconf(_SC_PAGE_SIZE);
650 return pages * page_size;
655 {
return ebosSimulator_->vanguard().grid(); }
658 std::unique_ptr<EbosSimulator> ebosSimulator_;
661 std::any parallel_information_;
662 std::unique_ptr<Simulator> simulator_;
663 std::unique_ptr<SimulatorTimer> simtimer_;
Definition: FlowMainEbos.hpp:88
int execute()
This is the main function of Flow.
Definition: FlowMainEbos.hpp:315
void createSimulator()
This is the main function of Flow.
Definition: FlowMainEbos.hpp:640
a simulator for the blackoil model
Definition: SimulatorFullyImplicitBlackoilEbos.hpp:72
This file contains a set of helper functions used by VFPProd / VFPInj.
Definition: BlackoilPhases.hpp:27
std::string moduleVersionName()
Return the version name of the module, for example "2015.10" (for a release branch) or "2016....
Definition: moduleVersion.cpp:29
std::string compileTimestamp()
Return a string "dd-mm-yyyy at HH::MM::SS hrs" which is the time the binary was compiled.
Definition: moduleVersion.cpp:52
std::string moduleVersion()
Return a string containing both the name and hash, if N is the name and H is the hash it will be "N (...
Definition: moduleVersion.cpp:45
Definition: FlowMainEbos.hpp:51
Definition: FlowMainEbos.hpp:59
Definition: FlowMainEbos.hpp:55