Version: SMASH-2.0
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 173 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 197 of file clock.h.

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

Member Function Documentation

◆ current_time()

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

Implements smash::Clock.

Definition at line 204 of file clock.h.

204  {
206  }
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 214 of file clock.h.

214  {
216  std::numeric_limits<Representation>::max() - timestep_duration_) {
217  throw std::overflow_error("Too many timesteps, clock overflow imminent");
218  }
220  }
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 222 of file clock.h.

222  {
223  return convert(timestep_duration_);
224  }
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 230 of file clock.h.

230  {
231  if (dt < 0.) {
232  throw std::range_error("No negative time increment allowed");
233  }
235  counter_ = 0;
237  }
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 245 of file clock.h.

245  {
246  double reset_time;
247  if (is_output_clock) {
248  reset_time =
249  std::floor(start_time / timestep_duration()) * timestep_duration();
250  } else {
251  reset_time = start_time;
252  }
253  if (reset_time < current_time()) {
254  logg[LClock].debug("Resetting clock from", current_time(), " fm/c to ",
255  reset_time, " fm/c");
256  }
257  reset_time_ = convert(reset_time);
258  counter_ = 0;
259  }
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 261 of file clock.h.

261 {};

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

273  {
274  if (big_timestep < 0.) {
275  throw std::range_error("The clock cannot be turned back.");
276  }
277  reset_time_ += convert(big_timestep);
278  return *this;
279  }
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 288 of file clock.h.

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

◆ convert() [1/2]

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

Convert a double x into the internal int representation.

Definition at line 304 of file clock.h.

304  {
305  return std::round(x * from_double);
306  }
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 308 of file clock.h.

308 { 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 186 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 299 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 301 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 311 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 313 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:299
smash::LClock
static constexpr int LClock
Definition: clock.h:25
smash::Clock::counter_
Representation counter_
Internally used to count the number of time steps.
Definition: clock.h:159
smash::UniformClock::current_time
double current_time() const override
Definition: clock.h:204
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:222
smash::UniformClock::timestep_duration_
Representation timestep_duration_
The time step size in $10^{-3}$ fm.
Definition: clock.h:311
smash::UniformClock::reset_time_
Representation reset_time_
The time of last reset (when counter_ was set to 0).
Definition: clock.h:313
smash::UniformClock::convert
static Representation convert(double x)
Convert a double x into the internal int representation.
Definition: clock.h:304
smash::UniformClock::from_double
static constexpr double from_double
A multiplier transfering the real time to the internal integer.
Definition: clock.h:301