opm-simulators
PyMain.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_PYMAINBO_HEADER_INCLUDED
24 #define OPM_PYMAINBO_HEADER_INCLUDED
25 
26 #include <opm/simulators/flow/FlowMain.hpp>
27 #include <opm/simulators/flow/Main.hpp>
28 
29 #include <cstddef>
30 #include <cstdlib>
31 #include <memory>
32 #include <string>
33 #include <vector>
34 
35 namespace Opm {
36 
37 template<class TypeTag>
38 std::unique_ptr<FlowMain<TypeTag>>
39 flowMainInit(int argc, char** argv, bool outputCout, bool outputFiles)
40 {
41  // we always want to use the default locale, and thus spare us the trouble
42  // with incorrect locale settings.
43  resetLocale();
44 
45  return std::make_unique<FlowMain<TypeTag>>(argc, argv, outputCout, outputFiles);
46 }
47 
48 // Adds a python-only initialization method
49 template<class TypeTag>
50 class PyMain : public Main
51 {
52 public:
54 
55  using Main::Main;
56 
57  void setArguments(const std::vector<std::string>& args)
58  {
59  if (args.empty()) {
60  return;
61  }
62 
63  // We have the two arguments previously setup (binary name and input
64  // case name) by the main class plus whichever args are in the
65  // parameter that was passed from the python side.
66  this->argc_ = 2 + args.size();
67 
68  // Setup our vector of char*'s
69  argv_python_.resize(2 + args.size());
70  argv_python_[0] = argv_[0];
71  argv_python_[1] = argv_[1];
72  for (std::size_t i = 0; i < args.size(); ++i) {
73  argv_python_[i+2] = const_cast<char*>(args[i].c_str());
74  }
75 
76  // Finally set the main class' argv pointer to the combined
77  // parameter list.
78  this->argv_ = argv_python_.data();
79  }
80 
81  // To be called from the Python interface code. Only do the
82  // initialization and then return a pointer to the FlowMain object that
83  // can later be accessed directly from the Python interface to
84  // e.g. advance the simulator one report step
85  std::unique_ptr<FlowMainType> initFlowBlackoil(int& exitCode)
86  {
87  exitCode = EXIT_SUCCESS;
88 
89  if (this->initialize_<Properties::TTag::FlowEarlyBird>(exitCode, true)) {
90  // TODO: check that this deck really represents a blackoil
91  // case. E.g. check that number of phases == 3
92  this->setupVanguard();
93 
94  return flowMainInit<TypeTag>(argc_, argv_, outputCout_, outputFiles_);
95  }
96 
97  // NOTE: exitCode was set by initialize_() above;
98  return {}; // nullptr
99  }
100 
101 private:
102  std::vector<char*> argv_python_{};
103 };
104 
105 } // namespace Opm
106 
107 #endif // OPM_PYMAINBO_HEADER_INCLUDED
Definition: Main.hpp:104
Definition: PyMain.hpp:50
Definition: FlowMain.hpp:66
This file contains a set of helper functions used by VFPProd / VFPInj.
Definition: blackoilbioeffectsmodules.hh:45