Skip to content

Commit

Permalink
Merge pull request #201 from joakim-hove/rft-open-file
Browse files Browse the repository at this point in the history
Make sure RFT file is opened in correct mode.
  • Loading branch information
Alf Birger Rustad authored Sep 11, 2017
2 parents 4bbf3d3 + 325cbad commit 4b6785e
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 7 deletions.
23 changes: 17 additions & 6 deletions opm/output/eclipse/EclipseIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
#include <utility> // move

#include <ert/ecl/EclKW.hpp>
#include <ert/ecl/FortIO.hpp>
#include <ert/ecl/EclFilename.hpp>

#include <ert/ecl/ecl_kw_magic.h>
Expand All @@ -59,6 +58,8 @@
#include <ert/ecl_well/well_const.h>
#include <ert/ecl/ecl_rsthead.h>
#include <ert/util/util.h>
#include <ert/ecl/fortio.h>

#define OPM_XWEL "OPM_XWEL"
#define OPM_IWEL "OPM_IWEL"

Expand Down Expand Up @@ -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 )
{}


Expand All @@ -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<double>& pressure = cells.data("PRESSURE");
const std::vector<double>& swat = cells.data("SWAT");
const std::vector<double>& sgas = cells.has("SGAS") ? cells.data("SGAS") : std::vector<double>( 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 ) {
Expand Down Expand Up @@ -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 ) {
Expand Down
17 changes: 16 additions & 1 deletion tests/testRFT.DATA
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ COMPDAT
DATES -- 2
10 OKT 2008 /
/
WRFT
WRFTPLT
'OP_1' 'YES' /
'OP_2' 'REPT' /
/
WELOPEN
'OP_1' OPEN /
Expand All @@ -45,3 +47,16 @@ WELOPEN
DATES -- 3
10 NOV 2008 /
/


DATES -- 4
20 NOV 2008 /
/

WRFTPLT
'OP_2' 'NO' /
/

DATES -- 5
30 NOV 2008 /
/
74 changes: 74 additions & 0 deletions tests/test_RFT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
}

0 comments on commit 4b6785e

Please sign in to comment.