My Project
Loading...
Searching...
No Matches
Main.hpp
1/*
2 Copyright 2013, 2014, 2015 SINTEF ICT, Applied Mathematics.
3 Copyright 2014 Dr. Blatt - HPC-Simulation-Software & Services
4 Copyright 2015 IRIS AS
5 Copyright 2014 STATOIL ASA.
6
7 This file is part of the Open Porous Media project (OPM).
8
9 OPM is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 OPM is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with OPM. If not, see <http://www.gnu.org/licenses/>.
21*/
22#ifndef OPM_MAIN_HEADER_INCLUDED
23#define OPM_MAIN_HEADER_INCLUDED
24
25#include <flow/flow_ebos_blackoil.hpp>
26#include <flow/flow_ebos_blackoil_legacyassembly.hpp>
27
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_gaswater_energy.hpp>
46#include <flow/flow_ebos_gaswater_dissolution.hpp>
47#include <flow/flow_ebos_gaswater_dissolution_diffuse.hpp>
48#include <flow/flow_ebos_energy.hpp>
49#include <flow/flow_ebos_oilwater_polymer.hpp>
50#include <flow/flow_ebos_oilwater_polymer_injectivity.hpp>
51#include <flow/flow_ebos_micp.hpp>
52
53#include <opm/input/eclipse/EclipseState/EclipseState.hpp>
54
55#include <opm/models/utils/propertysystem.hh>
56#include <opm/models/utils/parametersystem.hh>
57
58#include <opm/simulators/flow/Banners.hpp>
59#include <opm/simulators/flow/FlowMainEbos.hpp>
60
61#if HAVE_DUNE_FEM
62#include <dune/fem/misc/mpimanager.hh>
63#else
64#include <dune/common/parallel/mpihelper.hh>
65#endif
66
67#if HAVE_MPI
68#include <opm/simulators/utils/ParallelEclipseState.hpp>
69#endif
70
71#include <cassert>
72#include <cstdlib>
73#include <iostream>
74#include <memory>
75#include <stdexcept>
76#include <string>
77#include <string_view>
78#include <type_traits>
79#include <utility>
80
81namespace Opm::Properties {
82
83// this is a dummy type tag that is used to setup the parameters before the actual
84// simulator.
85namespace TTag {
87 using InheritsFrom = std::tuple<EclFlowProblem>;
88};
89}
90
91} // namespace Opm::Properties
92
93namespace Opm {
94
95namespace Action { class State; }
96class UDQState;
97class WellTestState;
98
99// ----------------- Main program -----------------
100template <class TypeTag>
101int flowEbosMain(int argc, char** argv, bool outputCout, bool outputFiles)
102{
103 // we always want to use the default locale, and thus spare us the trouble
104 // with incorrect locale settings.
105 resetLocale();
106
107 FlowMainEbos<TypeTag> mainfunc(argc, argv, outputCout, outputFiles);
108 return mainfunc.execute();
109}
110
111// ----------------- Main class -----------------
112// For now, we will either be instantiated from main() in flow.cpp,
113// or from a Python pybind11 module..
114// NOTE (March 2020): When used from a pybind11 module, we do not neccessarily
115// want to run the whole simulation by calling run(), it is also
116// useful to just run one report step at a time. According to these different
117// usage scenarios, we refactored the original run() in flow.cpp into this class.
118class Main
119{
120public:
121 Main(int argc, char** argv);
122
123 // This constructor can be called from Python
124 Main(const std::string& filename);
125
126 // This constructor can be called from Python when Python has
127 // already parsed a deck
128 Main(const std::string& filename,
129 std::shared_ptr<EclipseState> eclipseState,
130 std::shared_ptr<Schedule> schedule,
131 std::shared_ptr<SummaryConfig> summaryConfig);
132
133 ~Main();
134
135 void setArgvArgc_(const std::string& filename);
136
137 void initMPI();
138
139 int runDynamic()
140 {
142 if (isSimulationRank_) {
144 return this->dispatchDynamic_();
145 }
146 }
147
148 return exitCode;
149 }
150
151 template <class TypeTag>
152 int runStatic()
153 {
155 if (isSimulationRank_) {
157 return this->dispatchStatic_<TypeTag>();
158 }
159 }
160
161 return exitCode;
162 }
163
165 // To be called from the Python interface code. Only do the
166 // initialization and then return a pointer to the FlowEbosMain
167 // object that can later be accessed directly from the Python interface
168 // to e.g. advance the simulator one report step
169 std::unique_ptr<FlowMainEbosType> initFlowEbosBlackoil(int& exitCode)
170 {
173 // TODO: check that this deck really represents a blackoil
174 // case. E.g. check that number of phases == 3
175 this->setupVanguard();
177 argc_, argv_, outputCout_, outputFiles_);
178 } else {
179 //NOTE: exitCode was set by initialize_() above;
180 return std::unique_ptr<FlowMainEbosType>(); // nullptr
181 }
182 }
183
184private:
185 int dispatchDynamic_()
186 {
187 const auto& rspec = this->eclipseState_->runspec();
188 const auto& phases = rspec.phases();
189
190 this->setupVanguard();
191
192 // run the actual simulator
193 //
194 // TODO: make sure that no illegal combinations like thermal and
195 // twophase are requested.
196 const bool thermal = eclipseState_->getSimulationConfig().isThermal();
197
198 // Single-phase case
199 if (rspec.micp()) {
200 return this->runMICP(phases);
201 }
202
203 // water-only case
204 else if (phases.size() == 1 && phases.active(Phase::WATER) && !thermal) {
205 return this->runWaterOnly(phases);
206 }
207
208 // water-only case with energy
209 else if (phases.size() == 2 && phases.active(Phase::WATER) && thermal) {
210 return this->runWaterOnlyEnergy(phases);
211 }
212
213 // Twophase cases
214 else if (phases.size() == 2 && !thermal) {
215 return this->runTwoPhase(phases);
216 }
217
218 // Polymer case
219 else if (phases.active(Phase::POLYMER)) {
220 return this->runPolymer(phases);
221 }
222
223 // Foam case
224 else if (phases.active(Phase::FOAM)) {
225 return this->runFoam();
226 }
227
228 // Brine case
229 else if (phases.active(Phase::BRINE)) {
230 return this->runBrine(phases);
231 }
232
233 // Solvent case
234 else if (phases.active(Phase::SOLVENT)) {
235 return this->runSolvent();
236 }
237
238 // Extended BO case
239 else if (phases.active(Phase::ZFRACTION)) {
240 return this->runExtendedBlackOil();
241 }
242
243 // Energy case
244 else if (thermal) {
245 return this->runThermal(phases);
246 }
247
248 // Blackoil case
249 else if (phases.size() == 3) {
250 return this->runBlackOil();
251 }
252
253 else {
254 if (outputCout_) {
255 std::cerr << "No suitable configuration found, valid are "
256 << "Twophase, polymer, foam, brine, solvent, "
257 << "energy, and blackoil.\n";
258 }
259
260 return EXIT_FAILURE;
261 }
262 }
263
264 template <class TypeTag>
265 int dispatchStatic_()
266 {
267 this->setupVanguard();
268 return flowEbosMain<TypeTag>(argc_, argv_, outputCout_, outputFiles_);
269 }
270
277 template <class TypeTagEarlyBird>
278 bool initialize_(int& exitCode)
279 {
280 Dune::Timer externalSetupTimer;
281 externalSetupTimer.start();
282
283 handleVersionCmdLine_(argc_, argv_, Opm::moduleVersionName());
284
285 // we always want to use the default locale, and thus spare us the trouble
286 // with incorrect locale settings.
287 resetLocale();
288
289 // this is a work-around for a catch 22: we do not know what code path to use without
290 // parsing the deck, but we don't know the deck without having access to the
291 // parameters and this requires to know the type tag to be used. To solve this, we
292 // use a type tag just for parsing the parameters before we instantiate the actual
293 // simulator object. (Which parses the parameters again, but since this is done in an
294 // identical manner it does not matter.)
297
298 PreProblem::setBriefDescription("Flow, an advanced reservoir simulator for ECL-decks provided by the Open Porous Media project.");
299 int status = FlowMainEbos<PreTypeTag>::setupParameters_(argc_, argv_, EclGenericVanguard::comm());
300 if (status != 0) {
301 // if setupParameters_ returns a value smaller than 0, there was no error, but
302 // the program should abort. This is the case e.g. for the --help and the
303 // --print-properties parameters.
304#if HAVE_MPI
305 if (status >= 0)
306 MPI_Abort(MPI_COMM_WORLD, status);
307#endif
308 exitCode = (status > 0) ? status : EXIT_SUCCESS;
309 return false; // Whether to run the simulator
310 }
311
312 std::string deckFilename;
313 std::string outputDir;
314 if ( eclipseState_ ) {
315 deckFilename = eclipseState_->getIOConfig().fullBasePath();
316 outputDir = eclipseState_->getIOConfig().getOutputDir();
317 }
318 else {
319 deckFilename = EWOMS_GET_PARAM(PreTypeTag, std::string, EclDeckFileName);
321 }
322
323#if HAVE_DAMARIS
328 }
329#endif // HAVE_DAMARIS
330
331 int mpiRank = EclGenericVanguard::comm().rank();
332 outputCout_ = false;
333 if (mpiRank == 0)
334 outputCout_ = EWOMS_GET_PARAM(PreTypeTag, bool, EnableTerminalOutput);
335
336
337 if (deckFilename.empty()) {
338 if (mpiRank == 0) {
339 std::cerr << "No input case given. Try '--help' for a usage description.\n";
340 }
342 return false;
343 }
344
346 try {
347 deckFilename = PreVanguard::canonicalDeckPath(deckFilename);
348 }
349 catch (const std::exception& e) {
350 if ( mpiRank == 0 ) {
351 std::cerr << "Exception received: " << e.what() << ". Try '--help' for a usage description.\n";
352 }
353#if HAVE_MPI
355#endif
357 return false;
358 }
359
360 std::string cmdline_params;
361 if (outputCout_) {
362 printFlowBanner(EclGenericVanguard::comm().size(),
365 std::ostringstream str;
366 Parameters::printValues<PreTypeTag>(str);
367 cmdline_params = str.str();
368 }
369
370 // Create Deck and EclipseState.
371 try {
372 this->readDeck(deckFilename,
373 outputDir,
376 EWOMS_GET_PARAM(PreTypeTag, bool, EnableLoggingFalloutWarning),
378 mpiRank,
383 setupTime_ = externalSetupTimer.elapsed();
384 }
385 catch (const std::invalid_argument& e)
386 {
387 if (outputCout_) {
388 std::cerr << "Failed to create valid EclipseState object." << std::endl;
389 std::cerr << "Exception caught: " << e.what() << std::endl;
390 }
391#if HAVE_MPI
393#endif
395 return false;
396 }
397
399 return true;
400 }
401
402 // This function is an extreme special case, if the program has been invoked
403 // *exactly* as:
404 //
405 // flow --version
406 //
407 // the call is intercepted by this function which will print "flow $version"
408 // on stdout and exit(0).
409 void handleVersionCmdLine_(int argc, char** argv,
410 std::string_view moduleVersionName);
411
412 // This function is a special case, if the program has been invoked
413 // with the argument "--test-split-communicator=true" as the FIRST
414 // argument, it will be removed from the argument list and we set the
415 // test_split_comm_ flag to true.
416 // Note: initializing the parameter system before MPI could make this
417 // use the parameter system instead.
418 void handleTestSplitCommunicatorCmdLine_();
419
420 int runMICP(const Phases& phases)
421 {
422 if (!phases.active(Phase::WATER) || (phases.size() > 2)) {
423 if (outputCout_) {
424 std::cerr << "No valid configuration is found for MICP simulation, "
425 << "the only valid option is water + MICP\n";
426 }
427
428 return EXIT_FAILURE;
429 }
430
431 return flowEbosMICPMain(this->argc_,
432 this->argv_,
433 this->outputCout_,
434 this->outputFiles_);
435 }
436
437 int runTwoPhase(const Phases& phases)
438 {
439 const bool diffusive = eclipseState_->getSimulationConfig().isDiffusive();
440 const bool disgasw = eclipseState_->getSimulationConfig().hasDISGASW();
441
442 // oil-gas
443 if (phases.active( Phase::OIL ) && phases.active( Phase::GAS )) {
444 if (diffusive) {
445 return flowEbosGasOilDiffuseMain(argc_, argv_, outputCout_, outputFiles_);
446 } else {
447 return flowEbosGasOilMain(argc_, argv_, outputCout_, outputFiles_);
448 }
449 }
450
451 // oil-water
452 else if ( phases.active( Phase::OIL ) && phases.active( Phase::WATER ) ) {
453 if (diffusive) {
454 if (outputCout_) {
455 std::cerr << "The DIFFUSE option is not available for the two-phase water/oil model." << std::endl;
456 }
457 return EXIT_FAILURE;
458 }
459 return flowEbosOilWaterMain(argc_, argv_, outputCout_, outputFiles_);
460 }
461
462 // gas-water
463 else if ( phases.active( Phase::GAS ) && phases.active( Phase::WATER ) ) {
464 if (disgasw) {
465 if (diffusive) {
466 return flowEbosGasWaterDissolutionDiffuseMain(argc_, argv_, outputCout_, outputFiles_);
467 }
468 return flowEbosGasWaterDissolutionMain(argc_, argv_, outputCout_, outputFiles_);
469 }
470 if (diffusive) {
471 if (outputCout_) {
472 std::cerr << "The DIFFUSE option is not available for the two-phase gas/water model without disgasw." << std::endl;
473 }
474 return EXIT_FAILURE;
475 }
476
477 return flowEbosGasWaterMain(argc_, argv_, outputCout_, outputFiles_);
478 }
479 else {
480 if (outputCout_) {
481 std::cerr << "No suitable configuration found, valid are Twophase (oilwater, oilgas and gaswater), polymer, solvent, or blackoil" << std::endl;
482 }
483
484 return EXIT_FAILURE;
485 }
486 }
487
488 int runPolymer(const Phases& phases)
489 {
490 if (! phases.active(Phase::WATER)) {
491 if (outputCout_)
492 std::cerr << "No valid configuration is found for polymer simulation, valid options include "
493 << "oilwater + polymer and blackoil + polymer" << std::endl;
494
495 return EXIT_FAILURE;
496 }
497
498 // Need to track the polymer molecular weight
499 // for the injectivity study
500 if (phases.active(Phase::POLYMW)) {
501 // only oil water two phase for now
502 assert (phases.size() == 4);
503 return flowEbosOilWaterPolymerInjectivityMain(argc_, argv_, outputCout_, outputFiles_);
504 }
505
506 if (phases.size() == 3) { // oil water polymer case
507 return flowEbosOilWaterPolymerMain(argc_, argv_, outputCout_, outputFiles_);
508 }
509 else {
510 return flowEbosPolymerMain(argc_, argv_, outputCout_, outputFiles_);
511 }
512 }
513
514 int runFoam()
515 {
516 return flowEbosFoamMain(argc_, argv_, outputCout_, outputFiles_);
517 }
518
519 int runWaterOnly(const Phases& phases)
520 {
521 if (!phases.active(Phase::WATER) || phases.size() != 1) {
522 if (outputCout_)
523 std::cerr << "No valid configuration is found for water-only simulation, valid options include "
524 << "water, water + thermal" << std::endl;
525
526 return EXIT_FAILURE;
527 }
528
529 return flowEbosWaterOnlyMain(argc_, argv_, outputCout_, outputFiles_);
530 }
531
532 int runWaterOnlyEnergy(const Phases& phases)
533 {
534 if (!phases.active(Phase::WATER) || phases.size() != 2) {
535 if (outputCout_)
536 std::cerr << "No valid configuration is found for water-only simulation, valid options include "
537 << "water, water + thermal" << std::endl;
538
539 return EXIT_FAILURE;
540 }
541
542 return flowEbosWaterOnlyEnergyMain(argc_, argv_, outputCout_, outputFiles_);
543 }
544
545 int runBrine(const Phases& phases)
546 {
547 if (! phases.active(Phase::WATER) || phases.size() == 2) {
548 if (outputCout_)
549 std::cerr << "No valid configuration is found for brine simulation, valid options include "
550 << "oilwater + brine, gaswater + brine and blackoil + brine" << std::endl;
551
552 return EXIT_FAILURE;
553 }
554
555 if (phases.size() == 3) {
556
557 if (phases.active(Phase::OIL)){ // oil water brine case
558 return flowEbosOilWaterBrineMain(argc_, argv_, outputCout_, outputFiles_);
559 }
560 if (phases.active(Phase::GAS)){ // gas water brine case
561 if (eclipseState_->getSimulationConfig().hasPRECSALT() &&
562 eclipseState_->getSimulationConfig().hasVAPWAT()) {
563 //case with water vaporization into gas phase and salt precipitation
564 return flowEbosGasWaterSaltprecVapwatMain(argc_, argv_, outputCout_, outputFiles_);
565 }
566 else {
567 return flowEbosGasWaterBrineMain(argc_, argv_, outputCout_, outputFiles_);
568 }
569 }
570 }
571 else if (eclipseState_->getSimulationConfig().hasPRECSALT()) {
572 if (eclipseState_->getSimulationConfig().hasVAPWAT()) {
573 //case with water vaporization into gas phase and salt precipitation
574 return flowEbosBrinePrecsaltVapwatMain(argc_, argv_, outputCout_, outputFiles_);
575 }
576 else {
577 return flowEbosBrineSaltPrecipitationMain(argc_, argv_, outputCout_, outputFiles_);
578 }
579 }
580 else {
581 return flowEbosBrineMain(argc_, argv_, outputCout_, outputFiles_);
582 }
583
584 return EXIT_FAILURE;
585 }
586
587 int runSolvent()
588 {
589 return flowEbosSolventMain(argc_, argv_, outputCout_, outputFiles_);
590 }
591
592 int runExtendedBlackOil()
593 {
594 return flowEbosExtboMain(argc_, argv_, outputCout_, outputFiles_);
595 }
596
597 int runThermal(const Phases& phases)
598 {
599 // oil-gas-thermal
600 if (!phases.active( Phase::WATER ) && phases.active( Phase::OIL ) && phases.active( Phase::GAS )) {
601 return flowEbosGasOilEnergyMain(argc_, argv_, outputCout_, outputFiles_);
602 }
603
604 // water-gas-thermal
605 if (!phases.active( Phase::OIL ) && phases.active( Phase::WATER ) && phases.active( Phase::GAS )) {
606 return flowEbosGasWaterEnergyMain(argc_, argv_, outputCout_, outputFiles_);
607 }
608
609 return flowEbosEnergyMain(argc_, argv_, outputCout_, outputFiles_);
610 }
611
612 int runBlackOil()
613 {
614 const bool diffusive = eclipseState_->getSimulationConfig().isDiffusive();
615 if (diffusive) {
616 // Use the traditional linearizer, as the TpfaLinearizer does not
617 // support the diffusion module yet.
618 return flowEbosBlackoilMain(argc_, argv_, outputCout_, outputFiles_);
619 } else {
620 return flowEbosBlackoilTpfaMain(argc_, argv_, outputCout_, outputFiles_);
621 }
622 }
623
624 void readDeck(const std::string& deckFilename,
625 const std::string& outputDir,
626 const std::string& outputMode,
627 const bool init_from_restart_file,
628 const bool allRanksDbgPrtLog,
629 const std::string& parsingStrictness,
630 const int mpiRank,
631 const int output_param,
632 const std::string& parameters,
633 std::string_view moduleVersion,
634 std::string_view compileTimestamp);
635
636 void setupVanguard();
637
638 template<class TypeTag>
639 static int getNumThreads()
640 {
641
642 int threads = 1;
643
644#ifdef _OPENMP
645 // This function is called before the parallel OpenMP stuff gets initialized.
646 // That initialization happends after the deck is read and we want this message.
647 // Hence we duplicate the code of setupParallelism to get the number of threads.
648 if (std::getenv("OMP_NUM_THREADS"))
650 else
651 threads = std::min(2, omp_get_max_threads());
652
653 const int input_threads = EWOMS_GET_PARAM(TypeTag, int, ThreadsPerProcess);
654
655 if (input_threads > 0)
657#endif
658
659 return threads;
660 }
661
662#if HAVE_DAMARIS
663 void setupDamaris(const std::string& outputDir,
665#endif
666
667 int argc_{0};
668 char** argv_{nullptr};
669 bool outputCout_{false};
670 bool outputFiles_{false};
671 double setupTime_{0.0};
672 std::string deckFilename_{};
673 std::string flowProgName_{};
674 char *saveArgs_[3]{nullptr};
675 std::unique_ptr<UDQState> udqState_{};
676 std::unique_ptr<Action::State> actionState_{};
677 std::unique_ptr<WellTestState> wtestState_{};
678
679 // These variables may be owned by both Python and the simulator
680 std::shared_ptr<EclipseState> eclipseState_{};
681 std::shared_ptr<Schedule> schedule_{};
682 std::shared_ptr<SummaryConfig> summaryConfig_{};
683
684 // To demonstrate run with non_world_comm
685 bool test_split_comm_ = false;
686 bool isSimulationRank_ = true;
687#if HAVE_DAMARIS
688 bool enableDamarisOutput_ = false;
689#endif
690};
691
692} // namespace Opm
693
694#endif // OPM_MAIN_HEADER_INCLUDED
Definition AquiferInterface.hpp:35
Definition FlowMainEbos.hpp:93
Definition Main.hpp:119
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