Merge pull request #6482 from svenn-t/pysinglephase

Python one-phase (water) simulator
This commit is contained in:
Arne Morten Kvarving authored and GitHub committed 2025-09-23 15:24:02 +02:00
commit 0afc5f352a
14 files changed
+535 -6

No files matched your search

+5
View File
@@ -10,6 +10,11 @@
"class": "PyGasWaterSimulator",
"name": "GasWaterSimulator",
"doc": "The GasWaterSimulator class to run simulations using a given Deck."
},
"OnePhase": {
"class": "PyOnePhaseSimulator",
"name": "OnePhaseSimulator",
"doc": "The OnePhaseSimulator class to run simulations using a given Deck."
}
},
"constructors": {
+1
View File
@@ -11,3 +11,4 @@
#
from .BlackOil import BlackOilSimulator
from .GasWater import GasWaterSimulator
from .OnePhase import OnePhaseSimulator
+2 -1
View File
@@ -36,7 +36,8 @@ setup(
package_data={
'opm': [
'$<TARGET_FILE_NAME:BlackOil>',
'$<TARGET_FILE_NAME:GasWater>'
'$<TARGET_FILE_NAME:GasWater>',
'$<TARGET_FILE_NAME:OnePhase>',
]
},
include_package_data=True,
+17 -2
View File
@@ -4,6 +4,7 @@ set(PYTHON_OPM_SIMULATORS_PACKAGE_PATH ${PROJECT_BINARY_DIR}/python/opm/simulato
set(PYTHON_DOCSTRINGS_FILE "${PROJECT_SOURCE_DIR}/python/docstrings_simulators.json")
set(PYTHON_DOCSTRINGS_GENERATED_HPP "${PROJECT_BINARY_DIR}/python/PyBlackOilSimulatorDoc.hpp")
set(PYTHON_GW_DOCSTRINGS_GENERATED_HPP "${PROJECT_BINARY_DIR}/python/PyGasWaterSimulatorDoc.hpp")
set(PYTHON_OP_DOCSTRINGS_GENERATED_HPP "${PROJECT_BINARY_DIR}/python/PyOnePhaseSimulatorDoc.hpp")
# Note: If the new find_package(Python3) is used in the top level CMakeLists.txt, the variable
# ${PYTHON_EXECUTABLE} is set there to ${Python3_EXECUTABLE}
#
@@ -29,6 +30,14 @@ add_custom_command(
DEPENDS ${PYTHON_DOCSTRINGS_FILE}
COMMENT "Generating PyGasWaterSimulatorDoc.hpp from JSON file"
)
add_custom_command(
OUTPUT ${PYTHON_OP_DOCSTRINGS_GENERATED_HPP}
COMMAND ${CMAKE_COMMAND} -E env PYTHONPATH=${CMAKE_SOURCE_DIR}
${PYTHON_EXECUTABLE} ${PYTHON_GENERATE_DOCSTRINGS_PY}
${PYTHON_DOCSTRINGS_FILE} ${PYTHON_OP_DOCSTRINGS_GENERATED_HPP} PYONEPHASESIMULATORDOC_HPP "Opm::Pybind::DocStrings" "OnePhase"
DEPENDS ${PYTHON_DOCSTRINGS_FILE}
COMMENT "Generating PyOnePhaseSimulatorDoc.hpp from JSON file"
)
# 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
@@ -48,9 +57,15 @@ pybind11_add_module(GasWater ${PYBIND11_SYSTEM}
${PYTHON_GW_DOCSTRINGS_GENERATED_HPP} # Include the generated .hpp as a source file
)
pybind11_add_module(OnePhase ${PYBIND11_SYSTEM}
$<TARGET_OBJECTS:moduleVersion>
PyOnePhaseSimulator.cpp
${PYTHON_OP_DOCSTRINGS_GENERATED_HPP} # Include the generated .hpp as a source file
)
# Create a convenience target to build all Python simulator modules
add_custom_target(python_simulator_modules
DEPENDS BlackOil GasWater
DEPENDS BlackOil GasWater OnePhase
COMMENT "Building all Python simulator modules (BlackOil, GasWater, etc.)"
)
@@ -71,7 +86,7 @@ add_custom_target(copy_python ALL
${PROJECT_SOURCE_DIR}/python/test_data ${PROJECT_BINARY_DIR}/python 0
)
foreach(target_name IN ITEMS BlackOil GasWater)
foreach(target_name IN ITEMS BlackOil GasWater OnePhase)
set_target_properties(
${target_name} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${PYTHON_OPM_SIMULATORS_PACKAGE_PATH}
)
+135
View File
@@ -0,0 +1,135 @@
/*
Copyright 2025 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/models/blackoil/blackoilonephaseindices.hh>
#include <opm/simulators/flow/python/PyOnePhaseSimulator.hpp>
// NOTE: This file will be generated at compile time and placed in the build directory
// See python/generate_docstring_hpp.py, and python/simulators/CMakeLists.txt for details
#include <PyOnePhaseSimulatorDoc.hpp>
// NOTE: EXIT_SUCCESS, EXIT_FAILURE is defined in cstdlib
#include <cstdlib>
#include <stdexcept>
#include <string>
namespace Opm::Properties {
//! The indices required by the model
template<class TypeTag>
struct Indices<TypeTag, TTag::FlowOnePhaseProblem>
{
private:
// it is unfortunately not possible to simply use 'TypeTag' here because this leads
// to cyclic definitions of some properties. if this happens the compiler error
// messages unfortunately are *really* confusing and not really helpful.
using BaseTypeTag = TTag::FlowProblem;
using FluidSystem = GetPropType<BaseTypeTag, Properties::FluidSystem>;
public:
using type = BlackOilOnePhaseIndices<getPropValue<TypeTag, Properties::EnableSolvent>(),
getPropValue<TypeTag, Properties::EnableExtbo>(),
getPropValue<TypeTag, Properties::EnablePolymer>(),
getPropValue<TypeTag, Properties::EnableEnergy>(),
getPropValue<TypeTag, Properties::EnableFoam>(),
getPropValue<TypeTag, Properties::EnableBrine>(),
/*PVOffset=*/0,
/*enabledCompIdx=*/FluidSystem::waterCompIdx,
getPropValue<TypeTag, Properties::EnableBioeffects>()>;
}; // struct Indices
} // namespace Opm::Properties
// NOTE: We need the below explicit instantiations or else the symbols
// will not be available in the shared library and we will get
// undefined symbol errors when trying to import the module in Python.
namespace Opm::Pybind {
template class PyBaseSimulator<Opm::Properties::TTag::FlowOnePhaseProblem>;
} // namespace Opm::Pybind
// Define main function
namespace Opm {
template class PyMain<Opm::Properties::TTag::FlowOnePhaseProblem>;
template std::unique_ptr<FlowMain<Opm::Properties::TTag::FlowOnePhaseProblem>>
flowMainInit<Opm::Properties::TTag::FlowOnePhaseProblem>(
int argc, char** argv, bool outputCout, bool outputFiles);
} // namespace Opm
namespace py = pybind11;
namespace Opm::Pybind {
// Exported functions
void export_PyOnePhaseSimulator(py::module& m)
{
using namespace Opm::Pybind::DocStrings;
using TypeTag = Opm::Properties::TTag::FlowOnePhaseProblem;
py::class_<PyBaseSimulator<TypeTag>>(
m,
"_BaseSimulatorOP",
py::module_local()
);
py::class_<PyOnePhaseSimulator, PyBaseSimulator<TypeTag> >(m, "OnePhaseSimulator")
.def(py::init<const std::string&,
const std::vector<std::string>&>(),
PyOnePhaseSimulator_filename_constructor_docstring,
py::arg("filename"), py::arg("args") = std::vector<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>,
const std::vector<std::string>&>(),
PyOnePhaseSimulator_objects_constructor_docstring,
py::arg("Deck"), py::arg("EclipseState"), py::arg("Schedule"), py::arg("SummaryConfig"),
py::arg("args") = std::vector<std::string>{})
.def("advance", &PyBaseSimulator<TypeTag>::advance, advance_docstring, py::arg("report_step"))
.def("check_simulation_finished", &PyBaseSimulator<TypeTag>::checkSimulationFinished,
checkSimulationFinished_docstring)
.def("current_step", &PyBaseSimulator<TypeTag>::currentStep, currentStep_docstring)
.def("get_cell_volumes", &PyBaseSimulator<TypeTag>::getCellVolumes, getCellVolumes_docstring)
.def("get_dt", &PyBaseSimulator<TypeTag>::getDT, getDT_docstring)
.def("get_fluidstate_variable", &PyBaseSimulator<TypeTag>::getFluidStateVariable,
py::return_value_policy::copy, getFluidStateVariable_docstring, py::arg("name"))
.def("get_porosity", &PyBaseSimulator<TypeTag>::getPorosity, getPorosity_docstring)
.def("get_primary_variable_meaning", &PyBaseSimulator<TypeTag>::getPrimaryVarMeaning,
py::return_value_policy::copy, getPrimaryVarMeaning_docstring, py::arg("variable"))
.def("get_primary_variable_meaning_map", &PyBaseSimulator<TypeTag>::getPrimaryVarMeaningMap,
py::return_value_policy::copy, getPrimaryVarMeaningMap_docstring, py::arg("variable"))
.def("get_primary_variable", &PyBaseSimulator<TypeTag>::getPrimaryVariable,
py::return_value_policy::copy, getPrimaryVariable_docstring, py::arg("variable"))
.def("run", &PyBaseSimulator<TypeTag>::run, run_docstring)
.def("set_porosity", &PyBaseSimulator<TypeTag>::setPorosity, setPorosity_docstring, py::arg("array"))
.def("set_primary_variable", &PyBaseSimulator<TypeTag>::setPrimaryVariable,
py::arg("variable"), setPrimaryVariable_docstring, py::arg("value"))
.def("setup_mpi", &PyBaseSimulator<TypeTag>::setupMpi, setupMpi_docstring, py::arg("init"), py::arg("finalize"))
.def("step", &PyBaseSimulator<TypeTag>::step, step_docstring)
.def("step_cleanup", &PyBaseSimulator<TypeTag>::stepCleanup, stepCleanup_docstring)
.def("step_init", &PyBaseSimulator<TypeTag>::stepInit, stepInit_docstring);
}
PYBIND11_MODULE(OnePhase, m)
{
export_PyOnePhaseSimulator(m);
}
} // namespace Opm::Pybind
+16
View File
@@ -61,3 +61,19 @@ def create_gas_water_simulator(*args, **kwargs):
return GasWaterSimulator(*args, **kwargs)
def create_onephase_simulator(*args, **kwargs):
"""Create OnePhaseSimulator with test-safe default arguments.
Automatically disables async ECL output to prevent race conditions
with pushd context manager in tests.
"""
from opm.simulators import OnePhaseSimulator
flag_to_add = ENABLE_ASYNC_ECL_OUTPUT_FLAG
# Add our flag to args
kwargs['args'] = kwargs.get('args', [])
if flag_to_add not in kwargs['args']:
kwargs['args'].append(flag_to_add)
return OnePhaseSimulator(*args, **kwargs)
+22 -1
View File
@@ -1,13 +1,14 @@
import os
import unittest
from pathlib import Path
from .pytest_common import pushd, create_black_oil_simulator, create_gas_water_simulator
from .pytest_common import pushd, create_black_oil_simulator, create_gas_water_simulator, create_onephase_simulator
class TestBasic(unittest.TestCase):
@classmethod
def setUpClass(cls):
test_dir = Path(os.path.dirname(__file__))
cls.data_dir_bo = test_dir.parent.joinpath("test_data/SPE1CASE1a")
cls.data_dir_op = test_dir.parent.joinpath("test_data/SPE1CASE1")
cls.data_dir_gw = test_dir.parent.joinpath("test_data/SPE1CASE2")
# IMPORTANT: Since all the python unittests run in the same process we must be
@@ -43,6 +44,26 @@ class TestBasic(unittest.TestCase):
sim.step()
poro2 = sim.get_porosity()
self.assertAlmostEqual(poro2[0], 0.285, places=7, msg='value of porosity 2')
def test_02_onephase(self):
with pushd(self.data_dir_op):
sim = create_onephase_simulator(args=['--linear-solver=ilu0'], filename="SPE1CASE1_WATER.DATA")
sim.setup_mpi(init=False, finalize=False)
sim.step_init()
sim.step()
dt = sim.get_dt() # 31 days = 31 * 24 * 60 * 60 = 2678400 seconds
self.assertAlmostEqual(dt, 2678400., places=7, msg='value of timestep')
vol = sim.get_cell_volumes()
self.assertEqual(len(vol), 300, 'length of volume vector')
self.assertAlmostEqual(vol[0], 566336.93, places=2, msg='value of volume')
poro = sim.get_porosity()
self.assertEqual(len(poro), 300, 'length of porosity vector')
self.assertAlmostEqual(poro[0], 0.3, places=7, msg='value of porosity')
poro = poro *.95
sim.set_porosity(poro)
sim.step()
poro2 = sim.get_porosity()
self.assertAlmostEqual(poro2[0], 0.285, places=7, msg='value of porosity 2')
# IMPORTANT: This test must be run last since it calls MPI_Finalize()
def test_99_gaswater(self):
+15 -1
View File
@@ -1,13 +1,14 @@
import os
import unittest
from pathlib import Path
from .pytest_common import pushd, create_black_oil_simulator, create_gas_water_simulator
from .pytest_common import pushd, create_black_oil_simulator, create_gas_water_simulator, create_onephase_simulator
class TestBasic(unittest.TestCase):
@classmethod
def setUpClass(cls):
test_dir = Path(os.path.dirname(__file__))
cls.data_dir_bo = test_dir.parent.joinpath("test_data/SPE1CASE1a")
cls.data_dir_op = test_dir.parent.joinpath("test_data/SPE1CASE1")
cls.data_dir_gw = test_dir.parent.joinpath("test_data/SPE1CASE2")
# IMPORTANT: Since all the python unittests run in the same process we must be
@@ -49,6 +50,19 @@ class TestBasic(unittest.TestCase):
T = sim.get_fluidstate_variable(name='T')
self.assertAlmostEqual(T[0], 288.705, places=3, msg='value of temperature')
def test_02_onephase(self):
with pushd(self.data_dir_op):
sim = create_onephase_simulator("SPE1CASE1_WATER.DATA")
sim.setup_mpi(False, False)
sim.step_init()
sim.step()
water_pressure = sim.get_fluidstate_variable(name='pw')
self.assertAlmostEqual(water_pressure[0], 44780102.277570, delta=1e4, msg='value of water pressure')
rho_w = sim.get_fluidstate_variable(name='rho_w')
self.assertAlmostEqual(rho_w[0], 1003.182858, places=3, msg='value of water density')
Sw = sim.get_fluidstate_variable(name='Sw')
self.assertAlmostEqual(Sw[0], 1.0, places=5, msg='value of water saturation')
# IMPORTANT: This test must be run last since it calls MPI_Finalize()
def test_99_gaswater(self):
with pushd(self.data_dir_gw):
+26 -1
View File
@@ -1,7 +1,7 @@
import os
import unittest
from pathlib import Path
from .pytest_common import pushd, create_black_oil_simulator, create_gas_water_simulator
from .pytest_common import pushd, create_black_oil_simulator, create_gas_water_simulator, create_onephase_simulator
class TestBasic(unittest.TestCase):
@classmethod
@@ -11,6 +11,7 @@ class TestBasic(unittest.TestCase):
# it up in multiple test functions
test_dir = Path(os.path.dirname(__file__))
cls.data_dir_bo = test_dir.parent.joinpath("test_data/SPE1CASE1a")
cls.data_dir_op = test_dir.parent.joinpath("test_data/SPE1CASE1")
cls.data_dir_gw = test_dir.parent.joinpath("test_data/SPE1CASE2")
# IMPORTANT: Since all the python unittests run in the same process we must be
@@ -50,6 +51,30 @@ class TestBasic(unittest.TestCase):
variable='brine')
self.assertEqual(brine_meaning[0], brine_meaning_map["Disabled"])
def test_02_onephase(self):
with pushd(self.data_dir_op):
sim = create_onephase_simulator("SPE1CASE1_WATER.DATA")
sim.setup_mpi(False, False)
sim.step_init()
sim.step()
pressure = sim.get_primary_variable(variable='pressure')
self.assertAlmostEqual(pressure[0], 44780102.277570, delta=1e4, 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["Pw"])
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["Disabled"])
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"])
# IMPORTANT: This test must be run last since it calls MPI_Finalize()
def test_99_gaswater(self):
with pushd(self.data_dir_gw):
@@ -0,0 +1,203 @@
-- This reservoir simulation deck is made available under the Open Database
-- License: http://opendatacommons.org/licenses/odbl/1.0/. Any rights in
-- individual contents of the database are licensed under the Database Contents
-- License: http://opendatacommons.org/licenses/dbcl/1.0/
-- Copyright (C) 2019 SINTEF
-- Copyright (C) 2020 Equinor
-- This simulation deck is for flow_onephase
-- to simulate water injection/production
-- in a water single-phase system
---------------------------------------------------------------------------
------------------------ SPE1 - CASE 1 ------------------------------------
---------------------------------------------------------------------------
RUNSPEC
-- -------------------------------------------------------------------------
TITLE
SPE1 - CASE 1
DIMENS
10 10 3 /
-- The number of equilibration regions is inferred from the EQLDIMS
-- keyword.
EQLDIMS
/
-- The number of PVTW tables is inferred from the TABDIMS keyword;
-- when no data is included in the keyword the default values are used.
TABDIMS
/
WATER
FIELD
START
1 'JAN' 2015 /
WELLDIMS
-- Item 1: maximum number of wells in the model
-- - there are two wells in the problem; injector and producer
-- Item 2: maximum number of grid blocks connected to any one well
-- - must be one as the wells are located at specific grid blocks
-- Item 3: maximum number of groups in the model
-- - we are dealing with only one 'group'
-- Item 4: maximum number of wells in any one group
-- - there must be two wells in a group as there are two wells in total
2 1 1 2 /
UNIFIN
UNIFOUT
GRID
-- The INIT keyword is used to request an .INIT file. The .INIT file
-- is written before the simulation actually starts, and contains grid
-- properties and saturation tables as inferred from the input
-- deck. There are no other keywords which can be used to configure
-- exactly what is written to the .INIT file.
INIT
-- -------------------------------------------------------------------------
NOECHO
DX
-- There are in total 300 cells with length 1000ft in x-direction
300*1000 /
DY
-- There are in total 300 cells with length 1000ft in y-direction
300*1000 /
DZ
-- The layers are 20, 30 and 50 ft thick, in each layer there are 100 cells
100*20 100*30 100*50 /
TOPS
-- The depth of the top of each grid block
100*8325 /
PORO
-- Constant porosity of 0.3 throughout all 300 grid cells
300*0.3 /
PERMX
-- The layers have perm. 500mD, 50mD and 200mD, respectively.
100*500 100*50 100*200 /
PERMY
-- Equal to PERMX
100*500 100*50 100*200 /
PERMZ
100*500 100*50 100*200 /
ECHO
PROPS
-- -------------------------------------------------------------------------
PVTW
-- Item 1: pressure reference (psia)
-- Item 2: water FVF (rb per bbl or rb per stb)
-- Item 3: water compressibility (psi^{-1})
-- Item 4: water viscosity (cp)
-- Item 5: water 'viscosibility' (psi^{-1})
-- Using values from Norne:
-- In METRIC units:
-- 277.0 1.038 4.67E-5 0.318 0.0 /
-- In FIELD units:
4017.55 1.038 3.22E-6 0.318 0.0 /
ROCK
-- Item 1: reference pressure (psia)
-- Item 2: rock compressibility (psi^{-1})
-- Using values from table 1 in Odeh:
14.7 3E-6 /
DENSITY
-- Density (lb per ft³) at surface cond. of
-- oil, water and gas, respectively (in that order)
-- Using values from Norne:
-- In METRIC units:
-- 859.5 1033.0 0.854 /
-- In FIELD units:
53.66 64.49 0.0533 /
SOLUTION
-- -------------------------------------------------------------------------
PRESSURE
300*4800
/
SUMMARY
-- -------------------------------------------------------------------------
WBHP
'INJ'
'PROD'
/
WWIR
'INJ'
/
WWIT
'INJ'
/
WWPR
'PROD'
/
WWPT
'PROD'
/
SCHEDULE
-- -------------------------------------------------------------------------
RPTSCHED
'PRES' /
RPTRST
'BASIC=1' /
WELSPECS
-- Item #: 1 2 3 4 5 6
'PROD' 'G1' 10 10 8400 'WATER' /
'INJ' 'G1' 1 1 8335 'WATER' /
/
-- Coordinates in item 3-4 are retrieved from Odeh's figure 1 and 2
-- Note that the depth at the midpoint of the well grid blocks
-- has been used as reference depth for bottom hole pressure in item 5
COMPDAT
-- Item #: 1 2 3 4 5 6 7 8 9
'PROD' 10 10 3 3 'OPEN' 1* 1* 0.5 /
'INJ' 1 1 1 1 'OPEN' 1* 1* 0.5 /
/
-- Coordinates in item 2-5 are retreived from Odeh's figure 1 and 2
-- Item 9 is the well bore internal diameter,
-- the radius is given to be 0.25ft in Odeh's paper
WCONPROD
-- Item #:1 2 3 4 5 9
'PROD' 'OPEN' 'BHP' 1* 1* 1* 1* 1* 1000 /
/
WCONINJE
-- Item #:1 2 3 4 5 6 7
'INJ' 'WATER' 'OPEN' 'RATE' 100000 1* 9014 /
/
-- Stated in Odeh that gas inj. rate (item 5) is 100MMscf per day
-- BHP upper limit (item 7) should not be exceeding the highest
-- pressure in the PVT table=9014.7psia (default is 100 000psia)
TSTEP
--Advance the simulater once a month for ONE years:
31 28 31 30 31 30 31 31 30 31 30 31 /
END