-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFinal Project.py
350 lines (283 loc) · 10.5 KB
/
Final Project.py
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
# coding: utf-8
# In[72]:
from collections import namedtuple
import random
import sys
import math
import time
import copy
def printf(format, *args):
sys.stdout.write(format % args)
def showforest(nx,ny,t):
for i in range(ny):
for j in range(nx):
if tnew[i][j].STATE == 'F':
printf('\033[41m'"%c "'\033[0m',tnew[i][j].STATE)
elif tnew[i][j].STATE == '^':
printf('\033[42m'"%c "'\033[0m',tnew[i][j].STATE)
elif tnew[i][j].STATE == '.':
printf('\033[100m'"%c "'\033[0m',tnew[i][j].STATE)
else:
printf('\033[0m'"%c "'\033[0m',tnew[i][j].STATE)
printf("\n")
printf('\x1b[2J\x1b[H')
# time.sleep(1)
def showforest_persist(nx,ny,t):
for i in range(ny):
for j in range(nx):
if tnew[i][j].STATE == 'F':
printf('\033[41m'"%c "'\033[0m',tnew[i][j].STATE)
elif tnew[i][j].STATE == '^':
printf('\033[42m'"%c "'\033[0m',tnew[i][j].STATE)
elif tnew[i][j].STATE == '.':
printf('\033[100m'"%c "'\033[0m',tnew[i][j].STATE)
else:
printf('\033[0m'"%c "'\033[0m',tnew[i][j].STATE)
printf("\n")
NX = 30
NY = 30
generations = 20
Forest = namedtuple('Forest', 'STATE P B f')
tnew = []
for i in range(NY):
new = []
for j in range(NX):
Node = Forest(' ',0.002,0.99,0.002)
new.append(Node)
tnew.append(new)
# Fill forest with trees
Dinit = 1
for i in range(1,NY-1):
for j in range(1,NX-1):
tnew[i][j] = tnew[i][j]._replace(STATE='^')
for step in range(generations):
t = copy.deepcopy(tnew)
# print(id(t),id(tnew),id(t[0]),id(tnew[0]),id(new),id(t[0][0]))
for i in range(1,NY-1):
for j in range(1,NX-1):
#A burning tree becomes an empty site.
if t[i][j].STATE == 'F':
tnew[i][j] = tnew[i][j]._replace(STATE='.')
# If cell is unburnt but has burning neighbors see
# if cell ignites.
if t[i][j].STATE == '^':
#A tree without a burning nearest neighbor becomes a burning tree with probability f.
if t[i][j].f > random.random():
tnew[i][j] = tnew[i][j]._replace(STATE='F')
# Either a corner neighbor possibly ignites cell
# or side neighbor;
# Corner neighbor influence is suppressed but 1/sqrt(2)
# 0.293 is approx = 1-1/sqrt(2)
elif 0.293 > random.random():
if (t[i+1][j+1].STATE=='F')or(t[i-1][j+1].STATE=='F')or(t[i+1][j-1].STATE=='F')or(t[i-1][j-1].STATE=='F'):
if t[i][j].B > random.random():
tnew[i][j] = tnew[i][j]._replace(STATE='F')
else:
if (t[i-1][j].STATE == 'F')or(t[i][j-1].STATE == 'F')or(t[i][j+1].STATE == 'F')or(t[i+1][j].STATE == 'F') :
if t[i][j].B > random.random():
tnew[i][j] = tnew[i][j]._replace(STATE='F')
#At an empty site, a tree grows with probability p.
if t[i][j].STATE == '.':
if t[i][j].P > random.random():
tnew[i][j] = tnew[i][j]._replace(STATE='^')
showforest(NX,NY,tnew)
showforest_persist(NX,NY,tnew)
# In[54]:
from collections import namedtuple
import random
import sys
import math
import time
import copy
def printf(format, *args):
sys.stdout.write(format % args)
def showforest(nx,ny,t):
for i in range(ny):
for j in range(nx):
if tnew[i][j].STATE == 'F':
printf('\033[41m'"%c "'\033[0m',tnew[i][j].STATE)
elif tnew[i][j].STATE == '^':
printf('\033[42m'"%c "'\033[0m',tnew[i][j].STATE)
elif tnew[i][j].STATE == '.':
printf('\033[100m'"%c "'\033[0m',tnew[i][j].STATE)
else:
printf('\033[0m'"%c "'\033[0m',tnew[i][j].STATE)
printf("\n")
printf('\x1b[2J\x1b[H')
# time.sleep(1)
def showforest_persist(nx,ny,t):
for i in range(ny):
for j in range(nx):
if tnew[i][j].STATE == 'F':
printf('\033[41m'"%c "'\033[0m',tnew[i][j].STATE)
elif tnew[i][j].STATE == '^':
printf('\033[42m'"%c "'\033[0m',tnew[i][j].STATE)
elif tnew[i][j].STATE == '.':
printf('\033[100m'"%c "'\033[0m',tnew[i][j].STATE)
else:
printf('\033[0m'"%c "'\033[0m',tnew[i][j].STATE)
printf("\n")
NX = 30
NY = 30
generations = 20
Forest = namedtuple('Forest', 'STATE B I D')
tnew = []
for i in range(NY):
new = []
for j in range(NX):
#Node = Forest('E',random.random(),random.random(),random.random())
Node = Forest(' ',0.5,0.99,random.random())
new.append(Node)
tnew.append(new)
# Fill forest with trees
for i in range(1,NY-1):
for j in range(1,NX-1):
if Dinit - tnew[i][j].D > 0:
tnew[i][j] = tnew[i][j]._replace(STATE='^')
#Start a fire in the middle of the grid */
# Constant source fire */ // tnew[nx/2][ny/2].B = 1;
tnew[NY//2][NX//2] = tnew[NY//2][NX//2]._replace(STATE = 'F');
for step in range(generations):
t = copy.deepcopy(tnew)
# print(id(t),id(tnew),id(t[0]),id(tnew[0]),id(new),id(t[0][0]))
for i in range(1,NY-1):
for j in range(1,NX-1):
#If a cell is burning see if it continues burning
#otherwise the fire goes out.
if t[i][j].STATE == 'F':
if t[i][j].B < random.random():
tnew[i][j] = tnew[i][j]._replace(STATE='.')
# If cell is unburnt but has burning neighbors see
# if cell ignites.
if t[i][j].STATE == '^':
# Either a corner neighbor possibly ignites cell
# or side neighbor;
# Corner neighbor influence is suppressed but 1/sqrt(2)
# 0.293 is approx = 1-1/sqrt(2)
if 0.293 > random.random():
if (t[i+1][j+1].STATE=='F')or(t[i-1][j+1].STATE=='F')or(t[i+1][j-1].STATE=='F')or(t[i-1][j-1].STATE=='F'):
if t[i][j].I > random.random():
tnew[i][j] = tnew[i][j]._replace(STATE='F')
else :
if (t[i-1][j].STATE == 'F')or(t[i][j-1].STATE == 'F')or(t[i][j+1].STATE == 'F')or(t[i+1][j].STATE == 'F') :
if t[i][j].I > random.random():
tnew[i][j] = tnew[i][j]._replace(STATE='F')
showforest(NX,NY,tnew)
showforest_persist(NX,NY,tnew)
# In[ ]:
#parallelized version
from collections import namedtuple
import random
import sys
import math
import time
import copy
import itertools
from mpi4py import MPI
comm = MPI.COMM_WORLD
size = comm.Get_size()
rank = comm.Get_rank()
stat = MPI.Status()
NX = 50
NY = 50
generations = 50
if size > NY:
print("Not enough ROWS")
exit()
subGridRowSize = NY//size + 2
def printf(format, *args):
sys.stdout.write(format % args)
def showforest(nx,ny,tnew):
for i in range(ny):
for j in range(nx):
if tnew[i][j].STATE == 'F':
printf('\033[41m'"%c "'\033[0m',tnew[i][j].STATE)
elif tnew[i][j].STATE == '^':
printf('\033[42m'"%c "'\033[0m',tnew[i][j].STATE)
elif tnew[i][j].STATE == '.':
printf('\033[100m'"%c "'\033[0m',tnew[i][j].STATE)
else:
printf('\033[0m'"%c "'\033[0m',tnew[i][j].STATE)
printf("\n")
printf('\x1b[2J\x1b[H')
def showforest_persist(nx,ny,tnew):
for i in range(ny):
for j in range(nx):
if tnew[i][j].STATE == 'F':
printf('\033[41m'"%c "'\033[0m',tnew[i][j].STATE)
elif tnew[i][j].STATE == '^':
printf('\033[42m'"%c "'\033[0m',tnew[i][j].STATE)
elif tnew[i][j].STATE == '.':
printf('\033[100m'"%c "'\033[0m',tnew[i][j].STATE)
else:
printf('\033[0m'"%c "'\033[0m',tnew[i][j].STATE)
printf("\n")
def sendDown(subGrid):
comm.send(subGrid[1],dest=rank-1)
subGrid[0] = comm.recv(source=rank-1)
def sendUp(subGrid):
comm.send(subGrid[subGridRowSize-2],dest=rank+1)
subGrid[subGridRowSize-1]=comm.recv(source=rank+1)
def forestfire(tnew):
t = copy.deepcopy(tnew)
# print(id(t),id(tnew),id(t[0]),id(tnew[0]),id(new),id(t[0][0]))
for i in range(1,subGridRowSize-1):
for j in range(1,NX-1):
#A burning tree becomes an empty site.
if t[i][j].STATE == 'F':
tnew[i][j] = tnew[i][j]._replace(STATE='.')
# If cell is unburnt but has burning neighbors see
# if cell ignites.
if t[i][j].STATE == '^':
#A tree without a burning nearest neighbor becomes a burning tree with probability f.
if t[i][j].f > random.random():
tnew[i][j] = tnew[i][j]._replace(STATE='F')
# Either a corner neighbor possibly ignites cell
# or side neighbor;
# Corner neighbor influence is suppressed but 1/sqrt(2)
# 0.293 is approx = 1-1/sqrt(2)
elif 0.293 > random.random():
if (t[i+1][j+1].STATE=='F')or(t[i-1][j+1].STATE=='F')or(t[i+1][j-1].STATE=='F')or(t[i-1][j-1].STATE=='F'):
if t[i][j].B > random.random():
tnew[i][j] = tnew[i][j]._replace(STATE='F')
else:
if (t[i-1][j].STATE == 'F')or(t[i][j-1].STATE == 'F')or(t[i][j+1].STATE == 'F')or(t[i+1][j].STATE == 'F') :
if t[i][j].B > random.random():
tnew[i][j] = tnew[i][j]._replace(STATE='F')
#At an empty site, a tree grows with probability p.
if t[i][j].STATE == '.':
if t[i][j].P > random.random():
tnew[i][j] = tnew[i][j]._replace(STATE='^')
return tnew
Forest = namedtuple('Forest', 'STATE P B f')
tnew = []
for i in range(subGridRowSize):
new = []
for j in range(NX):
Node = Forest(' ',0.002,0.99,0.002)
new.append(Node)
tnew.append(new)
# Fill forest with trees
Dinit = 1
for i in range(1,subGridRowSize-1):
for j in range(1,NX-1):
tnew[i][j] = tnew[i][j]._replace(STATE='^')
if rank == size // 2:
print "ignite:",rank
tnew[subGridRowSize//2][NX//2] = tnew[subGridRowSize//2][NX//2]._replace(STATE = 'F')
time.sleep(2)
for i in range(generations):
tnew = forestfire(tnew)
if rank == 0:
sendUp(tnew)
elif rank == size -1:
sendDown(tnew)
else:
sendUp(tnew)
sendDown(tnew)
finalGrid = comm.gather(tnew[1:subGridRowSize-1],root=0)
if rank == 0:
list_forest = list(itertools.chain.from_iterable(finalGrid))
showforest_persist(NX,NY-2,list_forest) # [changed to NY-2 to match the number of columns]
print "----------------------------" #[print a line to show more clearly]
time.sleep(1)