search

Class myphysicslab.lab.app.SimRunner

Provided By
Extends
All Implemented Interfaces

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.

Parameters Created

  • ParameterNumber named SimRunner.en.TIME_STEP see #setTimeStep

  • ParameterNumber named SimRunner.en.DISPLAY_PERIOD see #setDisplayPeriod

  • ParameterBoolean named SimRunner.en.RUNNING see #setRunning

  • ParameterBoolean named SimRunner.en.NON_STOP see #setNonStop

Events Broadcast

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

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 callback is set to be the SimRunner method #callback.

  • The callback() method reschedules itself to run again by calling Timer.finishAt() or Timer.fireAfter() thus continuing the chain of callbacks. The callback reschedules itself regardless of whether the Clock is paused, stepping, or running (though it calculates the delay differently in those cases).

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

  • 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. The callback chain continues to be rescheduled with Timer.fireAfter(), but the Clock time is frozen. 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.

new SimRunner( advance, opt_name )

Parameters
advancemyphysicslab.lab.model.AdvanceStrategy

the AdvanceStrategy which runs advances the Simulation

opt_name(string|undefined)

name of this SimRunner.

Instance Methods

Static Properties

Type Definitions