27#ifndef EWOMS_RESTART_HH
28#define EWOMS_RESTART_HH
46 template <
class Gr
idView>
47 static const std::string magicRestartCookie_(
const GridView& gridView)
49 static const std::string gridName =
"blubb";
50 static const int dim = GridView::dimension;
52 int numVertices = gridView.size(
dim);
53 int numElements = gridView.size(0);
54 int numEdges = gridView.size(
dim - 1);
55 int numCPUs = gridView.comm().size();
56 int rank = gridView.comm().rank();
58 std::ostringstream oss;
59 oss <<
"eWoms restart file: "
60 <<
"gridName='" << gridName <<
"' "
61 <<
"numCPUs=" << numCPUs <<
" "
62 <<
"myRank=" << rank <<
" "
63 <<
"numElements=" << numElements <<
" "
64 <<
"numEdges=" << numEdges <<
" "
65 <<
"numVertices=" << numVertices;
72 template <
class Gr
idView,
class Scalar>
73 static const std::string restartFileName_(
const GridView& gridView,
74 const std::string& outputDir,
75 const std::string& simName,
78 std::string dir = outputDir;
81 else if (!dir.empty() && dir.back() !=
'/')
84 int rank = gridView.comm().rank();
85 std::ostringstream oss;
86 oss << dir << simName <<
"_time=" << t <<
"_rank=" << rank <<
".ers";
100 template <
class Simulator>
103 const std::string magicCookie = magicRestartCookie_(simulator.
gridView());
104 fileName_ = restartFileName_(simulator.
gridView(),
105 simulator.
problem().outputDir(),
110 outStream_.open(fileName_.c_str());
111 outStream_.precision(20);
121 {
return outStream_; }
127 { outStream_ << cookie <<
"\n"; }
133 { outStream_ <<
"\n"; }
140 template <
int codim,
class Serializer,
class Gr
idView>
143 std::ostringstream oss;
144 oss <<
"Entities: Codim " << codim;
145 std::string cookie = oss.str();
149 using Iterator =
typename GridView::template Codim<codim>::Iterator;
151 Iterator it = gridView.template begin<codim>();
152 const Iterator& endIt = gridView.template end<codim>();
153 for (; it != endIt; ++it) {
154 serializer.serializeEntity(outStream_, *it);
165 { outStream_.close(); }
171 template <
class Simulator,
class Scalar>
174 fileName_ = restartFileName_(simulator.
gridView(), simulator.
problem().outputDir(), simulator.
problem().name(), t);
177 inStream_.open(fileName_.c_str());
178 if (!inStream_.good()) {
179 throw std::runtime_error(
"Restart file '"+fileName_+
"' could not be opened properly");
183 inStream_.seekg(0, std::ios::end);
184 auto pos = inStream_.tellg();
186 throw std::runtime_error(
"Restart file '"+fileName_+
"' is empty");
188 inStream_.seekg(0, std::ios::beg);
190 const std::string magicCookie = magicRestartCookie_(simulator.
gridView());
201 {
return inStream_; }
208 if (!inStream_.good())
209 throw std::runtime_error(
"Encountered unexpected EOF in restart file.");
211 std::getline(inStream_, buf);
213 throw std::runtime_error(
"Could not start section '"+cookie+
"'");
222 std::getline(inStream_, dummy);
223 for (
unsigned i = 0; i < dummy.length(); ++i) {
224 if (!std::isspace(dummy[i])) {
225 throw std::logic_error(
"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 using Iterator =
typename GridView::template Codim<codim>::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 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_;
Load or save a state of a problem to/from the harddisk.
Definition: restart.hh:41
std::istream & deserializeStream()
The input stream to read the data which ought to be deserialized.
Definition: restart.hh:200
void serializeSectionEnd()
End of a section in the serialized output.
Definition: restart.hh:132
std::ostream & serializeStream()
The output stream to write the serialized data.
Definition: restart.hh:120
void serializeBegin(Simulator &simulator)
Write the current state of the model to disk.
Definition: restart.hh:101
void serializeEnd()
Finish the restart file.
Definition: restart.hh:164
const std::string & fileName() const
Returns the name of the file which is (de-)serialized.
Definition: restart.hh:94
void serializeEntities(Serializer &serializer, const GridView &gridView)
Serialize all leaf entities of a codim in a gridView.
Definition: restart.hh:141
void deserializeEntities(Deserializer &deserializer, const GridView &gridView)
Deserialize all leaf entities of a codim in a grid.
Definition: restart.hh:236
void deserializeSectionEnd()
End of a section in the serialized output.
Definition: restart.hh:219
void deserializeBegin(Simulator &simulator, Scalar t)
Start reading a restart file at a certain simulated time.
Definition: restart.hh:172
void serializeSectionBegin(const std::string &cookie)
Start a new section in the serialized output.
Definition: restart.hh:126
void deserializeSectionBegin(const std::string &cookie)
Start reading a new section of the restart file.
Definition: restart.hh:206
void deserializeEnd()
Stop reading the restart file.
Definition: restart.hh:265
Manages the initializing and running of time dependent problems.
Definition: simulator.hh:102
Problem & problem()
Return the object which specifies the pysical setup of the simulation.
Definition: simulator.hh:306
const GridView & gridView() const
Return the grid view for which the simulation is done.
Definition: simulator.hh:287
Scalar time() const
Return the number of seconds of simulated time which have elapsed since the start time.
Definition: simulator.hh:358
static const int dim
Definition: structuredgridvanguard.hh:67
Definition: blackoilboundaryratevector.hh:37