Dune Core Modules (unstable)

backuprestore.hh
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
15#include <dune/grid/common/backuprestore.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
22namespace Dune
23{
24
25 template<class Coordinates>
26 struct MaybeHaveOrigin
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>
40 {
42 }
43 };
44
45 template<class ctype, int dim>
46 struct MaybeHaveOrigin<Dune::EquidistantOffsetCoordinates<ctype, dim> >
47 {
48 typedef typename Dune::EquidistantOffsetCoordinates<ctype, dim> Coordinates;
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>
72 {
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;
86 typedef typename Grid::Traits::Communication Comm;
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;
156 if (version != YASPGRID_BACKUPRESTORE_FORMAT_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
196 stream >> input;
197 for (int i=0; i<dim; i++)
198 stream >> h[i];
199
201 MaybeHaveOrigin<Coordinates>::readOrigin(stream, origin);
202
203 // the constructor takes the upper right corner...
205 for (int i=0; i<dim; i++)
206 length[i] *= coarseSize[i];
207
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
228 typedef typename Grid::Traits::Communication Comm;
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;
299 if (version != YASPGRID_BACKUPRESTORE_FORMAT_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
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
Container for equidistant coordinates in a YaspGrid with non-trivial origin.
Definition: coordinates.hh:131
ct origin(int d) const
Definition: coordinates.hh:185
Base class for Dune-Exceptions.
Definition: exceptions.hh:98
vector space out of a tensor product of fields.
Definition: fvector.hh:97
Coordinate container for a tensor product YaspGrid.
Definition: coordinates.hh:245
const iTupel & dims() const
return dimensions of torus
Definition: torus.hh:112
[ provides Dune::Grid ]
Definition: yaspgrid.hh:166
const Torus< Communication, dim > & torus() const
return reference to torus
Definition: yaspgrid.hh:246
void globalRefine(int refCount)
refine the grid refCount times.
Definition: yaspgrid.hh:1216
YGridLevelIterator end() const
return iterator pointing to one past the finest level
Definition: yaspgrid.hh:307
int maxLevel() const
Definition: yaspgrid.hh:1210
int overlapSize(int level, int codim) const
return size (= distance in graph) of overlap region
Definition: yaspgrid.hh:1399
bool isPeriodic(int i) const
return whether the grid is periodic in direction i
Definition: yaspgrid.hh:279
void refineOptions(bool keepPhysicalOverlap)
set options for refinement
Definition: yaspgrid.hh:1270
ReservedVector< YGridLevel, 32 >::const_iterator YGridLevelIterator
Iterator over the grid levels.
Definition: yaspgrid.hh:290
const Communication & comm() const
return a communication object
Definition: yaspgrid.hh:1756
Coordinates::ctype ctype
Type used for coordinates.
Definition: yaspgrid.hh:179
int levelSize(int l, int i) const
return size of the grid (in cells) on level l in direction i
Definition: yaspgrid.hh:264
YGridLevelIterator begin() const
return iterator pointing to coarsest level
Definition: yaspgrid.hh:293
Implement partitioner that gets a fixed partitioning from an array If the given partitioning doesn't ...
Definition: partitioning.hh:147
A few common exception classes.
Implements a vector constructed from a given type representing a field and a compile-time given size.
#define DUNE_THROW(E,...)
Definition: exceptions.hh:314
Dune namespace.
Definition: alignedallocator.hh:13
constexpr std::integral_constant< std::size_t, sizeof...(II)> size(std::integer_sequence< T, II... >)
Return the size of the sequence.
Definition: integersequence.hh:75
static void backup(const Grid &grid, std::ostream &stream)
write a hierarchic grid to disk
Definition: backuprestore.hh:105
static Grid * restore(std::istream &stream, Comm comm=Comm())
read a hierarchic grid from a stream
Definition: backuprestore.hh:149
static Grid * restore(const std::string &filename, Comm comm=Comm())
read a hierarchic grid from disk
Definition: backuprestore.hh:134
static void backup(const Grid &grid, const std::string &filename)
write a hierarchic grid to disk
Definition: backuprestore.hh:89
static void backup(const Grid &grid, const std::string &filename)
write a hierarchic grid to disk
Definition: backuprestore.hh:231
static Grid * restore(std::istream &stream, Comm comm=Comm())
read a hierarchic grid from disk
Definition: backuprestore.hh:292
static Grid * restore(const std::string &filename, Comm comm=Comm())
read a hierarchic grid from disk
Definition: backuprestore.hh:273
static void backup(const Grid &grid, std::ostream &stream)
write a hierarchic grid to disk
Definition: backuprestore.hh:246
facility for writing and reading grids
Definition: backuprestore.hh:43
static Grid * restore(const std::string &)
read a hierarchic grid from disk
Definition: backuprestore.hh:78
static void backup(const Grid &, const std::string &)
write a hierarchic grid to disk
Definition: backuprestore.hh:51
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden & Uni Heidelberg  |  generated with Hugo v0.111.3 (Nov 15, 23:38, 2025)