2.5.��Basic Simulation of Daily Cycle

Your first working Simulation Code

Jeff Elpern


Objectives

List the objectives of this section

Introducing the event loop

Stuff about Daylight event loop

Figure��1.��Event Loop Output

Event Loop Output

DailyCycle code

# Knowledge and Economics Simulation Engine
# RealTime driver "Go West" scenario

from SimPy.SimulationRT  import *           1

simDays = 20
simHours = simDays * 24


# Print feedback control
printDaylightCycle = True


print 'got here'
#------------------------------------------------------------------------------
# daylight massaging Process
#-------------------------------------------------------------------------------
'''
This is a simple 12 hour cycle. However the procuss could be extended to
simulate seasons and/or latitude.

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

class Daylight(Process):

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

        # start 24 hour loop
        while True:
            yield hold, self, 1               # tick internal clock to first hour of next day
            # send start of new day event
            newDay.signal()
            if printDaylightCycle == True:
                print 'From Daylight Process:  Start of day at %d' %now()
            
            # wait for daylight and them sed event
            yield hold, self, self.sunUpHour
            sunUp.signal()
            if printDaylightCycle == True:
                print 'From Daylight Process: sun up at %d' %now()

            # wait for nightfall and then send event
            yield hold, self, (self.sunDownHour-self.sunUpHour)-1   # sub 1 to set at start of sundown
            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()

# 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')
sunDown = SimEvent(name='Sun set event')
newDay = SimEvent(name='Start of day event')
dayEnd = SimEvent(name='End of day event')

# simualtion starts here
simulate(real_time=True, rel_speed=12, until=simHours) ##unit sim time = 1 sec clock
      

1

Load of Real Time SimPy lib.

openCIE/keseGoWest/ChapSunUp/DaylightCycle (last edited 2015-03-06 18:11:27 by localhost)