Skip to content

Commit

Permalink
Remove ErtConfig from EverestRunModel
Browse files Browse the repository at this point in the history
  • Loading branch information
frode-aarstad committed Jan 24, 2025
1 parent 2e7fba6 commit 851bcae
Show file tree
Hide file tree
Showing 15 changed files with 467 additions and 325 deletions.
621 changes: 317 additions & 304 deletions src/ert/config/ert_config.py

Large diffs are not rendered by default.

149 changes: 128 additions & 21 deletions src/ert/run_models/everest_run_model.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

import collections
import copy
import datetime
import functools
import json
Expand Down Expand Up @@ -27,14 +29,32 @@
from typing_extensions import TypedDict

from _ert.events import EESnapshot, EESnapshotUpdate, Event
from ert.config import ErtConfig, ExtParamConfig
from ert.config import ExtParamConfig
from ert.config.ensemble_config import EnsembleConfig
from ert.config.ert_config import (
ErtConfig,
_substitutions_from_dict,
create_list_of_forward_model_steps_to_run,
installed_forward_model_steps_from_dict,
read_templates,
uppercase_subkeys_and_stringify_subvalues,
workflows_from_dict,
)
from ert.config.forward_model_step import ForwardModelStep, ForwardModelStepPlugin
from ert.config.model_config import ModelConfig
from ert.config.queue_config import QueueConfig
from ert.ensemble_evaluator import EnsembleSnapshot, EvaluatorServerConfig
from ert.plugins.plugin_manager import ErtPluginManager
from ert.runpaths import Runpaths
from ert.storage import open_storage
from everest.config import ControlConfig, ControlVariableGuessListConfig, EverestConfig
from everest.config.control_variable_config import ControlVariableConfig
from everest.optimizer.everest2ropt import everest2ropt
from everest.simulator.everest_to_ert import everest_to_ert_config
from everest.strings import EVEREST
from everest.simulator.everest_to_ert import (
everest_to_ert_config,
everest_to_ert_config_dict,
)
from everest.strings import EVEREST, STORAGE_DIR

from ..run_arg import RunArg, create_run_arguments
from .base_run_model import BaseRunModel, StatusEvents
Expand Down Expand Up @@ -104,6 +124,9 @@ def __init__(
simulation_callback: SimulationCallback | None,
optimization_callback: OptimizerCallback | None,
):
assert everest_config.log_dir is not None
assert everest_config.optimization_output_dir is not None

Path(everest_config.log_dir).mkdir(parents=True, exist_ok=True)
Path(everest_config.optimization_output_dir).mkdir(parents=True, exist_ok=True)

Expand Down Expand Up @@ -135,39 +158,122 @@ def __init__(
self._batch_id: int = 0
self._status: SimulationStatus | None = None

storage = open_storage(config.ens_path, mode="w")
ens_path = os.path.join(everest_config.output_dir, STORAGE_DIR)
storage = open_storage(ens_path, mode="w")
status_queue: queue.SimpleQueue[StatusEvents] = queue.SimpleQueue()

config_dict = everest_to_ert_config_dict(everest_config)

runpath_file: Path = Path(
os.path.join(everest_config.output_dir, ".res_runpath_list")
)

assert everest_config.config_file is not None
config_file: Path = Path(everest_config.config_file)

model_config = ModelConfig.from_dict(config_dict)
queue_config = QueueConfig.from_dict(config_dict)

ensemble_config = EnsembleConfig.from_dict(config_dict)

def _get_variables(
variables: list[ControlVariableConfig]
| list[ControlVariableGuessListConfig],
) -> list[str] | dict[str, list[str]]:
if (
isinstance(variables[0], ControlVariableConfig)
and getattr(variables[0], "index", None) is None
):
return [var.name for var in variables]
result: collections.defaultdict[str, list] = collections.defaultdict(list)

Check failure on line 188 in src/ert/run_models/everest_run_model.py

View workflow job for this annotation

GitHub Actions / type-checking (3.12)

Missing type parameters for generic type "list"
for variable in variables:
if isinstance(variable, ControlVariableGuessListConfig):
result[variable.name].extend(
str(index + 1) for index, _ in enumerate(variable.initial_guess)
)
else:
result[variable.name].append(str(variable.index)) # type: ignore

Check failure on line 195 in src/ert/run_models/everest_run_model.py

View workflow job for this annotation

GitHub Actions / type-checking (3.12)

Unused "type: ignore" comment
return dict(result)

# This adds an EXT_PARAM key to the ert_config, which is not a true ERT
# configuration key. When initializing an ERT config object, it is ignored.
# It is used by the Simulator object to inject ExtParamConfig nodes.
for control in everest_config.controls or []:
ensemble_config.parameter_configs[control.name] = ExtParamConfig(
name=control.name,
input_keys=_get_variables(control.variables),
output_file=control.name + ".json",
)

substitutions = _substitutions_from_dict(config_dict)
substitutions["<RUNPATH_FILE>"] = str(runpath_file)
substitutions["<RUNPATH>"] = model_config.runpath_format_string
substitutions["<ECL_BASE>"] = model_config.eclbase_format_string
substitutions["<ECLBASE>"] = model_config.eclbase_format_string
substitutions["<NUM_CPU>"] = str(queue_config.preferred_num_cpu)

ert_templates = read_templates(config_dict)
_, _, hooked_workflows = workflows_from_dict(config_dict, substitutions)

Check failure on line 216 in src/ert/run_models/everest_run_model.py

View workflow job for this annotation

GitHub Actions / type-checking (3.12)

Call to untyped function "workflows_from_dict" in typed context

preinstalled_fm_steps: dict[str, ForwardModelStepPlugin] = {}
pm = ErtPluginManager()
for fm_step_subclass in pm.forward_model_steps:
fm_step = fm_step_subclass()

Check failure on line 221 in src/ert/run_models/everest_run_model.py

View workflow job for this annotation

GitHub Actions / type-checking (3.12)

Missing positional arguments "name", "command" in call to "ForwardModelStepPlugin"
preinstalled_fm_steps[fm_step.name] = fm_step

installed_forward_model_steps: dict[str, ForwardModelStep] = {}
installed_forward_model_steps = copy.deepcopy(preinstalled_fm_steps) # fix

Check failure on line 225 in src/ert/run_models/everest_run_model.py

View workflow job for this annotation

GitHub Actions / type-checking (3.12)

Argument 1 to "deepcopy" has incompatible type "dict[str, ForwardModelStepPlugin]"; expected "dict[str, ForwardModelStep]"
installed_forward_model_steps.update(
installed_forward_model_steps_from_dict(config_dict)
)

env_pr_fm_step = uppercase_subkeys_and_stringify_subvalues(
pm.get_forward_model_configuration()
)

forward_model_steps = create_list_of_forward_model_steps_to_run(
installed_forward_model_steps,
substitutions,
config_dict,
installed_forward_model_steps,
env_pr_fm_step,
)

env_vars = {}
for key, val in config_dict.get("SETENV", []):

Check failure on line 243 in src/ert/run_models/everest_run_model.py

View workflow job for this annotation

GitHub Actions / type-checking (3.12)

No overload variant of "get" of "dict" matches argument types "str", "list[Never]"
env_vars[key] = val

self.support_restart = False
self._parameter_configuration = ensemble_config.parameter_configuration
self._parameter_configs = ensemble_config.parameter_configs
self._response_configuration = ensemble_config.response_configuration

super().__init__(
storage,
config.runpath_file,
Path(config.user_config_file),
config.env_vars,
config.env_pr_fm_step,
config.model_config,
config.queue_config,
config.forward_model_steps,
runpath_file,
config_file,
env_vars,
env_pr_fm_step,
model_config,
queue_config,
forward_model_steps,
status_queue,
config.substitutions,
config.ert_templates,
config.hooked_workflows,
substitutions,
ert_templates,
hooked_workflows,
active_realizations=[], # Set dynamically in run_forward_model()
)
self.support_restart = False
self._parameter_configuration = config.ensemble_config.parameter_configuration
self._parameter_configs = config.ensemble_config.parameter_configs
self._response_configuration = config.ensemble_config.response_configuration

@classmethod
def create(
cls,
ever_config: EverestConfig,
everest_config: EverestConfig,
simulation_callback: SimulationCallback | None = None,
optimization_callback: OptimizerCallback | None = None,
) -> EverestRunModel:
return cls(
config=everest_to_ert_config(ever_config),
everest_config=ever_config,
config=everest_to_ert_config(everest_config),
everest_config=everest_config,
simulation_callback=simulation_callback,
optimization_callback=optimization_callback,
)
Expand Down Expand Up @@ -291,6 +397,7 @@ def _create_optimizer(self) -> BasicOptimizer:
# simplifying code that reads them as fixed width tables. `maximize` is
# set because ropt reports minimization results, while everest wants
# maximization results, necessitating a conversion step.
assert self._everest_config.optimization_output_dir is not None
ropt_output_folder = Path(self._everest_config.optimization_output_dir)
optimizer = (
BasicOptimizer(
Expand Down
8 changes: 8 additions & 0 deletions src/everest/simulator/everest_to_ert.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,14 @@ def _everest_to_ert_config_dict(
return ert_config


def everest_to_ert_config_dict(everest_config: EverestConfig) -> ConfigDict:
with ErtPluginContext():
config_dict = _everest_to_ert_config_dict(
everest_config, site_config=ErtConfig.read_site_config()
)
return config_dict


def everest_to_ert_config(ever_config: EverestConfig) -> ErtConfig:
with ErtPluginContext():
config_dict = _everest_to_ert_config_dict(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
EXECUTABLE /Users/FAAR/code/ert/src/everest/jobs/scripts/recovery_factor
1 change: 1 addition & 0 deletions test-data/everest/math_func/everest_output/.jobs/_render
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
EXECUTABLE /Users/FAAR/code/ert/src/everest/jobs/scripts/render
1 change: 1 addition & 0 deletions test-data/everest/math_func/everest_output/.jobs/_wdcompl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
EXECUTABLE /Users/FAAR/code/ert/src/everest/jobs/scripts/wdcompl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
EXECUTABLE /Users/FAAR/code/ert/src/everest/jobs/scripts/wddatefilter
1 change: 1 addition & 0 deletions test-data/everest/math_func/everest_output/.jobs/_wdfilter
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
EXECUTABLE /Users/FAAR/code/ert/src/everest/jobs/scripts/wdfilter
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
EXECUTABLE /Users/FAAR/code/ert/src/everest/jobs/scripts/wdreorder
1 change: 1 addition & 0 deletions test-data/everest/math_func/everest_output/.jobs/_wdset
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
EXECUTABLE /Users/FAAR/code/ert/src/everest/jobs/scripts/wdset
1 change: 1 addition & 0 deletions test-data/everest/math_func/everest_output/.jobs/_wdupdate
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
EXECUTABLE /Users/FAAR/code/ert/src/everest/jobs/scripts/wdupdate
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"version": 9,
"migrations": []
}
Empty file.

0 comments on commit 851bcae

Please sign in to comment.