File: variableTimeRatio.py

package info (click to toggle)
python-simpy 2.3.1%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,864 kB
  • sloc: python: 11,171; makefile: 143
file content (40 lines) | stat: -rw-r--r-- 1,300 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"""variableTimeRatio.py
Shows the SimulationRT capability to change the ratio simulation
time to wallclock time during the run of a simulation.
"""
from SimPy.SimulationRT import *

class Changer(Process):
    def change(self,when,rat):
        global ratio
        yield hold,self,when
        rtset(rat)
        ratio=rat
class Series(Process):
    def tick(self,nrTicks):
        oldratio=ratio
        for i in range(nrTicks):
            tLastSim=now()
            tLastWallclock=wallclock()
            yield hold,self,1
            diffSim=now()-tLastSim
            diffWall=wallclock()-tLastWallclock
            print "now(): %s, sim. time elapsed: %s, wall clock elapsed: "\
                "%6.3f, sim/wall time ratio: %6.3f"\
                %(now(),diffSim,diffWall,diffSim/diffWall)
            if not ratio==oldratio:
                print "At simulation time %s: ratio simulation/wallclock "\
                    "time now changed to %s"%(now(),ratio)
                oldratio=ratio
initialize()
ticks=15
s=Series()
activate(s,s.tick(nrTicks=ticks))
c=Changer()
activate(c,c.change(5,5))
c=Changer()
activate(c,c.change(10,10))
ratio=1
print "At simulation time %s: set ratio simulation/wallclock time to %s"\
       %(now(),ratio)
simulate(until=100,real_time=True,rel_speed=ratio)