version 3.8
restart.hh
Go to the documentation of this file.
1// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2// vi: set et ts=4 sw=4 sts=4:
3//
4// SPDX-FileCopyrightInfo: Copyright © DuMux Project contributors, see AUTHORS.md in root folder
5// SPDX-License-Identifier: GPL-3.0-or-later
6//
12#ifndef DUMUX_RESTART_HH
13#define DUMUX_RESTART_HH
14
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>
19
20#include <list>
21#include <string>
22#include <iostream>
23#include <fstream>
24#include <sstream>
25
26namespace Dumux {
27
32class Restart {
35 template <class GridView>
36 static const std::string magicRestartCookie_(const GridView &gridView)
37 {
38 const std::string gridName = "blubb"; //gridView.grid().name();
39 const int dim = GridView::dimension;
40
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();
46
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;
55 return oss.str();
56 }
57
59 template <class GridView>
60 static const std::string restartFileName_(const GridView &gridView,
61 const std::string &simName,
62 double t)
63 {
64 int rank = gridView.comm().rank();
65 std::ostringstream oss;
66 oss << simName<<"_time="<<t<<"_rank="<<rank<<".drs";
67 return oss.str();
68 }
69
70
71public:
75 const std::string &fileName() const
76 { return fileName_; }
77
81 template <class Problem>
82 void serializeBegin(Problem &problem)
83 {
84 const std::string magicCookie = magicRestartCookie_(problem.gridView());
85 fileName_ = restartFileName_(problem.gridView(),
86 problem.name(),
87 problem.timeManager().time()+problem.timeManager().timeStepSize());
88
89 // open output file and write magic cookie
90 outStream_.open(fileName_.c_str());
91 outStream_.precision(20);
92
93 serializeSectionBegin(magicCookie);
95 }
96
100 std::ostream &serializeStream()
101 {
102 return outStream_;
103 }
104
108 void serializeSectionBegin(const std::string &cookie)
109 {
110 outStream_ << cookie << "\n";
111 }
112
117 { outStream_ << "\n"; }
118
124 template <int codim, class Serializer, class GridView>
125 void serializeEntities(Serializer &serializer,
126 const GridView &gridView)
127 {
128 std::ostringstream oss;
129 oss << "Entities: Codim " << codim;
130 std::string cookie = oss.str();
131 serializeSectionBegin(cookie);
132
133 // write entity data
134 for (const auto& entity : entities(gridView, Dune::Codim<codim>())) {
135 serializer.serializeEntity(outStream_, entity);
136 outStream_ << "\n";
137 }
138
140 }
141
146 {
147 outStream_.close();
148 }
149
150
155 template <class Problem>
156 void deserializeBegin(Problem &problem, double t)
157 {
158 fileName_ = restartFileName_(problem.gridView(),
159 problem.name(),
160 t);
161
162
163 // open input file and read magic cookie
164 inStream_.open(fileName_.c_str());
165 if (!inStream_.good()) {
166 DUNE_THROW(Dune::IOError,
167 "Restart file '"
168 << fileName_
169 << "' could not be opened properly");
170 }
171
172 // make sure that we don't open an empty file
173 inStream_.seekg(0, std::ios::end);
174 int pos = inStream_.tellg();
175 if (pos == 0) {
176 DUNE_THROW(Dune::IOError,
177 "Restart file '"
178 << fileName_
179 << "' is empty");
180 }
181 inStream_.seekg(0, std::ios::beg);
182
183 const std::string magicCookie =
184 magicRestartCookie_(problem.gridView());
185
186 deserializeSectionBegin(magicCookie);
188 }
189
194 std::istream &deserializeStream()
195 {
196 return inStream_;
197 }
198
202 void deserializeSectionBegin(const std::string &cookie)
203 {
204 if (!inStream_.good())
205 DUNE_THROW(Dune::IOError,
206 "Encountered unexpected EOF in restart file.");
207 std::string buf;
208 std::getline(inStream_, buf);
209 if (buf != cookie)
210 DUNE_THROW(Dune::IOError,
211 "Could not start section '" << cookie << "'");
212 }
213
218 {
219 std::string dummy;
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");
225 }
226 }
227 }
228
229
235 template <int codim, class Deserializer, class GridView>
236 void deserializeEntities(Deserializer &deserializer,
237 const GridView &gridView)
238 {
239 std::ostringstream oss;
240 oss << "Entities: Codim " << codim;
241 std::string cookie = oss.str();
243
244 std::string curLine;
245
246 // read entity data
247 for (const auto& entity : entities(gridView, Dune::Codim<codim>())) {
248 if (!inStream_.good()) {
249 DUNE_THROW(Dune::IOError,
250 "Restart file is corrupted");
251 }
252
253 std::getline(inStream_, curLine);
254 std::istringstream curLineStream(curLine);
255 deserializer.deserializeEntity(curLineStream, entity);
256 }
257
259 }
260
265 {
266 inStream_.close();
267 }
268
272 static void restartFileList(std::list<std::string> &fileList,
273 const std::string directory=".")
274 {
275 DUNE_THROW(Dune::NotImplemented,
276 "Restart::restartFileList()");
277 }
278
279
280private:
281 std::string fileName_;
282 std::ifstream inStream_;
283 std::ofstream outStream_;
284};
285}
286
287#endif
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
Definition: adapt.hh:17