Making a simulation of burning forest in R 2018-02-26T16:08:30+00:00

Project Description

This was a mathematical simulation of burning forest. Basically I simulated a random forest (the parameters of how it should look like can be adjusted) and based on the flammability and weather conditions I simulate the fire in the forest. The wind speed and wind deviation play an important role in overall spreading. Also the probability that certain tree will catch fire makes a big change. You can see the plot that is changing in real time on trees (green fields), burning trees (yellow fields) and already burned trees (black fields) how the fire progresses in real time. The project was successful because it sufficiently simulates the fire.

Example run

In example run we decrease time of burning and flamability so the example won't run for too long. As a result, smaller amount of forest is burned.

GIF Forest

Code:


### Copyright, Nejc Znidar(C)

sansa <- function(verjetnost){
x <- runif(1,0,1)
if (x>verjetnost){
x <- 0
}
else{
x <- 1
}
return(x)
}

definiraj.matriko <- function(forestation,x,y){
vektor <- replicate(x*y, sansa(forestation))
return(matrix(vektor,x,y))
}

postavitev.gozda <- function(matrika, x, y, cas,i,j){
if (matrika[i,j] == 1){
rect(i-1, y-j, i, y-j+1, col="green",border="transparent")
}
if (matrika[i,j] > 1 & matrika[i,j] < cas + 1){
rect(i-1, y-j, i, y-j+1, col="yellow",border="transparent")
}
if (matrika[i,j] >= cas + 1){
rect(i-1, y-j, i, y-j+1, col="black",border="transparent")
}
}
#podaj zacetni položaj in položaj v obliki vektorja c(i,j)
verjetnost.prenosa <- function (zacetni.polozaj, kot, flammability,polozaj){
if (zacetni.polozaj[1] == polozaj[1] -1){
a <- ((sin(kot))**2 * flammability)
}
if (zacetni.polozaj[2] == polozaj[2] -1){
a <- ((cos(kot))**2 * flammability)
}
t <- sansa(a) + 1
return(t)
}

veter <- function(smer, odklon){
return(runif(1,smer,odklon))
}

gozd <- function(x,y,forestation,wind.dir,wind.deviation,flammability,duration){
A <- definiraj.matriko(forestation,x,y)
i <- round(x/2)
j <- round(y/2)
A[i,j] <- 1.1
plot(c(0,x),c(0,y),type="n", main = "Fire in the forest",xlab="",ylab="")
i <- 1
j <- 1
while (length(
intersect(as.vector(A)[as.vector(A)>1], as.vector(A)[as.vector(A)<duration + 1])) != 0){
i <- 1
while (i <= x){
j <- 1
while (j <= y){
postavitev.gozda(A, x, y, duration,i,j)
if ((A[i,j] != 1) & (A[i,j] != 0) & (A[i,j] < (duration + 1))){
A[i,j] <- A[i,j] + 1
postavitev.gozda(A, x, y, duration,i,j)
if (j != 1 && A[i,j-1] == 1){
sprem <- verjetnost.prenosa(c(i,j), veter(wind.dir, wind.deviation), flammability,c(i,j+1))#pazi na napako
A[i,j-1] <- sprem
}
if (j != 20 && A[i,j+1] == 1){
sprem <- verjetnost.prenosa(c(i,j), veter(wind.dir, wind.deviation), flammability,c(i,j+1))
A[i,j+1] <- sprem
}
if (i != 1 && A[i-1,j] == 1){
sprem <- verjetnost.prenosa(c(i,j), veter(wind.dir, wind.deviation), flammability,c(i+1,j))#pazi na napako
A[i-1,j] <- sprem
}
if (i != 20 && A[i+1,j] == 1){
sprem <- verjetnost.prenosa(c(i,j), veter(wind.dir, wind.deviation), flammability,c(i+1,j))
A[i+1,j] <- sprem
}
}
j <- j +1
}
i <- i + 1
}
Sys.sleep(0.3)
}
return(print("Fire has been stopped!"))
}
gozd(40,40,0.8,30,40,0.7,8)

 

error: Content is protected !!