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>
32#include <flow/flow_ebos_blackoil.hpp>
34#include <opm/input/eclipse/EclipseState/EclipseState.hpp>
35#include <opm/input/eclipse/EclipseState/IOConfig/IOConfig.hpp>
36#include <opm/input/eclipse/EclipseState/InitConfig/InitConfig.hpp>
37#include <opm/input/eclipse/Schedule/UDQ/UDQState.hpp>
38#include <opm/common/utility/String.hpp>
40#include <fmt/format.h>
44#include <dune/fem/misc/mpimanager.hh>
46#include <dune/common/parallel/mpihelper.hh>
49namespace Opm::Properties {
51template<
class TypeTag,
class MyTypeTag>
53 using type = UndefinedProperty;
55template<
class TypeTag,
class MyTypeTag>
57 using type = UndefinedProperty;
59template<
class TypeTag,
class MyTypeTag>
61 using type = UndefinedProperty;
65template<
class TypeTag>
67 static constexpr auto value =
"auto";
70template<
class TypeTag>
72 static constexpr bool value =
false;
74template<
class TypeTag>
76 static constexpr int value = 1;
87 template <
class TypeTag>
91 using MaterialLawManager =
typename GetProp<TypeTag, Properties::MaterialLaw>::EclMaterialLawManager;
92 using EbosSimulator = GetPropType<TypeTag, Properties::Simulator>;
93 using Grid = GetPropType<TypeTag, Properties::Grid>;
94 using GridView = GetPropType<TypeTag, Properties::GridView>;
95 using Problem = GetPropType<TypeTag, Properties::Problem>;
96 using Scalar = GetPropType<TypeTag, Properties::Scalar>;
97 using FluidSystem = GetPropType<TypeTag, Properties::FluidSystem>;
101 FlowMainEbos(
int argc,
char **argv,
bool output_cout,
bool output_files )
102 : argc_{argc}, argv_{argv},
103 output_cout_{output_cout}, output_files_{output_files}
109 static int setupParameters_(
int argc,
char** argv, Parallel::Communication comm)
111 using ParamsMeta = GetProp<TypeTag, Properties::ParameterMetaData>;
112 if (!ParamsMeta::registrationOpen()) {
123 EWOMS_REGISTER_PARAM(TypeTag, std::string, EnableDryRun,
124 "Specify if the simulation ought to be actually run, or just pretended to be");
125 EWOMS_REGISTER_PARAM(TypeTag,
int, OutputInterval,
126 "Specify the number of report steps between two consecutive writes of restart data");
127 EWOMS_REGISTER_PARAM(TypeTag,
bool, EnableLoggingFalloutWarning,
128 "Developer option to see whether logging was on non-root processors. In that case it will be appended to the *.DBG or *.PRT files");
130 Simulator::registerParameters();
133 registerAllParameters_<TypeTag>(
false);
136 EWOMS_HIDE_PARAM(TypeTag, EnableGravity);
137 EWOMS_HIDE_PARAM(TypeTag, EnableGridAdaptation);
142 EWOMS_HIDE_PARAM(TypeTag, EnableIntensiveQuantityCache);
146 EWOMS_HIDE_PARAM(TypeTag, EnableThermodynamicHints);
149 EWOMS_HIDE_PARAM(TypeTag, EndTime);
152 EWOMS_HIDE_PARAM(TypeTag, InitialTimeStepSize);
153 EWOMS_HIDE_PARAM(TypeTag, MaxTimeStepDivisions);
154 EWOMS_HIDE_PARAM(TypeTag, MaxTimeStepSize);
155 EWOMS_HIDE_PARAM(TypeTag, MinTimeStepSize);
156 EWOMS_HIDE_PARAM(TypeTag, PredeterminedTimeStepsFile);
158 EWOMS_HIDE_PARAM(TypeTag, EclMaxTimeStepSizeAfterWellEvent);
159 EWOMS_HIDE_PARAM(TypeTag, EclRestartShrinkFactor);
160 EWOMS_HIDE_PARAM(TypeTag, EclEnableTuning);
163 EWOMS_HIDE_PARAM(TypeTag, NewtonMaxError);
164 EWOMS_HIDE_PARAM(TypeTag, NewtonMaxIterations);
165 EWOMS_HIDE_PARAM(TypeTag, NewtonTolerance);
166 EWOMS_HIDE_PARAM(TypeTag, NewtonTargetIterations);
167 EWOMS_HIDE_PARAM(TypeTag, NewtonVerbose);
168 EWOMS_HIDE_PARAM(TypeTag, NewtonWriteConvergence);
169 EWOMS_HIDE_PARAM(TypeTag, EclNewtonSumTolerance);
170 EWOMS_HIDE_PARAM(TypeTag, EclNewtonSumToleranceExponent);
171 EWOMS_HIDE_PARAM(TypeTag, EclNewtonStrictIterations);
172 EWOMS_HIDE_PARAM(TypeTag, EclNewtonRelaxedVolumeFraction);
173 EWOMS_HIDE_PARAM(TypeTag, EclNewtonRelaxedTolerance);
176 EWOMS_HIDE_PARAM(TypeTag, RestartTime);
177 EWOMS_HIDE_PARAM(TypeTag, RestartWritingInterval);
180 EWOMS_HIDE_PARAM(TypeTag, VtkWriteOilFormationVolumeFactor);
181 EWOMS_HIDE_PARAM(TypeTag, VtkWriteOilSaturationPressure);
182 EWOMS_HIDE_PARAM(TypeTag, VtkWriteOilVaporizationFactor);
183 EWOMS_HIDE_PARAM(TypeTag, VtkWritePorosity);
184 EWOMS_HIDE_PARAM(TypeTag, VtkWritePotentialGradients);
185 EWOMS_HIDE_PARAM(TypeTag, VtkWritePressures);
186 EWOMS_HIDE_PARAM(TypeTag, VtkWritePrimaryVars);
187 EWOMS_HIDE_PARAM(TypeTag, VtkWritePrimaryVarsMeaning);
188 EWOMS_HIDE_PARAM(TypeTag, VtkWriteProcessRank);
189 EWOMS_HIDE_PARAM(TypeTag, VtkWriteRelativePermeabilities);
190 EWOMS_HIDE_PARAM(TypeTag, VtkWriteSaturatedGasOilVaporizationFactor);
191 EWOMS_HIDE_PARAM(TypeTag, VtkWriteSaturatedOilGasDissolutionFactor);
192 EWOMS_HIDE_PARAM(TypeTag, VtkWriteSaturationRatios);
193 EWOMS_HIDE_PARAM(TypeTag, VtkWriteSaturations);
194 EWOMS_HIDE_PARAM(TypeTag, VtkWriteTemperature);
195 EWOMS_HIDE_PARAM(TypeTag, VtkWriteViscosities);
196 EWOMS_HIDE_PARAM(TypeTag, VtkWriteWaterFormationVolumeFactor);
197 EWOMS_HIDE_PARAM(TypeTag, VtkWriteGasDissolutionFactor);
198 EWOMS_HIDE_PARAM(TypeTag, VtkWriteGasFormationVolumeFactor);
199 EWOMS_HIDE_PARAM(TypeTag, VtkWriteGasSaturationPressure);
200 EWOMS_HIDE_PARAM(TypeTag, VtkWriteIntrinsicPermeabilities);
201 EWOMS_HIDE_PARAM(TypeTag, VtkWriteEclTracerConcentration);
202 EWOMS_HIDE_PARAM(TypeTag, VtkWriteExtrusionFactor);
203 EWOMS_HIDE_PARAM(TypeTag, VtkWriteFilterVelocities);
204 EWOMS_HIDE_PARAM(TypeTag, VtkWriteDensities);
205 EWOMS_HIDE_PARAM(TypeTag, VtkWriteDofIndex);
206 EWOMS_HIDE_PARAM(TypeTag, VtkWriteMobilities);
208 EWOMS_HIDE_PARAM(TypeTag, VtkWriteAverageMolarMasses);
209 EWOMS_HIDE_PARAM(TypeTag, VtkWriteFugacities);
210 EWOMS_HIDE_PARAM(TypeTag, VtkWriteFugacityCoeffs);
211 EWOMS_HIDE_PARAM(TypeTag, VtkWriteMassFractions);
212 EWOMS_HIDE_PARAM(TypeTag, VtkWriteMolarities);
213 EWOMS_HIDE_PARAM(TypeTag, VtkWriteMoleFractions);
214 EWOMS_HIDE_PARAM(TypeTag, VtkWriteTotalMassFractions);
215 EWOMS_HIDE_PARAM(TypeTag, VtkWriteTotalMoleFractions);
217 EWOMS_HIDE_PARAM(TypeTag, VtkWriteTortuosities);
218 EWOMS_HIDE_PARAM(TypeTag, VtkWriteDiffusionCoefficients);
219 EWOMS_HIDE_PARAM(TypeTag, VtkWriteEffectiveDiffusionCoefficients);
221 EWOMS_END_PARAM_REGISTRATION(TypeTag);
223 int mpiRank = comm.rank();
226 int status = ::Opm::setupParameters_<TypeTag>(argc,
const_cast<const char**
>(argv),
false,
true, (mpiRank==0));
231 int unknownKeyWords = 0;
233 unknownKeyWords = Parameters::printUnused<TypeTag>(std::cerr);
235 int globalUnknownKeyWords = comm.sum(unknownKeyWords);
236 unknownKeyWords = globalUnknownKeyWords;
237 if ( unknownKeyWords )
241 std::string msg =
"Aborting simulation due to unknown "
242 "parameters. Please query \"flow --help\" for "
243 "supported command line parameters.";
244 if (OpmLog::hasBackend(
"STREAMLOG"))
249 std::cerr << msg << std::endl;
259 if (EWOMS_GET_PARAM(TypeTag,
int, PrintProperties) == 1) {
262 Properties::printValues<TypeTag>();
265 if (EWOMS_GET_PARAM(TypeTag,
int, PrintParameters) == 1) {
268 Parameters::printValues<TypeTag>();
278 static void printBanner(Parallel::Communication comm)
280 const int lineLen = 70;
282 const std::string banner =
"This is flow "+version;
283 const int bannerPreLen = (lineLen - 2 - banner.size())/2;
284 const int bannerPostLen = bannerPreLen + (lineLen - 2 - banner.size())%2;
285 std::cout <<
"**********************************************************************\n";
286 std::cout <<
"* *\n";
287 std::cout <<
"*" << std::string(bannerPreLen,
' ') << banner << std::string(bannerPostLen,
' ') <<
"*\n";
288 std::cout <<
"* *\n";
289 std::cout <<
"* Flow is a simulator for fully implicit three-phase black-oil flow, *\n";
290 std::cout <<
"* including solvent and polymer capabilities. *\n";
291 std::cout <<
"* For more information, see https://opm-project.org *\n";
292 std::cout <<
"* *\n";
293 std::cout <<
"**********************************************************************\n\n";
301 if (getenv(
"OMP_NUM_THREADS"))
302 threads = omp_get_max_threads();
304 threads = std::min(2, omp_get_max_threads());
306 const int input_threads = EWOMS_GET_PARAM(TypeTag,
int, ThreadsPerProcess);
308 if (input_threads > 0)
309 threads = std::min(input_threads, omp_get_max_threads());
312 int mpiSize = comm.size();
314 std::cout <<
"Using "<< mpiSize <<
" MPI processes with "<< threads <<
" OMP threads on each \n\n";
322 return execute_(&FlowMainEbos::runSimulator,
true);
325 int executeInitStep()
327 return execute_(&FlowMainEbos::runSimulatorInit,
false);
334 return simulator_->runStep(*simtimer_);
339 int executeStepsCleanup()
341 SimulatorReport report = simulator_->finalize();
342 runSimulatorAfterSim_(report);
343 return report.success.exit_status;
348 static void printPRTHeader(
bool output_cout)
352 const double megabyte = 1024 * 1024;
353 unsigned num_cpu = std::thread::hardware_concurrency();
355 const char* user = getlogin();
356 time_t now = std::time(0);
359 tstruct = *localtime(&now);
360 strftime(tmstr,
sizeof(tmstr),
"%d-%m-%Y at %X", &tstruct);
361 const double mem_size = getTotalSystemMemory() / megabyte;
362 std::ostringstream ss;
364 ss <<
" ######## # ###### # #\n";
365 ss <<
" # # # # # # \n";
366 ss <<
" ##### # # # # # # \n";
367 ss <<
" # # # # # # # # \n";
368 ss <<
" # ####### ###### # # \n\n";
369 ss <<
"Flow is a simulator for fully implicit three-phase black-oil flow,";
370 ss <<
" and is part of OPM.\nFor more information visit: https://opm-project.org \n\n";
371 ss <<
"Flow Version = " + version +
"\n";
372 if (uname(&arch) == 0) {
373 ss <<
"Machine name = " << arch.nodename <<
" (Number of logical cores: " << num_cpu;
374 ss <<
", Memory size: " << std::fixed << std::setprecision (2) << mem_size <<
" MB) \n";
375 ss <<
"Operating system = " << arch.sysname <<
" " << arch.machine <<
" (Kernel: " << arch.release;
376 ss <<
", " << arch.version <<
" )\n";
380 ss <<
"User = " << user << std::endl;
382 ss <<
"Simulation started on " << tmstr <<
" hrs\n";
384 ss <<
"Parameters used by Flow:\n";
385 Parameters::printValues<TypeTag>(ss);
387 OpmLog::note(ss.str());
391 EbosSimulator *getSimulatorPtr() {
392 return ebosSimulator_.get();
395 SimulatorTimer* getSimTimer() {
396 return simtimer_.get();
401 int execute_(
int (FlowMainEbos::* runOrInitFunc)(),
bool cleanup)
406 int status = setupParameters_(this->argc_, this->argv_, EclGenericVanguard::comm());
411 setupEbosSimulator();
415 int exitCode = (this->*runOrInitFunc)();
421 catch (
const std::exception& e) {
422 std::ostringstream message;
423 message <<
"Program threw an exception: " << e.what();
425 if (this->output_cout_) {
428 if (OpmLog::hasBackend(
"STREAMLOG")) {
429 OpmLog::error(message.str());
432 std::cout << message.str() <<
"\n";
436 if (this->mpi_size_ > 1)
437 MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
443 void executeCleanup_() {
445 mergeParallelLogFiles();
449 void setupParallelism()
454 auto comm = EclGenericVanguard::comm();
455 mpi_rank_ = comm.rank();
456 mpi_size_ = comm.size();
460 if (!getenv(
"OMP_NUM_THREADS"))
461 omp_set_num_threads(std::min(2, omp_get_num_procs()));
464 using ThreadManager = GetPropType<TypeTag, Properties::ThreadManager>;
465 ThreadManager::init();
470 void mergeParallelLogFiles()
473 OpmLog::removeAllBackends();
475 if (mpi_rank_ != 0 || mpi_size_ < 2 || !this->output_files_) {
479 namespace fs = ::std::filesystem;
480 const std::string& output_dir = eclState().getIOConfig().getOutputDir();
481 fs::path output_path(output_dir);
482 fs::path deck_filename(EWOMS_GET_PARAM(TypeTag, std::string, EclDeckFileName));
483 std::string basename;
485 std::string extension = uppercase(deck_filename.extension().string());
486 if ( extension ==
".DATA" || extension ==
"." )
488 basename = uppercase(deck_filename.stem().string());
492 basename = uppercase(deck_filename.filename().string());
494 std::for_each(fs::directory_iterator(output_path),
495 fs::directory_iterator(),
496 detail::ParallelFileMerger(output_path, basename,
497 EWOMS_GET_PARAM(TypeTag,
bool, EnableLoggingFalloutWarning)));
500 void setupEbosSimulator()
502 ebosSimulator_.reset(
new EbosSimulator(EclGenericVanguard::comm(),
false));
503 ebosSimulator_->executionTimer().start();
504 ebosSimulator_->model().applyInitialSolution();
508 const std::string& dryRunString = EWOMS_GET_PARAM(TypeTag, std::string, EnableDryRun);
509 if (dryRunString !=
"" && dryRunString !=
"auto") {
511 if (dryRunString ==
"true"
512 || dryRunString ==
"t"
513 || dryRunString ==
"1")
515 else if (dryRunString ==
"false"
516 || dryRunString ==
"f"
517 || dryRunString ==
"0")
520 throw std::invalid_argument(
"Invalid value for parameter EnableDryRun: '"
522 auto& ioConfig = eclState().getIOConfig();
523 ioConfig.overrideNOSIM(yesno);
526 catch (
const std::invalid_argument& e) {
527 std::cerr <<
"Failed to create valid EclipseState object" << std::endl;
528 std::cerr <<
"Exception caught: " << e.what() << std::endl;
533 const Deck& deck()
const
534 {
return ebosSimulator_->vanguard().deck(); }
537 {
return ebosSimulator_->vanguard().deck(); }
539 const EclipseState& eclState()
const
540 {
return ebosSimulator_->vanguard().eclState(); }
542 EclipseState& eclState()
543 {
return ebosSimulator_->vanguard().eclState(); }
545 const Schedule& schedule()
const
546 {
return ebosSimulator_->vanguard().schedule(); }
551 return runSimulatorInitOrRun_(&FlowMainEbos::runSimulatorRunCallback_);
554 int runSimulatorInit()
556 return runSimulatorInitOrRun_(&FlowMainEbos::runSimulatorInitCallback_);
561 int runSimulatorRunCallback_()
563 SimulatorReport report = simulator_->run(*simtimer_);
564 runSimulatorAfterSim_(report);
565 return report.success.exit_status;
569 int runSimulatorInitCallback_()
571 simulator_->init(*simtimer_);
576 void runSimulatorAfterSim_(SimulatorReport &report)
578 if (this->output_cout_) {
579 std::ostringstream ss;
580 ss <<
"\n\n================ End of simulation ===============\n\n";
581 ss << fmt::format(
"Number of MPI processes: {:9}\n", mpi_size_ );
583 int threads = omp_get_max_threads();
587 ss << fmt::format(
"Threads per MPI process: {:9}\n", threads);
588 report.reportFullyImplicit(ss);
589 OpmLog::info(ss.str());
590 const std::string dir = eclState().getIOConfig().getOutputDir();
591 namespace fs = ::std::filesystem;
592 fs::path output_dir(dir);
594 std::string filename = eclState().getIOConfig().getBaseName() +
".INFOSTEP";
595 fs::path fullpath = output_dir / filename;
596 std::ofstream os(fullpath.string());
597 report.fullReports(os);
603 int runSimulatorInitOrRun_(
int (FlowMainEbos::* initOrRunFunc)())
606 const auto& schedule = this->schedule();
607 auto& ioConfig = eclState().getIOConfig();
608 simtimer_ = std::make_unique<SimulatorTimer>();
611 const auto& initConfig = eclState().getInitConfig();
612 simtimer_->init(schedule, (
size_t)initConfig.getRestartStep());
614 if (this->output_cout_) {
615 std::ostringstream oss;
619 if (Parameters::printUnused<TypeTag>(oss)) {
620 std::cout <<
"----------------- Unrecognized parameters: -----------------\n";
621 std::cout << oss.str();
622 std::cout <<
"----------------------------------------------------------------" << std::endl;
626 if (!ioConfig.initOnly()) {
627 if (this->output_cout_) {
629 msg =
"\n\n================ Starting main simulation loop ===============\n";
633 return (this->*initOrRunFunc)();
636 if (this->output_cout_) {
637 std::cout <<
"\n\n================ Simulation turned off ===============\n" << std::flush;
652 simulator_.reset(
new Simulator(*ebosSimulator_));
655 static unsigned long long getTotalSystemMemory()
657 long pages = sysconf(_SC_PHYS_PAGES);
658 long page_size = sysconf(_SC_PAGE_SIZE);
659 return pages * page_size;
664 {
return ebosSimulator_->vanguard().grid(); }
667 std::unique_ptr<EbosSimulator> ebosSimulator_;
670 std::any parallel_information_;
671 std::unique_ptr<Simulator> simulator_;
672 std::unique_ptr<SimulatorTimer> simtimer_;
Definition: FlowMainEbos.hpp:89
int execute()
This is the main function of Flow.
Definition: FlowMainEbos.hpp:320
void createSimulator()
This is the main function of Flow.
Definition: FlowMainEbos.hpp:649
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:34
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:57
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:50
Definition: FlowMainEbos.hpp:52
Definition: FlowMainEbos.hpp:60
Definition: FlowMainEbos.hpp:56