dune-grid  2.11
yaspgrid/backuprestore.hh
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root
2 // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
3 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
4 // vi: set et ts=4 sw=2 sts=2:
5 #ifndef DUNE_GRID_YASPGRID_BACKUPRESTORE_HH
6 #define DUNE_GRID_YASPGRID_BACKUPRESTORE_HH
7 
8 //- system headers
9 #include <fstream>
10 #include <iterator>
11 
12 //- Dune headers
13 #include <dune/common/exceptions.hh>
14 #include <dune/common/fvector.hh>
16 #include <dune/grid/yaspgrid.hh>
17 
18 // bump this version number up if you introduce any changes
19 // to the output format of the YaspGrid BackupRestoreFacility.
20 #define YASPGRID_BACKUPRESTORE_FORMAT_VERSION 2
21 
22 namespace Dune
23 {
24 
25  template<class Coordinates>
27  {
28 
29  template<class S>
30  static void writeOrigin(S& /* s */, const Coordinates& /* coord */)
31  {}
32 
33  template<class S>
34  static void readOrigin(S& /* s */, Dune::FieldVector<typename Coordinates::ctype,Coordinates::dimension>& /* coord */)
35  {}
36 
37  template<typename... A>
39  const Dune::FieldVector<typename Coordinates::ctype,Coordinates::dimension>& /* lowerleft */, A... args)
40  {
42  }
43  };
44 
45  template<class ctype, int dim>
47  {
49 
50  template<class S>
51  static void writeOrigin(S& s, const Coordinates& coord)
52  {
53  s << "Origin: ";
54  for (int i=0; i<dim; i++)
55  s << coord.origin(i) << " ";
56  s << std::endl;
57  }
58 
59  template<class S>
60  static void readOrigin(S& s, Dune::FieldVector<ctype, dim>& coord)
61  {
62  std::string token;
63  s >> token;
64  for (int i=0; i<dim; i++)
65  s >> coord[i];
66  }
67 
68  template<typename... A>
70  const Dune::FieldVector<typename Coordinates::ctype,Coordinates::dimension>& lowerleft,
71  const Dune::FieldVector<typename Coordinates::ctype,Coordinates::dimension>& extension, A... args)
72  {
73  Dune::FieldVector<typename Coordinates::ctype,Coordinates::dimension> upperright(lowerleft);
74  upperright += extension;
75  return new Dune::YaspGrid<Coordinates::dimension,Coordinates>(lowerleft, upperright, args...);
76  }
77  };
78 
80  template<int dim, class Coordinates>
81  struct BackupRestoreFacility<Dune::YaspGrid<dim, Coordinates> >
82  {
83  // type of grid
85  typedef typename Grid::ctype ctype;
87 
89  static void backup ( const Grid &grid, const std::string &filename )
90  {
91  if (grid.comm().rank() == 0)
92  {
93  std::ofstream file(filename);
94  if( file )
95  {
96  backup(grid,file);
97  file.close();
98  }
99  else
100  std::cerr << "ERROR: BackupRestoreFacility::backup: couldn't open file `" << filename << "'" << std::endl;
101  }
102  }
103 
105  static void backup ( const Grid &grid, std::ostream &stream )
106  {
107  stream << "YaspGrid BackupRestore Format Version: " << YASPGRID_BACKUPRESTORE_FORMAT_VERSION << std::endl;
108  stream << "Torus structure: ";
109  for (int i=0; i<dim; i++)
110  stream << grid.torus().dims(i) << " ";
111  stream << std::endl << "Refinement level: " << grid.maxLevel() << std::endl;
112  stream << "Periodicity: ";
113  for (int i=0; i<dim; i++)
114  stream << (grid.isPeriodic(i) ? "1 " : "0 ");
115  stream << std::endl << "Overlap: " << grid.overlapSize(0,0) << std::endl;
116  stream << "KeepPhysicalOverlap: ";
117  for (typename Grid::YGridLevelIterator i=std::next(grid.begin()); i != grid.end(); ++i)
118  stream << (i->keepOverlap ? "1" : "0") << " ";
119  stream << std::endl;
120  stream << "Coarse Size: ";
121  for (int i=0; i<dim; i++)
122  stream << grid.levelSize(0,i) << " ";
123  stream << std::endl;
124  stream << "Meshsize: " ;
125  for (int i=0; i<dim; i++)
126  stream << grid.begin()->coords.meshsize(i,0) << " ";
127  stream << std::endl;
128  MaybeHaveOrigin<Coordinates>::writeOrigin(stream, grid.begin()->coords);
129  }
130 
134  static Grid *restore (const std::string &filename, Comm comm = Comm())
135  {
136  std::ifstream file(filename);
137  if( file )
138  return restore(file,comm);
139  else
140  {
141  std::cerr << "ERROR: BackupRestoreFacility::restore: couldn't open file `" << filename << "'" << std::endl;
142  return 0;
143  }
144  }
145 
149  static Grid *restore (std::istream &stream, Comm comm = Comm())
150  {
151  std::string input;
152 
153  int version;
154  stream >> input >> input >> input >> input;
155  stream >> version;
157  DUNE_THROW(Dune::Exception, "Your YaspGrid backup file is written in an outdated format!");
158 
159  std::array<int,dim> torus_dims;
160  stream >> input >> input;
161  for (int i=0; i<dim; i++)
162  stream >> torus_dims[i];
163 
164  int refinement;
165  stream >> input >> input;
166  stream >> refinement;
167 
168  std::bitset<dim> periodic;
169  bool b;
170  stream >> input;
171  for (int i=0; i<dim; i++)
172  {
173  stream >> b;
174  periodic[i] = b;
175  }
176 
177  int overlap;
178  stream >> input;
179  stream >> overlap;
180 
181  std::vector<bool> physicalOverlapSize;
182  physicalOverlapSize.resize(refinement);
183  stream >> input;
184  for (int i=0; i<refinement; ++i)
185  {
186  stream >> b;
187  physicalOverlapSize[i] = b;
188  }
189 
190  std::array<int,dim> coarseSize;
191  stream >> input >> input;
192  for (int i=0; i<dim; i++)
193  stream >> coarseSize[i];
194 
195  Dune::FieldVector<ctype,dim> h;
196  stream >> input;
197  for (int i=0; i<dim; i++)
198  stream >> h[i];
199 
200  Dune::FieldVector<ctype,dim> origin;
202 
203  // the constructor takes the upper right corner...
204  Dune::FieldVector<ctype,dim> length(h);
205  for (int i=0; i<dim; i++)
206  length[i] *= coarseSize[i];
207 
208  Yasp::FixedSizePartitioning<dim> lb(torus_dims);
209 
210  Grid* grid = MaybeHaveOrigin<Coordinates>::createGrid(origin, length, coarseSize, periodic, overlap, comm, &lb);
211 
212  for (int i=0; i<refinement; ++i)
213  {
214  grid->refineOptions(physicalOverlapSize[i]);
215  grid->globalRefine(1);
216  }
217 
218  return grid;
219  }
220  };
221 
223  template<int dim, class ctype>
225  {
226  // type of grid
229 
231  static void backup ( const Grid &grid, const std::string &filename )
232  {
233  std::ostringstream filename_str;
234  filename_str << filename << grid.comm().rank();
235  std::ofstream file( filename_str.str() );
236  if( file )
237  {
238  backup(grid,file);
239  file.close();
240  }
241  else
242  std::cerr << "ERROR: BackupRestoreFacility::backup: couldn't open file `" << filename_str.str() << "'" << std::endl;
243  }
244 
246  static void backup ( const Grid &grid, std::ostream &stream )
247  {
248  stream << "YaspGrid BackupRestore Format Version: " << YASPGRID_BACKUPRESTORE_FORMAT_VERSION << std::endl;
249  stream << "Torus structure: ";
250  for (int i=0; i<dim; i++)
251  stream << grid.torus().dims(i) << " ";
252  stream << std::endl << "Refinement level: " << grid.maxLevel() << std::endl;
253  stream << "Periodicity: ";
254  for (int i=0; i<dim; i++)
255  stream << (grid.isPeriodic(i) ? "1 " : "0 ");
256  stream << std::endl << "Overlap: " << grid.overlapSize(0,0) << std::endl;
257  stream << "KeepPhysicalOverlap: ";
258  for (typename Grid::YGridLevelIterator i=std::next(grid.begin()); i != grid.end(); ++i)
259  stream << (i->keepOverlap ? "1" : "0") << " ";
260  stream << std::endl;
261  stream << "Coarse Size: ";
262  for (int i=0; i<dim; i++)
263  stream << grid.levelSize(0,i) << " ";
264  stream << std::endl;
265 
266  grid.begin()->coords.print(stream);
267  }
268 
273  static Grid *restore (const std::string &filename, Comm comm = Comm())
274  {
275  std::ostringstream filename_str;
276  filename_str << filename;
277  filename_str << comm.rank();
278  std::ifstream file(filename_str.str());
279  if( file )
280  return restore(file, comm);
281  else
282  {
283  std::cerr << "ERROR: BackupRestoreFacility::restore: couldn't open file `" << filename_str.str() << "'" << std::endl;
284  return 0;
285  }
286  }
287 
292  static Grid *restore (std::istream &stream, Comm comm = Comm())
293  {
294  std::string input;
295 
296  int version;
297  stream >> input >> input >> input >> input;
298  stream >> version;
300  DUNE_THROW(Dune::Exception, "Your YaspGrid backup file is written in an outdated format!");
301 
302  std::array<int,dim> torus_dims;
303  stream >> input >> input;
304  for (int i=0; i<dim; i++)
305  stream >> torus_dims[i];
306 
307  int refinement;
308  stream >> input >> input;
309  stream >> refinement;
310 
311  std::bitset<dim> periodic;
312  bool b;
313  stream >> input;
314  for (int i=0; i<dim; i++)
315  {
316  stream >> b;
317  periodic[i] = b;
318  }
319 
320  int overlap;
321  stream >> input;
322  stream >> overlap;
323 
324  std::vector<bool> physicalOverlapSize;
325  physicalOverlapSize.resize(refinement);
326  stream >> input;
327  for (int i=0; i<refinement; ++i)
328  {
329  stream >> b;
330  physicalOverlapSize[i] = b;
331  }
332 
333 
334  std::array<int,dim> coarseSize;
335  stream >> input >> input;
336  for (int i=0; i<dim; i++)
337  stream >> coarseSize[i];
338 
339  std::array<std::vector<ctype>,dim> coords;
340  stream >> input >> input >> input >> input;
341  for (int d=0; d<dim; d++)
342  {
343  stream >> input >> input;
344  int size;
345  stream >> size;
346  stream >> input;
347  ctype tmp;
348  coords[d].resize(size);
349  for (int i=0; i<size; i++)
350  {
351  stream >> tmp;
352  coords[d][i] = tmp;
353  }
354  }
355 
356  Yasp::FixedSizePartitioning<dim> lb(torus_dims);
357  Grid* grid = new Grid(coords, periodic, overlap, comm, coarseSize, &lb);
358 
359  for (int i=0; i<refinement; ++i)
360  {
361  grid->refineOptions(physicalOverlapSize[i]);
362  grid->globalRefine(1);
363  }
364 
365  return grid;
366  }
367  };
368 } // namespace Dune
369 
370 #endif // #ifndef DUNE_GRID_YASPGRID_BACKUPRESTORE_HH
static void backup(const Grid &grid, const std::string &filename)
write a hierarchic grid to disk
Definition: yaspgrid/backuprestore.hh:89
YGridLevelIterator end() const
return iterator pointing to one past the finest level
Definition: yaspgrid.hh:307
const Torus< Communication, dim > & torus() const
return reference to torus
Definition: yaspgrid.hh:246
static Grid * restore(std::istream &stream, Comm comm=Comm())
read a hierarchic grid from a stream
Definition: yaspgrid/backuprestore.hh:149
typename Base::Communication Communication
Definition: yaspgrid.hh:181
static void backup(const Grid &grid, std::ostream &stream)
write a hierarchic grid to disk
Definition: yaspgrid/backuprestore.hh:105
Grid::Traits::Communication Comm
Definition: yaspgrid/backuprestore.hh:228
static Dune::YaspGrid< Coordinates::dimension, Coordinates > * createGrid(const Dune::FieldVector< typename Coordinates::ctype, Coordinates::dimension > &lowerleft, const Dune::FieldVector< typename Coordinates::ctype, Coordinates::dimension > &extension, A... args)
Definition: yaspgrid/backuprestore.hh:69
[ provides Dune::Grid ]
Definition: yaspgrid.hh:54
ct origin(int d) const
Definition: coordinates.hh:185
YaspGrid< dim, TensorProductCoordinates< ctype, dim > > Grid
Definition: yaspgrid/backuprestore.hh:227
int overlapSize(int level, [[maybe_unused]] int codim) const
return size (= distance in graph) of overlap region
Definition: yaspgrid.hh:1399
int maxLevel() const
Definition: yaspgrid.hh:1210
static Grid * restore(const std::string &filename, Comm comm=Comm())
read a hierarchic grid from disk
Definition: yaspgrid/backuprestore.hh:134
static void readOrigin(S &s, Dune::FieldVector< ctype, dim > &coord)
Definition: yaspgrid/backuprestore.hh:60
Dune::YaspGrid< dim, Coordinates > Grid
Definition: yaspgrid/backuprestore.hh:84
Grid::ctype ctype
Definition: yaspgrid/backuprestore.hh:85
Container for equidistant coordinates in a YaspGrid with non-trivial origin.
Definition: coordinates.hh:130
ReservedVector< YGridLevel, 32 >::const_iterator YGridLevelIterator
Iterator over the grid levels.
Definition: yaspgrid.hh:290
Coordinates::ctype ctype
Type used for coordinates.
Definition: yaspgrid.hh:179
static Grid * restore(std::istream &stream, Comm comm=Comm())
read a hierarchic grid from disk
Definition: yaspgrid/backuprestore.hh:292
facility for writing and reading grids
Definition: common/backuprestore.hh:42
Implement partitioner that gets a fixed partitioning from an array If the given partitioning doesn&#39;t ...
Definition: partitioning.hh:146
static Grid * restore(const std::string &)
read a hierarchic grid from disk
Definition: common/backuprestore.hh:78
Include standard header files.
Definition: agrid.hh:59
static void backup(const Grid &, const std::string &)
write a hierarchic grid to disk
Definition: common/backuprestore.hh:51
Grid::Traits::Communication Comm
Definition: yaspgrid/backuprestore.hh:86
concept Grid
Requirements for implementations of the Dune::Grid interface.The Grid concept defines interface requi...
Definition: concepts/grid.hh:109
static void readOrigin(S &, Dune::FieldVector< typename Coordinates::ctype, Coordinates::dimension > &)
Definition: yaspgrid/backuprestore.hh:34
static void backup(const Grid &grid, const std::string &filename)
write a hierarchic grid to disk
Definition: yaspgrid/backuprestore.hh:231
constexpr Overlap overlap
PartitionSet for the overlap partition.
Definition: partitionset.hh:277
void globalRefine(int refCount)
refine the grid refCount times.
Definition: yaspgrid.hh:1216
const Communication & comm() const
return a communication object
Definition: yaspgrid.hh:1756
static void writeOrigin(S &s, const Coordinates &coord)
Definition: yaspgrid/backuprestore.hh:51
static Grid * restore(const std::string &filename, Comm comm=Comm())
read a hierarchic grid from disk
Definition: yaspgrid/backuprestore.hh:273
Coordinate container for a tensor product YaspGrid.
Definition: coordinates.hh:244
static void writeOrigin(S &, const Coordinates &)
Definition: yaspgrid/backuprestore.hh:30
YGridLevelIterator begin() const
return iterator pointing to coarsest level
Definition: yaspgrid.hh:293
static Dune::YaspGrid< Coordinates::dimension, Coordinates > * createGrid(const Dune::FieldVector< typename Coordinates::ctype, Coordinates::dimension > &, A... args)
Definition: yaspgrid/backuprestore.hh:38
static void backup(const Grid &grid, std::ostream &stream)
write a hierarchic grid to disk
Definition: yaspgrid/backuprestore.hh:246
Dune::EquidistantOffsetCoordinates< ctype, dim > Coordinates
Definition: yaspgrid/backuprestore.hh:48
void refineOptions(bool keepPhysicalOverlap)
set options for refinement
Definition: yaspgrid.hh:1270
Definition: yaspgrid/backuprestore.hh:26
bool isPeriodic(int i) const
return whether the grid is periodic in direction i
Definition: yaspgrid.hh:279
#define YASPGRID_BACKUPRESTORE_FORMAT_VERSION
Definition: yaspgrid/backuprestore.hh:20
int levelSize(int l, int i) const
return size of the grid (in cells) on level l in direction i
Definition: yaspgrid.hh:264
const iTupel & dims() const
return dimensions of torus
Definition: torus.hh:112