From a2c579a6bdb54838ea4f41f13873f8d29a7a787e Mon Sep 17 00:00:00 2001 From: Alif Be <11570927+alifbe@users.noreply.github.com> Date: Fri, 7 Jun 2024 06:53:56 +0000 Subject: [PATCH] Move run_simulator function from test_check_swatinit_simulators --- tests/test_check_swatinit_simulators.py | 33 ++-------------- tests/utils.py | 50 +++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 30 deletions(-) create mode 100644 tests/utils.py diff --git a/tests/test_check_swatinit_simulators.py b/tests/test_check_swatinit_simulators.py index 72fb2f158..4371907e6 100644 --- a/tests/test_check_swatinit_simulators.py +++ b/tests/test_check_swatinit_simulators.py @@ -9,8 +9,6 @@ """ import os -import subprocess -import time from pathlib import Path import numpy as np @@ -31,6 +29,8 @@ ) from subscript.check_swatinit.pillarmodel import PillarModel +from .utils import run_simulator + IN_SUBSCRIPT_GITHUB_ACTIONS = os.getenv("GITHUB_REPOSITORY") == "equinor/subscript" pd.set_option("display.max_columns", 100) @@ -58,35 +58,8 @@ def run_reservoir_simulator(simulator, resmodel, perform_qc=True): pd.DataFrame if perform_qc is True, else None """ Path("FOO.DATA").write_text(str(resmodel), encoding="utf8") - simulator_option = [] - if "runeclipse" in simulator: - simulator_option = ["-i"] - if "flow" in simulator: - simulator_option = ["--parsing-strictness=low"] - - result = subprocess.run( # pylint: disable=subprocess-run-check - [simulator] + simulator_option + ["FOO.DATA"], - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - ) - - if ( - result.returncode != 0 - and "runeclipse" in simulator - and "LICENSE FAILURE" in result.stdout.decode() + result.stderr.decode() - ): - print("Eclipse failed due to license server issues. Retrying in 30 seconds.") - time.sleep(30) - result = subprocess.run( # pylint: disable=subprocess-run-check - [simulator] + simulator_option + ["FOO.DATA"], - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - ) - if result.returncode != 0: - print(result.stdout.decode()) - print(result.stderr.decode()) - raise AssertionError(f"reservoir simulator failed in {os.getcwd()}") + run_simulator(simulator, "FOO.DATA") if perform_qc: return make_qc_gridframe(res2df.ResdataFiles("FOO.DATA")) diff --git a/tests/utils.py b/tests/utils.py new file mode 100644 index 000000000..d56fa6c57 --- /dev/null +++ b/tests/utils.py @@ -0,0 +1,50 @@ +import subprocess +import time +from os import getcwd + + +def run_simulator(simulator, data_file_path): + """Run the given simulator (Eclipse100 or OPM-flow) + on a DATA file + + Will write to cwd. Caller is responsible for starting + in a suitable directory. + + If the simulator fails, the stdout and stderr will be printed. + + Args: + simulator (string): Path to a working reservoir simulator + executable + data_file_path (str): Location of DATA file + Returns: + None + """ + simulator_option = [] + if "runeclipse" in simulator: + simulator_option = ["-i"] + if "flow" in simulator: + simulator_option = ["--parsing-strictness=low"] + + result = subprocess.run( # pylint: disable=subprocess-run-check + [simulator] + simulator_option + [data_file_path], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + ) + + if ( + result.returncode != 0 + and "runeclipse" in simulator + and "LICENSE FAILURE" in result.stdout.decode() + result.stderr.decode() + ): + print("Eclipse failed due to license server issues. Retrying in 30 seconds.") + time.sleep(30) + result = subprocess.run( # pylint: disable=subprocess-run-check + [simulator] + simulator_option + [data_file_path], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + ) + + if result.returncode != 0: + print(result.stdout.decode()) + print(result.stderr.decode()) + raise AssertionError(f"reservoir simulator failed in {getcwd()}")