2.1. Basic Simulation of Daily Cycle

Your first working Simulation Code

Jeff Elpern


2.1.1. Introducing the event loop

In this section we will create the "backbone" of the Go West simulation - the daily event loop. We create a SimPy Process that uses the yield hold functionality to step thought a 24 hour cycle and fire event massages at key daily point such as sun up. The events fired by this daily loop drive all the other simulation processes and resources added in the following sections and chapters.

The console output shown in Figure 1, “Event Loop Trace Output” is the trace output generated by the daily event loop.

Figure 1. Event Loop Trace Output

Event Loop Trace Output

2.1.2. Objectives

List the objectives of this section

  • Simulation setup and initialization. ???

  • Simulation event loop. ???

  • Global variables. 

  • Trace and debug output control. ???

2.1.3. DailyCycle code

Below is a full listing of the simulation code for this section.

Figure 2. Callouts for Python Statements

1

comment statement

2

Load a library

3

Define a variable

4

Coding Style

5

Define Class

6

Initiate Class

7

Define a Method

8

while statement

11

if statement

12

print statement

13

A variable as an attribute

14

A variable as an attribute


[Note]Note

Authors. Need lead-in for sim callouts

Figure 3. Callouts for PySim Statements

15

Initialize simulation

(16)

PySim Events

(17)

Launch simulation

9

yield statement

10

Event Signal statement


Below is a full listing of the simulation code for this section.

Figure 4.  Full listing of Daily Cycle simulation code

# File: kese_gw_dailyCycle_v1.py   1
# Knowledge and Economics Simulation Engine
# RealTime driver "Go West" scenario

from SimPy.SimulationRT  import *  2

simDays = 5  3
simHours = simDays * 24

# Print feedback control
printDaylightCycle = True 4

#---------------------------------------------------------------------
# daylight massaging Process
#---------------------------------------------------------------------
'''
This is a simple 12 hour sun up to sun down time. However the process
could be extended to simulate seasons and/or latitude.

Note: this process also fires a new day event
'''

class Daylight(Process):  5

    def __init__(self): 6
        Process.__init__(self, name='Daylight')
        self.sunUpHour = 6
        self.sunDownHour = 18
    
    def sunEvents(self): 7

        # start 24 hour loop
        while True: 8
            # tick internal clock to first hour of next day
            yield hold, self, 1 9              

            # send start of new day event
            newDay.signal()  10  
            if printDaylightCycle == True:  11 
                print 'From Daylight Process: ', \  12 
                'Start of day %d at simulation hour %d'\
                %( int(now()/24)+1, now() )
            
            # wait for daylight and them sed event
            yield hold, self, self.sunUpHour  13
            sunUp.signal()
            if printDaylightCycle == True:
                print 'From Daylight Process: sun up at %d' %now()

            # wait for nightfall and then send event
            #   subtract 1 to set at start of sundown
            yield hold, self, (self.sunDownHour-self.sunUpHour)-1  14
            sunDown.signal()
            if printDaylightCycle == True:
                print 'From Daylight Process: sundown at %d' %now()

            # wait for midnight
            yield hold, self, 24-self.sunDownHour
            dayEnd.signal()
            if printDaylightCycle == True:
             print 'From Daylight Process:  Day ended at %d' %now()

##########################################################################
#
# Module level code that initializes simulation
#
##########################################################################

initialize() 15

# create day and daylight event Process
daylightEvents = Daylight()
activate(daylightEvents, daylightEvents.sunEvents())

# Create Event Objects  
externalityPhase = SimEvent(name='Externality Phase signal for Economic Cycle') 
sunUp = SimEvent(name='Sun rise event')  (16)
sunDown = SimEvent(name='Sun set event')
newDay = SimEvent(name='Start of day event')
dayEnd = SimEvent(name='End of day event')

# simualtion starts here - 12 rel unit sim time = 2 sec clock per day
simulate(real_time=True, rel_speed=12, until=simHours)  (17)
      

2.1.4. Discussion of Python Statements

2.1.4.1. comment statement

[Note]Note

Authors. TODO: tutorial for this statement. ???

2.1.4.2. from statement to load a library

[Note]Note

Authors. TODO: tutorial for this statement. ???

2.1.4.3. Define a Variable

[Note]Note

Authors. TODO: tutorial for this statement. ???

2.1.4.4. Coding Style

[Note]Note

Authors. TODO: tutorial for this statement. ???

2.1.4.5. class statement

[Note]Note

Authors. TODO: tutorial for this statement. ???

2.1.4.6. def __init__(self): statement

[Note]Note

Authors. TODO: tutorial for this statement. ???

2.1.4.7. Method statement

[Note]Note

Authors. TODO: tutorial for this statement. ???

2.1.4.8. while statement

[Note]Note

Authors. TODO: tutorial for this statement. ???

2.1.4.9. if statement

[Note]Note

Authors. TODO: tutorial for this statement. ???

2.1.4.10. print statement

[Note]Note

Authors. TODO: tutorial for this statement. ???

2.1.4.11. Variable as Attribute

[Note]Note

Authors. TODO: tutorial for this statement. ???

2.1.4.12. Calculated Attribute

[Note]Note

Authors. TODO: tutorial for this statement. ???

2.1.5. Discussion of Python Statements

2.1.5.1. initialize() statement

[Note]Note

Authors. TODO: tutorial for this statement. ???

2.1.5.2. SimEvent() statement

[Note]Note

Authors. TODO: tutorial for this statement. ???

2.1.5.3. simulate() statement

[Note]Note

Authors. TODO: tutorial for this statement. ???

2.1.5.4. yield statement

[Note]Note

Authors. TODO: tutorial for this statement. ???

2.1.5.5. Event.signal() statement

[Note]Note

Authors. TODO: tutorial for this statement. ???

2.1.6. Exercises

[Note]Note

Authors. TODO: create exercises that show understanding. ???

KESL/RandD/pythonGoWest/ChapSunUp/DaylightCycle (last edited 2008-10-07 21:43:50 by root)