|
| 1 | +import json |
1 | 2 | import math |
2 | 3 | import re |
| 4 | +from itertools import permutations |
3 | 5 | from pathlib import Path |
4 | 6 | from textwrap import dedent |
5 | 7 |
|
@@ -856,3 +858,58 @@ def test_that_const_keyword_sets_update_to_false(tmpdir): |
856 | 858 |
|
857 | 859 | gen_kw_config = ert_config.ensemble_config.parameter_configs["CONST_TEST"] |
858 | 860 | 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