3.5-git
DUNE for Multi-{Phase, Component, Scale, Physics, ...} flow and transport in porous media
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 * See the file COPYING for full copying permissions. *
5 * *
6 * This program is free software: you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation, either version 3 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
18 *****************************************************************************/
24#ifndef DUMUX_RESTART_HH
25#define DUMUX_RESTART_HH
26
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>
31
32#include <list>
33#include <string>
34#include <iostream>
35#include <fstream>
36#include <sstream>
37
38namespace Dumux {
39
44class Restart {
47 template <class GridView>
48 static const std::string magicRestartCookie_(const GridView &gridView)
49 {
50 const std::string gridName = "blubb"; //gridView.grid().name();
51 const int dim = GridView::dimension;
52
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();
58
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;
67 return oss.str();
68 }
69
71 template <class GridView>
72 static const std::string restartFileName_(const GridView &gridView,
73 const std::string &simName,
74 double t)
75 {
76 int rank = gridView.comm().rank();
77 std::ostringstream oss;
78 oss << simName<<"_time="<<t<<"_rank="<<rank<<".drs";
79 return oss.str();
80 }
81
82
83public:
87 const std::string &fileName() const
88 { return fileName_; }
89
93 template <class Problem>
94 void serializeBegin(Problem &problem)
95 {
96 const std::string magicCookie = magicRestartCookie_(problem.gridView());
97 fileName_ = restartFileName_(problem.gridView(),
98 problem.name(),
99 problem.timeManager().time()+problem.timeManager().timeStepSize());
100
101 // open output file and write magic cookie
102 outStream_.open(fileName_.c_str());
103 outStream_.precision(20);
104
105 serializeSectionBegin(magicCookie);
107 }
108
112 std::ostream &serializeStream()
113 {
114 return outStream_;
115 }
116
120 void serializeSectionBegin(const std::string &cookie)
121 {
122 outStream_ << cookie << "\n";
123 }
124
129 { outStream_ << "\n"; }
130
136 template <int codim, class Serializer, class GridView>
137 void serializeEntities(Serializer &serializer,
138 const GridView &gridView)
139 {
140 std::ostringstream oss;
141 oss << "Entities: Codim " << codim;
142 std::string cookie = oss.str();
143 serializeSectionBegin(cookie);
144
145 // write entity data
146 for (const auto& entity : entities(gridView, Dune::Codim<codim>())) {
147 serializer.serializeEntity(outStream_, entity);
148 outStream_ << "\n";
149 }
150
152 }
153
158 {
159 outStream_.close();
160 }
161
162
167 template <class Problem>
168 void deserializeBegin(Problem &problem, double t)
169 {
170 fileName_ = restartFileName_(problem.gridView(),
171 problem.name(),
172 t);
173
174
175 // open input file and read magic cookie
176 inStream_.open(fileName_.c_str());
177 if (!inStream_.good()) {
178 DUNE_THROW(Dune::IOError,
179 "Restart file '"
180 << fileName_
181 << "' could not be opened properly");
182 }
183
184 // make sure that we don't open an empty file
185 inStream_.seekg(0, std::ios::end);
186 int pos = inStream_.tellg();
187 if (pos == 0) {
188 DUNE_THROW(Dune::IOError,
189 "Restart file '"
190 << fileName_
191 << "' is empty");
192 }
193 inStream_.seekg(0, std::ios::beg);
194
195 const std::string magicCookie =
196 magicRestartCookie_(problem.gridView());
197
198 deserializeSectionBegin(magicCookie);
200 }
201
206 std::istream &deserializeStream()
207 {
208 return inStream_;
209 }
210
214 void deserializeSectionBegin(const std::string &cookie)
215 {
216 if (!inStream_.good())
217 DUNE_THROW(Dune::IOError,
218 "Encountered unexpected EOF in restart file.");
219 std::string buf;
220 std::getline(inStream_, buf);
221 if (buf != cookie)
222 DUNE_THROW(Dune::IOError,
223 "Could not start section '" << cookie << "'");
224 }
225
230 {
231 std::string dummy;
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");
237 }
238 }
239 }
240
241
247 template <int codim, class Deserializer, class GridView>
248 void deserializeEntities(Deserializer &deserializer,
249 const GridView &gridView)
250 {
251 std::ostringstream oss;
252 oss << "Entities: Codim " << codim;
253 std::string cookie = oss.str();
255
256 std::string curLine;
257
258 // read entity data
259 for (const auto& entity : entities(gridView, Dune::Codim<codim>())) {
260 if (!inStream_.good()) {
261 DUNE_THROW(Dune::IOError,
262 "Restart file is corrupted");
263 }
264
265 std::getline(inStream_, curLine);
266 std::istringstream curLineStream(curLine);
267 deserializer.deserializeEntity(curLineStream, entity);
268 }
269
271 }
272
277 {
278 inStream_.close();
279 }
280
284 static void restartFileList(std::list<std::string> &fileList,
285 const std::string directory=".")
286 {
287 DUNE_THROW(Dune::NotImplemented,
288 "Restart::restartFileList()");
289 }
290
291
292private:
293 std::string fileName_;
294 std::ifstream inStream_;
295 std::ofstream outStream_;
296};
297}
298
299#endif
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