dune-fem 2.8.0
Loading...
Searching...
No Matches
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
14namespace Dune
15{
16
17 namespace Fem
18 {
19
36 {
38
39 public:
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_;
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 {
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
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;
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;
288 using BaseType::time_;
289 using BaseType::dt_;
290 using BaseType::valid_;
291
292 inline void initTimeStep ()
293 {
294 valid_ = true;
296 }
297
299 };
300
301
402 template< class CollectiveCommunication = typename MPIManager::CollectiveCommunication >
404 : public TimeProviderBase
405 {
408
409 public:
410 typedef CollectiveCommunication CollectiveCommunicationType;
411
412 protected:
413
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() ),
439 {}
440
442 : BaseType( parameter ),
443 comm_( comm ),
444 cfl_( getCflFactor() ),
447 {}
448
454 explicit TimeProvider ( const double startTime,
456 : BaseType( startTime ),
457 comm_( comm ),
458 cfl_( getCflFactor() ),
461 {}
462
469 TimeProvider ( const double startTime,
470 const double cfl,
472 : BaseType( startTime ),
473 comm_( comm ),
474 cfl_( cfl ),
475 updateStep_( 1 ),
477 {}
478
480 {}
481
482 TimeProvider ( const ThisType & ) = delete;
483
484 ThisType &operator= ( const ThisType & ) = delete;
485
488 inline void init ()
489 {
491 }
492
498 inline void init ( const double timeStep )
499 {
501 }
502
508 inline void next ()
509 {
510 assert( this->dtEstimateValid_ );
511 advance();
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:
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:
590
592 const double cfl_;
593 const int updateStep_;
595 };
596
597
598
606 template< class Grid >
608 : public TimeProvider< typename Grid::Traits::CollectiveCommunication >
609 {
612
613 // type of DofManager for sequence number
614 typedef DofManager < Grid > DofManagerType ;
615
616 public:
617 typedef typename Grid::Traits::CollectiveCommunication CollectiveCommunicationType;
618
619 explicit GridTimeProvider ( const Grid &grid )
620 : BaseType( grid.comm() ),
621 dm_( DofManagerType ::instance( grid ) ),
622 sequence_( -1 )
623 {}
624
625 GridTimeProvider ( const double startTime,
626 const Grid &grid )
627 : BaseType( startTime, grid.comm() ),
628 dm_( DofManagerType ::instance( grid ) ),
629 sequence_( -1 )
630 {}
631
632 GridTimeProvider ( const double startTime,
633 const double cfl,
634 const Grid &grid )
635 : BaseType( startTime, cfl, grid.comm() ),
636 dm_( DofManagerType ::instance( grid ) ),
637 sequence_( -1 )
638 {}
639
640 virtual ~GridTimeProvider() {}
641
642 protected:
643 using BaseType :: counter_ ;
644 using BaseType :: updateStep_ ;
645
646 // this initTimeStep method also check the sequence number
647 // in case the grid has changed due to adaptivity
648 void initTimeStep ( const double dtEstimate )
649 {
650 const int currentSequence = dm_.sequence();
651 // check sequence number
652 if( sequence_ != currentSequence )
653 {
654 // if sequence number changed, update in any case
656 sequence_ = currentSequence ;
657 }
658
659 // call initTimeStep on base class
660 BaseType :: initTimeStep( dtEstimate );
661 }
662
665 };
666
667 } // namespace Fem
668
669} // namespace Dune
670
671#endif // #ifndef DUNE_FEM_TIMEPROVIDER_HH
int sequence() const
return number of sequence, if dofmanagers memory was changed by calling some method like resize,...
Definition: dofmanager.hh:978
double min(const Dune::Fem::Double &v, const double p)
Definition: double.hh:953
Definition: bindguard.hh:11
static void backupValue(const std::string &token, const T &value)
Definition: persistencemanager.hh:395
static void restoreValue(const std::string &token, T &value)
Definition: persistencemanager.hh:401
base class for auto persistent objects
Definition: persistencemanager.hh:580
static ParameterContainer & container()
Definition: io/parameter.hh:193
T getValidValue(const std::string &key, const Validator &validator) const
get optional parameter
Definition: reader.hh:197
Definition: mpimanager.hh:337
static const CollectiveCommunication & comm()
Definition: mpimanager.hh:393
general base for time providers
Definition: timeprovider.hh:36
double timeStepEstimate() const
obtain current estimate on time step
Definition: timeprovider.hh:133
TimeProviderBase(const ParameterReader &parameter=Parameter::container())
Definition: timeprovider.hh:40
TimeProviderBase(const double startTime, const ParameterReader &parameter=Parameter::container())
Definition: timeprovider.hh:53
bool dtEstimateValid_
Definition: timeprovider.hh:175
bool valid_
Definition: timeprovider.hh:174
void restore()
restore persistent object
Definition: timeprovider.hh:75
virtual ~TimeProviderBase()
Definition: timeprovider.hh:65
TimeProviderBase(const ThisType &)=delete
double invdt_
Definition: timeprovider.hh:173
ThisType & operator=(const ThisType &)=delete
double dtUpperBound_
Definition: timeprovider.hh:177
void advance()
Definition: timeprovider.hh:180
double time() const
obtain the current time
Definition: timeprovider.hh:94
double dtEstimate_
Definition: timeprovider.hh:176
int timeStep() const
obtain number of the current time step
Definition: timeprovider.hh:103
int timeStep_
Definition: timeprovider.hh:171
void provideTimeStepEstimate(const double dtEstimate)
set time step estimate to minimum of given value and internal time step estiamte
Definition: timeprovider.hh:142
void initTimeStepEstimate()
Definition: timeprovider.hh:189
bool timeStepValid() const
return if this time step should be used
Definition: timeprovider.hh:164
double deltaT() const
obtain the size of the current time step
Definition: timeprovider.hh:113
void provideTimeStepUpperBound(const double upperBound)
set upper bound for time step to minimum of given value and internal bound
Definition: timeprovider.hh:151
double inverseDeltaT() const
obtain the size of the inverse of the current time step
Definition: timeprovider.hh:123
void backup() const
backup persistent object
Definition: timeprovider.hh:68
double time_
Definition: timeprovider.hh:170
void invalidateTimeStep()
count current time step a not valid
Definition: timeprovider.hh:158
double dt_
Definition: timeprovider.hh:172
ParameterReader parameter_
Definition: timeprovider.hh:178
Definition: timeprovider.hh:218
FixedStepTimeProvider(const ParameterReader &parameter=Parameter::container())
constructor
Definition: timeprovider.hh:255
ThisType & operator=(const ThisType &)=delete
CollectiveCommunication CollectiveCommunicationType
Definition: timeprovider.hh:223
FixedStepTimeProvider(ThisType &&)=delete
bool valid_
Definition: timeprovider.hh:174
virtual ~FixedStepTimeProvider()
Definition: timeprovider.hh:269
FixedStepTimeProvider(const ThisType &)=delete
const CollectiveCommunicationType & comm_
Definition: timeprovider.hh:298
void advance()
Definition: timeprovider.hh:180
void next()
goto next time step
Definition: timeprovider.hh:277
void initTimeStep()
Definition: timeprovider.hh:292
FixedStepTimeProvider(const double startTime, const double timeStepSize, const CollectiveCommunicationType &comm, const ParameterReader &parameter=Parameter::container())
constructor
Definition: timeprovider.hh:231
void initTimeStepEstimate()
Definition: timeprovider.hh:189
FixedStepTimeProvider(const double startTime, const double timeStepSize, const ParameterReader &parameter=Parameter::container())
Definition: timeprovider.hh:240
FixedStepTimeProvider(const CollectiveCommunicationType &comm, const ParameterReader &parameter=Parameter::container())
Definition: timeprovider.hh:262
double dt_
Definition: timeprovider.hh:172
manager for global simulation time of time-dependent solutions
Definition: timeprovider.hh:405
TimeProvider(const double startTime, const CollectiveCommunicationType &comm=MPIManager::comm())
constructor taking start time
Definition: timeprovider.hh:454
TimeProvider(const ThisType &)=delete
TimeProvider(const ParameterReader &parameter=Parameter::container())
default constructor
Definition: timeprovider.hh:433
const int updateStep_
Definition: timeprovider.hh:593
bool valid_
Definition: timeprovider.hh:174
virtual void backup() const
backup persistent object
Definition: timeprovider.hh:572
TimeProvider(const CollectiveCommunicationType &comm, const ParameterReader &parameter=Parameter::container())
Definition: timeprovider.hh:441
void initTimeStep(const double dtEstimate)
Definition: timeprovider.hh:541
double invdt_
Definition: timeprovider.hh:173
CollectiveCommunication CollectiveCommunicationType
Definition: timeprovider.hh:410
double dtUpperBound_
Definition: timeprovider.hh:177
void advance()
Definition: timeprovider.hh:180
void next(const double timeStep)
goto next time step
Definition: timeprovider.hh:523
void init(const double timeStep)
init dt with provided time step
Definition: timeprovider.hh:498
int getUpdateStep() const
Definition: timeprovider.hh:422
const CollectiveCommunicationType & comm_
Definition: timeprovider.hh:591
double dtEstimate_
Definition: timeprovider.hh:176
TimeProvider(const double startTime, const double cfl, const CollectiveCommunicationType &comm=MPIManager::comm())
constructor taking start time and CFL constant
Definition: timeprovider.hh:469
const double cfl_
Definition: timeprovider.hh:592
int timeStep_
Definition: timeprovider.hh:171
void initTimeStepEstimate()
Definition: timeprovider.hh:189
double factor() const
return the global factor number
Definition: timeprovider.hh:532
int counter_
Definition: timeprovider.hh:594
virtual void restore()
restore persistent object
Definition: timeprovider.hh:577
double getCflFactor() const
Definition: timeprovider.hh:416
void restore(const double time, const int timeStep)
restore time and timestep from outside (i.e. from former calculation)
Definition: timeprovider.hh:566
void next()
goto next time step
Definition: timeprovider.hh:508
ThisType & operator=(const ThisType &)=delete
virtual ~TimeProvider()
Definition: timeprovider.hh:479
void init()
init dt with time step estimate
Definition: timeprovider.hh:488
double dt_
Definition: timeprovider.hh:172
ParameterReader parameter_
Definition: timeprovider.hh:178
the same functionality as the Dune::TimeProvider.
Definition: timeprovider.hh:609
GridTimeProvider(const double startTime, const double cfl, const Grid &grid)
Definition: timeprovider.hh:632
GridTimeProvider(const Grid &grid)
Definition: timeprovider.hh:619
void initTimeStep(const double dtEstimate)
Definition: timeprovider.hh:648
virtual ~GridTimeProvider()
Definition: timeprovider.hh:640
Grid::Traits::CollectiveCommunication CollectiveCommunicationType
Definition: timeprovider.hh:617
int sequence_
Definition: timeprovider.hh:664
const DofManagerType & dm_
Definition: timeprovider.hh:663
GridTimeProvider(const double startTime, const Grid &grid)
Definition: timeprovider.hh:625
Definition: dofmanager.hh:761