def test_gen_data(self): # Must have %d in filename argument with self.assertRaises(ValueError): config_node = EnkfConfigNode.create_gen_data( "KEY", "FILE" ) config_node = EnkfConfigNode.create_gen_data( "KEY", "FILE%d" ) self.assertIsInstance( config_node, EnkfConfigNode ) gen_data = config_node.getModelConfig( ) self.assertEqual( 1, gen_data.getNumReportStep( ) ) self.assertEqual( 0, gen_data.getReportStep(0) ) config_node = EnkfConfigNode.create_gen_data( "KEY", "FILE%d" , report_steps = [10,20,30]) self.assertIsInstance( config_node, EnkfConfigNode ) gen_data = config_node.getModelConfig( ) self.assertEqual( 3, gen_data.getNumReportStep( ) ) for r1,r2 in zip([10,20,30] , gen_data.getReportSteps()): self.assertEqual( r1,r2 )
def __init__( self, ensemble_config_node: EnkfConfigNode, file_system, input_mask=None ): assert isinstance(ensemble_config_node, EnkfConfigNode) assert ensemble_config_node.getImplementationType() == ErtImplType.GEN_KW c_pointer = self._alloc(ensemble_config_node) super().__init__(c_pointer) self.__load(file_system, input_mask)
def test_observations(self): with TestAreaContext("enkf_test") as work_area: work_area.copy_directory(self.case_directory) res_config = ResConfig("simple_config/minimum_config") main = EnKFMain(res_config) count = 10 summary_key = "test_key" observation_key = "test_obs_key" summary_observation_node = EnkfConfigNode.createSummaryConfigNode( summary_key, LoadFailTypeEnum.LOAD_FAIL_EXIT) observation_vector = ObsVector( EnkfObservationImplementationType.SUMMARY_OBS, observation_key, summary_observation_node, count, ) main.getObservations().addObservationVector(observation_vector) values = [] for index in range(0, count): value = index * 10.5 std = index / 10.0 summary_observation_node = SummaryObservation( summary_key, observation_key, value, std) observation_vector.installNode(index, summary_observation_node) self.assertEqual(observation_vector.getNode(index), summary_observation_node) self.assertEqual(value, summary_observation_node.getValue()) values.append((index, value, std)) observations = main.getObservations() test_vector = observations[observation_key] index = 0 for node in test_vector: self.assertTrue(isinstance(node, SummaryObservation)) self.assertEqual(node.getValue(), index * 10.5) index += 1 self.assertEqual(observation_vector, test_vector) for index, value, std in values: self.assertTrue(test_vector.isActive(index)) summary_observation_node = test_vector.getNode(index) """@type: SummaryObservation""" self.assertEqual(value, summary_observation_node.getValue()) self.assertEqual( std, summary_observation_node.getStandardDeviation()) self.assertEqual(summary_key, summary_observation_node.getSummaryKey())
def test_config(self): keys = ["Key1", "Key2", "Key3"] with TestAreaContext("enkf_node"): config = EnkfConfigNode.create_ext_param("key", keys) node = EnkfNode(config) ext_node = node.as_ext_param() ext_config = config.getModelConfig() ext_node.set_vector([1, 2, 3]) node.ecl_write("path") d = json.load(open("path/key.json")) self.assertEqual(d["Key1"], 1) self.assertEqual(d["Key3"], 3)
def __init__(self, res_config, controls, results, callback=None): """Will create simulator which can be used to run multiple simulations. The @res_config argument should be a ResConfig object, representing the fully configured state of libres. The @controls argument configures which parameters the simulator should get when actually simulating. The @controls argument should be a dictionary like this : controls = { "cmode": ["Well", "Group"], "order": "W" : ["01", "02", "03"] } In this example, the first group of controls "cmode" includes two controls, called "Well" and "Group". The second group of controls "order" has one control "W" with three suffixes. Note that: - Either no variable in a group has suffixes or all the variables in the group have suffixes. - Suffixes must be specified as non-empty collections of strings. - No duplicate groups/controls/suffixes are allowed When actually simulating, these values will be written to json files looking like this: cmode.json = {"Well": 1.0, "Group": 2.0} order.json = { "W": "01": 0.3, "02": 1.0, "03": 4.2 } When later invoking the start() method the simulator expects to get values for all parameters configured with the @controls argument, otherwise an exception will be raised. Internally in libres code the controls will be implemented as 'ext_param' instances. The @results argument is a list of keys of results which the simulator expects to be generated by the forward model. If argument @results looks like: results = ["CMODE", "order"] The simulator will look for the files 'CMODE_0' and 'order_0' in the simulation folder. If those files are not produced by the simulator an exception will be raised. The optional argument callback can be used to provide a callable which will be called as: callback(run_context) When the simulator has started. For the callable passed as callback you are encouraged to use the future proof signature: def callback(*args, **kwargs): .... """ if not isinstance(res_config, ResConfig): raise ValueError( "The first argument must be valid ResConfig instance") self.res_config = res_config self.ert = EnKFMain(self.res_config) self.control_keys = set(controls.keys()) self.result_keys = set(results) self.callback = callback ens_config = self.res_config.ensemble_config for control_name, variables in controls.items(): ens_config.addNode( EnkfConfigNode.create_ext_param(control_name, variables)) for key in results: ens_config.addNode( EnkfConfigNode.create_gen_data(key, "{}_%d".format(key)))
def __init__(self, config_content=None, grid=None, refcase=None, config_dict=None): if config_content is not None and config_dict is not None: raise ValueError( "Attempting to create EnsembleConfig object with multiple config objects" ) c_ptr = None if config_dict is not None: c_ptr = self._alloc_full( config_dict.get(ConfigKeys.GEN_KW_TAG_FORMAT)) if c_ptr is None: raise ValueError( "Failed to construct EnsembleConfig instance from dict") super(EnsembleConfig, self).__init__(c_ptr) gen_param_list = config_dict.get(ConfigKeys.GEN_PARAM, []) for gene_param in gen_param_list: gen_param_node = EnkfConfigNode.create_gen_param( gene_param.get(ConfigKeys.NAME), gene_param.get(ConfigKeys.FORWARD_INIT), gene_param.get(ConfigKeys.INPUT_FORMAT), gene_param.get(ConfigKeys.OUTPUT_FORMAT), gene_param.get(ConfigKeys.INIT_FILES), gene_param.get(ConfigKeys.ECL_FILE), gene_param.get(ConfigKeys.MIN_STD), gene_param.get(ConfigKeys.TEMPLATE), gene_param.get(ConfigKeys.KEY_KEY)) self.addNode(gen_param_node) gen_data_list = config_dict.get(ConfigKeys.GEN_DATA, []) for gene_data in gen_data_list: gen_data_node = EnkfConfigNode.create_gen_data_full( gene_data.get(ConfigKeys.NAME), gene_data.get(ConfigKeys.RESULT_FILE), gene_data.get(ConfigKeys.INPUT_FORMAT), gene_data.get(ConfigKeys.REPORT_STEPS), gene_data.get(ConfigKeys.ECL_FILE), gene_data.get(ConfigKeys.INIT_FILES), gene_data.get(ConfigKeys.TEMPLATE), gene_data.get(ConfigKeys.KEY_KEY)) self.addNode(gen_data_node) custom_kw_list = config_dict.get(ConfigKeys.CUSTOM_KW, []) for custom_kw in custom_kw_list: custom_kw_node = EnkfConfigNode.create_custom_kw( custom_kw.get(ConfigKeys.NAME), custom_kw.get(ConfigKeys.RESULT_FILE), custom_kw.get(ConfigKeys.OUT_FILE)) self.addNode(custom_kw_node) gen_kw_list = config_dict.get(ConfigKeys.GEN_KW, []) for gen_kw in gen_kw_list: gen_kw_node = EnkfConfigNode.create_gen_kw( gen_kw.get(ConfigKeys.NAME), _get_abs_path(gen_kw.get(ConfigKeys.TEMPLATE)), gen_kw.get(ConfigKeys.OUT_FILE), _get_abs_path(gen_kw.get(ConfigKeys.PARAMETER_FILE)), gen_kw.get(ConfigKeys.FORWARD_INIT), gen_kw.get(ConfigKeys.MIN_STD), gen_kw.get(ConfigKeys.INIT_FILES), config_dict.get(ConfigKeys.GEN_KW_TAG_FORMAT)) self.addNode(gen_kw_node) surface_list = config_dict.get(ConfigKeys.SURFACE_KEY, []) for surface in surface_list: surface_node = EnkfConfigNode.create_surface( surface.get(ConfigKeys.NAME), surface.get(ConfigKeys.INIT_FILES), surface.get(ConfigKeys.OUT_FILE), surface.get(ConfigKeys.BASE_SURFACE_KEY), surface.get(ConfigKeys.MIN_STD), surface.get(ConfigKeys.FORWARD_INIT)) self.addNode(surface_node) summary_list = config_dict.get(ConfigKeys.SUMMARY, []) for a in summary_list: self.add_summary_full(a, refcase) field_list = config_dict.get(ConfigKeys.FIELD_KEY, []) for field in field_list: field_node = EnkfConfigNode.create_field( field.get(ConfigKeys.NAME), field.get(ConfigKeys.VAR_TYPE), grid, self._get_trans_table(), field.get(ConfigKeys.OUT_FILE), field.get(ConfigKeys.ENKF_INFILE), field.get(ConfigKeys.FORWARD_INIT), field.get(ConfigKeys.INIT_TRANSFORM), field.get(ConfigKeys.OUTPUT_TRANSFORM), field.get(ConfigKeys.INPUT_TRANSFORM), field.get(ConfigKeys.MIN_STD), field.get(ConfigKeys.MIN_KEY), field.get(ConfigKeys.MAX_KEY), field.get(ConfigKeys.INIT_FILES)) self.addNode(field_node) schedule_file_list = config_dict.get( ConfigKeys.SCHEDULE_PREDICTION_FILE, []) for schedule_file in schedule_file_list: schedule_file_node = EnkfConfigNode.create_gen_kw( ConfigKeys.PRED_KEY, schedule_file.get(ConfigKeys.TEMPLATE), _get_filename(schedule_file.get(ConfigKeys.TEMPLATE)), schedule_file.get(ConfigKeys.PARAMETER_KEY), False, schedule_file.get(ConfigKeys.MIN_STD), schedule_file.get(ConfigKeys.INIT_FILES), config_dict.get(ConfigKeys.GEN_KW_TAG_FORMAT)) self.addNode(schedule_file_node) container_list = config_dict.get(ConfigKeys.CONTAINER_KEY, []) for container in container_list: container_node = EnkfConfigNode.create_container( container.get(ConfigKeys.NAME)) for child_key in container.get(ConfigKeys.ARGLIST): container_node._update_container(self.getNode(child_key)) self.addNode(container_node) return c_ptr = self._alloc(config_content, grid, refcase) if c_ptr is None: raise ValueError("Failed to construct EnsembleConfig instance") super(EnsembleConfig, self).__init__(c_ptr)
def addNode(self, config_node: EnkfConfigNode): assert isinstance(config_node, EnkfConfigNode) self._add_node(config_node) config_node.convertToCReference(self)