24#ifndef DUMUX_TIME_LOOP_HH
25#define DUMUX_TIME_LOOP_HH
31#include <dune/common/float_cmp.hh>
32#include <dune/common/timer.hh>
34#include <dune/common/version.hh>
35#if DUNE_VERSION_LT(DUNE_COMMON,2,7)
36#include <dune/common/parallel/collectivecommunication.hh>
38#include <dune/common/parallel/communication.hh>
41#include <dune/common/parallel/mpihelper.hh>
42#include <dune/common/exceptions.hh>
82 virtual Scalar
time()
const = 0;
115template <
class Scalar>
158 void reset(Scalar startTime, Scalar dt, Scalar tEnd,
bool verbose =
true)
162 Dune::MPIHelper::getCollectiveCommunication().rank() == 0;
240 std::cout <<
"Set new end time to t = " << t <<
" seconds." << std::endl;
247 {
return timer_.elapsed(); }
338 using std::min;
using std::max;
352 std::cout <<
"[" << std::fixed << std::setw( 3 ) << std::setfill(
' ' )
353 << std::setprecision( 0 ) << percent <<
"%] "
356 <<
"Wall clock time: " << std::setprecision( 3 ) << cpuTime
357 <<
", time: " << std::setprecision( 5 ) <<
time_
366 template<
class Communicator = Dune::CollectiveCommunication<
typename Dune::MPIHelper::MPICommunicator> >
367 void finalize(
const Communicator& comm = Dune::MPIHelper::getCollectiveCommunication())
369 auto cpuTime =
timer_.stop();
373 std::cout <<
"Simulation took " << cpuTime <<
" seconds on "
374 << comm.size() <<
" processes.\n";
378 cpuTime = comm.sum(cpuTime);
382 std::cout <<
"The cumulative CPU time was " << cpuTime <<
" seconds.\n";
412template <
class Scalar>
419 periodicCheckPoints_ =
false;
420 deltaPeriodicCheckPoint_ = 0.0;
421 lastPeriodicCheckPoint_ = startTime;
422 isCheckPoint_ =
false;
431 const auto newTime = this->
time()+dt;
435 if (periodicCheckPoints_ && Dune::FloatCmp::eq(newTime - lastPeriodicCheckPoint_, deltaPeriodicCheckPoint_, 1e-7))
437 lastPeriodicCheckPoint_ += deltaPeriodicCheckPoint_;
438 isCheckPoint_ =
true;
442 else if (!checkPoints_.empty() && Dune::FloatCmp::eq(newTime - checkPoints_.front(), 0.0, 1e-7))
445 isCheckPoint_ =
true;
451 isCheckPoint_ =
false;
471 const auto threshold = 0.2*nextDt;
472 const auto nextTime = this->
time() + nextDt;
474 if (periodicCheckPoints_ && Dune::FloatCmp::le(lastPeriodicCheckPoint_ + deltaPeriodicCheckPoint_ - nextTime, threshold))
475 nextDt = lastPeriodicCheckPoint_ + deltaPeriodicCheckPoint_ - this->
time();
477 if (!checkPoints_.empty() && Dune::FloatCmp::le(checkPoints_.front() - nextTime, threshold))
478 nextDt = checkPoints_.front() - this->
time();
480 assert(nextDt > 0.0);
493 const auto maxCheckPointDt = computeStepSizeRespectingCheckPoints_();
495 return min(maxDtParent, maxCheckPointDt);
511 if (signbit(interval))
512 DUNE_THROW(Dune::InvalidStateException,
"Interval has to be positive!");
514 periodicCheckPoints_ =
true;
515 deltaPeriodicCheckPoint_ = interval;
516 lastPeriodicCheckPoint_ = offset;
517 while (lastPeriodicCheckPoint_ + interval - this->
time() < 1e-14*interval)
518 lastPeriodicCheckPoint_ += interval;
521 std::cout <<
"Enabled periodic check points every " << interval
522 <<
" seconds with the next check point at " << lastPeriodicCheckPoint_ + interval <<
" seconds." << std::endl;
525 if (Dune::FloatCmp::eq(this->
time()-lastPeriodicCheckPoint_, 0.0, 1e-7))
526 isCheckPoint_ =
true;
534 { periodicCheckPoints_ =
false; }
539 periodicCheckPoints_ =
false;
540 while (!checkPoints_.empty())
549 {
return isCheckPoint_; }
582 template<
class ForwardIterator>
586 for (; first != last; ++first)
587 setCheckPoint_(*first);
595 void setCheckPoint_(Scalar t)
597 if (t < this->
time())
600 std::cerr <<
"Couldn't insert checkpoint at t = " << t
601 <<
" because that's in the past! (current simulation time is " << this->
time() <<
")" << std::endl;
605 if (!checkPoints_.empty())
607 if (t < checkPoints_.back())
610 std::cerr <<
"Couldn't insert checkpoint as it is earlier than the last check point in the queue.\n"
611 <<
"Checkpoints can only be inserted in ascending order." << std::endl;
616 checkPoints_.push(t);
618 std::cout <<
"Set check point at t = " << t <<
" seconds." << std::endl;
624 Scalar computeStepSizeRespectingCheckPoints_()
const
627 auto maxDt = std::numeric_limits<Scalar>::max();
629 if (periodicCheckPoints_)
630 maxDt = min(maxDt, lastPeriodicCheckPoint_ + deltaPeriodicCheckPoint_ - this->
time());
632 if (!checkPoints_.empty())
633 maxDt = min(maxDt, checkPoints_.front() - this->time());
638 bool periodicCheckPoints_;
639 Scalar deltaPeriodicCheckPoint_;
640 Scalar lastPeriodicCheckPoint_;
641 std::queue<Scalar> checkPoints_;
The infrastructure to retrieve run-time parameters from Dune::ParameterTrees.
Manages the handling of time dependent problems.
Definition: timeloop.hh:72
virtual ~TimeLoopBase()
Abstract base class needs virtual constructor.
Definition: timeloop.hh:75
virtual void setTimeStepSize(Scalar dt)=0
Set the current time step size to a given value.
virtual Scalar time() const =0
Return the time before the time integration. To get the time after the time integration you have to ...
virtual Scalar timeStepSize() const =0
Returns the suggested time step length .
virtual bool finished() const =0
Returns true if the simulation is finished.
virtual void advanceTimeStep()=0
Advance to the next time step.
virtual Scalar maxTimeStepSize() const =0
Get the maximum possible time step size .
The default time loop for instationary simulations.
Definition: timeloop.hh:117
Scalar maxTimeStepSize() const override
The current maximum time step size.
Definition: timeloop.hh:333
void setMaxTimeStepSize(Scalar maxDt)
Set the maximum time step size to a given value.
Definition: timeloop.hh:271
void resetTimer()
Reset the timer.
Definition: timeloop.hh:150
TimeLoop(Scalar startTime, Scalar dt, Scalar tEnd, bool verbose=true)
Definition: timeloop.hh:119
void setTime(Scalar t, int stepIdx)
Set the current simulated time and the time step index.
Definition: timeloop.hh:214
Scalar time_
Definition: timeloop.hh:396
int timeStepIndex() const
Returns number of time steps which have been executed since the beginning of the simulation.
Definition: timeloop.hh:289
double stop()
Tells the time loop to stop tracking the time.
Definition: timeloop.hh:142
Scalar endTime_
Definition: timeloop.hh:397
Scalar previousTimeStepSize_
Definition: timeloop.hh:400
void advanceTimeStep() override
Advance time step.
Definition: timeloop.hh:184
int timeStepIdx_
Definition: timeloop.hh:403
void start()
Tells the time loop to start tracking the time.
Definition: timeloop.hh:133
Scalar userSetMaxTimeStepSize_
Definition: timeloop.hh:401
void setFinished(bool finished=true)
Specify whether the simulation is finished.
Definition: timeloop.hh:305
bool verbose() const
If the time loop has verbose output.
Definition: timeloop.hh:387
bool verbose_
Definition: timeloop.hh:405
bool willBeFinished() const
Returns true if the simulation is finished after the time level is incremented by the current time st...
Definition: timeloop.hh:323
Scalar previousTimeStepSize() const
The previous time step size.
Definition: timeloop.hh:295
void reset(Scalar startTime, Scalar dt, Scalar tEnd, bool verbose=true)
Reset the time loop.
Definition: timeloop.hh:158
Scalar endTime() const
Returns the number of (simulated) seconds which the simulation runs.
Definition: timeloop.hh:228
void setTimeStepSize(Scalar dt) final
Set the current time step size to a given value.
Definition: timeloop.hh:259
bool finished() const override
Returns true if the simulation is finished.
Definition: timeloop.hh:314
Scalar timeStepSize_
Definition: timeloop.hh:399
void finalize(const Communicator &comm=Dune::MPIHelper::getCollectiveCommunication())
Print final status and stops tracking the time.
Definition: timeloop.hh:367
void reportTimeStep() const
State info on cpu time.
Definition: timeloop.hh:346
Scalar timeStepSize() const final
Returns the suggested time step length so that we don't miss the beginning of the next episode or cr...
Definition: timeloop.hh:282
bool finished_
Definition: timeloop.hh:404
Dune::Timer timer_
Definition: timeloop.hh:395
void setTime(Scalar t)
Set the current simulated time, don't change the current time step index.
Definition: timeloop.hh:205
void setEndTime(Scalar t)
Set the time of simulated seconds at which the simulation runs.
Definition: timeloop.hh:236
double wallClockTime() const
Returns the current wall clock time (cpu time) spend in this time loop.
Definition: timeloop.hh:246
Scalar timeAfterLastTimeStep_
Definition: timeloop.hh:402
Scalar time() const final
Return the time before the time integration. To get the time after the time integration you have to ...
Definition: timeloop.hh:222
Scalar timeStepWallClockTime_
Definition: timeloop.hh:402
A time loop with a check point mechanism.
Definition: timeloop.hh:414
bool isCheckPoint() const
Whether now is a time checkpoint.
Definition: timeloop.hh:548
void setPeriodicCheckPoint(Scalar interval, Scalar offset=0.0)
Set a periodic check point.
Definition: timeloop.hh:508
void setCheckPoint(Scalar t)
add a checkpoint to the queue
Definition: timeloop.hh:557
void advanceTimeStep() override
Advance time step.
Definition: timeloop.hh:428
void removeAllCheckPoints()
remove all check points
Definition: timeloop.hh:537
void setCheckPoint(const std::vector< Scalar > &checkPoints)
add checkpoints to the queue from a vector of time points
Definition: timeloop.hh:572
Scalar maxTimeStepSize() const override
The current maximum time step size.
Definition: timeloop.hh:490
void setCheckPoint(ForwardIterator first, ForwardIterator last)
add checkpoints to the queue from a container from the first iterator to the last iterator
Definition: timeloop.hh:583
CheckPointTimeLoop(Scalar startTime, Scalar dt, Scalar tEnd, bool verbose=true)
Definition: timeloop.hh:416
void disablePeriodicCheckPoints()
disable periodic check points
Definition: timeloop.hh:533