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#include <dune/common/parallel/communication.hh>
37#include <dune/common/parallel/mpihelper.hh>
38#include <dune/common/exceptions.hh>
79 virtual Scalar
time()
const = 0;
112template <
class Scalar>
155 void reset(Scalar startTime, Scalar dt, Scalar tEnd,
bool verbose =
true)
159 Dune::MPIHelper::getCommunication().rank() == 0;
237 std::cout << Fmt::format(
"Set new end time to t = {:.5g} seconds.\n", t);
244 {
return timer_.elapsed(); }
261 std::cerr << Fmt::format(
"You have set a very small timestep size (dt = {:.5g}).",
timeStepSize_)
262 <<
" This might lead to numerical problems!\n";
338 using std::min;
using std::max;
353 std::cout << Fmt::format(
"[{:3.0f}%] ", percent)
362 template<
class Communicator = Dune::Communication<
typename Dune::MPIHelper::MPICommunicator> >
363 void finalize(
const Communicator& comm = Dune::MPIHelper::getCommunication())
365 auto cpuTime =
timer_.stop();
368 std::cout << Fmt::format(
"Simulation took {:.5g} seconds on {} processes.\n", cpuTime, comm.size());
371 cpuTime = comm.sum(cpuTime);
374 std::cout << Fmt::format(
"The cumulative CPU time was {:.5g} seconds.\n", cpuTime);
407template <
class Scalar>
414 periodicCheckPoints_ =
false;
415 deltaPeriodicCheckPoint_ = 0.0;
416 lastPeriodicCheckPoint_ = startTime;
417 isCheckPoint_ =
false;
426 const auto newTime = this->
time()+dt;
430 if (periodicCheckPoints_ && Dune::FloatCmp::eq(newTime - lastPeriodicCheckPoint_, deltaPeriodicCheckPoint_, 1e-7))
432 lastPeriodicCheckPoint_ += deltaPeriodicCheckPoint_;
433 isCheckPoint_ =
true;
437 else if (!checkPoints_.empty() && Dune::FloatCmp::eq(newTime - checkPoints_.front(), 0.0, 1e-7))
440 isCheckPoint_ =
true;
446 isCheckPoint_ =
false;
466 const auto threshold = 0.2*nextDt;
467 const auto nextTime = this->
time() + nextDt;
469 if (periodicCheckPoints_ && Dune::FloatCmp::le(lastPeriodicCheckPoint_ + deltaPeriodicCheckPoint_ - nextTime, threshold))
470 nextDt = lastPeriodicCheckPoint_ + deltaPeriodicCheckPoint_ - this->
time();
472 if (!checkPoints_.empty() && Dune::FloatCmp::le(checkPoints_.front() - nextTime, threshold))
473 nextDt = checkPoints_.front() - this->
time();
475 assert(nextDt > 0.0);
488 const auto maxCheckPointDt = computeStepSizeRespectingCheckPoints_();
490 return min(maxDtParent, maxCheckPointDt);
506 if (signbit(interval))
507 DUNE_THROW(Dune::InvalidStateException,
"Interval has to be positive!");
509 periodicCheckPoints_ =
true;
510 deltaPeriodicCheckPoint_ = interval;
511 lastPeriodicCheckPoint_ = offset;
512 while (lastPeriodicCheckPoint_ + interval - this->
time() < 1e-14*interval)
513 lastPeriodicCheckPoint_ += interval;
516 std::cout << Fmt::format(
"Enabled periodic check points every {:.5g} seconds ", interval)
517 << Fmt::format(
"with the next check point at {:.5g} seconds.\n", lastPeriodicCheckPoint_ + interval);
520 if (Dune::FloatCmp::eq(this->
time()-lastPeriodicCheckPoint_, 0.0, 1e-7))
521 isCheckPoint_ =
true;
529 { periodicCheckPoints_ =
false; }
534 periodicCheckPoints_ =
false;
535 while (!checkPoints_.empty())
544 {
return isCheckPoint_; }
577 template<
class ForwardIterator>
581 for (; first != last; ++first)
582 setCheckPoint_(*first);
590 void setCheckPoint_(Scalar t)
595 std::cerr << Fmt::format(
"Couldn't insert checkpoint at t = {:.5g} ", t)
596 << Fmt::format(
"because that's in the past! (current simulation time is {:.5g})\n", this->
time());
600 if (!checkPoints_.empty())
602 if (t < checkPoints_.back())
605 std::cerr <<
"Couldn't insert checkpoint as it is earlier than the last check point in the queue.\n"
606 <<
"Checkpoints can only be inserted in ascending order." << std::endl;
611 checkPoints_.push(t);
613 std::cout << Fmt::format(
"Set check point at t = {:.5g} seconds.\n", t);
619 Scalar computeStepSizeRespectingCheckPoints_()
const
622 auto maxDt = std::numeric_limits<Scalar>::max();
624 if (periodicCheckPoints_)
625 maxDt = min(maxDt, lastPeriodicCheckPoint_ + deltaPeriodicCheckPoint_ - this->
time());
627 if (!checkPoints_.empty())
628 maxDt = min(maxDt, checkPoints_.front() - this->time());
633 bool periodicCheckPoints_;
634 Scalar deltaPeriodicCheckPoint_;
635 Scalar lastPeriodicCheckPoint_;
636 std::queue<Scalar> checkPoints_;
The infrastructure to retrieve run-time parameters from Dune::ParameterTrees.
Formatting based on the fmt-library which implements std::format of C++20.
Manages the handling of time dependent problems.
Definition: common/timeloop.hh:69
virtual ~TimeLoopBase()
Abstract base class needs virtual constructor.
Definition: common/timeloop.hh:72
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: common/timeloop.hh:114
Scalar maxTimeStepSize() const override
The current maximum time step size.
Definition: common/timeloop.hh:333
void setMaxTimeStepSize(Scalar maxDt)
Set the maximum time step size to a given value.
Definition: common/timeloop.hh:271
void resetTimer()
Reset the timer.
Definition: common/timeloop.hh:147
TimeLoop(Scalar startTime, Scalar dt, Scalar tEnd, bool verbose=true)
Definition: common/timeloop.hh:116
void setVerbose(bool verbose=true)
Sets time loop verbosity.
Definition: common/timeloop.hh:382
void setTime(Scalar t, int stepIdx)
Set the current simulated time and the time step index.
Definition: common/timeloop.hh:211
Scalar time_
Definition: common/timeloop.hh:391
int timeStepIndex() const
Returns number of time steps which have been executed since the beginning of the simulation.
Definition: common/timeloop.hh:289
double stop()
Tells the time loop to stop tracking the time.
Definition: common/timeloop.hh:139
Scalar endTime_
Definition: common/timeloop.hh:392
Scalar previousTimeStepSize_
Definition: common/timeloop.hh:395
void advanceTimeStep() override
Advance time step.
Definition: common/timeloop.hh:181
int timeStepIdx_
Definition: common/timeloop.hh:398
void start()
Tells the time loop to start tracking the time.
Definition: common/timeloop.hh:130
Scalar userSetMaxTimeStepSize_
Definition: common/timeloop.hh:396
void setFinished(bool finished=true)
Specify whether the simulation is finished.
Definition: common/timeloop.hh:305
bool verbose() const
If the time loop has verbose output.
Definition: common/timeloop.hh:378
void finalize(const Communicator &comm=Dune::MPIHelper::getCommunication())
Print final status and stops tracking the time.
Definition: common/timeloop.hh:363
bool verbose_
Definition: common/timeloop.hh:400
bool willBeFinished() const
Returns true if the simulation is finished after the time level is incremented by the current time st...
Definition: common/timeloop.hh:323
Scalar previousTimeStepSize() const
The previous time step size.
Definition: common/timeloop.hh:295
void reset(Scalar startTime, Scalar dt, Scalar tEnd, bool verbose=true)
Reset the time loop.
Definition: common/timeloop.hh:155
Scalar endTime() const
Returns the number of (simulated) seconds which the simulation runs.
Definition: common/timeloop.hh:225
void setTimeStepSize(Scalar dt) final
Set the current time step size to a given value.
Definition: common/timeloop.hh:256
bool finished() const override
Returns true if the simulation is finished.
Definition: common/timeloop.hh:314
Scalar timeStepSize_
Definition: common/timeloop.hh:394
void reportTimeStep() const
State info on cpu time.
Definition: common/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: common/timeloop.hh:282
bool finished_
Definition: common/timeloop.hh:399
Dune::Timer timer_
Definition: common/timeloop.hh:390
void setTime(Scalar t)
Set the current simulated time, don't change the current time step index.
Definition: common/timeloop.hh:202
void setEndTime(Scalar t)
Set the time of simulated seconds at which the simulation runs.
Definition: common/timeloop.hh:233
double wallClockTime() const
Returns the current wall clock time (cpu time) spend in this time loop.
Definition: common/timeloop.hh:243
Scalar timeAfterLastTimeStep_
Definition: common/timeloop.hh:397
Scalar time() const final
Return the time before the time integration. To get the time after the time integration you have to ...
Definition: common/timeloop.hh:219
Scalar timeStepWallClockTime_
Definition: common/timeloop.hh:397
A time loop with a check point mechanism.
Definition: common/timeloop.hh:409
bool isCheckPoint() const
Whether now is a time checkpoint.
Definition: common/timeloop.hh:543
void setPeriodicCheckPoint(Scalar interval, Scalar offset=0.0)
Set a periodic check point.
Definition: common/timeloop.hh:503
void setCheckPoint(Scalar t)
add a checkpoint to the queue
Definition: common/timeloop.hh:552
void advanceTimeStep() override
Advance time step.
Definition: common/timeloop.hh:423
void removeAllCheckPoints()
remove all check points
Definition: common/timeloop.hh:532
void setCheckPoint(const std::vector< Scalar > &checkPoints)
add checkpoints to the queue from a vector of time points
Definition: common/timeloop.hh:567
Scalar maxTimeStepSize() const override
The current maximum time step size.
Definition: common/timeloop.hh:485
void setCheckPoint(ForwardIterator first, ForwardIterator last)
add checkpoints to the queue from a container from the first iterator to the last iterator
Definition: common/timeloop.hh:578
CheckPointTimeLoop(Scalar startTime, Scalar dt, Scalar tEnd, bool verbose=true)
Definition: common/timeloop.hh:411
void disablePeriodicCheckPoints()
disable periodic check points
Definition: common/timeloop.hh:528