mirror of
https://github.com/OPM/opm-simulators.git
synced 2025-02-25 18:55:30 -06:00
restart: add translation unit
This commit is contained in:
parent
e22276b91a
commit
93d796ef22
@ -61,6 +61,7 @@ list (APPEND MAIN_SOURCE_FILES
|
|||||||
opm/models/blackoil/blackoilmicpparams.cpp
|
opm/models/blackoil/blackoilmicpparams.cpp
|
||||||
opm/models/blackoil/blackoilpolymerparams.cpp
|
opm/models/blackoil/blackoilpolymerparams.cpp
|
||||||
opm/models/blackoil/blackoilsolventparams.cpp
|
opm/models/blackoil/blackoilsolventparams.cpp
|
||||||
|
opm/models/io/restart.cpp
|
||||||
opm/models/parallel/mpiutil.cpp
|
opm/models/parallel/mpiutil.cpp
|
||||||
opm/models/parallel/tasklets.cpp
|
opm/models/parallel/tasklets.cpp
|
||||||
opm/models/parallel/threadmanager.cpp
|
opm/models/parallel/threadmanager.cpp
|
||||||
|
120
opm/models/io/restart.cpp
Normal file
120
opm/models/io/restart.cpp
Normal file
@ -0,0 +1,120 @@
|
|||||||
|
// -*- 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <config.h>
|
||||||
|
#include <opm/models/io/restart.hpp>
|
||||||
|
|
||||||
|
namespace Opm {
|
||||||
|
|
||||||
|
std::string Restart::restartFileName_(int rank,
|
||||||
|
const std::string& outputDir,
|
||||||
|
const std::string& simName,
|
||||||
|
double t)
|
||||||
|
{
|
||||||
|
std::string dir = outputDir;
|
||||||
|
if (dir == ".") {
|
||||||
|
dir = "";
|
||||||
|
}
|
||||||
|
else if (!dir.empty() && dir.back() != '/') {
|
||||||
|
dir += "/";
|
||||||
|
}
|
||||||
|
|
||||||
|
std::ostringstream oss;
|
||||||
|
oss << dir << simName << "_time=" << t << "_rank=" << rank << ".ers";
|
||||||
|
return oss.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Restart::serializeSectionBegin(const std::string& cookie)
|
||||||
|
{
|
||||||
|
outStream_ << cookie << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
void Restart::serializeSectionEnd()
|
||||||
|
{
|
||||||
|
outStream_ << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
void Restart::deserializeSectionBegin(const std::string& cookie)
|
||||||
|
{
|
||||||
|
if (!inStream_.good()) {
|
||||||
|
throw std::runtime_error("Encountered unexpected EOF in restart file.");
|
||||||
|
}
|
||||||
|
std::string buf;
|
||||||
|
std::getline(inStream_, buf);
|
||||||
|
if (buf != cookie) {
|
||||||
|
throw std::runtime_error("Could not start section '"+cookie+"'");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Restart::deserializeSectionEnd()
|
||||||
|
{
|
||||||
|
std::string dummy;
|
||||||
|
std::getline(inStream_, dummy);
|
||||||
|
for (unsigned i = 0; i < dummy.length(); ++i) {
|
||||||
|
if (!std::isspace(dummy[i])) {
|
||||||
|
throw std::logic_error("Encountered unread values while deserializing");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Restart::deserializeEnd()
|
||||||
|
{
|
||||||
|
inStream_.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Restart::serializeEnd()
|
||||||
|
{
|
||||||
|
outStream_.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Restart::openInputStream(const std::string& cookie)
|
||||||
|
{
|
||||||
|
// open input file and read magic cookie
|
||||||
|
inStream_.open(fileName_.c_str());
|
||||||
|
if (!inStream_.good()) {
|
||||||
|
throw std::runtime_error("Restart file '" + fileName_ +
|
||||||
|
"' could not be opened properly");
|
||||||
|
}
|
||||||
|
|
||||||
|
// make sure that we don't open an empty file
|
||||||
|
inStream_.seekg(0, std::ios::end);
|
||||||
|
auto pos = inStream_.tellg();
|
||||||
|
if (pos == 0) {
|
||||||
|
throw std::runtime_error("Restart file '" + fileName_ + "' is empty");
|
||||||
|
}
|
||||||
|
inStream_.seekg(0, std::ios::beg);
|
||||||
|
|
||||||
|
deserializeSectionBegin(cookie);
|
||||||
|
deserializeSectionEnd();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Restart::openOutputStream(const std::string& cookie)
|
||||||
|
{
|
||||||
|
outStream_.open(fileName_.c_str());
|
||||||
|
outStream_.precision(20);
|
||||||
|
|
||||||
|
serializeSectionBegin(cookie);
|
||||||
|
serializeSectionEnd();
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Opm
|
@ -44,7 +44,7 @@ class Restart
|
|||||||
* unlikely to load a restart file for an incorrectly.
|
* unlikely to load a restart file for an incorrectly.
|
||||||
*/
|
*/
|
||||||
template <class GridView>
|
template <class GridView>
|
||||||
static const std::string magicRestartCookie_(const GridView& gridView)
|
static std::string magicRestartCookie_(const GridView& gridView)
|
||||||
{
|
{
|
||||||
static const std::string gridName = "blubb"; // gridView.grid().name();
|
static const std::string gridName = "blubb"; // gridView.grid().name();
|
||||||
static const int dim = GridView::dimension;
|
static const int dim = GridView::dimension;
|
||||||
@ -69,23 +69,10 @@ class Restart
|
|||||||
/*!
|
/*!
|
||||||
* \brief Return the restart file name.
|
* \brief Return the restart file name.
|
||||||
*/
|
*/
|
||||||
template <class GridView, class Scalar>
|
static std::string restartFileName_(int rank,
|
||||||
static const std::string restartFileName_(const GridView& gridView,
|
const std::string& outputDir,
|
||||||
const std::string& outputDir,
|
const std::string& simName,
|
||||||
const std::string& simName,
|
double t);
|
||||||
Scalar t)
|
|
||||||
{
|
|
||||||
std::string dir = outputDir;
|
|
||||||
if (dir == ".")
|
|
||||||
dir = "";
|
|
||||||
else if (!dir.empty() && dir.back() != '/')
|
|
||||||
dir += "/";
|
|
||||||
|
|
||||||
int rank = gridView.comm().rank();
|
|
||||||
std::ostringstream oss;
|
|
||||||
oss << dir << simName << "_time=" << t << "_rank=" << rank << ".ers";
|
|
||||||
return oss.str();
|
|
||||||
}
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/*!
|
/*!
|
||||||
@ -100,18 +87,13 @@ public:
|
|||||||
template <class Simulator>
|
template <class Simulator>
|
||||||
void serializeBegin(Simulator& simulator)
|
void serializeBegin(Simulator& simulator)
|
||||||
{
|
{
|
||||||
const std::string magicCookie = magicRestartCookie_(simulator.gridView());
|
fileName_ = restartFileName_(simulator.gridView().comm().rank(),
|
||||||
fileName_ = restartFileName_(simulator.gridView(),
|
|
||||||
simulator.problem().outputDir(),
|
simulator.problem().outputDir(),
|
||||||
simulator.problem().name(),
|
simulator.problem().name(),
|
||||||
simulator.time());
|
simulator.time());
|
||||||
|
|
||||||
// open output file and write magic cookie
|
// open output file and write magic cookie
|
||||||
outStream_.open(fileName_.c_str());
|
openOutputStream(magicRestartCookie_(simulator.gridView()));
|
||||||
outStream_.precision(20);
|
|
||||||
|
|
||||||
serializeSectionBegin(magicCookie);
|
|
||||||
serializeSectionEnd();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@ -123,14 +105,12 @@ public:
|
|||||||
/*!
|
/*!
|
||||||
* \brief Start a new section in the serialized output.
|
* \brief Start a new section in the serialized output.
|
||||||
*/
|
*/
|
||||||
void serializeSectionBegin(const std::string& cookie)
|
void serializeSectionBegin(const std::string& cookie);
|
||||||
{ outStream_ << cookie << "\n"; }
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* \brief End of a section in the serialized output.
|
* \brief End of a section in the serialized output.
|
||||||
*/
|
*/
|
||||||
void serializeSectionEnd()
|
void serializeSectionEnd();
|
||||||
{ outStream_ << "\n"; }
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* \brief Serialize all leaf entities of a codim in a gridView.
|
* \brief Serialize all leaf entities of a codim in a gridView.
|
||||||
@ -161,8 +141,7 @@ public:
|
|||||||
/*!
|
/*!
|
||||||
* \brief Finish the restart file.
|
* \brief Finish the restart file.
|
||||||
*/
|
*/
|
||||||
void serializeEnd()
|
void serializeEnd();
|
||||||
{ outStream_.close(); }
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* \brief Start reading a restart file at a certain simulated
|
* \brief Start reading a restart file at a certain simulated
|
||||||
@ -171,26 +150,10 @@ public:
|
|||||||
template <class Simulator, class Scalar>
|
template <class Simulator, class Scalar>
|
||||||
void deserializeBegin(Simulator& simulator, Scalar t)
|
void deserializeBegin(Simulator& simulator, Scalar t)
|
||||||
{
|
{
|
||||||
fileName_ = restartFileName_(simulator.gridView(), simulator.problem().outputDir(), simulator.problem().name(), t);
|
fileName_ = restartFileName_(simulator.gridView().comm().rank(),
|
||||||
|
simulator.problem().outputDir(),
|
||||||
// open input file and read magic cookie
|
simulator.problem().name(), t);
|
||||||
inStream_.open(fileName_.c_str());
|
openInputStream(magicRestartCookie_(simulator.gridView()));
|
||||||
if (!inStream_.good()) {
|
|
||||||
throw std::runtime_error("Restart file '"+fileName_+"' could not be opened properly");
|
|
||||||
}
|
|
||||||
|
|
||||||
// make sure that we don't open an empty file
|
|
||||||
inStream_.seekg(0, std::ios::end);
|
|
||||||
auto pos = inStream_.tellg();
|
|
||||||
if (pos == 0) {
|
|
||||||
throw std::runtime_error("Restart file '"+fileName_+"' is empty");
|
|
||||||
}
|
|
||||||
inStream_.seekg(0, std::ios::beg);
|
|
||||||
|
|
||||||
const std::string magicCookie = magicRestartCookie_(simulator.gridView());
|
|
||||||
|
|
||||||
deserializeSectionBegin(magicCookie);
|
|
||||||
deserializeSectionEnd();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@ -203,29 +166,12 @@ public:
|
|||||||
/*!
|
/*!
|
||||||
* \brief Start reading a new section of the restart file.
|
* \brief Start reading a new section of the restart file.
|
||||||
*/
|
*/
|
||||||
void deserializeSectionBegin(const std::string& cookie)
|
void deserializeSectionBegin(const std::string& cookie);
|
||||||
{
|
|
||||||
if (!inStream_.good())
|
|
||||||
throw std::runtime_error("Encountered unexpected EOF in restart file.");
|
|
||||||
std::string buf;
|
|
||||||
std::getline(inStream_, buf);
|
|
||||||
if (buf != cookie)
|
|
||||||
throw std::runtime_error("Could not start section '"+cookie+"'");
|
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* \brief End of a section in the serialized output.
|
* \brief End of a section in the serialized output.
|
||||||
*/
|
*/
|
||||||
void deserializeSectionEnd()
|
void deserializeSectionEnd();
|
||||||
{
|
|
||||||
std::string dummy;
|
|
||||||
std::getline(inStream_, dummy);
|
|
||||||
for (unsigned i = 0; i < dummy.length(); ++i) {
|
|
||||||
if (!std::isspace(dummy[i])) {
|
|
||||||
throw std::logic_error("Encountered unread values while deserializing");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* \brief Deserialize all leaf entities of a codim in a grid.
|
* \brief Deserialize all leaf entities of a codim in a grid.
|
||||||
@ -262,10 +208,21 @@ public:
|
|||||||
/*!
|
/*!
|
||||||
* \brief Stop reading the restart file.
|
* \brief Stop reading the restart file.
|
||||||
*/
|
*/
|
||||||
void deserializeEnd()
|
void deserializeEnd();
|
||||||
{ inStream_.close(); }
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
/*!
|
||||||
|
* \brief Open input stream and check for magic cookie.
|
||||||
|
* \param cookie Magic cookie to check for
|
||||||
|
*/
|
||||||
|
void openInputStream(const std::string& cookie);
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Open output stream and write magic cookie.
|
||||||
|
* \param cookie Magic cookie to write
|
||||||
|
*/
|
||||||
|
void openOutputStream(const std::string& cookie);
|
||||||
|
|
||||||
std::string fileName_;
|
std::string fileName_;
|
||||||
std::ifstream inStream_;
|
std::ifstream inStream_;
|
||||||
std::ofstream outStream_;
|
std::ofstream outStream_;
|
||||||
|
Loading…
Reference in New Issue
Block a user