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  void advance(int report_step);
60 
61  bool checkSimulationFinished();
62 
63  int currentStep();
64 
65  py::array_t<double>
66  getFluidStateVariable(const std::string& name) const;
67 
68  py::array_t<double> getCellVolumes();
69 
70  double getDT();
71 
72  py::array_t<double> getPorosity();
73 
74  py::array_t<double> getPrimaryVariable(const std::string& variable) const;
75  py::array_t<int> getPrimaryVarMeaning(const std::string& variable) const;
76 
77  std::map<std::string, int>
78  getPrimaryVarMeaningMap(const std::string& variable) const;
79 
80  int run();
81 
82  using PyCArray = py::array_t<double, py::array::c_style | py::array::forcecast>;
83 
84  void setPorosity(PyCArray array);
85 
86  void setPrimaryVariable(const std::string& idx_name,
87  PyCArray array);
88 
89  void setupMpi(bool init_mpi, bool finalize_mpi);
90  int step();
91  int stepCleanup();
92  int stepInit();
93 
94 protected:
95  FlowMain<TypeTag>& getFlowMain() const;
96  PyFluidState<TypeTag>& getFluidState() const;
97  PyMaterialState<TypeTag>& getMaterialState() const;
98 
99  const std::string deck_filename_{};
100  bool has_run_init_{false};
101  bool has_run_cleanup_{false};
102  bool mpi_init_{true};
103  bool mpi_finalize_{true};
104 
105  // NOTE: the main_ pointer *must* be declared before the flow_main_ pointer
106  // to ensure that it is destroyed before the main object since the main object
107  // destructor will call MPI_Finalize() and flow_main_ object destructor will call
108  // MPI_Comm_free().
109  std::unique_ptr<PyMain<TypeTag>> main_{};
110  std::unique_ptr<FlowMain<TypeTag>> flow_main_{};
111  Simulator* simulator_{nullptr};
112  std::unique_ptr<PyFluidState<TypeTag>> fluid_state_{};
113  std::unique_ptr<PyMaterialState<TypeTag>> material_state_{};
114  std::shared_ptr<Deck> deck_{};
115  std::shared_ptr<EclipseState> eclipse_state_{};
116  std::shared_ptr<Schedule> schedule_{};
117  std::shared_ptr<SummaryConfig> summary_config_{};
118  std::vector<std::string> args_{};
119 }; // class PyBaseSimulator
120 
121 } // namespace Opm::Pybind
122 
123 #include <opm/simulators/flow/python/PyBaseSimulator_impl.hpp>
124 
125 #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