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> 30 namespace py = pybind11;
34 template<
class TypeTag>
35 PyBaseSimulator<TypeTag>::PyBaseSimulator(
const std::string& deck_filename,
36 const std::vector<std::string>& args)
37 : deck_filename_{deck_filename}
42 template<
class TypeTag>
43 PyBaseSimulator<TypeTag>::
44 PyBaseSimulator(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)}
59 template<
class TypeTag>
60 void PyBaseSimulator<TypeTag>::advance(
int report_step)
62 while (currentStep() < report_step) {
67 template<
class TypeTag>
68 bool PyBaseSimulator<TypeTag>::checkSimulationFinished()
70 return getFlowMain().getSimTimer()->done();
75 template<
class TypeTag>
76 int PyBaseSimulator<TypeTag>::currentStep()
78 return getFlowMain().getSimTimer()->currentStepNum();
85 template<
class TypeTag>
87 PyBaseSimulator<TypeTag>::getCellVolumes()
89 auto vector = getMaterialState().getCellVolumes();
90 return py::array(vector.size(), vector.data());
93 template<
class TypeTag>
94 double PyBaseSimulator<TypeTag>::getDT()
96 return getFlowMain().getPreviousReportStepSize();
99 template<
class TypeTag>
101 PyBaseSimulator<TypeTag>::getPorosity()
103 auto vector = getMaterialState().getPorosity();
104 return py::array(vector.size(), vector.data());
107 template<
class TypeTag>
109 PyBaseSimulator<TypeTag>::
110 getFluidStateVariable(
const std::string& name)
const 112 auto vector = getFluidState().getFluidStateVariable(name);
113 return py::array(vector.size(), vector.data());
116 template<
class TypeTag>
118 PyBaseSimulator<TypeTag>::
119 getPrimaryVariable(
const std::string& variable)
const 121 auto vector = getFluidState().getPrimaryVariable(variable);
122 return py::array(vector.size(), vector.data());
125 template<
class TypeTag>
127 PyBaseSimulator<TypeTag>::
128 getPrimaryVarMeaning(
const std::string& variable)
const 130 auto vector = getFluidState().getPrimaryVarMeaning(variable);
131 return py::array(vector.size(), vector.data());
134 template<
class TypeTag>
135 std::map<std::string, int>
136 PyBaseSimulator<TypeTag>::
137 getPrimaryVarMeaningMap(
const std::string& variable)
const 140 return getFluidState().getPrimaryVarMeaningMap(variable);
143 template<
class TypeTag>
144 void PyBaseSimulator<TypeTag>::setPorosity(PyCArray array)
146 std::size_t size_ = array.size();
147 const double *poro = array.data();
148 getMaterialState().setPorosity(poro, size_);
151 template<
class TypeTag>
153 PyBaseSimulator<TypeTag>::
154 setPrimaryVariable(
const std::string& variable,
157 std::size_t size_ = array.size();
158 const double *data = array.data();
159 getFluidState().setPrimaryVariable(variable, data, size_);
162 template<
class TypeTag>
163 void PyBaseSimulator<TypeTag>::
164 setupMpi(
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;
173 template<
class TypeTag>
174 int 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();
189 template<
class TypeTag>
190 int PyBaseSimulator<TypeTag>::stepCleanup()
192 this->has_run_cleanup_ =
true;
193 return getFlowMain().executeStepsCleanup();
196 template<
class TypeTag>
197 int 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_);
241 template<
class TypeTag>
242 int PyBaseSimulator<TypeTag>::run()
244 auto main_object = Main( this->deck_filename_ );
245 return main_object.runStatic<TypeTag>();
250 template<
class TypeTag>
252 PyBaseSimulator<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" 265 template<
class TypeTag>
266 PyFluidState<TypeTag>&
267 PyBaseSimulator<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" 280 template<
class TypeTag>
281 PyMaterialState<TypeTag>&
282 PyBaseSimulator<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" 297 #endif // OPM_PY_BASE_SIMULATOR_IMPL_HEADER_INCLUDED Definition: PyBaseSimulator.hpp:41