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.
List the objectives of this section
Simulation setup and initialization. ???
Simulation event loop. ???
Global variables.
Trace and debug output control. ???
Below is a full listing of the simulation code for this section.
![]() | Note Authors. Need lead-in for sim callouts |
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# Knowledge and Economics Simulation Engine # RealTime driver "Go West" scenario from SimPy.SimulationRT import *
simDays = 5
simHours = simDays * 24 # Print feedback control printDaylightCycle = True
#--------------------------------------------------------------------- # 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):
def __init__(self):
Process.__init__(self, name='Daylight') self.sunUpHour = 6 self.sunDownHour = 18 def sunEvents(self):
# start 24 hour loop while True:
# tick internal clock to first hour of next day yield hold, self, 1
# send start of new day event newDay.signal()
if printDaylightCycle == True:
print 'From Daylight Process: ', \
'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
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
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') (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)














![[Note]](/wiki/docbook/images/note.png)


