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, 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_HIDE_PARAM(TypeTag, VtkWriteTortuosities);
217 EWOMS_HIDE_PARAM(TypeTag, VtkWriteDiffusionCoefficients);
218 EWOMS_HIDE_PARAM(TypeTag, VtkWriteEffectiveDiffusionCoefficients);
220 EWOMS_END_PARAM_REGISTRATION(TypeTag);
222 int mpiRank = comm.rank();
225 int status = ::Opm::setupParameters_<TypeTag>(argc,
const_cast<const char**
>(argv),
false,
true, (mpiRank==0));
230 int unknownKeyWords = 0;
232 unknownKeyWords = Parameters::printUnused<TypeTag>(std::cerr);
234 int globalUnknownKeyWords = comm.sum(unknownKeyWords);
235 unknownKeyWords = globalUnknownKeyWords;
236 if ( unknownKeyWords )
240 std::string msg =
"Aborting simulation due to unknown "
241 "parameters. Please query \"flow --help\" for "
242 "supported command line parameters.";
243 if (OpmLog::hasBackend(
"STREAMLOG"))
248 std::cerr << msg << std::endl;
258 if (EWOMS_GET_PARAM(TypeTag,
int, PrintProperties) == 1) {
261 Properties::printValues<TypeTag>();
264 if (EWOMS_GET_PARAM(TypeTag,
int, PrintParameters) == 1) {
267 Parameters::printValues<TypeTag>();
277 static void printBanner(Parallel::Communication comm)
279 const int lineLen = 70;
281 const std::string banner =
"This is flow "+version;
282 const int bannerPreLen = (lineLen - 2 - banner.size())/2;
283 const int bannerPostLen = bannerPreLen + (lineLen - 2 - banner.size())%2;
284 std::cout <<
"**********************************************************************\n";
285 std::cout <<
"* *\n";
286 std::cout <<
"*" << std::string(bannerPreLen,
' ') << banner << std::string(bannerPostLen,
' ') <<
"*\n";
287 std::cout <<
"* *\n";
288 std::cout <<
"* Flow is a simulator for fully implicit three-phase black-oil flow, *\n";
289 std::cout <<
"* including solvent and polymer capabilities. *\n";
290 std::cout <<
"* For more information, see https://opm-project.org *\n";
291 std::cout <<
"* *\n";
292 std::cout <<
"**********************************************************************\n\n";
300 if (getenv(
"OMP_NUM_THREADS"))
301 threads = omp_get_max_threads();
303 threads = std::min(2, omp_get_max_threads());
305 const int input_threads = EWOMS_GET_PARAM(TypeTag,
int, ThreadsPerProcess);
307 if (input_threads > 0)
308 threads = std::min(input_threads, omp_get_max_threads());
311 int mpiSize = comm.size();
313 std::cout <<
"Using "<< mpiSize <<
" MPI processes with "<< threads <<
" OMP threads on each \n\n";
321 return execute_(&FlowMainEbos::runSimulator,
true);
324 int executeInitStep()
326 return execute_(&FlowMainEbos::runSimulatorInit,
false);
333 return simulator_->runStep(*simtimer_);
338 int executeStepsCleanup()
340 SimulatorReport report = simulator_->finalize();
341 runSimulatorAfterSim_(report);
342 return report.success.exit_status;
347 static void printPRTHeader(
bool output_cout)
351 const double megabyte = 1024 * 1024;
352 unsigned num_cpu = std::thread::hardware_concurrency();
354 const char* user = getlogin();
355 time_t now = std::time(0);
358 tstruct = *localtime(&now);
359 strftime(tmstr,
sizeof(tmstr),
"%d-%m-%Y at %X", &tstruct);
360 const double mem_size = getTotalSystemMemory() / megabyte;
361 std::ostringstream ss;
363 ss <<
" ######## # ###### # #\n";
364 ss <<
" # # # # # # \n";
365 ss <<
" ##### # # # # # # \n";
366 ss <<
" # # # # # # # # \n";
367 ss <<
" # ####### ###### # # \n\n";
368 ss <<
"Flow is a simulator for fully implicit three-phase black-oil flow,";
369 ss <<
" and is part of OPM.\nFor more information visit: https://opm-project.org \n\n";
370 ss <<
"Flow Version = " + version +
"\n";
371 if (uname(&arch) == 0) {
372 ss <<
"Machine name = " << arch.nodename <<
" (Number of logical cores: " << num_cpu;
373 ss <<
", Memory size: " << std::fixed << std::setprecision (2) << mem_size <<
" MB) \n";
374 ss <<
"Operating system = " << arch.sysname <<
" " << arch.machine <<
" (Kernel: " << arch.release;
375 ss <<
", " << arch.version <<
" )\n";
379 ss <<
"User = " << user << std::endl;
381 ss <<
"Simulation started on " << tmstr <<
" hrs\n";
383 ss <<
"Parameters used by Flow:\n";
384 Parameters::printValues<TypeTag>(ss);
386 OpmLog::note(ss.str());
390 EbosSimulator *getSimulatorPtr() {
391 return ebosSimulator_.get();
394 SimulatorTimer* getSimTimer() {
395 return simtimer_.get();
400 int execute_(
int (FlowMainEbos::* runOrInitFunc)(),
bool cleanup)
405 int status = setupParameters_(this->argc_, this->argv_, EclGenericVanguard::comm());
410 setupEbosSimulator();
414 int exitCode = (this->*runOrInitFunc)();
420 catch (
const std::exception& e) {
421 std::ostringstream message;
422 message <<
"Program threw an exception: " << e.what();
424 if (this->output_cout_) {
427 if (OpmLog::hasBackend(
"STREAMLOG")) {
428 OpmLog::error(message.str());
431 std::cout << message.str() <<
"\n";
435 if (this->mpi_size_ > 1)
436 MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
442 void executeCleanup_() {
444 mergeParallelLogFiles();
448 void setupParallelism()
453 auto comm = EclGenericVanguard::comm();
454 mpi_rank_ = comm.rank();
455 mpi_size_ = comm.size();
459 if (!getenv(
"OMP_NUM_THREADS"))
460 omp_set_num_threads(std::min(2, omp_get_num_procs()));
463 using ThreadManager = GetPropType<TypeTag, Properties::ThreadManager>;
464 ThreadManager::init();
469 void mergeParallelLogFiles()
472 OpmLog::removeAllBackends();
474 if (mpi_rank_ != 0 || mpi_size_ < 2 || !this->output_files_) {
478 namespace fs = ::std::filesystem;
479 const std::string& output_dir = eclState().getIOConfig().getOutputDir();
480 fs::path output_path(output_dir);
481 fs::path deck_filename(EWOMS_GET_PARAM(TypeTag, std::string, EclDeckFileName));
482 std::string basename;
484 std::string extension = uppercase(deck_filename.extension().string());
485 if ( extension ==
".DATA" || extension ==
"." )
487 basename = uppercase(deck_filename.stem().string());
491 basename = uppercase(deck_filename.filename().string());
493 std::for_each(fs::directory_iterator(output_path),
494 fs::directory_iterator(),
495 detail::ParallelFileMerger(output_path, basename,
496 EWOMS_GET_PARAM(TypeTag,
bool, EnableLoggingFalloutWarning)));
499 void setupEbosSimulator()
501 ebosSimulator_.reset(
new EbosSimulator(EclGenericVanguard::comm(),
false));
502 ebosSimulator_->executionTimer().start();
503 ebosSimulator_->model().applyInitialSolution();
507 const std::string& dryRunString = EWOMS_GET_PARAM(TypeTag, std::string, EnableDryRun);
508 if (dryRunString !=
"" && dryRunString !=
"auto") {
510 if (dryRunString ==
"true"
511 || dryRunString ==
"t"
512 || dryRunString ==
"1")
514 else if (dryRunString ==
"false"
515 || dryRunString ==
"f"
516 || dryRunString ==
"0")
519 throw std::invalid_argument(
"Invalid value for parameter EnableDryRun: '"
521 auto& ioConfig = eclState().getIOConfig();
522 ioConfig.overrideNOSIM(yesno);
525 catch (
const std::invalid_argument& e) {
526 std::cerr <<
"Failed to create valid EclipseState object" << std::endl;
527 std::cerr <<
"Exception caught: " << e.what() << std::endl;
532 const Deck& deck()
const
533 {
return ebosSimulator_->vanguard().deck(); }
536 {
return ebosSimulator_->vanguard().deck(); }
538 const EclipseState& eclState()
const
539 {
return ebosSimulator_->vanguard().eclState(); }
541 EclipseState& eclState()
542 {
return ebosSimulator_->vanguard().eclState(); }
544 const Schedule& schedule()
const
545 {
return ebosSimulator_->vanguard().schedule(); }
550 return runSimulatorInitOrRun_(&FlowMainEbos::runSimulatorRunCallback_);
553 int runSimulatorInit()
555 return runSimulatorInitOrRun_(&FlowMainEbos::runSimulatorInitCallback_);
560 int runSimulatorRunCallback_()
562 SimulatorReport report = simulator_->run(*simtimer_);
563 runSimulatorAfterSim_(report);
564 return report.success.exit_status;
568 int runSimulatorInitCallback_()
570 simulator_->init(*simtimer_);
575 void runSimulatorAfterSim_(SimulatorReport &report)
577 if (this->output_cout_) {
578 std::ostringstream ss;
579 ss <<
"\n\n================ End of simulation ===============\n\n";
580 ss << fmt::format(
"Number of MPI processes: {:9}\n", mpi_size_ );
582 int threads = omp_get_max_threads();
586 ss << fmt::format(
"Threads per MPI process: {:9}\n", threads);
587 report.reportFullyImplicit(ss);
588 OpmLog::info(ss.str());
589 const std::string dir = eclState().getIOConfig().getOutputDir();
590 namespace fs = ::std::filesystem;
591 fs::path output_dir(dir);
593 std::string filename = eclState().getIOConfig().getBaseName() +
".INFOSTEP";
594 fs::path fullpath = output_dir / filename;
595 std::ofstream os(fullpath.string());
596 report.fullReports(os);
602 int runSimulatorInitOrRun_(
int (FlowMainEbos::* initOrRunFunc)())
605 const auto& schedule = this->schedule();
606 auto& ioConfig = eclState().getIOConfig();
607 simtimer_ = std::make_unique<SimulatorTimer>();
610 const auto& initConfig = eclState().getInitConfig();
611 simtimer_->init(schedule, (
size_t)initConfig.getRestartStep());
613 if (this->output_cout_) {
614 std::ostringstream oss;
618 if (Parameters::printUnused<TypeTag>(oss)) {
619 std::cout <<
"----------------- Unrecognized parameters: -----------------\n";
620 std::cout << oss.str();
621 std::cout <<
"----------------------------------------------------------------" << std::endl;
625 if (!ioConfig.initOnly()) {
626 if (this->output_cout_) {
628 msg =
"\n\n================ Starting main simulation loop ===============\n";
632 return (this->*initOrRunFunc)();
635 if (this->output_cout_) {
636 std::cout <<
"\n\n================ Simulation turned off ===============\n" << std::flush;
651 simulator_.reset(
new Simulator(*ebosSimulator_));
654 static unsigned long long getTotalSystemMemory()
656 long pages = sysconf(_SC_PHYS_PAGES);
657 long page_size = sysconf(_SC_PAGE_SIZE);
658 return pages * page_size;
663 {
return ebosSimulator_->vanguard().grid(); }
666 std::unique_ptr<EbosSimulator> ebosSimulator_;
669 std::any parallel_information_;
670 std::unique_ptr<Simulator> simulator_;
671 std::unique_ptr<SimulatorTimer> simtimer_;
Definition: FlowMainEbos.hpp:89
int execute()
This is the main function of Flow.
Definition: FlowMainEbos.hpp:319
void createSimulator()
This is the main function of Flow.
Definition: FlowMainEbos.hpp:648
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