mirror of
https://github.com/OPM/opm-simulators.git
synced 2026-09-05 04:40:19 -05:00
Initialize blackoil simulator from schedule shared with Python.
Adds a new constructor to Main.hpp that takes shared pointers to Deck, EclipseState, Schedule, and SummaryConfig. This makes it possible to share these variables with Python without worrying about lifetime issues of the underlying C++ objects. For example, a Python script can first create an opm.io.schedule.Schedule object which is modified from Python. Then, assume the same Python script creates an opm.simulators.BlackOilSimulator which is initialized with the same schedule object. Since the underlying C++ object is a shared pointer, the Schedule object in Python may go out of scope (get deleted by Python) without having the C++ schedule object being deleted. And the Python BlackOilSimulator may continue to be used after the Python Schedule object has been deleted since it still has a valid C++ schedule object.
This commit is contained in:
@@ -1,11 +1,18 @@
|
||||
# NOTE: we assume that add_subdirectory( pybind11 ) is called from the
|
||||
# parent folder's CMakeLists.txt before this CMakeLists.txt is loaded.
|
||||
# Therefore, pybind11's CMakeLists.txt has already run
|
||||
# find_package(PYTHON) to define variables like
|
||||
# ${PYTHON_EXECUTABLE}
|
||||
# parent folder's CMakeLists.txt before this CMakeLists.txt is loaded.
|
||||
# Therefore, pybind11's CMakeLists.txt has already run
|
||||
# find_package(PYTHON) to define variables like ${PYTHON_EXECUTABLE}
|
||||
#
|
||||
|
||||
pybind11_add_module(simulators simulators.cpp ${PYBIND11_SYSTEM})
|
||||
# NOTE: The variable ${PYBIND11_SYSTEM} is set in python/CMakeLists.txt
|
||||
# to the value "SYSTEM" or unset, depending on the current version of Pybind11.
|
||||
# The value is then forwarded to target_include_directories(), see
|
||||
#
|
||||
# https://cmake.org/cmake/help/latest/command/target_include_directories.html
|
||||
# https://pybind11.readthedocs.io/en/stable/compiling.html
|
||||
#
|
||||
pybind11_add_module(simulators ${PYBIND11_SYSTEM}
|
||||
PyBlackOilSimulator.cpp
|
||||
Pybind11Exporter.cpp)
|
||||
|
||||
set_target_properties( simulators PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/python/opm2 )
|
||||
|
||||
@@ -30,9 +37,19 @@ if(OPM_ENABLE_PYTHON_TESTS)
|
||||
if(Python3_EXECUTABLE AND NOT PYTHON_EXECUTABLE)
|
||||
set(PYTHON_EXECUTABLE ${Python3_EXECUTABLE})
|
||||
endif()
|
||||
add_test(NAME python_tests
|
||||
set(PYTHON_PATH ${PROJECT_BINARY_DIR}/python:${opm-common_DIR}/python:$ENV{PYTHONPATH})
|
||||
# NOTE: See comment in test_basic.py for the reason why we are
|
||||
# splitting the python tests into multiple add_test() tests instead
|
||||
# of having a single "python -m unittest" test call that will run all
|
||||
# the tests in the "test" sub directory.
|
||||
add_test(NAME python_basic
|
||||
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/python
|
||||
COMMAND ${CMAKE_COMMAND}
|
||||
-E env PYTHONPATH=${PROJECT_BINARY_DIR}/python:$ENV{PYTHONPATH}
|
||||
${PYTHON_EXECUTABLE} -m unittest )
|
||||
-E env PYTHONPATH=${PYTHON_PATH} ${PYTHON_EXECUTABLE}
|
||||
-m unittest test/test_basic.py)
|
||||
add_test(NAME python_schedule
|
||||
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/python
|
||||
COMMAND ${CMAKE_COMMAND}
|
||||
-E env PYTHONPATH=${PYTHON_PATH} ${PYTHON_EXECUTABLE}
|
||||
-m unittest test/test_schedule.py)
|
||||
endif()
|
||||
|
||||
@@ -0,0 +1,162 @@
|
||||
/*
|
||||
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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
#include <opm/parser/eclipse/Deck/Deck.hpp>
|
||||
#include <opm/parser/eclipse/EclipseState/EclipseState.hpp>
|
||||
#include <opm/parser/eclipse/EclipseState/Schedule/Schedule.hpp>
|
||||
#include <opm/parser/eclipse/EclipseState/SummaryConfig/SummaryConfig.hpp>
|
||||
#define FLOW_BLACKOIL_ONLY
|
||||
#include <opm/simulators/flow/Main.hpp>
|
||||
#include <opm/simulators/flow/FlowMainEbos.hpp>
|
||||
// NOTE: EXIT_SUCCESS, EXIT_FAILURE is defined in cstdlib
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <opm/simulators/flow/python/PyBlackOilSimulator.hpp>
|
||||
|
||||
namespace py = pybind11;
|
||||
|
||||
namespace Opm::Pybind {
|
||||
PyBlackOilSimulator::PyBlackOilSimulator( const std::string &deckFilename)
|
||||
: deckFilename_{deckFilename}
|
||||
{
|
||||
}
|
||||
|
||||
PyBlackOilSimulator::PyBlackOilSimulator(
|
||||
std::shared_ptr<Opm::Deck>& deck,
|
||||
std::shared_ptr<Opm::EclipseState>& state,
|
||||
std::shared_ptr<Opm::Schedule>& schedule,
|
||||
std::shared_ptr<Opm::SummaryConfig>& summary_config
|
||||
)
|
||||
: deck_{deck}
|
||||
, eclipse_state_{state}
|
||||
, schedule_{schedule}
|
||||
, summary_config_{summary_config}
|
||||
{
|
||||
}
|
||||
|
||||
const Opm::FlowMainEbos<typename Opm::Pybind::PyBlackOilSimulator::TypeTag>&
|
||||
PyBlackOilSimulator::getFlowMainEbos() const
|
||||
{
|
||||
if (this->mainEbos_) {
|
||||
return *(this->mainEbos_.get());
|
||||
}
|
||||
else {
|
||||
throw std::runtime_error("BlackOilSimulator not initialized: "
|
||||
"Cannot get reference to FlowMainEbos object" );
|
||||
}
|
||||
}
|
||||
|
||||
py::array_t<double> PyBlackOilSimulator::getPorosity()
|
||||
{
|
||||
std::size_t len;
|
||||
auto array = materialState_->getPorosity(&len);
|
||||
return py::array(len, array.get());
|
||||
}
|
||||
|
||||
int PyBlackOilSimulator::run()
|
||||
{
|
||||
auto mainObject = Opm::Main( deckFilename_ );
|
||||
return mainObject.runDynamic();
|
||||
}
|
||||
|
||||
void PyBlackOilSimulator::setPorosity( py::array_t<double,
|
||||
py::array::c_style | py::array::forcecast> array)
|
||||
{
|
||||
std::size_t size_ = array.size();
|
||||
const double *poro = array.data();
|
||||
materialState_->setPorosity(poro, size_);
|
||||
}
|
||||
|
||||
int PyBlackOilSimulator::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 PyBlackOilSimulator::stepCleanup()
|
||||
{
|
||||
hasRunCleanup_ = true;
|
||||
return mainEbos_->executeStepsCleanup();
|
||||
}
|
||||
|
||||
int PyBlackOilSimulator::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;
|
||||
}
|
||||
}
|
||||
if (this->deck_) {
|
||||
main_ = std::make_unique<Opm::Main>(
|
||||
this->deck_,
|
||||
this->eclipse_state_,
|
||||
this->schedule_,
|
||||
this->summary_config_
|
||||
);
|
||||
}
|
||||
else {
|
||||
main_ = std::make_unique<Opm::Main>( deckFilename_ );
|
||||
}
|
||||
int exitCode = EXIT_SUCCESS;
|
||||
mainEbos_ = main_->initFlowEbosBlackoil(exitCode);
|
||||
if (mainEbos_) {
|
||||
int result = mainEbos_->executeInitStep();
|
||||
hasRunInit_ = true;
|
||||
ebosSimulator_ = mainEbos_->getSimulatorPtr();
|
||||
materialState_ = std::make_unique<PyMaterialState<TypeTag>>(
|
||||
ebosSimulator_);
|
||||
return result;
|
||||
}
|
||||
else {
|
||||
return exitCode;
|
||||
}
|
||||
}
|
||||
|
||||
void export_PyBlackOilSimulator(py::module& m)
|
||||
{
|
||||
py::class_<PyBlackOilSimulator>(m, "BlackOilSimulator")
|
||||
.def(py::init< const std::string& >())
|
||||
.def(py::init<
|
||||
std::shared_ptr<Opm::Deck>&,
|
||||
std::shared_ptr<Opm::EclipseState>&,
|
||||
std::shared_ptr<Opm::Schedule>&,
|
||||
std::shared_ptr<Opm::SummaryConfig>& >())
|
||||
.def("get_porosity", &PyBlackOilSimulator::getPorosity,
|
||||
py::return_value_policy::copy)
|
||||
.def("run", &PyBlackOilSimulator::run)
|
||||
.def("set_porosity", &PyBlackOilSimulator::setPorosity)
|
||||
.def("step", &PyBlackOilSimulator::step)
|
||||
.def("step_init", &PyBlackOilSimulator::stepInit)
|
||||
.def("step_cleanup", &PyBlackOilSimulator::stepCleanup);
|
||||
}
|
||||
|
||||
} // namespace Opm::Pybind
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
#include <pybind11/pybind11.h>
|
||||
#include <opm/simulators/flow/python/Pybind11Exporter.hpp>
|
||||
|
||||
void Opm::Pybind::export_all(py::module& m) {
|
||||
export_PyBlackOilSimulator(m);
|
||||
}
|
||||
|
||||
PYBIND11_MODULE(simulators, m)
|
||||
{
|
||||
Opm::Pybind::export_all(m);
|
||||
}
|
||||
Reference in New Issue
Block a user