Version: SMASH-3.1
smash::UniformClock Class Reference

#include <clock.h>

Clock with uniformly spaced time steps.

Internal clock mechanisms

This clock stores a time step size \(\Delta t\), a base time \(t_0\) as well as an end time \(t_{end}\) and 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 to the present time and \(n\) is set to 0. As soon as \(t\geq t_{end}\), the clock will always return \(t_{end}\) as current and next time. The time step size can be retrieved and the returned value is

\[ \begin{aligned} \Delta t \qquad&\mbox{if}& &t\leq t_{end}-\Delta t \\ t_{end}-t \qquad&\mbox{if}& t_{end}-\Delta t<{}&t<t_{end} \\ 0.0 \qquad&\mbox{if}& &t\geq t_{end} \\ \end{aligned} \]

In the last case, i.e. if the time step size is required when the clock ticked beyond the simulation end, a warning is given to the user.

Definition at line 209 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 (double time, double dt, 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 (double dt)
 Sets the time step size (and resets the counter). More...
 
void reset (double start_time, 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 internal times of two clocks. More...
 
bool operator< (double time) const
 Compares the internal time of the clock against a fixed time. More...
 
bool operator> (double time) const
 Compares the internal time of the clock against a fixed time. More...
 
virtual ~Clock ()=default
 

Protected Member Functions

double present_internal_time () const override
 Access the internal time of the clock, independently from the end time. More...
 

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_ = 0
 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 (namely the smallest representable time difference). More...
 
static constexpr double to_double = resolution
 A multiplier transferring the internal integer to the real time. More...
 
static constexpr double from_double = 1. / resolution
 A multiplier transferring 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 ( double  time,
double  dt,
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 234 of file clock.h.

236  reset_time_(convert(time)),
237  time_end_(convert(time_end)) {
238  if (dt <= 0.) {
239  throw std::range_error("Time increment must be positive and non-zero");
240  }
241  if (reset_time_ >= time_end_) {
242  throw std::range_error(
243  "The initial time of UniformClock must be smaller than the end time. "
244  "(Attempt to set initial time to " +
245  std::to_string(time) + " and end time to " +
246  std::to_string(time_end) + " not possible)");
247  }
248  }
Representation timestep_duration_
The time step size in .
Definition: clock.h:396
Representation time_end_
The end time of the particle propagation.
Definition: clock.h:400
static Representation convert(double x)
Convert a double x into the internal int representation.
Definition: clock.h:389
Representation reset_time_
The time of last reset (when counter_ was set to 0).
Definition: clock.h:398

Member Function Documentation

◆ current_time()

double smash::UniformClock::current_time ( ) const
inlineoverridevirtual
Returns
the current time or the end time if the clock ticked beyond it.

Implements smash::Clock.

Definition at line 252 of file clock.h.

252  {
253  auto present_time = present_internal_time();
254  // Do comparison in internal representation unit and return converted values
255  if (convert(present_time) > time_end_) {
256  return convert(time_end_);
257  } else {
258  return present_time;
259  }
260  }
double present_internal_time() const override
Access the internal time of the clock, independently from the end time.
Definition: clock.h:378

◆ next_time()

double smash::UniformClock::next_time ( ) const
inlineoverridevirtual
Returns
the time in the next tick or the end time if the clock ticked beyond it.
Note
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 269 of file clock.h.

269  {
271  std::numeric_limits<Representation>::max() - timestep_duration_) {
272  throw std::overflow_error("Too many timesteps, clock overflow imminent");
273  }
274  auto next_point_in_time = reset_time_ + timestep_duration_ * (counter_ + 1);
275  if (next_point_in_time > time_end_) {
276  return convert(time_end_);
277  } else {
278  return convert(next_point_in_time);
279  }
280  }
Representation counter_
Internally used to count the number of time steps.
Definition: clock.h:185

◆ timestep_duration()

double smash::UniformClock::timestep_duration ( ) const
inlineoverridevirtual
Returns
the time step size from the current time. If a full tick would result in a time larger then the end time, a smaller size is returned. If the clock is already beyond the end time, 0.0 is returned and a warning is printed.
See also
UniformClock description.

Implements smash::Clock.

Definition at line 288 of file clock.h.

288  {
289  auto present_time = convert(present_internal_time());
290  if (present_time > time_end_) {
291  logg[LClock].warn() << "UniformClock asked for timestep duration beyond "
292  "end of simulation, returning 0.";
293  return 0.0;
294  } else if (present_time + timestep_duration_ > time_end_) {
295  return convert(time_end_ - present_time);
296  } else {
297  return convert(timestep_duration_);
298  }
299  }
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

◆ set_timestep_duration()

void smash::UniformClock::set_timestep_duration ( double  dt)
inline

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

Parameters
[in]dtnew time step size

Definition at line 305 of file clock.h.

305  {
306  if (dt <= 0.) {
307  throw std::range_error("Time increment must be positive and non-zero!");
308  }
310  counter_ = 0;
312  }

◆ reset()

void smash::UniformClock::reset ( double  start_time,
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 320 of file clock.h.

320  {
321  double reset_time;
322  if (is_output_clock) {
323  auto delta_t = convert(timestep_duration_);
324  reset_time = std::floor(start_time / delta_t) * delta_t;
325  } else {
326  reset_time = start_time;
327  }
328  if (reset_time < current_time()) {
329  logg[LClock].debug("Resetting clock from", current_time(), " fm to ",
330  reset_time, " fm");
331  }
332  reset_time_ = convert(reset_time);
333  counter_ = 0;
334  }
double current_time() const override
Definition: clock.h:252

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

336 {};

◆ 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_timestepTime step by which the clock is advanced.
Note
It uses a template parameter only for disambiguation with the overload below.

Definition at line 348 of file clock.h.

348  {
349  if (big_timestep < 0.) {
350  throw std::range_error("The clock cannot be turned back.");
351  }
352  reset_time_ += convert(big_timestep);
353  return *this;
354  }

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

363  {
364  if (counter_ >= std::numeric_limits<Representation>::max() -
365  advance_several_timesteps) {
366  throw std::overflow_error("Too many timesteps, clock overflow imminent");
367  }
368  counter_ += advance_several_timesteps;
369  return *this;
370  }

◆ present_internal_time()

double smash::UniformClock::present_internal_time ( ) const
inlineoverrideprotectedvirtual

Access the internal time of the clock, independently from the end time.

Returns
the internal clock time.

Implements smash::Clock.

Definition at line 378 of file clock.h.

378  {
380  }

◆ convert() [1/2]

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

Convert a double x into the internal int representation.

Definition at line 389 of file clock.h.

389  {
390  return std::round(x * from_double);
391  }
static constexpr double from_double
A multiplier transferring the real time to the internal integer.
Definition: clock.h:386

◆ convert() [2/2]

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

Convert an internal int value x into the double representation.

Definition at line 393 of file clock.h.

393 { return x * to_double; }
static constexpr double to_double
A multiplier transferring the internal integer to the real time.
Definition: clock.h:384

Member Data Documentation

◆ resolution

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

Defines the resolution of the clock (namely 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 222 of file clock.h.

◆ to_double

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

A multiplier transferring the internal integer to the real time.

Definition at line 384 of file clock.h.

◆ from_double

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

A multiplier transferring the real time to the internal integer.

Definition at line 386 of file clock.h.

◆ timestep_duration_

Representation smash::UniformClock::timestep_duration_ = 0
private

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

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

◆ time_end_

Representation smash::UniformClock::time_end_ = 0
private

The end time of the particle propagation.

Definition at line 400 of file clock.h.


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