From 6a3c38721f1352bf96f193cf794016065b578140 Mon Sep 17 00:00:00 2001 From: Arne Morten Kvarving Date: Wed, 8 Oct 2025 14:37:01 +0200 Subject: [PATCH 1/5] fix import path for kerasify --- python/test/ml/hybrid_newton/test_hybrid_newton.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/python/test/ml/hybrid_newton/test_hybrid_newton.py b/python/test/ml/hybrid_newton/test_hybrid_newton.py index e032683f05c..bb09cd55e36 100644 --- a/python/test/ml/hybrid_newton/test_hybrid_newton.py +++ b/python/test/ml/hybrid_newton/test_hybrid_newton.py @@ -8,8 +8,7 @@ from .utils import collect_input_features, compute_output_vars, write_config, extract_newtit_from_file, extract_unrst_variables from .utils import (ABSOLUTE_CASES, RELATIVE_CASES, FEATURE_ENGINEERING_CASES, SCALING_CASES, MULTI_MODEL_CASES, ZERO_NEWTON_CASES, ALL_CASES) -from kerasify import export_model - +from opm.ml.ml_tools.kerasify import export_model class TestHybridNewton(unittest.TestCase): @classmethod From 7b0aeb1a4b13f97f0e00d84f1b9ec8be537ce5ce Mon Sep 17 00:00:00 2001 From: Arne Morten Kvarving Date: Wed, 8 Oct 2025 14:40:12 +0200 Subject: [PATCH 2/5] (optionally) read path to flow binary from FLOW_BINARY environment variable --- python/test/ml/hybrid_newton/test_hybrid_newton.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/test/ml/hybrid_newton/test_hybrid_newton.py b/python/test/ml/hybrid_newton/test_hybrid_newton.py index bb09cd55e36..b741f70aa5b 100644 --- a/python/test/ml/hybrid_newton/test_hybrid_newton.py +++ b/python/test/ml/hybrid_newton/test_hybrid_newton.py @@ -23,7 +23,7 @@ def setUpClass(cls): cls.times = [] baseline_cmd = [ - "flow", + os.environ.get("FLOW_BINARY", default="flow"), str(cls.data_dir / cls.deck_file), "--output-extra-convergence-info=steps,iterations", "--newton-min-iterations=0", @@ -124,7 +124,7 @@ def _build_case_models(self, case, models_dir): def _run_hybrid_newton_test(self, json_path): """Run hybrid Newton via subprocess and extract Newton iterations.""" cmd = [ - "flow", + os.environ.get("FLOW_BINARY", default="flow"), str(self.data_dir / self.deck_file), f"--hy-ne-config-file={json_path}", "--use-hy-ne=true", From 66b037d9a78c840d3102564d23c6da3d69e5c13e Mon Sep 17 00:00:00 2001 From: Arne Morten Kvarving Date: Wed, 8 Oct 2025 14:40:38 +0200 Subject: [PATCH 3/5] write output to cwd --- .../test/ml/hybrid_newton/test_hybrid_newton.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/python/test/ml/hybrid_newton/test_hybrid_newton.py b/python/test/ml/hybrid_newton/test_hybrid_newton.py index b741f70aa5b..a4db6f64032 100644 --- a/python/test/ml/hybrid_newton/test_hybrid_newton.py +++ b/python/test/ml/hybrid_newton/test_hybrid_newton.py @@ -27,23 +27,24 @@ def setUpClass(cls): str(cls.data_dir / cls.deck_file), "--output-extra-convergence-info=steps,iterations", "--newton-min-iterations=0", + "--output-dir=.", "--full-time-step-initially=true", ] - print("Running baseline simulation...") - subprocess.run(baseline_cmd, cwd=cls.data_dir, stdout=subprocess.DEVNULL, check=True) + print("Running baseline simulation... ") + subprocess.run(baseline_cmd, stdout=subprocess.DEVNULL, check=True) # Extract Newton iterations - cls.baseline_iters = extract_newtit_from_file(cls.data_dir / Path(cls.deck_file).stem) + cls.baseline_iters = extract_newtit_from_file(Path('.') / Path(cls.deck_file).stem) print(f"Baseline Newton iterations: {cls.baseline_iters}") # Extract fluid state from UNRST for all timesteps - cls.unrst, cls.times = extract_unrst_variables(cls.data_dir, cls.deck_file) + cls.unrst, cls.times = extract_unrst_variables(".", cls.deck_file) cls.rd_init["PERMX"] = [10**2] * len(cls.unrst["PRESSURE"][0]) cls.n_cells = len(cls.unrst["PRESSURE"][0]) def _run_cases(self, cases): - models_dir = self.data_dir / "models" + models_dir = Path('.') / "models" os.makedirs(models_dir, exist_ok=True) for case in cases: @@ -130,15 +131,16 @@ def _run_hybrid_newton_test(self, json_path): "--use-hy-ne=true", "--output-extra-convergence-info=steps,iterations", "--newton-min-iterations=0", + "--output-dir=.", "--full-time-step-initially=true" ] print(f"Running hybrid Newton simulation with config {json_path}...") - subprocess.run(cmd, cwd=self.data_dir, + subprocess.run(cmd, stdout=subprocess.DEVNULL, check=True) - hybrid_iters = extract_newtit_from_file(self.data_dir / Path(self.deck_file).stem) + hybrid_iters = extract_newtit_from_file(Path('.') / Path(self.deck_file).stem) return hybrid_iters def test_absolute_cases(self): From 3c365a428acbc550d3763d0a824d6b47aa23607d Mon Sep 17 00:00:00 2001 From: Arne Morten Kvarving Date: Wed, 8 Oct 2025 14:41:58 +0200 Subject: [PATCH 4/5] move hybrid newton test to an integration test directory this is not a unit test for the python binaries --- python/{test => integration_tests}/ml/README.md | 0 python/{test => integration_tests}/ml/__init__.py | 0 python/{test => integration_tests}/ml/hybrid_newton/README.md | 0 python/{test => integration_tests}/ml/hybrid_newton/__init__.py | 0 .../ml/hybrid_newton/test_hybrid_newton.py | 0 python/{test => integration_tests}/ml/hybrid_newton/utils.py | 0 6 files changed, 0 insertions(+), 0 deletions(-) rename python/{test => integration_tests}/ml/README.md (100%) rename python/{test => integration_tests}/ml/__init__.py (100%) rename python/{test => integration_tests}/ml/hybrid_newton/README.md (100%) rename python/{test => integration_tests}/ml/hybrid_newton/__init__.py (100%) rename python/{test => integration_tests}/ml/hybrid_newton/test_hybrid_newton.py (100%) rename python/{test => integration_tests}/ml/hybrid_newton/utils.py (100%) diff --git a/python/test/ml/README.md b/python/integration_tests/ml/README.md similarity index 100% rename from python/test/ml/README.md rename to python/integration_tests/ml/README.md diff --git a/python/test/ml/__init__.py b/python/integration_tests/ml/__init__.py similarity index 100% rename from python/test/ml/__init__.py rename to python/integration_tests/ml/__init__.py diff --git a/python/test/ml/hybrid_newton/README.md b/python/integration_tests/ml/hybrid_newton/README.md similarity index 100% rename from python/test/ml/hybrid_newton/README.md rename to python/integration_tests/ml/hybrid_newton/README.md diff --git a/python/test/ml/hybrid_newton/__init__.py b/python/integration_tests/ml/hybrid_newton/__init__.py similarity index 100% rename from python/test/ml/hybrid_newton/__init__.py rename to python/integration_tests/ml/hybrid_newton/__init__.py diff --git a/python/test/ml/hybrid_newton/test_hybrid_newton.py b/python/integration_tests/ml/hybrid_newton/test_hybrid_newton.py similarity index 100% rename from python/test/ml/hybrid_newton/test_hybrid_newton.py rename to python/integration_tests/ml/hybrid_newton/test_hybrid_newton.py diff --git a/python/test/ml/hybrid_newton/utils.py b/python/integration_tests/ml/hybrid_newton/utils.py similarity index 100% rename from python/test/ml/hybrid_newton/utils.py rename to python/integration_tests/ml/hybrid_newton/utils.py From dc0cf9589311d3d0912e47b446253bbbecbc475c Mon Sep 17 00:00:00 2001 From: Arne Morten Kvarving Date: Wed, 8 Oct 2025 14:44:42 +0200 Subject: [PATCH 5/5] move HybridNewton to a python integration test --- CMakeLists.txt | 4 ++++ pythonIntegrationTests.cmake | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 pythonIntegrationTests.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index ef08ba51900..b2e015b12d2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -525,6 +525,10 @@ if(MPI_FOUND) include (${CMAKE_CURRENT_SOURCE_DIR}/parallelUnitTests.cmake) endif() +if(OPM_ENABLE_PYTHON) + include (${CMAKE_CURRENT_SOURCE_DIR}/pythonIntegrationTests.cmake) +endif() + include(OpmBashCompletion) if (NOT BUILD_FLOW) diff --git a/pythonIntegrationTests.cmake b/pythonIntegrationTests.cmake new file mode 100644 index 00000000000..40d259e2e8a --- /dev/null +++ b/pythonIntegrationTests.cmake @@ -0,0 +1,20 @@ +file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/tests/integration/HybridNewton) + +add_test(NAME + HybridNewton + COMMAND + pytest ${PROJECT_SOURCE_DIR}/python/integration_tests/ml/hybrid_newton + WORKING_DIRECTORY + ${PROJECT_BINARY_DIR}/tests/integration/HybridNewton + ) +if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.22) + set(modifications + "PYTHONPATH=path_list_append:${opm-common_DIR}/python" + "PYTHONPATH=path_list_append:${CMAKE_CURRENT_BINARY_DIR}/python" + "FLOW_BINARY=set:$" + ) + set_tests_properties(HybridNewton PROPERTIES + ENVIRONMENT_MODIFICATION + "${modifications}" + ) +endif()