1#ifndef DUNE_SPGRID_BACKUPRESTORE_HH
2#define DUNE_SPGRID_BACKUPRESTORE_HH
7#include <dune/grid/common/backuprestore.hh>
9#include <dune/grid/spgrid/communication.hh>
10#include <dune/grid/spgrid/declaration.hh>
11#include <dune/grid/spgrid/fileio.hh>
36 template<
class ct,
int dim,
template<
int >
class Ref,
class Comm >
37 struct BackupRestoreFacility< SPGrid< ct, dim, Ref, Comm > >
39 typedef SPGrid< ct, dim, Ref, Comm > Grid;
49 static void backup (
const Grid &grid,
const std::string &path,
const std::string &fileprefix )
51 backup( path +
"/" + fileprefix +
".spgrid" );
59 static void backup (
const Grid &grid,
const std::string &filename )
61 SPGridIOData< ct, dim, Ref > ioData;
65 if( grid.comm().rank() == 0 )
67 std::ofstream stream( filename.c_str() );
69 result = int( ioData.write( stream ) );
71 grid.comm().broadcast( &result, 1, 0 );
73 DUNE_THROW( IOError,
"Unable to write SPGrid to file '" + filename +
"'." );
81 static void backup (
const Grid &grid, std::ostream &stream )
83 SPGridIOData< ct, dim, Ref > ioData;
87 if( grid.comm().rank() == 0 )
88 result = int( ioData.write( stream ) );
89 grid.comm().broadcast( &result, 1, 0 );
91 DUNE_THROW( IOError,
"Unable to write SPGrid to stream." );
102 static Grid *
restore (
const std::string &path,
const std::string &fileprefix,
103 const Communication &comm = SPCommunicationTraits< Comm >::defaultComm() )
105 return restore( path +
"/" + fileprefix +
".spgrid" );
115 static Grid *
restore (
const std::string &filename,
116 const Communication &comm = SPCommunicationTraits< Comm >::defaultComm() )
118 std::ifstream stream( filename.c_str() );
120 DUNE_THROW( IOError,
"Unable to open file: " + filename );
123 SPGridIOData< ct, dim, Ref > ioData;
124 if( ioData.read( stream, filename ) )
125 grid =
restore( ioData, comm );
127 if( !parallelAnd( comm, grid ) )
130 DUNE_THROW( IOError,
"Unable to read grid from file '" + filename +
"'." );
140 static Grid *
restore ( std::istream &stream,
141 const Communication &comm = SPCommunicationTraits< Comm >::defaultComm() )
144 SPGridIOData< ct, dim, Ref > ioData;
145 if( ioData.read( stream ) )
146 grid =
restore( ioData, comm );
148 if( !parallelAnd( comm, grid ) )
151 DUNE_THROW( IOError,
"Unable to read grid from stream." );
157 static void backup (
const Grid &grid, SPGridIOData< ct, dim, Ref > &ioData )
160 ioData.cubes.push_back( grid.domain().cube() );
161 ioData.topology = grid.domain().topology();
162 ioData.cells = grid.globalMesh_.width();
163 ioData.partitions = grid.comm().size();
164 ioData.overlap = grid.overlap_;
165 ioData.maxLevel = grid.maxLevel();
166 ioData.refinements.resize( ioData.maxLevel );
167 for(
int level = 0; level < ioData.maxLevel; ++level )
168 ioData.refinements[ level ] = grid.gridLevel( level+1 ).refinement().policy();
171 static Grid *
restore (
const SPGridIOData< ct, dim, Ref > &ioData,
172 const Communication &comm = SPCommunicationTraits< Comm >::defaultComm() )
174 if( ioData.partitions != comm.size() )
176 std::cerr <<
"Warning: Reading grid with different number of partitions,"
177 <<
" index sets will not coincide." << std::endl;
180 typename Grid::Domain domain( ioData.cubes, ioData.topology );
181 Grid *grid =
new Grid( domain, ioData.cells, ioData.overlap, comm );
183 for(
int level = 0; level < ioData.maxLevel; ++level )
185 if( level <
int( ioData.refinements.size() ) )
186 grid->globalRefine( 1, ioData.refinements[ level ] );
188 grid->globalRefine( 1 );
193 static bool parallelAnd (
const Communication &comm,
bool condition )
195 int result( condition );
196 result = comm.sum( result );
197 return (result == comm.size());
detected_or_fallback_t< DeprecatedCollectiveCommunication_t, Communication_t, typename GridFamily::Traits > Communication
A type that is a model of Dune::Communication. It provides a portable way for communication on the se...
Definition: grid.hh:525
A few common exception classes.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
Dune namespace.
Definition: alignedallocator.hh:13
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
Various macros to work with Dune module version numbers.