dune-fem  2.4.1-rc
timeprovider.hh
Go to the documentation of this file.
1 #ifndef DUNE_FEM_TIMEPROVIDER_HH
2 #define DUNE_FEM_TIMEPROVIDER_HH
3 
4 #include <cassert>
5 #include <limits>
6 #include <tuple>
7 
11 
13 
14 namespace Dune
15 {
16 
17  namespace Fem
18  {
19 
36  {
37  typedef TimeProviderBase ThisType;
38 
39  public:
40  inline TimeProviderBase ( const ParameterReader &parameter = Parameter::container() )
41  : time_( parameter.getValue( "fem.timeprovider.starttime",
42  static_cast<double>(0.0) ) ),
43  timeStep_( 0 ),
44  dt_( 0.0 ),
45  invdt_( HUGE_VAL ),
46  valid_( false ),
47  dtEstimateValid_( false ),
48  parameter_( parameter )
49  {
51  }
52 
53  inline explicit TimeProviderBase ( const double startTime, const ParameterReader &parameter = Parameter::container() )
54  : time_( startTime ),
55  timeStep_( 0 ),
56  dt_( 0.0 ),
57  invdt_( HUGE_VAL ),
58  valid_( false ),
59  dtEstimateValid_( false ),
60  parameter_( parameter )
61  {
63  }
64 
65  inline virtual ~TimeProviderBase()
66  {}
67 
68  void backup() const
69  {
70  std::tuple<const double&,const int&,const double&,const bool&,const double&>
72  PersistenceManager::backupValue("timeprovider",values);
73  }
74 
75  void restore()
76  {
77  std::tuple<double&,int&,double&,bool&,double&>
79  PersistenceManager::restoreValue("timeprovider",values);
80  dtEstimateValid_ = true;
81  invdt_ = 1.0 / dt_;
82  }
83 
84 
85  TimeProviderBase ( const ThisType & ) = delete;
86 
87  ThisType &operator= ( const ThisType & ) = delete;
88 
89 
94  inline double time () const
95  {
96  return time_;
97  }
98 
103  inline int timeStep () const
104  {
105  assert( timeStepValid() );
106  return timeStep_;
107  }
108 
113  inline double deltaT () const
114  {
115  assert( timeStepValid() );
116  return dt_;
117  }
118 
123  inline double inverseDeltaT () const
124  {
125  assert( timeStepValid() );
126  return invdt_;
127  }
128 
133  inline double timeStepEstimate () const
134  {
135  return dtEstimate_;
136  }
137 
142  inline void provideTimeStepEstimate ( const double dtEstimate )
143  {
144  dtEstimate_ = std::min( dtEstimate_, dtEstimate );
145  dtEstimateValid_ = true;
146  }
151  inline void provideTimeStepUpperBound ( const double upperBound )
152  {
153  dtUpperBound_ = std::min( dtUpperBound_, upperBound );
154  dtEstimateValid_ = true;
155  }
156 
158  inline void invalidateTimeStep ()
159  {
160  valid_ = false;
161  }
162 
164  inline bool timeStepValid () const
165  {
166  return valid_;
167  }
168 
169  protected:
170  double time_;
172  double dt_;
173  double invdt_;
174  bool valid_;
176  double dtEstimate_;
179 
180  void advance ()
181  {
182  if( timeStepValid() )
183  {
184  time_ += deltaT();
185  ++timeStep_;
186  }
187  }
188 
190  {
191  dtEstimate_ = std::numeric_limits< double >::max();
192  dtUpperBound_ = std::numeric_limits< double >::max();
193  dtEstimateValid_ = false;
194  }
195  };
196 
197 
215  template< class CollectiveCommunication = typename MPIManager::CollectiveCommunication >
217  : public TimeProviderBase
218  {
220  typedef TimeProviderBase BaseType;
221 
222  public:
223  typedef CollectiveCommunication CollectiveCommunicationType;
224 
231  explicit FixedStepTimeProvider ( const double startTime, const double timeStepSize,
232  const CollectiveCommunicationType &comm,
233  const ParameterReader &parameter = Parameter::container() )
234  : BaseType( startTime, parameter ), comm_( comm )
235  {
236  dt_ = timeStepSize;
237  initTimeStep();
238  }
239 
240  explicit FixedStepTimeProvider ( const double startTime, const double timeStepSize,
241  const ParameterReader &parameter = Parameter::container() )
242  : BaseType( startTime, parameter ), comm_( MPIManager::comm() )
243  {
244  dt_ = timeStepSize;
245  initTimeStep();
246  }
247 
256  : BaseType( parameter.getValue< double>( "fem.timeprovider.starttime", 0.0 ), parameter ), comm_( MPIManager::comm() )
257  {
258  dt_ = parameter.getValidValue< double >("fem.timeprovider.fixedtimestep", [] ( double v ) { return v > 0.0;} );
259  initTimeStep();
260  }
261 
262  explicit FixedStepTimeProvider ( const CollectiveCommunicationType &comm,
263  const ParameterReader &parameter = Parameter::container() )
264  : BaseType( parameter.getValue< double>( "fem.timeprovider.starttime", 0.0 ), parameter ), comm_( comm )
265  {
266  dt_ = parameter.getValidValue< double >("fem.timeprovider.fixedtimestep", [] ( double v ) { return v > 0.0;} );
267  initTimeStep();
268  }
270 
271  FixedStepTimeProvider ( const ThisType & ) = delete;
272  FixedStepTimeProvider ( ThisType && ) = delete;
273  ThisType &operator= ( const ThisType & ) = delete;
274  ThisType &operator= ( ThisType && ) = delete;
275 
277  void next ()
278  {
279  if( !timeStepValid() )
280  DUNE_THROW( InvalidStateException, "Invalid Time Step in FixedStepTimeProvider" );
281  advance();
282  initTimeStep();
283  }
284 
285  protected:
286  using BaseType::advance;
287  using BaseType::initTimeStepEstimate;
288  using BaseType::time_;
289  using BaseType::dt_;
290  using BaseType::valid_;
291 
292  inline void initTimeStep ()
293  {
294  valid_ = true;
296  }
297 
298  const CollectiveCommunicationType &comm_;
299  };
300 
301 
402  template< class CollectiveCommunication = typename MPIManager::CollectiveCommunication >
404  : public TimeProviderBase
405  {
407  typedef TimeProviderBase BaseType;
408 
409  public:
410  typedef CollectiveCommunication CollectiveCommunicationType;
411 
412  protected:
413 
414  using BaseType::parameter_;
415 
416  inline double getCflFactor() const
417  {
418  return parameter_.getValidValue( "fem.timeprovider.factor", static_cast<double>(1.0),
419  [] ( double val ) { return val > 0.0; } );
420  }
421 
422  inline int getUpdateStep () const
423  {
424  return parameter_.getValidValue( "fem.timeprovider.updatestep", static_cast<int>(1),
425  [] ( int step ) { return step > 0; } );
426  }
427 
428  public:
433  explicit TimeProvider ( const ParameterReader &parameter = Parameter::container() )
434  : BaseType( parameter ),
435  comm_( MPIManager::comm() ),
436  cfl_( getCflFactor() ),
437  updateStep_( getUpdateStep() ),
438  counter_( updateStep_ )
439  {}
440 
441  explicit TimeProvider ( const CollectiveCommunicationType &comm, const ParameterReader &parameter = Parameter::container() )
442  : BaseType( parameter ),
443  comm_( comm ),
444  cfl_( getCflFactor() ),
445  updateStep_( getUpdateStep() ),
446  counter_( updateStep_ )
447  {}
448 
454  explicit TimeProvider ( const double startTime,
455  const CollectiveCommunicationType &comm = MPIManager::comm() )
456  : BaseType( startTime ),
457  comm_( comm ),
458  cfl_( getCflFactor() ),
459  updateStep_( getUpdateStep() ),
460  counter_( updateStep_ )
461  {}
462 
469  TimeProvider ( const double startTime,
470  const double cfl,
471  const CollectiveCommunicationType &comm = MPIManager::comm() )
472  : BaseType( startTime ),
473  comm_( comm ),
474  cfl_( cfl ),
475  updateStep_( 1 ),
476  counter_( updateStep_ )
477  {}
478 
479  virtual ~TimeProvider()
480  {}
481 
482  TimeProvider ( const ThisType & ) = delete;
483 
484  ThisType &operator= ( const ThisType & ) = delete;
485 
488  inline void init ()
489  {
490  initTimeStep( dtEstimate_ );
491  }
492 
498  inline void init ( const double timeStep )
499  {
500  initTimeStep( timeStep );
501  }
502 
508  inline void next ()
509  {
510  assert( this->dtEstimateValid_ );
511  advance();
512  initTimeStep( dtEstimate_ );
513  }
514 
523  inline void next ( const double timeStep )
524  {
525  advance();
526  initTimeStep(timeStep);
527  }
528 
532  inline double factor () const
533  {
534  return cfl_;
535  }
536 
537  protected:
538  using BaseType::advance;
539  using BaseType::initTimeStepEstimate;
540 
541  void initTimeStep ( const double dtEstimate )
542  {
543  // increase counter
544  ++counter_ ;
545 
546  if( counter_ >= updateStep_ )
547  {
548  // set timestep estimate
549  dt_ = std::min(cfl_ * dtEstimate,dtUpperBound_);
550  dt_ = comm_.min( dt_ );
551  invdt_ = 1.0 / dt_;
552  valid_ = (dt_ > 0.0);
553  // reset counter
554  counter_ = 0;
555  }
556 
558  }
559 
560  public:
566  inline void restore ( const double time, const int timeStep )
567  {
568  time_ = time;
570  }
571 
572  inline virtual void backup () const
573  {
575  }
576 
577  inline virtual void restore ()
578  {
580  const_cast< double & >( cfl_ ) = getCflFactor();
581  }
582 
583  protected:
584  using BaseType::dt_;
585  using BaseType::invdt_;
586  using BaseType::dtEstimate_;
587  using BaseType::dtUpperBound_;
588  using BaseType::valid_;
589  using BaseType::timeStep_;
590 
591  const CollectiveCommunicationType& comm_;
592  const double cfl_;
593  const int updateStep_;
594  int counter_;
595  };
596 
597 
598 
599  // TODO : remove!
601 
602 
603 
611  template< class Grid >
613  : public TimeProvider< typename Grid::Traits::CollectiveCommunication >
614  {
615  typedef GridTimeProvider< Grid > ThisType;
617 
618  // type of DofManager for sequence number
620 
621  public:
622  typedef typename Grid::Traits::CollectiveCommunication CollectiveCommunicationType;
623 
624  explicit GridTimeProvider ( const Grid &grid )
625  : BaseType( grid.comm() ),
626  dm_( DofManagerType ::instance( grid ) ),
627  sequence_( -1 )
628  {}
629 
630  GridTimeProvider ( const double startTime,
631  const Grid &grid )
632  : BaseType( startTime, grid.comm() ),
633  dm_( DofManagerType ::instance( grid ) ),
634  sequence_( -1 )
635  {}
636 
637  GridTimeProvider ( const double startTime,
638  const double cfl,
639  const Grid &grid )
640  : BaseType( startTime, cfl, grid.comm() ),
641  dm_( DofManagerType ::instance( grid ) ),
642  sequence_( -1 )
643  {}
644 
645  virtual ~GridTimeProvider() {}
646 
647  protected:
648  using BaseType :: counter_ ;
649  using BaseType :: updateStep_ ;
650 
651  // this initTimeStep method also check the sequence number
652  // in case the grid has changed due to adaptivity
653  void initTimeStep ( const double dtEstimate )
654  {
655  const int currentSequence = dm_.sequence();
656  // check sequence number
657  if( sequence_ != currentSequence )
658  {
659  // if sequence number changed, update in any case
660  counter_ = updateStep_ ;
661  sequence_ = currentSequence ;
662  }
663 
664  // call initTimeStep on base class
665  BaseType :: initTimeStep( dtEstimate );
666  }
667 
668  const DofManagerType& dm_;
669  int sequence_ ;
670  };
671 
672  } // namespace Fem
673 
674 } // namespace Dune
675 
676 #endif // #ifndef DUNE_FEM_TIMEPROVIDER_HH
CollectiveCommunication CollectiveCommunicationType
Definition: timeprovider.hh:410
int sequence_
Definition: timeprovider.hh:669
TimeProvider(const double startTime, const CollectiveCommunicationType &comm=MPIManager::comm())
constructor taking start time
Definition: timeprovider.hh:454
double dt_
Definition: timeprovider.hh:172
virtual ~GridTimeProvider()
Definition: timeprovider.hh:645
void initTimeStepEstimate()
Definition: timeprovider.hh:189
virtual void backup() const
backup persistent object
Definition: timeprovider.hh:572
void next()
goto next time step
Definition: timeprovider.hh:277
virtual ~TimeProviderBase()
Definition: timeprovider.hh:65
static constexpr T min(T a)
Definition: utility.hh:81
the same functionality as the Dune::TimeProvider.
Definition: timeprovider.hh:612
void init()
init dt with time step estimate
Definition: timeprovider.hh:488
Definition: timeprovider.hh:216
int timeStep() const
obtain number of the current time step
Definition: timeprovider.hh:103
virtual ~TimeProvider()
Definition: timeprovider.hh:479
void provideTimeStepEstimate(const double dtEstimate)
set time step estimate to minimum of given value and internal time step estiamte
Definition: timeprovider.hh:142
double deltaT() const
obtain the size of the current time step
Definition: timeprovider.hh:113
double time_
Definition: timeprovider.hh:170
static const CollectiveCommunication & comm()
Definition: mpimanager.hh:108
double inverseDeltaT() const
obtain the size of the inverse of the current time step
Definition: timeprovider.hh:123
static constexpr T max(T a)
Definition: utility.hh:65
virtual ~FixedStepTimeProvider()
Definition: timeprovider.hh:269
TimeProviderBase(const ParameterReader &parameter=Parameter::container())
Definition: timeprovider.hh:40
CollectiveCommunication CollectiveCommunicationType
Definition: timeprovider.hh:223
GridTimeProvider(const Grid &grid)
Definition: timeprovider.hh:624
ThisType & operator=(const ThisType &)=delete
bool timeStepValid() const
return if this time step should be used
Definition: timeprovider.hh:164
TimeProvider(const double startTime, const double cfl, const CollectiveCommunicationType &comm=MPIManager::comm())
constructor taking start time and CFL constant
Definition: timeprovider.hh:469
Definition: datacollector.hh:45
virtual void restore()=0
restore persistent object
void backup() const
backup persistent object
Definition: timeprovider.hh:68
const int updateStep_
Definition: timeprovider.hh:593
void init(const double timeStep)
init dt with provided time step
Definition: timeprovider.hh:498
void next()
goto next time step
Definition: timeprovider.hh:508
void restore(const double time, const int timeStep)
restore time and timestep from outside (i.e. from former calculation)
Definition: timeprovider.hh:566
GridTimeProvider(const double startTime, const Grid &grid)
Definition: timeprovider.hh:630
FixedStepTimeProvider(const CollectiveCommunicationType &comm, const ParameterReader &parameter=Parameter::container())
Definition: timeprovider.hh:262
void restore()
restore persistent object
Definition: timeprovider.hh:75
virtual void backup() const =0
backup persistent object
GridTimeProvider(const double startTime, const double cfl, const Grid &grid)
Definition: timeprovider.hh:637
ParameterReader parameter_
Definition: timeprovider.hh:178
general base for time providers
Definition: timeprovider.hh:35
const DofManagerType & dm_
Definition: timeprovider.hh:668
int counter_
Definition: timeprovider.hh:594
int timeStep_
Definition: timeprovider.hh:171
TimeProvider DefaultTimeProvider
Definition: timeprovider.hh:600
double dtEstimate_
Definition: timeprovider.hh:176
virtual void restore()
restore persistent object
Definition: timeprovider.hh:577
void initTimeStep(const double dtEstimate)
Definition: timeprovider.hh:541
Grid::Traits::CollectiveCommunication CollectiveCommunicationType
Definition: timeprovider.hh:622
Definition: coordinate.hh:4
Definition: mpimanager.hh:19
FixedStepTimeProvider(const double startTime, const double timeStepSize, const ParameterReader &parameter=Parameter::container())
Definition: timeprovider.hh:240
TimeProviderBase(const double startTime, const ParameterReader &parameter=Parameter::container())
Definition: timeprovider.hh:53
void initTimeStep(const double dtEstimate)
Definition: timeprovider.hh:653
T getValidValue(const std::string &key, const Validator &validator) const
get optional parameter
Definition: reader.hh:187
static ParameterContainer & container()
Definition: io/parameter.hh:190
const CollectiveCommunicationType & comm_
Definition: timeprovider.hh:298
const double cfl_
Definition: timeprovider.hh:592
static void restoreValue(const std::string &token, T &value)
Definition: persistencemanager.hh:385
base class for auto persistent objects
Definition: persistencemanager.hh:562
base class for persistent objects
Definition: persistencemanager.hh:96
TimeProvider(const ParameterReader &parameter=Parameter::container())
default constructor
Definition: timeprovider.hh:433
bool dtEstimateValid_
Definition: timeprovider.hh:175
manager for global simulation time of time-dependent solutions
Definition: timeprovider.hh:403
bool valid_
Definition: timeprovider.hh:174
double factor() const
return the global factor number
Definition: timeprovider.hh:532
double dtUpperBound_
Definition: timeprovider.hh:177
void invalidateTimeStep()
count current time step a not valid
Definition: timeprovider.hh:158
const CollectiveCommunicationType & comm_
Definition: timeprovider.hh:591
FixedStepTimeProvider(const ParameterReader &parameter=Parameter::container())
constructor
Definition: timeprovider.hh:255
TimeProvider(const CollectiveCommunicationType &comm, const ParameterReader &parameter=Parameter::container())
Definition: timeprovider.hh:441
void next(const double timeStep)
goto next time step
Definition: timeprovider.hh:523
int getUpdateStep() const
Definition: timeprovider.hh:422
double time() const
obtain the current time
Definition: timeprovider.hh:94
void advance()
Definition: timeprovider.hh:180
void provideTimeStepUpperBound(const double upperBound)
set upper bound for time step to minimum of given value and internal bound
Definition: timeprovider.hh:151
static void backupValue(const std::string &token, const T &value)
Definition: persistencemanager.hh:379
double timeStepEstimate() const
obtain current estimate on time step
Definition: timeprovider.hh:133
double invdt_
Definition: timeprovider.hh:173
double getCflFactor() const
Definition: timeprovider.hh:416
FixedStepTimeProvider(const double startTime, const double timeStepSize, const CollectiveCommunicationType &comm, const ParameterReader &parameter=Parameter::container())
constructor
Definition: timeprovider.hh:231
void initTimeStep()
Definition: timeprovider.hh:292