File: SimStepManual.html

package info (click to toggle)
python-simpy 2.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 17,300 kB
  • ctags: 2,568
  • sloc: python: 11,172; makefile: 145
file content (423 lines) | stat: -rw-r--r-- 17,456 bytes parent folder | download
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Simulation with Event Stepping &mdash; SimPy 2.3.1 documentation</title>
    
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '2.3.1',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="top" title="SimPy 2.3.1 documentation" href="../index.html" />
    <link rel="up" title="Manuals" href="../Manuals.html" />
    <link rel="next" title="Cheatsheets" href="../Cheatsheets.html" />
    <link rel="prev" title="Simulation with Real Time Synchronization" href="SimRTManual.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../Cheatsheets.html" title="Cheatsheets"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="SimRTManual.html" title="Simulation with Real Time Synchronization"
             accesskey="P">previous</a> |</li>
        <li><a href="../index.html">SimPy 2.3.1 documentation</a> &raquo;</li>
          <li><a href="../Manuals.html" accesskey="U">Manuals</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="simulation-with-event-stepping">
<h1><a class="toc-backref" href="#id1">Simulation with Event Stepping</a><a class="headerlink" href="#simulation-with-event-stepping" title="Permalink to this headline">¶</a></h1>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Authors:</th><td class="field-body"><ul class="first simple">
<li>Klaus Muller &lt;<a class="reference external" href="mailto:Muller&#37;&#52;&#48;users&#46;sourceforge&#46;net">Muller<span>&#64;</span>users<span>&#46;</span>sourceforge<span>&#46;</span>net</a>&gt;</li>
<li>Tony Vignaux &lt;<a class="reference external" href="mailto:Vignaux&#37;&#52;&#48;users&#46;sourceforge&#46;net">Vignaux<span>&#64;</span>users<span>&#46;</span>sourceforge<span>&#46;</span>net</a>&gt;</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">SimPy release:</th><td class="field-body"><p class="first">2.3.1</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">Web-site:</th><td class="field-body"><p class="first"><a class="reference external" href="http://simpy.sourceforge.net/">http://simpy.sourceforge.net/</a></p>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Python-Version:</th><td class="field-body"><p class="first">2.6+</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">Date:</th><td class="field-body"><p class="first last">December 2011</p>
</td>
</tr>
</tbody>
</table>
<div class="contents topic" id="contents">
<p class="topic-title first">Contents</p>
<ul class="simple">
<li><a class="reference internal" href="#simulation-with-event-stepping" id="id1">Simulation with Event Stepping</a><ul>
<li><a class="reference internal" href="#introduction" id="id2">Introduction</a></li>
<li><a class="reference internal" href="#simulationstep-overview" id="id3">SimulationStep overview</a></li>
<li><a class="reference internal" href="#the-simulationstep-api" id="id4">The SimulationStep API</a></li>
</ul>
</li>
</ul>
</div>
<p>This manual describes <strong>SimulationStep</strong>, a SimPy module which supports
stepping through a simulation model event by event.</p>
<div class="section" id="introduction">
<h2><a class="toc-backref" href="#id2">Introduction</a><a class="headerlink" href="#introduction" title="Permalink to this headline">¶</a></h2>
<p>SimulationStep can assist with debugging models, interacting with them on
an event-by-event basis, getting event-by-event output from a model (e.g.
for plotting purposes), etc.</p>
<p>SimulationStep is a derivative of the Simulation module. Over and above
the capabilities provided by Simulation, SimulationStep supports stepping
through a simulation model event by event. It caters for:</p>
<blockquote>
<div><ul class="simple">
<li>running a simulation model, with calling a user-defined procedure after every event,</li>
<li>running a simulation model one event at a time by repeated calls,</li>
<li>starting and stopping the event stepping mode under program control.</li>
</ul>
</div></blockquote>
</div>
<div class="section" id="simulationstep-overview">
<h2><a class="toc-backref" href="#id3">SimulationStep overview</a><a class="headerlink" href="#simulationstep-overview" title="Permalink to this headline">¶</a></h2>
<p>Here is a simple program which shows basic event stepping capabilities:</p>
<div class="highlight-python"><pre>## Stepping1.py
from __future__ import generators
from SimPy.SimulationStep import *                  # (1)

def callbackTimeTrace():                            # (2)
    """Prints event times
    """
    print "at time=%s"%now()

class Man(Process):
    def walk(self):
        print "got up"
        yield hold,self,1
        print "got to door"
        yield hold,self,10
        print "got to mail box"
        yield hold,self,10
        print "got home again"

#trace event times
initialize()
otto=Man()
activate(otto,otto.walk())
startStepping()                                     # (3)
simulate(callback=callbackTimeTrace,until=100)      # (4)</pre>
</div>
<p>A trivial simulation model, but with event stepping:</p>
<blockquote>
<div><ol class="arabic simple">
<li>import the stepping version of Simulation</li>
<li>define a procedure which gets called after every event</li>
<li>switch into event stepping mode</li>
<li>run the model with event callback to the procedure defined at (2); <tt class="docutils literal"><span class="pre">simulate</span></tt> in SimulationStep has an extra named parameter, <tt class="docutils literal"><span class="pre">callback</span></tt>.</li>
</ol>
</div></blockquote>
<p>Running it produces this output:</p>
<div class="highlight-python"><pre>got up
at time=0
got to door
at time=1
got to mail box
at time=11
got home again
at time=21
at time=21</pre>
</div>
<p>The callback outputs the simulation time after every event.</p>
<p>Here is another example, the same model, but now with the user getting control back after every
event:</p>
<div class="highlight-python"><pre>## Stepping2.py
from __future__ import generators
from SimPy.SimulationStep import *

def callbackUserControl():
    """Allows user to control stepping
    """
    a=raw_input("[Time=%s] Select one: End run (e), Continue stepping (s), Run to end (r)= "%now())
    if a=="e":
        stopSimulation()
    elif a=="s":
        return
    else:
        stopStepping()

class Man(Process):
    def walk(self):
        print "got up"
        yield hold,self,1
        print "got to door"
        yield hold,self,10
        print "got to mail box"
        yield hold,self,10
        print "got home again"
#allow user control
initialize()
otto=Man()
activate(otto,otto.walk())
startStepping()
simulate(callback=callbackUserControl,until=100)</pre>
</div>
<p>Its interactive output looks like this:</p>
<div class="highlight-python"><pre>got up
[Time=0] Select one: End run (e), Continue stepping (s), Run to end (r)= s
got to door
[Time=1] Select one: End run (e), Continue stepping (s), Run to end (r)= s
got to mail box
[Time=11] Select one: End run (e), Continue stepping (s), Run to end (r)= s
got home again
[Time=21] Select one: End run (e), Continue stepping (s), Run to end (r)= s
[Time=21] Select one: End run (e), Continue stepping (s), Run to end (r)= s</pre>
</div>
<p>or this (the user stopped stepping mode at time=1):</p>
<div class="highlight-python"><pre>got up
[Time=0] Select one: End run (e), Continue stepping (s), Run to end (r)= s
got to door
[Time=1] Select one: End run (e), Continue stepping (s), Run to end (r)= r
got to mail box
got home again</pre>
</div>
<p>If one wants to run a tested/debugged model full speed, i.e. without stepping,
one can write a program as follows:</p>
<div class="highlight-python"><pre>## Stepping2Fast.py
from __future__ import generators
if __debug__:
        from SimPy.SimulationStep import *
else:
        from SimPy.Simulation import *

def callbackUserControl():
    """Allows user to control stepping
    """
    if __debug__:
                a=raw_input("[Time=%s] Select one: End run (e), Continue stepping (s),\
                     Run to end (r)= "%now())
                if a=="e":
                    stopSimulation()
                elif a=="s":
                    return
                else:
                    stopStepping()

class Man(Process):
    def walk(self):
        print "got up"
        yield hold,self,1
        print "got to door"
        yield hold,self,10
        print "got to mail box"
        yield hold,self,10
        print "got home again"
#allow user control if debugging
initialize()
otto=Man()
activate(otto,otto.walk())
if __debug__:
        startStepping()
        simulate(callback=callbackUserControl,until=100)
else:
        simulate(until=100)</pre>
</div>
<p>If one runs this with the Python command line option &#8216;-O&#8217;, any
statement starting with <tt class="docutils literal"><span class="pre">if</span> <span class="pre">__debug__:</span></tt> is ignored/skipped by the
Python interpreter.</p>
</div>
<div class="section" id="the-simulationstep-api">
<h2><a class="toc-backref" href="#id4">The SimulationStep API</a><a class="headerlink" href="#the-simulationstep-api" title="Permalink to this headline">¶</a></h2>
<div class="section" id="structure">
<h3>Structure<a class="headerlink" href="#structure" title="Permalink to this headline">¶</a></h3>
<p>Basically, SimulationStep has the same API as Simulation, but with
the following additions and changes:</p>
<div class="highlight-python"><pre>def startStepping()         **new**
def stopStepping()          **new**
def simulate()              **changed**
def simulateStep()          **new**</pre>
</div>
</div>
<div class="section" id="startstepping">
<h3><strong>startStepping</strong><a class="headerlink" href="#startstepping" title="Permalink to this headline">¶</a></h3>
<p>Starts the event-stepping.</p>
<p>Call:</p>
<blockquote>
<div><strong>startStepping()</strong></div></blockquote>
<dl class="docutils">
<dt>Mandatory parameters:</dt>
<dd>None.</dd>
<dt>Optional parameters:</dt>
<dd>None</dd>
<dt>Return value:</dt>
<dd>None.</dd>
</dl>
</div>
<div class="section" id="stopstepping">
<h3><strong>stopStepping</strong><a class="headerlink" href="#stopstepping" title="Permalink to this headline">¶</a></h3>
<p>Stops event-stepping.</p>
<dl class="docutils">
<dt>Call:</dt>
<dd><strong>stopStepping()</strong></dd>
<dt>Mandatory parameters:</dt>
<dd>None</dd>
<dt>Optional parameters:</dt>
<dd>None</dd>
<dt>Return value:</dt>
<dd>None</dd>
</dl>
</div>
<div class="section" id="simulate">
<h3><strong>simulate</strong><a class="headerlink" href="#simulate" title="Permalink to this headline">¶</a></h3>
<p>Runs a simulation with callback to a user-defined function after each event, if stepping is turned on.
By default, stepping is switched off.</p>
<dl class="docutils">
<dt>Call:</dt>
<dd><strong>simulate(callback=&lt;proc&gt;,until=&lt;endtime&gt;)</strong></dd>
<dt>Mandatory parameters:</dt>
<dd>None</dd>
<dt>Optional parameters:</dt>
<dd><ul class="first last simple">
<li><strong>until = 0</strong>: the simulation time until which the simulation is to run (positive floating point or integer number)</li>
<li><strong>callback = lambda:None</strong>: the function to be called after every event (function reference)</li>
</ul>
</dd>
<dt>Return value:</dt>
<dd>The simulation status at exit (string)</dd>
</dl>
</div>
<div class="section" id="simulatestep">
<h3><strong>simulateStep</strong><a class="headerlink" href="#simulatestep" title="Permalink to this headline">¶</a></h3>
<p>Runs a simulation for one event, with (optional) callback to a user-defined function
after the event, if stepping is turned on. By default, stepping is switched off.
Thus, to execute the model to completion, <em>simulateStep</em> must be called repeatedly.</p>
<p><strong>Note: it is not yet clear to the developers whether this part of the API offers any advantages
or capabilities over and above the *simulate* function. The survival of this function
in future versions depends on the feedback from the user community.</strong></p>
<dl class="docutils">
<dt>Call:</dt>
<dd><strong>simulateStep(callback=&lt;proc&gt;,until=&lt;endtime&gt;)</strong></dd>
<dt>Mandatory parameters:</dt>
<dd>None</dd>
<dt>Optional parameters:</dt>
<dd><ul class="first last simple">
<li><strong>until = 0</strong>: the simulation time until which the simulation is to run (positive floating point or integer number)</li>
<li><strong>callback = lambda:None</strong>: the function to be called after every event (function reference)</li>
</ul>
</dd>
<dt>Return value:</dt>
<dd>The tuple <strong>(simulation status at exit (string),&lt;resumability flag&gt;)</strong>.
&lt;resumability flag&gt; can have one of two string values: <strong>&#8220;resumable&#8221;</strong> if there
are more events to be executed, and <strong>&#8220;notResumable&#8221;</strong> if all events have been exhausted
or an error has occurred. <em>simulateStep</em> should normally only be called if
&#8220;resumable&#8221; is returned.</dd>
</dl>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../index.html">
              <img class="logo" src="../_static/sm_SimPy_Logo.png" alt="Logo"/>
            </a></p>
  <h3><a href="../index.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Simulation with Event Stepping</a><ul>
<li><a class="reference internal" href="#introduction">Introduction</a></li>
<li><a class="reference internal" href="#simulationstep-overview">SimulationStep overview</a></li>
<li><a class="reference internal" href="#the-simulationstep-api">The SimulationStep API</a><ul>
<li><a class="reference internal" href="#structure">Structure</a></li>
<li><a class="reference internal" href="#startstepping"><strong>startStepping</strong></a></li>
<li><a class="reference internal" href="#stopstepping"><strong>stopStepping</strong></a></li>
<li><a class="reference internal" href="#simulate"><strong>simulate</strong></a></li>
<li><a class="reference internal" href="#simulatestep"><strong>simulateStep</strong></a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="SimRTManual.html"
                        title="previous chapter">Simulation with Real Time Synchronization</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="../Cheatsheets.html"
                        title="next chapter">Cheatsheets</a></p>
  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="../_sources/Manuals/SimStepManual.txt"
           rel="nofollow">Show Source</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../Cheatsheets.html" title="Cheatsheets"
             >next</a> |</li>
        <li class="right" >
          <a href="SimRTManual.html" title="Simulation with Real Time Synchronization"
             >previous</a> |</li>
        <li><a href="../index.html">SimPy 2.3.1 documentation</a> &raquo;</li>
          <li><a href="../Manuals.html" >Manuals</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2002-2011, Klaus Müller, Tony Vignaux, Ontje Lünsdorf, Stefan Scherfke.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.2.
    </div>
  </body>
</html>