void layer_add_ijbarrier( layer_type * layer , int i1 , int j1 , int i2 , int j2 ) { if ((j1 == j2) || (i1 == i2)) { if (i1 == i2) { int j; int jmin = util_int_min(j1,j2); int jmax = util_int_max(j1,j2); for (j=jmin; j < jmax; j++) { cell_type * cell = layer_iget_cell__( layer , i1 , j ); cell->left_barrier = true; } } else { int i; int imin = util_int_min(i1,i2); int imax = util_int_max(i1,i2); for (i=imin; i < imax; i++) { cell_type * cell = layer_iget_cell__( layer , i , j1 ); cell->bottom_barrier = true; } } } else util_abort("%s: fatal error must have i1 == i2 || j1 == j2 \n",__func__); }
static void util_fprintf_backtrace(FILE * stream) { const char * with_linenr_format = " #%02d %s(..) %s in %s:%d\n"; const char * func_format = " #%02d %s(..) %s in ???\n"; const char * unknown_format = " #%02d ???? \n"; const int max_bt = 100; const int max_func_length = 70; void *bt_addr[max_bt]; int size,i; size = backtrace(bt_addr , max_bt); fprintf(stream , "--------------------------------------------------------------------------------\n"); for (i=0; i < size; i++) { int line_nr; char * func_name; char * file_name; char * padding = NULL; if (util_addr2line_lookup(bt_addr[i], &func_name , &file_name , &line_nr)) { int pad_length; char * function; // Seems it can return true - but with func_name == NULL?! Static/inlinded functions? if (func_name) function = func_name; else function = "???"; pad_length = util_int_max (2, 2 + max_func_length - strlen(function)); padding = realloc_padding( padding , pad_length); fprintf(stream , with_linenr_format , i , function , padding , file_name , line_nr); } else { if (func_name != NULL) { int pad_length = util_int_max( 2 , 2 + max_func_length - strlen(func_name)); padding = realloc_padding( padding , pad_length); fprintf(stream , func_format , i , func_name , padding); } else { padding = realloc_padding( padding , 2 + max_func_length ); fprintf(stream , unknown_format , i , padding); } } util_safe_free( func_name ); util_safe_free( file_name ); util_safe_free( padding ); } fprintf(stream , "--------------------------------------------------------------------------------\n"); }
bool fault_block_layer_scan_kw( fault_block_layer_type * layer , const ecl_kw_type * fault_block_kw) { bool assign_zero = true; if (ecl_kw_get_size( fault_block_kw) != ecl_grid_get_global_size(layer->grid)) return false; else if (!ecl_type_is_int(ecl_kw_get_data_type( fault_block_kw ))) return false; else { int i,j; int max_block_id = 0; layer_type * work_layer = layer_alloc( ecl_grid_get_nx( layer->grid ) , ecl_grid_get_ny( layer->grid )); for (j=0; j < ecl_grid_get_ny( layer->grid ); j++) { for (i=0; i < ecl_grid_get_nx( layer->grid ); i++) { int g = ecl_grid_get_global_index3( layer->grid , i , j , layer->k ); int block_id = ecl_kw_iget_int( fault_block_kw , g ); if (block_id > 0) { layer_iset_cell_value( work_layer , i , j , block_id ); max_block_id = util_int_max( block_id , max_block_id ); } } } if (assign_zero) layer_replace_cell_values( work_layer , 0 , max_block_id + 1); fault_block_layer_scan_layer( layer , work_layer ); layer_free( work_layer ); return true; } }
bool nnc_info_equal( const nnc_info_type * nnc_info1 , const nnc_info_type * nnc_info2 ) { if (nnc_info1 == nnc_info2) return true; if ((nnc_info1 == NULL) || (nnc_info2 == NULL)) return false; { if (nnc_info1->lgr_nr != nnc_info2->lgr_nr) return false; if ((int_vector_size( nnc_info1->lgr_index_map ) > 0) && (int_vector_size( nnc_info2->lgr_index_map ) > 0)) { int max_lgr_nr = util_int_max( int_vector_size( nnc_info1->lgr_index_map ), int_vector_size( nnc_info2->lgr_index_map ) ); int lgr_nr = 0; while (true) { nnc_vector_type * vector1 = nnc_info_get_vector( nnc_info1 , lgr_nr ); nnc_vector_type * vector2 = nnc_info_get_vector( nnc_info2 , lgr_nr ); if (!nnc_vector_equal(vector1 , vector2)) return false; lgr_nr++; if (lgr_nr > max_lgr_nr) return true; } } else { if (int_vector_size( nnc_info1->lgr_index_map ) == int_vector_size( nnc_info2->lgr_index_map )) return true; else return false; } } }
static void ecl_sum_data_build_index( ecl_sum_data_type * sum_data ) { /* Clear the existing index (if any): */ ecl_sum_data_clear_index( sum_data ); /* Sort the internal storage vector after sim_time. */ vector_sort( sum_data->data , cmp_ministep ); /* Identify various global first and last values. */ { const ecl_sum_tstep_type * first_ministep = ecl_sum_data_iget_ministep( sum_data , 0 ); sum_data->first_ministep = ecl_sum_tstep_get_ministep( first_ministep ); /* In most cases the days_start and data_start_time will agree with the global simulation start; however in the case where we have loaded a summary case from a restarted simulation where the case we have restarted from is not available - then there will be a difference. */ sum_data->days_start = ecl_sum_tstep_get_sim_days( first_ministep ); sum_data->data_start_time = ecl_sum_tstep_get_sim_time( first_ministep ); } ecl_sum_data_update_end_info( sum_data ); /* Build up the report -> ministep mapping. */ { int internal_index; for (internal_index = 0; internal_index < vector_get_size( sum_data->data ); internal_index++) { const ecl_sum_tstep_type * ministep = ecl_sum_data_iget_ministep( sum_data , internal_index ); int report_step = ecl_sum_tstep_get_report(ministep); /* Indexing internal_index - report_step */ { int current_first_index = int_vector_safe_iget( sum_data->report_first_index , report_step ); if (current_first_index < 0) /* i.e. currently not set. */ int_vector_iset( sum_data->report_first_index , report_step , internal_index); else if (internal_index < current_first_index) int_vector_iset( sum_data->report_first_index , report_step , internal_index); } { int current_last_index = int_vector_safe_iget( sum_data->report_last_index , report_step ); if (current_last_index < 0) int_vector_iset( sum_data->report_last_index , report_step , internal_index); else if (internal_index > current_last_index) int_vector_iset( sum_data->report_last_index , report_step , internal_index); } sum_data->first_report_step = util_int_min( sum_data->first_report_step , report_step ); sum_data->last_report_step = util_int_max( sum_data->last_report_step , report_step ); } } sum_data->index_valid = true; }
bool layer_cell_contact( const layer_type * layer , int i1 , int j1 , int i2 , int j2) { layer_assert_cell_index( layer , i1 , j1 ); layer_assert_cell_index( layer , i2 , j2 ); { if ((abs(i1 - i2) == 1) && (j1 == j2)) { int i = util_int_max( i1,i2 ); const cell_type * cell = layer_iget_cell( layer , i , j1 ); return !cell->left_barrier; } if ((i1 == i2) && (abs(j1 - j2) == 1)) { int j = util_int_max( j1 , j2 ); const cell_type * cell = layer_iget_cell( layer , i1 , j ); return !cell->bottom_barrier; } return false; } }
static void menu_display(const menu_type * menu) { int i; int length = strlen(menu->title); for (i = 0; i < vector_get_size(menu->items); i++) { const menu_item_type * item = vector_iget_const( menu->items , i); if(!item->helptext) length = util_int_max(length , item->label_length); if(item->helptext) length = util_int_max(length , 60); /* Hardcoded length for helptext*/ } printf("\n"); __print_line(length + 10 , 0); printf("| "); util_fprintf_string(menu->title , length + 6 , center_pad , stdout); printf(" |\n"); __print_line(length + 10 , 1); for (i=0; i < vector_get_size(menu->items); i++) { const menu_item_type * item = vector_iget_const( menu->items , i); if (item->separator) __print_sep(length + 6); else if (item->helptext) __print_helptext(item->label,length); else { printf("| %c: ", menu_item_get_key( item )); util_fprintf_string(item->label , length + 3 , right_pad , stdout); printf(" |\n"); } } __print_sep(length + 6); printf("| %c: ",menu->quit_keys[0]); util_fprintf_string(menu->quit_label , length + 3 , right_pad , stdout); printf(" |\n"); __print_line(length + 10 , 2); printf("\n"); }
void util_abort(const char * fmt , ...) { pthread_mutex_lock( &__abort_mutex ); /* Abort before unlock() */ { va_list ap; va_start(ap , fmt); printf("\n\n"); fprintf(stderr,"\n\n"); vfprintf(stderr , fmt , ap); va_end(ap); /* The backtrace is based on calling the external program addr2line; the call is based on util_fork_exec() which is currently only available on POSIX. */ const bool include_backtrace = true; if (include_backtrace) { const int max_bt = 50; char *executable; void *array[max_bt]; char **strings; char ** func_list; char ** file_line_list; int max_func_length = 0; int size,i; if (__abort_program_message != NULL) { fprintf(stderr,"--------------------------------------------------------------------------------\n"); fprintf(stderr,"%s",__abort_program_message); fprintf(stderr,"--------------------------------------------------------------------------------\n"); } fprintf(stderr,"\n"); fprintf(stderr,"****************************************************************************\n"); fprintf(stderr,"** **\n"); fprintf(stderr,"** A fatal error occured, and we have to abort. **\n"); fprintf(stderr,"** **\n"); fprintf(stderr,"** We now *try* to provide a backtrace, which would be very useful **\n"); fprintf(stderr,"** when debugging. The process of making a (human readable) backtrace **\n"); fprintf(stderr,"** is quite complex, among other things it involves several calls to the **\n"); fprintf(stderr,"** external program addr2line. We have arrived here because the program **\n"); fprintf(stderr,"** state is already quite broken, so the backtrace might be (seriously) **\n"); fprintf(stderr,"** broken as well. **\n"); fprintf(stderr,"** **\n"); fprintf(stderr,"****************************************************************************\n"); size = backtrace(array , max_bt); strings = backtrace_symbols(array , size); executable = util_bt_alloc_current_executable(strings[0]); if (executable != NULL) { fprintf(stderr,"Current executable : %s \n",executable); func_list = util_calloc(size , sizeof * func_list ); file_line_list = util_calloc(size , sizeof * file_line_list ); for (i=0; i < size; i++) { util_addr2line_lookup(executable , strings[i] , &func_list[i] , &file_line_list[i]); max_func_length = util_int_max(max_func_length , strlen(func_list[i])); } { char string_fmt[64]; sprintf(string_fmt, " #%s02d %s-%ds(..) in %ss \n" , "%" , "%" , max_func_length , "%"); fprintf(stderr , "--------------------------------------------------------------------------------\n"); for (i=0; i < size; i++) { int line_nr; if (util_sscanf_int(file_line_list[i] , &line_nr)) fprintf(stderr, string_fmt , i , func_list[i], file_line_list[i]); else fprintf(stderr, string_fmt , i , func_list[i], file_line_list[i]); } fprintf(stderr , "--------------------------------------------------------------------------------\n"); util_free_stringlist(func_list , size); util_free_stringlist(file_line_list , size); } } else fprintf(stderr,"Could not determine executable file for:%s - no backtrace. \n",strings[0]); free(strings); util_safe_free(executable); } if (getenv("UTIL_ABORT") != NULL) { fprintf(stderr , "Aborting ... \n"); abort(); } else { fprintf(stderr , "Exiting ... \n"); exit(1); } // Would have preferred abort() here - but that comes in conflict with the SIGABRT signal. } pthread_mutex_unlock( &__abort_mutex ); }
void matrix_dgemm(matrix_type *C , const matrix_type *A , const matrix_type * B , bool transA, bool transB , double alpha , double beta) { int m = matrix_get_rows( C ); int n = matrix_get_columns( C ); int lda = matrix_get_column_stride( A ); int ldb = matrix_get_column_stride( B ); int ldc = matrix_get_column_stride( C ); char transA_c; char transB_c; int k , innerA, innerB , outerA , outerB; if (transA) k = matrix_get_rows( A ); else k = matrix_get_columns( A ); if (transA) { innerA = matrix_get_rows(A); outerA = matrix_get_columns(A); transA_c = 'T'; } else { innerA = matrix_get_columns(A); outerA = matrix_get_rows(A); transA_c = 'N'; } if (transB) { innerB = matrix_get_columns( B ); outerB = matrix_get_rows( B ); transB_c = 'T'; } else { transB_c = 'N'; innerB = matrix_get_rows( B ); outerB = matrix_get_columns( B ); } /* This is the dimension check which must pass: -------------------------------------------------- A | B | Columns(A) = Rows(B) Trans(A) | Trans(B) | Rows(A) = Columns(B) A | Trans(B) | Columns(A) = Columns(B) Trans(A) | B | Rows(A) = Rows(B) -------------------------------------------------- -------------------------------------------------- A | Rows(A) = Rows(C) Trans(A) | Columns(A) = Rows(C) B | Columns(B) = Columns(C) Trans(B) | Rows(B) = Columns(B) -------------------------------------------------- */ if (innerA != innerB) { dgemm_debug(C,A,B,transA , transB); util_abort("%s: matrix size mismatch between A and B \n", __func__); } if (outerA != matrix_get_rows( C )) { dgemm_debug(C,A,B,transA , transB); printf("outerA:%d rows(C):%d \n",outerA , matrix_get_rows( C )); util_abort("%s: matrix size mismatch between A and C \n",__func__); } if (outerB != matrix_get_columns( C )) { dgemm_debug(C,A,B,transA , transB); util_abort("%s: matrix size mismatch between B and C \n",__func__); } if (!ldc >= util_int_max(1 , m)) { dgemm_debug(C,A,B,transA , transB); fprintf(stderr,"Tried to capture blas message: \"** On entry to DGEMM parameter 13 had an illegal value\"\n"); fprintf(stderr,"m:%d ldc:%d ldc should be >= max(1,%d) \n",m,ldc,m); util_abort("%s: invalid value for ldc\n",__func__); } dgemm_(&transA_c , // 1 &transB_c , // 2 &m , // 3 &n , // 4 &k , // 5 &alpha , // 6 matrix_get_data( A ) , // 7 &lda , // 8 matrix_get_data( B ) , // 9 &ldb , // 10 &beta , // 11 matrix_get_data( C ) , // 12 &ldc); // 13 }
double statistics_empirical_quantile__( const double_vector_type * data , double quantile ) { if ((quantile < 0) || (quantile > 1.0)) util_abort("%s: quantile must be in [0,1] \n",__func__); { const int size = (double_vector_size( data ) - 1); if (double_vector_iget( data , 0) == double_vector_iget( data , size)) /* All elements are equal - and it is impossible to find a meaingful quantile, we just return "the value". */ return double_vector_iget( data, 0 ); else { double value; double lower_value; double upper_value; double real_index; double upper_quantile; double lower_quantile; int lower_index; int upper_index; real_index = quantile * size; lower_index = floor( real_index ); upper_index = ceil( real_index ); upper_value = double_vector_iget( data , upper_index ); lower_value = double_vector_iget( data , lower_index ); /* Will iterate in this loop until we have found upper_value != lower_value. As long as we know that now all elements are equal (the first test), this is guaranteed to succeed, but of course the estimate will not be very meaningful if the sample consist of a significant number of equal values. */ while (true) { /*1: Try to shift the upper index up. */ if (upper_value == lower_value) { upper_index = util_int_min( size , upper_index + 1); upper_value = double_vector_iget( data , upper_index ); } else break; /*2: Try to shift the lower index down. */ if (upper_value == lower_value) { lower_index = util_int_max( 0 , lower_index - 1); lower_value = double_vector_iget( data , lower_index ); } else break; } upper_quantile = upper_index * 1.0 / size; lower_quantile = lower_index * 1.0 / size; /* Linear interpolation: */ { double a = (upper_value - lower_value) / (upper_quantile - lower_quantile); value = lower_value + a*(quantile - lower_quantile); return value; } } } }
void matrix_dgesvd(dgesvd_vector_enum jobu , dgesvd_vector_enum jobvt , matrix_type * A , double * S , matrix_type * U , matrix_type * VT) { char _jobu = dgesvd_get_vector_job( jobu ); char _jobvt = dgesvd_get_vector_job( jobvt ); int m = matrix_get_rows( A ); int n = matrix_get_columns( A ); int lda = matrix_get_column_stride( A ); int ldu, ldvt; double * VT_data , *U_data; int info = 0; int min_worksize = util_int_max(3* util_int_min(m , n) + util_int_max(m , n) , 5 * util_int_min(m , n)); double * work; int worksize; if (U == NULL) { ldu = 1; U_data = NULL; if (jobu != DGESVD_NONE) util_abort("%s: internal error \n",__func__); } else { ldu = matrix_get_column_stride( U ); U_data = matrix_get_data( U ); if (jobu == DGESVD_NONE) util_abort("%s: internal error \n",__func__); } if (VT == NULL) { ldvt = 1; /* Will fail if set to zero */ VT_data = NULL; if (jobvt != DGESVD_NONE) util_abort("%s: internal error \n",__func__); } else { ldvt = matrix_get_column_stride( VT ); VT_data = matrix_get_data( VT ); if (jobvt == DGESVD_NONE) util_abort("%s: internal error \n",__func__); } /* Query the routine for optimal worksize. */ work = util_calloc( 1 , sizeof * work ); worksize = -1; dgesvd_(&_jobu , /* 1 */ &_jobvt , /* 2 */ &m , /* 3 */ &n , /* 4 */ matrix_get_data( A ) , /* 5 */ &lda , /* 6 */ S , /* 7 */ U_data , /* 8 */ &ldu , /* 9 */ VT_data , /* 10 */ &ldvt , /* 11 */ work , /* 12 */ &worksize , /* 13 */ &info); /* 14 */ /* Try to allocate optimal worksize. */ worksize = (int) work[0]; work = realloc( work , sizeof * work * worksize ); if (work == NULL) { /* Could not allocate optimal worksize - settle for the minimum. This can not fail. */ worksize = min_worksize; work = util_calloc( worksize , sizeof * work ); } dgesvd_(&_jobu , &_jobvt , &m , &n , matrix_get_data( A ) , &lda , S , U_data , &ldu , VT_data , &ldvt , work , &worksize , &info); free( work ); }