Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace interface towards eclrun and flow using fmstep_config #9108

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/ert/config/ert_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,17 @@ def apply_config_content_defaults(content_dict: dict, config_dir: str):
path.join(config_dir, content_dict[ConfigKeys.RUNPATH_FILE])
)

@staticmethod
def _uppercase_subkeys_and_stringify_subvalues(
nested_dict: Dict[str, Dict[str, Any]],
) -> Dict[str, Dict[str, str]]:
fixed_dict: dict[str, dict[str, str]] = {}
for key, value in nested_dict.items():
fixed_dict[key] = {
subkey.upper(): str(subvalue) for subkey, subvalue in value.items()
}
return fixed_dict

@classmethod
def read_site_config(cls) -> ConfigDict:
site_config_file = site_config_location()
Expand Down Expand Up @@ -748,6 +759,9 @@ def handle_default(fm_step: ForwardModelStep, arg: str) -> str:

job_list_errors = []
job_list: List[ForwardModelStepJSON] = []
env_pr_fm_step = cls._uppercase_subkeys_and_stringify_subvalues(
ErtPluginManager().get_forward_model_configuration()
)
for idx, fm_step in enumerate(forward_model_steps):
substituter = Substituter(fm_step)
fm_step_json = {
Expand All @@ -771,7 +785,9 @@ def handle_default(fm_step: ForwardModelStep, arg: str) -> str:
handle_default(fm_step, substituter.substitute(arg))
for arg in fm_step.arglist
],
"environment": substituter.filter_env_dict(fm_step.environment),
"environment": substituter.filter_env_dict(
dict(env_pr_fm_step.get(fm_step.name, {}), **fm_step.environment)
),
"exec_env": substituter.filter_env_dict(fm_step.exec_env),
"max_running_minutes": fm_step.max_running_minutes,
}
Expand Down
1 change: 1 addition & 0 deletions src/ert/plugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def inner(*args: P.args, **kwargs: P.kwargs) -> Any:
"installable_workflow_jobs",
"help_links",
"installable_forward_model_steps",
"forward_model_configuration",
"ecl100_config_path",
"ecl300_config_path",
"flow_config_path",
Expand Down
51 changes: 20 additions & 31 deletions src/ert/plugins/hook_implementations/forward_model_steps.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import os
import shutil
import subprocess
from pathlib import Path
from textwrap import dedent
from typing import List, Literal, Optional, Type

import yaml

from ert import (
ForwardModelStepDocumentation,
ForwardModelStepJSON,
Expand Down Expand Up @@ -207,12 +204,12 @@ def __init__(self) -> None:
str(
(
Path(__file__)
/ "../../../resources/forward_models/res/script/ecl100.py"
/ "../../../resources/forward_models/run_reservoirsimulator.py"
).resolve()
),
"<ECLBASE>",
"-v",
"eclipse",
"<VERSION>",
"<ECLBASE>",
"-n",
"<NUM_CPU>",
"<OPTS>",
Expand Down Expand Up @@ -265,12 +262,12 @@ def __init__(self) -> None:
str(
(
Path(__file__)
/ "../../../resources/forward_models/res/script/ecl300.py"
/ "../../../resources/forward_models/run_reservoirsimulator.py"
).resolve()
),
"<ECLBASE>",
"-v",
"e300",
"<VERSION>",
"<ECLBASE>",
"-n",
"<NUM_CPU>",
"<OPTS>",
Expand Down Expand Up @@ -317,11 +314,11 @@ def __init__(self) -> None:
str(
(
Path(__file__)
/ "../../../resources/forward_models/res/script/flow.py"
/ "../../../resources/forward_models/run_reservoirsimulator.py"
).resolve()
),
"flow",
"<ECLBASE>",
"-v",
"<VERSION>",
"-n",
"<NUM_CPU>",
Expand Down Expand Up @@ -629,31 +626,23 @@ def installable_forward_model_steps() -> List[Type[ForwardModelStepPlugin]]:


def _available_eclrun_versions(simulator: Literal["eclipse", "e300"]) -> List[str]:
if shutil.which("eclrun") is None:
return []
pm = ErtPluginManager()
ecl_config_path = (
pm.get_ecl100_config_path()
if simulator == "eclipse"
else pm.get_ecl300_config_path()
eclrun_env = os.environ.copy()
_simulator_to_fm_stepname = {"eclipse": "ECLIPSE100", "e300": "ECLIPSE300"}
eclrun_path = (
pm.get_forward_model_configuration()
.get(_simulator_to_fm_stepname[simulator], {})
.get("ECLRUN_PATH", "")
)
eclrun_env["PATH"] = os.pathsep.join(
[eclrun_path, *os.getenv("PATH", "").split(os.pathsep)]
)

if not ecl_config_path:
return []
eclrun_env = {"PATH": os.getenv("PATH", "")}

with open(ecl_config_path, encoding="utf-8") as f:
try:
config = yaml.safe_load(f)
except yaml.YAMLError as e:
raise ValueError(f"Failed parse: {ecl_config_path} as yaml") from e
ecl_install_path = config.get("eclrun_env", {}).get("PATH", "")
eclrun_env["PATH"] = eclrun_env["PATH"] + os.pathsep + ecl_install_path

try:
return (
subprocess.check_output(
["eclrun", "--report-versions", simulator],
# It is not sufficient to just give the correct path to eclrun, its PATH must include its location
# for versions to be displayed. Ensure this is covered in tests.
["eclrun", simulator, "--report-versions"],
env=eclrun_env,
)
.decode("utf-8")
Expand Down
2 changes: 2 additions & 0 deletions src/ert/plugins/hook_specifications/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
flow_config_path,
)
from .forward_model_steps import (
forward_model_configuration,
installable_forward_model_steps,
)
from .help_resources import help_links
Expand All @@ -21,6 +22,7 @@
"ecl100_config_path",
"ecl300_config_path",
"flow_config_path",
"forward_model_configuration",
"help_links",
"installable_forward_model_steps",
"installable_jobs",
Expand Down
8 changes: 8 additions & 0 deletions src/ert/plugins/hook_specifications/forward_model_steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,11 @@ def installable_forward_model_steps() -> (
:return: List of forward model step plugins in the form of subclasses of the
ForwardModelStepPlugin class
"""


@no_type_check
@hook_specification
def forward_model_configuration() -> PluginResponse[List[Type[ForwardModelStepPlugin]]]:
"""
:return: List of configurations to be merged to be provided to forward model steps.
"""
45 changes: 45 additions & 0 deletions src/ert/plugins/plugin_manager.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import collections
import logging
import os
import shutil
Expand Down Expand Up @@ -139,6 +140,50 @@ def get_flow_config_path(self) -> Optional[str]:
hook=self.hook.flow_config_path, config_name="flow"
)

def get_forward_model_configuration(self) -> Dict[str, Dict[str, Any]]:
response: List[PluginResponse[Dict[str, str]]] = (
self.hook.forward_model_configuration()
)
if response == []:
return {}

fm_configs: Dict[str, Dict[str, Any]] = collections.defaultdict(dict)
for res in response:
if not isinstance(res.data, dict):
raise TypeError(
f"{res.plugin_metadata.plugin_name} did not provide "
"dict[str, dict]"
)

for fmstep_name, fmstep_config in res.data.items():
if not isinstance(fmstep_name, str):
raise TypeError(
f"{res.plugin_metadata.plugin_name} did not "
"provide dict[str, dict[str, Any]]"
)
if not isinstance(fmstep_config, dict):
raise TypeError(
f"{res.plugin_metadata.plugin_name} did not "
"provide dict[str, dict[str, Any]]"
)
for key, value in fmstep_config.items():
if not isinstance(key, str):
raise TypeError(
f"{res.plugin_metadata.plugin_name} did not "
"provide dict[str, dict[str, Any]]"
)
if key.lower() in [
existing.lower() for existing in fm_configs[fmstep_name]
]:
raise RuntimeError(
"Duplicate configuration or fm_step "
f"{fmstep_name} for key {key} when parsing plugin "
f"{res.plugin_metadata.plugin_name}, it is already "
"registered by another plugin."
)
fm_configs[fmstep_name][key] = value
return fm_configs

def _site_config_lines(self) -> List[str]:
try:
plugin_responses = self.hook.site_config_lines()
Expand Down
9 changes: 0 additions & 9 deletions src/ert/resources/forward_models/res/script/ecl100.py

This file was deleted.

14 changes: 0 additions & 14 deletions src/ert/resources/forward_models/res/script/ecl100_config.yml

This file was deleted.

9 changes: 0 additions & 9 deletions src/ert/resources/forward_models/res/script/ecl300.py

This file was deleted.

8 changes: 0 additions & 8 deletions src/ert/resources/forward_models/res/script/ecl300_config.yml

This file was deleted.

Loading
Loading