1 #ifndef DUNE_SPGRID_BACKUPRESTORE_HH
2 #define DUNE_SPGRID_BACKUPRESTORE_HH
4 #include <dune/common/exceptions.hh>
6 #include <dune/grid/common/backuprestore.hh>
35 template<
class ct,
int dim,
template<
int >
class Ref,
class Comm >
48 static void backup (
const Grid &grid,
const std::string &path,
const std::string &fileprefix )
50 backup( path +
"/" + fileprefix +
".spgrid" );
58 static void backup (
const Grid &grid,
const std::string &filename )
61 backup( grid, ioData );
64 if( grid.
comm().rank() == 0 )
66 std::ofstream stream( filename.c_str() );
68 result = int( ioData.
write( stream ) );
70 grid.
comm().broadcast( &result, 1, 0 );
72 DUNE_THROW( IOError,
"Unable to write SPGrid to file '" + filename +
"'." );
80 static void backup (
const Grid &grid, std::ostream &stream )
83 backup( grid, ioData );
86 if( grid.
comm().rank() == 0 )
87 result = int( ioData.
write( stream ) );
88 grid.
comm().broadcast( &result, 1, 0 );
90 DUNE_THROW( IOError,
"Unable to write SPGrid to stream." );
101 static Grid *
restore (
const std::string &path,
const std::string &fileprefix,
104 return restore( path +
"/" + fileprefix +
".spgrid" );
117 std::ifstream stream( filename.c_str() );
119 DUNE_THROW( IOError,
"Unable to open file: " + filename );
123 if( ioData.
read( stream, filename ) )
124 grid = restore( ioData, comm );
126 if( !parallelAnd( comm, grid ) )
129 DUNE_THROW( IOError,
"Unable to read grid from file '" + filename +
"'." );
144 if( ioData.
read( stream ) )
145 grid = restore( ioData, comm );
147 if( !parallelAnd( comm, grid ) )
150 DUNE_THROW( IOError,
"Unable to read grid from stream." );
159 ioData.
cubes.push_back( grid.domain().cube() );
160 ioData.
topology = grid.domain().topology();
161 ioData.
cells = grid.globalMesh_.width();
163 ioData.
overlap = grid.overlap_;
166 for(
int level = 0; level < ioData.
maxLevel; ++level )
167 ioData.
refinements[ level ] = grid.gridLevel( level+1 ).refinement().policy();
170 static Grid *restore (
const SPGridIOData< ct, dim, Ref > &ioData,
173 if( ioData.partitions != comm.size() )
175 std::cerr <<
"Warning: Reading grid with different number of partitions,"
176 <<
" index sets will not coincide." << std::endl;
179 typename Grid::Domain domain( ioData.cubes, ioData.topology );
180 Grid *grid =
new Grid( domain, ioData.cells, ioData.overlap, comm );
182 for(
int level = 0; level < ioData.maxLevel; ++level )
184 if( level <
int( ioData.refinements.size() ) )
185 grid->globalRefine( 1, ioData.refinements[ level ] );
187 grid->globalRefine( 1 );
192 static bool parallelAnd (
const CollectiveCommunication &comm,
bool condition )
194 int result( condition );
195 result = comm.sum( result );
196 return (result == comm.size());
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
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