12#ifndef DUMUX_RESTART_HH
13#define DUMUX_RESTART_HH
15#include <dune/common/exceptions.hh>
16#include <dune/common/fvector.hh>
17#include <dune/istl/bvector.hh>
18#include <dune/grid/common/rangegenerators.hh>
35 template <
class Gr
idView>
36 static const std::string magicRestartCookie_(
const GridView &gridView)
38 const std::string gridName =
"blubb";
39 const int dim = GridView::dimension;
41 int numVertices = gridView.size(dim);
42 int numElements = gridView.size(0);
43 int numEdges = gridView.size(dim-1);
44 int numCPUs = gridView.comm().size();
45 int rank = gridView.comm().rank();
47 std::ostringstream oss;
48 oss <<
"DuMux restart file: "
49 <<
"gridName='"<<gridName<<
"' "
50 <<
"numCPUs="<<numCPUs<<
" "
51 <<
"myRank="<<rank<<
" "
52 <<
"numElements="<<numElements<<
" "
53 <<
"numEdges="<<numEdges<<
" "
54 <<
"numVertices="<<numVertices;
59 template <
class Gr
idView>
60 static const std::string restartFileName_(
const GridView &gridView,
61 const std::string &simName,
64 int rank = gridView.comm().rank();
65 std::ostringstream oss;
66 oss << simName<<
"_time="<<t<<
"_rank="<<rank<<
".drs";
81 template <
class Problem>
84 const std::string magicCookie = magicRestartCookie_(problem.gridView());
85 fileName_ = restartFileName_(problem.gridView(),
87 problem.timeManager().time()+problem.timeManager().timeStepSize());
90 outStream_.open(fileName_.c_str());
91 outStream_.precision(20);
110 outStream_ << cookie <<
"\n";
117 { outStream_ <<
"\n"; }
124 template <
int codim,
class Serializer,
class Gr
idView>
126 const GridView &gridView)
128 std::ostringstream oss;
129 oss <<
"Entities: Codim " << codim;
130 std::string cookie = oss.str();
134 for (
const auto& entity : entities(gridView, Dune::Codim<codim>())) {
135 serializer.serializeEntity(outStream_, entity);
155 template <
class Problem>
158 fileName_ = restartFileName_(problem.gridView(),
164 inStream_.open(fileName_.c_str());
165 if (!inStream_.good()) {
166 DUNE_THROW(Dune::IOError,
169 <<
"' could not be opened properly");
173 inStream_.seekg(0, std::ios::end);
174 int pos = inStream_.tellg();
176 DUNE_THROW(Dune::IOError,
181 inStream_.seekg(0, std::ios::beg);
183 const std::string magicCookie =
184 magicRestartCookie_(problem.gridView());
204 if (!inStream_.good())
205 DUNE_THROW(Dune::IOError,
206 "Encountered unexpected EOF in restart file.");
208 std::getline(inStream_, buf);
210 DUNE_THROW(Dune::IOError,
211 "Could not start section '" << cookie <<
"'");
220 std::getline(inStream_, dummy);
221 for (
unsigned int i = 0; i < dummy.length(); ++i) {
222 if (!std::isspace(dummy[i])) {
223 DUNE_THROW(Dune::InvalidStateException,
224 "Encountered unread values while deserializing");
235 template <
int codim,
class Deserializer,
class Gr
idView>
237 const GridView &gridView)
239 std::ostringstream oss;
240 oss <<
"Entities: Codim " << codim;
241 std::string cookie = oss.str();
247 for (
const auto& entity : entities(gridView, Dune::Codim<codim>())) {
248 if (!inStream_.good()) {
249 DUNE_THROW(Dune::IOError,
250 "Restart file is corrupted");
253 std::getline(inStream_, curLine);
254 std::istringstream curLineStream(curLine);
255 deserializer.deserializeEntity(curLineStream, entity);
273 const std::string directory=
".")
275 DUNE_THROW(Dune::NotImplemented,
276 "Restart::restartFileList()");
281 std::string fileName_;
282 std::ifstream inStream_;
283 std::ofstream outStream_;
Load or save a state of a model to/from the harddisk.
Definition: restart.hh:32
void deserializeBegin(Problem &problem, double t)
Start reading a restart file at a certain simulated time.
Definition: restart.hh:156
void serializeSectionBegin(const std::string &cookie)
Start a new section in the serialized output.
Definition: restart.hh:108
void deserializeEnd()
Stop reading the restart file.
Definition: restart.hh:264
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:217
std::ostream & serializeStream()
The output stream to write the serialized data.
Definition: restart.hh:100
const std::string & fileName() const
Returns the name of the file which is (de-)serialized.
Definition: restart.hh:75
void deserializeSectionBegin(const std::string &cookie)
Start reading a new section of the restart file.
Definition: restart.hh:202
void serializeEnd()
Finish the restart file.
Definition: restart.hh:145
std::istream & deserializeStream()
The input stream to read the data which ought to be deserialized.
Definition: restart.hh:194
void serializeSectionEnd()
End of a section in the serialized output.
Definition: restart.hh:116
static void restartFileList(std::list< std::string > &fileList, const std::string directory=".")
Returns the list of restart files in the current directory.
Definition: restart.hh:272
void serializeBegin(Problem &problem)
Write the current state of the model to disk.
Definition: restart.hh:82
void serializeEntities(Serializer &serializer, const GridView &gridView)
Serialize all leaf entities of a codim in a gridView.
Definition: restart.hh:125