-
Notifications
You must be signed in to change notification settings - Fork 2
/
fire_simulation_model.py
280 lines (196 loc) · 6.81 KB
/
fire_simulation_model.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Nov 13 18:10:02 2018
@author: ameanasad
"""
import numpy as np
import random
import matplotlib.pyplot as plt
plt.rcParams['animation.ffmpeg_path'] = '/Users/ameanasad/anaconda/bin/ffmpeg'
import matplotlib.animation as animation
import math
def take_step(cell, wind_direction, wind_speed):
"""
Cell: Tuple of an x,y coordinates of one cell in the grid
- The function will randomly choose to take a step in the Moore
neighborhood of the given cell (ie the eight surrounding cells)
- Returns the position of a new cell that the step was taken in
"""
cell = np.array(cell)
potential_steps = [
[0,0],
[0,1],
[1,1],
[1,0],
[0,-1],
[-1,-1],
[-1,0],
[-1,1],
[1,-1]
]
for i in range(wind_speed//2):
potential_steps.append(wind_direction)
# Taking a step is similar to adding
step = np.array(random.choice(potential_steps))
return step + cell
def matrix_edge_detection(matrix, size):
"""
matrix: Array of the fire spread area
size: The size of the fire spread matrix
- Function will return the boundary cells of a spreading fire.
"""
boundary = []
# Size is decreased by 1 to avoid hitting dealing with the edge cases
for x in range(1,size- 1):
for y in range(1,size-1):
if matrix[x][y] >0:
if cell_edge_detection((x,y), matrix):
boundary.append((x,y))
return boundary
def remodel_matrix(matrix, new_fire_cells, moisture_matrix):
"""
matrix: Array of the fire spread area
new_fire_cells: list of tuples, each tuple representing the x,y coordinates
of a new cell that has been affected by the fire spread.
"""
for cell in new_fire_cells:
x = int(cell[0])
y = int(cell[1])
matrix[x][y] = 0.5*(moisture_matrix[x][y])
return matrix
def cell_edge_detection(cell, m):
"""
Detects if a cell is an edge of a fire growth model by checking the
Moore neighborhood of the cell.
cell: Tuple of x,y coordinates for a given cell
m: array o the fire spread area
Returns True if cell is an edge and False if not
"""
x = cell[0]
y = cell[1]
matrix_boundaries = [
m[x][y+1],
m[x+1][y+1],
m[x+1][y],
m[x+1][y-1],
m[x][y-1],
m[x-1][y-1],
m[x-1][y],
m[x-1][y+1] ]
if any(boundary==0 for boundary in matrix_boundaries):
return True
else:
return False
class node(object):
def __init__(self, x,y):
self.x = x
self.y = y
def get_loc(self):
return (self.x,self.y)
def __str__(self):
return "< " + str(self.x) + ', ' + str(self.y) + " >"
class fire_model(object):
def __init__(self, size):
self.size = size
self.spread_area = np.zeros((self.size, self.size))
self.moisture_matrix = np.true_divide(np.random.rand(self.size,self.size),2)+ np.ones((self.size,self.size))
def initialize_fire(self):
x_start = random.randint(1,self.size//2)
y_start = random.randint(1,self.size//2)
fire_start_location = node(x_start, y_start)
initial_coord = fire_start_location.get_loc()
self.spread_area[50, 50]= 1
return initial_coord
def take_fire_step(self):
boundary = matrix_edge_detection(self.spread_area, self.size-1)
new_fire_cells = []
for cell in boundary:
new_fire_cells.append(take_step(cell, [-1,0],40))
self.spread_area = remodel_matrix(self.spread_area, new_fire_cells, self.moisture_matrix)
return self.spread_area
def get_area(self):
return self.spread_area
Writer = animation.writers['ffmpeg']
writer = Writer(fps=15, metadata=dict(artist='Me'), bitrate=1800)
fig = plt.figure()
ax0 = fig.add_subplot(1,1,1)
global a
a = fire_model(100)
start_coordinates = a.initialize_fire()
print(start_coordinates)
#ax1.title(" Fire simulation with initalized start at: ")
#def animate(i):
#
# g = np.zeros((0,0))
#
# a.take_fire_step()
# g = a.get_area()
# ax0.clear()
# ax0.imshow(g, cmap='hot', interpolation='bilinear')
#ani = animation.FuncAnimation(fig, animate, interval=20)
#ani.save('fire_animation.mp4', writer=writer)
#
#plt.show()
#
fire = fire_model(100)
n = fire.initialize_fire()
for i in range(100):
fire.take_fire_step()
class area_model(object):
def __init___(self, size):
self.area_matrix = np.zeros((self.size, self.size))
def generate_model(self):
pass
def return_model(self):
return self.area_matrix
def cluster_generator(cluster_number, size, sample_points, bounding_box):
density_matrix = np.zeros((size,size))
cluster_points = []
house_locations = []
for i in range(cluster_number):
x = random.randint(bounding_box+1,size-(bounding_box +1))
y = random.randint(bounding_box+1,size-(bounding_box + 1))
cluster_points.append((x,y))
for cluster_point in cluster_points:
for cell in range(random.randint(0,sample_points//4)):
center_x = cluster_point[0]
center_y = cluster_point[1]
new_cell_x = random.randint(center_x-(bounding_box-1), center_x + (bounding_box-1))
new_cell_y = random.randint(center_y-(bounding_box-1), center_y + (bounding_box-1))
house_locations.append((new_cell_x,new_cell_y))
density_matrix[new_cell_x][new_cell_y] = 0.5
return density_matrix, house_locations
def moore_neighberhood_increase(m, cell_locations):
for location in cell_locations:
x = location[0]
y = location[1]
m[x][y+1] += 1
m[x+1][y+1] +=1
m[x+1][y] += 1
m[x+1][y-1] += 1
m[x][y-1] += 1
m[x-1][y-1] += 1
m[x-1][y] += 1
m[x-1][y+1] += 1
return m
d, locations = cluster_generator(20,100, 400, 5)
final_area = fire.get_area()
f = moore_neighberhood_increase(d, locations)
m = np.zeros((100,100))
for x in range(100):
for y in range(100):
if f[x][y] !=0 and final_area[x][y] !=0:
m[x][y] = f[x][y] + final_area[x][y]
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
ax1.matshow(final_area, cmap='hot', interpolation='bilinear')
plt.show()
fig = plt.figure()
ax2 = fig.add_subplot(1,1,1)
ax2.matshow(f, cmap='YlGnBu', interpolation='bilinear')
plt.show()
fig = plt.figure()
ax3 = fig.add_subplot(1,1,1)
ax3.matshow(m, cmap='YlGnBu', interpolation='bilinear')
plt.show()