function cells1 = separaterule(cells1,x,y) global gd %global gu global P global J global N global emptynum global waternum global oilnum global solutenum blocked = 5; xlen = length(x); ylen = length(y); totnums = xlen .* ylen; randord = randperm(totnums); %x = x(randperm(length(x))); %y = y(randperm(length(y))); for k = randord yj = rem(k,xlen)+1; xi = floor((k-1)./xlen)+1; i = x(xi); j = y(yj); %for i = x % for j = y rannum = rand; prob = zeros(7,1); evnts = []; thiscell = cells1(i,j); if i == 1 cellabove = blocked; cell2above = emptynum; elseif i == 2 cellabove = cells1(i-1,j); cell2above = emptynum; else cellabove = cells1(i-1,j); cell2above = cells1(i-2,j); end if j == ylen cellright = blocked; cell2right = emptynum; elseif j == ylen - 1 cellright = cells1(i,j+1); cell2right = emptynum; else cellright = cells1(i,j+1); cell2right = cells1(i,j+2); end if i == xlen cellbelow = blocked; cell2below = emptynum; elseif i == xlen - 1 cellbelow = cells1(i+1,j); cell2below = emptynum; else cellbelow = cells1(i+1,j); cell2below = cells1(i+2,j); end if j == 1 cellleft = blocked; cell2left = emptynum; elseif j == 2 cellleft = cells1(i,j-1); cell2left = emptynum; else cellleft = cells1(i,j-1); cell2left = cells1(i,j-2); end if thiscell ~= emptynum %calc prob of moving up if cellabove ~= blocked if cellabove ~= emptynum % calc prob of switch with cell above if cellabove ~= thiscell prob(5) = gd(thiscell,cellabove); end else if cellbelow ~= emptynum & cellbelow ~= blocked prob(1) = prob(1) + P(thiscell,cellbelow); end if cell2above ~= emptynum prob(1) = prob(1) + J(thiscell,cell2above); end end end if cellright == emptynum if cellleft ~= emptynum & cellleft ~= blocked prob(2) = prob(2) + P(thiscell,cellleft); end if cell2right ~= emptynum prob(2) = prob(2) + J(thiscell,cell2right); end end %calc prob of moving down if cellbelow ~= blocked if cellbelow ~= emptynum % calc prob of switch with cell below if cellbelow ~= thiscell prob(6) = gd(cellbelow,thiscell); end else if cellabove ~= emptynum & cellabove ~= blocked prob(3) = prob(3) + P(thiscell,cellabove); end if cell2below ~= emptynum prob(3) = prob(3) + J(thiscell,cell2below); end end end if cellleft == emptynum if cellright ~= emptynum & cellright ~= blocked prob(4) = prob(4) + P(thiscell,cellright); end if cell2left ~= emptynum prob(4) = prob(4) + J(thiscell,cell2left); end end cumprob = cumsum(prob); if cumprob(end) ~= 0 cumprob = cumprob .* (1-N(thiscell)) ./ cumprob(end); evnts = find(rannum < cumprob); if ~isempty(evnts) execevnt = evnts(1); else execevnt = 7; end switch execevnt case 1 cells1(i,j) = 4; cells1(i-1,j) = thiscell; case 2 cells1(i,j) = 4; cells1(i,j+1) = thiscell; case 3 cells1(i,j) = 4; cells1(i+1,j) = thiscell; case 4 cells1(i,j) = 4; cells1(i,j-1) = thiscell; case 5 cells1(i,j) = cellabove; cells1(i-1,j) = thiscell; case 6 cells1(i,j) = cellbelow; cells1(i+1,j) = thiscell; case 7 end end end % end %end end