From c69208047c811ee018d6511a2cfb9e4fc44058e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20Gl=C3=A4ser?= <dennis.glaeser@iws.uni-stuttgart.de> Date: Thu, 28 Sep 2023 17:55:49 +0200 Subject: [PATCH] [timeloop] allow resetting from durations --- dumux/common/timeloop.hh | 22 ++++++++++++++++++++++ test/common/timeloop/test_timeloop.cc | 7 +++++++ 2 files changed, 29 insertions(+) diff --git a/dumux/common/timeloop.hh b/dumux/common/timeloop.hh index 88696f464f..85627a0bc9 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 e709efaba3..47a8fbc88a 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}); -- GitLab