Abelian Sandpile Model General Problem
Show older comments
I'm trying to develop an Abelian sandpile model, but I'm running into trouble.
I'm picking Matlab up from scratch, I've no experience with programming whatsoever & progress is painfully slow.
I've found a complete program that does what I want, but I want to develop my own program and understand/follow what I've done. http://www.mathworks.com/matlabcentral/fileexchange/18215-sandpile/content//sandpile.m
I understand the error messages I'm getting, but I just don't know how to fix them. If you could help me debug and send me in the right direction, I'd be very grateful.
I'd also like to add a counter for grains that fall off the grid, but I've no idea how to do it.
%Cellular automaton model (CA) of a sandpile on a 2-dimensional grid. The size of the grid matrix is given by size (e.g. [20 20]) and nrsteps is a scalar positive integer defining the number of time steps of the model run.
Each time step a sandgrain is added to a random location on the grid. When the critical number of grains in a cell exceeds 3 all grains in the cell are turned to the 4 neighbors (v. Neumann Neighborhood). This avalanche may prograde and trigger even more avalanches. After a while the sandpile comes to a state of self-organized criticality.%
size = [10 10]; %size is a 10x10 matrix
n = 0; %step counter
nrsteps = 1000; %num. of steps = 1000
nrc = 100; %number of cells
Mcr = 4; % critical value of M
Column = randi([1,10],1); % creates a random number between 1 & 10
Row = randi([1,10],1); % creates a random number between 1 & 10
M = randi([0,4],10,10); % random 10 x 10 matrix with values between 0 and 4
while n < nrsteps
for i = 1:size(1) %scans row 1 through 10
for j = 1:size(2) %scans column 1 through 10
if M(i,j) == 4
M(i,j) = 0;
M(i+1,j) = M(i+1,j) + 1;
M(i-1,j) = M(i-1,j) + 1;
M(i,j+1) = M(i,j+1) + 1;
M(i,j-1) = M(i,j-1) + 1;
% if statement to relax sites at critical value and disribute
% grains to adjacient sites
else M(Row, Column) = M(Row, Column) + 1; %%adds 1 grain to a random site
n = n + 1; %adds 1 to step counter
end
imagesc(M)
end
end
end
Answers (1)
Geoff Hayes
on 25 Jan 2016
Edited: Geoff Hayes
on 25 Jan 2016
Steven - while I'm sure this problem was solved a year or more ago, the following error is generated when I run the above code
Attempted to access M(0,2); index must be a positive integer or logical.
That is because we are trying to access a cell that is not within the "gaming" area (MATLAB uses one-based indexing for accessing arrays) when trying to distribute the grain of sand to a neighbouring cell. So you need to guard against this which will also help in identifying whether the grain has fallen over the edge. Before we do that, take a closer look at the following
size = [10 10]; %size is a 10x10 matrix
Be careful when naming variables. In this case, size is also the name of a built-in MATLAB function and so it is best to avoid these types of conflicts which will lead to errors. Instead, rename the variable to something like gameBoardDim for the dimension of the gaming area/board. Also, MATLAB uses i and j to represent the imaginary number so alternatives to these should be used when naming the indexing variables.
Now consider the following
for r = 1: gameBoardDim(1) %scans row 1 through 10
for c = 1: gameBoardDim(2) %scans column 1 through 10
if M(r,c) == 4
M(r,c) = 0;
Note how we iterate over each cell on the board and check to see if its count is exactly four. We then set the count to zero and the subsequent code adds one to each of its neighbours. The problem with this approach is that if a neighbouring cell already has a count of four, then when we add one to it, its count will now be five and so will never be captured in the above condition. Also, as we distribute the grains to neighbouring cells and continue to iterate over the board, we may only capture half of those cells that have had their count incremented to four which will lead to the board becoming unbalanced. What we need to consider instead are all of those cells at the current iteration (generation) whose count is at least four. Then, for all of these cells, we distribute the grains to its neighbours. If the count for any of these neighbours is now four, then on the subsequent generation we will distribute their grains to their neighbours. Something like
close all;
n = 0; %step counter
nrsteps = 1000000000; %num. of steps = 1000
M = randi([0,4],10,10); % random 10 x 10 matrix with values between 0 and 4
M = zeros(51,51);
M(26,26) = 2800;
numLostGrains = 0;
while n < nrsteps
[rIdcs,cIdcs] = find(M>=4);
if ~isempty(rIdcs)
for k=1:length(rIdcs)
r = rIdcs(k);
c = cIdcs(k);
M(r,c) = M(r,c) - 4;
[M,numLostGrains] = addGrainToCell(M,r+1,c,numLostGrains);
[M,numLostGrains] = addGrainToCell(M,r-1,c,numLostGrains);
[M,numLostGrains] = addGrainToCell(M,r,c+1,numLostGrains);
[M,numLostGrains] = addGrainToCell(M,r,c-1,numLostGrains);
n = n + 1; %adds 1 to step counter
end
else
break;
end
imagesc(M);
pause(0.01);
end
function [M,numLostGrains] = addGrainToCell(M,r,c,numLostGrains)
if r < 1 || r > size(M,1) || c < 1 || c > size(M,2)
numLostGrains = numLostGrains + 1;
else
M(r,c) = M(r,c) + 1;
end
The above scenario is a little more extreme in that 2800 grains of sand are poured on to the centre cell and so may take upwards of 20 seconds or so before you start to notice something being drawn within the figure.
Categories
Find more on Programming in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!