예제 #1
0
config_root_path_type * config_root_path_alloc( const char * input_path ) {
  if (input_path == NULL || util_is_directory( input_path )) {
    config_root_path_type * root_path = util_malloc( sizeof * root_path );
    {
      char * cwd = util_alloc_cwd();
      
      root_path->input_path = util_alloc_string_copy( input_path );
      if (input_path == NULL) {
        root_path->rel_path = NULL;
        root_path->abs_path = util_alloc_string_copy( cwd );
      } else {
        if (util_is_abs_path( input_path )) {
          root_path->abs_path = util_alloc_string_copy( input_path );
          root_path->rel_path = util_alloc_rel_path( cwd , root_path->abs_path);
        } else {
          root_path->rel_path = util_alloc_string_copy( input_path );
          {
            char * abs_path = util_alloc_filename( cwd , input_path , NULL );
            root_path->abs_path = util_alloc_realpath( abs_path );
            free( abs_path );
          }
        }
      }
      free( cwd );
    }
    return root_path;
  } else 
    return NULL;
}
예제 #2
0
char * util_alloc_tmp_file(const char * path, const char * prefix , bool include_pid ) {
  // Should be reimplemented to use mkstemp() 
  const int pid_digits    = 6;
  const int random_digits = 6;
  const int random_max    = 1000000;

#ifdef HAVE_PID_T  
  const int pid_max     = 1000000;
  pid_t  pid            = getpid() % pid_max;
#else
  int    pid            = 0;
#endif

  char * file           = util_calloc(strlen(path) + 1 + strlen(prefix) + 1 + pid_digits + 1 + random_digits + 1 , sizeof * file );
  char * tmp_prefix     = util_alloc_string_copy( prefix );
  
  if (!util_is_directory(path))
    util_make_path(path);
  util_string_tr( tmp_prefix ,  UTIL_PATH_SEP_CHAR , '_');  /* removing path seps. */
  
  do {
    long int rand_int = rand() % random_max;
    if (include_pid)
      sprintf(file , "%s%c%s-%d-%ld" , path , UTIL_PATH_SEP_CHAR , tmp_prefix , pid , rand_int);
    else
      sprintf(file , "%s%c%s-%ld" , path , UTIL_PATH_SEP_CHAR , tmp_prefix , rand_int);
  } while (util_file_exists(file));   

  free( tmp_prefix );
  return file;
}
예제 #3
0
stringlist_type * enkf_main_alloc_caselist( const enkf_main_type * enkf_main ) {
    stringlist_type * case_list = stringlist_alloc_new( );
    {
        const char * ens_path = model_config_get_enspath( enkf_main->model_config );
        DIR * ens_dir = opendir( ens_path );
        if (ens_dir != NULL) {
            int ens_fd = dirfd( ens_dir );
            if (ens_fd != -1) {
                struct dirent * dp;
                do {
                    dp = readdir( ens_dir );
                    if (dp != NULL) {
                        if (!(util_string_equal( dp->d_name , ".") || util_string_equal(dp->d_name , ".."))) {
                            if (!util_string_equal( dp->d_name , CURRENT_CASE_FILE)) {
                                char * full_path = util_alloc_filename( ens_path , dp->d_name , NULL);
                                if (util_is_directory( full_path ))
                                    stringlist_append_copy( case_list , dp->d_name );
                                free( full_path);
                            }
                        }
                    }
                } while (dp != NULL);
            }
        }
        closedir( ens_dir );
    }
    return case_list;
}
예제 #4
0
void test_create_case_job(ert_test_context_type * test_context, const char * job_name , const char * job_file) {
  stringlist_type * args = stringlist_alloc_new();
  stringlist_append_copy( args , "newly_created_case");
  test_assert_true( ert_test_context_install_workflow_job( test_context , job_name , job_file ));
  test_assert_true( ert_test_context_run_worklow_job( test_context , job_name , args) );

  char * new_case = util_alloc_filename( "storage" , "newly_created_case" , NULL);
  test_assert_true(util_is_directory(new_case));
  free(new_case);

  stringlist_free( args );
}
예제 #5
0
void util_make_path(const char *_path) {
  char *active_path;
  char *path = (char *) _path;
  int current_pos = 0;
  
  if (!util_is_directory(path)) {
    int i = 0;
    active_path = util_calloc(strlen(path) + 1 , sizeof * active_path );
    do {
      int n = strcspn(path , UTIL_PATH_SEP_STRING);
      if (n < strlen(path))
        n += 1;
      path += n;
      i++;
      strncpy(active_path , _path , n + current_pos); 
      active_path[n+current_pos] = '\0';
      current_pos += n; 
      
      if (!util_is_directory(active_path)) {
        if (util_mkdir(active_path) != 0) { 
          bool fail = false;
          switch (errno) {
          case(EEXIST):
            if (util_is_directory(active_path))
              fail = false;
            break;
          default:
            fail = true;
            break;
          }
          if (fail)
            util_abort("%s: failed to make directory:%s - aborting\n: %s(%d) \n",__func__ , active_path , strerror(errno), errno);
        }
      }
      
    } while (strlen(active_path) < strlen(_path));
    free(active_path);
  }
}
예제 #6
0
void test_create() {
  char * cwd0 = util_alloc_cwd();
  char * cwd1;
  {
    ERT::TestArea ta("test/area");
    cwd1 = util_alloc_cwd();
    test_assert_string_not_equal( cwd0 , cwd1 );
    test_assert_string_equal( cwd1 , ta.getCwd().c_str());
    test_assert_string_equal( cwd0 , ta.getOriginalCwd( ).c_str() );
  }
  test_assert_false( util_is_directory(cwd1) );
  free( cwd1 );
  free( cwd0 );
}
예제 #7
0
int main(int argc , char ** argv) {
  enkf_main_install_SIGNALS();
  const char * root_path   = argv[1];
  const char * config_file = argv[2];
  const char * init_file   = argv[3];
  const char * forward_init_string = argv[4];
  
  test_work_area_type * work_area = test_work_area_alloc(config_file );
  test_work_area_copy_directory_content( work_area , root_path );
  test_work_area_install_file( work_area , init_file );
  test_work_area_set_store(work_area, true); 
  
  bool strict = true;
  enkf_main_type * enkf_main = enkf_main_bootstrap( NULL , config_file , strict , true );
  enkf_fs_type * init_fs = enkf_main_get_fs(enkf_main);
  enkf_state_type * state = enkf_main_iget_state( enkf_main , 0 );
  run_arg_type * run_arg = run_arg_alloc_ENSEMBLE_EXPERIMENT( init_fs , 0 ,0 , "simulations/run0");
  enkf_node_type * field_node = enkf_state_get_node( state , "PORO" );
  
  bool forward_init;
  test_assert_true( util_sscanf_bool( forward_init_string , &forward_init));
  test_assert_bool_equal( enkf_node_use_forward_init( field_node ) , forward_init );
  test_assert_bool_equal( forward_init, ensemble_config_have_forward_init( enkf_main_get_ensemble_config( enkf_main )));
  
  util_clear_directory( "Storage" , true , true );
  
  create_runpath( enkf_main );
  test_assert_true( util_is_directory( "simulations/run0" ));

  if (forward_init)
    util_copy_file( init_file , "simulations/run0/petro.grdecl");

  {
    bool_vector_type * iactive = bool_vector_alloc( enkf_main_get_ensemble_size(enkf_main) , true);
    int error = 0;
    stringlist_type * msg_list = stringlist_alloc_new();  
    enkf_state_load_from_forward_model( state , run_arg ,  &error , false , msg_list );
    stringlist_free( msg_list );
    bool_vector_free( iactive );
    test_assert_int_equal(error, 0); 
  }

  test_assert_true(check_original_exported_data_equal(field_node));

  run_arg_free( run_arg );
  enkf_main_free(enkf_main);    
  test_work_area_free(work_area); 
}
예제 #8
0
void create_test_area(const char * test_name , bool store) {
  char * pre_cwd = util_alloc_cwd();
  test_work_area_type * work_area = test_work_area_alloc( test_name , store);
  char * work_path = util_alloc_string_copy( test_work_area_get_cwd( work_area ));
  
  test_assert_true( util_is_directory( work_path ));
  test_work_area_free( work_area );
  test_assert_bool_equal( store , util_entry_exists( work_path ));
  
  {
    char * post_cwd = util_alloc_cwd();
    test_assert_string_equal( pre_cwd , post_cwd );
    free( post_cwd );
  }
  free( pre_cwd );
  free( work_path );
}
예제 #9
0
void test_copy_parent_directory( const char * path ) {
  test_work_area_type * work_area = test_work_area_alloc( "copy-parent-directory" );
  char * parent_path;

  {
    char * full_path = util_alloc_abs_path( path );
    util_alloc_file_components( path , &parent_path , NULL , NULL);
    free( full_path );
  }

  test_assert_false( test_work_area_copy_parent_directory( work_area , "Does/not/exist") );
  test_assert_true( test_work_area_copy_parent_directory( work_area , path ) );

  test_assert_true( util_entry_exists( parent_path ));
  test_assert_true( util_is_directory( parent_path ));

  test_work_area_free( work_area );
  free( parent_path );
}
예제 #10
0
EclipseIO::EclipseIO( const EclipseState& es,
                      EclipseGrid grid,
                      const Schedule& schedule,
                      const SummaryConfig& summary_config)
    : impl( new Impl( es, std::move( grid ), schedule , summary_config) )
{
    if( !this->impl->output_enabled )
        return;
    {
        const auto& outputDir = this->impl->outputDir;

        // make sure that the output directory exists, if not try to create it
        if ( !util_entry_exists( outputDir.c_str() ) ) {
            util_make_path( outputDir.c_str() );
        }

        if( !util_is_directory( outputDir.c_str() ) ) {
            throw std::runtime_error( "The path specified as output directory '"
                                      + outputDir + "' is not a directory");
        }
    }
}
예제 #11
0
int main(int argc , char ** argv) {
  enkf_main_install_SIGNALS();
  const char * root_path = argv[1];
  const char * config_file = argv[2];
  const char * forward_init_string = argv[3];
  test_work_area_type * work_area = test_work_area_alloc(config_file );
  test_work_area_copy_directory_content( work_area , root_path );
  {  
    bool forward_init;
    bool strict = true;
    enkf_main_type * enkf_main;
    
    test_assert_true( util_sscanf_bool( forward_init_string , &forward_init));
    
    util_clear_directory( "Storage" , true , true );
    enkf_main = enkf_main_bootstrap( NULL , config_file , strict , true );
    {
      enkf_state_type * state   = enkf_main_iget_state( enkf_main , 0 );
      enkf_node_type * gen_kw_node = enkf_state_get_node( state , "MULTFLT" );
      {
        const enkf_config_node_type * gen_kw_config_node = enkf_node_get_config( gen_kw_node );
        char * init_file1 = enkf_config_node_alloc_initfile( gen_kw_config_node , NULL , 0);
        char * init_file2 = enkf_config_node_alloc_initfile( gen_kw_config_node , "/tmp", 0);
        
        test_assert_bool_equal( enkf_config_node_use_forward_init( gen_kw_config_node ) , forward_init );
        test_assert_string_equal( init_file1 , "MULTFLT_INIT");
        test_assert_string_equal( init_file2 , "/tmp/MULTFLT_INIT");
        
        free( init_file1 );
        free( init_file2 );
      }
      
      test_assert_bool_equal( enkf_node_use_forward_init( gen_kw_node ) , forward_init );
      if (forward_init)
        test_assert_bool_not_equal( enkf_node_initialize( gen_kw_node , 0 , enkf_state_get_rng( state )) , forward_init);
      // else hard_failure()
    }
    test_assert_bool_equal( forward_init, ensemble_config_have_forward_init( enkf_main_get_ensemble_config( enkf_main )));
    
    if (forward_init) {
      enkf_state_type * state   = enkf_main_iget_state( enkf_main , 0 );
      enkf_fs_type * fs = enkf_main_get_fs( enkf_main );
      run_arg_type * run_arg = run_arg_alloc_ENSEMBLE_EXPERIMENT( fs , 0 , 0 , "simulations/run0");
      enkf_node_type * gen_kw_node = enkf_state_get_node( state , "MULTFLT" );
      node_id_type node_id = {.report_step = 0 ,  
                              .iens = 0,
                              .state = ANALYZED };
      
      create_runpath( enkf_main );
      test_assert_true( util_is_directory( "simulations/run0" ));
      
      {
        int error = 0;
        stringlist_type * msg_list = stringlist_alloc_new();
        bool_vector_type * iactive = bool_vector_alloc( enkf_main_get_ensemble_size( enkf_main ) , true);
        
        test_assert_false( enkf_node_has_data( gen_kw_node , fs, node_id ));
        util_unlink_existing( "simulations/run0/MULTFLT_INIT" );
        

        test_assert_false( enkf_node_forward_init( gen_kw_node , "simulations/run0" , 0 ));
        enkf_state_forward_init( state , run_arg ,  &error );
        test_assert_true(LOAD_FAILURE & error);
        
        error = 0;
        {
          enkf_fs_type * fs = enkf_main_get_fs( enkf_main );
          state_map_type * state_map = enkf_fs_get_state_map(fs);
          state_map_iset(state_map , 0 , STATE_INITIALIZED);
        }
        enkf_state_load_from_forward_model( state , run_arg ,  &error , false , msg_list );
        stringlist_free( msg_list );
        bool_vector_free( iactive );
        test_assert_true(LOAD_FAILURE & error);
      }
      
      
      
      {
        FILE * stream = util_fopen("simulations/run0/MULTFLT_INIT" , "w");
        fprintf(stream , "123456.0\n" );
        fclose( stream );
      }
      
      {
        int error = 0;
        stringlist_type * msg_list = stringlist_alloc_new();

        test_assert_true( enkf_node_forward_init( gen_kw_node , "simulations/run0" , 0 ));
        enkf_state_forward_init( state , run_arg ,  &error );
        test_assert_int_equal(0, error);
        enkf_state_load_from_forward_model( state , run_arg , &error , false , msg_list );
       
        stringlist_free( msg_list );
        test_assert_int_equal(0, error);

        {
          double value;
          test_assert_true( enkf_node_user_get( gen_kw_node , fs , "MULTFLT" , node_id , &value)); 
          test_assert_double_equal( 123456.0 , value);
        }
      }

      test_assert_true( util_is_file ("simulations/run0/parameters.txt")); //Export of gen kw params

      util_clear_directory( "simulations" , true , true );
      create_runpath( enkf_main );
      test_assert_true( util_is_directory( "simulations/run0" ));
      test_assert_true( util_is_file( "simulations/run0/MULTFLT.INC" ));
      {
        FILE * stream = util_fopen("simulations/run0/MULTFLT.INC" , "r");
        double value;
        fscanf(stream , "%lg" , &value);
        fclose( stream );
        test_assert_double_equal( 123456.0 , value);
      }
      util_clear_directory( "simulations" , true , true );
      run_arg_free( run_arg );
    }
    enkf_main_free( enkf_main );
  }
  test_work_area_free( work_area );
}
int main(int argc , char ** argv) {
  enkf_main_install_SIGNALS();
  {
    const char * config_file = argv[1];
    const char * init_file = argv[2];
    const char * forward_init_string = argv[3];
    bool forward_init;
    bool strict = true;
    enkf_main_type * enkf_main;

    test_assert_true( util_sscanf_bool( forward_init_string , &forward_init));

    util_clear_directory( "/tmp/Storage" , true , true );
    enkf_main = enkf_main_bootstrap( NULL , config_file , strict , true );
    {
      enkf_state_type * state   = enkf_main_iget_state( enkf_main , 0 );
      enkf_node_type * surface_node = enkf_state_get_node( state , "SURFACE" );
      {
        const enkf_config_node_type * surface_config_node = enkf_node_get_config( surface_node );
        char * init_file1 = enkf_config_node_alloc_initfile( surface_config_node , NULL , 0);
        char * init_file2 = enkf_config_node_alloc_initfile( surface_config_node , "/tmp", 0);

        test_assert_bool_equal( enkf_config_node_use_forward_init( surface_config_node ) , forward_init );
        test_assert_string_equal( init_file1 , "Surface.irap");
        test_assert_string_equal( init_file2 , "/tmp/Surface.irap");

        free( init_file1 );
        free( init_file2 );
      }
  
      test_assert_bool_equal( enkf_node_use_forward_init( surface_node ) , forward_init );
      if (forward_init)
        test_assert_bool_not_equal( enkf_node_initialize( surface_node , 0 , enkf_state_get_rng( state )) , forward_init);
      // else hard_failure()
    }
    test_assert_bool_equal( forward_init, ensemble_config_have_forward_init( enkf_main_get_ensemble_config( enkf_main )));
    
    if (forward_init) {
      enkf_state_type * state   = enkf_main_iget_state( enkf_main , 0 );
      enkf_fs_type * fs = enkf_main_get_fs( enkf_main );
      enkf_node_type * surface_node = enkf_state_get_node( state , "SURFACE" );
      node_id_type node_id = {.report_step = 0 ,  
                              .iens = 0,
                              .state = ANALYZED };

      create_runpath( enkf_main );
      test_assert_true( util_is_directory( "/tmp/simulations/run0" ));
      
      {
        bool loadOK = true;
        stringlist_type * msg_list = stringlist_alloc_new();

        {
          run_mode_type run_mode = ENSEMBLE_EXPERIMENT; 
          enkf_main_init_run(enkf_main , run_mode);     /* This is ugly */
        }
        
        
        test_assert_false( enkf_node_has_data( surface_node , fs, node_id ));
        
        util_unlink_existing( "/tmp/simulations/run0/Surface.irap" );
        
        test_assert_false( enkf_node_forward_init( surface_node , "/tmp/simulations/run0" , 0 ));
        enkf_state_forward_init( state , fs , &loadOK );
        test_assert_false( loadOK );

        loadOK = true;
        enkf_state_load_from_forward_model( state , fs , &loadOK , false , msg_list );
        stringlist_free( msg_list );
        test_assert_false( loadOK );
      }
      

      util_copy_file( init_file , "/tmp/simulations/run0/Surface.irap");
      {
        bool loadOK = true;
        stringlist_type * msg_list = stringlist_alloc_new();

        {
          run_mode_type run_mode = ENSEMBLE_EXPERIMENT; 
          enkf_main_init_run(enkf_main , run_mode);     /* This is ugly */
        }
        
        test_assert_true( enkf_node_forward_init( surface_node , "/tmp/simulations/run0" , 0 ));
        enkf_state_forward_init( state , fs , &loadOK );
        test_assert_true( loadOK );
        enkf_state_load_from_forward_model( state , fs , &loadOK , false , msg_list );
        stringlist_free( msg_list );
        test_assert_true( loadOK );

        {
          double value;
          test_assert_true( enkf_node_user_get( surface_node , fs , "0" , node_id , &value)); 
          test_assert_double_equal( 2735.7461 , value);

          test_assert_true( enkf_node_user_get( surface_node , fs , "5" , node_id , &value)); 
          test_assert_double_equal( 2737.0122 , value);
        }
      }
      util_clear_directory( "/tmp/simulations" , true , true );
      create_runpath( enkf_main );
      test_assert_true( util_is_directory( "/tmp/simulations/run0" ));
      test_assert_true( util_is_file( "/tmp/simulations/run0/SURFACE.INC" ));
      test_assert_true( enkf_node_fload( surface_node , "/tmp/simulations/run0/SURFACE.INC"));
      {
        double value;
        test_assert_true( enkf_node_user_get( surface_node , fs , "0" , node_id , &value)); 
        test_assert_double_equal( 2735.7461 , value);
      
        test_assert_true( enkf_node_user_get( surface_node , fs , "5" , node_id , &value)); 
        test_assert_double_equal( 2737.0122 , value);
      }
      util_clear_directory( "/tmp/simulations" , true , true );
    }
    enkf_main_free( enkf_main );
  }
}
예제 #13
0
void enkf_tui_export_scalar2csv(void * arg) {
  enkf_main_type * enkf_main = enkf_main_safe_cast( arg );
  const ensemble_config_type * ensemble_config = enkf_main_get_ensemble_config(enkf_main);
  const enkf_config_node_type * config_node;
  char * user_key, *key_index;

  util_printf_prompt("Scalar to export (KEY:INDEX)" , PROMPT_LEN , '=' , "=> "); user_key = util_alloc_stdin_line();
  config_node = ensemble_config_user_get_node( ensemble_config , user_key , &key_index);
  if (config_node != NULL) {
    int    report_step , first_report, last_report;
    int    iens1 , iens2, iens;
    char * csv_file;

    iens2        = enkf_main_get_ensemble_size( enkf_main ) - 1;
    iens1        = 0;
    first_report = 0;
    last_report  = enkf_main_get_history_length( enkf_main );
    {
      char * path;
      char * prompt = util_alloc_sprintf("File to store \'%s\'", user_key);
      util_printf_prompt(prompt , PROMPT_LEN , '=' , "=> ");
      csv_file = util_alloc_stdin_line();

      util_alloc_file_components( csv_file , &path , NULL , NULL);
      if (path != NULL) {
        if (util_entry_exists( path )) {
          if (!util_is_directory( path )) {
            /* The path component already exists in the filesystem - and it is not a directory - we leave the building. */
            fprintf(stderr,"Sorry: %s already exists - and is not a directory.\n",path);
            free(path);
            free(csv_file);
            free(user_key);
            return ;
          }
        } else {
          /* The path does not exist - we make it. */
          enkf_tui_util_msg("Creating new directory: %s\n" , path);
          util_make_path( path );
        }
      }
      free(prompt);
    }
    {
      /* Seriously manual creation of csv file. */
      enkf_fs_type * fs     = enkf_main_tui_get_fs(enkf_main);
      enkf_node_type * node = enkf_node_alloc( config_node );
      FILE * stream         = util_fopen( csv_file , "w");
      node_id_type node_id;


      /* Header line */
      fprintf(stream , "\"Report step\"");
      for (iens = iens1; iens <= iens2; iens++)
        fprintf(stream , "%s\"%s(%d)\"" , CSV_SEP , user_key , iens);
      fprintf(stream , CSV_NEWLINE);

      for (report_step = first_report; report_step <= last_report; report_step++) {
        fprintf(stream , "%6d" , report_step);
        node_id.report_step = report_step;
        for (iens = iens1; iens <= iens2; iens++) {
          double value;
          char label[32];
          /*
             Have not implemented a choice on forecast/analyzed. Tries
             analyzed first, then forecast.
          */
          node_id.iens = iens;
          sprintf(label , "%03d/%03d" , report_step , iens);

          if (enkf_node_user_get( node , fs , key_index , node_id ,  &value))
            fprintf(stream , "%s%g" , CSV_SEP , value);
          else
            fprintf(stream , "%s%s" , CSV_SEP , CSV_MISSING_VALUE);

        }
        fprintf(stream , CSV_NEWLINE);
      }

      enkf_node_free( node );
      fclose(stream);
    }
  } else
    fprintf(stderr,"Sorry - could not find any nodes with key:%s\n",user_key);

  free(user_key);
}
예제 #14
0
int main(int argc , char ** argv) {
  enkf_main_install_SIGNALS();
  const char * root_path = argv[1];
  const char * config_file = argv[2];
  const char * init_file = argv[3];
  const char * forward_init_string = argv[4];
  test_work_area_type * work_area = test_work_area_alloc(config_file );

  test_work_area_copy_directory_content( work_area , root_path );
  test_work_area_install_file( work_area , init_file );
  {

    bool forward_init;
    bool strict = true;
    enkf_main_type * enkf_main;

    test_assert_true( util_sscanf_bool( forward_init_string , &forward_init));

    util_clear_directory( "Storage" , true , true );
    enkf_main = enkf_main_bootstrap( config_file , strict , true );
    {
      enkf_state_type * state   = enkf_main_iget_state( enkf_main , 0 );
      enkf_node_type * surface_node = enkf_state_get_node( state , "SURFACE" );
      {
        const enkf_config_node_type * surface_config_node = enkf_node_get_config( surface_node );
        char * init_file1 = enkf_config_node_alloc_initfile( surface_config_node , NULL , 0);
        char * init_file2 = enkf_config_node_alloc_initfile( surface_config_node , "/tmp", 0);

        test_assert_bool_equal( enkf_config_node_use_forward_init( surface_config_node ) , forward_init );
        test_assert_string_equal( init_file1 , "Surface.irap");
        test_assert_string_equal( init_file2 , "/tmp/Surface.irap");

        free( init_file1 );
        free( init_file2 );
      }
  
      test_assert_bool_equal( enkf_node_use_forward_init( surface_node ) , forward_init );
      if (forward_init)
        test_assert_bool_not_equal( enkf_node_initialize( surface_node , 0 , enkf_state_get_rng( state )) , forward_init);
      // else hard_failure()
    }
    test_assert_bool_equal( forward_init, ensemble_config_have_forward_init( enkf_main_get_ensemble_config( enkf_main )));
    
    if (forward_init) {
      enkf_state_type * state   = enkf_main_iget_state( enkf_main , 0 );
      enkf_fs_type * fs = enkf_main_get_fs( enkf_main );
      run_arg_type * run_arg = run_arg_alloc_ENSEMBLE_EXPERIMENT( fs , 0 ,0 , "simulations/run0");
      enkf_node_type * surface_node = enkf_state_get_node( state , "SURFACE" );
      node_id_type node_id = {.report_step = 0 ,  
                              .iens = 0,
                              .state = ANALYZED };

      create_runpath( enkf_main );
      test_assert_true( util_is_directory( "simulations/run0" ));
      
      {
        int error = 0;
        stringlist_type * msg_list = stringlist_alloc_new();

                
        test_assert_false( enkf_node_has_data( surface_node , fs, node_id ));
        
        util_unlink_existing( "simulations/run0/Surface.irap" );
        
        test_assert_false( enkf_node_forward_init( surface_node , "simulations/run0" , 0 ));
        enkf_state_forward_init( state , run_arg , &error );
        test_assert_true(LOAD_FAILURE & error);

        error = 0;
        {
          enkf_fs_type * fs = enkf_main_get_fs(enkf_main);
          state_map_type * state_map = enkf_fs_get_state_map(fs);
          state_map_iset(state_map, 0, STATE_INITIALIZED);
        }
        enkf_state_load_from_forward_model(state, run_arg , &error, false, msg_list);

        stringlist_free( msg_list );
        test_assert_true(LOAD_FAILURE & error);
      }
      

      util_copy_file( init_file , "simulations/run0/Surface.irap");
      {
        int error = 0; 
        stringlist_type * msg_list = stringlist_alloc_new();

        
        test_assert_true( enkf_node_forward_init( surface_node , "simulations/run0" , 0 ));
        enkf_state_forward_init( state , run_arg , &error );
        test_assert_int_equal(0, error); 
        enkf_state_load_from_forward_model( state , run_arg , &error , false , msg_list );
        stringlist_free( msg_list );
        test_assert_int_equal(0, error); 

        {
          double value;
          test_assert_true( enkf_node_user_get( surface_node , fs , "0" , node_id , &value)); 
          test_assert_double_equal( 2735.7461 , value);

          test_assert_true( enkf_node_user_get( surface_node , fs , "5" , node_id , &value)); 
          test_assert_double_equal( 2737.0122 , value);
        }
      }
      util_clear_directory( "simulations" , true , true );
      create_runpath( enkf_main );
      test_assert_true( util_is_directory( "simulations/run0" ));
      test_assert_true( util_is_file( "simulations/run0/SURFACE.INC" ));
      test_assert_true( enkf_node_fload( surface_node , "simulations/run0/SURFACE.INC"));
      {
        double value;
        test_assert_true( enkf_node_user_get( surface_node , fs , "0" , node_id , &value)); 
        test_assert_double_equal( 2735.7461 , value);
      
        test_assert_true( enkf_node_user_get( surface_node , fs , "5" , node_id , &value)); 
        test_assert_double_equal( 2737.0122 , value);
      }
      util_clear_directory( "simulations" , true , true );
    }
    enkf_main_free( enkf_main );
  }
}
예제 #15
0
bool util_proc_alive(pid_t pid) {
  char proc_path[16];
  sprintf(proc_path , "/proc/%d" , pid);
  return util_is_directory(proc_path);
}
예제 #16
0
void test_copy_directory(const char * rel_path) {
  test_work_area_type * work_area = test_work_area_alloc( "FILE-TEST" );
  test_work_area_copy_directory( work_area , rel_path );
  test_assert_true( util_is_directory( rel_path ));
  test_work_area_free( work_area );
}
예제 #17
0
job_queue_node_type * job_queue_node_alloc( const char * job_name ,
                                            const char * run_path ,
                                            const char * run_cmd ,
                                            int argc ,
                                            const char ** argv,
                                            int num_cpu,
                                            const char * ok_file,
                                            const char * exit_file,
                                            job_callback_ftype * done_callback,
                                            job_callback_ftype * retry_callback,
                                            job_callback_ftype * exit_callback,
                                            void * callback_arg) {

  if (util_is_directory( run_path )) {
    job_queue_node_type * node = util_malloc(sizeof * node );

    UTIL_TYPE_ID_INIT( node , JOB_QUEUE_NODE_TYPE_ID );
    {
      /* The data initialized in this block should *NEVER* change. */
      node->job_name       = util_alloc_string_copy( job_name );

      if (util_is_abs_path(run_path))
        node->run_path = util_alloc_string_copy( run_path );
      else
        node->run_path = util_alloc_realpath( run_path );

      node->run_cmd        = util_alloc_string_copy( run_cmd );
      node->argc           = argc;
      node->argv           = util_alloc_stringlist_copy( argv , argc );
      node->num_cpu        = num_cpu;

      if (ok_file)
        node->ok_file = util_alloc_filename(node->run_path , ok_file , NULL);
      else
        node->ok_file = NULL;

      if (exit_file)
        node->exit_file = util_alloc_filename(node->run_path , exit_file , NULL);
      else
        node->exit_file = NULL;

      node->exit_callback  = exit_callback;
      node->retry_callback = retry_callback;
      node->done_callback  = done_callback;
      node->callback_arg   = callback_arg;
    }
    {
      node->error_reason   = NULL;
      node->stderr_capture = NULL;
      node->stderr_file    = NULL;
      node->failed_job     = NULL;
    }
    {
      node->job_status     = JOB_QUEUE_NOT_ACTIVE;
      node->queue_index    = INVALID_QUEUE_INDEX;
      node->submit_attempt = 0;
      node->job_data       = NULL;                                    /* The allocation is run in single thread mode - we assume. */
      node->sim_start      = 0;
      node->sim_end        = 0;
      node->submit_time    = time( NULL );
    }

    pthread_mutex_init( &node->data_mutex , NULL );
    return node;
  } else
    return NULL;
}
예제 #18
0
int main(int argc , char ** argv) {
  enkf_main_install_SIGNALS();
  const char * root_path = argv[1];
  const char * config_file = argv[2];
  const char * forward_init_string = argv[3];
  test_work_area_type * work_area = test_work_area_alloc(config_file , false);
  test_work_area_copy_directory_content( work_area , root_path );
  {
    bool forward_init;
    bool strict = true;
    enkf_main_type * enkf_main;
    
    test_assert_true( util_sscanf_bool( forward_init_string , &forward_init));

    util_clear_directory( "Storage" , true , true );
    enkf_main = enkf_main_bootstrap( NULL , config_file , strict , true );
    {
      enkf_state_type * state   = enkf_main_iget_state( enkf_main , 0 );
      enkf_node_type * gen_param_node = enkf_state_get_node( state , "PARAM" );
      {
        const enkf_config_node_type * gen_param_config_node = enkf_node_get_config( gen_param_node );
        char * init_file1 = enkf_config_node_alloc_initfile( gen_param_config_node , NULL , 0);
        char * init_file2 = enkf_config_node_alloc_initfile( gen_param_config_node , "/tmp", 0);

        test_assert_bool_equal( enkf_config_node_use_forward_init( gen_param_config_node ) , forward_init );
        test_assert_string_equal( init_file1 , "PARAM_INIT");
        test_assert_string_equal( init_file2 , "/tmp/PARAM_INIT");

        free( init_file1 );
        free( init_file2 );
      }
  
      test_assert_bool_equal( enkf_node_use_forward_init( gen_param_node ) , forward_init );
      if (forward_init)
        test_assert_bool_not_equal( enkf_node_initialize( gen_param_node , 0 , enkf_state_get_rng( state )) , forward_init);
      // else hard_failure()
    }
    test_assert_bool_equal( forward_init, ensemble_config_have_forward_init( enkf_main_get_ensemble_config( enkf_main )));
    
    if (forward_init) {
      enkf_state_type * state   = enkf_main_iget_state( enkf_main , 0 );
      enkf_fs_type * fs = enkf_main_get_fs( enkf_main );
      enkf_node_type * gen_param_node = enkf_state_get_node( state , "PARAM" );
      node_id_type node_id = {.report_step = 0 ,  
                              .iens = 0,
                              .state = ANALYZED };

      create_runpath( enkf_main );
      test_assert_true( util_is_directory( "simulations/run0" ));
      
      {
        run_mode_type run_mode = ENSEMBLE_EXPERIMENT; 
        enkf_main_init_run(enkf_main , run_mode);     /* This is ugly */
        
        
        test_assert_false( enkf_node_has_data( gen_param_node , fs, node_id ));
        util_unlink_existing( "simulations/run0/PARAM_INIT" );
      }
      


      {
        FILE * stream = util_fopen("simulations/run0/PARAM_INIT" , "w");
        fprintf(stream , "0\n1\n2\n3\n" );
        fclose( stream );
      }
      
      {
        bool loadOK = true;
        stringlist_type * msg_list = stringlist_alloc_new();

        {
          run_mode_type run_mode = ENSEMBLE_EXPERIMENT; 
          enkf_main_init_run(enkf_main , run_mode);     /* This is ugly */
        }
        
        test_assert_true( enkf_node_forward_init( gen_param_node , "simulations/run0" , 0 ));
        enkf_state_forward_init( state , fs , &loadOK );
        test_assert_true( loadOK );
        enkf_state_load_from_forward_model( state , fs , &loadOK , false , msg_list );
        stringlist_free( msg_list );
        test_assert_true( loadOK );

        {
          double value;
          test_assert_true( enkf_node_user_get( gen_param_node , fs , "0" , node_id , &value)); 
          test_assert_double_equal( 0 , value);

          test_assert_true( enkf_node_user_get( gen_param_node , fs , "1" , node_id , &value)); 
          test_assert_double_equal( 1 , value);

          test_assert_true( enkf_node_user_get( gen_param_node , fs , "2" , node_id , &value)); 
          test_assert_double_equal( 2 , value);
        }
      }
      util_clear_directory( "simulations" , true , true );
      create_runpath( enkf_main );
      test_assert_true( util_is_directory( "simulations/run0" ));
      test_assert_true( util_is_file( "simulations/run0/PARAM.INC" ));
      {
        FILE * stream = util_fopen("simulations/run0/PARAM.INC" , "r");
        double v0,v1,v2,v3;
        fscanf(stream , "%lg %lg %lg %lg" , &v0,&v1,&v2,&v3);
        fclose( stream );                
        test_assert_double_equal( 0 , v0);
        test_assert_double_equal( 1 , v1);
        test_assert_double_equal( 2 , v2);
        test_assert_double_equal( 3 , v3);
      }
      util_clear_directory( "simulations" , true , true );
    }
    enkf_main_free( enkf_main );
  }
  test_work_area_free( work_area );
}
예제 #19
0
파일: rms_stats.c 프로젝트: Ensembles/ert
void rms_stats_update_ens(const char *prior_path , const char *posterior_path , const char **file_list , const char *param_name , int ens_size , const double **X) {
  int iens , j;
  rms_tagkey_type ** prior;
  rms_tagkey_type  * post ;
  rms_tag_type     * dim_tag = NULL;

  if (!util_is_directory(posterior_path)) {
    fprintf(stderr,"%s: posterior_path:%s does not exist - aborting \n",__func__ , posterior_path);
    abort();
  }

  prior = malloc(ens_size * sizeof * prior);
  printf("Loading: ");
  for (iens = 0; iens < ens_size; iens++) {
    char * file_name         = util_alloc_filename(prior_path , file_list[iens] , NULL);
    printf("%s",file_name); fflush(stdout);
    {
      rms_file_type * rms_file = rms_file_alloc(file_name , false);
      prior[iens]              = rms_file_fread_alloc_data_tagkey(rms_file , "parameter" , "name" , param_name);
    
      if (iens == 0)
        dim_tag = rms_file_fread_alloc_tag(rms_file , "dimensions" , NULL , NULL);
    
      rms_file_free(rms_file);
    }
    for (j = 0; j < strlen(file_name); j++) fputc('\b' , stdout);
      
    
    free(file_name);
  }
  printf("\n");

  printf("Writing: ");
  post = rms_tagkey_copyc(prior[0]);
  for (iens = 0; iens < ens_size; iens++) {
    rms_tagkey_clear(post);
    
    for (j=0; j < iens; j++) 
      rms_tagkey_inplace_add_scaled(post , prior[j] , X[iens][j]);
    
    {
      char * file_name    = util_alloc_filename(posterior_path , file_list[iens] , NULL);
      rms_file_type *file = rms_file_alloc(file_name , false);
      FILE *stream        = rms_file_fopen_w(file);

      printf("%s",file_name); fflush(stdout);
      rms_file_init_fwrite(file , "parameter");
      rms_tag_fwrite(dim_tag , stream);
      rms_tag_fwrite_parameter(param_name , post , stream);
      rms_file_complete_fwrite(file);
      fclose(stream);
      rms_file_free(file);
      
      for (j = 0; j < strlen(file_name); j++) fputc('\b' , stdout);
      free(file_name);
    }
  }
  printf("\n");
  
  rms_tag_free(dim_tag);
  rms_tagkey_free(post);
  for (iens = 0; iens < ens_size; iens++) 
    rms_tagkey_free(prior[iens]);
  free(prior);
}