From 22a0cf67fb5a0e150b12fa10d0070a8b675aadf0 Mon Sep 17 00:00:00 2001 From: Payton Thomas Date: Fri, 28 Jan 2022 21:44:37 -0700 Subject: [PATCH] Fixed convolutions --- model.py | 23 +++++++++++++++-------- test.py | 7 ++++--- util.py | 10 ++++++++-- 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/model.py b/model.py index fd14c75..ec39f8c 100644 --- a/model.py +++ b/model.py @@ -1,10 +1,9 @@ import matplotlib import numpy as np -from scipy.ndimage import convolve import util '''features to add: -basic functionality + ''' @@ -12,7 +11,7 @@ #tempHigh: vector of high temperatures over the next 14 days #tempLow: vector of low temperatures over the next 14 days #precipVec: vector of precipitation values (decimal) over the next 14 days -#windVec: vector of wind speeds over the next 14 days (all northwesternly) +#windVec: vector of wind speeds over the next 14 days (all southeasternly) #humidVec: vector of humidity values over the next 14 days #density: tree density #r: side length of partitions in km @@ -36,13 +35,21 @@ def __init__(self, initialState, tempHigh, tempLow, precip, wind, humid, density self.temp = tempLow[0] #figure this out in more detail later self.humid = humid[0] self.precip = precip[0] + self.mask = np.ones(initialState.shape) def fireSpread(self): - kernel = util.findKernel(self.state, self.wind, self.humid, self.precip) - self.state = convolve(self.state, kernel, mode='constant') + kernel = util.findKernel(self.state, self.wind, self.humid, self.precip) #calculate kernel for convolution + self.state = util.maskConvolve(self.state, kernel, self.mask) #convolve but skipping burnt out cells + self.burnout() #check if any regions are over 100% burned - #iterate over all matrix entries, randomly determine spread according to temperature, density, humiditiy, wind + #need to add sensitivity to wind, humidity, and preciptiation + def burnout(self): + for i in range(self.state.shape[0]): + for j in range(self.state.shape[1]): + if self.state[i,j] >= 1: + self.state[i,j] = 0 + self.mask[i,j] = 0 def timeStep(self): - #fireSpread and fireGrow; increment t - pass \ No newline at end of file + self.fireSpread() + self.t += 1 \ No newline at end of file diff --git a/test.py b/test.py index 30e08ae..5775cfb 100644 --- a/test.py +++ b/test.py @@ -3,8 +3,8 @@ import model import time -initialState = np.zeros((5,5)) -initialState[2,0] = 0.1 +initialState = np.zeros((36,88)) +initialState[18,44] = 0.1 tempHigh = np.array([94,87,86,97,98,89,92,96,98,99,103,103,97,91]) tempLow = np.array([60,56,55,59,61,61,57,58,59,64,65,64,62,57]) @@ -18,7 +18,8 @@ plt.imshow(testModel.state, cmap=plt.get_cmap('inferno')) plt.show for i in range(7): - testModel.fireSpread() + testModel.timeStep() + #print(testModel.state) plt.figure(i+2) plt.imshow(testModel.state, cmap=plt.get_cmap('inferno')) diff --git a/util.py b/util.py index c8350c0..700e3ad 100644 --- a/util.py +++ b/util.py @@ -1,4 +1,10 @@ import numpy as np +from scipy.ndimage import convolve -def findKernel(state,wind,humidity,precip): - return np.ones((3,3)) \ No newline at end of file +def findKernel(state,wind,humidity,precip): + kernel = np.ones((3,3)) + return kernel + +def maskConvolve(state,kernel,mask): + conv = convolve(state, kernel, mode='constant') + return np.where(mask, conv, state) \ No newline at end of file