diff --git a/opm/output/eclipse/EclipseIO.cpp b/opm/output/eclipse/EclipseIO.cpp index 4d7d680..343d158 100644 --- a/opm/output/eclipse/EclipseIO.cpp +++ b/opm/output/eclipse/EclipseIO.cpp @@ -47,7 +47,6 @@ #include // move #include -#include #include #include @@ -59,6 +58,8 @@ #include #include #include +#include + #define OPM_XWEL "OPM_XWEL" #define OPM_IWEL "OPM_IWEL" @@ -110,13 +111,15 @@ class RFT { data::Solution cells); private: std::string filename; + bool fmt_file; }; RFT::RFT( const std::string& output_dir, const std::string& basename, bool format ) : - filename( ERT::EclFilename( output_dir, basename, ECL_RFT_FILE, format ) ) + filename( ERT::EclFilename( output_dir, basename, ECL_RFT_FILE, format ) ), + fmt_file( format ) {} @@ -127,12 +130,20 @@ void RFT::writeTimeStep( std::vector< const Well* > wells, double days, const UnitSystem& units, data::Solution cells) { - using rft = ERT::ert_unique_ptr< ecl_rft_node_type, ecl_rft_node_free >; const std::vector& pressure = cells.data("PRESSURE"); const std::vector& swat = cells.data("SWAT"); const std::vector& sgas = cells.has("SGAS") ? cells.data("SGAS") : std::vector( pressure.size() , 0 ); - ERT::FortIO fortio(filename, std::ios_base::out); + fortio_type * fortio; + int first_report_step = report_step; + + for (const auto* well : wells) + first_report_step = std::min( first_report_step, well->firstRFTOutput()); + + if (report_step > first_report_step) + fortio = fortio_open_append( filename.c_str() , fmt_file , ECL_ENDIAN_FLIP ); + else + fortio = fortio_open_writer( filename.c_str() , fmt_file , ECL_ENDIAN_FLIP ); cells.convertFromSI( units ); for ( const auto& well : wells ) { @@ -163,10 +174,10 @@ void RFT::writeTimeStep( std::vector< const Well* > wells, } rft ecl_node( rft_node ); - ecl_rft_node_fwrite( ecl_node.get(), fortio.get(), units.getEclType() ); + ecl_rft_node_fwrite( ecl_node.get(), fortio, units.getEclType() ); } - fortio.close(); + fortio_fclose( fortio ); } inline std::string uppercase( std::string x ) { diff --git a/tests/testRFT.DATA b/tests/testRFT.DATA index d59590e..c1d09f7 100644 --- a/tests/testRFT.DATA +++ b/tests/testRFT.DATA @@ -36,7 +36,9 @@ COMPDAT DATES -- 2 10 OKT 2008 / / -WRFT +WRFTPLT + 'OP_1' 'YES' / + 'OP_2' 'REPT' / / WELOPEN 'OP_1' OPEN / @@ -45,3 +47,16 @@ WELOPEN DATES -- 3 10 NOV 2008 / / + + +DATES -- 4 + 20 NOV 2008 / +/ + +WRFTPLT + 'OP_2' 'NO' / +/ + +DATES -- 5 + 30 NOV 2008 / +/ diff --git a/tests/test_RFT.cpp b/tests/test_RFT.cpp index cfb7c95..8ffd7a2 100755 --- a/tests/test_RFT.cpp +++ b/tests/test_RFT.cpp @@ -148,3 +148,77 @@ BOOST_AUTO_TEST_CASE(test_RFT) { verifyRFTFile("TESTRFT.RFT"); } + +namespace { +void verifyRFTFile2(const std::string& rft_filename) { + ecl_rft_file_type * rft_file = ecl_rft_file_alloc(rft_filename.c_str()); + /* + Expected occurences: + + WELL | DATE + --------------------- + OP_1 | 10. OKT 2008 + OP_2 | 10. OKT 2008 + OP_2 | 10. NOV 2008 + --------------------- + */ + + BOOST_CHECK_EQUAL( 3 , ecl_rft_file_get_size( rft_file )); + BOOST_CHECK( ecl_rft_file_get_well_time_rft(rft_file , "OP_1" , ecl_util_make_date(10, 10, 2008)) != NULL ); + BOOST_CHECK( ecl_rft_file_get_well_time_rft(rft_file , "OP_2" , ecl_util_make_date(10, 10, 2008)) != NULL ); + BOOST_CHECK( ecl_rft_file_get_well_time_rft(rft_file , "OP_2" , ecl_util_make_date(10, 11, 2008)) != NULL ); + + ecl_rft_file_free( rft_file ); +} +} + + +BOOST_AUTO_TEST_CASE(test_RFT2) { + + std::string eclipse_data_filename = "testRFT.DATA"; + ERT::TestArea test_area("test_RFT"); + test_area.copyFile( eclipse_data_filename ); + + auto eclipseState = Parser::parse( eclipse_data_filename ); + { + /* eclipseWriter is scoped here to ensure it is destroyed after the + * file itself has been written, because we're going to reload it + * immediately. first upon destruction can we guarantee it being + * written to disk and flushed. + */ + + const auto& grid = eclipseState.getInputGrid(); + const auto numCells = grid.getCartesianSize( ); + + EclipseIO eclipseWriter( eclipseState, grid); + time_t start_time = eclipseState.getSchedule().posixStartTime(); + const auto& time_map = eclipseState.getSchedule().getTimeMap( ); + + for (int counter = 0; counter < 2; counter++) { + for (size_t step = 0; step < time_map.size(); step++) { + time_t step_time = time_map[step]; + + data::Rates r1, r2; + r1.set( data::Rates::opt::wat, 4.11 ); + r1.set( data::Rates::opt::oil, 4.12 ); + r1.set( data::Rates::opt::gas, 4.13 ); + + r2.set( data::Rates::opt::wat, 4.21 ); + r2.set( data::Rates::opt::oil, 4.22 ); + r2.set( data::Rates::opt::gas, 4.23 ); + + Opm::data::Wells wells; + wells["OP_1"] = { r1, 1.0, 1.1, 3.1, 1, {} }; + wells["OP_2"] = { r2, 1.0, 1.1, 3.2, 1, {} }; + + eclipseWriter.writeTimeStep( step, + false, + step_time - start_time, + createBlackoilState( 2, numCells ), + wells, + {}); + } + verifyRFTFile2("TESTRFT.RFT"); + } + } +}