opm-simulators
PyBaseSimulator.hpp
1 /*
2  Copyright 2020 Equinor ASA.
3 
4  This file is part of the Open Porous Media project (OPM).
5 
6  OPM is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  OPM is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with OPM. If not, see <http://www.gnu.org/licenses/>.
18 */
19 #ifndef OPM_PY_BASE_SIMULATOR_HEADER_INCLUDED
20 #define OPM_PY_BASE_SIMULATOR_HEADER_INCLUDED
21 
24 
25 #include <opm/simulators/flow/FlowMain.hpp>
26 #include <opm/simulators/flow/python/PyMain.hpp>
27 #include <opm/simulators/flow/python/PyFluidState.hpp>
28 #include <opm/simulators/flow/python/PyMaterialState.hpp>
29 #include <opm/simulators/flow/python/Pybind11Exporter.hpp>
30 
31 #include <opm/input/eclipse/EclipseState/EclipseState.hpp>
32 #include <opm/input/eclipse/EclipseState/SummaryConfig/SummaryConfig.hpp>
33 #include <opm/input/eclipse/Schedule/Schedule.hpp>
34 #include <opm/input/eclipse/Deck/Deck.hpp>
35 
36 #include <map>
37 #include <memory>
38 #include <string>
39 #include <vector>
40 
41 namespace Opm::Pybind {
42 
43 template<class TypeTag>
45 {
46 private:
48 
49 public:
50  PyBaseSimulator(const std::string& deckFilename,
51  const std::vector<std::string>& args);
52 
53  PyBaseSimulator(std::shared_ptr<Deck> deck,
54  std::shared_ptr<EclipseState> state,
55  std::shared_ptr<Schedule> schedule,
56  std::shared_ptr<SummaryConfig> summary_config,
57  const std::vector<std::string>& args);
58 
59  virtual ~PyBaseSimulator()
60  {
61  Parameters::reset();
62  }
63 
64  void advance(int report_step);
65 
66  bool checkSimulationFinished();
67 
68  int currentStep();
69 
70  py::array_t<double>
71  getFluidStateVariable(const std::string& name) const;
72 
73  py::array_t<double> getCellVolumes();
74 
75  double getDT();
76 
77  py::array_t<double> getPorosity();
78 
79  py::array_t<double> getPrimaryVariable(const std::string& variable) const;
80  py::array_t<int> getPrimaryVarMeaning(const std::string& variable) const;
81 
82  std::map<std::string, int>
83  getPrimaryVarMeaningMap(const std::string& variable) const;
84 
85  int run();
86 
87  using PyCArray = py::array_t<double, py::array::c_style | py::array::forcecast>;
88 
89  void setPorosity(PyCArray array);
90 
91  void setPrimaryVariable(const std::string& idx_name,
92  PyCArray array);
93 
94  void setupMpi(bool init_mpi, bool finalize_mpi);
95  int step();
96  int stepCleanup();
97  int stepInit();
98 
99 protected:
100  FlowMain<TypeTag>& getFlowMain() const;
101  PyFluidState<TypeTag>& getFluidState() const;
102  PyMaterialState<TypeTag>& getMaterialState() const;
103 
104  const std::string deck_filename_{};
105  bool has_run_init_{false};
106  bool has_run_cleanup_{false};
107  bool mpi_init_{true};
108  bool mpi_finalize_{true};
109 
110  // NOTE: the main_ pointer *must* be declared before the flow_main_ pointer
111  // to ensure that it is destroyed before the main object since the main object
112  // destructor will call MPI_Finalize() and flow_main_ object destructor will call
113  // MPI_Comm_free().
114  std::unique_ptr<PyMain<TypeTag>> main_{};
115  std::unique_ptr<FlowMain<TypeTag>> flow_main_{};
116  Simulator* simulator_{nullptr};
117  std::unique_ptr<PyFluidState<TypeTag>> fluid_state_{};
118  std::unique_ptr<PyMaterialState<TypeTag>> material_state_{};
119  std::shared_ptr<Deck> deck_{};
120  std::shared_ptr<EclipseState> eclipse_state_{};
121  std::shared_ptr<Schedule> schedule_{};
122  std::shared_ptr<SummaryConfig> summary_config_{};
123  std::vector<std::string> args_{};
124 }; // class PyBaseSimulator
125 
126 } // namespace Opm::Pybind
127 
128 #include <opm/simulators/flow/python/PyBaseSimulator_impl.hpp>
129 
130 #endif // OPM_PY_BASE_SIMULATOR_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
This file provides the infrastructure to retrieve run-time parameters.
Definition: FlowMain.hpp:66
Definition: PyMaterialState.hpp:33
Definition: PyBaseSimulator.hpp:44
The Opm property system, traits with inheritance.
Definition: PyBaseSimulator.hpp:41
Definition: PyFluidState.hpp:36