19#ifndef OPM_PY_BASE_SIMULATOR_IMPL_HEADER_INCLUDED
20#define OPM_PY_BASE_SIMULATOR_IMPL_HEADER_INCLUDED
23#ifndef OPM_PY_BASE_SIMULATOR_HEADER_INCLUDED
25#include <opm/simulators/flow/python/PyBaseSimulator.hpp>
30namespace py = pybind11;
32namespace Opm::Pybind {
34template<
class TypeTag>
35PyBaseSimulator<TypeTag>::PyBaseSimulator(
const std::string& deck_filename,
36 const std::vector<std::string>& args)
37 : deck_filename_{deck_filename}
42template<
class TypeTag>
43PyBaseSimulator<TypeTag>::
44PyBaseSimulator(std::shared_ptr<Deck> deck,
45 std::shared_ptr<EclipseState> state,
46 std::shared_ptr<Schedule> schedule,
47 std::shared_ptr<SummaryConfig> summary_config,
48 const std::vector<std::string>& args)
49 : deck_{std::move(deck)}
50 , eclipse_state_{std::move(state)}
51 , schedule_{std::move(schedule)}
52 , summary_config_{std::move(summary_config)}
59template<
class TypeTag>
60void PyBaseSimulator<TypeTag>::advance(
int report_step)
62 while (currentStep() < report_step) {
67template<
class TypeTag>
68bool PyBaseSimulator<TypeTag>::checkSimulationFinished()
70 return getFlowMain().getSimTimer()->done();
75template<
class TypeTag>
76int PyBaseSimulator<TypeTag>::currentStep()
78 return getFlowMain().getSimTimer()->currentStepNum();
85template<
class TypeTag>
87PyBaseSimulator<TypeTag>::getCellVolumes()
89 auto vector = getMaterialState().getCellVolumes();
90 return py::array(vector.size(), vector.data());
93template<
class TypeTag>
94double PyBaseSimulator<TypeTag>::getDT()
96 return getFlowMain().getPreviousReportStepSize();
99template<
class TypeTag>
101PyBaseSimulator<TypeTag>::getPorosity()
103 auto vector = getMaterialState().getPorosity();
104 return py::array(vector.size(), vector.data());
107template<
class TypeTag>
109PyBaseSimulator<TypeTag>::
110getFluidStateVariable(
const std::string& name)
const
112 auto vector = getFluidState().getFluidStateVariable(name);
113 return py::array(vector.size(), vector.data());
116template<
class TypeTag>
118PyBaseSimulator<TypeTag>::
119getPrimaryVariable(
const std::string& variable)
const
121 auto vector = getFluidState().getPrimaryVariable(variable);
122 return py::array(vector.size(), vector.data());
125template<
class TypeTag>
127PyBaseSimulator<TypeTag>::
128getPrimaryVarMeaning(
const std::string& variable)
const
130 auto vector = getFluidState().getPrimaryVarMeaning(variable);
131 return py::array(vector.size(), vector.data());
134template<
class TypeTag>
135std::map<std::string, int>
136PyBaseSimulator<TypeTag>::
137getPrimaryVarMeaningMap(
const std::string& variable)
const
140 return getFluidState().getPrimaryVarMeaningMap(variable);
143template<
class TypeTag>
144void PyBaseSimulator<TypeTag>::setPorosity(PyCArray array)
146 std::size_t size_ = array.size();
147 const double *poro = array.data();
148 getMaterialState().setPorosity(poro, size_);
151template<
class TypeTag>
153PyBaseSimulator<TypeTag>::
154setPrimaryVariable(
const std::string& variable,
157 std::size_t size_ = array.size();
158 const double *data = array.data();
159 getFluidState().setPrimaryVariable(variable, data, size_);
162template<
class TypeTag>
163void PyBaseSimulator<TypeTag>::
164setupMpi(
bool mpi_init,
bool mpi_finalize)
166 if (this->has_run_init_) {
167 throw std::logic_error(
"mpi_init() called after step_init()");
169 this->mpi_init_ = mpi_init;
170 this->mpi_finalize_ = mpi_finalize;
173template<
class TypeTag>
174int PyBaseSimulator<TypeTag>::step()
176 if (!this->has_run_init_) {
177 throw std::logic_error(
"step() called before step_init()");
179 if (this->has_run_cleanup_) {
180 throw std::logic_error(
"step() called after step_cleanup().");
182 if(checkSimulationFinished()) {
183 throw std::logic_error(
"step() called, but simulation is done");
185 auto result = getFlowMain().executeStep();
189template<
class TypeTag>
190int PyBaseSimulator<TypeTag>::stepCleanup()
192 this->has_run_cleanup_ =
true;
193 return getFlowMain().executeStepsCleanup();
196template<
class TypeTag>
197int PyBaseSimulator<TypeTag>::stepInit()
199 if (this->has_run_init_) {
201 if (this->has_run_cleanup_) {
202 throw std::logic_error(
"step_init() called again");
209 this->main_ = std::make_unique<PyMain<TypeTag>>(
210 this->deck_->getDataFile(),
211 this->eclipse_state_,
213 this->summary_config_,
219 this->main_ = std::make_unique<PyMain<TypeTag>>(
220 this->deck_filename_,
225 this->main_->setArguments(args_);
226 int exit_code = EXIT_SUCCESS;
227 this->flow_main_ = this->main_->initFlowBlackoil(exit_code);
228 if (this->flow_main_) {
229 const int result = this->flow_main_->executeInitStep();
230 this->has_run_init_ =
true;
231 this->simulator_ = this->flow_main_->getSimulatorPtr();
232 this->fluid_state_ = std::make_unique<PyFluidState<TypeTag>>(this->simulator_);
233 this->material_state_ = std::make_unique<PyMaterialState<TypeTag>>(this->simulator_);
241template<
class TypeTag>
242int PyBaseSimulator<TypeTag>::run()
244 auto main_object = Main( this->deck_filename_ );
245 return main_object.runStatic<TypeTag>();
250template<
class TypeTag>
252PyBaseSimulator<TypeTag>::getFlowMain()
const
254 if (this->flow_main_) {
255 return *this->flow_main_;
258 throw std::runtime_error(
259 "BlackOilSimulator not initialized: "
260 "Cannot get reference to FlowMain object"
265template<
class TypeTag>
266PyFluidState<TypeTag>&
267PyBaseSimulator<TypeTag>::getFluidState()
const
269 if (this->fluid_state_) {
270 return *this->fluid_state_;
273 throw std::runtime_error(
274 "BlackOilSimulator not initialized: "
275 "Cannot get reference to fluid state object"
280template<
class TypeTag>
281PyMaterialState<TypeTag>&
282PyBaseSimulator<TypeTag>::getMaterialState()
const
284 if (this->material_state_) {
285 return *this->material_state_;
288 throw std::runtime_error(
289 "BlackOilSimulator not initialized: "
290 "Cannot get reference to material state object"