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>
14#include <dune/fem/io/file/persistencemanager.hh>
15#include <dune/fem/io/file/iointerface.hh>
16#include <dune/fem/io/parameter.hh>
17#include <dune/fem/space/common/dofmanager.hh>
31 typedef PersistenceManager :: BackupStreamType OutStreamType ;
32 typedef PersistenceManager :: RestoreStreamType InStreamType ;
33 static const bool singleBackupRestoreFile = PersistenceManager :: singleBackupRestoreFile;
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)
53 return pathAndName(path,name,
"_grid");
57 static std::string dataName(
const std::string& path,
const std::string& name)
59 return pathAndName(path,name,
"_data");
63 static std::string rankName(
const std::string& path,
const std::string& name,
64 const int rank,
const int size )
66 std::stringstream rankStr;
67 const int number = ( singleBackupRestoreFile ) ?
size : rank ;
69 rankStr <<
"." << number ;
70 return pathAndName(path,name,rankStr.str());
73 template <
class Gr
idType >
75 restoreGrid(GridType *&grid,
76 InStreamType& inStream,
77 const std::string& filename )
82 std::istream& stream = inStream.stream();
86 catch ( NotImplemented )
96 template <
class Gr
idType>
98 restoreDofManager (
const GridType& grid,
99 InStreamType& inStream )
102 typedef DofManager<GridType> DofManagerType;
105 DofManagerType& dm = DofManagerType :: instance ( grid );
111 dm.resizeForRestrict();
115 template <
class Gr
idType >
117 void writeGrid(
const GridType& grid,
118 OutStreamType& outStream,
119 const std::string& filename )
124 std::ostream& stream = outStream.stream();
128 catch (
const NotImplemented& )
135 typedef DofManager< GridType > DofManagerType;
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;
164 typedef Tuple ReturnType ;
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 );
182 std::cout <<
"IOTuple: Reading data from " << filename << std::endl;
185 InStreamType& inStream = *(Fem :: StreamFactory< InStreamType > :: create( filename, rank ));
190 IOTupleBase::restoreGrid( grid, inStream, filename );
194 Tuple *ret =
new Tuple;
196 [&](
auto i ) { CreateData<i>::apply( *grid, *ret ); });
201 IOTupleBase::restoreDofManager( *grid, inStream );
209 [&](
auto i ) { RestoreStream<i>::apply( inStream, *ret ); });
212 std::cout <<
" FINISHED!" << std::endl;
221 template<
class Gr
idType >
222 static void restoreData ( Tuple &data,
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() );
232 InStreamType inStream( filename );
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 ));
255 writeGrid( grid, outStream, 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 >
276 static void addToDisplayOrRemove ( Disp &disp,
const DINFO *dinf,
double time, Tuple &tuple )
279 [&](
auto i ) { AddToDisplayOrRemove<i>::apply( disp, dinf, time, tuple ); });
282 template<
class Disp >
283 static void addToDisplay ( Disp &disp, Tuple &tuple )
286 [&](
auto i ) { AddToDisplay<i>::apply( disp, tuple ); });
289 static void removeData ( Tuple &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());
442 class IOTuple<
std::tuple<> >
445 typedef std::tuple<> Tuple;
448 static const int length = 0;
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 >
459 static void restoreData ( Tuple &data,
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() );
480 OutStreamType outStream( filename );
483 writeGrid( grid, outStream, filename );
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 >
494 static void addToDisplayOrRemove ( Disp &disp,
const DINFO *dinf,
double time, Tuple &tuple )
497 template<
class Disp >
498 static void addToDisplay ( Disp &disp, Tuple &tuple )
501 static void removeData ( Tuple &tuple )
static bool verbose()
obtain the cached value for fem.verbose with default verbosity level 2
Definition: parameter.hh:466
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
constexpr void forEach(Range &&range, F &&f)
Range based for loop.
Definition: hybridutilities.hh:256
Dune namespace.
Definition: alignedallocator.hh:13
constexpr std::integral_constant< std::size_t, sizeof...(II)> size(std::integer_sequence< T, II... >)
Return the size of the sequence.
Definition: integersequence.hh:75
static Grid * restore(const std::string &filename)
read a hierarchic grid from disk
Definition: backuprestore.hh:78
static void backup(const Grid &grid, const std::string &filename)
write a hierarchic grid to disk
Definition: backuprestore.hh:51