Version: SMASH-1.8
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 172 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

◆ UniformClock() [1/2]

smash::UniformClock::UniformClock ( )
default

default initializer: Timestep size is set to 0!

◆ UniformClock() [2/2]

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 196 of file clock.h.

198  if (dt < 0.) {
199  throw std::range_error("No negative time increment allowed");
200  }
201  }

Member Function Documentation

◆ current_time()

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

Implements smash::Clock.

Definition at line 203 of file clock.h.

203  {
205  }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ next_time()

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 213 of file clock.h.

213  {
215  std::numeric_limits<Representation>::max() - timestep_duration_) {
216  throw std::overflow_error("Too many timesteps, clock overflow imminent");
217  }
219  }
Here is the call graph for this function:

◆ timestep_duration()

double smash::UniformClock::timestep_duration ( ) const
inlineoverridevirtual
Returns
the time step size.

Implements smash::Clock.

Definition at line 221 of file clock.h.

221  {
222  return convert(timestep_duration_);
223  }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ set_timestep_duration()

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 229 of file clock.h.

229  {
230  if (dt < 0.) {
231  throw std::range_error("No negative time increment allowed");
232  }
234  counter_ = 0;
236  }
Here is the call graph for this function:

◆ reset()

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 244 of file clock.h.

244  {
245  double reset_time;
246  if (is_output_clock) {
247  reset_time =
248  std::floor(start_time / timestep_duration()) * timestep_duration();
249  } else {
250  reset_time = start_time;
251  }
252  if (reset_time < current_time()) {
253  logg[LClock].debug("Resetting clock from", current_time(), " fm/c to ",
254  reset_time, " fm/c");
255  }
256  reset_time_ = convert(reset_time);
257  counter_ = 0;
258  }
Here is the call graph for this function:

◆ remove_times_in_past()

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 260 of file clock.h.

260 {};

◆ operator+=() [1/2]

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 272 of file clock.h.

272  {
273  if (big_timestep < 0.) {
274  throw std::range_error("The clock cannot be turned back.");
275  }
276  reset_time_ += convert(big_timestep);
277  return *this;
278  }
Here is the call graph for this function:

◆ operator+=() [2/2]

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 287 of file clock.h.

287  {
288  if (counter_ >= std::numeric_limits<Representation>::max() -
289  advance_several_timesteps) {
290  throw std::overflow_error("Too many timesteps, clock overflow imminent");
291  }
292  counter_ += advance_several_timesteps;
293  return *this;
294  }

◆ convert() [1/2]

static Representation smash::UniformClock::convert ( double  x)
inlinestaticprivate

Convert a double x into the internal int representation.

Definition at line 303 of file clock.h.

303  {
304  return std::round(x * from_double);
305  }
Here is the caller graph for this function:

◆ convert() [2/2]

static double smash::UniformClock::convert ( Representation  x)
inlinestaticprivate

Convert an internal int value x into the double representation.

Definition at line 307 of file clock.h.

307 { return x * to_double; }

Member Data Documentation

◆ resolution

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

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 185 of file clock.h.

◆ to_double

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

A multiplier transfering the internal integer to the real time.

Definition at line 298 of file clock.h.

◆ from_double

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

A multiplier transfering the real time to the internal integer.

Definition at line 300 of file clock.h.

◆ timestep_duration_

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

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

Definition at line 310 of file clock.h.

◆ reset_time_

Representation smash::UniformClock::reset_time_ = 0
private

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

Definition at line 312 of file clock.h.


The documentation for this class was generated from the following file:
smash::UniformClock::to_double
static constexpr double to_double
A multiplier transfering the internal integer to the real time.
Definition: clock.h:298
smash::LClock
static constexpr int LClock
Definition: clock.h:24
smash::Clock::counter_
Representation counter_
Internally used to count the number of time steps.
Definition: clock.h:158
smash::UniformClock::current_time
double current_time() const override
Definition: clock.h:203
smash::logg
std::array< einhard::Logger<>, std::tuple_size< LogArea::AreaTuple >::value > logg
An array that stores all pre-configured Logger objects.
Definition: logging.cc:39
smash::UniformClock::timestep_duration
double timestep_duration() const override
Definition: clock.h:221
smash::UniformClock::timestep_duration_
Representation timestep_duration_
The time step size in $10^{-3}$ fm.
Definition: clock.h:310
smash::UniformClock::reset_time_
Representation reset_time_
The time of last reset (when counter_ was set to 0).
Definition: clock.h:312
smash::UniformClock::convert
static Representation convert(double x)
Convert a double x into the internal int representation.
Definition: clock.h:303
smash::UniformClock::from_double
static constexpr double from_double
A multiplier transfering the real time to the internal integer.
Definition: clock.h:300