Merge pull request #1335 from atgeirr/refactor-sim

Removing unused code parts and minor refactoring
This commit is contained in:
Andreas Lauser 2017-11-22 13:57:58 +01:00 committed by GitHub
commit ef76d0a8cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 3 additions and 496 deletions

View File

@ -153,7 +153,6 @@ list (APPEND PUBLIC_HEADER_FILES
opm/autodiff/AutoDiffHelpers.hpp
opm/autodiff/AutoDiffMatrix.hpp
opm/autodiff/AutoDiff.hpp
opm/autodiff/BackupRestore.hpp
opm/autodiff/BlackoilDetails.hpp
opm/autodiff/BlackoilLegacyDetails.hpp
opm/autodiff/BlackoilModel.hpp

View File

@ -1,321 +0,0 @@
/*
Copyright 2014 IRIS AS
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OPM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef OPM_BACKUPRESTORE_HEADER_INCLUDED
#define OPM_BACKUPRESTORE_HEADER_INCLUDED
#include <iostream>
#include <opm/common/data/SimulationDataContainer.hpp>
#include <opm/core/simulator/BlackoilState.hpp>
#include <opm/autodiff/WellStateFullyImplicitBlackoil.hpp>
namespace Opm {
namespace {
template <class T>
void writeValue( std::ostream& stream, const T& value )
{
stream.write( (const char *) &value, sizeof( T ) );
}
template <class T>
void readValue( std::istream& stream, T& value )
{
stream.read( (char *) &value, sizeof( T ) );
}
// write any container that provides a size method and a data method
template <class Vector>
void writeContainer( std::ostream& stream, const Vector& vector)
{
typedef typename Vector :: value_type T;
unsigned int size = vector.size() * sizeof( T );
writeValue( stream, size );
if( size > 0 ) {
stream.write( (const char *) vector.data(), size );
}
}
// write map where the key is a std::string
// and the value is a std::vector or std::array
template <class Map>
void writeMap( std::ostream& stream, const Map& m)
{
const unsigned int mapEntries = m.size();
writeValue( stream, mapEntries );
if( mapEntries > 0 )
{
const auto& end = m.end();
for(auto it = m.begin(); it != end; ++it )
{
// write key (assume that key is a container)
writeContainer( stream, (*it).first );
// write value (assume that value is a container)
writeContainer( stream, (*it).second );
}
}
}
template <class Container>
void resizeContainer( Container& container, size_t size )
{
container.resize( size );
}
template <class T, size_t n>
void resizeContainer( std::array<T, n>& /* a */, size_t size )
{
static_cast<void>(size);
assert( int(size) == int(n) );
}
template <class Container>
void readData(std::istream& stream, Container& container, size_t datasize)
{
stream.read( reinterpret_cast<char*>( container.data() ), datasize );
}
//We need to be careful with string, because string.data() returns something that
//"A program shall not alter any of the characters in this sequence."
template <>
void readData<std::string>(std::istream& stream, std::string& string, size_t datasize)
{
std::vector<char> raw_data(datasize);
readData(stream, raw_data, datasize);
string = std::string(raw_data.data(), datasize);
}
template <class Container>
void readContainerImpl( std::istream& stream, Container& container, const bool adjustSize )
{
typedef typename Container :: value_type T;
unsigned int dataSize = 0;
readValue( stream, dataSize );
if( adjustSize && dataSize > 0 ) {
resizeContainer( container, dataSize/sizeof(T) );
}
if( dataSize != container.size() * sizeof( T ) )
{
OPM_THROW(std::logic_error,
"Size of stored data and simulation data does not match "
<< dataSize << " " << (container.size() * sizeof( T )) );
}
if( dataSize > 0 ) {
readData(stream, container, dataSize);
}
}
template <class Container>
void readAndResizeContainer( std::istream& stream, Container& container )
{
readContainerImpl( stream, container, true );
}
template <class Container>
void readContainer( std::istream& stream, Container& container )
{
readContainerImpl( stream, container, false );
}
template <class Map>
void readMap( std::istream& stream, Map& m)
{
m.clear();
unsigned int mapEntries = 0;
readValue( stream, mapEntries );
for( unsigned int entry = 0; entry<mapEntries; ++entry )
{
std::pair< typename Map :: key_type, typename Map :: mapped_type > mapEntry;
// read key
readAndResizeContainer( stream, mapEntry.first );
// read values
readContainer( stream, mapEntry.second );
// insert entry into map
m.insert( mapEntry );
}
}
enum { SimulatorStateId = 0,
WellStateId = 1,
WellStateFullyImplicitBackoilId = 3 };
inline int objectId( const SimulationDataContainer& /* state */) {
return SimulatorStateId;
}
inline int objectId( const WellState& /* state */) {
return WellStateId;
}
inline int objectId( const WellStateFullyImplicitBlackoil& /* state */) {
return WellStateFullyImplicitBackoilId;
}
template <class State>
void checkObjectId( std::istream& in, const State& state )
{
// read id and compare with object
int id = -1;
readValue( in, id );
if( id != objectId( state ) ) {
OPM_THROW(std::logic_error,"backup-restore object type mismatch");
}
}
}
// SimulationDataContainer
inline
std::ostream& operator << (std::ostream& out, const SimulationDataContainer& state )
{
// write id of object to stream
writeValue( out, objectId( state ) );
const int numPhases = state.numPhases();
writeValue( out, numPhases );
// write variables
writeContainer( out, state.pressure() );
writeContainer( out, state.temperature() );
writeContainer( out, state.facepressure() );
writeContainer( out, state.faceflux() );
writeContainer( out, state.saturation() );
return out;
}
inline
std::istream& operator >> (std::istream& in, SimulationDataContainer& state )
{
// check id of stored object
checkObjectId( in, state );
int numPhases = 0;
readValue( in, numPhases );
if( numPhases != (int) state.numPhases() )
OPM_THROW(std::logic_error,"num phases wrong");
// read variables
readContainer( in, state.pressure() );
readContainer( in, state.temperature() );
readContainer( in, state.facepressure() );
readContainer( in, state.faceflux() );
readContainer( in, state.saturation() );
return in;
}
// WellState
inline
std::ostream& operator << (std::ostream& out, const WellState& state )
{
// write id of object to stream
writeValue( out, objectId( state ) );
// backup well state
writeContainer( out, state.bhp() );
writeContainer( out, state.temperature() );
writeContainer( out, state.wellRates() );
writeContainer( out, state.perfRates() );
writeContainer( out, state.perfPress() );
return out;
}
inline
std::istream& operator >> (std::istream& in, WellState& state )
{
// check id of stored object
checkObjectId( in, state );
// restore well state
readAndResizeContainer( in, state.bhp() );
readAndResizeContainer( in, state.temperature() );
readAndResizeContainer( in, state.wellRates() );
readAndResizeContainer( in, state.perfRates() );
readAndResizeContainer( in, state.perfPress() );
return in;
}
// WellStateFullyImplicitBlackoil
inline
std::ostream& operator << (std::ostream& out, const WellStateFullyImplicitBlackoil& state )
{
// write id of object to stream
writeValue( out, objectId( state ) );
// backup well state
const WellState& wellState = static_cast< const WellState& > (state);
out << wellState;
const int numWells = state.numWells();
writeValue( out, numWells );
if( numWells > 0 )
{
const int numPhases = state.numPhases();
writeValue( out, numPhases );
// backup additional variables
writeContainer( out, state.perfPhaseRates() );
writeContainer( out, state.currentControls() );
writeMap( out, state.wellMap() );
}
return out;
}
inline
std::istream& operator >> (std::istream& in, WellStateFullyImplicitBlackoil& state )
{
// check id of stored object
checkObjectId( in, state );
// restore well state
WellState& wellState = static_cast< WellState& > (state);
in >> wellState;
int numWells = 0;
readValue( in, numWells );
if( numWells != state.numWells() )
OPM_THROW(std::logic_error,"wrong numWells");
if( numWells > 0 )
{
int numPhases = 0;
readValue( in, numPhases );
if( numPhases != state.numPhases() )
OPM_THROW(std::logic_error,"wrong numPhases");
// restore additional variables
readAndResizeContainer( in, state.perfPhaseRates() );
readAndResizeContainer( in, state.currentControls() );
readMap( in, state.wellMap() );
}
return in;
}
} // namespace Opm
#endif // OPM_BACKUPRESTORE_HEADER_INCLUDED

View File

@ -360,7 +360,8 @@ namespace Opm
strftime(tmstr, sizeof(tmstr), "%d-%m-%Y at %X", &tstruct);
const double mem_size = getTotalSystemMemory() / megabyte;
std::ostringstream ss;
ss << "\n\n\n ######## # ###### # #\n";
ss << "\n\n\n";
ss << " ######## # ###### # #\n";
ss << " # # # # # # \n";
ss << " ##### # # # # # # \n";
ss << " # # # # # # # # \n";
@ -693,11 +694,6 @@ namespace Opm
}
}
if (output_to_files_) {
std::string filename = output_dir_ + "/walltime.txt";
std::fstream tot_os(filename.c_str(), std::fstream::trunc | std::fstream::out);
successReport.reportParam(tot_os);
}
} else {
if (output_cout_) {
std::cout << "\n\n================ Simulation turned off ===============\n" << std::flush;
@ -730,7 +726,6 @@ namespace Opm
*fis_solver_,
FluidSystem::enableDissolvedGas(),
FluidSystem::enableVaporizedOil(),
eclState(),
*output_writer_));
}

View File

@ -132,19 +132,6 @@ namespace Opm
}
}
std::string restorefilename = param_.getDefault("restorefile", std::string("") );
if( ! restorefilename.empty() )
{
// -1 means that we'll take the last report step that was written
const int desiredRestoreStep = param_.getDefault("restorestep", int(-1) );
output_writer_.restore( timer,
state,
prev_well_state,
restorefilename,
desiredRestoreStep );
}
DynamicListEconLimited dynamic_list_econ_limited;
SimulatorReport report;
SimulatorReport stepReport;

View File

@ -98,7 +98,6 @@ public:
NewtonIterationBlackoilInterface& linsolver,
const bool has_disgas,
const bool has_vapoil,
const EclipseState& /* eclState */,
OutputWriter& output_writer)
: ebosSimulator_(ebosSimulator),
param_(param),
@ -122,6 +121,7 @@ public:
is_parallel_run_ = ( info.communicator().size() > 1 );
}
#endif
createLocalFipnum();
}
/// Run the simulation.
@ -162,16 +162,8 @@ public:
// Create timers and file for writing timing info.
Opm::time::StopWatch solver_timer;
Opm::time::StopWatch step_timer;
Opm::time::StopWatch total_timer;
total_timer.start();
std::string tstep_filename = output_writer_.outputDirectory() + "/step_timing.txt";
std::ofstream tstep_os;
if ( output_writer_.output() && output_writer_.isIORank() )
{
tstep_os.open(tstep_filename.c_str());
}
// adaptive time stepping
const auto& events = schedule().getEvents();
@ -192,29 +184,9 @@ public:
}
}
std::string restorefilename = param_.getDefault("restorefile", std::string("") );
if( ! restorefilename.empty() )
{
// -1 means that we'll take the last report step that was written
const int desiredRestoreStep = param_.getDefault("restorestep", int(-1) );
output_writer_.restore( timer,
state,
prev_well_state,
restorefilename,
desiredRestoreStep );
initHydroCarbonState(state, phaseUsage_, Opm::UgGridHelpers::numCells(grid()), has_disgas_, has_vapoil_);
initHysteresisParams(state);
// communicate the restart solution to ebos
convertInput(0, state, ebosSimulator_);
ebosSimulator_.model().invalidateIntensiveQuantitiesCache(/*timeIdx=*/0);
}
SimulatorReport report;
SimulatorReport stepReport;
createLocalFipnum();
WellModel well_model(ebosSimulator_, model_param_, terminal_output_);
if (output_writer_.isRestart()) {
well_model.setRestartWellState(prev_well_state); // Neccessary for perfect restarts
@ -225,7 +197,6 @@ public:
// Main simulation loop.
while (!timer.done()) {
// Report timestep.
step_timer.start();
if ( terminal_output_ )
{
std::ostringstream ss;
@ -321,11 +292,6 @@ public:
// update timing.
report.solver_time += solver_timer.secsSinceStart();
if ( output_writer_.output() && output_writer_.isIORank() )
{
stepReport.reportParam(tstep_os);
}
// We don't need the reservoir state anymore. It is just passed around to avoid
// code duplication. Pass empty state instead.
if (timer.initialStep()) {

View File

@ -34,7 +34,6 @@
#include <opm/parser/eclipse/Units/Units.hpp>
#include <opm/autodiff/GridHelpers.hpp>
#include <opm/autodiff/BackupRestore.hpp>
#include <sstream>
#include <iomanip>
@ -351,107 +350,6 @@ namespace Opm
restart_double_si_);
}
}
// write backup file
if( backupfile_.is_open() )
{
int reportStep = timer.reportStepNum();
int currentTimeStep = timer.currentStepNum();
if( (reportStep == currentTimeStep || // true for SimulatorTimer
currentTimeStep == 0 || // true for AdaptiveSimulatorTimer at reportStep
timer.done() ) // true for AdaptiveSimulatorTimer at reportStep
&& lastBackupReportStep_ != reportStep ) // only backup report step once
{
// store report step
lastBackupReportStep_ = reportStep;
// write resport step number
backupfile_.write( (const char *) &reportStep, sizeof(int) );
try {
backupfile_ << state;
const WellStateFullyImplicitBlackoil& boWellState = static_cast< const WellStateFullyImplicitBlackoil& > (wellState);
backupfile_ << boWellState;
}
catch ( const std::bad_cast& e )
{
}
backupfile_ << std::flush;
}
} // end backup
}
void
BlackoilOutputWriter::
restore(SimulatorTimerInterface& timer,
BlackoilState& state,
WellStateFullyImplicitBlackoil& wellState,
const std::string& filename,
const int desiredResportStep )
{
std::ifstream restorefile( filename.c_str() );
if( restorefile )
{
std::cout << "============================================================================"<<std::endl;
std::cout << "Restoring from ";
if( desiredResportStep < 0 ) {
std::cout << "last";
}
else {
std::cout << desiredResportStep;
}
std::cout << " report step! filename = " << filename << std::endl << std::endl;
int reportStep;
restorefile.read( (char *) &reportStep, sizeof(int) );
const int readReportStep = (desiredResportStep < 0) ?
std::numeric_limits<int>::max() : desiredResportStep;
while( reportStep <= readReportStep && ! timer.done() && restorefile )
{
restorefile >> state;
restorefile >> wellState;
// No per cell data is written for restore steps, but will be
// for subsequent steps, when we have started simulating
writeTimeStepWithoutCellProperties( timer, state, wellState, {}, {});
// some output
std::cout << "Restored step " << timer.reportStepNum() << " at day "
<< unit::convert::to(timer.simulationTimeElapsed(),unit::day) << std::endl;
if( readReportStep == reportStep ) {
break;
}
// if the stream is not valid anymore we just use the last state read
if( ! restorefile ) {
std::cerr << "Reached EOF, using last state read!" << std::endl;
break;
}
// try to read next report step
restorefile.read( (char *) &reportStep, sizeof(int) );
// if read failed, exit loop
if( ! restorefile ) {
break;
}
// next step
timer.advance();
if( timer.reportStepNum() != reportStep ) {
break;
}
}
}
else
{
std::cerr << "Warning: Couldn't open restore file '" << filename << "'" << std::endl;
}
}

View File

@ -289,13 +289,6 @@ namespace Opm
return parallelOutput_->isIORank();
}
void restore(SimulatorTimerInterface& timer,
BlackoilState& state,
WellStateFullyImplicitBlackoil& wellState,
const std::string& filename,
const int desiredReportStep);
template <class Grid, class WellState>
void initFromRestartFile(const PhaseUsage& phaseUsage,
const Grid& grid,
@ -315,9 +308,6 @@ namespace Opm
const std::string outputDir_;
const bool restart_double_si_;
int lastBackupReportStep_;
std::ofstream backupfile_;
Opm::PhaseUsage phaseUsage_;
std::unique_ptr< BlackoilSubWriter > vtkWriter_;
std::unique_ptr< BlackoilSubWriter > matlabWriter_;
@ -354,7 +344,6 @@ namespace Opm
parallelOutput_( output_ ? new ParallelDebugOutput< Grid >( grid, eclipseState, schedule, phaseUsage.num_phases, phaseUsage ) : 0 ),
outputDir_( eclipseState.getIOConfig().getOutputDir() ),
restart_double_si_( output_ ? param.getDefault("restart_double_si", false) : false ),
lastBackupReportStep_( -1 ),
phaseUsage_( phaseUsage ),
eclipseState_(eclipseState),
schedule_(schedule),
@ -390,12 +379,6 @@ namespace Opm
// Ensure that output dir exists
ensureDirectoryExists(outputDir_);
std::string backupfilename = param.getDefault("backupfile", std::string("") );
if( ! backupfilename.empty() )
{
backupfile_.open( backupfilename.c_str() );
}
}
// create output thread if enabled and rank is I/O rank