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  Copyright (C) 2009-2013 by Andreas Lauser
5 
6  This file is part of the Open Porous Media project (OPM).
7 
8  OPM is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 2 of the License, or
11  (at your option) any later version.
12 
13  OPM is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with OPM. If not, see <http://www.gnu.org/licenses/>.
20 */
25 #ifndef EWOMS_RESTART_HH
26 #define EWOMS_RESTART_HH
27 
28 #include <opm/common/Exceptions.hpp>
29 #include <opm/common/ErrorMacros.hpp>
30 
31 #include <string>
32 #include <fstream>
33 #include <iostream>
34 #include <sstream>
35 
36 namespace Ewoms {
37 
41 class Restart
42 {
47  template <class GridView>
48  static const std::string magicRestartCookie_(const GridView &gridView)
49  {
50  static const std::string gridName = "blubb"; // gridView.grid().name();
51  static 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 << "eWoms 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 
73  template <class GridView>
74  static const std::string restartFileName_(const GridView &gridView,
75  const std::string &simName,
76  double t)
77  {
78  int rank = gridView.comm().rank();
79  std::ostringstream oss;
80  oss << simName << "_time=" << t << "_rank=" << rank << ".ers";
81  return oss.str();
82  }
83 
84 public:
88  const std::string &fileName() const
89  { return fileName_; }
90 
94  template <class Simulator>
95  void serializeBegin(Simulator &simulator)
96  {
97  const std::string magicCookie = magicRestartCookie_(simulator.gridView());
98  fileName_ = restartFileName_(simulator.gridView(),
99  simulator.problem().name(),
100  simulator.time());
101 
102  // open output file and write magic cookie
103  outStream_.open(fileName_.c_str());
104  outStream_.precision(20);
105 
106  serializeSectionBegin(magicCookie);
108  }
109 
113  std::ostream &serializeStream()
114  { return outStream_; }
115 
119  void serializeSectionBegin(const std::string &cookie)
120  { outStream_ << cookie << "\n"; }
121 
126  { outStream_ << "\n"; }
127 
133  template <int codim, class Serializer, class GridView>
134  void serializeEntities(Serializer &serializer, const GridView &gridView)
135  {
136  std::ostringstream oss;
137  oss << "Entities: Codim " << codim;
138  std::string cookie = oss.str();
139  serializeSectionBegin(cookie);
140 
141  // write element data
142  typedef typename GridView::template Codim<codim>::Iterator Iterator;
143 
144  Iterator it = gridView.template begin<codim>();
145  const Iterator &endIt = gridView.template end<codim>();
146  for (; it != endIt; ++it) {
147  serializer.serializeEntity(outStream_, *it);
148  outStream_ << "\n";
149  }
150 
152  }
153 
158  { outStream_.close(); }
159 
164  template <class Simulator>
165  void deserializeBegin(Simulator &simulator, double t)
166  {
167  fileName_ = restartFileName_(simulator.gridView(), simulator.problem().name(), t);
168 
169  // open input file and read magic cookie
170  inStream_.open(fileName_.c_str());
171  if (!inStream_.good()) {
172  OPM_THROW(std::runtime_error, "Restart file '" << fileName_
173  << "' could not be opened properly");
174  }
175 
176  // make sure that we don't open an empty file
177  inStream_.seekg(0, std::ios::end);
178  int pos = inStream_.tellg();
179  if (pos == 0) {
180  OPM_THROW(std::runtime_error,
181  "Restart file '" << fileName_ << "' is empty");
182  }
183  inStream_.seekg(0, std::ios::beg);
184 
185  const std::string magicCookie = magicRestartCookie_(simulator.gridView());
186 
187  deserializeSectionBegin(magicCookie);
189  }
190 
195  std::istream &deserializeStream()
196  { return inStream_; }
197 
201  void deserializeSectionBegin(const std::string &cookie)
202  {
203  if (!inStream_.good())
204  OPM_THROW(std::runtime_error,
205  "Encountered unexpected EOF in restart file.");
206  std::string buf;
207  std::getline(inStream_, buf);
208  if (buf != cookie)
209  OPM_THROW(std::runtime_error,
210  "Could not start section '" << cookie << "'");
211  }
212 
217  {
218  std::string dummy;
219  std::getline(inStream_, dummy);
220  for (unsigned i = 0; i < dummy.length(); ++i) {
221  if (!std::isspace(dummy[i])) {
222  OPM_THROW(std::logic_error,
223  "Encountered unread values while deserializing");
224  }
225  }
226  }
227 
233  template <int codim, class Deserializer, class GridView>
234  void deserializeEntities(Deserializer &deserializer, const GridView &gridView)
235  {
236  std::ostringstream oss;
237  oss << "Entities: Codim " << codim;
238  std::string cookie = oss.str();
239  deserializeSectionBegin(cookie);
240 
241  std::string curLine;
242 
243  // read entity data
244  typedef typename GridView::template Codim<codim>::Iterator Iterator;
245  Iterator it = gridView.template begin<codim>();
246  const Iterator &endIt = gridView.template end<codim>();
247  for (; it != endIt; ++it) {
248  if (!inStream_.good()) {
249  OPM_THROW(std::runtime_error, "Restart file is corrupted");
250  }
251 
252  std::getline(inStream_, curLine);
253  std::istringstream curLineStream(curLine);
254  deserializer.deserializeEntity(curLineStream, *it);
255  }
256 
258  }
259 
264  { inStream_.close(); }
265 
266 private:
267  std::string fileName_;
268  std::ifstream inStream_;
269  std::ofstream outStream_;
270 };
271 } // namespace Ewoms
272 
273 #endif
void deserializeBegin(Simulator &simulator, double t)
Start reading a restart file at a certain simulated time.
Definition: restart.hh:165
Problem & problem()
Return the object which specifies the pysical setup of the simulation.
Definition: simulator.hh:189
void serializeBegin(Simulator &simulator)
Write the current state of the model to disk.
Definition: restart.hh:95
const std::string & fileName() const
Returns the name of the file which is (de-)serialized.
Definition: restart.hh:88
void deserializeSectionBegin(const std::string &cookie)
Start reading a new section of the restart file.
Definition: restart.hh:201
void serializeSectionBegin(const std::string &cookie)
Start a new section in the serialized output.
Definition: restart.hh:119
Load or save a state of a problem to/from the harddisk.
Definition: restart.hh:41
void serializeEntities(Serializer &serializer, const GridView &gridView)
Serialize all leaf entities of a codim in a gridView.
Definition: restart.hh:134
std::istream & deserializeStream()
The input stream to read the data which ought to be deserialized.
Definition: restart.hh:195
void deserializeEntities(Deserializer &deserializer, const GridView &gridView)
Deserialize all leaf entities of a codim in a grid.
Definition: restart.hh:234
void serializeEnd()
Finish the restart file.
Definition: restart.hh:157
Manages the initializing and running of time dependent problems.
Definition: simulator.hh:73
Definition: baseauxiliarymodule.hh:35
void deserializeSectionEnd()
End of a section in the serialized output.
Definition: restart.hh:216
void deserializeEnd()
Stop reading the restart file.
Definition: restart.hh:263
Scalar time() const
Return the number of seconds of simulated time which have elapsed since the start time...
Definition: simulator.hh:241
GridView & gridView()
Return the grid view for which the simulation is done.
Definition: simulator.hh:164
void serializeSectionEnd()
End of a section in the serialized output.
Definition: restart.hh:125
std::ostream & serializeStream()
The output stream to write the serialized data.
Definition: restart.hh:113