22#ifndef OPM_MAIN_HEADER_INCLUDED
23#define OPM_MAIN_HEADER_INCLUDED
25#include <flow/flow_ebos_blackoil.hpp>
26#include <flow/flow_ebos_blackoil_legacyassembly.hpp>
28#include <flow/flow_ebos_gasoil.hpp>
29#include <flow/flow_ebos_gasoildiffuse.hpp>
30#include <flow/flow_ebos_gasoil_energy.hpp>
31#include <flow/flow_ebos_oilwater.hpp>
32#include <flow/flow_ebos_gaswater.hpp>
33#include <flow/flow_ebos_solvent.hpp>
34#include <flow/flow_ebos_polymer.hpp>
35#include <flow/flow_ebos_extbo.hpp>
36#include <flow/flow_ebos_foam.hpp>
37#include <flow/flow_ebos_brine.hpp>
38#include <flow/flow_ebos_brine_saltprecipitation.hpp>
39#include <flow/flow_ebos_gaswater_saltprec_vapwat.hpp>
40#include <flow/flow_ebos_brine_precsalt_vapwat.hpp>
41#include <flow/flow_ebos_onephase.hpp>
42#include <flow/flow_ebos_onephase_energy.hpp>
43#include <flow/flow_ebos_oilwater_brine.hpp>
44#include <flow/flow_ebos_gaswater_brine.hpp>
45#include <flow/flow_ebos_energy.hpp>
46#include <flow/flow_ebos_oilwater_polymer.hpp>
47#include <flow/flow_ebos_oilwater_polymer_injectivity.hpp>
48#include <flow/flow_ebos_micp.hpp>
50#include <opm/input/eclipse/Deck/Deck.hpp>
51#include <opm/input/eclipse/Parser/ErrorGuard.hpp>
52#include <opm/input/eclipse/Parser/Parser.hpp>
53#include <opm/input/eclipse/Parser/ParseContext.hpp>
54#include <opm/input/eclipse/EclipseState/EclipseState.hpp>
55#include <opm/input/eclipse/EclipseState/checkDeck.hpp>
56#include <opm/input/eclipse/Schedule/ArrayDimChecker.hpp>
57#include <opm/input/eclipse/Schedule/UDQ/UDQState.hpp>
58#include <opm/input/eclipse/Schedule/Action/State.hpp>
59#include <opm/input/eclipse/Schedule/Well/WellTestState.hpp>
61#include <opm/models/utils/propertysystem.hh>
62#include <opm/models/utils/parametersystem.hh>
64#include <opm/simulators/flow/FlowMainEbos.hpp>
65#include <opm/simulators/utils/readDeck.hpp>
68#include <dune/fem/misc/mpimanager.hh>
70#include <dune/common/parallel/mpihelper.hh>
74#include <opm/simulators/utils/ParallelEclipseState.hpp>
78#include <opm/simulators/utils/DamarisOutputModule.hpp>
91namespace Opm::Properties {
97 using InheritsFrom = std::tuple<EclFlowProblem>;
106template <
class TypeTag>
107int flowEbosMain(
int argc,
char** argv,
bool outputCout,
bool outputFiles)
114 return mainfunc.execute();
127 Main(
int argc,
char** argv) : argc_(argc), argv_(argv) { initMPI(); }
130 Main(
const std::string& filename)
132 setArgvArgc_(filename);
138 Main(std::shared_ptr<Deck> deck,
139 std::shared_ptr<EclipseState> eclipseState,
140 std::shared_ptr<Schedule> schedule,
141 std::shared_ptr<SummaryConfig> summaryConfig)
142 : deck_{std::move(deck)}
143 , eclipseState_{std::move(eclipseState)}
144 , schedule_{std::move(schedule)}
145 , summaryConfig_{std::move(summaryConfig)}
147 setArgvArgc_(deck_->getDataFile());
155 if (test_split_comm_) {
160 MPI_Comm_size(MPI_COMM_WORLD, &world_size);
161 if (world_size > 1) {
162 MPI_Comm new_comm = EclGenericVanguard::comm();
164 MPI_Comm_compare(MPI_COMM_WORLD, new_comm, &result);
165 assert(result == MPI_UNEQUAL);
166 MPI_Comm_free(&new_comm);
171 EclGenericVanguard::setCommunication(
nullptr);
174 if (enableDamarisOutput_) {
176 if (isSimulationRank_) {
177 err = damaris_stop();
178 if (err != DAMARIS_OK) {
179 std::cerr <<
"ERROR: Damaris library produced an error result for damaris_stop()" << std::endl;
182 err = damaris_finalize();
183 if (err != DAMARIS_OK) {
184 std::cerr <<
"ERROR: Damaris library produced an error result for damaris_finalize()" << std::endl;
189#if HAVE_MPI && !HAVE_DUNE_FEM
194 void setArgvArgc_(
const std::string& filename)
196 this->deckFilename_ = filename;
197 this->flowProgName_ =
"flow";
200 this->saveArgs_[0] =
const_cast<char *
>(this->flowProgName_.c_str());
201 this->saveArgs_[1] =
const_cast<char *
>(this->deckFilename_.c_str());
204 assert ((
sizeof this->saveArgs_) > (this->argc_ *
sizeof this->saveArgs_[0]));
205 this->saveArgs_[this->argc_] =
nullptr;
207 this->argv_ = this->saveArgs_;
213 Dune::Fem::MPIManager::initialize(argc_, argv_);
215 MPI_Init(&argc_, &argv_);
217 EclGenericVanguard::setCommunication(std::make_unique<Parallel::Communication>());
219 handleTestSplitCommunicatorCmdLine_();
222 if (test_split_comm_ && EclGenericVanguard::comm().size() > 1) {
223 int world_rank = EclGenericVanguard::comm().rank();
224 int color = (world_rank == 0);
226 MPI_Comm_split(EclGenericVanguard::comm(), color, world_rank, &new_comm);
227 isSimulationRank_ = (world_rank > 0);
228 EclGenericVanguard::setCommunication(std::make_unique<Parallel::Communication>(new_comm));
235 int exitCode = EXIT_SUCCESS;
236 if (isSimulationRank_) {
237 if (initialize_<Properties::TTag::FlowEarlyBird>(exitCode)) {
238 return this->dispatchDynamic_();
245 template <
class TypeTag>
248 int exitCode = EXIT_SUCCESS;
249 if (isSimulationRank_) {
250 if (initialize_<TypeTag>(exitCode)) {
251 return this->dispatchStatic_<TypeTag>();
263 std::unique_ptr<FlowMainEbosType> initFlowEbosBlackoil(
int& exitCode)
265 exitCode = EXIT_SUCCESS;
266 if (initialize_<Properties::TTag::FlowEarlyBird>(exitCode)) {
269 EclGenericVanguard::setParams(
274 std::move(udqState_),
275 std::move(this->actionState_),
276 std::move(this->wtestState_),
278 return flowEbosBlackoilTpfaMainInit(
279 argc_, argv_, outputCout_, outputFiles_);
282 return std::unique_ptr<FlowMainEbosType>();
287 int dispatchDynamic_()
289 const auto& rspec = this->eclipseState_->runspec();
290 const auto& phases = rspec.phases();
292 EclGenericVanguard::setParams(this->setupTime_,
296 std::move(this->udqState_),
297 std::move(this->actionState_),
298 std::move(this->wtestState_),
299 this->summaryConfig_);
305 const bool thermal = eclipseState_->getSimulationConfig().isThermal();
309 return this->runMICP(phases);
313 else if(phases.size() == 1 && phases.active(Phase::WATER) && !thermal) {
314 return this->runWaterOnly(phases);
318 else if(phases.size() == 2 && phases.active(Phase::WATER) && thermal) {
319 return this->runWaterOnlyEnergy(phases);
323 else if (phases.size() == 2 && !thermal) {
324 return this->runTwoPhase(phases);
328 else if (phases.active(Phase::POLYMER)) {
329 return this->runPolymer(phases);
333 else if (phases.active(Phase::FOAM)) {
334 return this->runFoam();
338 else if (phases.active(Phase::BRINE)) {
339 return this->runBrine(phases);
343 else if (phases.active(Phase::SOLVENT)) {
344 return this->runSolvent();
348 else if (phases.active(Phase::ZFRACTION)) {
349 return this->runExtendedBlackOil();
354 return this->runThermal(phases);
358 else if (phases.size() == 3) {
359 return this->runBlackOil();
364 std::cerr <<
"No suitable configuration found, valid are "
365 <<
"Twophase, polymer, foam, brine, solvent, "
366 <<
"energy, and blackoil.\n";
373 template <
class TypeTag>
374 int dispatchStatic_()
376 EclGenericVanguard::setParams(this->setupTime_,
380 std::move(this->udqState_),
381 std::move(this->actionState_),
382 std::move(this->wtestState_),
383 this->summaryConfig_);
384 return flowEbosMain<TypeTag>(argc_, argv_, outputCout_, outputFiles_);
393 template <
class TypeTagEarlyBird>
394 bool initialize_(
int& exitCode)
396 Dune::Timer externalSetupTimer;
397 externalSetupTimer.start();
399 handleVersionCmdLine_(argc_, argv_);
411 typedef TypeTagEarlyBird PreTypeTag;
412 using PreProblem = GetPropType<PreTypeTag, Properties::Problem>;
414 PreProblem::setBriefDescription(
"Flow, an advanced reservoir simulator for ECL-decks provided by the Open Porous Media project.");
422 MPI_Abort(MPI_COMM_WORLD, status);
424 exitCode = (status > 0) ? status : EXIT_SUCCESS;
428 std::string deckFilename;
429 std::string outputDir;
430 if ( eclipseState_ ) {
431 deckFilename = eclipseState_->getIOConfig().fullBasePath();
432 outputDir = eclipseState_->getIOConfig().getOutputDir();
435 deckFilename = EWOMS_GET_PARAM(PreTypeTag, std::string, EclDeckFileName);
436 outputDir = EWOMS_GET_PARAM(PreTypeTag, std::string, OutputDir);
440 enableDamarisOutput_ = EWOMS_GET_PARAM(PreTypeTag,
bool, EnableDamarisOutput);
441 if (enableDamarisOutput_) {
442 if (!outputDir.empty()) {
443 ensureOutputDirExists(outputDir);
450 const bool enableDamarisOutputCollective = EWOMS_GET_PARAM(PreTypeTag,
bool, EnableDamarisOutputCollective);
452 DamarisOutput::initializeDamaris(EclGenericVanguard::comm(), EclGenericVanguard::comm().rank(), outputDir, enableDamarisOutputCollective);
455 int err = damaris_start(&is_client);
456 isSimulationRank_ = (is_client > 0);
457 if (isSimulationRank_ && err == DAMARIS_OK) {
458 damaris_client_comm_get(&new_comm);
459 EclGenericVanguard::setCommunication(std::make_unique<Parallel::Communication>(new_comm));
466 int mpiRank = EclGenericVanguard::comm().rank();
470 outputCout_ = EWOMS_GET_PARAM(PreTypeTag,
bool, EnableTerminalOutput);
473 if (deckFilename.empty()) {
475 std::cerr <<
"No input case given. Try '--help' for a usage description.\n";
477 exitCode = EXIT_FAILURE;
481 using PreVanguard = GetPropType<PreTypeTag, Properties::Vanguard>;
483 deckFilename = PreVanguard::canonicalDeckPath(deckFilename);
485 catch (
const std::exception& e) {
486 if ( mpiRank == 0 ) {
487 std::cerr <<
"Exception received: " << e.what() <<
". Try '--help' for a usage description.\n";
490 MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
492 exitCode = EXIT_FAILURE;
500 auto python = std::make_shared<Python>();
501 const bool init_from_restart_file = !EWOMS_GET_PARAM(PreTypeTag,
bool, SchedRestart);
502 const bool allRanksDbgPrtLog = EWOMS_GET_PARAM(PreTypeTag,
bool,
503 EnableLoggingFalloutWarning);
504 outputMode = setupLogging(mpiRank,
507 EWOMS_GET_PARAM(PreTypeTag, std::string, OutputMode),
508 outputCout_,
"STDOUT_LOGGER", allRanksDbgPrtLog);
510 std::make_unique<ParseContext>(std::vector<std::pair<std::string , InputError::Action>>
511 {{ParseContext::PARSE_RANDOM_SLASH, InputError::IGNORE},
512 {ParseContext::PARSE_MISSING_DIMS_KEYWORD, InputError::WARN},
513 {ParseContext::SUMMARY_UNKNOWN_WELL, InputError::WARN},
514 {ParseContext::SUMMARY_UNKNOWN_GROUP, InputError::WARN}});
515 if (EWOMS_GET_PARAM(PreTypeTag,
bool, EclStrictParsing))
516 parseContext->update(InputError::DELAYED_EXIT1);
521 OpmLog::info(
"Reading deck file '" + deckFilename +
"'");
524 std::optional<int> outputInterval;
525 int output_param = EWOMS_GET_PARAM(PreTypeTag,
int, EclOutputInterval);
526 if (output_param >= 0)
527 outputInterval = output_param;
529 readDeck(EclGenericVanguard::comm(), deckFilename, deck_, eclipseState_, schedule_, udqState_, actionState_, wtestState_,
530 summaryConfig_,
nullptr, python, std::move(parseContext),
531 init_from_restart_file, outputCout_, outputInterval);
533 verifyValidCellGeometry(EclGenericVanguard::comm(), *this->eclipseState_);
535 setupTime_ = externalSetupTimer.elapsed();
538 catch (
const std::invalid_argument& e)
541 std::cerr <<
"Failed to create valid EclipseState object." << std::endl;
542 std::cerr <<
"Exception caught: " << e.what() << std::endl;
545 MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
547 exitCode = EXIT_FAILURE;
551 exitCode = EXIT_SUCCESS;
555 std::filesystem::path simulationCaseName_(
const std::string& casename)
557 namespace fs = ::std::filesystem;
559 auto exists = [](
const fs::path& f)
561 return (fs::exists(f) && fs::is_regular_file(f))
562 || (fs::is_symlink(f) &&
563 fs::is_regular_file(fs::read_symlink(f)));
566 auto simcase = fs::path { casename };
568 if (exists(simcase)) {
572 for (
const auto& ext : { std::string(
"DATA"), std::string(
"data") }) {
573 if (exists(simcase.replace_extension(ext))) {
578 throw std::invalid_argument {
579 "Cannot find input case '" + casename +
'\''
590 void handleVersionCmdLine_(
int argc,
char** argv)
592 auto pos = std::find_if(argv, argv + argc,
595 return std::strcmp(arg,
"--version") == 0;
598 if (pos != argv + argc) {
600 std::exit(EXIT_SUCCESS);
610 void handleTestSplitCommunicatorCmdLine_()
612 if (argc_ >= 2 && std::strcmp(argv_[1],
"--test-split-communicator=true") == 0) {
613 test_split_comm_ =
true;
620 int runMICP(
const Phases& phases)
622 if (!phases.active(Phase::WATER) || (phases.size() > 2)) {
624 std::cerr <<
"No valid configuration is found for MICP simulation, "
625 <<
"the only valid option is water + MICP\n";
631 return flowEbosMICPMain(this->argc_,
637 int runTwoPhase(
const Phases& phases)
639 const bool diffusive = eclipseState_->getSimulationConfig().isDiffusive();
642 if (phases.active( Phase::OIL ) && phases.active( Phase::GAS )) {
644 return flowEbosGasOilDiffuseMain(argc_, argv_, outputCout_, outputFiles_);
646 return flowEbosGasOilMain(argc_, argv_, outputCout_, outputFiles_);
651 else if ( phases.active( Phase::OIL ) && phases.active( Phase::WATER ) ) {
654 std::cerr <<
"The DIFFUSE option is not available for the two-phase water/oil model." << std::endl;
658 return flowEbosOilWaterMain(argc_, argv_, outputCout_, outputFiles_);
662 else if ( phases.active( Phase::GAS ) && phases.active( Phase::WATER ) ) {
665 std::cerr <<
"The DIFFUSE option is not available for the two-phase gas/water model." << std::endl;
669 return flowEbosGasWaterMain(argc_, argv_, outputCout_, outputFiles_);
673 std::cerr <<
"No suitable configuration found, valid are Twophase (oilwater, oilgas and gaswater), polymer, solvent, or blackoil" << std::endl;
680 int runPolymer(
const Phases& phases)
682 if (! phases.active(Phase::WATER)) {
684 std::cerr <<
"No valid configuration is found for polymer simulation, valid options include "
685 <<
"oilwater + polymer and blackoil + polymer" << std::endl;
692 if (phases.active(Phase::POLYMW)) {
694 assert (phases.size() == 4);
695 return flowEbosOilWaterPolymerInjectivityMain(argc_, argv_, outputCout_, outputFiles_);
698 if (phases.size() == 3) {
699 return flowEbosOilWaterPolymerMain(argc_, argv_, outputCout_, outputFiles_);
702 return flowEbosPolymerMain(argc_, argv_, outputCout_, outputFiles_);
708 return flowEbosFoamMain(argc_, argv_, outputCout_, outputFiles_);
711 int runWaterOnly(
const Phases& phases)
713 if (!phases.active(Phase::WATER) || phases.size() != 1) {
715 std::cerr <<
"No valid configuration is found for water-only simulation, valid options include "
716 <<
"water, water + thermal" << std::endl;
721 return flowEbosWaterOnlyMain(argc_, argv_, outputCout_, outputFiles_);
724 int runWaterOnlyEnergy(
const Phases& phases)
726 if (!phases.active(Phase::WATER) || phases.size() != 2) {
728 std::cerr <<
"No valid configuration is found for water-only simulation, valid options include "
729 <<
"water, water + thermal" << std::endl;
734 return flowEbosWaterOnlyEnergyMain(argc_, argv_, outputCout_, outputFiles_);
737 int runBrine(
const Phases& phases)
739 if (! phases.active(Phase::WATER) || phases.size() == 2) {
741 std::cerr <<
"No valid configuration is found for brine simulation, valid options include "
742 <<
"oilwater + brine, gaswater + brine and blackoil + brine" << std::endl;
747 if (phases.size() == 3) {
749 if (phases.active(Phase::OIL)){
750 return flowEbosOilWaterBrineMain(argc_, argv_, outputCout_, outputFiles_);
752 if (phases.active(Phase::GAS)){
753 if (eclipseState_->getSimulationConfig().hasPRECSALT() &&
754 eclipseState_->getSimulationConfig().hasVAPWAT()) {
756 return flowEbosGasWaterSaltprecVapwatMain(argc_, argv_, outputCout_, outputFiles_);
759 return flowEbosGasWaterBrineMain(argc_, argv_, outputCout_, outputFiles_);
763 else if (eclipseState_->getSimulationConfig().hasPRECSALT()) {
764 if (eclipseState_->getSimulationConfig().hasVAPWAT()) {
766 return flowEbosBrinePrecsaltVapwatMain(argc_, argv_, outputCout_, outputFiles_);
769 return flowEbosBrineSaltPrecipitationMain(argc_, argv_, outputCout_, outputFiles_);
773 return flowEbosBrineMain(argc_, argv_, outputCout_, outputFiles_);
781 return flowEbosSolventMain(argc_, argv_, outputCout_, outputFiles_);
784 int runExtendedBlackOil()
786 return flowEbosExtboMain(argc_, argv_, outputCout_, outputFiles_);
789 int runThermal(
const Phases& phases)
792 if (!phases.active( Phase::WATER ) && phases.active( Phase::OIL ) && phases.active( Phase::GAS )) {
793 return flowEbosGasOilEnergyMain(argc_, argv_, outputCout_, outputFiles_);
796 return flowEbosEnergyMain(argc_, argv_, outputCout_, outputFiles_);
801 const bool diffusive = eclipseState_->getSimulationConfig().isDiffusive();
805 return flowEbosBlackoilMain(argc_, argv_, outputCout_, outputFiles_);
807 return flowEbosBlackoilTpfaMain(argc_, argv_, outputCout_, outputFiles_);
812 char** argv_{
nullptr};
813 bool outputCout_{
false};
814 bool outputFiles_{
false};
815 double setupTime_{0.0};
816 std::string deckFilename_{};
817 std::string flowProgName_{};
818 char *saveArgs_[3]{
nullptr};
819 std::unique_ptr<UDQState> udqState_{};
820 std::unique_ptr<Action::State> actionState_{};
821 std::unique_ptr<WellTestState> wtestState_{};
824 std::shared_ptr<Deck> deck_{};
825 std::shared_ptr<EclipseState> eclipseState_{};
826 std::shared_ptr<Schedule> schedule_{};
827 std::shared_ptr<SummaryConfig> summaryConfig_{};
830 bool test_split_comm_ =
false;
831 bool isSimulationRank_ =
true;
833 bool enableDamarisOutput_ =
false;
Definition: FlowMainEbos.hpp:89
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
FileOutputMode
Definition: readDeck.hpp:49
@ OUTPUT_NONE
No file output.
void readDeck(Parallel::Communication comm, const std::string &deckFilename, std::shared_ptr< Deck > &deck, std::shared_ptr< EclipseState > &eclipseState, std::shared_ptr< Schedule > &schedule, std::unique_ptr< UDQState > &udqState, std::unique_ptr< Action::State > &actionState, std::unique_ptr< WellTestState > &wtestState, std::shared_ptr< SummaryConfig > &summaryConfig, std::unique_ptr< ErrorGuard > errorGuard, std::shared_ptr< Python > python, std::unique_ptr< ParseContext > parseContext, bool initFromRestart, bool checkDeck, const std::optional< int > &outputInterval)
Reads the deck and creates all necessary objects if needed.
Definition: readDeck.cpp:481