Version: SMASH-1.7
smash::UniformClock Class Reference

#include <clock.h>

Clock with uniformly spaced time steps.

Internals

Clock stores a time step size \(\Delta t\) and a base time \(t_0\) as well as a counter \(n\). The current time is calculated from \(t = t_0 + n \cdot \Delta t\). When \(\Delta t\) is changed, \(t_0\) is reset.

Definition at line 170 of file clock.h.

Inheritance diagram for smash::UniformClock:
[legend]
Collaboration diagram for smash::UniformClock:
[legend]

Public Member Functions

 UniformClock ()=default
 default initializer: Timestep size is set to 0! More...
 
 UniformClock (const double time, const double dt)
 Initialize with base time and time step size. More...
 
double current_time () const override
 
double next_time () const override
 
double timestep_duration () const override
 
void set_timestep_duration (const double dt)
 Sets the time step size (and resets the counter). More...
 
void reset (const double start_time, const bool is_output_clock) override
 Resets the time to the starting time of an event. More...
 
void remove_times_in_past (double) override
 Remove output times before the starting time of the simulation if this is a custom clock. More...
 
template<typename T >
std::enable_if< std::is_floating_point< T >::value, Clock & >::type operator+= (T big_timestep)
 Advances the clock by an arbitrary timestep (multiple of 0.000001 fm/c). More...
 
Clockoperator+= (Representation advance_several_timesteps)
 advances the clock by an arbitrary number of ticks. More...
 
- Public Member Functions inherited from smash::Clock
Clockoperator++ ()
 Advances the clock by one tick. More...
 
Clockoperator+= (Representation advance_several_timesteps)
 advances the clock by an arbitrary number of ticks. More...
 
bool operator< (const Clock &rhs) const
 Compares the times between two clocks. More...
 
bool operator< (double time) const
 Compares the time of the clock against a fixed time. More...
 
bool operator> (double time) const
 Compares the time of the clock against a fixed time. More...
 
virtual ~Clock ()=default
 

Static Private Member Functions

static Representation convert (double x)
 Convert a double x into the internal int representation. More...
 
static double convert (Representation x)
 Convert an internal int value x into the double representation. More...
 

Private Attributes

Representation timestep_duration_ = 0u
 The time step size \(\Delta t\) in $10^{-3}$ fm. More...
 
Representation reset_time_ = 0
 The time of last reset (when counter_ was set to 0). More...
 

Static Private Attributes

static constexpr double resolution = 0.000001
 Defines the resolution of the clock (i.e. More...
 
static constexpr double to_double = resolution
 A multiplier transfering the internal integer to the real time. More...
 
static constexpr double from_double = 1. / resolution
 A multiplier transfering the real time to the internal integer. More...
 

Additional Inherited Members

- Public Types inherited from smash::Clock
using Representation = std::int64_t
 The type used for counting ticks/time. More...
 
- Protected Attributes inherited from smash::Clock
Representation counter_ = 0
 Internally used to count the number of time steps. More...
 

Constructor & Destructor Documentation

smash::UniformClock::UniformClock ( )
default

default initializer: Timestep size is set to 0!

smash::UniformClock::UniformClock ( const double  time,
const double  dt 
)
inline

Initialize with base time and time step size.

Parameters
[in]timebase time
[in]dtstep size

Definition at line 194 of file clock.h.

196  if (dt < 0.) {
197  throw std::range_error("No negative time increment allowed");
198  }
199  }
Representation reset_time_
The time of last reset (when counter_ was set to 0).
Definition: clock.h:310
static Representation convert(double x)
Convert a double x into the internal int representation.
Definition: clock.h:301
Representation timestep_duration_
The time step size in $10^{-3}$ fm.
Definition: clock.h:308

Member Function Documentation

double smash::UniformClock::current_time ( ) const
inlineoverridevirtual
Returns
the current time.

Implements smash::Clock.

Definition at line 201 of file clock.h.

201  {
203  }
Representation reset_time_
The time of last reset (when counter_ was set to 0).
Definition: clock.h:310
Representation counter_
Internally used to count the number of time steps.
Definition: clock.h:156
static Representation convert(double x)
Convert a double x into the internal int representation.
Definition: clock.h:301
Representation timestep_duration_
The time step size in $10^{-3}$ fm.
Definition: clock.h:308
double smash::UniformClock::next_time ( ) const
inlineoverridevirtual
Returns
the time in the next tick.

This function is needed, because current_time() + timestep_duration() is not the same as the next tick (numerically; this is due to floating point arithmetic).

Implements smash::Clock.

Definition at line 211 of file clock.h.

211  {
213  std::numeric_limits<Representation>::max() - timestep_duration_) {
214  throw std::overflow_error("Too many timesteps, clock overflow imminent");
215  }
217  }
Representation reset_time_
The time of last reset (when counter_ was set to 0).
Definition: clock.h:310
Representation counter_
Internally used to count the number of time steps.
Definition: clock.h:156
static Representation convert(double x)
Convert a double x into the internal int representation.
Definition: clock.h:301
Representation timestep_duration_
The time step size in $10^{-3}$ fm.
Definition: clock.h:308
double smash::UniformClock::timestep_duration ( ) const
inlineoverridevirtual
Returns
the time step size.

Implements smash::Clock.

Definition at line 219 of file clock.h.

219  {
220  return convert(timestep_duration_);
221  }
static Representation convert(double x)
Convert a double x into the internal int representation.
Definition: clock.h:301
Representation timestep_duration_
The time step size in $10^{-3}$ fm.
Definition: clock.h:308
void smash::UniformClock::set_timestep_duration ( const double  dt)
inline

Sets the time step size (and resets the counter).

Parameters
[in]dtnew time step size

Definition at line 227 of file clock.h.

227  {
228  if (dt < 0.) {
229  throw std::range_error("No negative time increment allowed");
230  }
232  counter_ = 0;
234  }
Representation reset_time_
The time of last reset (when counter_ was set to 0).
Definition: clock.h:310
Representation counter_
Internally used to count the number of time steps.
Definition: clock.h:156
static Representation convert(double x)
Convert a double x into the internal int representation.
Definition: clock.h:301
Representation timestep_duration_
The time step size in $10^{-3}$ fm.
Definition: clock.h:308
void smash::UniformClock::reset ( const double  start_time,
const bool  is_output_clock 
)
inlineoverridevirtual

Resets the time to the starting time of an event.

Parameters
[in]start_timeStarting time of the simulation
[in]is_output_clockwhether this is an output clock or a lab clock

Implements smash::Clock.

Definition at line 242 of file clock.h.

242  {
243  double reset_time;
244  if (is_output_clock) {
245  reset_time =
246  std::floor(start_time / timestep_duration()) * timestep_duration();
247  } else {
248  reset_time = start_time;
249  }
250  if (reset_time < current_time()) {
251  logger<LogArea::Clock>().debug("Resetting clock from", current_time(),
252  " fm/c to ", reset_time, " fm/c");
253  }
254  reset_time_ = convert(reset_time);
255  counter_ = 0;
256  }
Representation reset_time_
The time of last reset (when counter_ was set to 0).
Definition: clock.h:310
double current_time() const override
Definition: clock.h:201
Representation counter_
Internally used to count the number of time steps.
Definition: clock.h:156
double timestep_duration() const override
Definition: clock.h:219
static Representation convert(double x)
Convert a double x into the internal int representation.
Definition: clock.h:301

Here is the call graph for this function:

void smash::UniformClock::remove_times_in_past ( double  start_time)
inlineoverridevirtual

Remove output times before the starting time of the simulation if this is a custom clock.

Parameters
[in]start_timestarting time of the simulation

Implements smash::Clock.

Definition at line 258 of file clock.h.

258 {};
template<typename T >
std::enable_if<std::is_floating_point<T>::value, Clock&>::type smash::UniformClock::operator+= ( big_timestep)
inline

Advances the clock by an arbitrary timestep (multiple of 0.000001 fm/c).

Template Parameters
Ttype of the timestep
Parameters
[in]big_timestepTimestep by which the clock is advanced.
Note
It uses a template parameter only for disambiguation with the overload below.

Definition at line 270 of file clock.h.

270  {
271  if (big_timestep < 0.) {
272  throw std::range_error("The clock cannot be turned back.");
273  }
274  reset_time_ += convert(big_timestep);
275  return *this;
276  }
Representation reset_time_
The time of last reset (when counter_ was set to 0).
Definition: clock.h:310
static Representation convert(double x)
Convert a double x into the internal int representation.
Definition: clock.h:301
Clock& smash::UniformClock::operator+= ( Representation  advance_several_timesteps)
inline

advances the clock by an arbitrary number of ticks.

Parameters
[in]advance_several_timestepsNumber of the timesteps added to the clock
Exceptions
OverflowErrorif the number of the added timesteps exceeds the maximum value.

Definition at line 285 of file clock.h.

285  {
286  if (counter_ >= std::numeric_limits<Representation>::max() -
287  advance_several_timesteps) {
288  throw std::overflow_error("Too many timesteps, clock overflow imminent");
289  }
290  counter_ += advance_several_timesteps;
291  return *this;
292  }
Representation counter_
Internally used to count the number of time steps.
Definition: clock.h:156
static Representation smash::UniformClock::convert ( double  x)
inlinestaticprivate

Convert a double x into the internal int representation.

Definition at line 301 of file clock.h.

301  {
302  return std::round(x * from_double);
303  }
static constexpr double from_double
A multiplier transfering the real time to the internal integer.
Definition: clock.h:298
static double smash::UniformClock::convert ( Representation  x)
inlinestaticprivate

Convert an internal int value x into the double representation.

Definition at line 305 of file clock.h.

305 { return x * to_double; }
static constexpr double to_double
A multiplier transfering the internal integer to the real time.
Definition: clock.h:296

Member Data Documentation

constexpr double smash::UniformClock::resolution = 0.000001
staticprivate

Defines the resolution of the clock (i.e.

the smallest representable time difference).

The value 0.000001 is very well suited because

  • It should be \(10^{-n},\;n\in\mathbb{N}\). That's because we want to use it to convert user input/output and that's in decimal representation.
  • The floating-point representation of the constant should have a small error. 0.000001 has the smallest error (i.e. 0.022 ulp) in the range \(1\leq n \leq 10\). The small error helps to convert the internal integer representation as precise as possible to floating-point.

Definition at line 183 of file clock.h.

constexpr double smash::UniformClock::to_double = resolution
staticprivate

A multiplier transfering the internal integer to the real time.

Definition at line 296 of file clock.h.

constexpr double smash::UniformClock::from_double = 1. / resolution
staticprivate

A multiplier transfering the real time to the internal integer.

Definition at line 298 of file clock.h.

Representation smash::UniformClock::timestep_duration_ = 0u
private

The time step size \(\Delta t\) in $10^{-3}$ fm.

Definition at line 308 of file clock.h.

Representation smash::UniformClock::reset_time_ = 0
private

The time of last reset (when counter_ was set to 0).

Definition at line 310 of file clock.h.


The documentation for this class was generated from the following file: