Advances along with real time when active, and can execute tasks at appointed times. There are commands to pause, resume, and single-step the Clock, as well as set its time and speed relative to system time. Clock has a list of ClockTasks which it causes to be executed at the times specified by the ClockTasks. Clock has a parallel real time clock for measuring performance.

Clock time is used by a client object such as SimRunner to know how much to advance a simulation. This is how operations on Clock like pause, single-step, setting time rate, etc. affect the display of the Simulation. (When clock time is in the past, such as at time zero, then the client can restart the simulation from initial conditions.)

While a unit of simulation time can be interpreted to mean anything from a millisecond to a millenium, we use the Clock to advance the Simulation time along with real time as though each unit of time is equal to one second of real time.

Types of Time

There are several types of time considered here: system time, clock time, simulation time, and real time. All of these are measured in seconds, though simulation time might have a different meaning – see About Units Of Measurement.

  • System Time is given by SystemClock.systemTime. System time is the basis of the other time measurements. For example, clock time and real time each have a "system start time" by which they are measured. System time is always running.

  • Clock Time is given by getTime. Clock time advances at the current time rate (multiple of system time). Clock time can be modified directly by calling setTime. Clock time can be paused or resumed.

  • Simulation Time is given by Simulation.getTime. Simulation time is advanced by the client, usually to keep up with clock time. When performance problems occur, the clock time can be retarded via setTime to match the current simulation time.

  • Real Time is given by getRealTime. Closely related to clock time, real time is used to measure performance: how much the simulation time has slipped behind real time because the simulation couldn't compute quickly enough. Real time usually mirrors clock time – they are paused or resumed together and have the same time rate relative to system time – but real time is not affected by setTime. When performance problems happen the usual result is that clock time is retarded to match simulation time by setting clock time to an earlier value. In this case, real time is unaffected and will be ahead of clock time by the amount of time lost to performance problems.

ClockTask

A ClockTask contains a function which is to be executed at a particular time. The time is expressed in clock time.

ClockTasks are scheduled as a side effect of Clock methods such as setTime(), resume(), addTask(). ClockTasks are cancelled as a side effect of Clock methods such as pause(), removeTask().

A typical use of ClockTask is to restart the simulation after a few seconds, which makes the simulation repeatedly "loop" showing it's first few seconds.

Step Mode

The step method puts the Clock into a special step mode. Clients should check for this step mode by calling isStepping. Step mode being true means that clock time has advanced even though the clock is paused. The client should update the simulation to match the new clock time, and then call clearStepMode to indicate that it has advanced the simulation.

Parameters Created

Events Broadcast

All the Parameters are broadcast when their values change. In addition:

  • GenericEvent named CLOCK_PAUSE, see pause

  • GenericEvent named CLOCK_RESUME, see resume

  • GenericEvent named CLOCK_STEP, see step

  • GenericEvent named CLOCK_SET_TIME, see setTime

TO DO Should be able to have clock time (and therefore simulation time) start at something other than zero.

Hierarchy (view full)

Implements

Constructors

Properties

clockStart_sys_secs_: number

when 'zero clock time' occurs, in system time, in seconds

isRunning_: boolean = false

whether clock time is advancing

realStart_sys_secs_: number

when 'zero real time' occurs, in system time, in seconds

saveRealTime_secs_: number = 0

remembers the real time while clock is stopped, in seconds

saveTime_secs_: number = 0

remembers clock time while clock is stopped, in seconds

stepMode_: boolean = false

means we are currently in single-step mode: clock time has advanced even though clock is paused.

tasks_: ClockTask[] = []

array of ClockTasks to execute at their appointed times

timeRate_: number = 1.0

rate at which clock time advances compared to system time

CLOCK_PAUSE: "CLOCK_PAUSE" = 'CLOCK_PAUSE'

Name of the GenericEvent fired when the Clock is paused, see pause.

CLOCK_RESUME: "CLOCK_RESUME" = 'CLOCK_RESUME'

Name of the GenericEvent fired when the Clock is resumed, see resume.

CLOCK_SET_TIME: "CLOCK_SET_TIME" = 'CLOCK_SET_TIME'

Name of the GenericEvent fired when the Clock time is set, see setTime.

CLOCK_STEP: "CLOCK_STEP" = 'CLOCK_STEP'

Name of the GenericEvent fired when the Clock is stepped, see step.

Methods

  • Adds a ClockTask to the list of tasks which will be run, and schedules it to be run if its time is now or in the future. The time to run the task is specified in the ClockTask.

    Parameters

    • task: ClockTask

      the ClockTask to add to list of tasks to be run

    Returns void

  • Called during step mode, this indicates that the client has advanced the Simulation to match the clock time.

    Returns void

  • Converts clock time to system time. System time is defined by SystemClock.systemTime.

    Parameters

    • clockTime: number

      in seconds

    Returns number

    system time equivalent of clockTime

  • Schedules tasks for immediate execution that are in the given range of time from startTime to startTime + timeStep.

    Parameters

    • startTime: number
    • timeStep: number

    Returns void

  • Returns the real time in seconds which is in the same time scale as the clock time; used for checking simulation performance. Like clock time, real time starts at zero time; is paused when the Clock is paused; and runs at the same rate as clock time.

    When a simulation cannot keep up with real time the Clock is retarded by client code calling setTime to set clock time to an earlier time. In contrast, the real time is unaffected by setTime; therefore the difference between real time and clock time tells us how far behind real time the simulation is.

    When the simulation is reset, the clock is typically set to time zero. In that case the real time should be set to match clock time by using setRealTime.

    Returns number

    current real time in seconds

  • Returns the rate at which clock time passes compared to system time; a value of 2 makes clock time pass twice as fast as system time; a value of 0.5 makes clock time pass half as fast as system time.

    Returns number

    the rate at which clock time passes compared to system time

  • Whether the clock time and real time are advancing.

    Returns boolean

    true when clock time and real time are advancing

  • Returns true when in step mode which means that clock time has advanced even though the Clock is paused. The client should update the Simulation to match the new clock time, and call clearStepMode to indicate that the Simulation has advanced.

    Returns boolean

    true when in step mode

  • Removes the ClockTask from the list of tasks to be run, and cancels the task.

    Parameters

    Returns void

  • Resumes increasing clock time and real time. Schedules all ClockTasks that should run at or after the current clock time. Broadcasts a CLOCK_RESUME event.

    Returns void

  • Sets whether this Subject will broadcast events, typically used to temporarily disable broadcasting. Intended to be used in situations where a subclass overrides a method that broadcasts an event. This allows the subclass to prevent the superclass broadcasting that event, so that the subclass can broadcast the event when the method is completed.

    Parameters

    • value: boolean

      whether this Subject should broadcast events

    Returns boolean

    the previous value

  • Sets the real time to the given time in seconds. See getRealTime.

    Parameters

    • time_secs: number

      the time to set

    Returns void

  • Sets the clock time, in seconds. Also schedules all ClockTasks that should run at or after the given time. Broadcasts a CLOCK_SET_TIME event.

    Parameters

    • time_secs: number

      the time in seconds to set this Clock to

    Returns void

  • Sets the rate at which clock time passes compared to system time. A value of 2 makes clock time pass twice as fast as system time; a value of 0.5 makes clock time pass half as fast as system time. Broadcasts the TIME_RATE Parameter if the rate changes.

    Parameters

    • rate: number

      the rate at which clock time passes compared to system time

    Returns void

  • Performs a single step forward in time; puts the Clock into step mode; advances the clock time and real time by the specified time step; pauses the clock; and broadcasts a CLOCK_STEP event.

    When the client sees that isStepping is true, it should advance the Simulation to match the current clock time, and then call clearStepMode.

    Parameters

    • timeStep: number

      amount of time to advance the clock in seconds

    Returns void

  • Converts system time to clock time. System time is defined by SystemClock.

    Parameters

    • systemTime: number

      in seconds

    Returns number

    clock time equivalent of systemTime

  • Returns a minimal string representation of this object, usually giving just identity information like the class name and name of the object.

    For an object whose main purpose is to represent another Printable object, it is recommended to include the result of calling toStringShort on that other object. For example, calling toStringShort() on a DisplayShape might return something like this:

    DisplayShape{polygon:Polygon{'chain3'}}
    

    Returns string

    a minimal string representation of this object.

Generated using TypeDoc