diff --git a/dumux/common/timeloop.hh b/dumux/common/timeloop.hh index 88696f464f2ef4985388ef094aedbb9dec6749a0..85627a0bc96594b6f2d2fe78ae066f64dd773be7 100644 --- a/dumux/common/timeloop.hh +++ b/dumux/common/timeloop.hh @@ -42,6 +42,10 @@ Scalar toSeconds(std::chrono::duration<R, P> duration) return std::chrono::duration_cast<Second>(duration).count(); } +template<typename Scalar, typename T, std::enable_if_t<std::is_floating_point_v<T>, bool> = true> +Scalar toSeconds(T duration) +{ return static_cast<Scalar>(duration); } + } // namespace Detail #endif // DOXYGEN @@ -176,6 +180,24 @@ public: timer_.reset(); } + /*! + * \brief Reset the time loop + */ + template<class Rep1, class Period1, + class Rep2, class Period2, + class Rep3, class Period3> + void reset(std::chrono::duration<Rep1, Period1> startTime, + std::chrono::duration<Rep2, Period2> dt, + std::chrono::duration<Rep3, Period3> tEnd, + bool verbose = true) + { + reset( + Detail::toSeconds<Scalar>(startTime), + Detail::toSeconds<Scalar>(dt), + Detail::toSeconds<Scalar>(tEnd) + ); + } + /*! * \brief Reset the time loop */ diff --git a/test/common/timeloop/test_timeloop.cc b/test/common/timeloop/test_timeloop.cc index e709efaba31129696b9cea1b0586f9ff3e0db172..47a8fbc88a263c45db067fe4ad2e0428e0c7c8cd 100644 --- a/test/common/timeloop/test_timeloop.cc +++ b/test/common/timeloop/test_timeloop.cc @@ -226,6 +226,13 @@ void testWithDurations() if (!Dune::FloatCmp::eq(timeLoop.timeStepSize(), 0.0001, 1e-10)) DUNE_THROW(Dune::InvalidStateException, "Unexpected time step size"); std::cout << "Setting dt from a duration successful" << std::endl; + + timeLoop.reset(1ms, 0.5h, 2h); + if (!Dune::FloatCmp::eq(timeLoop.timeStepSize(), 1800.0, 1e-10)) + DUNE_THROW(Dune::InvalidStateException, "Resetting time step size failed"); + if (!Dune::FloatCmp::eq(timeLoop.endTime(), 7200.0, 1e-10)) + DUNE_THROW(Dune::InvalidStateException, "Resetting end time failed"); + std::cout << "Resetting from durations successful" << std::endl; }; _test(Dumux::TimeLoop<double>{0s, 1ms, 1h});