24#ifndef DUMUX_RESTART_HH
25#define DUMUX_RESTART_HH
27#include <dune/common/exceptions.hh>
28#include <dune/common/fvector.hh>
29#include <dune/istl/bvector.hh>
30#include <dune/grid/common/rangegenerators.hh>
47 template <
class Gr
idView>
48 static const std::string magicRestartCookie_(
const GridView &gridView)
50 const std::string gridName =
"blubb";
51 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 <<
"DuMux restart file: "
61 <<
"gridName='"<<gridName<<
"' "
62 <<
"numCPUs="<<numCPUs<<
" "
63 <<
"myRank="<<rank<<
" "
64 <<
"numElements="<<numElements<<
" "
65 <<
"numEdges="<<numEdges<<
" "
66 <<
"numVertices="<<numVertices;
71 template <
class Gr
idView>
72 static const std::string restartFileName_(
const GridView &gridView,
73 const std::string &simName,
76 int rank = gridView.comm().rank();
77 std::ostringstream oss;
78 oss << simName<<
"_time="<<t<<
"_rank="<<rank<<
".drs";
93 template <
class Problem>
96 const std::string magicCookie = magicRestartCookie_(problem.gridView());
97 fileName_ = restartFileName_(problem.gridView(),
99 problem.timeManager().time()+problem.timeManager().timeStepSize());
102 outStream_.open(fileName_.c_str());
103 outStream_.precision(20);
122 outStream_ << cookie <<
"\n";
129 { outStream_ <<
"\n"; }
136 template <
int codim,
class Serializer,
class Gr
idView>
138 const GridView &gridView)
140 std::ostringstream oss;
141 oss <<
"Entities: Codim " << codim;
142 std::string cookie = oss.str();
146 for (
const auto& entity : entities(gridView, Dune::Codim<codim>())) {
147 serializer.serializeEntity(outStream_, entity);
167 template <
class Problem>
170 fileName_ = restartFileName_(problem.gridView(),
176 inStream_.open(fileName_.c_str());
177 if (!inStream_.good()) {
178 DUNE_THROW(Dune::IOError,
181 <<
"' could not be opened properly");
185 inStream_.seekg(0, std::ios::end);
186 int pos = inStream_.tellg();
188 DUNE_THROW(Dune::IOError,
193 inStream_.seekg(0, std::ios::beg);
195 const std::string magicCookie =
196 magicRestartCookie_(problem.gridView());
216 if (!inStream_.good())
217 DUNE_THROW(Dune::IOError,
218 "Encountered unexpected EOF in restart file.");
220 std::getline(inStream_, buf);
222 DUNE_THROW(Dune::IOError,
223 "Could not start section '" << cookie <<
"'");
232 std::getline(inStream_, dummy);
233 for (
unsigned int i = 0; i < dummy.length(); ++i) {
234 if (!std::isspace(dummy[i])) {
235 DUNE_THROW(Dune::InvalidStateException,
236 "Encountered unread values while deserializing");
247 template <
int codim,
class Deserializer,
class Gr
idView>
249 const GridView &gridView)
251 std::ostringstream oss;
252 oss <<
"Entities: Codim " << codim;
253 std::string cookie = oss.str();
259 for (
const auto& entity : entities(gridView, Dune::Codim<codim>())) {
260 if (!inStream_.good()) {
261 DUNE_THROW(Dune::IOError,
262 "Restart file is corrupted");
265 std::getline(inStream_, curLine);
266 std::istringstream curLineStream(curLine);
267 deserializer.deserializeEntity(curLineStream, entity);
285 const std::string directory=
".")
287 DUNE_THROW(Dune::NotImplemented,
288 "Restart::restartFileList()");
293 std::string fileName_;
294 std::ifstream inStream_;
295 std::ofstream outStream_;
make the local view function available whenever we use the grid geometry
Definition: adapt.hh:29
Load or save a state of a model to/from the harddisk.
Definition: restart.hh:44
void deserializeBegin(Problem &problem, double t)
Start reading a restart file at a certain simulated time.
Definition: restart.hh:168
void serializeSectionBegin(const std::string &cookie)
Start a new section in the serialized output.
Definition: restart.hh:120
void deserializeEnd()
Stop reading the restart file.
Definition: restart.hh:276
void deserializeEntities(Deserializer &deserializer, const GridView &gridView)
Deserialize all leaf entities of a codim in a grid.
Definition: restart.hh:248
void deserializeSectionEnd()
End of a section in the serialized output.
Definition: restart.hh:229
std::ostream & serializeStream()
The output stream to write the serialized data.
Definition: restart.hh:112
const std::string & fileName() const
Returns the name of the file which is (de-)serialized.
Definition: restart.hh:87
void deserializeSectionBegin(const std::string &cookie)
Start reading a new section of the restart file.
Definition: restart.hh:214
void serializeEnd()
Finish the restart file.
Definition: restart.hh:157
std::istream & deserializeStream()
The input stream to read the data which ought to be deserialized.
Definition: restart.hh:206
void serializeSectionEnd()
End of a section in the serialized output.
Definition: restart.hh:128
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:284
void serializeBegin(Problem &problem)
Write the current state of the model to disk.
Definition: restart.hh:94
void serializeEntities(Serializer &serializer, const GridView &gridView)
Serialize all leaf entities of a codim in a gridView.
Definition: restart.hh:137