I'm simulating forest-fire, and one of my tasks is to plot the density of trees vs those currently burning and empty plots. I have the disparate parts, however I need assistance in putting them together as I can't work out how to put my code together. Currently, I have my initial conditions
p, f = 0.5, 0.3
nx, ny = 100, 100
X = np.zeros((ny, nx))
adjacent = ((-1,0), (0,-1), (0, 1), (1,0))
E, T, F = 0, 1, 2
xvalues = [0]
yvalues = [0]
my function that generates the next frame (distribution of fire) is
def iterate(X):
Xnew = np.zeros((ny, nx))
for ix in range(1,nx-1):
for iy in range(1,ny-1):
if X[iy,ix] == E and np.random.random() <= p:
Xnew[iy,ix] = T
if X[iy,ix] == T:
Xnew[iy,ix] = T
for dx,dy in adjacent:
if X[iy+dy,ix+dx] == F:
Xnew[iy,ix] = F
else:
if np.random.random() <= f:
Xnew[iy,ix] = F
return Xnew
print(Xnew)
The bit I'm struggling with is how to write the following correctly with the above material so that I could go up to Xn where n is about 1000
X1 = iterate(X)
X2 = iterate(X1)
X3 = iterate(X2) and so on
and for each iteration calculate
num_empty = (Xn == 0).sum()
num_tree = (Xn == 1).sum()
num_fire = (Xn == 2).sum()
density = num_tree/(num_fire+num_empty)
xvalues.append(i)
yvalues.append(density)
print(density)
Any help would be appreciated!
I think you need to iterate over the range of n ints rather than "function".
i = 0
_X = iteration(X)
num_empty = (_X == 0).sum()
num_tree = (_X == 1).sum()
num_fire = (_X == 2).sum()
density = num_tree / (num_fire + num_empty)
print(i, density)
xvalues.append(i)
yvalues.append(density)
n = 1000
for i in range(1, n):
_X = iteration(_X)
num_empty = (_X == 0).sum()
num_tree = (_X == 1).sum()
num_fire = (_X == 2).sum()
density = num_tree / (num_fire + num_empty)
print(i, density)
xvalues.append(i)
yvalues.append(density)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.