| 1 |
|
| 2 |
|
| 3 |
=============================================== |
| 4 |
Simulation with Real Time Synchronization |
| 5 |
=============================================== |
| 6 |
|
| 7 |
.. highlight:: python |
| 8 |
:linenothreshold: 5 |
| 9 |
|
| 10 |
:Authors: - Klaus Muller <[email protected]> |
| 11 |
- Tony Vignaux <[email protected]> |
| 12 |
:SimPy release: |release| |
| 13 |
:Web-site: http://simpy.sourceforge.net/ |
| 14 |
:Python-Version: 2.6+ |
| 15 |
:Date: December 2011 |
| 16 |
|
| 17 |
.. contents:: Contents |
| 18 |
:depth: 3 |
| 19 |
|
| 20 |
This manual describes **SimulationRT**, a SimPy module which supports |
| 21 |
synchronizing the execution of simulation models with real (wallclock) time. |
| 22 |
|
| 23 |
Acknowledgement |
| 24 |
=============== |
| 25 |
|
| 26 |
SimulationRT is based on an idea by Geoff Jarrad of CSIRO (Australia). He |
| 27 |
contributed a lot to its development and testing on Windows and Unix. |
| 28 |
|
| 29 |
The code for the adjustment of the execution speed during the simulation run |
| 30 |
was contributed by Robert C. Ramsdell. |
| 31 |
|
| 32 |
Synchronizing with wall clock time |
| 33 |
=================================== |
| 34 |
|
| 35 |
SimulationRT allows synchronizing simulation time and real (wallclock) time. |
| 36 |
This capability can be used to implement e.g. interactive game applications or |
| 37 |
to demonstrate a model's execution in real time. |
| 38 |
|
| 39 |
It is identical to Simulation, except for the *simulate* function which takes |
| 40 |
an additional parameter controlling real-time execution speed. |
| 41 |
|
| 42 |
Here is an example: |
| 43 |
|
| 44 |
.. highlight:: python |
| 45 |
:linenothreshold: 5 |
| 46 |
|
| 47 |
.. literalinclude:: SimulationRTprograms/RealTimeFireworks.py |
| 48 |
|
| 49 |
*rel_speed* is the ratio **simulated time/wallclock time**. *rels_speed=1* sets the synchronization so that 1 simulation time unit |
| 50 |
is executed in approximately 1 second of wallclock time. |
| 51 |
Run under Python 2.6 on a Windows Vista-box (2.3 GHz), this output resulted |
| 52 |
over about 17.5 seconds of wallclock time: |
| 53 |
|
| 54 |
.. literalinclude:: SimulationRTprograms/RealTimeFireworks.out |
| 55 |
|
| 56 |
Clearly, the wallclock time does not deviate significantly from the simulation time. |
| 57 |
|
| 58 |
Changing the execution speed during a simulation run |
| 59 |
================================================================== |
| 60 |
|
| 61 |
By calling method *rtset* with a parameter, the ratio simulated time to wallclock time |
| 62 |
can be changed during a run. |
| 63 |
|
| 64 |
Here is an example: |
| 65 |
|
| 66 |
.. literalinclude:: SimulationRTprograms/variableTimeRatio.py |
| 67 |
|
| 68 |
The program changes the time ratio twice, at simulation times 5 and 10. |
| 69 |
|
| 70 |
When run on a Windows Vista computer under Python 2.6, this results in this output: |
| 71 |
|
| 72 |
.. literalinclude:: SimulationRTprograms/variableTimeRatio.out |
| 73 |
|
| 74 |
Limitations |
| 75 |
============ |
| 76 |
This module works much better under Windows than under Unix or Linux, i.e., it gives |
| 77 |
much closer synchronization. |
| 78 |
Unfortunately, the handling of time in Python is not platform-independent at all. |
| 79 |
Here is a quote from the documentation of the *time* module:: |
| 80 |
|
| 81 |
"clock() |
| 82 |
On Unix, return the current processor time as a floating point number expressed in seconds. |
| 83 |
The precision, and in fact the very definition of the meaning of ``processor time'' , depends |
| 84 |
on that of the C function of the same name, but in any case, this is the function to use for |
| 85 |
benchmarking Python or timing algorithms. |
| 86 |
|
| 87 |
On Windows, this function returns wall-clock seconds elapsed since the first call to this |
| 88 |
function, as a floating point number, based on the Win32 function QueryPerformanceCounter(). |
| 89 |
The resolution is typically better than one microsecond. |
| 90 |
" |
| 91 |
|
| 92 |
|
| 93 |
The SimulationRT API |
| 94 |
====================== |
| 95 |
|
| 96 |
Structure |
| 97 |
--------- |
| 98 |
Basically, SimulationStep has the same API as Simulation, but with: |
| 99 |
|
| 100 |
- a change in the definition of simulate, and |
| 101 |
- an additional method to change execution speed during a simulation |
| 102 |
run. |
| 103 |
|
| 104 |
**simulate** |
| 105 |
------------------ |
| 106 |
|
| 107 |
Executes the simulation model. |
| 108 |
|
| 109 |
Call: |
| 110 |
|
| 111 |
**simulate(<optional parameters>)** |
| 112 |
|
| 113 |
Mandatory parameters: |
| 114 |
None. |
| 115 |
|
| 116 |
Optional parameters: |
| 117 |
- **until=0** : the maximum simulation (end) time (positive floating point number; default: 0) |
| 118 |
- **real_time=False** : flag to switch real time synchronization on or off (boolean; default: False, meaning no synchronization) |
| 119 |
- **rel_speed=1** : ratio simulation time over wallclock time; example: *rel_speed=200* executes 200 units of simulation time in about one second (positive floating point number; default: 1, i.e. 1 sec of simulation time is executed in about 1 sec of wallclock time) |
| 120 |
|
| 121 |
Return value: |
| 122 |
Simulation status at exit. |
| 123 |
|
| 124 |
**rtset** |
| 125 |
------------------ |
| 126 |
|
| 127 |
Changes the ratio simulation time over wall clock time. |
| 128 |
|
| 129 |
Call: |
| 130 |
|
| 131 |
**rtset(<new ratio>)** |
| 132 |
|
| 133 |
Mandatory parameters: |
| 134 |
None |
| 135 |
|
| 136 |
Optional parameters: |
| 137 |
- **rel_speed=1** : ratio simulation time over wallclock time; example: *rel_speed=200* executes 200 units of simulation time in about one second (positive floating point number; default: 1, i.e. 1 sec of simulation time is executed in about 1 sec of wallclock time) |
| 138 |
|
| 139 |
Return value: |
| 140 |
None |
| 141 |
|
| 142 |
|