opm-simulators
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  Copyright 2023 Inria
7 
8  This file is part of the Open Porous Media project (OPM).
9 
10  OPM is free software: you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  OPM is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with OPM. If not, see <http://www.gnu.org/licenses/>.
22 */
23 #ifndef OPM_MAIN_HEADER_INCLUDED
24 #define OPM_MAIN_HEADER_INCLUDED
25 
26 #include <opm/input/eclipse/EclipseState/EclipseState.hpp>
27 #include <opm/input/eclipse/Schedule/Action/State.hpp>
28 #include <opm/input/eclipse/Schedule/UDQ/UDQState.hpp>
29 
32 
33 #include <opm/simulators/flow/Banners.hpp>
34 #include <opm/simulators/flow/FlowMain.hpp>
36 
37 #if HAVE_DUNE_FEM
38 #include <dune/fem/misc/mpimanager.hh>
39 #else
40 #include <dune/common/parallel/mpihelper.hh>
41 #endif
42 
43 #if HAVE_MPI
44 #include <opm/simulators/utils/ParallelEclipseState.hpp>
45 #endif
46 
47 #if HAVE_CUDA
48 #include <opm/simulators/linalg/gpuistl/device_management.hpp>
49 #endif
50 
51 #if HAVE_DAMARIS
52 #include <opm/simulators/utils/DamarisKeywords.hpp>
53 #endif
54 
55 #include <cassert>
56 #include <charconv>
57 #include <cstdlib>
58 #include <filesystem>
59 #include <iostream>
60 #include <memory>
61 #include <stdexcept>
62 #include <string>
63 #include <string_view>
64 #include <type_traits>
65 #include <utility>
66 
67 namespace Opm::Properties {
68 
69 // this is a dummy type tag that is used to setup the parameters before the actual
70 // simulator.
71 namespace TTag {
72 struct FlowEarlyBird {
73  using InheritsFrom = std::tuple<FlowProblem>;
74 };
75 }
76 
77 } // namespace Opm::Properties
78 
79 namespace Opm {
80 
81 namespace Action { class State; }
82 class UDQState;
83 class WellTestState;
84 
85 // ----------------- Main program -----------------
86 template <class TypeTag>
87 int flowMain(int argc, char** argv, bool outputCout, bool outputFiles)
88 {
89  // we always want to use the default locale, and thus spare us the trouble
90  // with incorrect locale settings.
91  resetLocale();
92 
93  FlowMain<TypeTag> mainfunc(argc, argv, outputCout, outputFiles);
94  return mainfunc.execute();
95 }
96 
97 // ----------------- Main class -----------------
98 // For now, we will either be instantiated from main() in flow.cpp,
99 // or from a Python pybind11 module..
100 // NOTE (March 2020): When used from a pybind11 module, we do not neccessarily
101 // want to run the whole simulation by calling run(), it is also
102 // useful to just run one report step at a time. According to these different
103 // usage scenarios, we refactored the original run() in flow.cpp into this class.
104 class Main
105 {
106 public:
107  Main(int argc, char** argv, bool ownMPI = true);
108 
109  // This constructor can be called from Python
110  explicit Main(const std::string& filename, bool mpi_init = true, bool mpi_finalize = true);
111 
112  // This constructor can be called from Python when Python has
113  // already parsed a deck
114  Main(const std::string& filename,
115  std::shared_ptr<EclipseState> eclipseState,
116  std::shared_ptr<Schedule> schedule,
117  std::shared_ptr<SummaryConfig> summaryConfig,
118  bool mpi_init = true,
119  bool mpi_finalize = true);
120 
121  ~Main();
122 
123  void setArgvArgc_(const std::string& filename);
124  void maybeSaveReservoirCouplingSlaveLogFilename_();
125  void maybeRedirectReservoirCouplingSlaveOutput_();
126  void initMPI();
127 
135  {
136  int exitCode = EXIT_SUCCESS;
137  if (initialize_<Properties::TTag::FlowEarlyBird>(exitCode)) {
138  Parameters::reset();
139  if (isSimulationRank_) {
140  return this->dispatchDynamic_();
141  }
142  }
143 
144  return exitCode;
145  }
146 
154  template <class TypeTag>
155  int runStatic()
156  {
157  int exitCode = EXIT_SUCCESS;
158  if (initialize_<TypeTag>(exitCode)) {
159  if (isSimulationRank_) {
160  return this->dispatchStatic_<TypeTag>();
161  }
162  }
163 
164  return exitCode;
165  }
166 
169  {
170  int exitCode = EXIT_SUCCESS;
171  initialize_<Properties::TTag::FlowEarlyBird>(exitCode);
172  return exitCode;
173  }
174 
175 protected:
183  template <class TypeTagEarlyBird>
184  bool initialize_(int& exitCode, bool keepKeywords = false)
185  {
186  Dune::Timer externalSetupTimer;
187  externalSetupTimer.start();
188 
189  handleVersionCmdLine_(argc_, argv_, Opm::moduleVersionName());
190 
191  // we always want to use the default locale, and thus spare us the trouble
192  // with incorrect locale settings.
193  resetLocale();
194 
195  // this is a work-around for a catch 22: we do not know what code path to use without
196  // parsing the deck, but we don't know the deck without having access to the
197  // parameters and this requires to know the type tag to be used. To solve this, we
198  // use a type tag just for parsing the parameters before we instantiate the actual
199  // simulator object. (Which parses the parameters again, but since this is done in an
200  // identical manner it does not matter.)
201  typedef TypeTagEarlyBird PreTypeTag;
203 
204  PreProblem::setBriefDescription("Flow, an advanced reservoir simulator for ECL-decks provided by the Open Porous Media project.");
206  if (status != 0) {
207  // if setupParameters_ returns a value smaller than 0, there was no error, but
208  // the program should abort. This is the case e.g. for the --help and the
209  // --print-properties parameters.
210 #if HAVE_MPI
211  if (status >= 0)
212  MPI_Abort(MPI_COMM_WORLD, status);
213 #endif
214  exitCode = (status > 0) ? status : EXIT_SUCCESS;
215  return false; // Whether to run the simulator
216  }
217 
218  OpmLog::setDebugVerbosityLevel(Parameters::Get<Parameters::DebugVerbosityLevel>());
219 
220  std::string deckFilename;
221  std::string outputDir;
222  if ( eclipseState_ ) {
223  deckFilename = eclipseState_->getIOConfig().fullBasePath();
224  outputDir = eclipseState_->getIOConfig().getOutputDir();
225  }
226  else {
227  deckFilename = Parameters::Get<Parameters::EclDeckFileName>();
228  outputDir = Parameters::Get<Parameters::OutputDir>();
229  }
230 
231 #if HAVE_DAMARIS
232  enableDamarisOutput_ = Parameters::Get<Parameters::EnableDamarisOutput>();
233 
234  // Reset to false as we cannot use Damaris if there is only one rank.
235  if ((enableDamarisOutput_ == true) && (FlowGenericVanguard::comm().size() == 1)) {
236  std::string msg ;
237  msg = "\nUse of Damaris (command line argument --enable-damaris-output=true) has been disabled for run with only one rank.\n" ;
238  OpmLog::warning(msg);
239  enableDamarisOutput_ = false ;
240  }
241 
242  if (enableDamarisOutput_) {
243  // Deal with empty (defaulted) output dir, should be deck dir
244  auto damarisOutputDir = outputDir;
245  if (outputDir.empty()) {
246  auto odir = std::filesystem::path{deckFilename}.parent_path();
247  if (odir.empty()) {
248  damarisOutputDir = ".";
249  } else {
250  damarisOutputDir = odir.generic_string();
251  }
252  }
253  // Damaris server ranks will block here until damaris_stop() is called by client ranks
254  this->setupDamaris(damarisOutputDir);
255  }
256 #endif // HAVE_DAMARIS
257 
258  // Guard for when the Damaris core(s) return from damaris_start()
259  // which happens when damaris_stop() is called in main simulation
260  if (!isSimulationRank_) {
261  exitCode = EXIT_SUCCESS;
262  return true;
263  }
264 
265  int mpiRank = FlowGenericVanguard::comm().rank();
266  outputCout_ = false;
267  if (mpiRank == 0)
268  outputCout_ = Parameters::Get<Parameters::EnableTerminalOutput>();
269 
270  if (deckFilename.empty()) {
271  if (mpiRank == 0) {
272  std::cerr << "No input case given. Try '--help' for a usage description.\n";
273  }
274  exitCode = EXIT_FAILURE;
275  return false;
276  }
277 
279  try {
280  deckFilename = PreVanguard::canonicalDeckPath(deckFilename);
281  }
282  catch (const std::exception& e) {
283  if ( mpiRank == 0 ) {
284  std::cerr << "Exception received: " << e.what() << ". Try '--help' for a usage description.\n";
285  }
286 #if HAVE_MPI
287  MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
288 #endif
289  exitCode = EXIT_FAILURE;
290  return false;
291  }
292 
293  std::string cmdline_params;
294  if (outputCout_) {
295  printFlowBanner(FlowGenericVanguard::comm().size(),
296  getNumThreads(),
298  std::ostringstream str;
299  Parameters::printValues(str);
300  cmdline_params = str.str();
301  }
302 
303  // Create Deck and EclipseState.
304  try {
305  this->readDeck(deckFilename,
306  outputDir,
307  Parameters::Get<Parameters::OutputMode>(),
308  !Parameters::Get<Parameters::SchedRestart>(),
309  Parameters::Get<Parameters::EnableLoggingFalloutWarning>(),
310  Parameters::Get<Parameters::ParsingStrictness>(),
311  Parameters::Get<Parameters::ActionParsingStrictness>(),
312  Parameters::Get<Parameters::InputSkipMode>(),
313  keepKeywords,
314  getNumThreads(),
315  Parameters::Get<Parameters::EclOutputInterval>(),
316  Parameters::Get<Parameters::Slave>(),
317  cmdline_params,
320  setupTime_ = externalSetupTimer.elapsed();
321  }
322  // readDeck() may throw std::runtime_error on parse failure.
323  // These exceptions are synchronized across MPI ranks via comm.min()
324  // inside readDeck(), so cooperative shutdown is safe — returning false
325  // lets Main::~Main() call MPI_Finalize().
326  catch (const std::runtime_error& e)
327  {
328  if (outputCout_) {
329  std::cerr << "Failed to create valid EclipseState object." << std::endl;
330  std::cerr << e.what() << std::endl;
331  }
332  exitCode = EXIT_FAILURE;
333  return false;
334  }
335  // Other exceptions (e.g., std::bad_alloc, std::invalid_argument)
336  // may not be synchronized across ranks. Use MPI_Abort() to prevent
337  // deadlocks from unsynchronized failures.
338  catch (const std::exception& e)
339  {
340  if (outputCout_) {
341  std::cerr << "Unexpected error during initialization." << std::endl;
342  std::cerr << "Exception caught: " << e.what() << std::endl;
343  }
344 #if HAVE_MPI
345  MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
346 #endif
347  exitCode = EXIT_FAILURE;
348  return false;
349  }
350 
351 #if HAVE_CUDA
352  Opm::gpuistl::printDevice();
353 #endif
354 
355  exitCode = EXIT_SUCCESS;
356  return true;
357  }
358 
359  void setupVanguard();
360 
361 private:
362  // This function is an extreme special case, if the program has been invoked
363  // *exactly* as:
364  //
365  // flow --version
366  //
367  // the call is intercepted by this function which will print "flow $version"
368  // on stdout and exit(0).
369  void handleVersionCmdLine_(int argc, char** argv,
370  std::string_view moduleVersionName);
371 
372  // This function is a special case, if the program has been invoked
373  // with the argument "--test-split-communicator=true" as the FIRST
374  // argument, it will be removed from the argument list and we set the
375  // test_split_comm_ flag to true.
376  // Note: initializing the parameter system before MPI could make this
377  // use the parameter system instead.
378  void handleTestSplitCommunicatorCmdLine_();
379 
385  int dispatchDynamic_();
386 
395  template <class TypeTag>
396  int dispatchStatic_()
397  {
398  this->setupVanguard();
399  return flowMain<TypeTag>(argc_, argv_, outputCout_, outputFiles_);
400  }
401 
410  int runMICP(const Phases& phases);
411 
420  int runTwoPhase(const Phases& phases);
421 
430  int runBiofilm(const Phases& phases);
431 
440  int runPolymer(const Phases& phases);
441 
447  int runFoam();
448 
457  int runWaterOnly(const Phases& phases);
458 
467  int runWaterOnlyEnergy(const Phases& phases);
468 
477  int runBrine(const Phases& phases);
478 
487  int runSolvent(const Phases& phases);
488 
494  int runExtendedBlackOil();
495 
504  int runThermal(const Phases& phases);
505 
512  int runBlackOilTemp();
513 
519  int runBlackOil();
520 
521 
522 
523  void readDeck(const std::string& deckFilename,
524  const std::string& outputDir,
525  const std::string& outputMode,
526  const bool init_from_restart_file,
527  const bool allRanksDbgPrtLog,
528  const std::string& parsingStrictness,
529  const std::string& actionParsingStrictness,
530  const std::string& inputSkipMode,
531  const bool keepKeywords,
532  const std::size_t numThreads,
533  const int output_param,
534  const bool slaveMode,
535  const std::string& parameters,
536  std::string_view moduleVersion,
537  std::string_view compileTimestamp);
538 
539  static int getNumThreads();
540 
541 #if HAVE_DAMARIS
542  void setupDamaris(const std::string& outputDir);
543 #endif
544 
545 protected:
546  int argc_{0};
547  char** argv_{nullptr};
548  bool outputCout_{false};
549  bool outputFiles_{false};
550 
551 private:
552  bool ownMPI_{true};
553  double setupTime_{0.0};
554  std::string deckFilename_{};
555  std::string flowProgName_{};
556  char *saveArgs_[3]{nullptr};
557  std::unique_ptr<UDQState> udqState_{};
558  std::unique_ptr<Action::State> actionState_{};
559  std::unique_ptr<WellTestState> wtestState_{};
560 
561  // These variables may be owned by both Python and the simulator
562  std::shared_ptr<EclipseState> eclipseState_{};
563  std::shared_ptr<Schedule> schedule_{};
564  std::shared_ptr<SummaryConfig> summaryConfig_{};
565  bool mpi_init_{true};
566  bool mpi_finalize_{true};
567 
568  // To demonstrate run with non_world_comm
569  bool test_split_comm_ = false;
570  bool isSimulationRank_ = true;
571 #if HAVE_MPI
572  std::string reservoirCouplingSlaveOutputFilename_{};
573 #endif
574 #if HAVE_DAMARIS
575  bool enableDamarisOutput_ = false;
576 #endif
577 };
578 
579 } // namespace Opm
580 
581 #endif // OPM_MAIN_HEADER_INCLUDED
typename Properties::Detail::GetPropImpl< TypeTag, Property >::type::type GetPropType
get the type alias defined in the property (equivalent to old macro GET_PROP_TYPE(...))
Definition: propertysystem.hh:233
Definition: Main.hpp:104
std::string moduleVersionName()
Return the version name of the module, for example "2015.10" (for a release branch) or "2016...
Definition: moduleVersion.cpp:34
int runDynamic()
Run simulation.
Definition: Main.hpp:134
This file provides the infrastructure to retrieve run-time parameters.
Definition: FlowMain.hpp:66
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
This file contains a set of helper functions used by VFPProd / VFPInj.
Definition: blackoilbioeffectsmodules.hh:45
static Parallel::Communication & comm()
Obtain global communicator.
Definition: FlowGenericVanguard.hpp:336
This problem simulates an input file given in the data format used by the commercial ECLiPSE simulato...
int runStatic()
Run simulation.
Definition: Main.hpp:155
int justInitialize()
Used for test_outputdir.
Definition: Main.hpp:168
bool initialize_(int &exitCode, bool keepKeywords=false)
Initialize.
Definition: Main.hpp:184
The Opm property system, traits with inheritance.
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: blackoilmodel.hh:80