1 #ifndef DUNE_SPGRID_FILEIO_HH
2 #define DUNE_SPGRID_FILEIO_HH
8 #include <dune/common/fvector.hh>
9 #include <dune/common/exceptions.hh>
22 template<
class ctype,
int dim,
template<
int >
class Ref >
41 bool write ( std::ostream &stream )
const;
42 bool write (
const std::string &filename )
const;
43 bool read ( std::istream &stream,
const std::string &info =
"" );
44 bool read (
const std::string &filename );
47 static std::pair< unsigned int, unsigned int > version ()
49 return std::pair< unsigned int, unsigned int >( DUNE_SPGRID_VERSION_MAJOR, DUNE_SPGRID_VERSION_MINOR );
52 static std::string readLine ( std::istream &stream,
unsigned int *count = 0 );
60 template<
class ctype,
int dim,
template<
int >
class Ref >
65 stream <<
" dimension=" << dim;
66 stream <<
" version=" << version().first <<
"." << version().second;
67 stream << std::endl << std::endl;
70 stream <<
"time " << time << std::endl;
75 for(
typename std::vector< Cube >::const_iterator it = cubes.begin(); it != cubes.end(); ++it )
80 for(
int i = 0; i < dim; ++i )
82 if( topology.periodic( i ) )
85 stream << std::endl << std::endl;
88 stream <<
"cells " << cells << std::endl;
89 stream <<
"partitions " << partitions << std::endl;
90 stream <<
"overlap " << overlap << std::endl;
94 stream <<
"maxLevel " << maxLevel << std::endl;
95 stream <<
"refinement " << Refinement::type() << std::endl;
96 stream <<
"refinements";
97 for(
unsigned int i = 0; i < refinements.size(); ++i )
98 stream <<
" " << refinements[ i ];
100 return bool( stream );
104 template<
class ctype,
int dim,
template<
int >
class Ref >
107 std::ofstream stream( filename.c_str() );
108 return (stream ? write( stream ) :
false);
112 template<
class ctype,
int dim,
template<
int >
class Ref >
116 unsigned int lineNr = 0;
117 std::string line = readLine( stream, &lineNr );
118 std::istringstream lineIn( line );
119 lineIn >>
match( std::string(
"SPGrid" ) );
122 std::cerr << info <<
"[ " << lineNr <<
" ]: 'SPGrid' expected." << std::endl;
133 const size_t eq = tag.find(
'=' );
134 const std::string key = tag.substr( 0, eq );
135 const std::string value = (eq+1 < tag.size() ? tag.substr( eq+1 ) : std::string());
136 std::istringstream valueIn( value );
137 if( key ==
"version" )
140 std::pair< unsigned int, unsigned int > fileVersion;
141 valueIn >> fileVersion.first >>
match(
'.' ) >> fileVersion.second;
142 if( fileVersion > version() )
144 std::cerr << info <<
"[ " << lineNr <<
" ]: File was created by newer version of SPGrid." << std::endl;
148 else if( key ==
"dimension" )
152 std::cerr << info <<
"[ " << lineNr <<
" ]: Invalid tag: '" << key <<
"'." << std::endl;
157 std::cerr << info <<
"[ " << lineNr <<
" ]: Invalid value for tag '" << key <<
"'." << std::endl;
164 std::cerr << info <<
"[ " << lineNr <<
" ]: File has wrong grid dimension." << std::endl;
169 overlap = MultiIndex::zero();
173 const unsigned int flagDomain = 1;
174 const unsigned int flagCells = 2;
175 const unsigned int flagMaxLevel = 4;
176 const unsigned int flagRefinement = 8;
177 const unsigned int flagAll = 15;
178 unsigned int flags = 0;
182 std::string line = readLine( stream, &lineNr );
185 std::istringstream lineIn( line );
192 else if ( cmd ==
"domain" )
199 cubes.push_back( cube );
204 else if( cmd ==
"periodic" )
211 if( (axis < 0) || (axis >= dim) )
213 std::cerr << info <<
"[ " << lineNr <<
" ]: Invalid periodic axis: " << axis <<
"." << std::endl;
216 periodic |= (1 << axis);
220 else if( cmd ==
"cells" )
226 else if( cmd ==
"partitions" )
228 lineIn >> partitions;
230 else if( cmd ==
"overlap" )
232 else if( cmd ==
"maxLevel" )
235 flags |= flagMaxLevel;
237 else if( cmd ==
"refinement" )
239 lineIn >>
match( Refinement::type() );
240 flags |= flagRefinement;
242 else if( cmd ==
"refinements" )
248 refinements.push_back( policy );
253 std::cerr << info <<
"[ " << lineNr <<
" ]: Invalid statement: '" << cmd <<
"'." << std::endl;
258 std::cerr << info <<
"[ " << lineNr <<
" ]: Invalid arguments for '" << cmd <<
"'." << std::endl;
263 if( flags != flagAll )
265 std::cerr << info <<
": File misses required field." << std::endl;
272 template<
class ctype,
int dim,
template<
int >
class Ref >
275 std::ifstream stream( filename.c_str() );
276 return (stream ? read( stream, filename ) :
false);
280 template<
class ctype,
int dim,
template<
int >
class Ref >
284 while( line.empty() && !stream.eof() )
286 std::getline( stream, line );
291 const size_t first = line.find_first_not_of(
" \t" );
292 if( first != std::string::npos )
293 line = line.substr( first );
296 line = line.substr( 0, line.find_first_of(
'#' ) );
topology of a Cartesian grid
Definition: iostream.hh:7
bool isGood(std::basic_istream< char_type, traits > &in)
Definition: iostream.hh:69
iostream::Match< typename iostream::MatchTraits< T >::Type > match(const T &value)
Definition: iostream.hh:87
FieldVector< ctype, dimension > GlobalVector
type of global vectors, i.e., vectors within the domain
Definition: cube.hh:27
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
Cube::GlobalVector GlobalVector
Definition: fileio.hh:27
MultiIndex cells
Definition: fileio.hh:35
Refinement::Policy RefinementPolicy
Definition: fileio.hh:30
SPMultiIndex< dim > MultiIndex
Definition: fileio.hh:28
Ref< dim > Refinement
Definition: fileio.hh:29
Topology topology
Definition: fileio.hh:34
bool read(std::istream &stream, const std::string &info="")
Definition: fileio.hh:114
SPCube< ctype, dim > Cube
Definition: fileio.hh:26
std::vector< Cube > cubes
Definition: fileio.hh:33
SPTopology< dim > Topology
Definition: fileio.hh:25
bool write(std::ostream &stream) const
Definition: fileio.hh:61
multiindex
Definition: multiindex.hh:25
description of the grid's topology
Definition: topology.hh:31