Dune Core Modules (2.9.0)

backuprestore.hh
1#ifndef DUNE_SPGRID_BACKUPRESTORE_HH
2#define DUNE_SPGRID_BACKUPRESTORE_HH
3
6
7#include <dune/grid/common/backuprestore.hh>
8
9#include <dune/grid/spgrid/communication.hh>
10#include <dune/grid/spgrid/declaration.hh>
11#include <dune/grid/spgrid/fileio.hh>
12
13namespace Dune
14{
15
36 template< class ct, int dim, template< int > class Ref, class Comm >
37 struct BackupRestoreFacility< SPGrid< ct, dim, Ref, Comm > >
38 {
39 typedef SPGrid< ct, dim, Ref, Comm > Grid;
40
41 typedef typename Grid::Communication Communication;
42
49 static void backup ( const Grid &grid, const std::string &path, const std::string &fileprefix )
50 {
51 backup( path + "/" + fileprefix + ".spgrid" );
52 }
53
59 static void backup ( const Grid &grid, const std::string &filename )
60 {
61 SPGridIOData< ct, dim, Ref > ioData;
62 backup( grid, ioData );
63
64 int result = 0;
65 if( grid.comm().rank() == 0 )
66 {
67 std::ofstream stream( filename.c_str() );
68 if( stream )
69 result = int( ioData.write( stream ) );
70 }
71 grid.comm().broadcast( &result, 1, 0 );
72 if( !result )
73 DUNE_THROW( IOError, "Unable to write SPGrid to file '" + filename + "'." );
74 }
75
81 static void backup ( const Grid &grid, std::ostream &stream )
82 {
83 SPGridIOData< ct, dim, Ref > ioData;
84 backup( grid, ioData );
85
86 int result = 0;
87 if( grid.comm().rank() == 0 )
88 result = int( ioData.write( stream ) );
89 grid.comm().broadcast( &result, 1, 0 );
90 if( !result )
91 DUNE_THROW( IOError, "Unable to write SPGrid to stream." );
92 }
93
102 static Grid *restore ( const std::string &path, const std::string &fileprefix,
103 const Communication &comm = SPCommunicationTraits< Comm >::defaultComm() )
104 {
105 return restore( path + "/" + fileprefix + ".spgrid" );
106 }
107
115 static Grid *restore ( const std::string &filename,
116 const Communication &comm = SPCommunicationTraits< Comm >::defaultComm() )
117 {
118 std::ifstream stream( filename.c_str() );
119 if( !stream )
120 DUNE_THROW( IOError, "Unable to open file: " + filename );
121
122 Grid *grid = 0;
123 SPGridIOData< ct, dim, Ref > ioData;
124 if( ioData.read( stream, filename ) )
125 grid = restore( ioData, comm );
126
127 if( !parallelAnd( comm, grid ) )
128 {
129 delete grid;
130 DUNE_THROW( IOError, "Unable to read grid from file '" + filename + "'." );
131 }
132 return grid;
133 }
134
140 static Grid *restore ( std::istream &stream,
141 const Communication &comm = SPCommunicationTraits< Comm >::defaultComm() )
142 {
143 Grid *grid = 0;
144 SPGridIOData< ct, dim, Ref > ioData;
145 if( ioData.read( stream ) )
146 grid = restore( ioData, comm );
147
148 if( !parallelAnd( comm, grid ) )
149 {
150 delete grid;
151 DUNE_THROW( IOError, "Unable to read grid from stream." );
152 }
153 return grid;
154 }
155
156 private:
157 static void backup ( const Grid &grid, SPGridIOData< ct, dim, Ref > &ioData )
158 {
159 ioData.time = 0;
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();
169 }
170
171 static Grid *restore ( const SPGridIOData< ct, dim, Ref > &ioData,
172 const Communication &comm = SPCommunicationTraits< Comm >::defaultComm() )
173 {
174 if( ioData.partitions != comm.size() )
175 {
176 std::cerr << "Warning: Reading grid with different number of partitions,"
177 << " index sets will not coincide." << std::endl;
178 }
179
180 typename Grid::Domain domain( ioData.cubes, ioData.topology );
181 Grid *grid = new Grid( domain, ioData.cells, ioData.overlap, comm );
182
183 for( int level = 0; level < ioData.maxLevel; ++level )
184 {
185 if( level < int( ioData.refinements.size() ) )
186 grid->globalRefine( 1, ioData.refinements[ level ] );
187 else
188 grid->globalRefine( 1 );
189 }
190 return grid;
191 }
192
193 static bool parallelAnd ( const Communication &comm, bool condition )
194 {
195 int result( condition );
196 result = comm.sum( result );
197 return (result == comm.size());
198 }
199 };
200
201} // namespace Dune
202
203#endif // #ifndef DUNE_SPGRID_BACKUPRESTORE_HH
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.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Jul 15, 22:36, 2024)