dune-fem  2.4.1-rc
iotuple.hh
Go to the documentation of this file.
1 #ifndef DUNE_FEM_INPUTOUTPUTTUPLES_HH
2 #define DUNE_FEM_INPUTOUTPUTTUPLES_HH
3 
4 //- system includes
5 #include <string>
6 #include <tuple>
7 
8 //- Dune includes
9 #include <dune/common/forloop.hh>
10 #include <dune/common/typetraits.hh>
11 #include <dune/common/tuples.hh>
12 
13 //- Dune grid includes
14 #include <dune/grid/common/backuprestore.hh>
15 
16 //- Dune fem includes
20 #include <dune/fem/io/parameter.hh>
21 #include <dune/grid/common/backuprestore.hh>
22 
23 namespace Dune
24 {
25 
26  namespace Fem
27  {
28 
29  // IOTupleBase
30  // -----------
31 
32  struct IOTupleBase
33  {
34  // take binary stream types from PersistenceManager (overload with define)
38 
39  // return path/ prefix name as one string
40  static std::string pathAndName(const std::string& path, const std::string& name, const std::string& suffix)
41  {
42  std::string comboname;
43  if (path != "")
44  comboname += path;
45  else
46  comboname += ".";
47 
48  comboname += "/";
49  comboname += name;
50  comboname += suffix;
51  return comboname;
52  }
53 
54  // return grids name
55  static std::string gridName(const std::string& path, const std::string& name)
56  {
57  return pathAndName(path,name,"_grid");
58  }
59 
60  // return datas name
61  static std::string dataName(const std::string& path, const std::string& name)
62  {
63  return pathAndName(path,name,"_data");
64  }
65 
66  // return name with rank
67  static std::string rankName(const std::string& path, const std::string& name,
68  const int rank, const int size )
69  {
70  std::stringstream rankStr;
71  const int number = ( singleBackupRestoreFile ) ? size : rank ;
72  // add rank if output/input is with different files
73  rankStr << "." << number ;
74  return pathAndName(path,name,rankStr.str());
75  }
76 
77  template < class GridType >
78  static void
79  restoreGrid(GridType *&grid,
80  InStreamType& inStream,
81  const std::string& filename )
82  {
83  try
84  {
85  // get standard istream
86  std::istream& stream = inStream.stream();
87  // restore grid from stream (grid implementation has to take care of byte order)
88  grid = BackupRestoreFacility< GridType > :: restore( stream );
89  }
90  catch ( NotImplemented )
91  {
92  // write grid to file with filename + extension
93  grid = BackupRestoreFacility< GridType > :: restore( filename );
94  }
95 
96  if( grid == 0 )
97  DUNE_THROW(IOError,"Unable to restore grid" );
98  }
99 
100  template <class GridType>
101  static void
102  restoreDofManager ( const GridType& grid,
103  InStreamType& inStream )
104  {
105  // type of DofManager
106  typedef DofManager<GridType> DofManagerType;
107 
108  // read DofManager's index sets
109  DofManagerType& dm = DofManagerType :: instance ( grid );
110 
111  // read data
112  dm.read( inStream );
113 
114  // resize data
115  dm.resizeForRestrict();
116  }
117 
119  template < class GridType >
120  static
121  void writeGrid( const GridType& grid,
122  OutStreamType& outStream,
123  const std::string& filename )
124  {
125  try
126  {
127  // get standard ostream
128  std::ostream& stream = outStream.stream();
129  // write grid to stream (grid implementation has to take care of byte order)
130  BackupRestoreFacility< GridType > :: backup( grid, stream );
131  }
132  catch ( NotImplemented )
133  {
134  // write grid to file with filename + extension
135  BackupRestoreFacility< GridType > :: backup( grid, filename );
136  }
137 
138  // type of DofManager
139  typedef DofManager< GridType > DofManagerType;
140 
141  // get DofManager reference
142  DofManagerType& dm = DofManagerType :: instance ( grid );
143 
144  // write DofManager's index sets
145  dm.write( outStream );
146  }
147  };
148 
149 
150 
151  // IOTuple
152  // -------
153 
154  template< class Tuple >
155  class IOTuple
156  : public IOTupleBase
157  {
158  template< int N > struct CreateData;
159  template< int N > struct RestoreStream;
160  template< int N > struct OutputStream;
161  template< int N > struct AddToDisplay;
162  template< int N > struct AddToDisplayOrRemove;
163  template< int N > struct RemoveData;
164 
165  public:
166  static const int length = tuple_size< Tuple >::value;
167 
168  public:
169  typedef Tuple ReturnType ;
170 
171  template < class GridType >
172  static Tuple *input ( GridType *&grid,
173  double& time,
174  const int rank,
175  const int size,
176  const std::string &path,
177  const std::string &name )
178  {
179  // true if grid has to be read
180  const bool newGrid = (grid == 0);
181  assert( newGrid );
182 
183  // get filename
184  std::string filename = rankName( path, name, rank, size );
185 
186  if( Parameter :: verbose () )
187  std::cout << "IOTuple: Reading data from " << filename << std::endl;
188 
189  // create binary stream
190  InStreamType& inStream = *(Fem :: StreamFactory< InStreamType > :: create( filename, rank ));
191 
192  if( newGrid )
193  {
194  // create and read grid
195  IOTupleBase::restoreGrid( grid, inStream, filename );
196  }
197 
198  // create all data
199  Tuple *ret = new Tuple;
200  ForLoop< CreateData, 0, length-1 >::apply( *grid, *ret );
201 
202  if( newGrid )
203  {
204  // now read dofmanager and index sets
205  IOTupleBase::restoreDofManager( *grid, inStream );
206  }
207 
208  // get simulation time of data
209  inStream >> time ;
210 
211  // now read all data
212  ForLoop< RestoreStream, 0, length-1 >::apply( inStream, *ret );
213 
214  if( Parameter :: verbose() )
215  std::cout << " FINISHED!" << std::endl;
216 
217  // delete stream which was created by the StreamFactory
218  delete &inStream;
219 
220  return ret;
221  }
222 
224  template< class GridType >
225  static void restoreData ( Tuple &data,
226  const GridType &grid,
227  const std::string &path,
228  const std::string &name )
229  {
230  // get filename
231  std::string filename =
232  rankName( path, name, grid.comm().rank(), grid.comm().size() );
233 
234  // create binary stream
235  InStreamType inStream( filename );
236 
237  // read all data now
238  ForLoop< RestoreStream, 0, length-1 >::apply( inStream, data );
239  }
240 
242  template< class GridType >
243  static void output ( const GridType &grid,
244  const double time,
245  const std::string &path,
246  const std::string &name,
247  const Tuple &tuple )
248  {
249  // get filename
250  std::string filename =
251  rankName( path, name, grid.comm().rank(), grid.comm().size() );
252 
253  // create binary stream
255 
256  // write grid, either to binaryStream or with given filename
257  writeGrid( grid, outStream, filename );
258 
259  // save simulation time of data
260  outStream << time;
261 
262  // write data to stream
263  ForLoop< OutputStream, 0, length-1 >::apply( outStream, tuple );
264 
265  // delete stream created by StreamFactory
266  delete &outStream;
267  }
268 
269  template< class Disp, class DINFO >
270  static void addToDisplay ( Disp &disp, const DINFO *dinf, double time, Tuple &tuple )
271  {
272  ForLoop< AddToDisplay, 0, length-1 >::apply( disp, dinf, time, tuple );
273  }
274 
275  template< class Disp, class DINFO >
276  static void addToDisplayOrRemove ( Disp &disp, const DINFO *dinf, double time, Tuple &tuple )
277  {
278  ForLoop< AddToDisplayOrRemove, 0, length-1 >::apply( disp, dinf, time, tuple );
279  }
280 
281  template< class Disp >
282  static void addToDisplay ( Disp &disp, Tuple &tuple )
283  {
284  ForLoop< AddToDisplay, 0, length-1 >::apply( disp, tuple );
285  }
286 
287  static void removeData ( Tuple &tuple )
288  {
289  ForLoop< RemoveData, 0, length-1 >::apply( tuple );
290  }
291  };
292 
293 
294  template< class Tuple >
295  template< int N >
296  struct IOTuple< Tuple >::CreateData
297  {
298  typedef typename TypeTraits< typename tuple_element< N, Tuple >::type >::PointeeType DiscreteFunction;
299  typedef typename DiscreteFunction::DiscreteFunctionSpaceType DiscreteFunctionSpace;
300  typedef typename DiscreteFunctionSpace::GridPartType GridPart;
301 
302  template< class Grid >
303  static void apply ( Grid &grid, Tuple &tuple )
304  {
305  GridPart *gridPart = new GridPart( grid );
306  DiscreteFunctionSpace *space = new DiscreteFunctionSpace( *gridPart );
307  std::get< N >( tuple ) = new DiscreteFunction( "", *space );
308  }
309  };
310 
311  template< class Tuple >
312  template< int N >
313  struct IOTuple< Tuple >::RestoreStream
314  {
315  typedef typename TypeTraits< typename tuple_element< N, Tuple >::type >::PointeeType DiscreteFunction;
316 
317  template< class StreamTraits >
318  static void apply ( InStreamInterface< StreamTraits > &inStream, Tuple &tuple )
319  {
320  DiscreteFunction *df = std::get< N >( tuple );
321  bool readDF = false ;
322  inStream >> readDF ;
323  // if discrete function was written, we can read it
324  if( readDF )
325  {
326  if( df )
327  df->read( inStream );
328  else
329  DUNE_THROW(InvalidStateException,"no discrete function on input");
330  }
331  }
332  };
333 
334  template< class Tuple >
335  template< int N >
336  struct IOTuple< Tuple >::OutputStream
337  {
338  typedef typename TypeTraits< typename tuple_element< N, Tuple >::type >::PointeeType DiscreteFunction;
339 
341  template< class StreamTraits >
342  static void apply ( OutStreamInterface< StreamTraits > &outStream, const Tuple &tuple )
343  {
344  const DiscreteFunction *df = std::get< N >( tuple );
345  const bool writeDF = ( df != 0 );
346 
347  // write flag whether discrete function was written, for later restore
348  outStream << writeDF ;
349 
350  // if pointer is valid: write function to stream
351  if( writeDF )
352  {
353  df->write( outStream );
354  }
355  }
356  };
357 
358  template< class Tuple >
359  template< int N >
360  struct IOTuple< Tuple >::AddToDisplay
361  {
362  // revert tuple order to reverse deletion to creation
363  static const int pos = tuple_size< Tuple >::value - 1 - N;
364 
365  typedef typename TypeTraits< typename tuple_element< pos, Tuple >::type >::PointeeType DiscreteFunction;
366 
367  template< class Disp, class DINFO >
368  static void apply ( Disp &disp, const DINFO *&dinf, const double &time, Tuple &tuple )
369  {
370  DiscreteFunction *df = std::get< pos >( tuple );
371  if( df )
372  {
373  assert( dinf->comp );
374  std::cout << "adding to display " << dinf->name << std::endl;
375  disp.addData( *df, dinf, time );
376  }
377  }
378 
379  template< class Disp >
380  static void apply ( Disp &disp, Tuple &tuple )
381  {
382  DiscreteFunction *df = std::get< pos >( tuple );
383  if( df )
384  {
385  disp.addData( *df );
386  }
387  }
388  };
389 
390 
391  template< class Tuple >
392  template< int N >
393  struct IOTuple< Tuple >::AddToDisplayOrRemove
394  {
395  template< class Disp, class DINFO >
396  static void apply ( Disp &disp, const DINFO *&dinf, const double &time, Tuple &tuple )
397  {
398  if( dinf->comp )
399  AddToDisplay< N >::apply( disp, dinf, time, tuple );
400  else
401  // RemoveData reverts N itself
402  RemoveData< N >::apply( tuple );
403  }
404  };
405 
406 
407  template< class Tuple >
408  template< int N >
409  struct IOTuple< Tuple >::RemoveData
410  {
411  // revert tuple order to reverse deletion to creation
412  static const int pos = tuple_size< Tuple >::value - 1 - N;
413  typedef typename TypeTraits< typename tuple_element< pos, Tuple >::type >::PointeeType DiscreteFunction;
414  typedef typename DiscreteFunction::DiscreteFunctionSpaceType DiscreteFunctionSpace;
415  typedef typename DiscreteFunctionSpace::GridPartType GridPart;
416 
417  static void apply ( Tuple &tuple )
418  {
419  DiscreteFunction *&df = std::get< pos >( tuple );
420  if( df )
421  {
422  const DiscreteFunctionSpace *space = &(df->space());
423  const GridPart *gridPart = &(space->gridPart());
424 
425  delete df;
426  delete space;
427  delete gridPart;
428  df = 0;
429  }
430  }
431  };
432 
433 
434 
435  // IOTuple for empty tuple
436  // -----------------------
437 
438  template<>
439  class IOTuple< tuple<> >
440  : public IOTupleBase
441  {
442  typedef tuple<> Tuple;
443 
444  public:
445  static const int length = 0;
446 
447  template <class DataIO,class GridType>
448  static Tuple *input ( DataIO &dataio, GridType *&grid, double &t, int n,
449  const std::string &path, const std::string &name )
450  {
451  return new Tuple();
452  }
453 
455  template< class GridType >
456  static void restoreData ( Tuple &data,
457  const GridType &grid,
458  const std::string &path,
459  const std::string &name )
460  {
461  // nothing to do here
462  }
463 
465  template< class GridType >
466  static void output ( const GridType &grid,
467  const double time,
468  const std::string &path,
469  const std::string &name,
470  const Tuple &tuple )
471  {
472  // get filename
473  std::string filename =
474  rankName( path, name, grid.comm().rank(), grid.comm().size() );
475 
476  // create binary stream
477  OutStreamType outStream( filename );
478 
479  // write grid, either to binaryStream or with given filename
480  writeGrid( grid, outStream, filename );
481 
482  // save simulation time of data
483  outStream << time ;
484  }
485 
486  template< class Disp, class DINFO >
487  static void addToDisplay ( Disp &disp, const DINFO *dinf, double time, Tuple &tuple )
488  {}
489 
490  template< class Disp, class DINFO >
491  static void addToDisplayOrRemove ( Disp &disp, const DINFO *dinf, double time, Tuple &tuple )
492  {}
493 
494  template< class Disp >
495  static void addToDisplay ( Disp &disp, Tuple &tuple )
496  {}
497 
498  static void removeData ( Tuple &tuple )
499  {}
500  };
501 
502  } // namespace Fem
503 
504 } // namespace Dune
505 
506 #endif // #ifndef DUNE_FEM_INPUTOUTPUTTUPLES_HH
static void addToDisplay(Disp &disp, Tuple &tuple)
Definition: iotuple.hh:495
static void removeData(Tuple &tuple)
Definition: iotuple.hh:498
static void restoreData(Tuple &data, const GridType &grid, const std::string &path, const std::string &name)
restore all data in tupel
Definition: iotuple.hh:456
static void addToDisplayOrRemove(Disp &disp, const DINFO *dinf, double time, Tuple &tuple)
Definition: iotuple.hh:491
static void writeGrid(const GridType &grid, OutStreamType &outStream, const std::string &filename)
write grid and data to given directory
Definition: iotuple.hh:121
static std::string rankName(const std::string &path, const std::string &name, const int rank, const int size)
Definition: iotuple.hh:67
static Tuple * input(GridType *&grid, double &time, const int rank, const int size, const std::string &path, const std::string &name)
Definition: iotuple.hh:172
static void addToDisplay(Disp &disp, Tuple &tuple)
Definition: iotuple.hh:282
std::ostream & stream()
Definition: standardstreams.hh:85
PersistenceManager::RestoreStreamType InStreamType
Definition: iotuple.hh:36
Definition: datacollector.hh:45
static void output(const GridType &grid, const double time, const std::string &path, const std::string &name, const Tuple &tuple)
write grid and data to given directory
Definition: iotuple.hh:466
static StreamImpl * create(const std::string &filename, const int rank=MPIManager::rank(), const MPICommunicatorType &mpiComm=MPIHelper::getCommunicator())
return pointer to stream object created by new.
Definition: streams.hh:369
constructor
Definition: binarystreams.hh:15
static std::string gridName(const std::string &path, const std::string &name)
Definition: iotuple.hh:55
static void addToDisplayOrRemove(Disp &disp, const DINFO *dinf, double time, Tuple &tuple)
Definition: iotuple.hh:276
Definition: coordinate.hh:4
Definition: iotuple.hh:155
abstract interface for an input stream
Definition: streams.hh:177
static Tuple * input(DataIO &dataio, GridType *&grid, double &t, int n, const std::string &path, const std::string &name)
Definition: iotuple.hh:448
std::istream & stream()
Definition: standardstreams.hh:220
static const bool singleBackupRestoreFile
Definition: iotuple.hh:37
PersistenceManager::BackupStreamType OutStreamType
Definition: iotuple.hh:35
std::string path
Definition: readioparams.cc:155
static void removeData(Tuple &tuple)
Definition: iotuple.hh:287
Tuple ReturnType
Definition: iotuple.hh:169
static bool verbose()
obtain the cached value for fem.verbose
Definition: io/parameter.hh:444
static void restoreData(Tuple &data, const GridType &grid, const std::string &path, const std::string &name)
restore all data in tupel
Definition: iotuple.hh:225
static void output(const GridType &grid, const double time, const std::string &path, const std::string &name, const Tuple &tuple)
write grid and data to given directory
Definition: iotuple.hh:243
static const bool singleBackupRestoreFile
Definition: persistencemanager.hh:152
static void restoreDofManager(const GridType &grid, InStreamType &inStream)
Definition: iotuple.hh:102
discrete function space
Definition: iotuple.hh:32
constructor
Definition: binarystreams.hh:56
static std::string pathAndName(const std::string &path, const std::string &name, const std::string &suffix)
Definition: iotuple.hh:40
static void addToDisplay(Disp &disp, const DINFO *dinf, double time, Tuple &tuple)
Definition: iotuple.hh:270
static void addToDisplay(Disp &disp, const DINFO *dinf, double time, Tuple &tuple)
Definition: iotuple.hh:487
static std::string dataName(const std::string &path, const std::string &name)
Definition: iotuple.hh:61
static void restoreGrid(GridType *&grid, InStreamType &inStream, const std::string &filename)
Definition: iotuple.hh:79
abstract interface for an output stream
Definition: streams.hh:44