void misfit_ensemble_initialize( misfit_ensemble_type * misfit_ensemble , const ensemble_config_type * ensemble_config , const enkf_obs_type * enkf_obs , enkf_fs_type * fs , int ens_size , int history_length, bool force_init) { if (force_init || !misfit_ensemble->initialized) { misfit_ensemble_clear( misfit_ensemble ); msg_type * msg = msg_alloc("Evaluating misfit for observation: " , false); double ** chi2_work = __2d_malloc( history_length + 1 , ens_size ); bool_vector_type * iens_valid = bool_vector_alloc( ens_size , true ); hash_iter_type * obs_iter = enkf_obs_alloc_iter( enkf_obs ); const char * obs_key = hash_iter_get_next_key( obs_iter ); misfit_ensemble->history_length = history_length; misfit_ensemble_set_ens_size( misfit_ensemble , ens_size ); msg_show( msg ); while (obs_key != NULL) { obs_vector_type * obs_vector = enkf_obs_get_vector( enkf_obs , obs_key ); msg_update( msg , obs_key ); bool_vector_reset( iens_valid ); bool_vector_iset( iens_valid , ens_size - 1 , true ); obs_vector_ensemble_chi2( obs_vector , fs , iens_valid , 0 , misfit_ensemble->history_length, 0 , ens_size , chi2_work); /** Internalizing the results from the chi2_work table into the misfit structure. */ for (int iens = 0; iens < ens_size; iens++) { misfit_member_type * node = misfit_ensemble_iget_member( misfit_ensemble , iens ); if (bool_vector_iget( iens_valid , iens)) misfit_member_update( node , obs_key , misfit_ensemble->history_length , iens , (const double **) chi2_work); } obs_key = hash_iter_get_next_key( obs_iter ); } bool_vector_free( iens_valid ); msg_free(msg , true ); hash_iter_free( obs_iter ); __2d_free( chi2_work , misfit_ensemble->history_length + 1); misfit_ensemble->initialized = true; } }
bool gen_data_fload_with_report_step( gen_data_type * gen_data , const char * filename , int report_step) { bool has_file = util_file_exists(filename); void * buffer = NULL; int size = 0; ecl_type_enum load_type; if ( has_file ) { ecl_type_enum internal_type = gen_data_config_get_internal_type(gen_data->config); gen_data_file_format_type input_format = gen_data_config_get_input_format( gen_data->config ); buffer = gen_common_fload_alloc( filename , input_format , internal_type , &load_type , &size); /* Look for file @filename_active - if that file is found it is interpreted as a an active|inactive mask created by the forward model. The file is assumed to be an ASCII file with integers, 0 indicates inactive elements and 1 active elements. The file should of course be as long as @filename. If the file is not found the gen_data->active_mask is set to all-true (i.e. the default true value is invoked). */ if (gen_data_config_is_dynamic( gen_data->config )) { bool_vector_reset( gen_data->active_mask ); bool_vector_iset( gen_data->active_mask , size - 1, true ); { char * active_file = util_alloc_sprintf("%s_active" , filename ); if (util_file_exists( active_file )) { FILE * stream = util_fopen( active_file , "r"); int active_int; for (int index=0; index < size; index++) { if (fscanf( stream , "%d" , &active_int) == 1) { if (active_int == 1) bool_vector_iset( gen_data->active_mask , index , true); else if (active_int == 0) bool_vector_iset( gen_data->active_mask , index , false); else util_abort("%s: error when loading active mask from:%s only 0 and 1 allowed \n",__func__ , active_file); } else util_abort("%s: error when loading active mask from:%s - file not long enough.\n",__func__ , active_file ); } fclose( stream ); } free( active_file ); } } } gen_data_set_data__(gen_data , size , report_step , load_type , buffer ); util_safe_free(buffer); return has_file; }
/** This function will load an active map from the enkf_fs filesystem. */ void gen_data_config_load_active( gen_data_config_type * config , enkf_fs_type * fs, int report_step , bool force_load) { bool fs_changed = false; if (fs != config->read_fs) { config->read_fs = fs; fs_changed = true; } if (!config->dynamic) return; /* This is used as a GEN_PARAM instance - and the loading of mask is not an option. */ pthread_mutex_lock( &config->update_lock ); { if ( force_load || (int_vector_iget( config->data_size_vector , report_step ) > 0)) { if (config->active_report_step != report_step || fs_changed) { char * filename = util_alloc_sprintf("%s_active" , config->key ); FILE * stream = enkf_fs_open_excase_tstep_file( fs , filename , report_step); if (stream != NULL) { bool_vector_fread( config->active_mask , stream ); fclose( stream ); } else { int gen_data_size = int_vector_safe_iget( config->data_size_vector, report_step ); if (gen_data_size < 0) { fprintf(stderr,"** Fatal internal error in function:%s \n",__func__); fprintf(stderr,"\n"); fprintf(stderr," 1: The active mask file:%s was not found \n",filename); fprintf(stderr," 2: The size of the gen_data vectors has not been set\n"); fprintf(stderr,"\n"); fprintf(stderr,"We can not create a suitable active_mask. Code should call gen_data_config_has_active_mask()\n\n"); util_abort("%s: fatal internal error - could not create a suitable active_mask \n",__func__); } else { fprintf(stdout,"** Info: could not locate active data elements file %s, filling active vector with true all elements active \n",filename); bool_vector_reset( config->active_mask ); bool_vector_iset( config->active_mask, gen_data_size - 1, true); } } free( filename ); } } config->active_report_step = report_step; } pthread_mutex_unlock( &config->update_lock ); }
bool gen_data_fload_with_report_step( gen_data_type * gen_data , const char * filename , const forward_load_context_type * load_context) { bool file_exists = util_file_exists(filename); void * buffer = NULL; ecl_data_type load_type; if ( file_exists ) { ecl_data_type internal_type = gen_data_config_get_internal_data_type(gen_data->config); gen_data_file_format_type input_format = gen_data_config_get_input_format( gen_data->config ); int size = 0; buffer = gen_common_fload_alloc( filename , input_format , internal_type , &load_type , &size); if (size > 0) { gen_data_fload_active__(gen_data, filename, size); } else { bool_vector_reset( gen_data->active_mask ); } gen_data_set_data__(gen_data , size , load_context , load_type , buffer ); util_safe_free(buffer); } return file_exists; }
void gen_data_config_update_active(gen_data_config_type * config, int report_step , const bool_vector_type * data_mask) { pthread_mutex_lock( &config->update_lock ); { if ( int_vector_iget( config->data_size_vector , report_step ) > 0) { if (config->active_report_step != report_step) { /* This is the first ensemeble member loading for this particular report_step. */ bool_vector_reset( config->active_mask ); bool_vector_iset( config->active_mask , int_vector_iget( config->data_size_vector , report_step ) - 1 , true ); config->mask_modified = true; } { int i; for (i=0; i < bool_vector_size( data_mask ); i++) { if (!bool_vector_iget( data_mask , i )) { bool_vector_iset( config->active_mask , i , false ); config->mask_modified = true; } } } if (config->mask_modified) { /** The global mask has been modified after the last load; i.e. we update the on-disk representation. */ char * filename = util_alloc_sprintf("%s_active" , config->key ); FILE * stream = enkf_fs_open_case_tstep_file( config->write_fs , filename , report_step , "w"); bool_vector_fwrite( config->active_mask , stream ); fclose( stream ); free( filename ); config->mask_modified = false; } } config->active_report_step = report_step; } pthread_mutex_unlock( &config->update_lock ); }
static bool gen_data_fload_active__(gen_data_type * gen_data, const char * filename, int size) { /* Look for file @filename_active - if that file is found it is interpreted as a an active|inactive mask created by the forward model. The file is assumed to be an ASCII file with integers, 0 indicates inactive elements and 1 active elements. The file should of course be as long as @filename. If the file is not found the gen_data->active_mask is set to all-true (i.e. the default true value is invoked). */ bool file_exists = false; if (gen_data_config_is_dynamic( gen_data->config )) { bool_vector_reset( gen_data->active_mask ); bool_vector_iset( gen_data->active_mask , size - 1, true ); { char * active_file = util_alloc_sprintf("%s_active" , filename ); if (util_file_exists( active_file )) { file_exists = true; FILE * stream = util_fopen( active_file , "r"); int active_int; for (int index=0; index < size; index++) { if (fscanf( stream , "%d" , &active_int) == 1) { if (active_int == 1) bool_vector_iset( gen_data->active_mask , index , true); else if (active_int == 0) bool_vector_iset( gen_data->active_mask , index , false); else util_abort("%s: error when loading active mask from:%s only 0 and 1 allowed \n",__func__ , active_file); } else util_abort("%s: error when loading active mask from:%s - file not long enough.\n",__func__ , active_file ); } fclose( stream ); } free( active_file ); } } return file_exists; }
void enkf_config_node_init_internalization(enkf_config_node_type * node) { if (node->internalize != NULL) bool_vector_reset( node->internalize ); }
bool string_util_init_active_mask( const char * range_string , bool_vector_type * active_mask ) { bool_vector_reset( active_mask ); return string_util_update_active_mask( range_string , active_mask ); }
/* Setting everything back to the default value: false. */ void model_config_init_internalization( model_config_type * config ) { bool_vector_reset(config->internalize_state); bool_vector_reset(config->__load_state); }