-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdvanced Computational Coursework.py
More file actions
245 lines (221 loc) · 7.13 KB
/
Advanced Computational Coursework.py
File metadata and controls
245 lines (221 loc) · 7.13 KB
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
from random import random
import matplotlib.pyplot as pyplot
import matplotlib as plt
from matplotlib import animation
#import time
#import numba
#Simulation Variables
EMPTY = 0
TREE = 1
BURNING = 2
probTree = 0.8
probBurning = 0.01
probImmune = 0.3
probLightning = 0.001
GridSizes = [100,400,800,1000,1200,2000]
timeStep =500
SequentialTimeList = []
ParallelTimeList = []
barWidth = 0.25
#Initialization of the the initial forest grid
def InitializeForest(n,probTree,probBurning):
forestGrid = []
for x in range(n):
rowList = []
for y in range(n):
if (random()<probTree):
if(random()<probBurning):
cell = BURNING
else:
cell = TREE
else:
cell = EMPTY
rowList.append(cell)
forestGrid.append(rowList)
return forestGrid
#Parallel copy of InitializeForest Function
#@numba.jit(nopython=False, parallel=True)
def InitializeForestParallel(n,probTree,probBurning):
forestGrid = []
for x in range(n):
rowList = []
for y in range(n):
if (random()<probTree):
if(random()<probBurning):
cell = BURNING
else:
cell = TREE
else:
cell = EMPTY
rowList.append(cell)
forestGrid.append(rowList)
return forestGrid
def ReflectiveBoundaryCondition(forestGrid, n):
ExtendedGrid = []
GridNS = forestGrid
GridNorth = forestGrid[0]
GridSouth = forestGrid[n-1]
GridNS.insert(0,GridNorth)
GridNS.insert(n,GridSouth)
for x in range(n+2):
rowList = []
for y in range(n+2):
if (y == 0):
cell = GridNS[x][0]
elif(y == n+1):
cell = GridNS[x][y-2]
else:
cell = GridNS[x][y-1]
rowList.append(cell)
ExtendedGrid.append(rowList)
return ExtendedGrid
#@numba.jit(nopython=False, parallel=True)
def ReflectiveBoundaryConditionParallel(forestGrid, n):
ExtendedGrid = []
GridNS = forestGrid
GridNorth = forestGrid[0]
GridSouth = forestGrid[n-1]
GridNS.insert(0,GridNorth)
GridNS.insert(n,GridSouth)
for x in range(n+2):
rowList = []
for y in range(n+2):
if (y == 0):
cell = GridNS[x][0]
elif(y == n+1):
cell = GridNS[x][y-2]
else:
cell = GridNS[x][y-1]
rowList.append(cell)
ExtendedGrid.append(rowList)
return ExtendedGrid
def MooreNeighborhood(ExtForestGrid,x,y):
if (ExtForestGrid[x][y+1]==2):
BURNING = True
elif (ExtForestGrid[x][y-1]==2):
BURNING = True
elif (ExtForestGrid[x+1][y]==2):
BURNING = True
elif (ExtForestGrid[x-1][y]==2):
BURNING = True
elif (ExtForestGrid[x-1][y-1]==2):
BURNING = True
elif (ExtForestGrid[x+1][y-1]==2):
BURNING = True
elif (ExtForestGrid[x-1][y+1]==2):
BURNING = True
elif (ExtForestGrid[x+1][y+1]==2):
BURNING = True
else:
BURNING = False
return BURNING
#@numba.jit(nopython=False, parallel=True)
def MooreNeighborhoodParallel(ExtForestGrid,x,y):
if (ExtForestGrid[x][y+1]==2):
BURNING = True
elif (ExtForestGrid[x][y-1]==2):
BURNING = True
elif (ExtForestGrid[x+1][y]==2):
BURNING = True
elif (ExtForestGrid[x-1][y]==2):
BURNING = True
elif (ExtForestGrid[x-1][y-1]==2):
BURNING = True
elif (ExtForestGrid[x+1][y-1]==2):
BURNING = True
elif (ExtForestGrid[x-1][y+1]==2):
BURNING = True
elif (ExtForestGrid[x+1][y+1]==2):
BURNING = True
else:
BURNING = False
return BURNING
#This is the spread function that determines the state of the individual cell at the next iteration.
def Spread(ExtForestGrid,x,y,probImmune,probLightning):
if (ExtForestGrid[x][y] == 0):
cell = EMPTY
elif (ExtForestGrid[x][y] == 1):
if(random()<probImmune):
cell = TREE
elif (MooreNeighborhood(ExtForestGrid,x,y) or random()<probLightning):
cell = BURNING
else:
cell = TREE
else:
cell = EMPTY
return cell
#Parallel copy of FireSpread Function
#@numba.jit(nopython=False, parallel=True)
def SpreadParallel(ExtForestGrid,x,y,probImmune,probLightning):
if (ExtForestGrid[x][y] == 0):
cell = EMPTY
elif (ExtForestGrid[x][y] == 1):
if(random()<probImmune):
cell = TREE
elif (MooreNeighborhoodParallel(ExtForestGrid,x,y) or random()<probLightning):
cell = BURNING
else:
cell = TREE
else:
cell = EMPTY
return cell
#This function applies the function FireSpread to the inner cells in the extended grid.
def ApplySpread(ExtGrid,probImmune,probLightning):
newGrid = []
n = len(ExtGrid)
for x in range(1,n-1):
rowList = []
for y in range(1,n-1):
cell = Spread(ExtGrid,x,y,probImmune,probLightning)
rowList.append(cell)
newGrid.append(rowList.copy())
return newGrid
#Parallel copy of ApplyFireSpread Function
#@numba.jit(nopython=False, parallel=True)
def ApplySpreadParallel(ExtGrid,probImmune,probLightning):
newGrid = []
n = len(ExtGrid)
for x in range(1,n-1):
rowList = []
for y in range(1,n-1):
cell = SpreadParallel(ExtGrid,x,y,probImmune,probLightning)
rowList.append(cell)
newGrid.append(rowList.copy())
return newGrid
#Function to run simulation
def Simulation(n,probTree,probBurning,probLightning,probImmune,timeStep):
initialGrid = InitializeForest(n,probTree,probBurning)
itemCount = 0
gridList = []
gridList.append(initialGrid)
for t in range(timeStep):
ExtendedGrid = ReflectiveBoundaryCondition(gridList[itemCount],n)
newGrid = ApplySpread(ExtendedGrid,probImmune,probLightning)
gridList.append(newGrid)
itemCount = itemCount + 1
return gridList
#Parallel copy of RunSimulation Function
#@numba.jit(nopython=False, parallel=True)
def SimulationParallel(n,probTree,probBurning,probLightning,probImmune,timeStep):
initialGrid = InitializeForestParallel(n,probTree,probBurning)
itemCount = 0
gridList = []
gridList.append(initialGrid)
for t in range(timeStep):
ExtendedGrid = ReflectiveBoundaryConditionParallel(gridList[itemCount],n)
newGrid = ApplySpreadParallel(ExtendedGrid,probImmune,probLightning)
gridList.append(newGrid)
itemCount = itemCount + 1
return gridList
result = Simulation(500, probTree, probBurning, probLightning, probImmune, timeStep)
cmap = plt.colors.ListedColormap(['yellow','green','red'])
bounds=[-0.5,0.5,1.5,2.5]
norm = plt.colors.BoundaryNorm(bounds, cmap.N)
fig = pyplot.figure(figsize=(25/3, 6.25))
ax = fig.add_subplot(111)
ax.set_axis_off()
im = ax.imshow(result[0], cmap=cmap, norm=norm)#, interpolation='nearest')
def animate(i):
im.set_data(result[i])
anim = animation.FuncAnimation(fig, animate, interval=200, frames=len(result))
pyplot.show()