25 #ifndef EWOMS_RESTART_HH
26 #define EWOMS_RESTART_HH
28 #include <opm/common/Exceptions.hpp>
29 #include <opm/common/ErrorMacros.hpp>
47 template <
class Gr
idView>
48 static const std::string magicRestartCookie_(
const GridView &gridView)
50 static const std::string gridName =
"blubb";
51 static const int dim = GridView::dimension;
53 int numVertices = gridView.size(dim);
54 int numElements = gridView.size(0);
55 int numEdges = gridView.size(dim - 1);
56 int numCPUs = gridView.comm().size();
57 int rank = gridView.comm().rank();
59 std::ostringstream oss;
60 oss <<
"eWoms restart file: "
61 <<
"gridName='" << gridName <<
"' "
62 <<
"numCPUs=" << numCPUs <<
" "
63 <<
"myRank=" << rank <<
" "
64 <<
"numElements=" << numElements <<
" "
65 <<
"numEdges=" << numEdges <<
" "
66 <<
"numVertices=" << numVertices;
73 template <
class Gr
idView>
74 static const std::string restartFileName_(
const GridView &gridView,
75 const std::string &simName,
78 int rank = gridView.comm().rank();
79 std::ostringstream oss;
80 oss << simName <<
"_time=" << t <<
"_rank=" << rank <<
".ers";
94 template <
class Simulator>
97 const std::string magicCookie = magicRestartCookie_(simulator.
gridView());
98 fileName_ = restartFileName_(simulator.
gridView(),
103 outStream_.open(fileName_.c_str());
104 outStream_.precision(20);
114 {
return outStream_; }
120 { outStream_ << cookie <<
"\n"; }
126 { outStream_ <<
"\n"; }
133 template <
int codim,
class Serializer,
class Gr
idView>
136 std::ostringstream oss;
137 oss <<
"Entities: Codim " << codim;
138 std::string cookie = oss.str();
142 typedef typename GridView::template Codim<codim>::Iterator Iterator;
144 Iterator it = gridView.template begin<codim>();
145 const Iterator &endIt = gridView.template end<codim>();
146 for (; it != endIt; ++it) {
147 serializer.serializeEntity(outStream_, *it);
158 { outStream_.close(); }
164 template <
class Simulator>
167 fileName_ = restartFileName_(simulator.
gridView(), simulator.
problem().name(), t);
170 inStream_.open(fileName_.c_str());
171 if (!inStream_.good()) {
172 OPM_THROW(std::runtime_error,
"Restart file '" << fileName_
173 <<
"' could not be opened properly");
177 inStream_.seekg(0, std::ios::end);
178 int pos = inStream_.tellg();
180 OPM_THROW(std::runtime_error,
181 "Restart file '" << fileName_ <<
"' is empty");
183 inStream_.seekg(0, std::ios::beg);
185 const std::string magicCookie = magicRestartCookie_(simulator.
gridView());
196 {
return inStream_; }
203 if (!inStream_.good())
204 OPM_THROW(std::runtime_error,
205 "Encountered unexpected EOF in restart file.");
207 std::getline(inStream_, buf);
209 OPM_THROW(std::runtime_error,
210 "Could not start section '" << cookie <<
"'");
219 std::getline(inStream_, dummy);
220 for (
unsigned i = 0; i < dummy.length(); ++i) {
221 if (!std::isspace(dummy[i])) {
222 OPM_THROW(std::logic_error,
223 "Encountered unread values while deserializing");
233 template <
int codim,
class Deserializer,
class Gr
idView>
236 std::ostringstream oss;
237 oss <<
"Entities: Codim " << codim;
238 std::string cookie = oss.str();
244 typedef typename GridView::template Codim<codim>::Iterator Iterator;
245 Iterator it = gridView.template begin<codim>();
246 const Iterator &endIt = gridView.template end<codim>();
247 for (; it != endIt; ++it) {
248 if (!inStream_.good()) {
249 OPM_THROW(std::runtime_error,
"Restart file is corrupted");
252 std::getline(inStream_, curLine);
253 std::istringstream curLineStream(curLine);
254 deserializer.deserializeEntity(curLineStream, *it);
264 { inStream_.close(); }
267 std::string fileName_;
268 std::ifstream inStream_;
269 std::ofstream outStream_;
void deserializeBegin(Simulator &simulator, double t)
Start reading a restart file at a certain simulated time.
Definition: restart.hh:165
Problem & problem()
Return the object which specifies the pysical setup of the simulation.
Definition: simulator.hh:189
void serializeBegin(Simulator &simulator)
Write the current state of the model to disk.
Definition: restart.hh:95
const std::string & fileName() const
Returns the name of the file which is (de-)serialized.
Definition: restart.hh:88
void deserializeSectionBegin(const std::string &cookie)
Start reading a new section of the restart file.
Definition: restart.hh:201
void serializeSectionBegin(const std::string &cookie)
Start a new section in the serialized output.
Definition: restart.hh:119
Load or save a state of a problem to/from the harddisk.
Definition: restart.hh:41
void serializeEntities(Serializer &serializer, const GridView &gridView)
Serialize all leaf entities of a codim in a gridView.
Definition: restart.hh:134
std::istream & deserializeStream()
The input stream to read the data which ought to be deserialized.
Definition: restart.hh:195
void deserializeEntities(Deserializer &deserializer, const GridView &gridView)
Deserialize all leaf entities of a codim in a grid.
Definition: restart.hh:234
void serializeEnd()
Finish the restart file.
Definition: restart.hh:157
Manages the initializing and running of time dependent problems.
Definition: simulator.hh:73
Definition: baseauxiliarymodule.hh:35
void deserializeSectionEnd()
End of a section in the serialized output.
Definition: restart.hh:216
void deserializeEnd()
Stop reading the restart file.
Definition: restart.hh:263
Scalar time() const
Return the number of seconds of simulated time which have elapsed since the start time...
Definition: simulator.hh:241
GridView & gridView()
Return the grid view for which the simulation is done.
Definition: simulator.hh:164
void serializeSectionEnd()
End of a section in the serialized output.
Definition: restart.hh:125
std::ostream & serializeStream()
The output stream to write the serialized data.
Definition: restart.hh:113