1#ifndef DUNE_FEM_INPUTOUTPUTTUPLES_HH
2#define DUNE_FEM_INPUTOUTPUTTUPLES_HH
10#include <dune/common/hybridutilities.hh>
12#include <dune/grid/common/backuprestore.hh>
36 static std::string
pathAndName(
const std::string&
path,
const std::string& name,
const std::string& suffix)
38 std::string comboname;
51 static std::string
gridName(
const std::string&
path,
const std::string& name)
57 static std::string
dataName(
const std::string&
path,
const std::string& name)
63 static std::string
rankName(
const std::string&
path,
const std::string& name,
64 const int rank,
const int size )
66 std::stringstream rankStr;
69 rankStr <<
"." << number ;
73 template <
class Gr
idType >
77 const std::string& filename )
82 std::istream& stream = inStream.
stream();
84 grid = BackupRestoreFacility< GridType > :: restore( stream );
86 catch ( NotImplemented )
89 grid = BackupRestoreFacility< GridType > :: restore( filename );
93 DUNE_THROW(IOError,
"Unable to restore grid" );
96 template <
class Gr
idType>
105 DofManagerType& dm = DofManagerType :: instance ( grid );
111 dm.resizeForRestrict();
115 template <
class Gr
idType >
119 const std::string& filename )
124 std::ostream& stream = outStream.
stream();
126 BackupRestoreFacility< GridType > :: backup( grid, stream );
128 catch (
const NotImplemented& )
131 BackupRestoreFacility< GridType > :: backup( grid, filename );
138 DofManagerType& dm = DofManagerType :: instance ( grid );
141 dm.write( outStream );
150 template<
class Tuple >
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;
162 static const int length = std::tuple_size< Tuple >::value;
166 template <
class Gr
idType >
167 static Tuple *
input ( GridType *&grid,
171 const std::string &
path,
172 const std::string &name )
175 const bool newGrid = (grid == 0);
179 std::string filename =
rankName(
path, name, rank, size );
181 if( Parameter :: verbose () )
182 std::cout <<
"IOTuple: Reading data from " << filename << std::endl;
185 InStreamType& inStream = *(Fem :: StreamFactory< InStreamType > :: create( filename, rank ));
194 Tuple *ret =
new Tuple;
196 [&](
auto i ) { CreateData<i>::apply( *grid, *ret ); });
209 [&](
auto i ) { RestoreStream<i>::apply( inStream, *ret ); });
211 if( Parameter :: verbose() )
212 std::cout <<
" FINISHED!" << std::endl;
221 template<
class Gr
idType >
223 const GridType &grid,
224 const std::string &
path,
225 const std::string &name )
228 std::string filename =
229 rankName(
path, name, grid.comm().rank(), grid.comm().size() );
236 [&](
auto i ) { RestoreStream<i>::apply( inStream, data ); });
240 template<
class Gr
idType >
241 static void output (
const GridType &grid,
243 const std::string &
path,
244 const std::string &name,
248 std::string filename =
249 rankName(
path, name, grid.comm().rank(), grid.comm().size() );
252 OutStreamType& outStream = *(Fem :: StreamFactory< OutStreamType > :: create( filename ));
262 [&](
auto i ) { OutputStream<i>::apply( outStream, tuple ); });
268 template<
class Disp,
class DINFO >
269 static void addToDisplay ( Disp &disp,
const DINFO *dinf,
double time, Tuple &tuple )
272 [&](
auto i ) { AddToDisplay<i>::apply( disp, dinf, time, tuple ); });
275 template<
class Disp,
class DINFO >
279 [&](
auto i ) { AddToDisplayOrRemove<i>::apply( disp, dinf, time, tuple ); });
282 template<
class Disp >
286 [&](
auto i ) { AddToDisplay<i>::apply( disp, tuple ); });
292 [&](
auto i ) { RemoveData<i>::apply( tuple ); });
297 template<
class Tuple >
299 struct IOTuple< Tuple >::CreateData
301 typedef typename std::remove_pointer< typename std::tuple_element< N, Tuple >::type >::type DiscreteFunction;
303 typedef typename DiscreteFunctionSpace::GridPartType GridPart;
305 template<
class Gr
id >
306 static void apply ( Grid &grid, Tuple &tuple )
308 GridPart *gridPart =
new GridPart( grid );
310 std::get< N >( tuple ) =
new DiscreteFunction(
"", *space );
314 template<
class Tuple >
316 struct IOTuple< Tuple >::RestoreStream
318 typedef typename std::remove_pointer< typename std::tuple_element< N, Tuple >::type >::type DiscreteFunction;
320 template<
class StreamTraits >
321 static void apply ( InStreamInterface< StreamTraits > &inStream, Tuple &tuple )
323 DiscreteFunction *df = std::get< N >( tuple );
324 bool readDF = false ;
330 df->read( inStream );
332 DUNE_THROW(InvalidStateException,
"no discrete function on input");
337 template<
class Tuple >
339 struct IOTuple< Tuple >::OutputStream
341 typedef typename std::remove_pointer< typename std::tuple_element< N, Tuple >::type >::type DiscreteFunction;
344 template<
class StreamTraits >
345 static void apply ( OutStreamInterface< StreamTraits > &outStream,
const Tuple &tuple )
347 const DiscreteFunction *df = std::get< N >( tuple );
348 const bool writeDF = ( df != 0 );
351 outStream << writeDF ;
356 df->write( outStream );
361 template<
class Tuple >
363 struct IOTuple< Tuple >::AddToDisplay
366 static const int pos = std::tuple_size< Tuple >::value - 1 - N;
368 typedef typename std::remove_pointer< typename std::tuple_element< pos, Tuple >::type >::type DiscreteFunction;
370 template<
class Disp,
class DINFO >
371 static void apply ( Disp &disp,
const DINFO *&dinf,
const double &time, Tuple &tuple )
373 DiscreteFunction *df = std::get< pos >( tuple );
376 assert( dinf->comp );
377 std::cout <<
"adding to display " << dinf->name << std::endl;
378 disp.addData( *df, dinf, time );
382 template<
class Disp >
383 static void apply ( Disp &disp, Tuple &tuple )
385 DiscreteFunction *df = std::get< pos >( tuple );
394 template<
class Tuple >
396 struct IOTuple< Tuple >::AddToDisplayOrRemove
398 template<
class Disp,
class DINFO >
399 static void apply ( Disp &disp,
const DINFO *&dinf,
const double &time, Tuple &tuple )
402 AddToDisplay< N >::apply( disp, dinf, time, tuple );
405 RemoveData< N >::apply( tuple );
410 template<
class Tuple >
412 struct IOTuple< Tuple >::RemoveData
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;
418 typedef typename DiscreteFunctionSpace::GridPartType GridPart;
420 static void apply ( Tuple &tuple )
422 DiscreteFunction *&df = std::get< pos >( tuple );
426 const GridPart *gridPart = &(space->gridPart());
445 typedef std::tuple<> Tuple;
450 template <
class DataIO,
class Gr
idType>
451 static Tuple *
input ( DataIO &dataio, GridType *&grid,
double &t,
int n,
452 const std::string &
path,
const std::string &name )
458 template<
class Gr
idType >
460 const GridType &grid,
461 const std::string &
path,
462 const std::string &name )
468 template<
class Gr
idType >
469 static void output (
const GridType &grid,
471 const std::string &
path,
472 const std::string &name,
476 std::string filename =
477 rankName(
path, name, grid.comm().rank(), grid.comm().size() );
489 template<
class Disp,
class DINFO >
490 static void addToDisplay ( Disp &disp,
const DINFO *dinf,
double time, Tuple &tuple )
493 template<
class Disp,
class DINFO >
497 template<
class Disp >
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
PersistenceManager::BackupStreamType OutStreamType
Definition: iotuple.hh:31
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
PersistenceManager::RestoreStreamType InStreamType
Definition: iotuple.hh:32
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
static std::string gridName(const std::string &path, const std::string &name)
Definition: iotuple.hh:51
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 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 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 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
Definition: binarystreams.hh:16
Definition: binarystreams.hh:57
std::ostream & stream()
Definition: standardstreams.hh:85
std::istream & stream()
Definition: standardstreams.hh:220
Definition: dofmanager.hh:761