-
Notifications
You must be signed in to change notification settings - Fork 16
/
Parallel_Wild_Fire.py
337 lines (272 loc) · 11.4 KB
/
Parallel_Wild_Fire.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
# Wild Fire Simulation Using Cellular Automaton and Parallel Computing
# Designed By Xiaochi (George) Li
# Final Project for High Performance Computing and Parallel Computing
# Data Science @ George Washington University
# May. 2018
# Algorithm based on this paper:A cellular automata model for forest fire spread prediction: The case
# of the wildfire that swept through Spetses Island in 1990
# Author: A. Alexandridis a, D. Vakalis b, C.I. Siettos c,*, G.V. Bafas a
# import os
import sys
import time
import math
import random
import copy
import itertools
# from tqdm import tqdm
from mpi4py import MPI
comm = MPI.COMM_WORLD
size = comm.Get_size()
rank = comm.Get_rank()
stat = MPI.Status()
try:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib import animation as animation
visual = True
except:
print("Error: This code need numpy and matplot to visualize!")
visual = False
if visual:
fig = plt.figure()
# ------------ Change the code below here for different initial environment ---------
# -------------Quick Change Begin-------------------------
# number of rows and columns of full grid and the generations
n_row_total = 300
n_col = 300
generation = 300
# the possibility a cell will continue to burn in next time step
# change the value to change the boundary of fire
p_continue_burn = 0.5
# Quick switch for factors in the model, turn on: True, turn off: False
wind = False
vegetation = False
density = False
altitude = False
# ------------- Quick Change End---------------------------------------
n_row = n_row_total // size + 2
thetas = [[45, 0, 45],
[90, 0, 90],
[135, 180, 135]]
def init_vegetation():
veg_matrix = [[0 for col in range(n_col)] for row in range(n_row)]
if vegetation == False: # turn off vegetation
for i in range(n_row):
for j in range(n_col):
veg_matrix[i][j] = 1
else:
for i in range(n_row):
for j in range(n_col):
if j <= n_col//3 : veg_matrix[i][j] = 1
elif j <= n_col*2//3: veg_matrix[i][j] = 2
else: veg_matrix[i][j] = 3
return veg_matrix
def init_density():
den_matrix = [[0 for col in range(n_col)] for row in range(n_row)]
if density == False: # turn off density
for i in range(n_row):
for j in range(n_col):
den_matrix[i][j] = 1
else:
for i in range(n_row):
for j in range(n_col):
if j <= n_col//3: den_matrix[i][j] = 1
elif j <= n_col*2//3: den_matrix[i][j] = 2
else: den_matrix[i][j] = 3
return den_matrix
def init_altitude():
alt_matrix = [[0 for col in range(n_col)] for row in range(n_row)]
if altitude == False: # turn off altitude
for i in range(n_row):
for j in range(n_col):
alt_matrix[i][j] = 1
else:
for i in range(n_row):
for j in range(n_col):
alt_matrix[i][j] = j
return alt_matrix
def init_forest():
forest = [[1 for col in range(n_col)] for row in range(n_row)]
for i in range(n_row):
if i == 0 or i == n_row - 1: # [parallel] sub grid initial exchange row is one
continue
for j in range(n_col):
if j == 0 or j == n_col - 1: # [parallel] sub grid initial margin column is one
continue
forest[i][j] = 2
ignite_col = int(n_col//2)
ignite_row = int(n_row//2)
if rank == size // 2: # [parallel] sub grid only ignite center sub grid
for row in range(ignite_row-1, ignite_row+1):
for col in range(ignite_col-1,ignite_col+1):
forest[row][col] = 3
# forest[ignite_row-2:ignite_row+2][ignite_col-2:ignite_col+2] = 3
return forest
# ------------------ Do not change anything below this line ----------------
# ------------------ Parallel Function ----------------
def msg_up(sub_grid):
# Sends and Receives rows with Rank+1
comm.send(sub_grid[n_row-2], dest=rank+1)
sub_grid[n_row-1] = comm.recv(source=rank+1)
return 0
def msg_down(sub_grid):
# Sends and Receives rows with Rank-1
comm.send(sub_grid[1], dest=rank-1)
sub_grid[0] = comm.recv(source=rank-1)
return 0
# ------------------ Parallel Function End ----------------
def colormap(title, array):
np_array = np.array(array)
plt.imshow(np_array, interpolation="none", cmap=cm.plasma)
plt.title(title)
plt.show()
def print_forest(title,forest):
print (title)
for i in range(len(forest)):
for j in range(n_col):
if not isinstance(title, int):
sys.stdout.write(str(forest[i][j]))
elif forest[i][j] == 1:
sys.stdout.write("\033[1;34;40m 1\033[0m") # blue for not burnable
elif forest[i][j] == 2:
sys.stdout.write("\033[1;32;40m 2\033[0m") # green for burnable
elif forest[i][j] == 3:
sys.stdout.write("\033[1;31;40m 3\033[0m") # red for burning
else:
sys.stdout.write("\033[1;30;40m 4\033[0m") # black for burned down
sys.stdout.write("\n")
print ("--------------------")
def tg(x):
return math.degrees(math.atan(x))
def get_slope(altitude_matrix):
slope_matrix = [[0 for col in range(n_col)] for row in range(n_row)]
for row in range(n_row):
for col in range(n_col):
sub_slope_matrix = [[0,0,0],[0,0,0],[0,0,0]]
if row == 0 or row == n_row-1 or col == 0 or col == n_col-1: # margin is flat
slope_matrix[row][col] = sub_slope_matrix
continue
current_altitude = altitude_matrix[row][col]
sub_slope_matrix[0][0] = tg((current_altitude - altitude_matrix[row-1][col-1])/1.414)
sub_slope_matrix[0][1] = tg(current_altitude - altitude_matrix[row-1][col])
sub_slope_matrix[0][2] = tg((current_altitude - altitude_matrix[row-1][col+1])/1.414)
sub_slope_matrix[1][0] = tg(current_altitude - altitude_matrix[row][col-1])
sub_slope_matrix[1][1] = 0
sub_slope_matrix[1][2] = tg(current_altitude - altitude_matrix[row][col+1])
sub_slope_matrix[2][0] = tg((current_altitude - altitude_matrix[row+1][col-1])/1.414)
sub_slope_matrix[2][1] = tg(current_altitude - altitude_matrix[row+1][col])
sub_slope_matrix[2][2] = tg((current_altitude - altitude_matrix[row+1][col+1])/1.414)
slope_matrix[row][col] = sub_slope_matrix
return slope_matrix
def calc_pw(theta):
c_1 = 0.045
c_2 = 0.131
V = 10
t = math.radians(theta)
ft = math.exp(V*c_2*(math.cos(t)-1))
return math.exp(c_1*V)*ft
def get_wind():
wind_matrix = [[0 for col in [0,1,2]] for row in [0,1,2]]
for row in [0,1,2]:
for col in [0,1,2]:
wind_matrix[row][col] = calc_pw(thetas[row][col])
wind_matrix[1][1] = 0
if wind == False: # turn off wind
wind_matrix = [[1 for col in [0,1,2]] for row in [0,1,2]]
return wind_matrix
def burn_or_not_burn(abs_row,abs_col,neighbour_matrix):
p_veg = {1:-0.3,2:0,3:0.4}[vegetation_matrix[abs_row][abs_col]]
p_den = {1:-0.4,2:0,3:0.3}[density_matrix[abs_row][abs_col]]
p_h = 0.58
a = 0.078
for row in [0,1,2]:
for col in [0,1,2]:
if neighbour_matrix[row][col] == 3: # we only care there is a neighbour that is burning
# print(row,col)
slope = slope_matrix[abs_row][abs_col][row][col]
p_slope = math.exp(a * slope)
p_wind = wind_matrix[row][col]
p_burn = p_h * (1 + p_veg) * (1 + p_den) * p_wind * p_slope
if p_burn > random.random():
return 3 #start burning
return 2 # not burning
def update_forest(old_forest):
result_forest = [[1 for i in range(n_col)] for j in range(n_row)]
for row in range(1, n_row-1):
for col in range(1, n_col-1):
if old_forest[row][col] == 1 or old_forest[row][col] == 4:
result_forest[row][col] = old_forest[row][col] # no fuel or burnt down
if old_forest[row][col] == 3:
if random.random() < p_continue_burn:
result_forest[row][col] = 3 # We can change here to control the burning time
else:
result_forest[row][col] = 4
if old_forest[row][col] == 2:
neighbours = [[row_vec[col_vec] for col_vec in range(col-1, col+2)]
for row_vec in old_forest[row-1:row+2]]
# print(neighbours)
result_forest[row][col] = burn_or_not_burn(row, col, neighbours)
return result_forest
# start simulation
vegetation_matrix = init_vegetation()
density_matrix = init_density()
altitude_matrix = init_altitude()
wind_matrix = get_wind()
slope_matrix = get_slope(altitude_matrix)
sub_forest = init_forest() # [parallel] each worker has their own sub grid
# draw initial condition in colour map
# TODO: need to change for parallel version
if rank == 0:
if visual:
colormap("Vegetation Map", vegetation_matrix)
colormap("Density Map", density_matrix)
colormap("Altitude Map", altitude_matrix)
else:
print_forest("Vegetation Map", vegetation_matrix)
print_forest("Density Map", density_matrix)
print_forest("Altitude Map", altitude_matrix)
print("This is the wind matrix:")
for row in wind_matrix:
print(row)
time.sleep(1)
ims = []
# for i in tqdm(range(generation)):
for i in range(generation): # tqdm will damage the visualization in Terminal
sub_forest = copy.deepcopy(update_forest(sub_forest))
# [parallel] message passing function
if rank == 0:
msg_up(sub_forest)
elif rank == size - 1:
msg_down(sub_forest)
else:
msg_up(sub_forest)
msg_down(sub_forest)
# transform the list to np array so we can use np.vstack later
# np_temp_grid = np.array(sub_forest[1:n_row - 1])
# temp_grid = comm.gather(np_temp_grid, root=0)
temp_grid = comm.gather(sub_forest[1:n_row - 1], root=0)
# [parallel] only worker 0 do the visualize
if rank == 0:
list_forest = list(itertools.chain.from_iterable(temp_grid))
if visual:
print(i, "/", generation)
new_forest = np.vstack(list_forest)
im = plt.imshow(new_forest, animated=True, interpolation="none", cmap=cm.plasma)
# plt.title(i)
ims.append([im])
# colormap(i,new_forest)
else:
# os.system("clear")
time.sleep(1)
print("-----------Generation:", i, "---------------")
# list_forest = new_forest.tolist()
# print (list_forest)
print_forest(i,list_forest)
# for components_of_forest in list_forest:
# print_forest(i, components_of_forest)
# print(new_forest)
if visual and rank == 0:
ani = animation.ArtistAnimation(fig, ims, interval=25, blit=True,repeat_delay=500)
# ani.save('animate_life.html')
plt.show()