Uses an AdvanceStrategy to advance the Simulation state; the process is driven by a Timer and a Clock to synchronize the Simulation with real time; updates the LabCanvas to show the current Simulation state.

How Simulation Advances with Clock

SimRunner advances the Simulation state, keeping it in sync with the Clock time, and therefore we see the Simulation advancing in real time. Here are the details:

  • The Timer repeatedly executes the SimRunner's callback. The callback continues being fired regardless of whether the Clock is paused, stepping, or running.

  • When the Clock is running or stepping, callback() advances the Simulation up to (or just beyond) the current clock time by calling AdvanceStrategy.advance. This keeps the Simulation in sync with the Clock.

  • Stepping forward by a single time step employs the special step mode of Clock. When callback() sees the Clock is in step mode, it advances the Simulation by a single time step and then clears the Clock's step mode so that the Clock will thereafter be in the regular "paused" state.

  • Sometimes the Simulation cannot be computed in real time. In that case callback() will retard the clock time when it is too far ahead of simulation time, by calling Clock.setTime. Once that happens Clock.getRealTime will be greater than Clock.getTime. We can calculate how much time has been lost due to performance problems by comparing these.

  • When the Clock is paused callback() still updates the LabCanvas, so any changes to objects will be seen. This allows the user to position objects while the simulation is paused.

  • The Timer period (the callback frequency) determines the frame rate of the simulation display, because a new frame is drawn each time the callback() callback fires. See setDisplayPeriod.

  • The Timer period has no effect on how often the Simulation's differential equation is calculated; that is determined separately by the time step used when calling AdvanceStrategy.advance(). See setTimeStep.

Stop Simulation When Window is Not Active

SimRunner listens for blur and focus events to stop and start the Timer. Those events occur when the browser window changes from being the front most active window to some other window or browser tab being active. This means the simulation will only run when the browser window is the frontmost active window. This helps reduce CPU usage when the user is not viewing the simulation.

There is a "non-stop" Parameter which allows the simulation to run even when the window is not active. This is useful if you want to view two simulations running in separate browser windows. See setNonStop.

Parameters Created

Events Broadcast

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

  • GenericEvent named RESET, see reset.

Hierarchy (view full)

Implements

Constructors

Properties

advanceList_: AdvanceStrategy[]

The AdvanceStrategys to run.

appName_: string = ''

Name of the application that created this SimRunner, for debugging. Useful when multiple apps are running simultaneously on a page: this tells which app this SimRunner belongs to.

displayPeriod_: number = 0

Amount of time between displaying frames of the simulation, in seconds.

nonStop_: boolean = false

Whether the Timer stops firing when the window is not active (when a blur event occurs).

timeStep_: number

Amount of time to advance the simulation, in seconds.

RESET: "RESET" = 'RESET'

Name of GenericEvent that is broadcast when reset method occurs.

Methods

  • Adds the LabCanvas to the list of LabCanvas's that need to be repainted and memorized after each advance of the Simulation.

    Parameters

    • canvas: LabCanvas

      the LabCanvas to add to the list of LabCanvas's to update

    Returns void

  • Adds an object to the list of ErrorObserver objects to be notified when an error occurs.

    Parameters

    • errorObserver: ErrorObserver

      object to add to the list of ErrorObserver objects

    Returns void

  • Adds an object to the list of Memorizable objects. These object's memorize methods will be called from this object's memorize method.

    Parameters

    • memorizable: Memorizable

      object to add to the list of Memorizable objects

    Returns void

    Throws

    if called during the memorize method.

  • Advances the simulation(s) to the target time and calls Memorizable.memorize on the list of Memorizables after each time step.

    Parameters

    • strategy: AdvanceStrategy

      the AdvanceStrategy which advances the simulation

    • targetTime: number

      the time to advance to

    Returns void

  • Advances the Simulation AdvanceStrategy(s) to match the current Clock time and repaints the LabCanvas's. Calls Memorizable.memorize on the list of Memorizables after each time step. This is the callback function that is being run by the Timer.

    Returns void

  • Remove connections to other objects to facilitate garbage collection.

    Returns void

  • Returns the list of LabCanvas's that need to be repainted after each advance of the Simulation.

    Returns LabCanvas[]

    the list of LabCanvas that need to be repainted

  • Returns the Clock which the Simulation is synchronized to.

    Returns Clock

    the Clock which the Simulation is synchronized to.

  • Returns the amount of time between displaying frames of the Simulation, in seconds.

    Returns number

    amount of time between displaying frames of the Simulation, in seconds.

  • Returns true if the Timer keeps firing even when the window is not active (not frontmost window). The default is for the Timer to be stoppable, which prevents the simulation from wasting CPU cycles when the user does not have the simulation window active.

    Returns boolean

    true means the Timer keeps firing even when the browser window is inactive

  • Returns true if the Clock is running.

    Returns boolean

    true if the Clock is running

  • Returns the small increment of time by which to advance the Simulation's state. Several steps of this size may be taken to advance the Simulation time to be equal to or beyond the Clock time.

    Returns number

    the length of a time step, in seconds.

  • Presents an alert to the user about the exception with instructions about how to get the Simulation running again; calls pause to stop the Simulation.

    Parameters

    • error: any

      the error that caused the exception

    Returns void

  • Memorize the current simulation data, or do some other function that should happen regularly after each simulation time step.

    Returns void

  • Notifies this Observer that a change has occurred in the Subject.

    Parameters

    • event: SubjectEvent

      contains information about what has changed in the Subject: typically either a one-time GenericEvent, or a change to the value of a Parameter

    Returns void

  • Paints all the LabCanvas's, which causes them to redraw their contents.

    Returns void

  • Pause the Clock, which therefore also pauses the Simulation.

    Returns number

    the current time on the Clock

  • Play the Simulation until the given time. Resumes the Clock, which therefore also resumes advancing the Simulation, and creates a ClockTask to stop the Clock.

    Parameters

    • pauseTime: number

      time when the Clock should be paused

    Returns number

    the current time on the Clock

  • Remove the LabCanvas from the list of LabCanvas's that need to be repainted and memorized after each advance of the Simulation.

    Parameters

    • canvas: LabCanvas

      the LabCanvas to remove from the list of LabCanvas's to update

    Returns void

  • Removes an object from the list of ErrorObserver objects to be notified when an error occurs.

    Parameters

    • errorObserver: ErrorObserver

      object to remove from the list of ErrorObserver objects

    Returns void

  • Removes an object from the list of Memorizable objects.

    Parameters

    • memorizable: Memorizable

      object to remove from the list of Memorizable objects

    Returns void

    Throws

    if called during the memorize method.

  • Sets the Simulation to its initial conditions by calling AdvanceStrategy.reset, sets the Clock to match the simulation time (usually zero), and pauses the Clock. Broadcasts a RESET event.

    Returns number

    the current time on the Clock after resetting

  • Resume the Clock, which therefore also resumes advancing the Simulation.

    Returns number

    the current time on the Clock

  • Save the initial conditions, which can be returned to with reset.

    Returns number

    the current time on the Clock

  • Set name of the application that created this SimRunner, for debugging.

    Parameters

    • name: string

      the name of the application that created this SimRunner

    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 amount of time between displaying frames of the Simulation, in seconds. Can be set to zero, in which case the fastest possible frame rate will happen, which is usually 60 frames per second.

    Parameters

    • displayPeriod: number

      amount of time between displaying frames of the Simulation, in seconds.

    Returns void

  • Sets whether the Timer is executing callback. However, if non-stop mode is on, then this will not stop the Timer, see setNonStop.

    Parameters

    • value: boolean

      true causes the Timer to start firing

    Returns void

  • Sets whether the Timer keeps firing when the window is not active (not the frontmost window). The default is for the Timer to be stoppable, which prevents the simulation from wasting CPU cycles when the user does not have the simulation window active.

    Parameters

    • value: boolean

      true means the Timer keeps firing even when the browser window is not active

    Returns void

  • Sets whether the Clock is running or paused.

    Parameters

    • value: boolean

      true means the Clock will be running

    Returns void

  • Sets the length of a time step, the small increment of time by which to advance the Simulation's state. Several steps of this size may be taken to advance the Simulation time to be equal to or beyond the Clock time.

    Parameters

    • timeStep: number

      the length of a time step, in seconds.

    Returns void

  • Steps the Clock and Simulation forward by a single timestep.

    Returns number

    the current time on the clock after stepping

  • 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