Skip to content

Instantly share code, notes, and snippets.

@owstron
Created January 30, 2019 15:59
Show Gist options
  • Select an option

  • Save owstron/9a081386f17ea8ae3854465d6f66e621 to your computer and use it in GitHub Desktop.

Select an option

Save owstron/9a081386f17ea8ae3854465d6f66e621 to your computer and use it in GitHub Desktop.
import matplotlib
matplotlib.use('TkAgg')
import pylab as PL
import random as RD
import scipy as SP
import numpy as NP
RD.seed()
width = 100
height = 100
p = 0.4
empty, tree, fire, char = range(4)
simulation_stop = False
last_burned_area = 1
new_burned_area = 0
initial_num_trees = 0
consider_ignition = False
i = 0.2
def init():
global time, config, nextConfig, last_burned_area, new_burned_area, simulation_stop, initial_num_trees
time = 0
last_burned_area = 0
new_burned_area = 0
simulation_stop = False
config = SP.zeros([height, width])
for x in range(width):
for y in range(height):
if RD.random() < p:
state = tree
else:
state = empty
config[y, x] = state
config[height//2, width//2] = fire
initial_num_trees = NP.count_nonzero(config==tree)
nextConfig = SP.zeros([height, width])
def draw():
PL.cla()
PL.pcolor(config, vmin = 0, vmax = 3, cmap = PL.cm.binary)
PL.axis('image')
PL.title('t = ' + str(time))
def step():
global time, config, nextConfig, last_burned_area, new_burned_area, simulation_stop, initial_num_trees
time += 1
for x in range(width):
for y in range(height):
state = config[y, x]
if state == fire:
state = char
elif state == tree:
for dx in range(-1, 2):
for dy in range(-1, 2):
if config[(y+dy)%height, (x+dx)%width] == fire:
if consider_ignition:
if RD.random() < i:
state = fire
else:
state = fire
nextConfig[y, x] = state
config, nextConfig = nextConfig, config
new_burned_area = NP.count_nonzero(config==char)
if last_burned_area == new_burned_area:
simulation_stop = True
else:
last_burned_area = new_burned_area
def update_p(val = p):
global p
p = float(val)
return val
def update_i(val=i):
global i
i = float(val)
return(val)
# def simulate_fire():
# total_burned_area = []
# times_burnout = []
# per_burnout_arr = []
# probs = []
# for i in range(0, 101, 1):
# update_p(i/100)
# init()
# while not simulation_stop:
# step()
# total_burned_area.append(last_burned_area)
# times_burnout.append(time - 1)
# if initial_num_trees == 0:
# per_burnout_arr.append(1)
# else:
# per_burnout_arr.append(last_burned_area/initial_num_trees * 100)
# probs.append(i/100)
# return (probs, total_burned_area, times_burnout, per_burnout_arr)
# probs, total_burned_area, times_burnout, per_burnout_arr = simulate_fire()
# PL.figure()
# PL.subplot(1, 2, 1)
# PL.plot(probs, per_burnout_arr)
# PL.ylabel('Percentage of trees burned out')
# PL.xlabel('Initial tree density')
# PL.subplot(1, 2, 2)
# PL.plot(probs, times_burnout)
# PL.ylabel('Time taken for complete burnout')
# PL.xlabel('Initial tree density')
# PL.show()
import pycxsimulator
consider_ignition = True
gui = pycxsimulator.GUI(parameterSetters=[update_p, update_i])
gui.start(func=[init, draw, step])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment