dune-spgrid  2.7
fileio.hh
Go to the documentation of this file.
1 #ifndef DUNE_SPGRID_FILEIO_HH
2 #define DUNE_SPGRID_FILEIO_HH
3 
4 #include <fstream>
5 #include <sstream>
6 #include <vector>
7 
8 #include <dune/common/fvector.hh>
9 #include <dune/common/exceptions.hh>
10 
11 #include <dune/grid/spgrid/cube.hh>
15 
16 namespace Dune
17 {
18 
19  // SPGridIOData
20  // ------------
21 
22  template< class ctype, int dim, template< int > class Ref >
23  struct SPGridIOData
24  {
27  typedef typename Cube::GlobalVector GlobalVector;
29  typedef Ref< dim > Refinement;
30  typedef typename Refinement::Policy RefinementPolicy;
31 
32  ctype time;
33  std::vector< Cube > cubes;
38  int maxLevel;
39  std::vector< RefinementPolicy > refinements;
40 
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 );
45 
46  private:
47  static std::pair< unsigned int, unsigned int > version ()
48  {
49  return std::pair< unsigned int, unsigned int >( DUNE_SPGRID_VERSION_MAJOR, DUNE_SPGRID_VERSION_MINOR );
50  }
51 
52  static std::string readLine ( std::istream &stream, unsigned int *count = 0 );
53  };
54 
55 
56 
57  // Implementation of SPGridIOData
58  // ------------------------------
59 
60  template< class ctype, int dim, template< int > class Ref >
61  inline bool SPGridIOData< ctype, dim, Ref >::write ( std::ostream &stream ) const
62  {
63  // write header
64  stream << "SPGrid";
65  stream << " dimension=" << dim;
66  stream << " version=" << version().first << "." << version().second;
67  stream << std::endl << std::endl;
68 
69  // write generic information
70  stream << "time " << time << std::endl;
71  stream << std::endl;
72 
73  // write domain information
74  stream << "domain";
75  for( typename std::vector< Cube >::const_iterator it = cubes.begin(); it != cubes.end(); ++it )
76  stream << " " << *it;
77  stream << std::endl;
78 
79  stream << "periodic";
80  for( int i = 0; i < dim; ++i )
81  {
82  if( topology.periodic( i ) )
83  stream << " " << i;
84  }
85  stream << std::endl << std::endl;
86 
87  // write discretization information
88  stream << "cells " << cells << std::endl;
89  stream << "partitions " << partitions << std::endl;
90  stream << "overlap " << overlap << std::endl;
91  stream << std::endl;
92 
93  // write refinement information
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 ];
99  stream << std::endl;
100  return bool( stream );
101  }
102 
103 
104  template< class ctype, int dim, template< int > class Ref >
105  inline bool SPGridIOData< ctype, dim, Ref >::write ( const std::string &filename ) const
106  {
107  std::ofstream stream( filename.c_str() );
108  return (stream ? write( stream ) : false);
109  }
110 
111 
112  template< class ctype, int dim, template< int > class Ref >
113  inline bool
114  SPGridIOData< ctype, dim, Ref >::read ( std::istream &stream, const std::string &info )
115  {
116  unsigned int lineNr = 0;
117  std::string line = readLine( stream, &lineNr );
118  std::istringstream lineIn( line );
119  lineIn >> match( std::string( "SPGrid" ) );
120  if( lineIn.fail() )
121  {
122  std::cerr << info << "[ " << lineNr << " ]: 'SPGrid' expected." << std::endl;
123  return false;
124  }
125 
126  int fdim = -1;
127 
128  while( isGood( lineIn ) )
129  {
130  std::string tag;
131  lineIn >> tag;
132 
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" )
138  {
139  // ensure that the check passes on read failure
140  std::pair< unsigned int, unsigned int > fileVersion;
141  valueIn >> fileVersion.first >> match( '.' ) >> fileVersion.second;
142  if( fileVersion > version() )
143  {
144  std::cerr << info << "[ " << lineNr << " ]: File was created by newer version of SPGrid." << std::endl;
145  return false;
146  }
147  }
148  else if( key == "dimension" )
149  valueIn >> fdim;
150  else
151  {
152  std::cerr << info << "[ " << lineNr << " ]: Invalid tag: '" << key << "'." << std::endl;
153  return false;
154  }
155  if( !valueIn )
156  {
157  std::cerr << info << "[ " << lineNr << " ]: Invalid value for tag '" << key << "'." << std::endl;
158  return false;
159  }
160  }
161 
162  if( fdim != dim )
163  {
164  std::cerr << info << "[ " << lineNr << " ]: File has wrong grid dimension." << std::endl;
165  return false;
166  }
167 
168  partitions = 1;
169  overlap = MultiIndex::zero();
170  time = ctype( 0 );
171  cubes.clear();
172 
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;
179 
180  while( true )
181  {
182  std::string line = readLine( stream, &lineNr );
183  if( line.empty() )
184  break;
185  std::istringstream lineIn( line );
186 
187  std::string cmd;
188  lineIn >> cmd;
189 
190  if( cmd == "time" )
191  lineIn >> time;
192  else if ( cmd == "domain" )
193  {
194  while( isGood( lineIn ) )
195  {
196  Cube cube;
197  lineIn >> cube;
198  if( lineIn )
199  cubes.push_back( cube );
200  }
201  if( lineIn )
202  flags |= flagDomain;
203  }
204  else if( cmd == "periodic" )
205  {
206  int periodic = 0;
207  while( isGood( lineIn ) )
208  {
209  int axis;
210  lineIn >> axis;
211  if( (axis < 0) || (axis >= dim) )
212  {
213  std::cerr << info << "[ " << lineNr << " ]: Invalid periodic axis: " << axis << "." << std::endl;
214  return false;
215  }
216  periodic |= (1 << axis);
217  }
218  topology = Topology( periodic );
219  }
220  else if( cmd == "cells" )
221  {
222  lineIn >> cells;
223  if( lineIn )
224  flags |= flagCells;
225  }
226  else if( cmd == "partitions" )
227  {
228  lineIn >> partitions;
229  }
230  else if( cmd == "overlap" )
231  lineIn >> overlap;
232  else if( cmd == "maxLevel" )
233  {
234  lineIn >> maxLevel;
235  flags |= flagMaxLevel;
236  }
237  else if( cmd == "refinement" )
238  {
239  lineIn >> match( Refinement::type() );
240  flags |= flagRefinement;
241  }
242  else if( cmd == "refinements" )
243  {
244  while( isGood( lineIn ) )
245  {
246  RefinementPolicy policy;
247  lineIn >> policy;
248  refinements.push_back( policy );
249  }
250  }
251  else
252  {
253  std::cerr << info << "[ " << lineNr << " ]: Invalid statement: '" << cmd << "'." << std::endl;
254  return false;
255  }
256  if( lineIn.fail() )
257  {
258  std::cerr << info << "[ " << lineNr << " ]: Invalid arguments for '" << cmd << "'." << std::endl;
259  return false;
260  }
261  }
262 
263  if( flags != flagAll )
264  {
265  std::cerr << info << ": File misses required field." << std::endl;
266  return false;
267  }
268  return true;
269  }
270 
271 
272  template< class ctype, int dim, template< int > class Ref >
273  inline bool SPGridIOData< ctype, dim, Ref >::read ( const std::string &filename )
274  {
275  std::ifstream stream( filename.c_str() );
276  return (stream ? read( stream, filename ) : false);
277  }
278 
279 
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 )
282  {
283  std::string line;
284  while( line.empty() && !stream.eof() )
285  {
286  std::getline( stream, line );
287  if( count != 0 )
288  ++(*count);
289 
290  // remove leading white space
291  const size_t first = line.find_first_not_of( " \t" );
292  if( first != std::string::npos )
293  line = line.substr( first );
294 
295  // remove trailing comments
296  line = line.substr( 0, line.find_first_of( '#' ) );
297  }
298  return line;
299  }
300 
301 } // namespace Dune
302 
303 #endif // #ifndef DUNE_SPGRID_FILEIO_HH
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
Definition: cube.hh:16
FieldVector< ctype, dimension > GlobalVector
type of global vectors, i.e., vectors within the domain
Definition: cube.hh:27
Definition: fileio.hh:24
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