Real time¶
A simulator that runs on the clock, in frames, and can be controlled from another thread.
A simulator that runs in real time, without any user interface.
The simulation runs in frames of a few steps, one every 1/rate seconds of the clock. Frames are on a schedule that does not drift: the time a frame takes is not added to the wait for the next one. A simulation that falls behind catches up, but only up to max_lag: after a longer stall the schedule starts over from now and the time that was lost is dropped, not replayed in a burst.
sim = RealTimeSimulator(ps, dt=5e-3, rate=50)
sim.main_loop() # or sim.start() to run it in a thread, sim.pause(), sim.stop() ...
RealTimeSimulator
¶
RealTimeSimulator(
ps,
dt=0.005,
rate=50.0,
steps_per_frame=None,
speed=1.0,
max_lag=25,
max_voltage=5.0,
on_frame=None,
clock=time.monotonic,
sleep=None,
**kwargs,
)
Bases: Simulator
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rate
|
Frames per second of the clock. |
50.0
|
|
steps_per_frame
|
Steps of dt in a frame. The default makes the simulation run in real time, i.e. the frame is as much simulated time as the clock time between two frames. Another number is a fixed time scale, see realtime_factor. |
None
|
|
speed
|
Frames come speed times as fast as rate says, so 2 is twice as fast as real time. See set_speed. |
1.0
|
|
max_lag
|
How far behind its schedule, in frame periods, the simulation may fall before the schedule starts over from now. None: never, the simulation always catches up. |
25
|
|
max_voltage
|
A bus voltage above this, in p.u., or a state that is not finite, ends the simulation with an ArithmeticError after the frame: the simulation has diverged. None: the voltages are not limited. |
5.0
|
|
on_frame
|
Function called with the simulator after each frame, e.g. to read the state, which is the place to pause() or stop(). An exception in it ends the simulation. |
None
|
|
clock
|
Function returning the time in seconds, and sleep: function waiting for that many seconds, which by default stop() interrupts. Other than the default, e.g. a clock that is moved by hand, they make the pacing testable. |
monotonic
|
|
kwargs
|
Passed on to Simulator (t_end, events, ...). Attributes: frame (frames run), schedule_time (the time of the last frame on the schedule, in seconds from the start), lag (seconds the last frame was behind its schedule), lag_resets (times the schedule was started over), error (the exception that ended a simulation run by start(), if one did), rejected (events from submit() that failed when they were applied, as pairs of the event and the exception). |
{}
|
Source code in src/tops/realtime.py
steps_per_frame
instance-attribute
¶
steps_per_frame = (
max(1, round(1 / (rate * dt)))
if steps_per_frame is None
else steps_per_frame
)
realtime_factor
property
¶
Simulated seconds per second of the clock: 1 is real time.
set_speed
¶
pause
¶
resume
¶
stop
¶
End the simulation, also if it is waiting for its next frame or paused. A stopped simulator stays stopped.
stopped
¶
submit
¶
Queue an event, to be applied as ps.apply_event(*event, **kwargs) just before the next frame, in the order of submission. May be called from any thread. The event is given as (event function, unit), e.g. submit(ps.lines.Line.events.disconnect, 'L1-2'), or as (category, model, unit, action).
An event that cannot exist (unknown unit, action, model or arguments) raises here, in the caller's thread. One that fails only when it is applied, e.g. a power that is not a number, is kept in rejected, and the simulation goes on.
Source code in src/tops/realtime.py
start
¶
Run main_loop in a thread. An exception that ends it is kept in error.