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