20 #ifndef OPM_BACKUPRESTORE_HEADER_INCLUDED
21 #define OPM_BACKUPRESTORE_HEADER_INCLUDED
25 #include <opm/core/simulator/SimulatorState.hpp>
26 #include <opm/core/simulator/BlackoilState.hpp>
33 void writeValue( std::ostream& stream,
const T& value )
35 stream.write( (
const char *) &value,
sizeof( T ) );
39 void readValue( std::istream& stream, T& value )
41 stream.read( (
char *) &value,
sizeof( T ) );
45 template <
class Vector>
46 void writeContainer( std::ostream& stream,
const Vector& vector)
48 typedef typename Vector :: value_type T;
49 unsigned int size = vector.size() *
sizeof( T );
50 writeValue( stream, size );
52 stream.write( (
const char *) vector.data(), size );
59 void writeMap( std::ostream& stream,
const Map& m)
61 const unsigned int mapEntries = m.size();
62 writeValue( stream, mapEntries );
65 const auto& end = m.end();
66 for(
auto it = m.begin(); it != end; ++it )
69 writeContainer( stream, (*it).first );
71 writeContainer( stream, (*it).second );
76 template <
class Container>
77 void resizeContainer( Container& container,
size_t size )
79 container.resize( size );
82 template <
class T,
size_t n>
83 void resizeContainer( std::array<T, n>& ,
size_t size )
85 static_cast<void>(size);
86 assert(
int(size) ==
int(n) );
89 template <
class Container>
90 void readData(std::istream& stream, Container& container,
size_t datasize)
92 stream.read( reinterpret_cast<char*>( container.data() ), datasize );
98 void readData<std::string>(std::istream& stream, std::string& string,
size_t datasize)
100 std::vector<char> raw_data(datasize);
101 readData(stream, raw_data, datasize);
102 string = std::string(raw_data.data(), datasize);
105 template <
class Container>
106 void readContainerImpl( std::istream& stream, Container& container,
const bool adjustSize )
108 typedef typename Container :: value_type T;
110 readValue( stream, dataSize );
111 if( adjustSize && dataSize > 0 ) {
112 resizeContainer( container, dataSize/
sizeof(T) );
115 if( dataSize != container.size() *
sizeof( T ) )
117 OPM_THROW(std::logic_error,
118 "Size of stored data and simulation data does not match"
119 << dataSize <<
" " << (container.size() *
sizeof( T )) );
122 readData(stream, container, dataSize);
126 template <
class Container>
127 void readAndResizeContainer( std::istream& stream, Container& container )
129 readContainerImpl( stream, container,
true );
132 template <
class Container>
133 void readContainer( std::istream& stream, Container& container )
135 readContainerImpl( stream, container,
false );
139 void readMap( std::istream& stream, Map& m)
142 unsigned int mapEntries = 0;
143 readValue( stream, mapEntries );
144 for(
unsigned int entry = 0; entry<mapEntries; ++entry )
146 std::pair< typename Map :: key_type, typename Map :: mapped_type > mapEntry;
148 readAndResizeContainer( stream, mapEntry.first );
150 readContainer( stream, mapEntry.second );
153 m.insert( mapEntry );
157 enum { SimulatorStateId = 0,
160 WellStateFullyImplicitBackoilId = 3 };
162 inline int objectId(
const SimulatorState& ) {
163 return SimulatorStateId;
165 inline int objectId(
const WellState& ) {
168 inline int objectId(
const BlackoilState& ) {
169 return BlackoilStateId;
171 inline int objectId(
const WellStateFullyImplicitBlackoil& ) {
172 return WellStateFullyImplicitBackoilId;
175 template <
class State>
176 void checkObjectId( std::istream& in,
const State& state )
181 if(
id != objectId( state ) ) {
182 OPM_THROW(std::logic_error,
"backup-restore object type mismatch");
189 std::ostream&
operator << (std::ostream& out,
const SimulatorState& state )
192 writeValue( out, objectId( state ) );
194 const int numPhases = state.numPhases();
195 writeValue( out, numPhases );
198 writeContainer( out, state.pressure() );
199 writeContainer( out, state.temperature() );
200 writeContainer( out, state.facepressure() );
201 writeContainer( out, state.faceflux() );
202 writeContainer( out, state.saturation() );
208 std::istream&
operator >> (std::istream& in, SimulatorState& state )
211 checkObjectId( in, state );
214 readValue( in, numPhases );
216 if( numPhases != state.numPhases() )
217 OPM_THROW(std::logic_error,
"num phases wrong");
220 readContainer( in, state.pressure() );
221 readContainer( in, state.temperature() );
222 readContainer( in, state.facepressure() );
223 readContainer( in, state.faceflux() );
224 readContainer( in, state.saturation() );
231 std::ostream&
operator << (std::ostream& out,
const BlackoilState& state )
234 writeValue( out, objectId( state ) );
237 const SimulatorState& simstate =
static_cast< const SimulatorState&
> (state);
241 writeContainer( out, state.surfacevol() );
242 writeContainer( out, state.gasoilratio() );
243 writeContainer( out, state.rv() );
249 std::istream&
operator >> (std::istream& in, BlackoilState& state )
252 checkObjectId( in, state );
255 SimulatorState& simstate =
static_cast< SimulatorState&
> (state);
259 readContainer( in, state.surfacevol() );
260 readContainer( in, state.gasoilratio() );
261 readContainer( in, state.rv() );
268 std::ostream&
operator << (std::ostream& out,
const WellState& state )
271 writeValue( out, objectId( state ) );
274 writeContainer( out, state.bhp() );
275 writeContainer( out, state.temperature() );
276 writeContainer( out, state.wellRates() );
277 writeContainer( out, state.perfRates() );
278 writeContainer( out, state.perfPress() );
287 checkObjectId( in, state );
290 readAndResizeContainer( in, state.bhp() );
291 readAndResizeContainer( in, state.temperature() );
292 readAndResizeContainer( in, state.wellRates() );
293 readAndResizeContainer( in, state.perfRates() );
294 readAndResizeContainer( in, state.perfPress() );
304 writeValue( out, objectId( state ) );
307 const WellState& wellState =
static_cast< const WellState&
> (state);
310 const int numWells = state.numWells();
311 writeValue( out, numWells );
314 const int numPhases = state.numPhases();
315 writeValue( out, numPhases );
320 writeMap( out, state.wellMap() );
330 checkObjectId( in, state );
333 WellState& wellState =
static_cast< WellState&
> (state);
337 readValue( in, numWells );
338 if( numWells != state.numWells() )
339 OPM_THROW(std::logic_error,
"wrong numWells");
344 readValue( in, numPhases );
345 if( numPhases != state.numPhases() )
346 OPM_THROW(std::logic_error,
"wrong numPhases");
351 readMap( in, state.wellMap() );
359 #endif // OPM_BACKUPRESTORE_HEADER_INCLUDED
Definition: AdditionalObjectDeleter.hpp:22
Definition: WellStateFullyImplicitBlackoil.hpp:41
std::vector< double > & perfPhaseRates()
One rate per phase and well connection.
Definition: WellStateFullyImplicitBlackoil.hpp:173
Ostream & operator<<(Ostream &os, const AutoDiff< Scalar > &fw)
Definition: AutoDiff.hpp:141
std::vector< int > & currentControls()
One current control per well.
Definition: WellStateFullyImplicitBlackoil.hpp:177
std::istream & operator>>(std::istream &in, SimulatorState &state)
Definition: BackupRestore.hpp:208