10 #ifndef SRC_INCLUDE_CLOCK_H_ 11 #define SRC_INCLUDE_CLOCK_H_ 85 virtual void reset(
double start_time,
const bool is_output_clock) = 0;
103 if (
counter_ >= std::numeric_limits<Representation>::max() - 1) {
104 throw std::overflow_error(
"Too many timesteps, clock overflow imminent");
119 if (
counter_ >= std::numeric_limits<Representation>::max() -
120 advance_several_timesteps) {
121 throw std::overflow_error(
"Too many timesteps, clock overflow imminent");
123 counter_ += advance_several_timesteps;
150 virtual ~Clock() =
default;
183 static constexpr
double resolution = 0.000001;
195 : timestep_duration_(convert(dt)), reset_time_(convert(time)) {
197 throw std::range_error(
"No negative time increment allowed");
202 return convert(reset_time_ + timestep_duration_ *
counter_);
212 if (
counter_ * timestep_duration_ >=
213 std::numeric_limits<Representation>::max() - timestep_duration_) {
214 throw std::overflow_error(
"Too many timesteps, clock overflow imminent");
216 return convert(reset_time_ + timestep_duration_ * (
counter_ + 1));
220 return convert(timestep_duration_);
229 throw std::range_error(
"No negative time increment allowed");
231 reset_time_ += timestep_duration_ *
counter_;
233 timestep_duration_ = convert(dt);
242 void reset(
const double start_time,
const bool is_output_clock)
override {
244 if (is_output_clock) {
248 reset_time = start_time;
251 logger<LogArea::Clock>().debug(
"Resetting clock from",
current_time(),
252 " fm/c to ", reset_time,
" fm/c");
254 reset_time_ = convert(reset_time);
268 template <
typename T>
269 typename std::enable_if<std::is_floating_point<T>::value,
Clock&>::type
271 if (big_timestep < 0.) {
272 throw std::range_error(
"The clock cannot be turned back.");
274 reset_time_ += convert(big_timestep);
286 if (
counter_ >= std::numeric_limits<Representation>::max() -
287 advance_several_timesteps) {
288 throw std::overflow_error(
"Too many timesteps, clock overflow imminent");
290 counter_ += advance_several_timesteps;
296 static constexpr
double to_double = resolution;
298 static constexpr
double from_double = 1. / resolution;
302 return std::round(x * from_double);
322 std::sort(custom_times_.begin(), custom_times_.end());
331 throw std::runtime_error(
"Trying to access undefined zeroth output time");
349 custom_times_.begin(), custom_times_.end(), [start_time](
double t) {
350 if (t <= start_time) {
351 logger<LogArea::Clock>().warn(
"Removing custom output time ", t,
352 " fm since it is earlier than the " 353 "starting time of the simulation");
367 #endif // SRC_INCLUDE_CLOCK_H_ std::vector< double > custom_times_
Vector of times where output is generated.
bool operator<(const Clock &rhs) const
Compares the times between two clocks.
Clock & operator+=(Representation advance_several_timesteps)
advances the clock by an arbitrary number of ticks.
virtual double next_time() const =0
Representation counter_
Internally used to count the number of time steps.
Clock with explicitly defined time steps.
virtual double current_time() const =0
double timestep_duration() const override
CustomClock(std::vector< double > times)
Initialises a custom clock with explicitly given output times.
Clock & operator++()
Advances the clock by one tick.
std::int64_t Representation
The type used for counting ticks/time.
Clock tracks the time in the simulation.
double next_time() const override
double current_time() const override
virtual void reset(double start_time, const bool is_output_clock)=0
reset the clock to the starting time of the simulation
virtual void remove_times_in_past(double start_time)=0
Remove output times before the starting time of the simulation if this is a custom clock...
void remove_times_in_past(double start_time) override
Remove all custom times before start_time.
void reset(double, bool) override
reset the clock to the starting time of the simulation
bool operator>(double time) const
Compares the time of the clock against a fixed time.
virtual double timestep_duration() const =0
bool operator<(double time) const
Compares the time of the clock against a fixed time.