Dune Core Modules (2.9.1)

backuprestore.hh
1// SPDX-FileCopyrightText: Copyright (C) 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 outout 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
132 static Grid *restore (const std::string &filename, Comm comm = Comm())
133 {
134 std::ifstream file(filename);
135 if( file )
136 return restore(file,comm);
137 else
138 {
139 std::cerr << "ERROR: BackupRestoreFacility::restore: couldn't open file `" << filename << "'" << std::endl;
140 return 0;
141 }
142 }
143
145 static Grid *restore (std::istream &stream, Comm comm = Comm())
146 {
147 std::string input;
148
149 int version;
150 stream >> input >> input >> input >> input;
151 stream >> version;
152 if (version != YASPGRID_BACKUPRESTORE_FORMAT_VERSION)
153 DUNE_THROW(Dune::Exception, "Your YaspGrid backup file is written in an outdated format!");
154
155 std::array<int,dim> torus_dims;
156 stream >> input >> input;
157 for (int i=0; i<dim; i++)
158 stream >> torus_dims[i];
159
160 int refinement;
161 stream >> input >> input;
162 stream >> refinement;
163
164 std::bitset<dim> periodic;
165 bool b;
166 stream >> input;
167 for (int i=0; i<dim; i++)
168 {
169 stream >> b;
170 periodic[i] = b;
171 }
172
173 int overlap;
174 stream >> input;
175 stream >> overlap;
176
177 std::vector<bool> physicalOverlapSize;
178 physicalOverlapSize.resize(refinement);
179 stream >> input;
180 for (int i=0; i<refinement; ++i)
181 {
182 stream >> b;
183 physicalOverlapSize[i] = b;
184 }
185
186 std::array<int,dim> coarseSize;
187 stream >> input >> input;
188 for (int i=0; i<dim; i++)
189 stream >> coarseSize[i];
190
192 stream >> input;
193 for (int i=0; i<dim; i++)
194 stream >> h[i];
195
197 MaybeHaveOrigin<Coordinates>::readOrigin(stream, origin);
198
199 // the constructor takes the upper right corner...
201 for (int i=0; i<dim; i++)
202 length[i] *= coarseSize[i];
203
205
206 Grid* grid = MaybeHaveOrigin<Coordinates>::createGrid(origin, length, coarseSize, periodic, overlap, comm, &lb);
207
208 for (int i=0; i<refinement; ++i)
209 {
210 grid->refineOptions(physicalOverlapSize[i]);
211 grid->globalRefine(1);
212 }
213
214 return grid;
215 }
216 };
217
219 template<int dim, class ctype>
221 {
222 // type of grid
224 typedef typename Grid::Traits::Communication Comm;
225
227 static void backup ( const Grid &grid, const std::string &filename )
228 {
229 std::ostringstream filename_str;
230 filename_str << filename << grid.comm().rank();
231 std::ofstream file( filename_str.str() );
232 if( file )
233 {
234 backup(grid,file);
235 file.close();
236 }
237 else
238 std::cerr << "ERROR: BackupRestoreFacility::backup: couldn't open file `" << filename_str.str() << "'" << std::endl;
239 }
240
242 static void backup ( const Grid &grid, std::ostream &stream )
243 {
244 stream << "YaspGrid BackupRestore Format Version: " << YASPGRID_BACKUPRESTORE_FORMAT_VERSION << std::endl;
245 stream << "Torus structure: ";
246 for (int i=0; i<dim; i++)
247 stream << grid.torus().dims(i) << " ";
248 stream << std::endl << "Refinement level: " << grid.maxLevel() << std::endl;
249 stream << "Periodicity: ";
250 for (int i=0; i<dim; i++)
251 stream << (grid.isPeriodic(i) ? "1 " : "0 ");
252 stream << std::endl << "Overlap: " << grid.overlapSize(0,0) << std::endl;
253 stream << "KeepPhysicalOverlap: ";
254 for (typename Grid::YGridLevelIterator i=std::next(grid.begin()); i != grid.end(); ++i)
255 stream << (i->keepOverlap ? "1" : "0") << " ";
256 stream << std::endl;
257 stream << "Coarse Size: ";
258 for (int i=0; i<dim; i++)
259 stream << grid.levelSize(0,i) << " ";
260 stream << std::endl;
261
262 grid.begin()->coords.print(stream);
263 }
264
266 static Grid *restore (const std::string &filename, Comm comm = Comm())
267 {
268 std::ostringstream filename_str;
269 filename_str << filename;
270 filename_str << comm.rank();
271 std::ifstream file(filename_str.str());
272 if( file )
273 return restore(file, comm);
274 else
275 {
276 std::cerr << "ERROR: BackupRestoreFacility::restore: couldn't open file `" << filename_str.str() << "'" << std::endl;
277 return 0;
278 }
279 }
280
282 static Grid *restore (std::istream &stream, Comm comm = Comm())
283 {
284 std::string input;
285
286 int version;
287 stream >> input >> input >> input >> input;
288 stream >> version;
289 if (version != YASPGRID_BACKUPRESTORE_FORMAT_VERSION)
290 DUNE_THROW(Dune::Exception, "Your YaspGrid backup file is written in an outdated format!");
291
292 std::array<int,dim> torus_dims;
293 stream >> input >> input;
294 for (int i=0; i<dim; i++)
295 stream >> torus_dims[i];
296
297 int refinement;
298 stream >> input >> input;
299 stream >> refinement;
300
301 std::bitset<dim> periodic;
302 bool b;
303 stream >> input;
304 for (int i=0; i<dim; i++)
305 {
306 stream >> b;
307 periodic[i] = b;
308 }
309
310 int overlap;
311 stream >> input;
312 stream >> overlap;
313
314 std::vector<bool> physicalOverlapSize;
315 physicalOverlapSize.resize(refinement);
316 stream >> input;
317 for (int i=0; i<refinement; ++i)
318 {
319 stream >> b;
320 physicalOverlapSize[i] = b;
321 }
322
323
324 std::array<int,dim> coarseSize;
325 stream >> input >> input;
326 for (int i=0; i<dim; i++)
327 stream >> coarseSize[i];
328
329 std::array<std::vector<ctype>,dim> coords;
330 stream >> input >> input >> input >> input;
331 for (int d=0; d<dim; d++)
332 {
333 stream >> input >> input;
334 int size;
335 stream >> size;
336 stream >> input;
337 ctype tmp;
338 coords[d].resize(size);
339 for (int i=0; i<size; i++)
340 {
341 stream >> tmp;
342 coords[d][i] = tmp;
343 }
344 }
345
347 Grid* grid = new Grid(coords, periodic, overlap, comm, coarseSize, &lb);
348
349 for (int i=0; i<refinement; ++i)
350 {
351 grid->refineOptions(physicalOverlapSize[i]);
352 grid->globalRefine(1);
353 }
354
355 return grid;
356 }
357 };
358} // namespace Dune
359
360#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:96
vector space out of a tensor product of fields.
Definition: fvector.hh:95
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:163
const Torus< Communication, dim > & torus() const
return reference to torus
Definition: yaspgrid.hh:244
void globalRefine(int refCount)
refine the grid refCount times.
Definition: yaspgrid.hh:1223
YGridLevelIterator end() const
return iterator pointing to one past the finest level
Definition: yaspgrid.hh:305
int maxLevel() const
Definition: yaspgrid.hh:1217
int overlapSize(int level, int codim) const
return size (= distance in graph) of overlap region
Definition: yaspgrid.hh:1406
bool isPeriodic(int i) const
return whether the grid is periodic in direction i
Definition: yaspgrid.hh:277
void refineOptions(bool keepPhysicalOverlap)
set options for refinement
Definition: yaspgrid.hh:1277
ReservedVector< YGridLevel, 32 >::const_iterator YGridLevelIterator
Iterator over the grid levels.
Definition: yaspgrid.hh:288
const Communication & comm() const
return a communication object
Definition: yaspgrid.hh:1764
Coordinates::ctype ctype
Type used for coordinates.
Definition: yaspgrid.hh:176
int levelSize(int l, int i) const
return size of the grid (in cells) on level l in direction i
Definition: yaspgrid.hh:262
YGridLevelIterator begin() const
return iterator pointing to coarsest level
Definition: yaspgrid.hh:291
Implement partitioner that gets a fixed partitioning from an array If the given partitioning doesn't ...
Definition: partitioning.hh:148
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, m)
Definition: exceptions.hh:218
Dune namespace.
Definition: alignedallocator.hh:13
static void backup(const Grid &grid, std::ostream &stream)
Definition: backuprestore.hh:105
static Grid * restore(std::istream &stream, Comm comm=Comm())
Definition: backuprestore.hh:145
static Grid * restore(const std::string &filename, Comm comm=Comm())
Definition: backuprestore.hh:132
static void backup(const Grid &grid, const std::string &filename)
Definition: backuprestore.hh:89
static void backup(const Grid &grid, const std::string &filename)
Definition: backuprestore.hh:227
static Grid * restore(std::istream &stream, Comm comm=Comm())
Definition: backuprestore.hh:282
static Grid * restore(const std::string &filename, Comm comm=Comm())
Definition: backuprestore.hh:266
static void backup(const Grid &grid, std::ostream &stream)
Definition: backuprestore.hh:242
facility for writing and reading grids
Definition: backuprestore.hh:43
static Grid * restore(const std::string &filename)
read a hierarchic grid from disk
Definition: backuprestore.hh:78
static void backup(const Grid &grid, const std::string &filename)
write a hierarchic grid to disk
Definition: backuprestore.hh:51
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Jul 15, 22:36, 2024)