mirror of
https://github.com/OPM/opm-simulators.git
synced 2026-09-05 04:40:19 -05:00
Get primary variables and fluid state from Python
Added methods to Python module opm.simulators.BlackOilSimulator to access primary variables and fluid state variables.
This commit is contained in:
@@ -41,21 +41,13 @@ if(OPM_ENABLE_PYTHON_TESTS)
|
||||
# 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=${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)
|
||||
add_test(NAME python_throw
|
||||
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/python
|
||||
COMMAND ${CMAKE_COMMAND}
|
||||
-E env PYTHONPATH=${PYTHON_PATH} ${PYTHON_EXECUTABLE}
|
||||
-m unittest test/test_throw.py)
|
||||
foreach(case_name IN ITEMS basic fluidstate_variables primary_variables schedule throw)
|
||||
add_test(NAME python_${case_name}
|
||||
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/python
|
||||
COMMAND ${CMAKE_COMMAND}
|
||||
-E env PYTHONPATH=${PYTHON_PATH} ${PYTHON_EXECUTABLE}
|
||||
-m unittest test/test_${case_name}.py)
|
||||
endforeach()
|
||||
endif()
|
||||
|
||||
find_file(PYTHON_INSTALL_PY install.py
|
||||
|
||||
@@ -111,6 +111,41 @@ py::array_t<double> PyBlackOilSimulator::getPorosity()
|
||||
return py::array(len, array.get());
|
||||
}
|
||||
|
||||
py::array_t<double>
|
||||
PyBlackOilSimulator::
|
||||
getFluidStateVariable(const std::string &name) const
|
||||
{
|
||||
std::size_t len;
|
||||
auto array = getFluidState().getFluidStateVariable(name, &len);
|
||||
return py::array(len, array.get());
|
||||
}
|
||||
|
||||
py::array_t<double>
|
||||
PyBlackOilSimulator::
|
||||
getPrimaryVariable(const std::string &variable) const
|
||||
{
|
||||
std::size_t len;
|
||||
auto array = getFluidState().getPrimaryVariable(variable, &len);
|
||||
return py::array(len, array.get());
|
||||
}
|
||||
|
||||
py::array_t<int>
|
||||
PyBlackOilSimulator::
|
||||
getPrimaryVarMeaning(const std::string &variable) const
|
||||
{
|
||||
std::size_t len;
|
||||
auto array = getFluidState().getPrimaryVarMeaning(variable, &len);
|
||||
return py::array(len, array.get());
|
||||
}
|
||||
|
||||
std::map<std::string, int>
|
||||
PyBlackOilSimulator::
|
||||
getPrimaryVarMeaningMap(const std::string &variable) const
|
||||
{
|
||||
|
||||
return getFluidState().getPrimaryVarMeaningMap(variable);
|
||||
}
|
||||
|
||||
int PyBlackOilSimulator::run()
|
||||
{
|
||||
auto main_object = Opm::Main( this->deck_filename_ );
|
||||
@@ -125,6 +160,19 @@ void PyBlackOilSimulator::setPorosity( py::array_t<double,
|
||||
getMaterialState().setPorosity(poro, size_);
|
||||
}
|
||||
|
||||
void
|
||||
PyBlackOilSimulator::
|
||||
setPrimaryVariable(
|
||||
const std::string &idx_name,
|
||||
py::array_t<double,
|
||||
py::array::c_style | py::array::forcecast> array
|
||||
)
|
||||
{
|
||||
std::size_t size_ = array.size();
|
||||
const double *data = array.data();
|
||||
getFluidState().setPrimaryVariable(idx_name, data, size_);
|
||||
}
|
||||
|
||||
int PyBlackOilSimulator::step()
|
||||
{
|
||||
if (!this->has_run_init_) {
|
||||
@@ -177,6 +225,7 @@ int PyBlackOilSimulator::stepInit()
|
||||
int result = this->main_ebos_->executeInitStep();
|
||||
this->has_run_init_ = true;
|
||||
this->ebos_simulator_ = this->main_ebos_->getSimulatorPtr();
|
||||
this->fluid_state_ = std::make_unique<PyFluidState<TypeTag>>(this->ebos_simulator_);
|
||||
this->material_state_ = std::make_unique<PyMaterialState<TypeTag>>(this->ebos_simulator_);
|
||||
return result;
|
||||
}
|
||||
@@ -200,7 +249,20 @@ Opm::FlowMain<typename Opm::Pybind::PyBlackOilSimulator::TypeTag>&
|
||||
}
|
||||
}
|
||||
|
||||
PyMaterialState<typename Opm::Pybind::PyBlackOilSimulator::TypeTag>&
|
||||
PyFluidState<typename PyBlackOilSimulator::TypeTag>&
|
||||
PyBlackOilSimulator::
|
||||
getFluidState() const
|
||||
{
|
||||
if (this->fluid_state_) {
|
||||
return *this->fluid_state_;
|
||||
}
|
||||
else {
|
||||
throw std::runtime_error("BlackOilSimulator not initialized: "
|
||||
"Cannot get reference to FlowMainEbos object" );
|
||||
}
|
||||
}
|
||||
|
||||
PyMaterialState<typename PyBlackOilSimulator::TypeTag>&
|
||||
PyBlackOilSimulator::getMaterialState() const
|
||||
{
|
||||
if (this->material_state_) {
|
||||
@@ -222,18 +284,29 @@ void export_PyBlackOilSimulator(py::module& m)
|
||||
std::shared_ptr<Opm::EclipseState>,
|
||||
std::shared_ptr<Opm::Schedule>,
|
||||
std::shared_ptr<Opm::SummaryConfig> >())
|
||||
.def("advance", &PyBlackOilSimulator::advance, py::arg("report_step"))
|
||||
.def("current_step", &PyBlackOilSimulator::currentStep)
|
||||
.def("get_cell_volumes", &PyBlackOilSimulator::getCellVolumes,
|
||||
py::return_value_policy::copy)
|
||||
.def("get_dt", &PyBlackOilSimulator::getDT)
|
||||
.def("get_fluidstate_variable", &PyBlackOilSimulator::getFluidStateVariable,
|
||||
py::return_value_policy::copy, py::arg("name"))
|
||||
.def("get_porosity", &PyBlackOilSimulator::getPorosity,
|
||||
py::return_value_policy::copy)
|
||||
.def("get_primary_variable_meaning", &PyBlackOilSimulator::getPrimaryVarMeaning,
|
||||
py::return_value_policy::copy, py::arg("variable"))
|
||||
.def("get_primary_variable_meaning_map", &PyBlackOilSimulator::getPrimaryVarMeaningMap,
|
||||
py::return_value_policy::copy, py::arg("variable"))
|
||||
.def("get_primary_variable", &PyBlackOilSimulator::getPrimaryVariable,
|
||||
py::return_value_policy::copy, py::arg("variable"))
|
||||
.def("run", &PyBlackOilSimulator::run)
|
||||
.def("set_porosity", &PyBlackOilSimulator::setPorosity)
|
||||
.def("set_primary_variable", &PyBlackOilSimulator::setPrimaryVariable,
|
||||
py::arg("idx_name"), py::arg("value"))
|
||||
.def("current_step", &PyBlackOilSimulator::currentStep)
|
||||
.def("step", &PyBlackOilSimulator::step)
|
||||
.def("advance", &PyBlackOilSimulator::advance, py::arg("report_step"))
|
||||
.def("step_init", &PyBlackOilSimulator::stepInit)
|
||||
.def("step_cleanup", &PyBlackOilSimulator::stepCleanup);
|
||||
.def("step_cleanup", &PyBlackOilSimulator::stepCleanup)
|
||||
.def("step_init", &PyBlackOilSimulator::stepInit);
|
||||
}
|
||||
|
||||
} // namespace Opm::Pybind
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import os
|
||||
import unittest
|
||||
from contextlib import contextmanager
|
||||
from pathlib import Path
|
||||
from opm.simulators import BlackOilSimulator
|
||||
from .pytest_common import pushd
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
import os
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from opm.simulators import BlackOilSimulator
|
||||
from .pytest_common import pushd
|
||||
|
||||
class TestBasic(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
# NOTE: See comment in test_basic.py for the reason why we are
|
||||
# only using a single test_all() function instead of splitting
|
||||
# it up in multiple test functions
|
||||
test_dir = Path(os.path.dirname(__file__))
|
||||
cls.data_dir = test_dir.parent.joinpath("test_data/SPE1CASE1a")
|
||||
|
||||
|
||||
def test_all(self):
|
||||
with pushd(self.data_dir):
|
||||
sim = BlackOilSimulator("SPE1CASE1.DATA")
|
||||
sim.step_init()
|
||||
sim.step()
|
||||
oil_pressure = sim.get_fluidstate_variable(name='po')
|
||||
self.assertAlmostEqual(oil_pressure[0], 41729978.837, places=2, msg='value of oil pressure')
|
||||
gas_pressure = sim.get_fluidstate_variable(name='pg')
|
||||
self.assertAlmostEqual(gas_pressure[0], 41729978.837, places=2, msg='value of gas pressure')
|
||||
water_pressure = sim.get_fluidstate_variable(name='pw')
|
||||
self.assertAlmostEqual(water_pressure[0], 41729978.837, places=2, msg='value of water pressure')
|
||||
rho_w = sim.get_fluidstate_variable(name='rho_w')
|
||||
self.assertAlmostEqual(rho_w[0], 1001.7549054, places=6, msg='value of water density')
|
||||
rho_g = sim.get_fluidstate_variable(name='rho_g')
|
||||
self.assertAlmostEqual(rho_g[0], 275.72397867, places=7, msg='value of gas density')
|
||||
rho_o = sim.get_fluidstate_variable(name='rho_o')
|
||||
self.assertAlmostEqual(rho_o[0], 639.64061021, places=7, msg='value of oil density')
|
||||
Rs = sim.get_fluidstate_variable(name='Rs')
|
||||
self.assertAlmostEqual(Rs[0], 226.196660482, places=7, msg='value of solution gas-oil ratio')
|
||||
Rv = sim.get_fluidstate_variable(name='Rv')
|
||||
self.assertAlmostEqual(Rv[0], 0.0, places=7, msg='value of volatile gas-oil ratio')
|
||||
Sw = sim.get_fluidstate_variable(name='Sw')
|
||||
self.assertAlmostEqual(Sw[0], 0.11905577997, places=10, msg='value of water saturation')
|
||||
So = sim.get_fluidstate_variable(name='So')
|
||||
self.assertAlmostEqual(So[0], 0.56951652831, places=10, msg='value of oil saturation')
|
||||
Sg = sim.get_fluidstate_variable(name='Sg')
|
||||
self.assertAlmostEqual(Sg[0], 0.31142769170, places=10, msg='value of gas saturation')
|
||||
T = sim.get_fluidstate_variable(name='T')
|
||||
self.assertAlmostEqual(T[0], 288.705, places=3, msg='value of temperature')
|
||||
@@ -0,0 +1,43 @@
|
||||
import os
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from opm.simulators import BlackOilSimulator
|
||||
from .pytest_common import pushd
|
||||
|
||||
class TestBasic(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
# NOTE: See comment in test_basic.py for the reason why we are
|
||||
# only using a single test_all() function instead of splitting
|
||||
# it up in multiple test functions
|
||||
test_dir = Path(os.path.dirname(__file__))
|
||||
cls.data_dir = test_dir.parent.joinpath("test_data/SPE1CASE1a")
|
||||
|
||||
|
||||
def test_all(self):
|
||||
with pushd(self.data_dir):
|
||||
sim = BlackOilSimulator("SPE1CASE1.DATA")
|
||||
sim.step_init()
|
||||
sim.step()
|
||||
pressure = sim.get_primary_variable(variable='pressure')
|
||||
self.assertAlmostEqual(pressure[0], 41729978.837, places=2, msg='value of pressure')
|
||||
pressure_meaning = sim.get_primary_variable_meaning(
|
||||
variable='pressure')
|
||||
pressure_meaning_map = sim.get_primary_variable_meaning_map(
|
||||
variable='pressure')
|
||||
self.assertEqual(pressure_meaning[0], pressure_meaning_map["Po"])
|
||||
water_meaning = sim.get_primary_variable_meaning(
|
||||
variable='water')
|
||||
water_meaning_map = sim.get_primary_variable_meaning_map(
|
||||
variable='water')
|
||||
self.assertEqual(water_meaning[0], water_meaning_map["Sw"])
|
||||
gas_meaning = sim.get_primary_variable_meaning(
|
||||
variable='gas')
|
||||
gas_meaning_map = sim.get_primary_variable_meaning_map(
|
||||
variable='gas')
|
||||
self.assertEqual(gas_meaning[0], gas_meaning_map["Sg"])
|
||||
brine_meaning = sim.get_primary_variable_meaning(
|
||||
variable='brine')
|
||||
brine_meaning_map = sim.get_primary_variable_meaning_map(
|
||||
variable='brine')
|
||||
self.assertEqual(brine_meaning[0], brine_meaning_map["Disabled"])
|
||||
@@ -1,6 +1,5 @@
|
||||
import os
|
||||
import unittest
|
||||
from contextlib import contextmanager
|
||||
import datetime as dt
|
||||
from pathlib import Path
|
||||
import re
|
||||
|
||||
Reference in New Issue
Block a user