I’m having some trouble iterating my simulation of a forest fire. I’m new to mathematica, so apologies for poor coding. I’m sure there are far neater and more efficient ways of doing this, but i would like to know whats the problem with the method im using currently if possible.
This is my code to get the adjacent elements, including ‘wrap around’:
neighbourPos[m_, i_, j_] := Transpose[Mod[{i, j} + {{-1, 0, 1, 0}, {0, -1, 0, 1}}, Dimensions[m],]];
For the fire, 2s represent burning trees, 1s trees and 0s empty ground. After one timestep, trees adjacent to a fire will set on fire and original fire(s) will burn themselves out and become ‘0s’. This my code to simulate the spread of the fire:
nextState[m_] := (firepos = Position[m, 2]; a = MapAt[2 # &, m, neighbourPos[m, Position[m, 2][[1, 1]], Position[m, 2][[1, 2]]]]; MapAt[0 &, a, firepos])
It seems to work. Up to a point anyway. I would like to eventually plot the thing with ListAnimate, however, When i try to iterate nextState until the fire is burnt out with FixedPointList it stops working properly after 6 iterations and i get a load of errors.
e.g
MatrixPlot[mat/2, PlotRange -> {0, 1}, ColorFunction -> "Rainbow", ColorFunctionScaling -> False]
Gives
Then
MatrixPlot[nextState[mat]/2, PlotRange -> {0, 1}, ColorFunction -> "Rainbow", ColorFunctionScaling -> False]
But then errors come after more iterations.
In[1]:= fireStates[m_] := FixedPointList[nextState, m]; In[14]:= frames = fireStates[mat]; During evaluation of In[14]:= Part::partw: Part 1 of {} does not exist. During evaluation of In[14]:= Part::partw: Part 1 of {} does not exist. During evaluation of In[14]:= MapAt::pkspec1: The expression Mod[1+{}[[1,1]],10,1] cannot be used as a part specification. During evaluation of In[14]:= Thread::tdlen: Objects of unequal length in Mod[{{0,1,2,1},{1,0,1,2}},{3},1] cannot be combined. During evaluation of In[14]:= MapAt::psl: Position specification Transpose[Mod[{{0,1,2,1},{1,0,1,2}},{3},1]] in MapAt[2 #1&,MapAt[2 #1&,<<1>>,{{Mod[-1+{}[[1,1]],10,1],Mod[{}[[1,2]],10,1]},{<<1>>,Mod[<<1>>]},<<1>>,{Mod[{}[[1,1]],10,1],Mod[1+{}[[1,2]],10,1]}}],Transpose[Mod[{{0,1,2,1},{1,0,1,2}},{3},1]]] is not a machine-sized integer or a list of machine-sized integers. During evaluation of In[14]:= MapAt::partw: Part {3,4,2,1,2,3} of MapAt[2 #1&,MapAt[2 #1&,<<1>>,{{Mod[-1+{}[[1,1]],10,1],Mod[{}[[1,2]],10,1]},{<<1>>,Mod[<<1>>]},<<1>>,{Mod[{}[[1,1]],10,1],Mod[1+{}[[1,2]],10,1]}}],Transpose[Mod[{{0,1,2,1},{1,0,1,2}},{3},1]]] does not exist. During evaluation of In[14]:= Thread::tdlen: Objects of unequal length in Mod[{{1,2,3,2},{1,0,1,2}},{3},1] cannot be combined. During evaluation of In[14]:= MapAt::psl: Position specification Transpose[Mod[{{1,2,3,2},{1,0,1,2}},{3},1]] in MapAt[2 #1&,MapAt[0&,MapAt[2 #1&,MapAt[<<1>>],Transpose[Mod[<<1>>]]],{{1,1,1},{3,1,2,1,3},{3,2,2,1,2,3},{3,3,2,1,3},{3,4,2,1,2,3}}],Transpose[Mod[{{1,2,3,2},{1,0,1,2}},{3},1]]] is not a machine-sized integer or a list of machine-sized integers. During evaluation of In[14]:= MapAt::partw: Part {2,2,3,4,2,1,2,3} of MapAt[2 #1&,MapAt[0&,MapAt[2 #1&,MapAt[<<1>>],Transpose[Mod[<<1>>]]],{{1,1,1},{3,1,2,1,3},{3,2,2,1,2,3},{3,3,2,1,3},{3,4,2,1,2,3}}],Transpose[Mod[{{1,2,3,2},{1,0,1,2}},{3},1]]] does not exist. During evaluation of In[14]:= Thread::tdlen: Objects of unequal length in Mod[{{1,2,3,2},{1,0,1,2}},{3},1] cannot be combined. During evaluation of In[14]:= General::stop: Further output of Thread::tdlen will be suppressed during this calculation. During evaluation of In[14]:= MapAt::psl: Position specification Transpose[Mod[{{1,2,3,2},{1,0,1,2}},{3},1]] in MapAt[2 #1&,MapAt[0&,MapAt[<<1>>],{{2,1,1,1},{2,2,1,1,1},{2,2,3,1,2,1,3},{2,2,3,2,2,1,2,3},<<3>>,{2,3,1,1,2,4},{3,2,3},{3,3,2},<<5>>}],Transpose[Mod[{{1,2,3,2},{1,0,1,2}},{3},1]]] is not a machine-sized integer or a list of machine-sized integers. During evaluation of In[14]:= General::stop: Further output of MapAt::psl will be suppressed during this calculation. During evaluation of In[14]:= MapAt::partw: Part {2,2,2,2,3,4,2,1,2,3} of MapAt[2 #1&,MapAt[0&,MapAt[<<1>>],{{2,1,1,1},{2,2,1,1,1},{2,2,3,1,2,1,3},{2,2,3,2,2,1,2,3},<<3>>,{2,3,1,1,2,4},{3,2,3},{3,3,2},<<5>>}],Transpose[Mod[{{1,2,3,2},{1,0,1,2}},{3},1]]] does not exist. During evaluation of In[14]:= General::stop: Further output of MapAt::partw will be suppressed during this calculation.
Many many thanks in advance for anyone kind enough to help.
Cheers.