From 7c82e69a00aebd23212251840c45f21a9e7c769f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Kvalsvik?= Date: Thu, 16 Jun 2016 13:16:00 +0200 Subject: [PATCH] Copy and save a Grid copy in Writer; apply actnum Simulators might modify the grid post EclipseState creation, so the Grid fetched from there is unreliable. Copy the Deck-provided grid and apply the manipulations at EclipseWriter construction time to ensure it uses the same dimensions and has the same properties as the simulator. --- opm/output/eclipse/EclipseWriter.hpp | 7 +- src/opm/output/eclipse/EclipseWriter.cpp | 86 +++++++++++++----------- tests/test_EclipseWriter.cpp | 4 +- tests/test_RFT.cpp | 4 +- tests/test_Restart.cpp | 4 +- tests/test_writenumwells.cpp | 4 +- 6 files changed, 58 insertions(+), 51 deletions(-) diff --git a/opm/output/eclipse/EclipseWriter.hpp b/opm/output/eclipse/EclipseWriter.hpp index 546e2b04e..77ddadf3f 100644 --- a/opm/output/eclipse/EclipseWriter.hpp +++ b/opm/output/eclipse/EclipseWriter.hpp @@ -48,14 +48,15 @@ public: */ EclipseWriter(std::shared_ptr< const EclipseState >, int numCells, - const int* compressedToCartesianCellIdx); + const int* compressedToCartesianCellIdx, + const NNC& ); /** * Write the static eclipse data (grid, PVT curves, etc) to disk. * - * If NNC is given, writes TRANNNC keyword. + * If NNC is given to the constructor, writes TRANNNC keyword. */ - void writeInit( const NNC& nnc ); + void writeInit(); /*! * \brief Write a reservoir state and summary information to disk. diff --git a/src/opm/output/eclipse/EclipseWriter.cpp b/src/opm/output/eclipse/EclipseWriter.cpp index 01746cafe..2fb6099c3 100644 --- a/src/opm/output/eclipse/EclipseWriter.cpp +++ b/src/opm/output/eclipse/EclipseWriter.cpp @@ -277,50 +277,25 @@ public: const int* compressedToCartesianCellIdx, time_t current_posix_time, const EclipseState& es, - int ert_phase_mask, - const NNC& nnc = NNC() ) + const EclipseGrid& eclGrid, + int ert_phase_mask ) { auto dataField = es.get3DProperties().getDoubleGridProperty("PORO").getData(); restrictAndReorderToActiveCells( dataField, numCells, compressedToCartesianCellIdx ); - auto eclGrid = es.getInputGridCopy(); - - // update the ACTNUM array using the processed cornerpoint grid - std::vector actnumData( eclGrid->getCartesianSize(), 1 ); - if ( compressedToCartesianCellIdx ) { - actnumData.assign( actnumData.size(), 0 ); - - for( int cellIdx = 0; cellIdx < numCells; ++cellIdx ) { - actnumData[ compressedToCartesianCellIdx[ cellIdx ] ] = 1; - } - } - - eclGrid->resetACTNUM( &actnumData[0] ); - - if (nnc.hasNNC()) - { - int idx = 0; - // const_cast is safe, since this is a copy of the input grid - auto ecl_grid = const_cast(eclGrid->c_ptr()); - for (NNCdata n : nnc.nncdata()) { - ecl_grid_add_self_nnc( ecl_grid, n.cell1, n.cell2, idx++); - } - } - - // finally, write the grid to disk const auto& ioConfig = *es.getIOConfig(); if( ioConfig.getWriteEGRIDFile() ) { const bool is_metric = es.getDeckUnitSystem().getType() == UnitSystem::UNIT_TYPE_METRIC; - eclGrid->fwriteEGRID( egrid.ertHandle(), is_metric ); + eclGrid.fwriteEGRID( egrid.ertHandle(), is_metric ); } if( ioConfig.getWriteINITFile() ) { ERT::EclKW< float > poro_kw( "PORO", dataField ); ecl_init_file_fwrite_header( this->ertHandle(), - eclGrid->c_ptr(), + eclGrid.c_ptr(), poro_kw.get(), ert_phase_mask, current_posix_time ); @@ -464,9 +439,12 @@ class EclipseWriter::Impl { public: Impl( std::shared_ptr< const EclipseState > es, int numCells, - const int* comp_to_cart ); + const int* comp_to_cart, + const NNC& ); std::shared_ptr< const EclipseState > es; + EclipseGrid grid; + const NNC& nnc; std::string outputDir; std::string baseName; out::Summary summary; @@ -482,8 +460,11 @@ class EclipseWriter::Impl { EclipseWriter::Impl::Impl( std::shared_ptr< const EclipseState > eclipseState, int numCellsArg, - const int* compressed_to_cart ) + const int* compressed_to_cart, + const NNC& rnnc ) : es( eclipseState ) + , grid( *eclipseState->getInputGrid() ) + , nnc( rnnc ) , outputDir( eclipseState->getIOConfig()->getOutputDir() ) , baseName( uppercase( eclipseState->getIOConfig()->getBaseName() ) ) , summary( *eclipseState, eclipseState->getSummaryConfig() ) @@ -499,7 +480,7 @@ EclipseWriter::Impl::Impl( std::shared_ptr< const EclipseState > eclipseState, , ert_phase_mask( ertPhaseMask( eclipseState->getTableManager() ) ) {} -void EclipseWriter::writeInit( const NNC& nnc ) { +void EclipseWriter::writeInit() { if( !this->impl->output_enabled ) return; @@ -513,6 +494,7 @@ void EclipseWriter::writeInit( const NNC& nnc ) { this->impl->compressed_to_cartesian, this->impl->sim_start_time, es, + this->impl->grid, this->impl->ert_phase_mask ); IOConfigConstPtr ioConfig = es.getIOConfigConst(); @@ -548,10 +530,10 @@ void EclipseWriter::writeInit( const NNC& nnc ) { fortio.writeKeyword("PERMZ", data); } - if( !nnc.hasNNC() ) return; + if( !this->impl->nnc.hasNNC() ) return; std::vector tran; - for( NNCdata nd : nnc.nncdata() ) { + for( NNCdata nd : this->impl->nnc.nncdata() ) { tran.push_back( nd.trans ); } @@ -576,6 +558,7 @@ void EclipseWriter::writeTimeStep(int report_step, time_t current_posix_time = this->impl->sim_start_time + secs_elapsed; const auto& gridToEclipseIdx = this->impl->gridToEclipseIdx; const auto& es = *this->impl->es; + const auto& grid = this->impl->grid; const auto& units = es.getUnits(); auto& pressure = cells[ dc::PRESSURE ]; @@ -635,9 +618,9 @@ void EclipseWriter::writeTimeStep(int report_step, ecl_rsthead_type rsthead_data = {}; rsthead_data.sim_time = current_posix_time; rsthead_data.nactive = this->impl->numCells; - rsthead_data.nx = es.getInputGrid()->getNX(); - rsthead_data.ny = es.getInputGrid()->getNY(); - rsthead_data.nz = es.getInputGrid()->getNZ(); + rsthead_data.nx = grid.getNX(); + rsthead_data.ny = grid.getNY(); + rsthead_data.nz = grid.getNZ(); rsthead_data.nwells = numWells; rsthead_data.niwelz = Restart::NIWELZ; rsthead_data.nzwelz = Restart::NZWELZ; @@ -697,7 +680,7 @@ void EclipseWriter::writeTimeStep(int report_step, const auto unit_type = es.getDeckUnitSystem().getType(); this->impl->rft.writeTimeStep( schedule.getWells( report_step ), - *es.getInputGrid(), + grid, report_step, current_posix_time, days, @@ -717,8 +700,9 @@ void EclipseWriter::writeTimeStep(int report_step, EclipseWriter::EclipseWriter( std::shared_ptr< const EclipseState > es, int numCells, - const int* compressedToCartesianCellIdx ) : - impl( new Impl( es, numCells, compressedToCartesianCellIdx ) ) + const int* compressedToCartesianCellIdx, + const NNC& nnc ) : + impl( new Impl( es, numCells, compressedToCartesianCellIdx, nnc ) ) { if( compressedToCartesianCellIdx ) { // if compressedToCartesianCellIdx available then @@ -741,6 +725,28 @@ EclipseWriter::EclipseWriter( std::shared_ptr< const EclipseState > es, } } + auto& grid = this->impl->grid; + + // update the ACTNUM array using the processed cornerpoint grid + std::vector< int > actnumData( grid.getCartesianSize(), 1 ); + if ( compressedToCartesianCellIdx ) { + actnumData.assign( actnumData.size(), 0 ); + + for( int cellIdx = 0; cellIdx < numCells; ++cellIdx ) + actnumData[ compressedToCartesianCellIdx[ cellIdx ] ] = 1; + } + + grid.resetACTNUM( &actnumData[0] ); + + if( nnc.hasNNC() ) { + int idx = 0; + // const_cast is safe, since this is a copy of the input grid + auto* ecl_grid = const_cast< ecl_grid_type* >( grid.c_ptr() ); + for( NNCdata n : nnc.nncdata() ) { + ecl_grid_add_self_nnc( ecl_grid, n.cell1, n.cell2, idx++); + } + } + if( !this->impl->output_enabled ) return; const auto& outputDir = this->impl->outputDir; diff --git a/tests/test_EclipseWriter.cpp b/tests/test_EclipseWriter.cpp index d4f9d545c..1fef940c6 100644 --- a/tests/test_EclipseWriter.cpp +++ b/tests/test_EclipseWriter.cpp @@ -262,11 +262,11 @@ BOOST_AUTO_TEST_CASE(EclipseWriterIntegration) auto& eclGrid = *es->getInputGrid(); { - EclipseWriter eclWriter( es, 3 * 3 * 3, nullptr ); + EclipseWriter eclWriter( es, 3 * 3 * 3, nullptr, NNC()); auto start_time = util_make_datetime( 0, 0, 0, 10, 10, 2008 ); auto first_step = util_make_datetime( 0, 0, 0, 10, 11, 2008 ); - eclWriter.writeInit( NNC() ); + eclWriter.writeInit(); data::Wells wells; diff --git a/tests/test_RFT.cpp b/tests/test_RFT.cpp index 356f315a7..4286f485f 100755 --- a/tests/test_RFT.cpp +++ b/tests/test_RFT.cpp @@ -121,11 +121,11 @@ BOOST_AUTO_TEST_CASE(test_RFT) { * eclipseState->getInputGrid()->getNY() * eclipseState->getInputGrid()->getNZ(); - EclipseWriter eclipseWriter( eclipseState, numCells, nullptr ); + EclipseWriter eclipseWriter( eclipseState, numCells, nullptr, NNC() ); time_t start_time = eclipseState->getSchedule()->posixStartTime(); /* step time read from deck and hard-coded here */ time_t step_time = util_make_datetime( 0, 0, 0, 10, 10, 2008 ); - eclipseWriter.writeInit( NNC() ); + eclipseWriter.writeInit(); Opm::data::Wells wells { { { "OP_1", { {}, 1.0, 1.1, {} } }, diff --git a/tests/test_Restart.cpp b/tests/test_Restart.cpp index d11d15050..f0604793c 100644 --- a/tests/test_Restart.cpp +++ b/tests/test_Restart.cpp @@ -229,10 +229,10 @@ first_sim(test_work_area_type * test_area) { const auto& grid = *eclipseState->getInputGrid(); auto num_cells = grid.getNX() * grid.getNY() * grid.getNZ(); - EclipseWriter eclWriter( eclipseState, num_cells, nullptr ); + EclipseWriter eclWriter( eclipseState, num_cells, nullptr, NNC() ); auto start_time = util_make_datetime( 0, 0, 0, 1, 11, 1979 ); auto first_step = util_make_datetime( 0, 0, 0, 10, 10, 2008 ); - eclWriter.writeInit( NNC() ); + eclWriter.writeInit(); auto sol = mkSolution( num_cells ); auto wells = mkWells(); diff --git a/tests/test_writenumwells.cpp b/tests/test_writenumwells.cpp index 4c476c5b2..0cf06c0c5 100644 --- a/tests/test_writenumwells.cpp +++ b/tests/test_writenumwells.cpp @@ -141,9 +141,9 @@ BOOST_AUTO_TEST_CASE(EclipseWriteRestartWellInfo) { auto es = Parser::parse( eclipse_data_filename, ParseContext() ); auto eclipseState = std::make_shared< EclipseState >( es ); const auto num_cells = eclipseState->getInputGrid()->getCartesianSize(); - EclipseWriter eclipseWriter( eclipseState, num_cells, nullptr ); + EclipseWriter eclipseWriter( eclipseState, num_cells, nullptr, NNC() ); - eclipseWriter.writeInit( NNC() ); + eclipseWriter.writeInit(); int countTimeStep = eclipseState->getSchedule()->getTimeMap()->numTimesteps();