27 #ifndef EWOMS_RESTART_HH
28 #define EWOMS_RESTART_HH
30 #include <opm/common/Exceptions.hpp>
31 #include <opm/common/ErrorMacros.hpp>
49 template <
class Gr
idView>
50 static const std::string magicRestartCookie_(
const GridView& gridView)
52 static const std::string gridName =
"blubb";
53 static const int dim = GridView::dimension;
55 int numVertices = gridView.size(dim);
56 int numElements = gridView.size(0);
57 int numEdges = gridView.size(dim - 1);
58 int numCPUs = gridView.comm().size();
59 int rank = gridView.comm().rank();
61 std::ostringstream oss;
62 oss <<
"eWoms restart file: "
63 <<
"gridName='" << gridName <<
"' "
64 <<
"numCPUs=" << numCPUs <<
" "
65 <<
"myRank=" << rank <<
" "
66 <<
"numElements=" << numElements <<
" "
67 <<
"numEdges=" << numEdges <<
" "
68 <<
"numVertices=" << numVertices;
75 template <
class Gr
idView,
class Scalar>
76 static const std::string restartFileName_(
const GridView& gridView,
77 const std::string& simName,
80 int rank = gridView.comm().rank();
81 std::ostringstream oss;
82 oss << simName <<
"_time=" << t <<
"_rank=" << rank <<
".ers";
96 template <
class Simulator>
99 const std::string magicCookie = magicRestartCookie_(simulator.
gridView());
100 fileName_ = restartFileName_(simulator.
gridView(),
105 outStream_.open(fileName_.c_str());
106 outStream_.precision(20);
116 {
return outStream_; }
122 { outStream_ << cookie <<
"\n"; }
128 { outStream_ <<
"\n"; }
135 template <
int codim,
class Serializer,
class Gr
idView>
138 std::ostringstream oss;
139 oss <<
"Entities: Codim " << codim;
140 std::string cookie = oss.str();
144 typedef typename GridView::template Codim<codim>::Iterator Iterator;
146 Iterator it = gridView.template begin<codim>();
147 const Iterator& endIt = gridView.template end<codim>();
148 for (; it != endIt; ++it) {
149 serializer.serializeEntity(outStream_, *it);
160 { outStream_.close(); }
166 template <
class Simulator,
class Scalar>
169 fileName_ = restartFileName_(simulator.
gridView(), simulator.
problem().name(), t);
172 inStream_.open(fileName_.c_str());
173 if (!inStream_.good()) {
174 OPM_THROW(std::runtime_error,
"Restart file '" << fileName_
175 <<
"' could not be opened properly");
179 inStream_.seekg(0, std::ios::end);
180 auto pos = inStream_.tellg();
182 OPM_THROW(std::runtime_error,
183 "Restart file '" << fileName_ <<
"' is empty");
185 inStream_.seekg(0, std::ios::beg);
187 const std::string magicCookie = magicRestartCookie_(simulator.
gridView());
198 {
return inStream_; }
205 if (!inStream_.good())
206 OPM_THROW(std::runtime_error,
207 "Encountered unexpected EOF in restart file.");
209 std::getline(inStream_, buf);
211 OPM_THROW(std::runtime_error,
212 "Could not start section '" << cookie <<
"'");
221 std::getline(inStream_, dummy);
222 for (
unsigned i = 0; i < dummy.length(); ++i) {
223 if (!std::isspace(dummy[i])) {
224 OPM_THROW(std::logic_error,
225 "Encountered unread values while deserializing");
235 template <
int codim,
class Deserializer,
class Gr
idView>
238 std::ostringstream oss;
239 oss <<
"Entities: Codim " << codim;
240 std::string cookie = oss.str();
246 typedef typename GridView::template Codim<codim>::Iterator Iterator;
247 Iterator it = gridView.template begin<codim>();
248 const Iterator& endIt = gridView.template end<codim>();
249 for (; it != endIt; ++it) {
250 if (!inStream_.good()) {
251 OPM_THROW(std::runtime_error,
"Restart file is corrupted");
254 std::getline(inStream_, curLine);
255 std::istringstream curLineStream(curLine);
256 deserializer.deserializeEntity(curLineStream, *it);
266 { inStream_.close(); }
269 std::string fileName_;
270 std::ifstream inStream_;
271 std::ofstream outStream_;
void serializeSectionBegin(const std::string &cookie)
Start a new section in the serialized output.
Definition: restart.hh:121
void serializeBegin(Simulator &simulator)
Write the current state of the model to disk.
Definition: restart.hh:97
void deserializeEnd()
Stop reading the restart file.
Definition: restart.hh:265
std::ostream & serializeStream()
The output stream to write the serialized data.
Definition: restart.hh:115
Problem & problem()
Return the object which specifies the pysical setup of the simulation.
Definition: simulator.hh:202
const std::string & fileName() const
Returns the name of the file which is (de-)serialized.
Definition: restart.hh:90
void serializeEntities(Serializer &serializer, const GridView &gridView)
Serialize all leaf entities of a codim in a gridView.
Definition: restart.hh:136
void deserializeSectionEnd()
End of a section in the serialized output.
Definition: restart.hh:218
Load or save a state of a problem to/from the harddisk.
Definition: restart.hh:43
void serializeSectionEnd()
End of a section in the serialized output.
Definition: restart.hh:127
Manages the initializing and running of time dependent problems.
Definition: simulator.hh:75
void deserializeBegin(Simulator &simulator, Scalar t)
Start reading a restart file at a certain simulated time.
Definition: restart.hh:167
const GridView & gridView() const
Return the grid view for which the simulation is done.
Definition: simulator.hh:183
void serializeEnd()
Finish the restart file.
Definition: restart.hh:159
void deserializeSectionBegin(const std::string &cookie)
Start reading a new section of the restart file.
Definition: restart.hh:203
Scalar time() const
Return the number of seconds of simulated time which have elapsed since the start time...
Definition: simulator.hh:254
std::istream & deserializeStream()
The input stream to read the data which ought to be deserialized.
Definition: restart.hh:197
void deserializeEntities(Deserializer &deserializer, const GridView &gridView)
Deserialize all leaf entities of a codim in a grid.
Definition: restart.hh:236