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/parallel/communication.hh>
36#include <dune/common/parallel/mpihelper.hh>
37#include <dune/common/exceptions.hh>
78 virtual Scalar
time()
const = 0;
111template <
class Scalar>
154 void reset(Scalar startTime, Scalar dt, Scalar tEnd,
bool verbose =
true)
158 Dune::MPIHelper::getCommunication().rank() == 0;
236 std::cout << Fmt::format(
"Set new end time to t = {:.5g} seconds.\n", t);
243 {
return timer_.elapsed(); }
260 std::cerr << Fmt::format(
"You have set a very small timestep size (dt = {:.5g}).",
timeStepSize_)
261 <<
" This might lead to numerical problems!\n";
337 using std::min;
using std::max;
352 std::cout << Fmt::format(
"[{:3.0f}%] ", percent)
361 template<
class Communicator = Dune::Communication<
typename Dune::MPIHelper::MPICommunicator> >
362 void finalize(
const Communicator& comm = Dune::MPIHelper::getCommunication())
364 auto cpuTime =
timer_.stop();
367 std::cout << Fmt::format(
"Simulation took {:.5g} seconds on {} processes.\n", cpuTime, comm.size());
370 cpuTime = comm.sum(cpuTime);
373 std::cout << Fmt::format(
"The cumulative CPU time was {:.5g} seconds.\n", cpuTime);
406template <
class Scalar>
413 periodicCheckPoints_ =
false;
414 deltaPeriodicCheckPoint_ = 0.0;
415 lastPeriodicCheckPoint_ = startTime;
416 isCheckPoint_ =
false;
425 const auto newTime = this->
time()+dt;
429 if (periodicCheckPoints_ && Dune::FloatCmp::eq(newTime - lastPeriodicCheckPoint_, deltaPeriodicCheckPoint_, 1e-7))
431 lastPeriodicCheckPoint_ += deltaPeriodicCheckPoint_;
432 isCheckPoint_ =
true;
436 else if (!checkPoints_.empty() && Dune::FloatCmp::eq(newTime - checkPoints_.front(), 0.0, 1e-7))
439 isCheckPoint_ =
true;
445 isCheckPoint_ =
false;
465 const auto threshold = 0.2*nextDt;
466 const auto nextTime = this->
time() + nextDt;
468 if (periodicCheckPoints_ && Dune::FloatCmp::le(lastPeriodicCheckPoint_ + deltaPeriodicCheckPoint_ - nextTime, threshold))
469 nextDt = lastPeriodicCheckPoint_ + deltaPeriodicCheckPoint_ - this->
time();
471 if (!checkPoints_.empty() && Dune::FloatCmp::le(checkPoints_.front() - nextTime, threshold))
472 nextDt = checkPoints_.front() - this->
time();
474 assert(nextDt > 0.0);
487 const auto maxCheckPointDt = computeStepSizeRespectingCheckPoints_();
489 return min(maxDtParent, maxCheckPointDt);
505 if (signbit(interval))
506 DUNE_THROW(Dune::InvalidStateException,
"Interval has to be positive!");
508 periodicCheckPoints_ =
true;
509 deltaPeriodicCheckPoint_ = interval;
510 lastPeriodicCheckPoint_ = offset;
511 while (lastPeriodicCheckPoint_ + interval - this->
time() < 1e-14*interval)
512 lastPeriodicCheckPoint_ += interval;
515 std::cout << Fmt::format(
"Enabled periodic check points every {:.5g} seconds ", interval)
516 << Fmt::format(
"with the next check point at {:.5g} seconds.\n", lastPeriodicCheckPoint_ + interval);
519 if (Dune::FloatCmp::eq(this->
time()-lastPeriodicCheckPoint_, 0.0, 1e-7))
520 isCheckPoint_ =
true;
528 { periodicCheckPoints_ =
false; }
533 periodicCheckPoints_ =
false;
534 while (!checkPoints_.empty())
543 {
return isCheckPoint_; }
576 template<
class ForwardIterator>
580 for (; first != last; ++first)
581 setCheckPoint_(*first);
589 void setCheckPoint_(Scalar t)
594 std::cerr << Fmt::format(
"Couldn't insert checkpoint at t = {:.5g} ", t)
595 << Fmt::format(
"because that's in the past! (current simulation time is {:.5g})\n", this->
time());
599 if (!checkPoints_.empty())
601 if (t < checkPoints_.back())
604 std::cerr <<
"Couldn't insert checkpoint as it is earlier than the last check point in the queue.\n"
605 <<
"Checkpoints can only be inserted in ascending order." << std::endl;
610 checkPoints_.push(t);
612 std::cout << Fmt::format(
"Set check point at t = {:.5g} seconds.\n", t);
618 Scalar computeStepSizeRespectingCheckPoints_()
const
621 auto maxDt = std::numeric_limits<Scalar>::max();
623 if (periodicCheckPoints_)
624 maxDt = min(maxDt, lastPeriodicCheckPoint_ + deltaPeriodicCheckPoint_ - this->
time());
626 if (!checkPoints_.empty())
627 maxDt = min(maxDt, checkPoints_.front() - this->time());
632 bool periodicCheckPoints_;
633 Scalar deltaPeriodicCheckPoint_;
634 Scalar lastPeriodicCheckPoint_;
635 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.
Adaption of the non-isothermal two-phase two-component flow model to problems with CO2.
Definition: adapt.hh:29
Manages the handling of time dependent problems.
Definition: common/timeloop.hh:68
virtual ~TimeLoopBase()
Abstract base class needs virtual constructor.
Definition: common/timeloop.hh:71
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:113
Scalar maxTimeStepSize() const override
The current maximum time step size.
Definition: common/timeloop.hh:332
void setMaxTimeStepSize(Scalar maxDt)
Set the maximum time step size to a given value.
Definition: common/timeloop.hh:270
void resetTimer()
Reset the timer.
Definition: common/timeloop.hh:146
TimeLoop(Scalar startTime, Scalar dt, Scalar tEnd, bool verbose=true)
Definition: common/timeloop.hh:115
void setVerbose(bool verbose=true)
Sets time loop verbosity.
Definition: common/timeloop.hh:381
void setTime(Scalar t, int stepIdx)
Set the current simulated time and the time step index.
Definition: common/timeloop.hh:210
Scalar time_
Definition: common/timeloop.hh:390
int timeStepIndex() const
Returns number of time steps which have been executed since the beginning of the simulation.
Definition: common/timeloop.hh:288
double stop()
Tells the time loop to stop tracking the time.
Definition: common/timeloop.hh:138
Scalar endTime_
Definition: common/timeloop.hh:391
Scalar previousTimeStepSize_
Definition: common/timeloop.hh:394
void advanceTimeStep() override
Advance time step.
Definition: common/timeloop.hh:180
int timeStepIdx_
Definition: common/timeloop.hh:397
void start()
Tells the time loop to start tracking the time.
Definition: common/timeloop.hh:129
Scalar userSetMaxTimeStepSize_
Definition: common/timeloop.hh:395
void setFinished(bool finished=true)
Specify whether the simulation is finished.
Definition: common/timeloop.hh:304
bool verbose() const
If the time loop has verbose output.
Definition: common/timeloop.hh:377
void finalize(const Communicator &comm=Dune::MPIHelper::getCommunication())
Print final status and stops tracking the time.
Definition: common/timeloop.hh:362
bool verbose_
Definition: common/timeloop.hh:399
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:322
Scalar previousTimeStepSize() const
The previous time step size.
Definition: common/timeloop.hh:294
void reset(Scalar startTime, Scalar dt, Scalar tEnd, bool verbose=true)
Reset the time loop.
Definition: common/timeloop.hh:154
Scalar endTime() const
Returns the number of (simulated) seconds which the simulation runs.
Definition: common/timeloop.hh:224
void setTimeStepSize(Scalar dt) final
Set the current time step size to a given value.
Definition: common/timeloop.hh:255
bool finished() const override
Returns true if the simulation is finished.
Definition: common/timeloop.hh:313
Scalar timeStepSize_
Definition: common/timeloop.hh:393
void reportTimeStep() const
State info on cpu time.
Definition: common/timeloop.hh:345
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:281
bool finished_
Definition: common/timeloop.hh:398
Dune::Timer timer_
Definition: common/timeloop.hh:389
void setTime(Scalar t)
Set the current simulated time, don't change the current time step index.
Definition: common/timeloop.hh:201
void setEndTime(Scalar t)
Set the time of simulated seconds at which the simulation runs.
Definition: common/timeloop.hh:232
double wallClockTime() const
Returns the current wall clock time (cpu time) spend in this time loop.
Definition: common/timeloop.hh:242
Scalar timeAfterLastTimeStep_
Definition: common/timeloop.hh:396
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:218
Scalar timeStepWallClockTime_
Definition: common/timeloop.hh:396
A time loop with a check point mechanism.
Definition: common/timeloop.hh:408
bool isCheckPoint() const
Whether now is a time checkpoint.
Definition: common/timeloop.hh:542
void setPeriodicCheckPoint(Scalar interval, Scalar offset=0.0)
Set a periodic check point.
Definition: common/timeloop.hh:502
void setCheckPoint(Scalar t)
add a checkpoint to the queue
Definition: common/timeloop.hh:551
void advanceTimeStep() override
Advance time step.
Definition: common/timeloop.hh:422
void removeAllCheckPoints()
remove all check points
Definition: common/timeloop.hh:531
void setCheckPoint(const std::vector< Scalar > &checkPoints)
add checkpoints to the queue from a vector of time points
Definition: common/timeloop.hh:566
Scalar maxTimeStepSize() const override
The current maximum time step size.
Definition: common/timeloop.hh:484
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:577
CheckPointTimeLoop(Scalar startTime, Scalar dt, Scalar tEnd, bool verbose=true)
Definition: common/timeloop.hh:410
void disablePeriodicCheckPoints()
disable periodic check points
Definition: common/timeloop.hh:527