1#ifndef DUNE_FEM_DATAWRITER_HH
2#define DUNE_FEM_DATAWRITER_HH
9#include <dune/fem/io/file/asciiparser.hh>
10#include <dune/fem/io/file/iointerface.hh>
11#include <dune/fem/io/file/iotuple.hh>
12#include <dune/fem/io/parameter.hh>
13#include <dune/fem/space/common/loadbalancer.hh>
14#include <dune/fem/gridpart/adaptiveleafgridpart.hh>
15#include <dune/fem/space/lagrange.hh>
16#include <dune/fem/function/adaptivefunction.hh>
17#include <dune/fem/quadrature/cachingquadrature.hh>
18#include <dune/fem/io/file/persistencemanager.hh>
19#include <dune/fem/io/file/dataoutput.hh>
20#include <dune/fem/misc/gridname.hh>
21#include <dune/grid/common/backuprestore.hh>
24#include <dune/grid/io/visual/grapedatadisplay.hh>
33 struct DataWriterParameters :
public DataOutputParameters
35 explicit DataWriterParameters ( std::string keyPrefix,
const ParameterReader ¶meter = Parameter::container() )
36 : DataOutputParameters( keyPrefix, parameter )
39 explicit DataWriterParameters (
const ParameterReader ¶meter = Parameter::container() )
40 : DataOutputParameters( parameter )
44 virtual std::string macroGridName (
const int dim)
const
50 virtual bool separateRankPath ()
const
63 template <
class GridImp,
87 mutable std::stringstream macroGrid_;
88 const bool separateRankPath_ ;
100 const DataWriterParameters& parameter = DataWriterParameters() )
101 :
BaseType( grid, data, parameter ),
102 separateRankPath_( parameter.separateRankPath() )
116 const DataWriterParameters& parameter = DataWriterParameters() )
117 :
BaseType( grid, data, tp, parameter ),
118 separateRankPath_( parameter.separateRankPath() )
132 writeMyBinaryData( sequenceStamp, writeStep_ , data_ );
135 template<
class OutputTuple >
136 std::string writeMyBinaryData (
const double sequenceStamp,
const int step,
137 OutputTuple &data )
const
142 typedef IOTuple< OutputTuple > IOTupleType ;
145 if( IOTupleType :: length > 0 )
148 IOTupleType :: output(
grid_, sequenceStamp, timeStepPath, datapref_, data );
175 DataWriterParameters( keyPrefix, Parameter::container() ),
180 DataWriterParameters( keyPrefix, Parameter::container() ),
193 return parameter().getValue<
int > ( keyPrefix_ +
"checkpointstep", 500 );
199 return parameter().getValue<
int > ( keyPrefix_ +
"checkpointmax", 2 );
205 return parameter().getValue< std::string > ( keyPrefix_ +
"checkpointfile",
"checkpoint" );
235 template<
class Gr
idImp >
239 typedef std::tuple<> DataImp;
250 const std::string name_;
255 name_( Fem :: gridName( grid_ ) )
260 const bool pushFront = true ;
262 PersistenceManager::insert( *
this, pushFront );
269 PersistenceManager::remove( *
this );
280 std::string gridBackup ;
283 std::stringstream gridBackupStream;
285 gridBackup = gridBackupStream.str();
287 PersistenceManager :: backupStream() << gridBackup;
289 catch (
const Dune :: NotImplemented& )
293 std::cerr <<
"GridPersistentObject::backup: cannot use stream backup." << std::endl;
298 std::string filename( PersistenceManager :: uniqueFileName( name_ ) );
301 catch (
const Dune :: NotImplemented& )
303 std::cerr <<
"ERROR: GridPersistentObject::backup: not possible!" << std::endl;
328 using BaseType :: separateRankPath_;
338 std::unique_ptr< PersistentGridObjectType > persistentGridObject_ ;
342 const int checkPointStep_;
343 const int maxCheckPointNumber_;
346 std::string checkPointFile_;
348 bool takeCareOfPersistenceManager_;
358 :
BaseType(grid, fakeData_, parameter)
361 , checkPointStep_( parameter.checkPointStep() )
362 , maxCheckPointNumber_( parameter.maxNumberOfCheckPoints() )
363 , myRank_( grid.comm().rank() )
364 , takeCareOfPersistenceManager_( true )
366 initialize( parameter );
379 :
BaseType(grid, fakeData_,tp,parameter)
381 , checkPointStep_( parameter.checkPointStep() )
382 , maxCheckPointNumber_( parameter.maxNumberOfCheckPoints() )
383 , myRank_( grid.comm().rank() )
384 , takeCareOfPersistenceManager_( true )
386 initialize( parameter );
393 outputFormat_ = BaseType :: binary;
395 grapeDisplay_ = false ;
397 checkPointFile_ = path_;
398 checkPointFile_ +=
"/";
399 checkPointFile_ += parameter.
prefix();
402 Parameter::write(
"parameter.log");
425 const char * checkFile,
426 const bool takeCareOfPersistenceManager =
true,
429 , persistentGridObject_( )
430 , checkPointStep_( 0 )
433 , takeCareOfPersistenceManager_( takeCareOfPersistenceManager )
436 outputFormat_ = BaseType :: binary;
438 grapeDisplay_ = false ;
446 checkPointFile_ = checkFile;
456 checkPointFile_ = path_;
457 checkPointFile_ +=
"/";
460 const bool warn = (myRank == 0);
463 std::cerr <<
"WARNING: Coudn't open file `" << checkFile <<
"' trying file `" << checkPointFile_ <<
"' instead!" << std::endl;
469 std::cerr <<
"ERROR: unable to open checkpoint file! \n";
493 const int givenRank = -1,
496 const int rank = ( givenRank < 0 ) ? MPIManager :: rank() : givenRank ;
500 const bool verbose = (rank == 0);
502 int checkPointNumber = 0;
504 if( ! readParameter(checkFile,
"LastCheckPoint",checkPointNumber, verbose,
false ) )
509 std::string checkPointFile =
path;
511 checkPointFile +=
"/";
515 std::cerr <<
"WARNING: Coudn't open file `" << checkFile <<
"' trying file `" << checkPointFile <<
"' instead!" << std::endl;
517 readParameter(checkPointFile,
"LastCheckPoint",checkPointNumber, verbose);
521 if( ! readParameter(checkFile,
"RecoverPath",
path,verbose) )
529 path = IOInterface::createRecoverPath(
533 PersistenceManager :: startRestore (
path );
539 std::string gridData;
540 PersistenceManager :: restoreStream() >> gridData;
543 std::stringstream gridStream( gridData );
545 gridData = std::string();
550 catch (
const Dune :: NotImplemented& )
554 std::cerr <<
"GridPersistentObject::restore: cannot use stream restore." << std::endl;
557 std::string name ( Fem :: gridName( *grid ) );
558 std::string filename( PersistenceManager :: uniqueFileName( name ) );
561 catch (
const Dune :: NotImplemented& )
563 std::cerr <<
"ERROR: GridPersistentObject::restore: not possible!" << std::endl;
585 const int myRank = ( rank < 0 ) ? grid.comm().rank() : rank ;
588 if( checkFile ==
"" )
607 std::string
path = IOInterface::createRecoverPath(
608 path_, myRank_ , datapref_, writeStep_, separateRankPath_ );
611 if( takeCareOfPersistenceManager_ )
614 PersistenceManager::restore(
path );
620 template<
class InputTuple >
621 void restoreUserData ( InputTuple &data )
638 restoreUserData( data_ );
643 virtual const char*
myClassName()
const {
return "CheckPointer"; }
652 return ( (checkPointStep_ > 0) && (((timestep % checkPointStep_) == 0) && timestep > 0) );
655 static void writeSingleCheckPoint(
const GridType& grid,
657 const bool storePersistenceManager,
661 checkPointer.writeBinaryData( time );
667 if( writeStep_ >= maxCheckPointNumber_ ) writeStep_ = 0;
670 std::string
path = this->writeMyBinaryData( time, writeStep_, data_ );
673 if( takeCareOfPersistenceManager_ )
676 PersistenceManager::backup(
path );
680 writeCheckPoint(
path, time,
693 if( readParameter(checkPointFile_,
"LastCheckPoint",writeStep_, verbose, warn ) )
695 std::string recoverPath;
697 if( ! readParameter(checkPointFile_,
"RecoverPath", recoverPath, verbose) )
703 int storedPersistentManager = 0;
704 if( readParameter(checkPointFile_,
"PersistenceManager", storedPersistentManager, verbose) )
706 takeCareOfPersistenceManager_ = ( storedPersistentManager > 0 );
718 void writeCheckPoint (
const std::string&
path,
720 const int savestep )
const
726 std::string checkpointstr ;
728 std::stringstream checkpoint;
729 checkpoint <<
"LastCheckPoint: " << savestep << std::endl;
730 checkpoint.precision( 16 );
731 checkpoint <<
"Time: " << std::scientific << time << std::endl;
732 checkpoint <<
"SaveCount: " << savestep << std::endl;
733 checkpoint <<
"PersistenceManager: " << takeCareOfPersistenceManager_ << std::endl;
734 checkpoint <<
"NumberProcessors: " <<
grid_.comm().size() << std::endl;
735 checkpoint <<
"# RecoverPath can be edited by hand if data has been moved!" << std::endl;
736 checkpoint <<
"RecoverPath: " << path_ << std::endl;
737 checkpointstr = checkpoint.str();
742 std::ofstream file (checkPointFile_.c_str());
745 file << checkpointstr;
751 std::string checkPointStepFile(
path );
752 checkPointStepFile +=
"/" + datapref_;
754 std::ofstream file ( checkPointStepFile.c_str() );
757 file << checkpointstr;
Implementation of the IOInterface. This class manages checkpointing.
Definition: datawriter.hh:238
static void restoreData(const GridType &grid, const std::string checkFile, const int rank=-1)
restores data, assumes that all objects have been created and inserted to PersistenceManager before t...
Definition: datawriter.hh:582
CheckPointer(const GridType &grid, const CheckPointerParameters ¶meter=CheckPointerParameters())
Constructor generating a checkpointer.
Definition: datawriter.hh:356
bool readCheckPoint(const bool warn=true)
read checkpoint file
Definition: datawriter.hh:688
bool willWrite(const TimeProviderBase &tp) const
returns true if data will be written on next write call
Definition: datawriter.hh:648
DataWriter< GridImp, DataImp > BaseType
type of base class
Definition: datawriter.hh:318
virtual const char * myClassName() const
print class name
Definition: datawriter.hh:643
GridImp GridType
used grid type
Definition: datawriter.hh:242
DataImp OutPutDataType
used data tuple
Definition: datawriter.hh:334
static GridType * restoreGrid(const std::string checkFile, const int givenRank=-1, const CheckPointerParameters ¶meter=CheckPointerParameters())
restore grid from previous runs
Definition: datawriter.hh:492
CheckPointer(const GridType &grid, const TimeProviderBase &tp, const CheckPointerParameters ¶meter=CheckPointerParameters())
Constructor generating a checkpointer.
Definition: datawriter.hh:376
CheckPointer(const GridType &grid, const int myRank, const char *checkFile, const bool takeCareOfPersistenceManager=true, const int writeStep=0)
Constructor generating a checkpointer to restore data.
Definition: datawriter.hh:423
CheckPointer< GridImp > ThisType
type of this class
Definition: datawriter.hh:331
std::string restorePersistentData()
restores data, assumes that all objects have been created before this method is called
Definition: datawriter.hh:604
virtual void writeBinaryData(const double time) const
write binary data
Definition: datawriter.hh:664
Implementation of the Dune::Fem::IOInterface. This class manages data output. Available output format...
Definition: dataoutput.hh:197
const std::string & path() const
return output path name
Definition: dataoutput.hh:413
GridImp GridType
type of grid used
Definition: dataoutput.hh:279
DataImp OutPutDataType
type of data tuple
Definition: dataoutput.hh:281
int writeStep() const
return write step
Definition: dataoutput.hh:419
virtual bool willWrite() const
returns true if data will be written on next write call
Definition: dataoutput.hh:350
const GridType & grid_
type of this class
Definition: dataoutput.hh:478
Implementation of the Dune::IOInterface. This class manages data output. Available output formats are...
Definition: datawriter.hh:66
DataWriter(const GridType &grid, OutPutDataType &data, const TimeProviderBase &tp, const DataWriterParameters ¶meter=DataWriterParameters())
Constructor creating data writer.
Definition: datawriter.hh:113
virtual void writeBinaryData(const double sequenceStamp) const
write binary data
Definition: datawriter.hh:130
virtual const char * myClassName() const
print class name
Definition: datawriter.hh:127
virtual ~DataWriter()
destructor
Definition: datawriter.hh:123
GridImp GridType
type of grid used
Definition: datawriter.hh:69
DataImp OutPutDataType
type of data tuple
Definition: datawriter.hh:71
DataWriter< GridImp, DataImp > ThisType
type of this class
Definition: datawriter.hh:74
DataWriter(const GridType &grid, OutPutDataType &data, const DataWriterParameters ¶meter=DataWriterParameters())
Constructor creating data writer.
Definition: datawriter.hh:98
Definition: dofmanager.hh:786
static std::string defaultGridKey(int dimension, bool check=true)
return FEM key for macro grid reading
Definition: iointerface.hh:181
static void createPath(const std::string &path)
create given path in combination with rank
Definition: iointerface.hh:232
static std::string readPath()
Definition: iointerface.hh:255
static bool verbose()
obtain the cached value for fem.verbose with default verbosity level 2
Definition: parameter.hh:466
base class for persistent objects
Definition: persistencemanager.hh:101
virtual void backup() const =0
backup persistent object
general base for time providers
Definition: timeprovider.hh:36
int timeStep() const
obtain number of the current time step
Definition: timeprovider.hh:103
Default exception if a function was called while the object is not in a valid state for that function...
Definition: exceptions.hh:281
static ThisType & instance(const GridType &grid)
obtain a reference to the DofManager for a given grid
Definition: dofmanager.hh:1251
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
Dune namespace.
Definition: alignedallocator.hh:13
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
local parameter collection for CheckPointer
Definition: datawriter.hh:169
virtual int outputformat() const
format of output (fem.io.outputformat)
Definition: datawriter.hh:220
virtual std::string checkPointPrefix() const
return default value for check point prefix
Definition: datawriter.hh:203
virtual int checkPointStep() const
return number of timestep to be passed until next checkpoint in written
Definition: datawriter.hh:191
virtual int maxNumberOfCheckPoints() const
maximal number of checkpoint stages written (default = 2)
Definition: datawriter.hh:197
virtual bool writeMode() const
writeMode, true when checkpointer is in backup mode
Definition: datawriter.hh:209
virtual std::string prefix() const
base of file name for data file (fem.io.datafileprefix)
Definition: datawriter.hh:185
virtual bool separateRankPath() const
return true if all data should be written to a spearate path per rank
Definition: datawriter.hh:215
call appropriate backup and restore methods on the grid class
Definition: datawriter.hh:248
virtual void restore()
restore grid
Definition: datawriter.hh:312
virtual void backup() const
backup grid
Definition: datawriter.hh:273
GridPersistentObject(const GridType &grid)
constructor storing grid
Definition: datawriter.hh:253
~GridPersistentObject()
destructor removing grid object
Definition: datawriter.hh:266