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 <opm/common/Exceptions.hpp>
31 #include <opm/common/ErrorMacros.hpp>
32 
33 #include <string>
34 #include <fstream>
35 #include <iostream>
36 #include <sstream>
37 
38 namespace Ewoms {
39 
43 class Restart
44 {
49  template <class GridView>
50  static const std::string magicRestartCookie_(const GridView& gridView)
51  {
52  static const std::string gridName = "blubb"; // gridView.grid().name();
53  static const int dim = GridView::dimension;
54 
55  int numVertices = gridView.size(dim);
56  int numElements = gridView.size(0);
57  int numEdges = gridView.size(dim - 1);
58  int numCPUs = gridView.comm().size();
59  int rank = gridView.comm().rank();
60 
61  std::ostringstream oss;
62  oss << "eWoms restart file: "
63  << "gridName='" << gridName << "' "
64  << "numCPUs=" << numCPUs << " "
65  << "myRank=" << rank << " "
66  << "numElements=" << numElements << " "
67  << "numEdges=" << numEdges << " "
68  << "numVertices=" << numVertices;
69  return oss.str();
70  }
71 
75  template <class GridView, class Scalar>
76  static const std::string restartFileName_(const GridView& gridView,
77  const std::string& simName,
78  Scalar t)
79  {
80  int rank = gridView.comm().rank();
81  std::ostringstream oss;
82  oss << simName << "_time=" << t << "_rank=" << rank << ".ers";
83  return oss.str();
84  }
85 
86 public:
90  const std::string& fileName() const
91  { return fileName_; }
92 
96  template <class Simulator>
97  void serializeBegin(Simulator& simulator)
98  {
99  const std::string magicCookie = magicRestartCookie_(simulator.gridView());
100  fileName_ = restartFileName_(simulator.gridView(),
101  simulator.problem().name(),
102  simulator.time());
103 
104  // open output file and write magic cookie
105  outStream_.open(fileName_.c_str());
106  outStream_.precision(20);
107 
108  serializeSectionBegin(magicCookie);
110  }
111 
115  std::ostream& serializeStream()
116  { return outStream_; }
117 
121  void serializeSectionBegin(const std::string& cookie)
122  { outStream_ << cookie << "\n"; }
123 
128  { outStream_ << "\n"; }
129 
135  template <int codim, class Serializer, class GridView>
136  void serializeEntities(Serializer& serializer, const GridView& gridView)
137  {
138  std::ostringstream oss;
139  oss << "Entities: Codim " << codim;
140  std::string cookie = oss.str();
141  serializeSectionBegin(cookie);
142 
143  // write element data
144  typedef typename GridView::template Codim<codim>::Iterator Iterator;
145 
146  Iterator it = gridView.template begin<codim>();
147  const Iterator& endIt = gridView.template end<codim>();
148  for (; it != endIt; ++it) {
149  serializer.serializeEntity(outStream_, *it);
150  outStream_ << "\n";
151  }
152 
154  }
155 
160  { outStream_.close(); }
161 
166  template <class Simulator, class Scalar>
167  void deserializeBegin(Simulator& simulator, Scalar t)
168  {
169  fileName_ = restartFileName_(simulator.gridView(), simulator.problem().name(), t);
170 
171  // open input file and read magic cookie
172  inStream_.open(fileName_.c_str());
173  if (!inStream_.good()) {
174  OPM_THROW(std::runtime_error, "Restart file '" << fileName_
175  << "' could not be opened properly");
176  }
177 
178  // make sure that we don't open an empty file
179  inStream_.seekg(0, std::ios::end);
180  auto pos = inStream_.tellg();
181  if (pos == 0) {
182  OPM_THROW(std::runtime_error,
183  "Restart file '" << fileName_ << "' is empty");
184  }
185  inStream_.seekg(0, std::ios::beg);
186 
187  const std::string magicCookie = magicRestartCookie_(simulator.gridView());
188 
189  deserializeSectionBegin(magicCookie);
191  }
192 
197  std::istream& deserializeStream()
198  { return inStream_; }
199 
203  void deserializeSectionBegin(const std::string& cookie)
204  {
205  if (!inStream_.good())
206  OPM_THROW(std::runtime_error,
207  "Encountered unexpected EOF in restart file.");
208  std::string buf;
209  std::getline(inStream_, buf);
210  if (buf != cookie)
211  OPM_THROW(std::runtime_error,
212  "Could not start section '" << cookie << "'");
213  }
214 
219  {
220  std::string dummy;
221  std::getline(inStream_, dummy);
222  for (unsigned i = 0; i < dummy.length(); ++i) {
223  if (!std::isspace(dummy[i])) {
224  OPM_THROW(std::logic_error,
225  "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();
241  deserializeSectionBegin(cookie);
242 
243  std::string curLine;
244 
245  // read entity data
246  typedef typename GridView::template Codim<codim>::Iterator 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  OPM_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 
268 private:
269  std::string fileName_;
270  std::ifstream inStream_;
271  std::ofstream outStream_;
272 };
273 } // namespace Ewoms
274 
275 #endif
void serializeSectionBegin(const std::string &cookie)
Start a new section in the serialized output.
Definition: restart.hh:121
void serializeBegin(Simulator &simulator)
Write the current state of the model to disk.
Definition: restart.hh:97
void deserializeEnd()
Stop reading the restart file.
Definition: restart.hh:265
Definition: baseauxiliarymodule.hh:37
std::ostream & serializeStream()
The output stream to write the serialized data.
Definition: restart.hh:115
Problem & problem()
Return the object which specifies the pysical setup of the simulation.
Definition: simulator.hh:202
void serializeEntities(Serializer &serializer, const GridView &gridView)
Serialize all leaf entities of a codim in a gridView.
Definition: restart.hh:136
const std::string & fileName() const
Returns the name of the file which is (de-)serialized.
Definition: restart.hh:90
void deserializeSectionEnd()
End of a section in the serialized output.
Definition: restart.hh:218
const GridView & gridView() const
Return the grid view for which the simulation is done.
Definition: simulator.hh:183
Load or save a state of a problem to/from the harddisk.
Definition: restart.hh:43
void serializeSectionEnd()
End of a section in the serialized output.
Definition: restart.hh:127
Manages the initializing and running of time dependent problems.
Definition: simulator.hh:75
void deserializeBegin(Simulator &simulator, Scalar t)
Start reading a restart file at a certain simulated time.
Definition: restart.hh:167
void serializeEnd()
Finish the restart file.
Definition: restart.hh:159
void deserializeSectionBegin(const std::string &cookie)
Start reading a new section of the restart file.
Definition: restart.hh:203
Scalar time() const
Return the number of seconds of simulated time which have elapsed since the start time...
Definition: simulator.hh:254
std::istream & deserializeStream()
The input stream to read the data which ought to be deserialized.
Definition: restart.hh:197
void deserializeEntities(Deserializer &deserializer, const GridView &gridView)
Deserialize all leaf entities of a codim in a grid.
Definition: restart.hh:236