dune-spgrid  2.7
backuprestore.hh
Go to the documentation of this file.
1 #ifndef DUNE_SPGRID_BACKUPRESTORE_HH
2 #define DUNE_SPGRID_BACKUPRESTORE_HH
3 
4 #include <dune/common/exceptions.hh>
5 
6 #include <dune/grid/common/backuprestore.hh>
7 
11 
12 namespace Dune
13 {
14 
35  template< class ct, int dim, template< int > class Ref, class Comm >
36  struct BackupRestoreFacility< SPGrid< ct, dim, Ref, Comm > >
37  {
39 
41 
48  static void backup ( const Grid &grid, const std::string &path, const std::string &fileprefix )
49  {
50  backup( path + "/" + fileprefix + ".spgrid" );
51  }
52 
58  static void backup ( const Grid &grid, const std::string &filename )
59  {
61  backup( grid, ioData );
62 
63  int result = 0;
64  if( grid.comm().rank() == 0 )
65  {
66  std::ofstream stream( filename.c_str() );
67  if( stream )
68  result = int( ioData.write( stream ) );
69  }
70  grid.comm().broadcast( &result, 1, 0 );
71  if( !result )
72  DUNE_THROW( IOError, "Unable to write SPGrid to file '" + filename + "'." );
73  }
74 
80  static void backup ( const Grid &grid, std::ostream &stream )
81  {
83  backup( grid, ioData );
84 
85  int result = 0;
86  if( grid.comm().rank() == 0 )
87  result = int( ioData.write( stream ) );
88  grid.comm().broadcast( &result, 1, 0 );
89  if( !result )
90  DUNE_THROW( IOError, "Unable to write SPGrid to stream." );
91  }
92 
101  static Grid *restore ( const std::string &path, const std::string &fileprefix,
103  {
104  return restore( path + "/" + fileprefix + ".spgrid" );
105  }
106 
114  static Grid *restore ( const std::string &filename,
116  {
117  std::ifstream stream( filename.c_str() );
118  if( !stream )
119  DUNE_THROW( IOError, "Unable to open file: " + filename );
120 
121  Grid *grid = 0;
123  if( ioData.read( stream, filename ) )
124  grid = restore( ioData, comm );
125 
126  if( !parallelAnd( comm, grid ) )
127  {
128  delete grid;
129  DUNE_THROW( IOError, "Unable to read grid from file '" + filename + "'." );
130  }
131  return grid;
132  }
133 
139  static Grid *restore ( std::istream &stream,
141  {
142  Grid *grid = 0;
144  if( ioData.read( stream ) )
145  grid = restore( ioData, comm );
146 
147  if( !parallelAnd( comm, grid ) )
148  {
149  delete grid;
150  DUNE_THROW( IOError, "Unable to read grid from stream." );
151  }
152  return grid;
153  }
154 
155  private:
156  static void backup ( const Grid &grid, SPGridIOData< ct, dim, Ref > &ioData )
157  {
158  ioData.time = 0;
159  ioData.cubes.push_back( grid.domain().cube() );
160  ioData.topology = grid.domain().topology();
161  ioData.cells = grid.globalMesh_.width();
162  ioData.partitions = grid.comm().size();
163  ioData.overlap = grid.overlap_;
164  ioData.maxLevel = grid.maxLevel();
165  ioData.refinements.resize( ioData.maxLevel );
166  for( int level = 0; level < ioData.maxLevel; ++level )
167  ioData.refinements[ level ] = grid.gridLevel( level+1 ).refinement().policy();
168  }
169 
170  static Grid *restore ( const SPGridIOData< ct, dim, Ref > &ioData,
171  const CollectiveCommunication &comm = SPCommunicationTraits< Comm >::defaultComm() )
172  {
173  if( ioData.partitions != comm.size() )
174  {
175  std::cerr << "Warning: Reading grid with different number of partitions,"
176  << " index sets will not coincide." << std::endl;
177  }
178 
179  typename Grid::Domain domain( ioData.cubes, ioData.topology );
180  Grid *grid = new Grid( domain, ioData.cells, ioData.overlap, comm );
181 
182  for( int level = 0; level < ioData.maxLevel; ++level )
183  {
184  if( level < int( ioData.refinements.size() ) )
185  grid->globalRefine( 1, ioData.refinements[ level ] );
186  else
187  grid->globalRefine( 1 );
188  }
189  return grid;
190  }
191 
192  static bool parallelAnd ( const CollectiveCommunication &comm, bool condition )
193  {
194  int result( condition );
195  result = comm.sum( result );
196  return (result == comm.size());
197  }
198  };
199 
200 } // namespace Dune
201 
202 #endif // #ifndef DUNE_SPGRID_BACKUPRESTORE_HH
Definition: iostream.hh:7
static void backup(const Grid &grid, const std::string &filename)
write a hierarchic grid to disk
Definition: backuprestore.hh:58
static Grid * restore(const std::string &path, const std::string &fileprefix, const CollectiveCommunication &comm=SPCommunicationTraits< Comm >::defaultComm())
read a hierarchic grid from disk
Definition: backuprestore.hh:101
static void backup(const Grid &grid, std::ostream &stream)
write a hierarchic grid into a stream
Definition: backuprestore.hh:80
static Grid * restore(std::istream &stream, const CollectiveCommunication &comm=SPCommunicationTraits< Comm >::defaultComm())
read a hierarchic grid from a stream
Definition: backuprestore.hh:139
static Grid * restore(const std::string &filename, const CollectiveCommunication &comm=SPCommunicationTraits< Comm >::defaultComm())
read a hierarchic grid from disk
Definition: backuprestore.hh:114
static void backup(const Grid &grid, const std::string &path, const std::string &fileprefix)
write a hierarchic grid to disk
Definition: backuprestore.hh:48
SPGrid< ct, dim, Ref, Comm > Grid
Definition: backuprestore.hh:38
Grid::CollectiveCommunication CollectiveCommunication
Definition: backuprestore.hh:40
Definition: communication.hh:24
static CollectiveCommunication defaultComm()
Definition: communication.hh:33
structured, parallel DUNE grid
Definition: grid.hh:136
const CollectiveCommunication & comm() const
Definition: grid.hh:664
Traits::CollectiveCommunication CollectiveCommunication
Definition: grid.hh:168
Definition: fileio.hh:24
int maxLevel
Definition: fileio.hh:38
int partitions
Definition: fileio.hh:37
MultiIndex overlap
Definition: fileio.hh:36
ctype time
Definition: fileio.hh:32
std::vector< RefinementPolicy > refinements
Definition: fileio.hh:39
MultiIndex cells
Definition: fileio.hh:35
Topology topology
Definition: fileio.hh:34
bool read(std::istream &stream, const std::string &info="")
Definition: fileio.hh:114
std::vector< Cube > cubes
Definition: fileio.hh:33
bool write(std::ostream &stream) const
Definition: fileio.hh:61
facility for writing and reading a SPGrid
Definition: grid.hh:33