void test_fread_alloc() { test_work_area_type * work_area = test_work_area_alloc("ecl_kw_fread" ); { ecl_kw_type * kw1 = ecl_kw_alloc( "INT" , 100 , ECL_INT ); int i; for (i=0; i < 100; i++) ecl_kw_iset_int( kw1 , i , i ); { fortio_type * fortio = fortio_open_writer("INT" , false , true ); ecl_kw_fwrite( kw1 , fortio ); fortio_fclose( fortio ); } { fortio_type * fortio = fortio_open_reader("INT" , false , true ); ecl_kw_type * kw2 = ecl_kw_fread_alloc( fortio ); test_assert_true( ecl_kw_is_instance( kw2 )); test_assert_true( ecl_kw_equal( kw1 , kw2 )); ecl_kw_free( kw2 ); fortio_fclose( fortio ); } { offset_type file_size = util_file_size("INT"); test_truncated("INT" , file_size - 4 ); test_truncated("INT" , file_size - 25 ); test_truncated("INT" , 5 ); test_truncated("INT" , 0 ); } ecl_kw_free( kw1 ); } test_work_area_free( work_area ); }
void test_at_eof() { test_work_area_type * work_area = test_work_area_alloc("fortio_truncated2" ); { fortio_type * fortio = fortio_open_writer("PRESSURE" , false , true); void * buffer = util_malloc( 100 ); fortio_fwrite_record( fortio , buffer , 100); free( buffer ); fortio_fclose( fortio ); } { fortio_type * fortio = fortio_open_reader("PRESSURE" , false , true); test_assert_false( fortio_read_at_eof( fortio )); fortio_fseek( fortio , 50 , SEEK_SET ); test_assert_false( fortio_read_at_eof( fortio )); fortio_fseek( fortio , 0 , SEEK_END ); test_assert_true( fortio_read_at_eof( fortio )); fortio_fclose( fortio ); } test_work_area_free( work_area ); }
void test_fread_truncated_tail() { test_work_area_type * work_area = test_work_area_alloc("fortio_truncated2" ); { const size_t buffer_size = 1000; void * buffer = util_malloc( buffer_size ); { fortio_type * fortio = fortio_open_writer( "PRESSURE" , false , true ); fortio_fwrite_record( fortio , buffer , buffer_size ); fortio_fseek( fortio , 0 , SEEK_SET); util_ftruncate( fortio_get_FILE(fortio) , buffer_size + 4); fortio_fclose( fortio ); } test_assert_long_equal( util_file_size( "PRESSURE") , buffer_size + 4); { fortio_type * fortio = fortio_open_reader( "PRESSURE" , false , true ); test_assert_false( fortio_fread_buffer( fortio , buffer , buffer_size )); fortio_fclose( fortio ); } free( buffer ); } test_work_area_free( work_area ); }
void test_fseek() { test_work_area_type * work_area = test_work_area_alloc("fortio_fseek" ); { fortio_type * fortio = fortio_open_writer("PRESSURE" , false , true); void * buffer = util_malloc( 100 ); fortio_fwrite_record( fortio , buffer , 100); free( buffer ); fortio_fclose( fortio ); } { fortio_type * fortio = fortio_open_reader("PRESSURE" , false , true); printf("Starting fssek test \n"); test_assert_true( fortio_fseek( fortio , 0 , SEEK_SET )); test_assert_true( fortio_fseek( fortio , 0 , SEEK_END )); test_assert_false( fortio_fseek( fortio , 100000 , SEEK_END)); test_assert_false( fortio_fseek( fortio , 100000 , SEEK_SET)); fortio_fclose( fortio ); } test_work_area_free( work_area ); }
void test_write_header() { int nx = 10; int ny = 10; int nz = 5; int_vector_type * actnum = int_vector_alloc( nx*ny*nz , 1 ); test_work_area_type * test_area = test_work_area_alloc( "ecl_init_file" ); time_t start_time = util_make_date(15 , 12 , 2010 ); ecl_grid_type * ecl_grid; int_vector_iset( actnum , 10 , 0 ); int_vector_iset( actnum , 100 , 0 ); ecl_grid = ecl_grid_alloc_rectangular(nx, ny, nz, 1, 1, 1, int_vector_get_ptr( actnum )); // Write poro with global size. { fortio_type * f = fortio_open_writer( "FOO1.INIT" , false , ECL_ENDIAN_FLIP ); ecl_kw_type * poro = ecl_kw_alloc( "PORO" , ecl_grid_get_global_size( ecl_grid ) , ECL_FLOAT_TYPE ); ecl_kw_scalar_set_float( poro , 0.10 ); ecl_init_file_fwrite_header( f , ecl_grid , poro , 7 , start_time ); ecl_kw_free( poro ); fortio_fclose( f ); } // Write poro with nactive size. { fortio_type * f = fortio_open_writer( "FOO2.INIT" , false , ECL_ENDIAN_FLIP ); ecl_kw_type * poro = ecl_kw_alloc( "PORO" , ecl_grid_get_global_size( ecl_grid ) , ECL_FLOAT_TYPE ); ecl_kw_scalar_set_float( poro , 0.10 ); ecl_init_file_fwrite_header( f , ecl_grid , poro , 7 , start_time ); ecl_kw_free( poro ); fortio_fclose( f ); } { ecl_file_type * file1 = ecl_file_open( "FOO1.INIT" , 0 ); ecl_file_type * file2 = ecl_file_open( "FOO2.INIT" , 0 ); test_assert_true( ecl_kw_equal( ecl_file_iget_named_kw( file1 , "PORV" , 0 ) , ecl_file_iget_named_kw( file2 , "PORV" , 0))); ecl_file_close( file2 ); ecl_file_close( file1 ); } // Poro == NULL { fortio_type * f = fortio_open_writer( "FOO3.INIT" , false , ECL_ENDIAN_FLIP ); ecl_init_file_fwrite_header( f , ecl_grid , NULL , 7 , start_time ); fortio_fclose( f ); } test_work_area_free( test_area ); }
void test_fread_invalid_tail() { test_work_area_type * work_area = test_work_area_alloc("fortio_invalid" ); int record_size = 10; void * buffer = util_malloc( record_size ); { FILE * stream = util_fopen("PRESSURE" , "w"); util_fwrite(&record_size , sizeof record_size , 1 , stream , __func__); util_fwrite(buffer , 1 , record_size , stream , __func__); util_fwrite(&record_size , sizeof record_size , 1 , stream , __func__); util_fwrite(&record_size , sizeof record_size , 1 , stream , __func__); util_fwrite(buffer , 1 , record_size , stream , __func__); record_size += 1; util_fwrite(&record_size , sizeof record_size , 1 , stream , __func__); fclose(stream); } { fortio_type * fortio = fortio_open_reader( "PRESSURE" , false , false ); record_size -= 1; test_assert_true( fortio_fread_buffer( fortio , buffer , record_size )); test_assert_false( fortio_fread_buffer( fortio , buffer , record_size )); fortio_fclose( fortio ); } free( buffer ); test_work_area_free( work_area ); }
void test_fortio_safe_cast(const char * filename ) { void * i_am_a_fortio = fortio_open_reader( filename , false , ECL_ENDIAN_FLIP); test_assert_not_NULL( i_am_a_fortio ); fortio_type * fortio = fortio_safe_cast(i_am_a_fortio); test_assert_true(fortio_is_instance(fortio)); fortio_fclose( fortio ); }
static void ecl_sum_data_fwrite_unified_step( const ecl_sum_data_type * data , const char * ecl_case , bool fmt_case , int report_step) { char * filename = ecl_util_alloc_filename( NULL , ecl_case , ECL_UNIFIED_SUMMARY_FILE , fmt_case , 0 ); fortio_type * fortio = fortio_open_readwrite( filename , fmt_case , ECL_ENDIAN_FLIP ); int current_step = 1; if (report_step > 1) { while (true) { if (ecl_kw_fseek_kw( SEQHDR_KW , false , false , fortio )) { if (current_step == report_step) break; current_step++; } else { current_step++; break; } } } if (current_step == report_step) { // We found the position: long size = fortio_ftell( fortio ); util_ftruncate( fortio_get_FILE( fortio ) , size ); ecl_sum_data_fwrite_report__( data , report_step , fortio ); } else util_abort("%s: hmm could not locate the position for report step:%d in summary file:%s \n",__func__ , report_step , filename); fortio_fclose( fortio ); free( filename ); }
void test_create_simple() { test_work_area_type * work_area = test_work_area_alloc("nnc-INIT"); { int nx = 10; int ny = 10; int nz = 10; ecl_grid_type * grid0 = ecl_grid_alloc_rectangular(nx,ny,nz,1,1,1,NULL); ecl_grid_add_self_nnc(grid0, 0 ,nx*ny + 0, 0 ); ecl_grid_add_self_nnc(grid0, 1 ,nx*ny + 1, 1 ); ecl_grid_add_self_nnc(grid0, 2 ,nx*ny + 2, 2 ); { ecl_nnc_geometry_type * nnc_geo = ecl_nnc_geometry_alloc( grid0 ); test_assert_int_equal( ecl_nnc_geometry_size( nnc_geo ) , 3 ); /* Create a dummy INIT file which *ony* contains a TRANNC keyword with the correct size. */ { ecl_kw_type * trann_nnc = ecl_kw_alloc(TRANNNC_KW , ecl_nnc_geometry_size( nnc_geo ), ECL_FLOAT); fortio_type * f = fortio_open_writer( "TEST.INIT" , false, ECL_ENDIAN_FLIP ); for (int i=0; i < ecl_kw_get_size( trann_nnc); i++) ecl_kw_iset_float( trann_nnc , i , i*1.0 ); ecl_kw_fwrite( trann_nnc , f ); fortio_fclose( f ); ecl_kw_free( trann_nnc ); } } ecl_grid_free( grid0 ); } test_work_area_free( work_area ); }
void test_write( const char * filename , bool path_exists) { fortio_type * fortio = fortio_open_writer( filename , false , ECL_ENDIAN_FLIP); if (path_exists) { test_assert_not_NULL( fortio ); fortio_fclose( fortio ); } else test_assert_NULL( fortio ); }
static void ecl_sum_data_fwrite_multiple_step( const ecl_sum_data_type * data , const char * ecl_case , bool fmt_case , int report_step) { char * filename = ecl_util_alloc_filename( NULL , ecl_case , ECL_UNIFIED_SUMMARY_FILE , fmt_case , 0 ); fortio_type * fortio = fortio_open_readwrite( filename , fmt_case , ECL_ENDIAN_FLIP ); ecl_sum_data_fwrite_report__( data , report_step , fortio ); fortio_fclose( fortio ); free(filename); }
int main(int argc, char ** argv) { if (argc < 4) { fprintf(stderr,"%s src_file target_file kw1 kw2 kw3 \n",argv[0]); exit(0); } { const char * src_file = argv[1]; const char * target_file = argv[2]; const char ** kw_list = (const char **) &argv[3]; int num_kw = argc - 3; fortio_type * fortio_src; fortio_type * fortio_target; bool fmt_src , fmt_target; set_type * kw_set = set_alloc( num_kw , kw_list ); if (!ecl_util_fmt_file(src_file, &fmt_src)) util_exit("Hmm - could not determine formatted/unformatted status for:%s \n",src_file); fmt_target = fmt_src; /* Can in principle be different */ fortio_src = fortio_open_reader(src_file , fmt_src , ECL_ENDIAN_FLIP); fortio_target = fortio_open_writer(target_file , fmt_target , ECL_ENDIAN_FLIP); { ecl_kw_type * ecl_kw = ecl_kw_alloc_empty(); while (true) { if (ecl_kw_fread_header( ecl_kw , fortio_src )) { const char * header = ecl_kw_get_header( ecl_kw ); if (set_has_key( kw_set , header )) { ecl_kw_fread_realloc_data(ecl_kw , fortio_src ); ecl_kw_fwrite( ecl_kw , fortio_target ); } else ecl_kw_fskip_data( ecl_kw , fortio_src ); } else break; /* We have reached EOF */ } ecl_kw_free( ecl_kw ); } fortio_fclose(fortio_src); fortio_fclose(fortio_target); set_free( kw_set ); } }
static void ecl_sum_data_fwrite_unified( const ecl_sum_data_type * data , const char * ecl_case , bool fmt_case ) { char * filename = ecl_util_alloc_filename( NULL , ecl_case , ECL_UNIFIED_SUMMARY_FILE , fmt_case , 0 ); fortio_type * fortio = fortio_open_writer( filename , fmt_case , ECL_ENDIAN_FLIP ); int report_step; for (report_step = data->first_report_step; report_step <= data->last_report_step; report_step++) { if (ecl_sum_data_has_report_step( data , report_step )) ecl_sum_data_fwrite_report__( data , report_step , fortio ); } fortio_fclose( fortio ); free( filename ); }
void test_fortio_is_instance(const char * filename ) { { fortio_type * fortio = fortio_open_reader( filename , false , ECL_ENDIAN_FLIP); test_assert_not_NULL( fortio ); test_assert_true(fortio_is_instance(fortio)); fortio_fclose( fortio ); } { vector_type * dummy_vector = vector_alloc_new(); test_assert_false(fortio_is_instance(dummy_vector)); vector_free(dummy_vector); } }
void test_truncated(const char * filename , offset_type truncate_size) { { FILE * stream = util_fopen(filename , "r+"); util_ftruncate( stream , truncate_size); fclose( stream ); } { fortio_type * fortio = fortio_open_reader( filename , false , true ); ecl_kw_type * kw2 = ecl_kw_fread_alloc( fortio ); test_assert_NULL( kw2 ); fortio_fclose(fortio); } }
void ecl_file_fwrite(const ecl_file_type * ecl_file , const char * filename, bool fmt_file) { bool __fmt_file; ecl_file_enum file_type; file_type = ecl_util_get_file_type( filename , &__fmt_file , NULL); if (file_type == ECL_OTHER_FILE) __fmt_file = fmt_file; { fortio_type * target = fortio_open_writer( filename , __fmt_file , ECL_ENDIAN_FLIP); ecl_file_fwrite_fortio( ecl_file , target , 0); fortio_fclose( target ); } }
void test_open_close_read( const char * filename ) { fortio_type * fortio = fortio_open_reader( filename , false , ECL_ENDIAN_FLIP); test_assert_not_NULL( fortio ); test_assert_true( fortio_stream_is_open( fortio )); test_assert_true( fortio_fclose_stream( fortio )); test_assert_false( fortio_stream_is_open( fortio )); test_assert_false( fortio_fclose_stream( fortio )); test_assert_true( fortio_fopen_stream( fortio )); test_assert_true( fortio_stream_is_open( fortio )); test_assert_false( fortio_fopen_stream( fortio )); fortio_fclose( fortio ); }
ecl_grid_dims_type * ecl_grid_dims_alloc( const char * grid_file , const char * data_file) { ecl_grid_dims_type * grid_dims = NULL; bool grid_fmt_file; ecl_file_enum grid_file_type = ecl_util_get_file_type( grid_file , &grid_fmt_file , NULL ); if ((grid_file_type == ECL_GRID_FILE) || (grid_file_type == ECL_EGRID_FILE)) { fortio_type * grid_fortio = fortio_open_reader( grid_file , grid_fmt_file , ECL_ENDIAN_FLIP ); if (grid_fortio) { grid_dims = util_malloc( sizeof * grid_dims ); grid_dims->dims_list = vector_alloc_new( ); { fortio_type * data_fortio = NULL; bool data_fmt_file; if (data_file) { ecl_util_get_file_type( data_file , &data_fmt_file , NULL ); data_fortio = fortio_open_reader( data_file , data_fmt_file , ECL_ENDIAN_FLIP ); } if (grid_file_type == ECL_EGRID_FILE) ecl_grid_dims_read_EGRID( grid_dims , grid_fortio , data_fortio ); else ecl_grid_dims_read_GRID( grid_dims , grid_fortio , data_fortio ); if (data_fortio) fortio_fclose( data_fortio ); } fortio_fclose( grid_fortio ); } } return grid_dims; }
void kw_list(const char *filename) { fortio_type *fortio; bool fmt_file = ecl_util_fmt_file(filename); ecl_kw_type * ecl_kw = ecl_kw_alloc_empty(); printf("-----------------------------------------------------------------\n"); printf("%s: \n",filename); fortio = fortio_open_reader(filename , fmt_file , ECL_ENDIAN_FLIP); while( ecl_kw_fread_realloc(ecl_kw , fortio) ) ecl_kw_summarize(ecl_kw); printf("-----------------------------------------------------------------\n"); ecl_kw_free(ecl_kw); fortio_fclose(fortio); }
void ecl_rft_file_update(const char * rft_file_name, ecl_rft_node_type ** nodes,int num_nodes, ert_ecl_unit_enum unit_set){ ecl_rft_file_type * rft_file; if(util_file_exists(rft_file_name)){ int node_index; rft_file = ecl_rft_file_alloc( rft_file_name ); for(node_index = 0; node_index < num_nodes; node_index++) { ecl_rft_node_type * new_node = nodes[node_index]; int storage_index = ecl_rft_file_get_node_index_time_rft(rft_file, ecl_rft_node_get_well_name(new_node), ecl_rft_node_get_date(new_node)); if (storage_index == -1) { ecl_rft_file_add_node(rft_file, new_node); } else { vector_iset_owned_ref(rft_file->data, storage_index, new_node,ecl_rft_node_free__); } } }else{ int node_index; rft_file = ecl_rft_file_alloc_empty( rft_file_name ); for(node_index = 0; node_index < num_nodes; node_index++) { ecl_rft_file_add_node(rft_file, nodes[node_index]); } } { bool fmt_file = false; fortio_type * fortio = fortio_open_writer( rft_file_name , fmt_file , ECL_ENDIAN_FLIP ); int node_index; /** The sorting here works directly on the internal node storage rft_file->data; that might in principle ruin the indexing of the ecl_file object - it is therefor absolutely essential that this ecl_rft_file object does not live beyond this function, and also that the ecl_rft_file api functions are avoided for the rest of this function. */ vector_sort(rft_file->data,(vector_cmp_ftype *) ecl_rft_node_cmp); for(node_index=0; node_index < vector_get_size( rft_file->data ); node_index++) { const ecl_rft_node_type *new_node = vector_iget_const(rft_file->data, node_index); ecl_rft_node_fwrite(new_node, fortio, unit_set); } fortio_fclose( fortio ); } ecl_rft_file_free(rft_file); }
void test_kw_io_charlength() { test_work_area_type * work_area = test_work_area_alloc("ecl_kw_io_charlength"); { const char * KW0 = "QWERTYUI"; const char * KW1 = "ABCDEFGHIJTTTTTTTTTTTTTTTTTTTTTTABCDEFGHIJKLMNOP"; ecl_kw_type * ecl_kw_out0 = ecl_kw_alloc(KW0 , 5, ECL_FLOAT); ecl_kw_type * ecl_kw_out1 = ecl_kw_alloc(KW1 , 5, ECL_FLOAT); for (int i=0; i < ecl_kw_get_size( ecl_kw_out1); i++) { ecl_kw_iset_float( ecl_kw_out0 , i , i*1.5 ); ecl_kw_iset_float( ecl_kw_out1 , i , i*1.5 ); } { fortio_type * f = fortio_open_writer( "TEST1" , false, ECL_ENDIAN_FLIP ); test_assert_true( ecl_kw_fwrite( ecl_kw_out0, f )); test_assert_false(ecl_kw_fwrite( ecl_kw_out1, f )); fortio_fclose( f ); } { test_assert_false( util_file_exists( "TEST1")); } { FILE * file = util_fopen("TEST2", "w"); ecl_kw_fprintf_grdecl(ecl_kw_out1 , file); fclose(file); } { FILE * file = util_fopen("TEST2", "r"); ecl_kw_type * ecl_kw_in = ecl_kw_fscanf_alloc_grdecl( file , KW1 , -1 , ECL_FLOAT); test_assert_string_equal(KW1, ecl_kw_get_header(ecl_kw_in) ); test_assert_int_equal(5, ecl_kw_get_size( ecl_kw_in) ); test_assert_double_equal(ecl_kw_iget_as_double(ecl_kw_in, 0), 0.0); test_assert_double_equal(ecl_kw_iget_as_double(ecl_kw_in, 4), 6.0); fclose(file); } ecl_kw_free( ecl_kw_out0 ); ecl_kw_free( ecl_kw_out1 ); } test_work_area_free( work_area ); }
void checkEgridFile() { size_t numCells = ourFineGridManagerPtr->c_grid()->number_of_cells; // use ERT directly to inspect the EGRID file produced by EclipseWriter auto egridFile = fortio_open_reader("FOO.EGRID", /*isFormated=*/0, ECL_ENDIAN_FLIP); auto eclGrid = eclipseState->getEclipseGrid(); ecl_kw_type *eclKeyword; // yes, that's an assignment! while ((eclKeyword = ecl_kw_fread_alloc(egridFile))) { std::string keywordName(ecl_kw_get_header(eclKeyword)); if (keywordName == "COORD") { std::vector<double> sourceData, resultData; eclGrid->exportCOORD(sourceData); getErtData(eclKeyword, resultData); compareErtData(sourceData, resultData, /*percentTolerance=*/1e-6); } else if (keywordName == "ZCORN") { std::vector<double> sourceData, resultData; eclGrid->exportZCORN(sourceData); getErtData(eclKeyword, resultData); compareErtData(sourceData, resultData, /*percentTolerance=*/1e-6); } else if (keywordName == "ACTNUM") { std::vector<int> sourceData, resultData; eclGrid->exportACTNUM(sourceData); getErtData(eclKeyword, resultData); if (resultData.size() == numCells && sourceData.size() == 0) { sourceData.resize(numCells); std::fill(sourceData.begin(), sourceData.end(), 1); } compareErtData(sourceData, resultData); } ecl_kw_free(eclKeyword); } fortio_fclose(egridFile); }
void test_fread_truncated_head() { test_work_area_type * work_area = test_work_area_alloc("fortio_truncated" ); { { FILE * stream = util_fopen("PRESSURE" , "w"); fclose( stream ); } { fortio_type * fortio = fortio_open_reader( "PRESSURE" , false , true ); void * buffer = NULL; int buffer_size = 10; test_assert_false( fortio_fread_buffer( fortio , buffer , buffer_size )); test_assert_true( fortio_read_at_eof( fortio )); fortio_fclose( fortio ); } } test_work_area_free( work_area ); }
// Inspired by the ecl_pack application in the ERT library void concatenateRestart(const std::string& basename) { std::string inputPath, inputBase; splitBasename(basename, inputPath, inputBase); stringlist_type* inputFiles = stringlist_alloc_new(); const int numFiles = ecl_util_select_filelist(inputPath.c_str(), inputBase.c_str(), ECL_RESTART_FILE, false, inputFiles); const char* target_file_name = ecl_util_alloc_filename(inputPath.c_str(), inputBase.c_str(), ECL_UNIFIED_RESTART_FILE, false, -1); fortio_type* target = fortio_open_writer(target_file_name, false, ECL_ENDIAN_FLIP); int dummy; ecl_kw_type* seqnum_kw = ecl_kw_alloc_new("SEQNUM", 1, ECL_INT, &dummy); int reportStep = 0; for (int i = 0; i < numFiles; ++i) { ecl_util_get_file_type(stringlist_iget(inputFiles, i), nullptr, &reportStep); ecl_file_type* src_file = ecl_file_open(stringlist_iget(inputFiles, i), 0); ecl_kw_iset_int(seqnum_kw, 0, reportStep); ecl_kw_fwrite(seqnum_kw, target); ecl_file_fwrite_fortio(src_file, target, 0); ecl_file_close(src_file); } fortio_fclose(target); stringlist_free(inputFiles); }
void checkInitFile() { // use ERT directly to inspect the INIT file produced by EclipseWriter auto initFile = fortio_open_reader("FOO.INIT", /*isFormated=*/0, ECL_ENDIAN_FLIP); ecl_kw_type *eclKeyword; // yes, that's an assignment! while ((eclKeyword = ecl_kw_fread_alloc(initFile))) { std::string keywordName(ecl_kw_get_header(eclKeyword)); if (keywordName == "PORO") { const std::vector<double> &sourceData = deck->getKeyword("PORO")->getSIDoubleData(); std::vector<double> resultData; getErtData(eclKeyword, resultData); compareErtData(sourceData, resultData, /*percentTolerance=*/1e-4); } if (keywordName == "PERMX") { std::vector<double> sourceData = deck->getKeyword("PERMX")->getSIDoubleData(); std::vector<double> resultData; getErtData(eclKeyword, resultData); // convert the data from ERT from Field to SI units (mD to m^2) for (size_t i = 0; i < resultData.size(); ++i) { resultData[i] *= 9.869233e-16; } compareErtData(sourceData, resultData, /*percentTolerance=*/1e-4); } ecl_kw_free(eclKeyword); } fortio_fclose(initFile); }
void checkRestartFile(int timeStepIdx) { size_t numCells = ourFineGridManagerPtr->c_grid()->number_of_cells; Opm::PhaseUsage phaseUsage = Opm::phaseUsageFromDeck(deck); int numActivePhases = phaseUsage.num_phases; int waterPhaseIdx = phaseUsage.phase_pos[Opm::BlackoilPhases::Aqua]; int gasPhaseIdx = phaseUsage.phase_pos[Opm::BlackoilPhases::Vapour]; for (int i = 0; i <= timeStepIdx; ++i) { createBlackoilState(i); // use ERT directly to inspect the restart file produced by EclipseWriter auto rstFile = fortio_open_reader("FOO.UNRST", /*isFormated=*/0, ECL_ENDIAN_FLIP); int curSeqnum = -1; ecl_kw_type *eclKeyword; // yes, that's an assignment! while ((eclKeyword = ecl_kw_fread_alloc(rstFile))) { std::string keywordName(ecl_kw_get_header(eclKeyword)); if (keywordName == "SEQNUM") { curSeqnum = *static_cast<int*>(ecl_kw_iget_ptr(eclKeyword, 0)); } if (curSeqnum != i) continue; if (keywordName == "PRESSURE") { std::vector<double> sourceData = blackoilState->pressure(); std::vector<double> resultData; getErtData(eclKeyword, resultData); // convert the data from ERT from Metric to SI units (bar to Pa) for (size_t i = 0; i < resultData.size(); ++i) { resultData[i] *= 1e5; } compareErtData(sourceData, resultData, /*percentTolerance=*/1e-4); } if (keywordName == "SWAT") { std::vector<double> sourceData; std::vector<double> resultData; getErtData(eclKeyword, resultData); // extract the water saturation from the black-oil state sourceData.resize(numCells); for (size_t i = 0; i < sourceData.size(); ++i) { // again, fun with direct index manipulation... sourceData[i] = blackoilState->saturation()[i*numActivePhases + waterPhaseIdx]; } compareErtData(sourceData, resultData, /*percentTolerance=*/1e-4); } if (keywordName == "SGAS") { std::vector<double> sourceData; std::vector<double> resultData; getErtData(eclKeyword, resultData); // extract the water saturation from the black-oil state sourceData.resize(numCells); for (size_t i = 0; i < sourceData.size(); ++i) { // again, fun with direct index manipulation... sourceData[i] = blackoilState->saturation()[i*numActivePhases + gasPhaseIdx]; } compareErtData(sourceData, resultData, /*percentTolerance=*/1e-4); } } fortio_fclose(rstFile); } }
int main(int argc, char ** argv) { int num_files = argc - 1; if (num_files >= 1) { /* File type and formatted / unformatted is determined from the first argument on the command line. */ char * ecl_base; char * path; ecl_file_enum file_type , target_type; bool fmt_file; /** Look at the first command line argument to determine type and formatted/unformatted status. */ file_type = ecl_util_get_file_type( argv[1] , &fmt_file , NULL); if (file_type == ECL_SUMMARY_FILE) target_type = ECL_UNIFIED_SUMMARY_FILE; else if (file_type == ECL_RESTART_FILE) target_type = ECL_UNIFIED_RESTART_FILE; else { util_exit("The ecl_pack program can only be used with ECLIPSE restart files or summary files.\n"); target_type = -1; } util_alloc_file_components( argv[1] , &path , &ecl_base , NULL); /** Will pack to cwd, even though the source files might be somewhere else. To unpack to the same directory as the source files, just send in @path as first argument when creating the target_file. */ { msg_type * msg; int i , report_step , prev_report_step; char * target_file_name = ecl_util_alloc_filename( NULL , ecl_base , target_type , fmt_file , -1); stringlist_type * filelist = stringlist_alloc_argv_copy( (const char **) &argv[1] , num_files ); ecl_kw_type * seqnum_kw = NULL; fortio_type * target = fortio_open_writer( target_file_name , fmt_file , ECL_ENDIAN_FLIP); if (target_type == ECL_UNIFIED_RESTART_FILE) { int dummy; seqnum_kw = ecl_kw_alloc_new("SEQNUM" , 1 , ECL_INT_TYPE , &dummy); } { char * msg_format = util_alloc_sprintf("Packing %s <= " , target_file_name); msg = msg_alloc( msg_format , false); free( msg_format ); } msg_show( msg ); stringlist_sort( filelist , ecl_util_fname_report_cmp); prev_report_step = -1; for (i=0; i < num_files; i++) { ecl_file_enum this_file_type; this_file_type = ecl_util_get_file_type( stringlist_iget(filelist , i) , NULL , &report_step); if (this_file_type == file_type) { if (report_step == prev_report_step) util_exit("Tried to write same report step twice: %s / %s \n", stringlist_iget(filelist , i-1) , stringlist_iget(filelist , i)); prev_report_step = report_step; msg_update(msg , stringlist_iget( filelist , i)); { ecl_file_type * src_file = ecl_file_open( stringlist_iget( filelist , i) , 0 ); if (target_type == ECL_UNIFIED_RESTART_FILE) { /* Must insert the SEQNUM keyword first. */ ecl_kw_iset_int(seqnum_kw , 0 , report_step); ecl_kw_fwrite( seqnum_kw , target ); } ecl_file_fwrite_fortio( src_file , target , 0); ecl_file_close( src_file ); } } /* Else skipping file of incorrect type. */ } msg_free(msg , false); fortio_fclose( target ); free(target_file_name); stringlist_free( filelist ); if (seqnum_kw != NULL) ecl_kw_free(seqnum_kw); } free(ecl_base); util_safe_free(path); } }
int main( int argc , char ** argv) { int num_kw = 1000; // Total file size should roughly exceed 2GB int kw_size = 600000; ecl_kw_type * kw = ecl_kw_alloc("KW" , kw_size , ECL_INT_TYPE ); rng_type * rng = rng_alloc( MZRAN , INIT_DEFAULT ); int i; offset_type file_size; for (i=0; i < kw_size; i++) ecl_kw_iset_int( kw , i , rng_get_int( rng , 912732 )); { fortio_type * fortio = fortio_open_writer( "LARGE_FILE.UNRST" , false , ECL_ENDIAN_FLIP); for (i = 0; i < num_kw; i++) { printf("Writing keyword %d/%d to file:LARGE_FILE.UNRST \n",i+1 , num_kw ); ecl_kw_fwrite( kw , fortio ); } fortio_fclose( fortio ); } /*{ fortio_type * fortio = fortio_open_reader( "LARGE_FILE.UNRST" , false , ECL_ENDIAN_FLIP); for (i = 0; i < num_kw - 1; i++) { printf("SKipping keyword %d/%d from file:LARGE_FILE.UNRST \n",i+1 , num_kw ); ecl_kw_fskip( fortio ); } { ecl_kw_type * file_kw = ecl_kw_fread_alloc( fortio ); if (ecl_kw_equal( kw , file_kw )) printf("Keyword read back from file correctly :-) \n"); else printf("Fatal error - keyword different on return ...\n"); ecl_kw_free( file_kw ); } fortio_fclose( fortio ); } */ file_size = util_file_size( "LARGE_FILE.UNRST" ); printf("File size: %lld \n",file_size); { fortio_type * fortio = fortio_open_reader( "LARGE_FILE.UNRST" , false , ECL_ENDIAN_FLIP); printf("Seeking to file end: "); fortio_fseek( fortio , file_size , SEEK_SET); fortio_fclose( fortio ); printf("Seek OK \n"); } printf("Doing ecl_file_open(..)\n"); { ecl_file_type * file = ecl_file_open( "LARGE_FILE.UNRST" , 0); ecl_kw_type * file_kw = ecl_file_iget_named_kw( file , "KW" , num_kw - 1); if (ecl_kw_equal( kw , file_kw )) printf("Keyword read back from file correctly :-) \n"); else printf("Fatal error - keyword different on return ...\n"); ecl_file_close( file ); } remove( "LARGE_FILE.UNRST" ); exit(0); }
void unpack_file(const char * filename) { ecl_file_enum target_type = ECL_OTHER_FILE; ecl_file_enum file_type; bool fmt_file; file_type = ecl_util_get_file_type(filename , &fmt_file , NULL); if (file_type == ECL_UNIFIED_SUMMARY_FILE) target_type = ECL_SUMMARY_FILE; else if (file_type == ECL_UNIFIED_RESTART_FILE) target_type = ECL_RESTART_FILE; else util_exit("Can only unpack unified ECLIPSE summary and restart files\n"); if (target_type == ECL_SUMMARY_FILE) { printf("** Warning: when unpacking unified summary files it as ambigous - starting with 0001 -> \n"); } { ecl_file_type * src_file = ecl_file_open( filename , 0 ); int size; int offset; int report_step = 0; int block_index = 0; char * path; char * base; msg_type * msg; util_alloc_file_components( filename , &path , &base , NULL); { char * label = util_alloc_sprintf("Unpacking %s => ", filename); msg = msg_alloc( label , false); free( label ); } msg_show(msg); if (target_type == ECL_SUMMARY_FILE) size = ecl_file_get_num_named_kw( src_file , "SEQHDR" ); else size = ecl_file_get_num_named_kw( src_file , "SEQNUM" ); while (true) { if (block_index == size) break; if (target_type == ECL_SUMMARY_FILE) { ecl_file_select_block( src_file , SEQHDR_KW , block_index ); report_step += 1; offset = 0; } else { ecl_kw_type * seqnum_kw; ecl_file_select_block( src_file , SEQNUM_KW , block_index ); seqnum_kw = ecl_file_iget_named_kw( src_file , SEQNUM_KW , 0); report_step = ecl_kw_iget_int( seqnum_kw , 0); offset = 1; } /** Will unpack to cwd, even though the source files might be somewhere else. To unpack to the same directory as the source files, just send in @path as first argument when creating the target_file. */ { char * target_file = ecl_util_alloc_filename( NULL , base , target_type , fmt_file , report_step); fortio_type * fortio_target = fortio_open_writer( target_file , fmt_file , ECL_ENDIAN_FLIP ); msg_update(msg , target_file); ecl_file_fwrite_fortio( src_file , fortio_target , offset); fortio_fclose(fortio_target); free(target_file); } block_index++; } ecl_file_close( src_file ); util_safe_free(path); free(base); msg_free(msg , true); } }
void test_existing_read(const char * filename) { fortio_type * fortio = fortio_open_reader( filename , false , ECL_ENDIAN_FLIP); test_assert_not_NULL( fortio ); fortio_fclose( fortio ); }