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>
33#include <dune/common/parallel/collectivecommunication.hh>
34#include <dune/common/parallel/mpihelper.hh>
35#include <dune/common/exceptions.hh>
75 virtual Scalar
time()
const = 0;
108template <
class Scalar>
151 void reset(Scalar startTime, Scalar dt, Scalar tEnd,
bool verbose =
true)
155 Dune::MPIHelper::getCollectiveCommunication().rank() == 0;
233 std::cout <<
"Set new end time to t = " << t <<
" seconds." << std::endl;
240 {
return timer_.elapsed(); }
331 using std::min;
using std::max;
345 std::cout <<
"[" << std::fixed << std::setw( 3 ) << std::setfill(
' ' )
346 << std::setprecision( 0 ) << percent <<
"%] "
349 <<
"Wall clock time: " << std::setprecision( 3 ) << cpuTime
350 <<
", time: " << std::setprecision( 5 ) <<
time_
359 template<
class Communicator = Dune::CollectiveCommunication<
typename Dune::MPIHelper::MPICommunicator> >
360 void finalize(
const Communicator& comm = Dune::MPIHelper::getCollectiveCommunication())
362 auto cpuTime =
timer_.stop();
366 std::cout <<
"Simulation took " << cpuTime <<
" seconds on "
367 << comm.size() <<
" processes.\n";
371 cpuTime = comm.sum(cpuTime);
375 std::cout <<
"The cumulative CPU time was " << cpuTime <<
" seconds.\n";
405template <
class Scalar>
412 periodicCheckPoints_ =
false;
413 deltaPeriodicCheckPoint_ = 0.0;
414 lastPeriodicCheckPoint_ = startTime;
415 isCheckPoint_ =
false;
424 const auto newTime = this->
time()+dt;
428 if (periodicCheckPoints_ && Dune::FloatCmp::eq(newTime - lastPeriodicCheckPoint_, deltaPeriodicCheckPoint_, 1e-7))
430 lastPeriodicCheckPoint_ += deltaPeriodicCheckPoint_;
431 isCheckPoint_ =
true;
435 else if (!checkPoints_.empty() && Dune::FloatCmp::eq(newTime - checkPoints_.front(), 0.0, 1e-7))
438 isCheckPoint_ =
true;
444 isCheckPoint_ =
false;
464 const auto threshold = 0.2*nextDt;
465 const auto nextTime = this->
time() + nextDt;
467 if (periodicCheckPoints_ && Dune::FloatCmp::le(lastPeriodicCheckPoint_ + deltaPeriodicCheckPoint_ - nextTime, threshold))
468 nextDt = lastPeriodicCheckPoint_ + deltaPeriodicCheckPoint_ - this->
time();
470 if (!checkPoints_.empty() && Dune::FloatCmp::le(checkPoints_.front() - nextTime, threshold))
471 nextDt = checkPoints_.front() - this->
time();
473 assert(nextDt > 0.0);
486 const auto maxCheckPointDt = computeStepSizeRespectingCheckPoints_();
488 return min(maxDtParent, maxCheckPointDt);
504 if (signbit(interval))
505 DUNE_THROW(Dune::InvalidStateException,
"Interval has to be positive!");
507 periodicCheckPoints_ =
true;
508 deltaPeriodicCheckPoint_ = interval;
509 lastPeriodicCheckPoint_ = offset;
510 while (lastPeriodicCheckPoint_ + interval - this->
time() < 1e-14*interval)
511 lastPeriodicCheckPoint_ += interval;
514 std::cout <<
"Enabled periodic check points every " << interval
515 <<
" seconds with the next check point at " << lastPeriodicCheckPoint_ + interval <<
" seconds." << std::endl;
518 if (Dune::FloatCmp::eq(this->
time()-lastPeriodicCheckPoint_, 0.0, 1e-7))
519 isCheckPoint_ =
true;
527 { periodicCheckPoints_ =
false; }
532 periodicCheckPoints_ =
false;
533 while (!checkPoints_.empty())
542 {
return isCheckPoint_; }
575 template<
class ForwardIterator>
579 for (; first != last; ++first)
580 setCheckPoint_(*first);
588 void setCheckPoint_(Scalar t)
590 if (t < this->
time())
593 std::cerr <<
"Couldn't insert checkpoint at t = " << t
594 <<
" because that's in the past! (current simulation time is " << this->
time() <<
")" << std::endl;
598 if (!checkPoints_.empty())
600 if (t < checkPoints_.back())
603 std::cerr <<
"Couldn't insert checkpoint as it is earlier than the last check point in the queue.\n"
604 <<
"Checkpoints can only be inserted in ascending order." << std::endl;
609 checkPoints_.push(t);
611 std::cout <<
"Set check point at t = " << t <<
" seconds." << std::endl;
617 Scalar computeStepSizeRespectingCheckPoints_()
const
620 auto maxDt = std::numeric_limits<Scalar>::max();
622 if (periodicCheckPoints_)
623 maxDt = min(maxDt, lastPeriodicCheckPoint_ + deltaPeriodicCheckPoint_ - this->
time());
625 if (!checkPoints_.empty())
626 maxDt = min(maxDt, checkPoints_.front() - this->time());
631 bool periodicCheckPoints_;
632 Scalar deltaPeriodicCheckPoint_;
633 Scalar lastPeriodicCheckPoint_;
634 std::queue<Scalar> checkPoints_;
The infrastructure to retrieve run-time parameters from Dune::ParameterTrees.
make the local view function available whenever we use the grid geometry
Definition: adapt.hh:29
Manages the handling of time dependent problems.
Definition: timeloop.hh:65
virtual ~TimeLoopBase()
Abstract base class needs virtual constructor.
Definition: timeloop.hh:68
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:110
Scalar maxTimeStepSize() const override
The current maximum time step size.
Definition: timeloop.hh:326
void setMaxTimeStepSize(Scalar maxDt)
Set the maximum time step size to a given value.
Definition: timeloop.hh:264
void resetTimer()
Reset the timer.
Definition: timeloop.hh:143
TimeLoop(Scalar startTime, Scalar dt, Scalar tEnd, bool verbose=true)
Definition: timeloop.hh:112
void setTime(Scalar t, int stepIdx)
Set the current simulated time and the time step index.
Definition: timeloop.hh:207
Scalar time_
Definition: timeloop.hh:389
int timeStepIndex() const
Returns number of time steps which have been executed since the beginning of the simulation.
Definition: timeloop.hh:282
double stop()
Tells the time loop to stop tracking the time.
Definition: timeloop.hh:135
Scalar endTime_
Definition: timeloop.hh:390
Scalar previousTimeStepSize_
Definition: timeloop.hh:393
void advanceTimeStep() override
Advance time step.
Definition: timeloop.hh:177
int timeStepIdx_
Definition: timeloop.hh:396
void start()
Tells the time loop to start tracking the time.
Definition: timeloop.hh:126
Scalar userSetMaxTimeStepSize_
Definition: timeloop.hh:394
void setFinished(bool finished=true)
Specify whether the simulation is finished.
Definition: timeloop.hh:298
bool verbose() const
If the time loop has verbose output.
Definition: timeloop.hh:380
bool verbose_
Definition: timeloop.hh:398
bool willBeFinished() const
Returns true if the simulation is finished after the time level is incremented by the current time st...
Definition: timeloop.hh:316
Scalar previousTimeStepSize() const
The previous time step size.
Definition: timeloop.hh:288
void reset(Scalar startTime, Scalar dt, Scalar tEnd, bool verbose=true)
Reset the time loop.
Definition: timeloop.hh:151
Scalar endTime() const
Returns the number of (simulated) seconds which the simulation runs.
Definition: timeloop.hh:221
void setTimeStepSize(Scalar dt) final
Set the current time step size to a given value.
Definition: timeloop.hh:252
bool finished() const override
Returns true if the simulation is finished.
Definition: timeloop.hh:307
Scalar timeStepSize_
Definition: timeloop.hh:392
void finalize(const Communicator &comm=Dune::MPIHelper::getCollectiveCommunication())
Print final status and stops tracking the time.
Definition: timeloop.hh:360
void reportTimeStep() const
State info on cpu time.
Definition: timeloop.hh:339
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:275
bool finished_
Definition: timeloop.hh:397
Dune::Timer timer_
Definition: timeloop.hh:388
void setTime(Scalar t)
Set the current simulated time, don't change the current time step index.
Definition: timeloop.hh:198
void setEndTime(Scalar t)
Set the time of simulated seconds at which the simulation runs.
Definition: timeloop.hh:229
double wallClockTime() const
Returns the current wall clock time (cpu time) spend in this time loop.
Definition: timeloop.hh:239
Scalar timeAfterLastTimeStep_
Definition: timeloop.hh:395
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:215
Scalar timeStepWallClockTime_
Definition: timeloop.hh:395
A time loop with a check point mechanism.
Definition: timeloop.hh:407
bool isCheckPoint() const
Whether now is a time checkpoint.
Definition: timeloop.hh:541
void setPeriodicCheckPoint(Scalar interval, Scalar offset=0.0)
Set a periodic check point.
Definition: timeloop.hh:501
void setCheckPoint(Scalar t)
add a checkpoint to the queue
Definition: timeloop.hh:550
void advanceTimeStep() override
Advance time step.
Definition: timeloop.hh:421
void removeAllCheckPoints()
remove all check points
Definition: timeloop.hh:530
void setCheckPoint(const std::vector< Scalar > &checkPoints)
add checkpoints to the queue from a vector of time points
Definition: timeloop.hh:565
Scalar maxTimeStepSize() const override
The current maximum time step size.
Definition: timeloop.hh:483
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:576
CheckPointTimeLoop(Scalar startTime, Scalar dt, Scalar tEnd, bool verbose=true)
Definition: timeloop.hh:409
void disablePeriodicCheckPoints()
disable periodic check points
Definition: timeloop.hh:526