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_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_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_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 ); }
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 ); }
FortIO::FortIO(const std::string& filename , std::ios_base::openmode mode , bool fmt_file , bool endian_flip_header) { if (mode == std::ios_base::in) { if (util_file_exists( filename.c_str() )) { fortio_type * c_ptr = fortio_open_reader( filename.c_str() , fmt_file , endian_flip_header); m_fortio.reset( c_ptr , fortio_fclose ); } else throw std::invalid_argument("File " + filename + " does not exist"); } else { fortio_type * c_ptr = fortio_open_writer( filename.c_str() , fmt_file , endian_flip_header); m_fortio.reset( c_ptr , fortio_fclose ); } }
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 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 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); }
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 ); } }
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 ); }
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); } }
void test_not_existing_read() { fortio_type * fortio = fortio_open_reader( "/does/not/exist" , false , ECL_ENDIAN_FLIP); test_assert_NULL( fortio ); }
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 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 ); }