Skip to content

Commit 40063a7

Browse files
committed
Fix ert using incorrect gen_kw when exporting to runpath if name is substring
This commit fixes the issue where two gen kw parameters where one of the names is a substring of the other one would be overwritten.
1 parent e8826e3 commit 40063a7

2 files changed

Lines changed: 58 additions & 1 deletion

File tree

src/ert/storage/local_ensemble.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ def load_scalar_keys(
606606
for col in df.columns:
607607
if col == "realization":
608608
continue
609-
if col.startswith(key):
609+
if col == key:
610610
tmp_configuration[col] = (
611611
self.experiment.parameter_configuration[key]
612612
)

tests/ert/unit_tests/config/test_gen_kw_config.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import json
12
import math
23
import re
4+
from itertools import permutations
35
from pathlib import Path
46
from textwrap import dedent
57

@@ -856,3 +858,58 @@ def test_that_const_keyword_sets_update_to_false(tmpdir):
856858

857859
gen_kw_config = ert_config.ensemble_config.parameter_configs["CONST_TEST"]
858860
assert gen_kw_config.update is False
861+
862+
863+
@pytest.mark.parametrize("order", list(permutations([("A", 1), ("AA", 2), ("AAA", 3)])))
864+
def test_that_gen_kw_substitutes_correctly(order, tmpdir, storage, run_args):
865+
"""This is a regression test to check that the substitution mechanism
866+
works correctly when there are multiple parameters with similar names."""
867+
with tmpdir.as_cwd():
868+
config = dedent(
869+
"""
870+
JOBNAME my_name%d
871+
NUM_REALIZATIONS 1
872+
GEN_KW KW_NAME prior.txt
873+
"""
874+
)
875+
Path("config.ert").write_text(config, encoding="utf-8")
876+
Path("prior.txt").write_text(
877+
"\n".join(
878+
f"{param_name} CONST {param_value}"
879+
for (param_name, param_value) in order
880+
),
881+
encoding="utf-8",
882+
)
883+
884+
ert_config = ErtConfig.from_file("config.ert")
885+
886+
experiment_id = storage.create_experiment(
887+
experiment_config={
888+
"parameter_configuration": (
889+
ert_config.ensemble_config.parameter_configuration
890+
)
891+
}
892+
)
893+
prior_ensemble = storage.create_ensemble(
894+
experiment_id, name="prior", ensemble_size=1
895+
)
896+
sample_prior(prior_ensemble, [0], 123, 1)
897+
create_run_path(
898+
run_args=run_args(ert_config, prior_ensemble),
899+
ensemble=prior_ensemble,
900+
runpaths=Runpaths.from_config(ert_config),
901+
user_config_file=ert_config.user_config_file,
902+
forward_model_steps=ert_config.forward_model_steps,
903+
env_vars=ert_config.env_vars,
904+
env_pr_fm_step=ert_config.env_pr_fm_step,
905+
substitutions=ert_config.substitutions,
906+
parameters_file="parameters",
907+
)
908+
909+
param_json = json.loads(
910+
Path("simulations/realization-0/iter-0/parameters.json").read_text(
911+
encoding="utf-8"
912+
)
913+
)
914+
for param_name, param_value in order:
915+
assert int(param_json[f"{param_name}"]["value"]) == param_value

0 commit comments

Comments
 (0)