1#ifndef DUNE_SPGRID_FILEIO_HH
2#define DUNE_SPGRID_FILEIO_HH
11#include <dune/grid/spgrid/cube.hh>
13#include <dune/grid/spgrid/multiindex.hh>
14#include <dune/grid/spgrid/refinement.hh>
22 template<
class ctype,
int dim,
template<
int >
class Ref >
25 typedef SPTopology< dim > Topology;
26 typedef SPCube< ctype, dim > Cube;
27 typedef typename Cube::GlobalVector GlobalVector;
28 typedef SPMultiIndex< dim > MultiIndex;
29 typedef Ref< dim > Refinement;
30 typedef typename Refinement::Policy RefinementPolicy;
33 std::vector< Cube > cubes;
39 std::vector< RefinementPolicy > refinements;
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 >
61 inline bool SPGridIOData< ctype, dim, Ref >::write ( std::ostream &stream )
const
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 >
105 inline bool SPGridIOData< ctype, dim, Ref >::write (
const std::string &filename )
const
107 std::ofstream stream( filename.c_str() );
108 return (stream ? write( stream ) :
false);
112 template<
class ctype,
int dim,
template<
int >
class Ref >
114 SPGridIOData< ctype, dim, Ref >::read ( std::istream &stream,
const std::string &info )
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;
128 while( isGood( lineIn ) )
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" )
194 while( isGood( lineIn ) )
199 cubes.push_back(
cube );
204 else if( cmd ==
"periodic" )
207 while( isGood( lineIn ) )
211 if( (axis < 0) || (axis >= dim) )
213 std::cerr << info <<
"[ " << lineNr <<
" ]: Invalid periodic axis: " << axis <<
"." << std::endl;
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" )
244 while( isGood( lineIn ) )
246 RefinementPolicy policy;
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 >
273 inline bool SPGridIOData< ctype, dim, Ref >::read (
const std::string &filename )
275 std::ifstream stream( filename.c_str() );
276 return (stream ? read( stream, filename ) :
false);
280 template<
class ctype,
int dim,
template<
int >
class Ref >
281 inline std::string SPGridIOData< ctype, dim, Ref >::readLine ( std::istream &stream,
unsigned int *count )
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 )
A few common exception classes.
topology of a Cartesian grid
Implements a vector constructed from a given type representing a field and a compile-time given size.
bool eq(const T &first, const T &second, typename EpsilonType< T >::Type epsilon)
test for equality using epsilon
Definition: float_cmp.cc:144
auto periodic(RawPreBasisIndicator &&rawPreBasisIndicator, PIS &&periodicIndexSet)
Create a pre-basis factory that can create a periodic pre-basis.
Definition: periodicbasis.hh:191
constexpr GeometryType line
GeometryType representing a line.
Definition: type.hh:512
Dune namespace.
Definition: alignedallocator.hh:13
@ cube
use only cube elements (i.e., quadrilaterals or hexahedra)
Definition: declaration.hh:19