Version: SMASH-3.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 175 of file clock.h.

Inheritance diagram for smash::UniformClock:
smash::Clock

Public Member Functions

 UniformClock ()=default
 default initializer: Timestep size is set to 0! More...
 
 UniformClock (const double time, const double dt, const double time_end)
 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). 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^{-6}\,\mathrm{fm}\). More...
 
Representation reset_time_ = 0
 The time of last reset (when counter_ was set to 0). More...
 
Representation time_end_ = 0
 The end time of the particle propagation. 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,
const double  time_end 
)
inline

Initialize with base time and time step size.

Parameters
[in]timebase time
[in]dtstep size
[in]time_endend time of particle propagation

Definition at line 200 of file clock.h.

202  reset_time_(convert(time)),
203  time_end_(convert(time_end)) {
204  if (dt < 0.) {
205  throw std::range_error("No negative time increment allowed");
206  }
207  }
Representation timestep_duration_
The time step size in .
Definition: clock.h:330
Representation time_end_
The end time of the particle propagation.
Definition: clock.h:334
static Representation convert(double x)
Convert a double x into the internal int representation.
Definition: clock.h:323
Representation reset_time_
The time of last reset (when counter_ was set to 0).
Definition: clock.h:332

Member Function Documentation

◆ current_time()

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

Implements smash::Clock.

Definition at line 209 of file clock.h.

209  {
211  return convert(time_end_);
212  } else {
214  }
215  }
Representation counter_
Internally used to count the number of time steps.
Definition: clock.h:161

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

223  {
225  std::numeric_limits<Representation>::max() - timestep_duration_) {
226  throw std::overflow_error("Too many timesteps, clock overflow imminent");
227  }
228  if ((reset_time_ + timestep_duration_ * (counter_ + 1)) > time_end_) {
229  return convert(time_end_);
230  } else {
232  }
233  }

◆ timestep_duration()

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

Implements smash::Clock.

Definition at line 235 of file clock.h.

235  {
236  if ((reset_time_ + timestep_duration_ * (counter_ + 1)) > time_end_) {
237  Representation last_timestep =
239  return convert(last_timestep);
240  } else {
241  return convert(timestep_duration_);
242  }
243  }
std::int64_t Representation
The type used for counting ticks/time.
Definition: clock.h:76

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

249  {
250  if (dt < 0.) {
251  throw std::range_error("No negative time increment allowed");
252  }
254  counter_ = 0;
256  }

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

264  {
265  double reset_time;
266  if (is_output_clock) {
267  reset_time =
268  std::floor(start_time / timestep_duration()) * timestep_duration();
269  } else {
270  reset_time = start_time;
271  }
272  if (reset_time < current_time()) {
273  logg[LClock].debug("Resetting clock from", current_time(), " fm to ",
274  reset_time, " fm");
275  }
276  reset_time_ = convert(reset_time);
277  counter_ = 0;
278  }
double timestep_duration() const override
Definition: clock.h:235
double current_time() const override
Definition: clock.h:209
std::array< einhard::Logger<>, std::tuple_size< LogArea::AreaTuple >::value > logg
An array that stores all pre-configured Logger objects.
Definition: logging.cc:39
static constexpr int LClock
Definition: clock.h:25

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

280 {};

◆ 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).

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

292  {
293  if (big_timestep < 0.) {
294  throw std::range_error("The clock cannot be turned back.");
295  }
296  reset_time_ += convert(big_timestep);
297  return *this;
298  }

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

307  {
308  if (counter_ >= std::numeric_limits<Representation>::max() -
309  advance_several_timesteps) {
310  throw std::overflow_error("Too many timesteps, clock overflow imminent");
311  }
312  counter_ += advance_several_timesteps;
313  return *this;
314  }

◆ convert() [1/2]

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

Convert a double x into the internal int representation.

Definition at line 323 of file clock.h.

323  {
324  return std::round(x * from_double);
325  }
static constexpr double from_double
A multiplier transfering the real time to the internal integer.
Definition: clock.h:320

◆ convert() [2/2]

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

Convert an internal int value x into the double representation.

Definition at line 327 of file clock.h.

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

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 188 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 318 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 320 of file clock.h.

◆ timestep_duration_

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

The time step size \(\Delta t\) in \(10^{-6}\,\mathrm{fm}\).

Definition at line 330 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 332 of file clock.h.

◆ time_end_

Representation smash::UniformClock::time_end_ = 0
private

The end time of the particle propagation.

Definition at line 334 of file clock.h.


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