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 <dune/geometry/dimension.hh>
31#include <dune/grid/common/rangegenerators.hh>
32
33#include <fstream>
34#include <iostream>
35#include <sstream>
36#include <stdexcept>
37#include <string>
38
39namespace Opm {
40
45{
50 template <class GridView>
51 static std::string magicRestartCookie_(const GridView& gridView)
52 {
53 static const std::string gridName = "blubb"; // gridView.grid().name();
54 static const int dim = GridView::dimension;
55
56 const int numVertices = gridView.size(dim);
57 const int numElements = gridView.size(0);
58 const int numEdges = gridView.size(dim - 1);
59 const int numCPUs = gridView.comm().size();
60 const int rank = gridView.comm().rank();
61
62 std::ostringstream oss;
63 oss << "eWoms restart file: "
64 << "gridName='" << gridName << "' "
65 << "numCPUs=" << numCPUs << " "
66 << "myRank=" << rank << " "
67 << "numElements=" << numElements << " "
68 << "numEdges=" << numEdges << " "
69 << "numVertices=" << numVertices;
70 return oss.str();
71 }
72
76 static std::string restartFileName_(int rank,
77 const std::string& outputDir,
78 const std::string& simName,
79 double t);
80
81public:
85 const std::string& fileName() const
86 { return fileName_; }
87
91 template <class Simulator>
92 void serializeBegin(Simulator& simulator)
93 {
94 fileName_ = restartFileName_(simulator.gridView().comm().rank(),
95 simulator.problem().outputDir(),
96 simulator.problem().name(),
97 simulator.time());
98
99 // open output file and write magic cookie
100 openOutputStream(magicRestartCookie_(simulator.gridView()));
101 }
102
106 std::ostream& serializeStream()
107 { return outStream_; }
108
112 void serializeSectionBegin(const std::string& cookie);
113
118
124 template <int codim, class Serializer, class GridView>
125 void serializeEntities(Serializer& serializer, const GridView& gridView)
126 {
127 serializeSectionBegin("Entities: Codim " + std::to_string(codim));
128
129 for (const auto& entity : entities(gridView, Dune::Codim<codim>())) {
130 serializer.serializeEntity(outStream_, entity);
131 outStream_ << "\n";
132 }
133
135 }
136
141
146 template <class Simulator, class Scalar>
147 void deserializeBegin(Simulator& simulator, Scalar t)
148 {
149 fileName_ = restartFileName_(simulator.gridView().comm().rank(),
150 simulator.problem().outputDir(),
151 simulator.problem().name(), t);
152 openInputStream(magicRestartCookie_(simulator.gridView()));
153 }
154
159 std::istream& deserializeStream()
160 { return inStream_; }
161
165 void deserializeSectionBegin(const std::string& cookie);
166
171
177 template <int codim, class Deserializer, class GridView>
178 void deserializeEntities(Deserializer& deserializer, const GridView& gridView)
179 {
180 deserializeSectionBegin("Entities: Codim " + std::to_string(codim));
181
182 std::string curLine;
183
184 // read entity data
185 for (const auto& entity : entities(gridView, Dune::Codim<codim>())) {
186 if (!inStream_.good()) {
187 throw std::runtime_error("Restart file is corrupted");
188 }
189
190 std::getline(inStream_, curLine);
191 std::istringstream curLineStream(curLine);
192 deserializer.deserializeEntity(curLineStream, entity);
193 }
194
196 }
197
202
203private:
208 void openInputStream(const std::string& cookie);
209
214 void openOutputStream(const std::string& cookie);
215
216 std::string fileName_;
217 std::ifstream inStream_;
218 std::ofstream outStream_;
219};
220
221} // namespace Opm
222
223#endif // OPM_RESTART_HPP
Load or save a state of a problem to/from the harddisk.
Definition: restart.hpp:45
std::istream & deserializeStream()
The input stream to read the data which ought to be deserialized.
Definition: restart.hpp:159
void serializeSectionEnd()
End of a section in the serialized output.
std::ostream & serializeStream()
The output stream to write the serialized data.
Definition: restart.hpp:106
void serializeBegin(Simulator &simulator)
Write the current state of the model to disk.
Definition: restart.hpp:92
void serializeEnd()
Finish the restart file.
const std::string & fileName() const
Returns the name of the file which is (de-)serialized.
Definition: restart.hpp:85
void serializeEntities(Serializer &serializer, const GridView &gridView)
Serialize all leaf entities of a codim in a gridView.
Definition: restart.hpp:125
void deserializeEntities(Deserializer &deserializer, const GridView &gridView)
Deserialize all leaf entities of a codim in a grid.
Definition: restart.hpp:178
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:147
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:84
Problem & problem()
Return the object which specifies the pysical setup of the simulation.
Definition: simulator.hh:265
const GridView & gridView() const
Return the grid view for which the simulation is done.
Definition: simulator.hh:246
Scalar time() const
Return the number of seconds of simulated time which have elapsed since the start time.
Definition: simulator.hh:317
static constexpr int dim
Definition: structuredgridvanguard.hh:68
Definition: blackoilboundaryratevector.hh:39
std::string to_string(const ConvergenceReport::ReservoirFailure::Type t)