static bool on_edge(double _x1 , double _y1 , double _x2 , double _y2 , double x0 , double y0) { double x1 = util_double_min( _x1 , _x2 ); double x2 = util_double_max( _x1 , _x2 ); double y1 = util_double_min( _y1 , _y2 ); double y2 = util_double_max( _y1 , _y2 ); { /* Vertical line */ if (((x1 == x2) && (x0 == x1)) && ((y1 <= y0) && (y0 <= y2))) return true; /* Horizontal line */ if (((x1 <= x0) && (x0 <= x2)) && ((y1 == y2) && (y0 == y1))) return true; /* General slope */ { double a = (y2 - y1) / (x2 - x1); double yc = a*(x0 - x1) + y1; if (yc == y0) { if ((x1 <= x0) && (x0 <= x2)) return true; } } return false; } }
void obs_vector_load_from_SUMMARY_OBSERVATION(obs_vector_type * obs_vector , const conf_instance_type * conf_instance , const history_type * history, ensemble_config_type * ensemble_config) { if(!conf_instance_is_of_class(conf_instance, "SUMMARY_OBSERVATION")) util_abort("%s: internal error. expected \"SUMMARY_OBSERVATION\" instance, got \"%s\".\n", __func__, conf_instance_get_class_name_ref(conf_instance) ); { double obs_value = conf_instance_get_item_value_double(conf_instance, "VALUE" ); double obs_error = conf_instance_get_item_value_double(conf_instance, "ERROR" ); double min_error = conf_instance_get_item_value_double(conf_instance, "ERROR_MIN"); const char * error_mode = conf_instance_get_item_value_ref( conf_instance, "ERROR_MODE"); const char * sum_key = conf_instance_get_item_value_ref( conf_instance, "KEY" ); const char * obs_key = conf_instance_get_name_ref(conf_instance); int size = history_get_last_restart( history ); int obs_restart_nr = __conf_instance_get_restart_nr(conf_instance , obs_key , history , size); if (obs_restart_nr == 0) { int day,month,year; time_t start_time = history_get_time_t_from_restart_nr( history , 0 ); util_set_date_values( start_time , &day , &month , &year); fprintf(stderr,"** ERROR: It is unfortunately not possible to use summary observations from the\n"); fprintf(stderr," start of the simulation. Problem with observation:%s at %02d/%02d/%4d\n",obs_key , day,month,year); exit(1); } { if (strcmp( error_mode , "REL") == 0) obs_error *= obs_value; else if (strcmp( error_mode , "RELMIN") == 0) obs_error = util_double_max( min_error , obs_error * obs_value ); obs_vector_add_summary_obs( obs_vector , obs_restart_nr , sum_key , obs_key , obs_value , obs_error , NULL , 0); } } }
double timer_stop(timer_type *timer) { time_t epoch_time; clock_t clock_time = clock(); time(&epoch_time); if (timer->running) { double cpu_sec; if (timer->epoch_time) cpu_sec = 1.0 * (epoch_time - timer->epoch_start); else cpu_sec = 1.0 * (clock_time - timer->clock_start) / CLOCKS_PER_SEC; timer->count++; timer->sum1 += cpu_sec; timer->sum2 += cpu_sec * cpu_sec; timer->min_time = util_double_min( timer->min_time , cpu_sec); timer->max_time = util_double_max( timer->max_time , cpu_sec); timer->running = false; return cpu_sec; } else util_abort("%s: Timer is not running. Aborting \n",__func__ ); return -1; }
bool geo_util_inside_polygon__(const double * xlist , const double * ylist , int num_points , double x0 , double y0 , bool force_edge_inside) { bool inside = false; int point_num; double y = y0; double xc = 0; for (point_num = 0; point_num < num_points; point_num++) { int next_point = ((point_num + 1) % num_points); double x1 = xlist[point_num]; double y1 = ylist[point_num]; double x2 = xlist[next_point]; double y2 = ylist[next_point]; double ymin = util_double_min(y1,y2); double ymax = util_double_max(y1,y2); double xmax = util_double_max(x1,x2); if (force_edge_inside) { if (on_edge(x1,y1,x2,y2,x0,y0)) { inside = true; break; } } if ((x1 == x2) && (y1 == y2)) continue; if ((y0 > ymin) && (y <= ymax)) { if (x0 <= xmax) { if (y1 != y2) xc = (y0 - y1) * (x2 - x1) / (y2 - y1) + x1; if ((x1 == x2) || (x0 <= xc)) inside = !inside; } } } return inside; }
void gen_kw_set_inflation(gen_kw_type * inflation , const gen_kw_type * std , const gen_kw_type * min_std) { const int data_size = gen_kw_config_get_data_size(std->config ); const double * std_data = std->data; const double * min_std_data = min_std->data; double * inflation_data = inflation->data; { for (int i=0; i < data_size; i++) { if (std_data[i] > 0) inflation_data[i] = util_double_max( 1.0 , min_std_data[i] / std_data[i]); else inflation_data[i] = 1; } } }
obs_vector_type * obs_vector_alloc_from_BLOCK_OBSERVATION(const conf_instance_type * conf_instance , const ecl_grid_type * grid , const ecl_sum_type * refcase , const history_type * history, ensemble_config_type * ensemble_config) { if(!conf_instance_is_of_class(conf_instance, "BLOCK_OBSERVATION")) util_abort("%s: internal error. expected \"BLOCK_OBSERVATION\" instance, got \"%s\".\n", __func__, conf_instance_get_class_name_ref(conf_instance) ); block_obs_source_type source_type = SOURCE_SUMMARY; const char * obs_label = conf_instance_get_name_ref(conf_instance); const char * source_string = conf_instance_get_item_value_ref(conf_instance , "SOURCE"); const char * field_name = conf_instance_get_item_value_ref(conf_instance , "FIELD"); const char * sum_kw = NULL; bool OK = true; if (strcmp(source_string , "FIELD") == 0) { source_type = SOURCE_FIELD; if (!ensemble_config_has_key( ensemble_config , field_name)) { OK = false; fprintf(stderr,"** Warning the ensemble key:%s does not exist - observation:%s not added \n", field_name , obs_label); } } else if (strcmp( source_string , "SUMMARY") == 0) { source_type = SOURCE_SUMMARY; sum_kw = __summary_kw( field_name ); } else util_abort("%s: internal error \n",__func__); if (OK) { obs_vector_type * obs_vector = NULL; int size = history_get_last_restart( history ); int obs_restart_nr ; stringlist_type * summary_keys = stringlist_alloc_new(); stringlist_type * obs_pt_keys = conf_instance_alloc_list_of_sub_instances_of_class_by_name(conf_instance, "OBS"); int num_obs_pts = stringlist_get_size(obs_pt_keys); double * obs_value = util_calloc(num_obs_pts , sizeof * obs_value); double * obs_std = util_calloc(num_obs_pts , sizeof * obs_std ); int * obs_i = util_calloc(num_obs_pts , sizeof * obs_i ); int * obs_j = util_calloc(num_obs_pts , sizeof * obs_j ); int * obs_k = util_calloc(num_obs_pts , sizeof * obs_k ); obs_restart_nr = __conf_instance_get_restart_nr(conf_instance , obs_label , history , size); /** Build the observation. */ for(int obs_pt_nr = 0; obs_pt_nr < num_obs_pts; obs_pt_nr++) { const char * obs_key = stringlist_iget(obs_pt_keys, obs_pt_nr); const conf_instance_type * obs_instance = conf_instance_get_sub_instance_ref(conf_instance, obs_key); const char * error_mode = conf_instance_get_item_value_ref(obs_instance, "ERROR_MODE"); double error = conf_instance_get_item_value_double(obs_instance, "ERROR"); double value = conf_instance_get_item_value_double(obs_instance, "VALUE"); double min_error = conf_instance_get_item_value_double(obs_instance, "ERROR_MIN"); if (strcmp( error_mode , "REL") == 0) error *= value; else if (strcmp( error_mode , "RELMIN") == 0) error = util_double_max( error * value , min_error ); obs_value[obs_pt_nr] = value; obs_std [obs_pt_nr] = error; /** The input values i,j,k come from the user, and are offset 1. They are immediately shifted with -1 to become C-based offset zero. */ obs_i[obs_pt_nr] = conf_instance_get_item_value_int( obs_instance, "I") - 1; obs_j[obs_pt_nr] = conf_instance_get_item_value_int( obs_instance, "J") - 1; obs_k[obs_pt_nr] = conf_instance_get_item_value_int( obs_instance, "K") - 1; if (source_type == SOURCE_SUMMARY) { char * summary_key = smspec_alloc_block_ijk_key( SUMMARY_KEY_JOIN_STRING , sum_kw , obs_i[obs_pt_nr] + 1 , obs_j[obs_pt_nr] + 1 , obs_k[obs_pt_nr] + 1 ); stringlist_append_owned_ref( summary_keys , summary_key ); } } if (source_type == SOURCE_FIELD) { const enkf_config_node_type * config_node = ensemble_config_get_node( ensemble_config , field_name); const field_config_type * field_config = enkf_config_node_get_ref( config_node ); block_obs_type * block_obs = block_obs_alloc_complete(obs_label, source_type , NULL , field_config , grid , num_obs_pts, obs_i, obs_j, obs_k, obs_value, obs_std); if (block_obs != NULL) { obs_vector = obs_vector_alloc( BLOCK_OBS , obs_label , ensemble_config_get_node(ensemble_config , field_name), size ); obs_vector_install_node( obs_vector , obs_restart_nr , block_obs); } } else if (source_type == SOURCE_SUMMARY) { OK = true; if (refcase != NULL) { for (int i=0; i < stringlist_get_size( summary_keys ); i++) { const char * sum_key = stringlist_iget( summary_keys , i ); if (!ecl_sum_has_key(refcase , sum_key)) { /* If the */ fprintf(stderr,"** Warning missing summary %s for cell: (%d,%d,%d) in refcase - make sure that \"BPR %d %d %d\" is included in ECLIPSE summary specification \n" , sum_key , obs_i[i]+1 , obs_j[i]+1 , obs_k[i]+1 , obs_i[i]+1 , obs_j[i]+1 , obs_k[i]+1 ); //OK = false; } } } if (OK) { // We can create the container node and add the summary nodes. enkf_config_node_type * container_config = ensemble_config_add_container( ensemble_config , NULL ); for (int i=0; i < stringlist_get_size( summary_keys ); i++) { const char * sum_key = stringlist_iget( summary_keys , i ); enkf_config_node_type * child_node = ensemble_config_add_summary( ensemble_config , sum_key , LOAD_FAIL_WARN ); enkf_config_node_update_container( container_config , child_node ); } { block_obs_type * block_obs = block_obs_alloc_complete(obs_label, source_type , summary_keys , enkf_config_node_get_ref(container_config) , grid , num_obs_pts, obs_i, obs_j, obs_k, obs_value, obs_std); if (block_obs != NULL) { obs_vector = obs_vector_alloc( BLOCK_OBS , obs_label , container_config, size ); obs_vector_install_node( obs_vector , obs_restart_nr , block_obs); } } } } else util_abort("%s: invalid source value \n",__func__); free(obs_value); free(obs_std); free(obs_i); free(obs_j); free(obs_k); stringlist_free(obs_pt_keys); stringlist_free(summary_keys); return obs_vector; } else { fprintf(stderr,"** Warning the ensemble key:%s does not exist - observation:%s not added \n", field_name , obs_label); return NULL; } }
bool obs_vector_load_from_HISTORY_OBSERVATION(obs_vector_type * obs_vector , const conf_instance_type * conf_instance , const history_type * history , ensemble_config_type * ensemble_config, double std_cutoff ) { if(!conf_instance_is_of_class(conf_instance, "HISTORY_OBSERVATION")) util_abort("%s: internal error. expected \"HISTORY_OBSERVATION\" instance, got \"%s\".\n",__func__, conf_instance_get_class_name_ref(conf_instance) ); { bool initOK = false; int size , restart_nr; double_vector_type * value = double_vector_alloc(0,0); double_vector_type * std = double_vector_alloc(0,0); bool_vector_type * valid = bool_vector_alloc(0 , false); /* The auto_corrf parameters can not be "segmentized" */ double auto_corrf_param = -1; const char * auto_corrf_name = NULL; double error = conf_instance_get_item_value_double(conf_instance, "ERROR" ); double error_min = conf_instance_get_item_value_double(conf_instance, "ERROR_MIN" ); const char * error_mode = conf_instance_get_item_value_ref( conf_instance, "ERROR_MODE"); const char * sum_key = conf_instance_get_name_ref( conf_instance ); if(conf_instance_has_item(conf_instance, "AUTO_CORRF")) { auto_corrf_name = conf_instance_get_item_value_ref( conf_instance , "AUTO_CORRF"); auto_corrf_param = conf_instance_get_item_value_double(conf_instance, "AUTO_CORRF_PARAM"); if(conf_instance_has_item(conf_instance, "AUTO_CORRF_PARAM")) auto_corrf_param = conf_instance_get_item_value_double(conf_instance, "AUTO_CORRF_PARAM"); else util_abort("%s: When specifying AUTO_CORRF you must also give a vlaue for AUTO_CORRF_PARAM",__func__); } // Get time series data from history object and allocate size = history_get_last_restart(history); if (history_init_ts( history , sum_key , value , valid )) { // Create the standard deviation vector if(strcmp(error_mode, "ABS") == 0) { for( restart_nr = 0; restart_nr < size; restart_nr++) double_vector_iset( std , restart_nr , error ); } else if(strcmp(error_mode, "REL") == 0) { for( restart_nr = 0; restart_nr < size; restart_nr++) double_vector_iset( std , restart_nr , error * abs( double_vector_iget( value , restart_nr ))); } else if(strcmp(error_mode, "RELMIN") == 0) { for(restart_nr = 0; restart_nr < size; restart_nr++) { double tmp_std = util_double_max( error_min , error * abs( double_vector_iget( value , restart_nr ))); double_vector_iset( std , restart_nr , tmp_std); } } else util_abort("%s: Internal error. Unknown error mode \"%s\"\n", __func__, error_mode); // Handle SEGMENTs which can be used to customize the observation error. */ { stringlist_type * segment_keys = conf_instance_alloc_list_of_sub_instances_of_class_by_name(conf_instance, "SEGMENT"); stringlist_sort( segment_keys , NULL ); int num_segments = stringlist_get_size(segment_keys); for(int segment_nr = 0; segment_nr < num_segments; segment_nr++) { const char * segment_name = stringlist_iget(segment_keys, segment_nr); const conf_instance_type * segment_conf = conf_instance_get_sub_instance_ref(conf_instance, segment_name); int start = conf_instance_get_item_value_int( segment_conf, "START" ); int stop = conf_instance_get_item_value_int( segment_conf, "STOP" ); double error_segment = conf_instance_get_item_value_double(segment_conf, "ERROR" ); double error_min_segment = conf_instance_get_item_value_double(segment_conf, "ERROR_MIN" ); const char * error_mode_segment = conf_instance_get_item_value_ref( segment_conf, "ERROR_MODE"); if(start < 0) { printf("%s: WARNING - Segment out of bounds. Truncating start of segment to 0.\n", __func__); start = 0; } if(stop >= size) { printf("%s: WARNING - Segment out of bounds. Truncating end of segment to %d.\n", __func__, size - 1); stop = size -1; } if(start > stop) { printf("%s: WARNING - Segment start after stop. Truncating end of segment to %d.\n", __func__, start ); stop = start; } // Create the standard deviation vector if(strcmp(error_mode_segment, "ABS") == 0) { for( restart_nr = start; restart_nr <= stop; restart_nr++) double_vector_iset( std , restart_nr , error_segment) ; } else if(strcmp(error_mode_segment, "REL") == 0) { for( restart_nr = start; restart_nr <= stop; restart_nr++) double_vector_iset( std , restart_nr , error_segment * abs(double_vector_iget( value , restart_nr))); } else if(strcmp(error_mode_segment, "RELMIN") == 0) { for(restart_nr = start; restart_nr <= stop ; restart_nr++) { double tmp_std = util_double_max( error_min_segment , error_segment * abs( double_vector_iget( value , restart_nr ))); double_vector_iset( std , restart_nr , tmp_std); } } else util_abort("%s: Internal error. Unknown error mode \"%s\"\n", __func__, error_mode); } stringlist_free(segment_keys); } /* This is where the summary observations are finally added. */ for (restart_nr = 0; restart_nr < size; restart_nr++) { if (bool_vector_safe_iget( valid , restart_nr)) { if (double_vector_iget( std , restart_nr) > std_cutoff) { obs_vector_add_summary_obs( obs_vector , restart_nr , sum_key , sum_key , double_vector_iget( value ,restart_nr) , double_vector_iget( std , restart_nr ) , auto_corrf_name , auto_corrf_param); } else fprintf(stderr,"** Warning: to small observation error in observation %s:%d - ignored. \n", sum_key , restart_nr); } } initOK = true; } double_vector_free(std); double_vector_free(value); bool_vector_free(valid); return initOK; } }
void plot_range_update_y( plot_range_type * range , double y ) { range->current_ymin = util_double_min( range->current_ymin , y ); range->current_ymax = util_double_max( range->current_ymax , y ); }
void plot_range_update_x( plot_range_type * range , double x ) { range->current_xmin = util_double_min( range->current_xmin , x ); range->current_xmax = util_double_max( range->current_xmax , x ); }
static int gravity_check_input( const ecl_grid_type * ecl_grid , const ecl_file_type * init_file , const ecl_file_type * restart_file1, const ecl_file_type * restart_file2, int * __model_phases, int * __file_phases) { { int model_phases = 0; int file_phases = 0; /* Check which phases are present in the model */ if (ecl_file_has_kw(restart_file1 , "OIL_DEN")) { model_phases += OIL; simulator = ECLIPSE100 ; } else if (ecl_file_has_kw(restart_file1 , "DENO")) { model_phases += OIL; simulator = ECLIPSE300 ; } ; if (ecl_file_has_kw(restart_file1 , "WAT_DEN")) { model_phases += WATER; simulator = ECLIPSE100 ; } else if (ecl_file_has_kw(restart_file1 , "DENW")) { model_phases += WATER; simulator = ECLIPSE300 ; } ; if (ecl_file_has_kw(restart_file1 , "GAS_DEN")) { model_phases += GAS; simulator = ECLIPSE100 ; } else if (ecl_file_has_kw(restart_file1 , "DENG")) { model_phases += GAS; simulator = ECLIPSE300 ; } ; /* Check which phases are present in the restart files. We assume the restart file NEVER has SOIL information */ if (ecl_file_has_kw(restart_file1 , "SWAT")) file_phases += WATER; if (ecl_file_has_kw(restart_file1 , "SGAS")) file_phases += GAS; /* Consiency check */ { /** The following assumptions are made: 1. All restart files should have water, i.e. the SWAT keyword. 2. All phases present in the restart file should also be present as densities, in addition the model must contain one additional phase. 3. The restart files can never contain oil saturation. */ if ( !has_phase( file_phases , WATER ) ) util_exit("Could not locate SWAT keyword in restart files\n"); if ( has_phase( file_phases , OIL )) util_exit("Can not handle restart files with SOIL keyword\n"); if (! has_phase( model_phases , WATER ) ) util_exit("Could not locate WAT_DEN keyword in restart files\n"); if ( has_phase( file_phases , GAS )) { /** Restart file has both water and gas - means we need all three densities. */ if (! (has_phase( model_phases , GAS) && has_phase( model_phases , OIL))) util_exit("Could not find GAS_DEN and OIL_DEN keywords in restart files\n"); } else { /* This is (water + oil) or (water + gas) system. We enforce one of the densities.*/ if ( !has_phase( model_phases , GAS + OIL)) util_exit("Could not find either GAS_DEN or OIL_DEN kewyords in restart files\n"); } } *__model_phases = model_phases; *__file_phases = file_phases; } /* Check that the restart files have RPORV information. This is ensured by giving the argument RPORV to the RPTRST keyword. */ if ( !(ecl_file_has_kw( restart_file1 , "RPORV") && ecl_file_has_kw( restart_file2 , "RPORV")) ) util_exit("Sorry: the restartfiles do not contain RPORV\n"); /** Check that the rporv values are in the right ballpark. For ECLIPSE version 2008.2 they are way f*****g off. Check PORV versus RPORV for ten 'random' locations in the grid. */ { const ecl_kw_type * rporv1_kw = ecl_file_iget_named_kw( restart_file1 , "RPORV" , 0); const ecl_kw_type * rporv2_kw = ecl_file_iget_named_kw( restart_file2 , "RPORV" , 0); const ecl_kw_type * init_porv_kw = ecl_file_iget_named_kw( init_file , "PORV" , 0); int active_index; int active_delta; int active_size; ecl_grid_get_dims( ecl_grid , NULL , NULL , NULL , &active_size ); active_delta = active_size / 12; for (active_index = active_delta; active_index < active_size; active_index += active_delta) { int global_index = ecl_grid_get_global_index1A( ecl_grid , active_index ); double init_porv = ecl_kw_iget_as_double( init_porv_kw , global_index ); /* NB - this uses global indexing. */ double rporv1 = ecl_kw_iget_as_double( rporv1_kw , active_index ); double rporv2 = ecl_kw_iget_as_double( rporv2_kw , active_index ); double rporv12 = 0.5 * ( rporv1 + rporv2 ); double fraction = util_double_min( init_porv , rporv12 ) / util_double_max( init_porv , rporv12 ); if (fraction < 0.50) { fprintf(stderr,"-----------------------------------------------------------------\n"); fprintf(stderr,"INIT PORV: %g \n",init_porv); fprintf(stderr,"RPORV1 : %g \n",rporv1); fprintf(stderr,"RPORV2 : %g \n",rporv2); fprintf(stderr,"Hmmm - the RPORV values extracted from the restart file seem to be \n"); fprintf(stderr,"veeery different from the initial rporv value. This might indicated\n"); fprintf(stderr,"an ECLIPSE bug. Version 2007.2 is known to be ok in this respect, \n"); fprintf(stderr,"whereas version 2008.2 is known to have a bug. \n"); fprintf(stderr,"-----------------------------------------------------------------\n"); exit(1); } } } return 0; }