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 This file is part of the Open Porous Media project (OPM).
5
6 OPM 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 2 of the License, or
9 (at your option) any later version.
10
11 OPM 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 OPM. If not, see <http://www.gnu.org/licenses/>.
18
19 Consult the COPYING file in the top-level source directory of this
20 module for the precise wording of the license and the list of
21 copyright holders.
22*/
27#ifndef EWOMS_RESTART_HH
28#define EWOMS_RESTART_HH
29
30#include <string>
31#include <fstream>
32#include <iostream>
33#include <sstream>
34
35namespace Opm {
36
41{
46 template <class GridView>
47 static const std::string magicRestartCookie_(const GridView& gridView)
48 {
49 static const std::string gridName = "blubb"; // gridView.grid().name();
50 static const int dim = GridView::dimension;
51
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();
57
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;
66 return oss.str();
67 }
68
72 template <class GridView, class Scalar>
73 static const std::string restartFileName_(const GridView& gridView,
74 const std::string& outputDir,
75 const std::string& simName,
76 Scalar t)
77 {
78 std::string dir = outputDir;
79 if (dir == ".")
80 dir = "";
81 else if (!dir.empty() && dir.back() != '/')
82 dir += "/";
83
84 int rank = gridView.comm().rank();
85 std::ostringstream oss;
86 oss << dir << simName << "_time=" << t << "_rank=" << rank << ".ers";
87 return oss.str();
88 }
89
90public:
94 const std::string& fileName() const
95 { return fileName_; }
96
100 template <class Simulator>
101 void serializeBegin(Simulator& simulator)
102 {
103 const std::string magicCookie = magicRestartCookie_(simulator.gridView());
104 fileName_ = restartFileName_(simulator.gridView(),
105 simulator.problem().outputDir(),
106 simulator.problem().name(),
107 simulator.time());
108
109 // open output file and write magic cookie
110 outStream_.open(fileName_.c_str());
111 outStream_.precision(20);
112
113 serializeSectionBegin(magicCookie);
115 }
116
120 std::ostream& serializeStream()
121 { return outStream_; }
122
126 void serializeSectionBegin(const std::string& cookie)
127 { outStream_ << cookie << "\n"; }
128
133 { outStream_ << "\n"; }
134
140 template <int codim, class Serializer, class GridView>
141 void serializeEntities(Serializer& serializer, const GridView& gridView)
142 {
143 std::ostringstream oss;
144 oss << "Entities: Codim " << codim;
145 std::string cookie = oss.str();
146 serializeSectionBegin(cookie);
147
148 // write element data
149 using Iterator = typename GridView::template Codim<codim>::Iterator;
150
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);
155 outStream_ << "\n";
156 }
157
159 }
160
165 { outStream_.close(); }
166
171 template <class Simulator, class Scalar>
172 void deserializeBegin(Simulator& simulator, Scalar t)
173 {
174 fileName_ = restartFileName_(simulator.gridView(), simulator.problem().outputDir(), simulator.problem().name(), t);
175
176 // open input file and read magic cookie
177 inStream_.open(fileName_.c_str());
178 if (!inStream_.good()) {
179 throw std::runtime_error("Restart file '"+fileName_+"' could not be opened properly");
180 }
181
182 // make sure that we don't open an empty file
183 inStream_.seekg(0, std::ios::end);
184 auto pos = inStream_.tellg();
185 if (pos == 0) {
186 throw std::runtime_error("Restart file '"+fileName_+"' is empty");
187 }
188 inStream_.seekg(0, std::ios::beg);
189
190 const std::string magicCookie = magicRestartCookie_(simulator.gridView());
191
192 deserializeSectionBegin(magicCookie);
194 }
195
200 std::istream& deserializeStream()
201 { return inStream_; }
202
206 void deserializeSectionBegin(const std::string& cookie)
207 {
208 if (!inStream_.good())
209 throw std::runtime_error("Encountered unexpected EOF in restart file.");
210 std::string buf;
211 std::getline(inStream_, buf);
212 if (buf != cookie)
213 throw std::runtime_error("Could not start section '"+cookie+"'");
214 }
215
220 {
221 std::string dummy;
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");
226 }
227 }
228 }
229
235 template <int codim, class Deserializer, class GridView>
236 void deserializeEntities(Deserializer& deserializer, const GridView& gridView)
237 {
238 std::ostringstream oss;
239 oss << "Entities: Codim " << codim;
240 std::string cookie = oss.str();
242
243 std::string curLine;
244
245 // read entity data
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");
252 }
253
254 std::getline(inStream_, curLine);
255 std::istringstream curLineStream(curLine);
256 deserializer.deserializeEntity(curLineStream, *it);
257 }
258
260 }
261
266 { inStream_.close(); }
267
268private:
269 std::string fileName_;
270 std::ifstream inStream_;
271 std::ofstream outStream_;
272};
273} // namespace Opm
274
275#endif
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:65
Definition: blackoilboundaryratevector.hh:37