Dune Core Modules (2.4.2)

backuprestore.hh
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3#ifndef DUNE_GRID_YASPGRID_BACKUPRESTORE_HH
4#define DUNE_GRID_YASPGRID_BACKUPRESTORE_HH
5
6//- system headers
7#include <fstream>
8
9//- Dune headers
12#include <dune/grid/common/backuprestore.hh>
13#include <dune/grid/yaspgrid.hh>
14
15// bump this version number up if you introduce any changes
16// to the outout format of the YaspGrid BackupRestoreFacility.
17#define YASPGRID_BACKUPRESTORE_FORMAT_VERSION 2
18
19namespace Dune
20{
21
22 template<class Coordinates>
23 struct MaybeHaveOrigin
24 {
25
26 template<class S>
27 static void writeOrigin(S& s, const Coordinates& coord)
28 {}
29
30 template<class S>
32 {}
33
34 template<typename... A>
37 {
39 }
40 };
41
42 template<class ctype, int dim>
43 struct MaybeHaveOrigin<Dune::EquidistantOffsetCoordinates<ctype, dim> >
44 {
45 typedef typename Dune::EquidistantOffsetCoordinates<ctype, dim> Coordinates;
46
47 template<class S>
48 static void writeOrigin(S& s, const Coordinates& coord)
49 {
50 s << "Origin: ";
51 for (int i=0; i<dim; i++)
52 s << coord.origin(i) << " ";
53 s << std::endl;
54 }
55
56 template<class S>
57 static void readOrigin(S& s, Dune::FieldVector<ctype, dim>& coord)
58 {
59 std::string token;
60 s >> token;
61 for (int i=0; i<dim; i++)
62 s >> coord[i];
63 }
64
65 template<typename... A>
69 {
71 upperright += extension;
72 return new Dune::YaspGrid<Coordinates::dimension,Coordinates>(lowerleft, upperright, args...);
73 }
74 };
75
77 template<int dim, class Coordinates>
78 struct BackupRestoreFacility<Dune::YaspGrid<dim, Coordinates> >
79 {
80 // type of grid
82 typedef typename Grid::ctype ctype;
83 typedef typename Grid::Traits::CollectiveCommunication Comm;
84
86 static void backup ( const Grid &grid, const std::string &filename )
87 {
88 if (grid.comm().rank() == 0)
89 {
90 std::ofstream file(filename);
91 if( file )
92 {
93 backup(grid,file);
94 file.close();
95 }
96 else
97 std::cerr << "ERROR: BackupRestoreFacility::backup: couldn't open file `" << filename << "'" << std::endl;
98 }
99 }
100
102 static void backup ( const Grid &grid, std::ostream &stream )
103 {
104 stream << "YaspGrid BackupRestore Format Version: " << YASPGRID_BACKUPRESTORE_FORMAT_VERSION << std::endl;
105 stream << "Torus structure: ";
106 for (int i=0; i<dim; i++)
107 stream << grid.torus().dims(i) << " ";
108 stream << std::endl << "Refinement level: " << grid.maxLevel() << std::endl;
109 stream << "Periodicity: ";
110 for (int i=0; i<dim; i++)
111 stream << (grid.isPeriodic(i) ? "1 " : "0 ");
112 stream << std::endl << "Overlap: " << grid.overlapSize(0,0) << std::endl;
113 stream << "KeepPhysicalOverlap: ";
114 for (typename Grid::YGridLevelIterator i=++grid.begin(); i != grid.end(); ++i)
115 stream << (i->keepOverlap ? "1" : "0") << " ";
116 stream << std::endl;
117 stream << "Coarse Size: ";
118 for (int i=0; i<dim; i++)
119 stream << grid.levelSize(0,i) << " ";
120 stream << std::endl;
121 stream << "Meshsize: " ;
122 for (int i=0; i<dim; i++)
123 stream << grid.begin()->coords.meshsize(i,0) << " ";
124 stream << std::endl;
125 MaybeHaveOrigin<Coordinates>::writeOrigin(stream, grid.begin()->coords);
126 }
127
129 static Grid *restore (const std::string &filename, Comm comm = Comm())
130 {
131 std::ifstream file(filename);
132 if( file )
133 return restore(file,comm);
134 else
135 {
136 std::cerr << "ERROR: BackupRestoreFacility::restore: couldn't open file `" << filename << "'" << std::endl;
137 return 0;
138 }
139 }
140
142 static Grid *restore (std::istream &stream, Comm comm = Comm())
143 {
144 std::string input;
145
146 int version;
147 stream >> input >> input >> input >> input;
148 stream >> version;
149 if (version != YASPGRID_BACKUPRESTORE_FORMAT_VERSION)
150 DUNE_THROW(Dune::Exception, "Your YaspGrid backup file is written in an outdated format!");
151
152 Dune::array<int,dim> torus_dims;
153 stream >> input >> input;
154 for (int i=0; i<dim; i++)
155 stream >> torus_dims[i];
156
157 int refinement;
158 stream >> input >> input;
159 stream >> refinement;
160
161 std::bitset<dim> periodic;
162 bool b;
163 stream >> input;
164 for (int i=0; i<dim; i++)
165 {
166 stream >> b;
167 periodic[i] = b;
168 }
169
170 int overlap;
171 stream >> input;
172 stream >> overlap;
173
174 std::vector<bool> physicalOverlapSize;
175 physicalOverlapSize.resize(refinement);
176 stream >> input;
177 for (int i=0; i<refinement; ++i)
178 {
179 stream >> b;
180 physicalOverlapSize[i] = b;
181 }
182
183 Dune::array<int,dim> coarseSize;
184 stream >> input >> input;
185 for (int i=0; i<dim; i++)
186 stream >> coarseSize[i];
187
189 stream >> input;
190 for (int i=0; i<dim; i++)
191 stream >> h[i];
192
194 MaybeHaveOrigin<Coordinates>::readOrigin(stream, origin);
195
196 // the constructor takes the upper right corner...
198 for (int i=0; i<dim; i++)
199 length[i] *= coarseSize[i];
200
201 YaspFixedSizePartitioner<dim> lb(torus_dims);
202
203 Grid* grid = MaybeHaveOrigin<Coordinates>::createGrid(origin, length, coarseSize, periodic, overlap, comm, &lb);
204
205 for (int i=0; i<refinement; ++i)
206 {
207 grid->refineOptions(physicalOverlapSize[i]);
208 grid->globalRefine(1);
209 }
210
211 return grid;
212 }
213 };
214
216 template<int dim, class ctype>
218 {
219 // type of grid
221 typedef typename Grid::Traits::CollectiveCommunication Comm;
222
224 static void backup ( const Grid &grid, const std::string &filename )
225 {
226 std::ostringstream filename_str;
227 filename_str << filename << grid.comm().rank();
228 std::ofstream file( filename_str.str() );
229 if( file )
230 {
231 backup(grid,file);
232 file.close();
233 }
234 else
235 std::cerr << "ERROR: BackupRestoreFacility::backup: couldn't open file `" << filename_str.str() << "'" << std::endl;
236 }
237
239 static void backup ( const Grid &grid, std::ostream &stream )
240 {
241 stream << "YaspGrid BackupRestore Format Version: " << YASPGRID_BACKUPRESTORE_FORMAT_VERSION << std::endl;
242 stream << "Torus structure: ";
243 for (int i=0; i<dim; i++)
244 stream << grid.torus().dims(i) << " ";
245 stream << std::endl << "Refinement level: " << grid.maxLevel() << std::endl;
246 stream << "Periodicity: ";
247 for (int i=0; i<dim; i++)
248 stream << (grid.isPeriodic(i) ? "1 " : "0 ");
249 stream << std::endl << "Overlap: " << grid.overlapSize(0,0) << std::endl;
250 stream << "KeepPhysicalOverlap: ";
251 for (typename Grid::YGridLevelIterator i=++grid.begin(); i != grid.end(); ++i)
252 stream << (i->keepOverlap ? "1" : "0") << " ";
253 stream << std::endl;
254 stream << "Coarse Size: ";
255 for (int i=0; i<dim; i++)
256 stream << grid.levelSize(0,i) << " ";
257 stream << std::endl;
258
259 grid.begin()->coords.print(stream);
260 }
261
263 static Grid *restore (const std::string &filename, Comm comm = Comm())
264 {
265 std::ostringstream filename_str;
266 filename_str << filename;
267 filename_str << comm.rank();
268 std::ifstream file(filename_str.str());
269 if( file )
270 return restore(file, comm);
271 else
272 {
273 std::cerr << "ERROR: BackupRestoreFacility::restore: couldn't open file `" << filename_str.str() << "'" << std::endl;
274 return 0;
275 }
276 }
277
279 static Grid *restore (std::istream &stream, Comm comm = Comm())
280 {
281 std::string input;
282
283 int version;
284 stream >> input >> input >> input >> input;
285 stream >> version;
286 if (version != YASPGRID_BACKUPRESTORE_FORMAT_VERSION)
287 DUNE_THROW(Dune::Exception, "Your YaspGrid backup file is written in an outdated format!");
288
289 Dune::array<int,dim> torus_dims;
290 stream >> input >> input;
291 for (int i=0; i<dim; i++)
292 stream >> torus_dims[i];
293
294 int refinement;
295 stream >> input >> input;
296 stream >> refinement;
297
298 std::bitset<dim> periodic;
299 bool b;
300 stream >> input;
301 for (int i=0; i<dim; i++)
302 {
303 stream >> b;
304 periodic[i] = b;
305 }
306
307 int overlap;
308 stream >> input;
309 stream >> overlap;
310
311 std::vector<bool> physicalOverlapSize;
312 physicalOverlapSize.resize(refinement);
313 stream >> input;
314 for (int i=0; i<refinement; ++i)
315 {
316 stream >> b;
317 physicalOverlapSize[i] = b;
318 }
319
320
321 Dune::array<int,dim> coarseSize;
322 stream >> input >> input;
323 for (int i=0; i<dim; i++)
324 stream >> coarseSize[i];
325
326 Dune::array<std::vector<ctype>,dim> coords;
327 stream >> input >> input >> input >> input;
328 for (int d=0; d<dim; d++)
329 {
330 stream >> input >> input;
331 int size;
332 stream >> size;
333 stream >> input;
334 ctype tmp;
335 coords[d].resize(size);
336 for (int i=0; i<size; i++)
337 {
338 stream >> tmp;
339 coords[d][i] = tmp;
340 }
341 }
342
343 YaspFixedSizePartitioner<dim> lb(torus_dims);
344 Grid* grid = new Grid(coords, periodic, overlap, comm, coarseSize, &lb);
345
346 for (int i=0; i<refinement; ++i)
347 {
348 grid->refineOptions(physicalOverlapSize[i]);
349 grid->globalRefine(1);
350 }
351
352 return grid;
353 }
354 };
355} // namespace Dune
356
357#endif // #ifndef DUNE_GRID_YASPGRID_BACKUPRESTORE_HH
Container for equidistant coordinates in a YaspGrid with non-trivial origin.
Definition: coordinates.hh:125
ct origin(int d) const
Definition: coordinates.hh:174
Base class for Dune-Exceptions.
Definition: exceptions.hh:91
vector space out of a tensor product of fields.
Definition: fvector.hh:94
int overlapSize(int level, int codim) const
overlapSize is zero by default
Definition: grid.hh:1199
GridFamily::Traits::CollectiveCommunication CollectiveCommunication
A type that is a model of Dune::CollectiveCommunication. It provides a portable way for collective co...
Definition: grid.hh:545
const CollectiveCommunication & comm() const
return const reference to a collective communication object. The return type is a model of Dune::Coll...
Definition: grid.hh:921
Coordinate container for a tensor product YaspGrid.
Definition: coordinates.hh:234
Implement partitioner that gets a fixed partitioning from an array If the given partitioning doesn't ...
Definition: partitioning.hh:116
[ provides Dune::Grid ]
Definition: yaspgrid.hh:166
void globalRefine(int refCount)
refine the grid refCount times.
Definition: yaspgrid.hh:1208
const Torus< CollectiveCommunicationType, dim > & torus() const
return reference to torus
Definition: yaspgrid.hh:250
YGridLevelIterator end() const
return iterator pointing to one past the finest level
Definition: yaspgrid.hh:311
int maxLevel() const
Definition: yaspgrid.hh:1202
bool isPeriodic(int i) const
return whether the grid is periodic in direction i
Definition: yaspgrid.hh:283
void refineOptions(bool keepPhysicalOverlap)
set options for refinement
Definition: yaspgrid.hh:1262
ReservedVector< YGridLevel, 32 >::const_iterator YGridLevelIterator
Iterator over the grid levels.
Definition: yaspgrid.hh:294
Coordinates::ctype ctype
Type used for coordinates.
Definition: yaspgrid.hh:180
int levelSize(int l, int i) const
return size of the grid (in cells) on level l in direction i
Definition: yaspgrid.hh:268
YGridLevelIterator begin() const
return iterator pointing to coarsest level
Definition: yaspgrid.hh:297
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:243
Dune namespace.
Definition: alignment.hh:10
static void backup(const Grid &grid, std::ostream &stream)
Definition: backuprestore.hh:102
static Grid * restore(std::istream &stream, Comm comm=Comm())
Definition: backuprestore.hh:142
static Grid * restore(const std::string &filename, Comm comm=Comm())
Definition: backuprestore.hh:129
static void backup(const Grid &grid, const std::string &filename)
Definition: backuprestore.hh:86
static void backup(const Grid &grid, const std::string &filename)
Definition: backuprestore.hh:224
static Grid * restore(std::istream &stream, Comm comm=Comm())
Definition: backuprestore.hh:279
static Grid * restore(const std::string &filename, Comm comm=Comm())
Definition: backuprestore.hh:263
static void backup(const Grid &grid, std::ostream &stream)
Definition: backuprestore.hh:239
facility for writing and reading grids
Definition: backuprestore.hh:41
static Grid * restore(const std::string &filename)
read a hierarchic grid from disk
Definition: backuprestore.hh:76
static void backup(const Grid &grid, const std::string &filename)
write a hierarchic grid to disk
Definition: backuprestore.hh:49
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Dec 21, 23:30, 2024)