KASKADE 7 development version
checkPoint.hh
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2/* */
3/* This file is part of the library KASKADE 7 */
4/* https://www.zib.de/research/projects/kaskade7-finite-element-toolbox */
5/* */
6/* Copyright (C) 2014-2017 Zuse Institute Berlin */
7/* */
8/* KASKADE 7 is distributed under the terms of the ZIB Academic License. */
9/* see $KASKADE/academic.txt */
10/* */
11/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
12
13#ifndef CHECK_POINT_HH
14#define CHECK_POINT_HH
15
16#include <string>
17#include <sstream>
18#include <memory>
19#include <iomanip>
20#include <ctime>
21#include <chrono>
22#include <vector>
23#include <utility>
24
25#include <boost/filesystem.hpp>
26
27#include <dune/grid/common/backuprestore.hh>
28#include <dune/istl/bvector.hh>
29
30#include <fem/gridmanager.hh>
31#include <fem/variables.hh>
32
33namespace Kaskade {
35 {
41
48 CheckPoint(std::string const& pathName);
49
53 bool readMode() const {
54 return timeStamp_.empty();
55 }
56
61 std::string timeStamp() const {
62 return timeStamp_;
63 }
64
70 template <class Grid>
71 void writeGrid(GridManager<Grid> const& gridManager, std::string const& gridName) const {
72 writeGrid(gridManager.grid(), gridName);
73 }
74
80 template <class Grid>
81 void writeGrid(Grid const& grid, std::string const& gridName) const {
82 boost::filesystem::path path = directory/(gridName+".grid");
83 boost::filesystem::path tmpPath = path;
84 tmpPath += std::string("~");
85 // unfortunately backup with AluGrid does not tell us if action was succesfull or not (e.g. due to wrong path)
86 Dune::BackupRestoreFacility<Grid>::backup(grid, tmpPath.string());
87 boost::filesystem::rename(tmpPath, path);
88 }
89
97 template <class Grid>
98 std::unique_ptr<Grid> readGrid(std::string const& gridName) const {
99 // Todo: throw if restore returns nullptr?
100 return std::unique_ptr<Grid>(Dune::BackupRestoreFacility<Grid>::restore((directory/(gridName+".grid")).string()));
101 }
102
113 template <class Index>
114 void writeRefinementHistory(std::vector<std::vector<std::pair<int, Index>>> const& refHist, std::string const& gridName) const {
115 boost::filesystem::path path = directory/(gridName+".refHist");
116 boost::filesystem::path tmpPath = path;
117 tmpPath += "~";
118 std::ofstream out(tmpPath.string(), std::ios::binary);
119 for(auto const& refStep : refHist) {
120 out << refStep.size() << " ";
121 out.write(reinterpret_cast<char const*>(refStep.data()), sizeof(std::pair<int, Index>) * refStep.size());
122 }
123 boost::filesystem::rename(tmpPath, path);
124 }
125
133 template <class Index>
134 void appendRefinementHistory(std::vector<std::pair<int, Index>> const& refStep, std::string const& gridName) const {
135 boost::filesystem::path path = directory/(gridName+".refHist");
136 boost::filesystem::path tmpPath = path;
137 tmpPath += "~";
138 if(boost::filesystem::exists(path)) boost::filesystem::copy_file(path, tmpPath);
139 std::ofstream out(tmpPath.string(), std::ios::binary | std::ios::app);
140 out << refStep.size() << " ";
141 out.write(reinterpret_cast<char const*>(refStep.data()), sizeof(std::pair<int, Index>) * refStep.size());
142 boost::filesystem::rename(tmpPath, path);
143 }
144
156 template <class Index>
157 std::vector<std::vector<std::pair<int, Index>>> readRefinementHistory(std::string const& gridName) const {
158 using RefHist = std::vector<std::vector<std::pair<int, Index>>>;
159 using RefStep = typename RefHist::value_type;
160 RefHist refHist;
161 std::ifstream in((directory/(gridName+".refHist")).string(), std::ios::binary);
162 typename RefStep::size_type nRefCells;
163 while(in >> nRefCells) {
164 refHist.push_back(RefStep(nRefCells));
165 in.get(); // skip one white space
166 in.read(reinterpret_cast<char *>(refHist.back().data()), sizeof(typename RefStep::value_type) * nRefCells);
167 }
168 return refHist;
169 }
170
176 template <template<class, int> class Array, class Scalar, int dim>
177 void writeData(Dune::BlockVector<Array<Scalar, dim>> const& data, std::string const& dataName) const {
178 boost::filesystem::path path = directory/(dataName+".data");
179 boost::filesystem::path tmpPath = path;
180 tmpPath += std::string("~");
181 writeData(data, std::ofstream(tmpPath.string(), std::ios::binary));
182 boost::filesystem::rename(tmpPath, path);
183 }
184
190 template <class DataBlocks>
191 void writeData(DataBlocks const& data, std::string const& dataName) const {
192 boost::filesystem::path path = directory/(dataName+".data");
193 boost::filesystem::path tmpPath = path;
194 tmpPath += std::string("~");
195 std::ofstream out(tmpPath.string(), std::ios::binary);
196 boost::fusion::for_each(data.data, [this, &out](auto const& dataBlock) {
197 out = this->writeData(dataBlock, std::move(out));
198 });
199 boost::filesystem::rename(tmpPath, path);
200 }
201
205 template <class FeSpace, int m>
206 void writeData(FunctionSpaceElement<FeSpace, m> const& data, std::string const& dataName) const {
207 writeData(data.coefficients(), dataName);
208 }
209
215 template <template<class, int> class Array, class Scalar, int dim>
216 void readData(Dune::BlockVector<Array<Scalar, dim>> & data, std::string const& dataName) const {
217 readData(data, std::ifstream((directory/(dataName+".data")).string(), std::ios::binary));
218 }
219
225 template <class DataBlocks>
226 void readData(DataBlocks & data, std::string const& dataName) const {
227 std::ifstream in((directory/(dataName+".data")).string(), std::ios::binary);
228 boost::fusion::for_each(data.data, [this, &in](auto & dataBlock) {
229 in = this->readData(dataBlock, std::move(in));
230 });
231 }
232
236 template <class FeSpace, int m>
237 void readData(FunctionSpaceElement<FeSpace, m> & data, std::string const& dataName) const {
238 readData(data.coefficients(), dataName);
239 }
240
241 protected:
243
244 template <template<class, int> class Array, class Scalar, int dim>
245 std::ofstream writeData(Dune::BlockVector<Array<Scalar, dim>> const& data, std::ofstream out) const {
246 out.write(reinterpret_cast<char const*>(&data[0]), sizeof(Scalar) * data.size() * dim);
247 return out;
248 }
249
250 template <class FeSpace, int m>
251 std::ofstream writeData(FunctionSpaceElement<FeSpace, m> const& data, std::ofstream out) const {
252 return writeData(data.coefficients(), std::move(out));
253 }
254
255 template <template<class, int> class Array, class Scalar, int dim>
256 std::ifstream readData(Dune::BlockVector<Array<Scalar, dim>> & data, std::ifstream in) const {
257 in.read(reinterpret_cast<char *>(&data[0]), sizeof(Scalar) * data.size() * dim);
258 return in;
259 }
260
261 template <class FeSpace, int m>
262 std::ifstream readData(FunctionSpaceElement<FeSpace, m> & data, std::ifstream in) const {
263 return readData(data.coefficients(), std::move(in));
264 }
265
266 std::string timeStamp_;
267 boost::filesystem::path directory;
268 };
269}
270#endif // CHECK_POINT_HH
A class for representing finite element functions.
StorageType const & coefficients() const
Provides read-only access to the coefficients of the finite element basis functions.
Grid const & grid() const
Returns a const reference on the owned grid.
Definition: gridmanager.hh:292
bool readMode() const
readMode returns true if this check point was created in read mode (i.e. with existing directory give...
Definition: checkPoint.hh:53
void appendRefinementHistory(std::vector< std::pair< int, Index > > const &refStep, std::string const &gridName) const
appends data of one refinement step of a grid to file.
Definition: checkPoint.hh:134
void writeGrid(GridManager< Grid > const &gridManager, std::string const &gridName) const
writes grid into file with name gridName.
Definition: checkPoint.hh:71
void writeData(Dune::BlockVector< Array< Scalar, dim > > const &data, std::string const &dataName) const
writes data into file with name dataName. Version for BlockVector.
Definition: checkPoint.hh:177
CheckPoint()
CheckPoint creates a new empty directory with current time stamp as name. Later writes will refer to ...
void writeRefinementHistory(std::vector< std::vector< std::pair< int, Index > > > const &refHist, std::string const &gridName) const
writes the refinement history of a grid.
Definition: checkPoint.hh:114
std::ofstream writeData(Dune::BlockVector< Array< Scalar, dim > > const &data, std::ofstream out) const
Definition: checkPoint.hh:245
CheckPoint(std::string const &pathName)
CheckPoint. If the directory with pathName already exists later reads will refer to this directory....
void readData(DataBlocks &data, std::string const &dataName) const
reads data from file with name dataName. Version for LinearProductSpace (of FE-functions or coefficie...
Definition: checkPoint.hh:226
void readData(FunctionSpaceElement< FeSpace, m > &data, std::string const &dataName) const
reads data from file with name dataName. Version for FE-function.
Definition: checkPoint.hh:237
std::ifstream readData(Dune::BlockVector< Array< Scalar, dim > > &data, std::ifstream in) const
Definition: checkPoint.hh:256
void writeGrid(Grid const &grid, std::string const &gridName) const
writes grid into file with name gridName.
Definition: checkPoint.hh:81
void readData(Dune::BlockVector< Array< Scalar, dim > > &data, std::string const &dataName) const
reads data from file with name dataName. Version for BlockVector.
Definition: checkPoint.hh:216
std::vector< std::vector< std::pair< int, Index > > > readRefinementHistory(std::string const &gridName) const
reads the refinement history of a grid.
Definition: checkPoint.hh:157
std::string timeStamp() const
timeStamp gives the time stamp when directory was created (at construction of this CheckPoint instanc...
Definition: checkPoint.hh:61
boost::filesystem::path directory
Definition: checkPoint.hh:267
void writeData(DataBlocks const &data, std::string const &dataName) const
writes data into file with name dataName. Version for LinearProductSpace (of FE-functions or coeffici...
Definition: checkPoint.hh:191
void writeData(FunctionSpaceElement< FeSpace, m > const &data, std::string const &dataName) const
writes data into file with name dataName. Version for FE-function.
Definition: checkPoint.hh:206
std::ofstream writeData(FunctionSpaceElement< FeSpace, m > const &data, std::ofstream out) const
Definition: checkPoint.hh:251
std::ifstream readData(FunctionSpaceElement< FeSpace, m > &data, std::ifstream in) const
Definition: checkPoint.hh:262
std::unique_ptr< Grid > readGrid(std::string const &gridName) const
reads grid from file with name gridName.
Definition: checkPoint.hh:98
std::string timeStamp_
Definition: checkPoint.hh:266
Variables and their descriptions.