restart.hpp
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 OPM_RESTART_HPP
28#define OPM_RESTART_HPP
29
30#include <string>
31#include <fstream>
32#include <iostream>
33#include <sstream>
34
35namespace Opm {
36
41{
46 template <class GridView>
47 static 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 static std::string restartFileName_(int rank,
73 const std::string& outputDir,
74 const std::string& simName,
75 double t);
76
77public:
81 const std::string& fileName() const
82 { return fileName_; }
83
87 template <class Simulator>
88 void serializeBegin(Simulator& simulator)
89 {
90 fileName_ = restartFileName_(simulator.gridView().comm().rank(),
91 simulator.problem().outputDir(),
92 simulator.problem().name(),
93 simulator.time());
94
95 // open output file and write magic cookie
96 openOutputStream(magicRestartCookie_(simulator.gridView()));
97 }
98
102 std::ostream& serializeStream()
103 { return outStream_; }
104
108 void serializeSectionBegin(const std::string& cookie);
109
114
120 template <int codim, class Serializer, class GridView>
121 void serializeEntities(Serializer& serializer, const GridView& gridView)
122 {
123 std::ostringstream oss;
124 oss << "Entities: Codim " << codim;
125 std::string cookie = oss.str();
126 serializeSectionBegin(cookie);
127
128 // write element data
129 using Iterator = typename GridView::template Codim<codim>::Iterator;
130
131 Iterator it = gridView.template begin<codim>();
132 const Iterator& endIt = gridView.template end<codim>();
133 for (; it != endIt; ++it) {
134 serializer.serializeEntity(outStream_, *it);
135 outStream_ << "\n";
136 }
137
139 }
140
145
150 template <class Simulator, class Scalar>
151 void deserializeBegin(Simulator& simulator, Scalar t)
152 {
153 fileName_ = restartFileName_(simulator.gridView().comm().rank(),
154 simulator.problem().outputDir(),
155 simulator.problem().name(), t);
156 openInputStream(magicRestartCookie_(simulator.gridView()));
157 }
158
163 std::istream& deserializeStream()
164 { return inStream_; }
165
169 void deserializeSectionBegin(const std::string& cookie);
170
175
181 template <int codim, class Deserializer, class GridView>
182 void deserializeEntities(Deserializer& deserializer, const GridView& gridView)
183 {
184 std::ostringstream oss;
185 oss << "Entities: Codim " << codim;
186 std::string cookie = oss.str();
188
189 std::string curLine;
190
191 // read entity data
192 using Iterator = typename GridView::template Codim<codim>::Iterator;
193 Iterator it = gridView.template begin<codim>();
194 const Iterator& endIt = gridView.template end<codim>();
195 for (; it != endIt; ++it) {
196 if (!inStream_.good()) {
197 throw std::runtime_error("Restart file is corrupted");
198 }
199
200 std::getline(inStream_, curLine);
201 std::istringstream curLineStream(curLine);
202 deserializer.deserializeEntity(curLineStream, *it);
203 }
204
206 }
207
212
213private:
218 void openInputStream(const std::string& cookie);
219
224 void openOutputStream(const std::string& cookie);
225
226 std::string fileName_;
227 std::ifstream inStream_;
228 std::ofstream outStream_;
229};
230
231} // namespace Opm
232
233#endif // OPM_RESTART_HPP
Load or save a state of a problem to/from the harddisk.
Definition: restart.hpp:41
std::istream & deserializeStream()
The input stream to read the data which ought to be deserialized.
Definition: restart.hpp:163
void serializeSectionEnd()
End of a section in the serialized output.
std::ostream & serializeStream()
The output stream to write the serialized data.
Definition: restart.hpp:102
void serializeBegin(Simulator &simulator)
Write the current state of the model to disk.
Definition: restart.hpp:88
void serializeEnd()
Finish the restart file.
const std::string & fileName() const
Returns the name of the file which is (de-)serialized.
Definition: restart.hpp:81
void serializeEntities(Serializer &serializer, const GridView &gridView)
Serialize all leaf entities of a codim in a gridView.
Definition: restart.hpp:121
void deserializeEntities(Deserializer &deserializer, const GridView &gridView)
Deserialize all leaf entities of a codim in a grid.
Definition: restart.hpp:182
void deserializeSectionEnd()
End of a section in the serialized output.
void deserializeBegin(Simulator &simulator, Scalar t)
Start reading a restart file at a certain simulated time.
Definition: restart.hpp:151
void serializeSectionBegin(const std::string &cookie)
Start a new section in the serialized output.
void deserializeSectionBegin(const std::string &cookie)
Start reading a new section of the restart file.
void deserializeEnd()
Stop reading the restart file.
Manages the initializing and running of time dependent problems.
Definition: simulator.hh:92
Problem & problem()
Return the object which specifies the pysical setup of the simulation.
Definition: simulator.hh:291
const GridView & gridView() const
Return the grid view for which the simulation is done.
Definition: simulator.hh:272
Scalar time() const
Return the number of seconds of simulated time which have elapsed since the start time.
Definition: simulator.hh:343
static const int dim
Definition: structuredgridvanguard.hh:67
Definition: blackoilboundaryratevector.hh:37