20 #ifndef OPM_BACKUPRESTORE_HEADER_INCLUDED 21 #define OPM_BACKUPRESTORE_HEADER_INCLUDED 24 #include <opm/common/data/SimulationDataContainer.hpp> 27 #include <opm/core/simulator/BlackoilState.hpp> 28 #include <opm/autodiff/WellStateFullyImplicitBlackoil.hpp> 34 void writeValue( std::ostream& stream,
const T& value )
36 stream.write( (
const char *) &value,
sizeof( T ) );
40 void readValue( std::istream& stream, T& value )
42 stream.read( (
char *) &value,
sizeof( T ) );
46 template <
class Vector>
47 void writeContainer( std::ostream& stream,
const Vector& vector)
49 typedef typename Vector :: value_type T;
50 unsigned int size = vector.size() *
sizeof( T );
51 writeValue( stream, size );
53 stream.write( (
const char *) vector.data(), size );
60 void writeMap( std::ostream& stream,
const Map& m)
62 const unsigned int mapEntries = m.size();
63 writeValue( stream, mapEntries );
66 const auto& end = m.end();
67 for(
auto it = m.begin(); it != end; ++it )
70 writeContainer( stream, (*it).first );
72 writeContainer( stream, (*it).second );
77 template <
class Container>
78 void resizeContainer( Container& container,
size_t size )
80 container.resize( size );
83 template <
class T,
size_t n>
84 void resizeContainer( std::array<T, n>& ,
size_t size )
86 static_cast<void>(size);
87 assert(
int(size) ==
int(n) );
90 template <
class Container>
91 void readData(std::istream& stream, Container& container,
size_t datasize)
93 stream.read( reinterpret_cast<char*>( container.data() ), datasize );
99 void readData<std::string>(std::istream& stream, std::string& string,
size_t datasize)
101 std::vector<char> raw_data(datasize);
102 readData(stream, raw_data, datasize);
103 string = std::string(raw_data.data(), datasize);
106 template <
class Container>
107 void readContainerImpl( std::istream& stream, Container& container,
const bool adjustSize )
109 typedef typename Container :: value_type T;
110 unsigned int dataSize = 0;
111 readValue( stream, dataSize );
112 if( adjustSize && dataSize > 0 ) {
113 resizeContainer( container, dataSize/
sizeof(T) );
116 if( dataSize != container.size() *
sizeof( T ) )
118 OPM_THROW(std::logic_error,
119 "Size of stored data and simulation data does not match " 120 << dataSize <<
" " << (container.size() *
sizeof( T )) );
123 readData(stream, container, dataSize);
127 template <
class Container>
128 void readAndResizeContainer( std::istream& stream, Container& container )
130 readContainerImpl( stream, container,
true );
133 template <
class Container>
134 void readContainer( std::istream& stream, Container& container )
136 readContainerImpl( stream, container,
false );
140 void readMap( std::istream& stream, Map& m)
143 unsigned int mapEntries = 0;
144 readValue( stream, mapEntries );
145 for(
unsigned int entry = 0; entry<mapEntries; ++entry )
147 std::pair< typename Map :: key_type, typename Map :: mapped_type > mapEntry;
149 readAndResizeContainer( stream, mapEntry.first );
151 readContainer( stream, mapEntry.second );
154 m.insert( mapEntry );
158 enum { SimulatorStateId = 0,
160 WellStateFullyImplicitBackoilId = 3 };
162 inline int objectId(
const SimulationDataContainer& ) {
163 return SimulatorStateId;
166 inline int objectId(
const WellState& ) {
170 inline int objectId(
const WellStateFullyImplicitBlackoil& ) {
171 return WellStateFullyImplicitBackoilId;
174 template <
class State>
175 void checkObjectId( std::istream& in,
const State& state )
180 if(
id != objectId( state ) ) {
181 OPM_THROW(std::logic_error,
"backup-restore object type mismatch");
188 std::ostream& operator << (std::ostream& out,
const SimulationDataContainer& state )
191 writeValue( out, objectId( state ) );
193 const int numPhases = state.numPhases();
194 writeValue( out, numPhases );
197 writeContainer( out, state.pressure() );
198 writeContainer( out, state.temperature() );
199 writeContainer( out, state.facepressure() );
200 writeContainer( out, state.faceflux() );
201 writeContainer( out, state.saturation() );
207 std::istream& operator >> (std::istream& in, SimulationDataContainer& state )
210 checkObjectId( in, state );
213 readValue( in, numPhases );
215 if( numPhases != (
int) state.numPhases() )
216 OPM_THROW(std::logic_error,
"num phases wrong");
219 readContainer( in, state.pressure() );
220 readContainer( in, state.temperature() );
221 readContainer( in, state.facepressure() );
222 readContainer( in, state.faceflux() );
223 readContainer( in, state.saturation() );
230 std::ostream& operator << (std::ostream& out,
const WellState& state )
233 writeValue( out, objectId( state ) );
236 writeContainer( out, state.bhp() );
237 writeContainer( out, state.temperature() );
238 writeContainer( out, state.wellRates() );
239 writeContainer( out, state.perfRates() );
240 writeContainer( out, state.perfPress() );
246 std::istream& operator >> (std::istream& in, WellState& state )
249 checkObjectId( in, state );
252 readAndResizeContainer( in, state.bhp() );
253 readAndResizeContainer( in, state.temperature() );
254 readAndResizeContainer( in, state.wellRates() );
255 readAndResizeContainer( in, state.perfRates() );
256 readAndResizeContainer( in, state.perfPress() );
263 std::ostream& operator << (std::ostream& out,
const WellStateFullyImplicitBlackoil& state )
266 writeValue( out, objectId( state ) );
269 const WellState& wellState =
static_cast< const WellState&
> (state);
272 const int numWells = state.numWells();
273 writeValue( out, numWells );
276 const int numPhases = state.numPhases();
277 writeValue( out, numPhases );
280 writeContainer( out, state.perfPhaseRates() );
281 writeContainer( out, state.currentControls() );
282 writeMap( out, state.wellMap() );
289 std::istream& operator >> (std::istream& in, WellStateFullyImplicitBlackoil& state )
292 checkObjectId( in, state );
295 WellState& wellState =
static_cast< WellState&
> (state);
299 readValue( in, numWells );
300 if( numWells != state.numWells() )
301 OPM_THROW(std::logic_error,
"wrong numWells");
306 readValue( in, numPhases );
307 if( numPhases != state.numPhases() )
308 OPM_THROW(std::logic_error,
"wrong numPhases");
311 readAndResizeContainer( in, state.perfPhaseRates() );
312 readAndResizeContainer( in, state.currentControls() );
313 readMap( in, state.wellMap() );
321 #endif // OPM_BACKUPRESTORE_HEADER_INCLUDED This file contains a set of helper functions used by VFPProd / VFPInj.
Definition: AdditionalObjectDeleter.hpp:22