def test_assert_symlink_deleted(setup_case): res_config = setup_case("local/snake_oil_field", "snake_oil.ert") ert = EnKFMain(res_config) runpath_list = ert.getRunpathList() runner = ert.getEnkfSimulationRunner() # create directory structure model_config = ert.getModelConfig() run_context = ErtRunContext.ensemble_experiment( ert.getEnkfFsManager().getCurrentFileSystem(), [True], model_config.getRunpathFormat(), model_config.getJobnameFormat(), ert.getDataKW(), 0, ) runner.createRunPath(run_context) # replace field file with symlink linkpath = "%s/permx.grdcel" % str(runpath_list[0].runpath) targetpath = "%s/permx.grdcel.target" % str(runpath_list[0].runpath) open(targetpath, "a").close() os.remove(linkpath) os.symlink(targetpath, linkpath) # recreate directory structure runner.createRunPath(run_context) # ensure field symlink is replaced by file assert not os.path.islink(linkpath)
def test_localization(setup_case, expected_target_gen_kw): """ Note that this is now a snapshot test, so there is no guarantee that the snapshots are correct, they are just documenting the current behavior. """ res_config = setup_case("local/snake_oil", "snake_oil.ert") ert = EnKFMain(res_config) es_update = ESUpdate(ert) fsm = ert.getEnkfFsManager() sim_fs = fsm.getFileSystem("default_0") target_fs = fsm.getFileSystem("target") # perform localization localized_idxs = (1, 2) local_config = ert.getLocalConfig() local_config.clear() obs = local_config.createObsdata("OBSSET_LOCA") obs.addNode("WOPR_OP1_72") ministep = local_config.createMinistep("MINISTEP_LOCA") ministep.addActiveData("SNAKE_OIL_PARAM") # replace dataset.addNode() active_list = ministep.getActiveList("SNAKE_OIL_PARAM") for i in localized_idxs: active_list.addActiveIndex(i) ministep.attachObsset(obs) updatestep = local_config.getUpdatestep() updatestep.attachMinistep(ministep) # Run ensemble smoother mask = [True] * ert.getEnsembleSize() model_config = ert.getModelConfig() path_fmt = model_config.getRunpathFormat() jobname_fmt = model_config.getJobnameFormat() subst_list = None run_context = ErtRunContext.ensemble_smoother( sim_fs, target_fs, mask, path_fmt, jobname_fmt, subst_list, 0 ) es_update.smootherUpdate(run_context) conf = ert.ensembleConfig()["SNAKE_OIL_PARAM"] sim_node = EnkfNode(conf) target_node = EnkfNode(conf) node_id = NodeId(0, 0) sim_node.load(sim_fs, node_id) target_node.load(target_fs, node_id) sim_gen_kw = list(sim_node.asGenKw()) target_gen_kw = list(target_node.asGenKw()) # Test that the localized values has been updated assert sim_gen_kw[1:3] != target_gen_kw[1:3] # test that all the other values are left unchanged assert sim_gen_kw[3:] == target_gen_kw[3:] assert sim_gen_kw[0] == target_gen_kw[0] assert target_gen_kw == pytest.approx(expected_target_gen_kw)
def test_forward_model_job(job, forward_model, expected_args): with open("job_file", "w") as fout: fout.write(job) with open("config_file.ert", "w") as fout: # Write a minimal config file fout.write(dedent(""" NUM_REALIZATIONS 1 """)) fout.write("INSTALL_JOB job_name job_file\n") fout.write(forward_model) res_config = ResConfig("config_file.ert") ert = EnKFMain(res_config) model_config = ert.getModelConfig() forward_model = model_config.getForwardModel() assert forward_model.get_size() == 1 assert forward_model.iget_job(0).get_argvalues() == expected_args
def test_simulation_job(job, forward_model, expected_args): with open("job_file", "w") as fout: fout.write(job) with open("config_file.ert", "w") as fout: # Write a minimal config file fout.write("NUM_REALIZATIONS 1\n") fout.write("INSTALL_JOB job_name job_file\n") fout.write(forward_model) res_config = ResConfig("config_file.ert") ert = EnKFMain(res_config) model_config = ert.getModelConfig() forward_model = model_config.getForwardModel() forward_model_job = forward_model.iget_job(0) assert forward_model.get_size() == 1 assert forward_model_job.get_argvalues() == expected_args assert forward_model_job.get_arglist() == expected_args assert forward_model_job.valid_args(forward_model_job.arg_types, forward_model_job.get_arglist())
def test_transfer_var(use_tmpdir): # Write a minimal config file with env with open("config_file.ert", "w") as fout: fout.write( dedent(""" NUM_REALIZATIONS 1 JOBNAME a_name_%d SETENV FIRST TheFirstValue SETENV SECOND TheSecondValue UPDATE_PATH THIRD TheThirdValue UPDATE_PATH FOURTH TheFourthValue """)) res_config = ResConfig("config_file.ert") ert = EnKFMain(res_config) fs_manager = ert.getEnkfFsManager() model_config = ert.getModelConfig() run_context = ErtRunContext.ensemble_experiment( fs_manager.getCurrentFileSystem(), [True], model_config.getRunpathFormat(), model_config.getJobnameFormat(), ert.getDataKW(), 0, ) ert.getEnkfSimulationRunner().createRunPath(run_context) os.chdir("simulations/realization0") with open("jobs.json", "r") as f: data = json.load(f) env_data = data["global_environment"] assert env_data["FIRST"] == "TheFirstValue" assert env_data["SECOND"] == "TheSecondValue" path_data = data["global_update_path"] assert "TheThirdValue" == path_data["THIRD"] assert "TheFourthValue" == path_data["FOURTH"]
def test_assert_export(use_tmpdir): # Write a minimal config file with env with open("config_file.ert", "w") as fout: fout.write( dedent( """ NUM_REALIZATIONS 1 JOBNAME a_name_%d RUNPATH_FILE directory/test_runpath_list.txt """ ) ) res_config = ResConfig("config_file.ert") ert = EnKFMain(res_config) runpath_list = ert.getRunpathList() assert not os.path.isfile(runpath_list.getExportFile()) fs_manager = ert.getEnkfFsManager() model_config = ert.getModelConfig() run_context = ErtRunContext.ensemble_experiment( fs_manager.getCurrentFileSystem(), [True], model_config.getRunpathFormat(), model_config.getJobnameFormat(), ert.getDataKW(), 0, ) ert.getEnkfSimulationRunner().createRunPath(run_context) assert os.path.isfile(runpath_list.getExportFile()) assert "test_runpath_list.txt" == os.path.basename(runpath_list.getExportFile()) assert ( Path(runpath_list.getExportFile()).read_text("utf-8") == f"000 {os.getcwd()}/simulations/realization0 a_name_0 000\n" )