diff --git a/tests/ert/ui_tests/cli/test_field_parameter.py b/tests/ert/ui_tests/cli/test_field_parameter.py index b26c6495b5e..2a346b6989a 100644 --- a/tests/ert/ui_tests/cli/test_field_parameter.py +++ b/tests/ert/ui_tests/cli/test_field_parameter.py @@ -5,6 +5,7 @@ import numpy as np import numpy.testing +import resfo import xtgeo from ert.config import ErtConfig @@ -14,126 +15,39 @@ from .run_cli import run_cli -def test_field_param_update(tmpdir): - """ - This replicates the poly example, only it uses FIELD parameter - """ - with tmpdir.as_cwd(): - config = dedent( - """ - NUM_REALIZATIONS 5 - QUEUE_SYSTEM LOCAL - QUEUE_OPTION LOCAL MAX_RUNNING 5 - OBS_CONFIG observations - - FIELD MY_PARAM PARAMETER my_param.grdecl INIT_FILES:my_param.grdecl FORWARD_INIT:True - GRID MY_EGRID.EGRID +def test_field_param_update_using_heat_equation(heat_equation_storage): + config = ErtConfig.from_file("config.ert") + with open_storage(config.ens_path, mode="w") as storage: + experiment = storage.get_experiment_by_name("es-mda") + prior = experiment.get_ensemble_by_name("default_0") + posterior = experiment.get_ensemble_by_name("default_1") - GEN_DATA MY_RESPONSE RESULT_FILE:gen_data_%d.out REPORT_STEPS:0 INPUT_FORMAT:ASCII - INSTALL_JOB poly_eval POLY_EVAL - FORWARD_MODEL poly_eval - """ - ) - with open("config.ert", "w", encoding="utf-8") as fh: - fh.writelines(config) - - NCOL = 5 - NROW = 4 - NLAY = 1 - grid = xtgeo.create_box_grid(dimension=(NCOL, NROW, NLAY)) - grid.to_file("MY_EGRID.EGRID", "egrid") - - with open("forward_model", "w", encoding="utf-8") as f: - f.write( - dedent( - """#!/usr/bin/env python -import xtgeo -import numpy as np -import os + prior_result = prior.load_parameters("COND")["values"] -if __name__ == "__main__": - if not os.path.exists("my_param.grdecl"): - values = np.random.standard_normal(5*4) - with open("my_param.grdecl", "w") as fout: - fout.write("MY_PARAM\\n") - fout.write(" ".join([str(val) for val in values]) + " /\\n") - with open("my_param.grdecl", "r") as fin: - for line_nr, line in enumerate(fin): - if line_nr == 1: - a, b, c, *_ = line.split() + param_config = config.ensemble_config.parameter_configs["COND"] + assert len(prior_result.x) == param_config.nx + assert len(prior_result.y) == param_config.ny + assert len(prior_result.z) == param_config.nz - output = [float(a) * x**2 + float(b) * x + float(c) for x in range(10)] - with open("gen_data_0.out", "w", encoding="utf-8") as f: - f.write("\\n".join(map(str, output))) - """ - ) + posterior_result = posterior.load_parameters("COND")["values"] + prior_covariance = np.cov( + prior_result.values.reshape( + prior.ensemble_size, param_config.nx * param_config.ny * param_config.nz ) - os.chmod( - "forward_model", - os.stat("forward_model").st_mode - | stat.S_IXUSR - | stat.S_IXGRP - | stat.S_IXOTH, ) - with open("POLY_EVAL", "w", encoding="utf-8") as fout: - fout.write("EXECUTABLE forward_model") - with open("observations", "w", encoding="utf-8") as fout: - fout.write( - dedent( - """ - GENERAL_OBSERVATION MY_OBS { - DATA = MY_RESPONSE; - INDEX_LIST = 0,2,4,6,8; - RESTART = 0; - OBS_FILE = obs.txt; - };""" - ) + posterior_covariance = np.cov( + posterior_result.values.reshape( + posterior.ensemble_size, + param_config.nx * param_config.ny * param_config.nz, ) - - with open("obs.txt", "w", encoding="utf-8") as fobs: - fobs.write( - dedent( - """ - 2.1457049781272213 0.6 - 8.769219841380755 1.4 - 12.388014786122742 3.0 - 25.600464531354252 5.4 - 42.35204755970952 8.6""" - ) - ) - - run_cli( - ENSEMBLE_SMOOTHER_MODE, - "--disable-monitor", - "config.ert", ) - config = ErtConfig.from_file("config.ert") - with open_storage(config.ens_path, mode="w") as storage: - experiment = storage.get_experiment_by_name("es") - prior = experiment.get_ensemble_by_name("iter-0") - posterior = experiment.get_ensemble_by_name("iter-1") - - prior_result = prior.load_parameters("MY_PARAM", list(range(5)))["values"] - assert len(prior_result.x) == NCOL - assert len(prior_result.y) == NROW - assert len(prior_result.z) == NLAY + # Check that generalized variance is reduced by update step. + assert np.trace(prior_covariance) > np.trace(posterior_covariance) - posterior_result = posterior.load_parameters("MY_PARAM", list(range(5)))[ - "values" - ] - # Only assert on the first three rows, as there are only three parameters, - # a, b and c, the rest have no correlation to the results. - assert np.linalg.det( - np.cov(prior_result.values.reshape(5, NCOL * NROW * NLAY).T[:3]) - ) > np.linalg.det( - np.cov(posterior_result.values.reshape(5, NCOL * NROW * NLAY).T[:3]) - ) - # This checks that the fields in the runpath are different between iterations - assert Path("simulations/realization-0/iter-0/my_param.grdecl").read_text( - encoding="utf-8" - ) != Path("simulations/realization-0/iter-1/my_param.grdecl").read_text( - encoding="utf-8" - ) + # Check that fields in the runpath are different between iterations + cond_iter0 = resfo.read("simulations/realization-0/iter-0/cond.bgrdecl")[0][1] + cond_iter1 = resfo.read("simulations/realization-0/iter-1/cond.bgrdecl")[0][1] + assert (cond_iter0 != cond_iter1).all() def test_parameter_update_with_inactive_cells_xtgeo_grdecl(tmpdir):