Implements access to the porosity from Python.

Implements access (read/write) to the porosity from Python.
This commit is contained in:
Håkon Hægland
2020-11-03 17:18:14 +01:00
parent 53cbe0c57e
commit 648bab7108
11 changed files with 734 additions and 12 deletions
+24
View File
@@ -1,3 +1,9 @@
# 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}
#
pybind11_add_module(simulators simulators.cpp SYSTEM)
set_target_properties( simulators PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/python/opm2 )
@@ -13,3 +19,21 @@ set(PYTHON_PACKAGE_PATH "site-packages")
set(PYTHON_INSTALL_PREFIX "lib/python${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}/${PYTHON_PACKAGE_PATH}" CACHE STRING "Subdirectory to install Python modules in")
install(TARGETS simulators DESTINATION ${DEST_PREFIX}${CMAKE_INSTALL_PREFIX}/${PYTHON_INSTALL_PREFIX}/opm)
configure_file(${PROJECT_SOURCE_DIR}/python/run-python-tests.sh.in
${PROJECT_BINARY_DIR}/tmp/run-python-tests.sh)
file( COPY ${PROJECT_BINARY_DIR}/tmp/run-python-tests.sh
DESTINATION ${PROJECT_BINARY_DIR}/python
FILE_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE )
file( COPY ${PROJECT_SOURCE_DIR}/python/test
DESTINATION ${PROJECT_BINARY_DIR}/python)
file( COPY ${PROJECT_SOURCE_DIR}/python/test_data
DESTINATION ${PROJECT_BINARY_DIR}/python)
find_program (BASH_EXECUTABLE bash)
if (BASH_EXECUTABLE)
add_test(NAME python_tests
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/python
COMMAND ${CMAKE_COMMAND} -E env PYTHONPATH=${PROJECT_BINARY_DIR}/python
${BASH_EXECUTABLE} run-python-tests.sh )
endif(BASH_EXECUTABLE)
+49 -6
View File
@@ -1,3 +1,22 @@
/*
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>
@@ -6,7 +25,9 @@
#define FLOW_BLACKOIL_ONLY
#include <opm/simulators/flow/Main.hpp>
#include <opm/simulators/flow/FlowMainEbos.hpp>
#include <opm/simulators/flow/python/PyMaterialState.hpp>
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <pybind11/embed.h>
// NOTE: EXIT_SUCCESS, EXIT_FAILURE is defined in cstdlib
#include <cstdlib>
@@ -22,12 +43,27 @@ BlackOilSimulator::BlackOilSimulator( const std::string &deckFilename)
{
}
py::array_t<double> 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<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 BlackOilSimulator::step()
{
if (!hasRunInit_) {
@@ -63,6 +99,9 @@ int BlackOilSimulator::stepInit()
if (mainEbos_) {
int result = mainEbos_->executeInitStep();
hasRunInit_ = true;
ebosSimulator_ = mainEbos_->getSimulatorPtr();
materialState_ = std::make_unique<PyMaterialState<TypeTag>>(
ebosSimulator_);
return result;
}
else {
@@ -70,14 +109,18 @@ int BlackOilSimulator::stepInit()
}
}
} // namespace Opm::Python
} // namespace Opm::Pybind
PYBIND11_MODULE(simulators, m)
{
py::class_<Opm::Pybind::BlackOilSimulator>(m, "BlackOilSimulator")
using namespace Opm::Pybind;
py::class_<BlackOilSimulator>(m, "BlackOilSimulator")
.def(py::init< const std::string& >())
.def("run", &Opm::Pybind::BlackOilSimulator::run)
.def("step", &Opm::Pybind::BlackOilSimulator::step)
.def("step_init", &Opm::Pybind::BlackOilSimulator::stepInit)
.def("step_cleanup", &Opm::Pybind::BlackOilSimulator::stepCleanup);
.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);
}