Exemplo n.º 1
0
/**
   It is a bug to call this before some function has set the size. 
*/
void gen_data_realloc_data(gen_data_type * gen_data) {
  int byte_size  = gen_data_config_get_byte_size(gen_data->config , gen_data->current_report_step );
  gen_data->data = util_realloc(gen_data->data , byte_size );
}
Exemplo n.º 2
0
int  matrix_dsyevx(bool             compute_eig_vectors , 
                   dsyevx_eig_enum  which_values        , /* DSYEVX | DSYEVX_VALUE_INTERVAL | DSYEVX_INDEX_INTERVAL */ 
                   dsyevx_uplo_enum uplo, 
                   matrix_type    * A ,                   /* The input matrix - is modified by the dsyevx() function. */ 
                   double VL          ,                   /* Lower limit when using DSYEVX_VALUE_INTERVAL */
                   double VU          ,                   /* Upper limit when using DSYEVX_VALUE_INTERVAL */
                   int    IL          ,                   /* Lower index when using DSYEVX_INDEX_INTERVAL */ 
                   int    IU          ,                   /* Upper index when using DSYEVX_INDEX_INTERVAL */
                   double *eig_values ,                   /* The calcualated eigenvalues                  */
                   matrix_type * Z    ) {                 /* The eigenvectors as columns vectors          */ 

  int lda  = matrix_get_column_stride( A );
  int n    = matrix_get_rows( A );
  char jobz;
  char range;
  char uplo_c;
  
  if (compute_eig_vectors)
    jobz = 'V';
  else
    jobz = 'N';
  
  switch(which_values) {
  case(DSYEVX_ALL):
    range = 'A';
    break;
  case(DSYEVX_VALUE_INTERVAL):
    range = 'V';
    break;
  case(DSYEVX_INDEX_INTERVAL):
    range = 'I';
    break;
  default:
    util_abort("%s: internal error \n",__func__);
  }

  if (uplo == DSYEVX_AUPPER)
    uplo_c = 'U';
  else if (uplo == DSYEVX_ALOWER)
    uplo_c = 'L';
  else
    util_abort("%s: internal error \n",__func__);
  
  
  if (!matrix_is_quadratic( A ))
    util_abort("%s: matrix A must be quadratic \n",__func__);
  
  {
    int      num_eigenvalues , ldz, info , worksize;
    int    * ifail = util_calloc( n     , sizeof * ifail );
    int    * iwork = util_calloc( 5 * n , sizeof * iwork );
    double * work  = util_calloc( 1     , sizeof * work  );
    double * z_data;
    double   abstol = 0.0; /* SHopuld */


    if (compute_eig_vectors) {
      ldz    = matrix_get_column_stride( Z );
      z_data = matrix_get_data( Z );
    } else {
      /* In this case we can accept that Z == NULL */
      ldz    = 1;
      z_data = NULL;
    }
    
    /* First call to determine optimal worksize. */
    worksize = -1;
    info     = 0;
    dsyevx_( &jobz,                /*  1 */
             &range,               /*  2 */
             &uplo_c,              /*  3 */
             &n,                   /*  4 */   
             matrix_get_data( A ), /*  5 */
             &lda ,                /*  6 */
             &VL ,                 /*  7 */
             &VU ,                 /*  8 */
             &IL ,                 /*  9 */
             &IU ,                 /* 10 */   
             &abstol ,             /* 11 */
             &num_eigenvalues ,    /* 12 */
             eig_values ,          /* 13 */
             z_data ,              /* 14 */
             &ldz ,                /* 15 */
             work ,                /* 16 */
             &worksize ,           /* 17 */
             iwork ,               /* 18 */
             ifail ,               /* 19 */
             &info);               /* 20 */
    
    
    worksize = (int) work[0];
    {
      double * tmp = realloc(work , sizeof * work * worksize );
      if (tmp == NULL) {
        /* 
           OK - we could not get the optimal worksize, 
           try again with the minimum.
        */
        worksize = 8 * n;
        work = util_realloc(work , sizeof * work * worksize );
      } else
        work = tmp; /* The request for optimal worksize succeeded */
    }
    /* Second call: do the job */
    info     = 0;
    dsyevx_( &jobz,
             &range,
             &uplo_c,
             &n,
             matrix_get_data( A ),
             &lda , 
             &VL ,
             &VU , 
             &IL , 
             &IU , 
             &abstol , 
             &num_eigenvalues , 
             eig_values ,  
             z_data , 
             &ldz , 
             work , 
             &worksize , 
             iwork , 
             ifail , 
             &info);

    free( ifail );
    free( work );
    free( iwork );
    return num_eigenvalues;
  }
}
Exemplo n.º 3
0
static char * fscanf_alloc_grdecl_data( const char * header , bool strict , ecl_type_enum ecl_type , int * kw_size , FILE * stream ) {
  char newline        = '\n';
  bool atEOF          = false;
  int init_size       = 32;
  int buffer_size     = 64;
  int data_index      = 0;
  int sizeof_ctype    = ecl_util_get_sizeof_ctype( ecl_type );
  int data_size       = init_size;
  char * buffer       = util_calloc( (buffer_size + 1) , sizeof * buffer      );
  char * data         = util_calloc( sizeof_ctype * data_size , sizeof * data );

  while (true) {
    if (fscanf(stream , "%32s" , buffer) == 1) {
      if (strcmp(buffer , ECL_COMMENT_STRING) == 0) {
        // We have read a comment marker - just read up to the end of line.
        char c;
        while (true) {
          c = fgetc( stream );
          if (c == newline)
            break;
          if (c == EOF) {
            atEOF = true;
            break;
          }
        }
      } else if (strcmp(buffer , ECL_DATA_TERMINATION) == 0) 
        break;
      else {
        // We have read a valid input string; scan numerical input values from it.
        // The multiplier algorithm will fail hard if there are spaces on either side
        // of the '*'.

        int multiplier;
        void * value_ptr = NULL;
        bool   char_input = false;
        
        if (ecl_type == ECL_INT_TYPE) {
          int value;

          if (sscanf(buffer , "%d*%d" , &multiplier , &value) == 2) 
            {}
          else if (sscanf( buffer , "%d" , &value) == 1) 
            multiplier = 1;
          else {
            char_input = true;
            if (strict)
              util_abort("%s: Malformed content:\"%s\" when reading keyword:%s \n",__func__ , buffer , header);
          }
          
          value_ptr = &value;
        } else if (ecl_type == ECL_FLOAT_TYPE) {
          float value;

          if (sscanf(buffer , "%d*%g" , &multiplier , &value) == 2) 
            {}
          else if (sscanf( buffer , "%g" , &value) == 1) 
            multiplier = 1;
          else {
            char_input = true;
            if (strict)
              util_abort("%s: Malformed content:\"%s\" when reading keyword:%s \n",__func__ , buffer , header);
          }

          value_ptr = &value;
        } else if (ecl_type == ECL_DOUBLE_TYPE) {
          double value;

          if (sscanf(buffer , "%d*%lg" , &multiplier , &value) == 2) 
            {}
          else if (sscanf( buffer , "%lg" , &value) == 1) 
            multiplier = 1;
          else {
            char_input = true;
            if (strict)
              util_abort("%s: Malformed content:\"%s\" when reading keyword:%s \n",__func__ , buffer , header);
          }
          
          value_ptr = &value;
        } else 
          util_abort("%s: sorry type:%s not supported \n",__func__ , ecl_util_get_type_name(ecl_type));
        
        
        if (char_input)
          fprintf(stderr,"Warning: character string: \'%s\' ignored when reading keyword:%s \n",buffer , header);
        else {
          if (data_index + multiplier >= data_size) {
            data_size  = 2*(data_index + multiplier);
            data       = util_realloc( data , sizeof_ctype * data_size * sizeof * data);
          }
          
          iset_range( data , data_index , sizeof_ctype , value_ptr , multiplier );
          data_index += multiplier;
        }

      }
      if (atEOF)
        break;
    } else 
      break;
  }
  free( buffer );
  *kw_size = data_index;
  data = util_realloc( data , sizeof_ctype * data_index * sizeof * data );
  return data;
}