Loading...
Searching...
No Matches
restart.hpp
Go to the documentation of this file.
// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
// vi: set et ts=4 sw=4 sts=4:
/*
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
OPM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
Consult the COPYING file in the top-level source directory of this
module for the precise wording of the license and the list of
copyright holders.
*/
#ifndef OPM_RESTART_HPP
#define OPM_RESTART_HPP
#include <dune/geometry/dimension.hh>
#include <dune/grid/common/rangegenerators.hh>
#include <fstream>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <string>
namespace Opm {
class Restart
{
template <class GridView>
static std::string magicRestartCookie_(const GridView& gridView)
{
static const std::string gridName = "blubb"; // gridView.grid().name();
static const int dim = GridView::dimension;
const int numVertices = gridView.size(dim);
const int numElements = gridView.size(0);
const int numEdges = gridView.size(dim - 1);
const int numCPUs = gridView.comm().size();
const int rank = gridView.comm().rank();
std::ostringstream oss;
oss << "eWoms restart file: "
<< "gridName='" << gridName << "' "
<< "numCPUs=" << numCPUs << " "
<< "myRank=" << rank << " "
<< "numElements=" << numElements << " "
<< "numEdges=" << numEdges << " "
<< "numVertices=" << numVertices;
return oss.str();
}
static std::string restartFileName_(int rank,
const std::string& outputDir,
const std::string& simName,
double t);
public:
const std::string& fileName() const
{ return fileName_; }
template <class Simulator>
void serializeBegin(Simulator& simulator)
{
fileName_ = restartFileName_(simulator.gridView().comm().rank(),
simulator.problem().outputDir(),
simulator.problem().name(),
simulator.time());
// open output file and write magic cookie
openOutputStream(magicRestartCookie_(simulator.gridView()));
}
std::ostream& serializeStream()
{ return outStream_; }
void serializeSectionBegin(const std::string& cookie);
void serializeSectionEnd();
template <int codim, class Serializer, class GridView>
void serializeEntities(Serializer& serializer, const GridView& gridView)
{
serializeSectionBegin("Entities: Codim " + std::to_string(codim));
for (const auto& entity : entities(gridView, Dune::Codim<codim>())) {
serializer.serializeEntity(outStream_, entity);
outStream_ << "\n";
}
serializeSectionEnd();
}
void serializeEnd();
template <class Simulator, class Scalar>
void deserializeBegin(Simulator& simulator, Scalar t)
{
fileName_ = restartFileName_(simulator.gridView().comm().rank(),
simulator.problem().outputDir(),
simulator.problem().name(), t);
openInputStream(magicRestartCookie_(simulator.gridView()));
}
std::istream& deserializeStream()
{ return inStream_; }
void deserializeSectionBegin(const std::string& cookie);
void deserializeSectionEnd();
template <int codim, class Deserializer, class GridView>
void deserializeEntities(Deserializer& deserializer, const GridView& gridView)
{
deserializeSectionBegin("Entities: Codim " + std::to_string(codim));
std::string curLine;
// read entity data
for (const auto& entity : entities(gridView, Dune::Codim<codim>())) {
if (!inStream_.good()) {
throw std::runtime_error("Restart file is corrupted");
}
std::getline(inStream_, curLine);
std::istringstream curLineStream(curLine);
deserializer.deserializeEntity(curLineStream, entity);
}
deserializeSectionEnd();
}
void deserializeEnd();
private:
void openInputStream(const std::string& cookie);
void openOutputStream(const std::string& cookie);
std::string fileName_;
std::ifstream inStream_;
std::ofstream outStream_;
};
} // namespace Opm
#endif // OPM_RESTART_HPP
Opm::Restart
Load or save a state of a problem to/from the harddisk.
Definition restart.hpp:45
Opm::Restart::deserializeStream
std::istream & deserializeStream()
The input stream to read the data which ought to be deserialized.
Definition restart.hpp:159
Opm::Restart::serializeSectionEnd
void serializeSectionEnd()
End of a section in the serialized output.
Definition restart.cpp:52
Opm::Restart::serializeStream
std::ostream & serializeStream()
The output stream to write the serialized data.
Definition restart.hpp:106
Opm::Restart::serializeBegin
void serializeBegin(Simulator &simulator)
Write the current state of the model to disk.
Definition restart.hpp:92
Opm::Restart::serializeEnd
void serializeEnd()
Finish the restart file.
Definition restart.cpp:85
Opm::Restart::fileName
const std::string & fileName() const
Returns the name of the file which is (de-)serialized.
Definition restart.hpp:85
Opm::Restart::serializeEntities
void serializeEntities(Serializer &serializer, const GridView &gridView)
Serialize all leaf entities of a codim in a gridView.
Definition restart.hpp:125
Opm::Restart::deserializeEntities
void deserializeEntities(Deserializer &deserializer, const GridView &gridView)
Deserialize all leaf entities of a codim in a grid.
Definition restart.hpp:178
Opm::Restart::deserializeSectionEnd
void deserializeSectionEnd()
End of a section in the serialized output.
Definition restart.cpp:69
Opm::Restart::deserializeBegin
void deserializeBegin(Simulator &simulator, Scalar t)
Start reading a restart file at a certain simulated time.
Definition restart.hpp:147
Opm::Restart::serializeSectionBegin
void serializeSectionBegin(const std::string &cookie)
Start a new section in the serialized output.
Definition restart.cpp:47
Opm::Restart::deserializeSectionBegin
void deserializeSectionBegin(const std::string &cookie)
Start reading a new section of the restart file.
Definition restart.cpp:57
Opm::Restart::deserializeEnd
void deserializeEnd()
Stop reading the restart file.
Definition restart.cpp:80
Opm::Simulator
Manages the initializing and running of time dependent problems.
Definition simulator.hh:84
Opm::Simulator::problem
Problem & problem()
Return the object which specifies the pysical setup of the simulation.
Definition simulator.hh:265
Opm::Simulator::gridView
const GridView & gridView() const
Return the grid view for which the simulation is done.
Definition simulator.hh:246
Opm::Simulator::time
Scalar time() const
Return the number of seconds of simulated time which have elapsed since the start time.
Definition simulator.hh:317
Opm
This file contains a set of helper functions used by VFPProd / VFPInj.
Definition blackoilbioeffectsmodules.hh:43