/* Copyright 2020 Equinor ASA. This file is part of the Open Porous Media project (OPM). OPM is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. OPM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OPM. If not, see . */ #include "config.h" #include #include #include #include #define FLOW_BLACKOIL_ONLY #include #include #include #include #include #include // NOTE: EXIT_SUCCESS, EXIT_FAILURE is defined in cstdlib #include #include #include #include namespace py = pybind11; namespace Opm::Pybind { BlackOilSimulator::BlackOilSimulator( const std::string &deckFilename) : deckFilename_{deckFilename} { } py::array_t BlackOilSimulator::getPorosity() { std::size_t len; auto array = materialState_->getPorosity(&len); return py::array(len, array.get()); } int BlackOilSimulator::run() { auto mainObject = Opm::Main( deckFilename_ ); return mainObject.runDynamic(); } void BlackOilSimulator::setPorosity( py::array_t array) { std::size_t size_ = array.size(); const double *poro = array.data(); materialState_->setPorosity(poro, size_); } int BlackOilSimulator::step() { if (!hasRunInit_) { throw std::logic_error("step() called before step_init()"); } if (hasRunCleanup_) { throw std::logic_error("step() called after step_cleanup()"); } return mainEbos_->executeStep(); } int BlackOilSimulator::stepCleanup() { hasRunCleanup_ = true; return mainEbos_->executeStepsCleanup(); } int BlackOilSimulator::stepInit() { if (hasRunInit_) { // Running step_init() multiple times is not implemented yet, if (hasRunCleanup_) { throw std::logic_error("step_init() called again"); } else { return EXIT_SUCCESS; } } main_ = std::make_unique( deckFilename_ ); int exitCode = EXIT_SUCCESS; mainEbos_ = main_->initFlowEbosBlackoil(exitCode); if (mainEbos_) { int result = mainEbos_->executeInitStep(); hasRunInit_ = true; ebosSimulator_ = mainEbos_->getSimulatorPtr(); materialState_ = std::make_unique>( ebosSimulator_); return result; } else { return exitCode; } } } // namespace Opm::Pybind PYBIND11_MODULE(simulators, m) { using namespace Opm::Pybind; py::class_(m, "BlackOilSimulator") .def(py::init< const std::string& >()) .def("get_porosity", &BlackOilSimulator::getPorosity, py::return_value_policy::copy) .def("run", &BlackOilSimulator::run) .def("set_porosity", &BlackOilSimulator::setPorosity) .def("step", &BlackOilSimulator::step) .def("step_init", &BlackOilSimulator::stepInit) .def("step_cleanup", &BlackOilSimulator::stepCleanup); }