Abelian sandpile model: Difference between revisions

Line 444:
[http://alahonua.com/temp/Abel_Z_color_100000.png Link to PNG output file for N=100000 ie. AbelSand.move(100000)] <br />
[http://alahonua.com/temp/Abel_Z_color_1000000.png Link to PNG output file (run time >90 min) for N=1000000 (move(1000000))]
 
=={{header|Phix}}==
{{libheader|pGUI}}
Generates moving images similar to the julia output.
The distributed version also has variable speed, additional display modes, and a random dropping toggle.
<lang Phix>-- demo\rosetta\Abelian_sandpile_model.exw
include pGUI.e
 
Ihandle dlg, canvas
cdCanvas cddbuffer
 
sequence board = {{0,0,0},
{0,0,0},
{0,0,0}}
 
procedure drop(integer y, x)
sequence moves = {}
while true do
board[y,x] += 1
if board[y,x]>=4 then
board[y,x] -= 4
moves &= {{y,x-1},{y,x+1},{y-1,x},{y+1,x}}
end if
-- extend board if rqd (maintain a border of zeroes)
if x=1 then -- extend left
for i=1 to length(board) do
board[i] = prepend(board[i],0)
end for
for i=1 to length(moves) do
moves[i][2] += 1
end for
elsif x=length(board[1]) then -- extend right
for i=1 to length(board) do
board[i] = append(board[i],0)
end for
end if
-- (copy the all-0 lines from the other end...)
if y=1 then -- extend up
board = prepend(board,board[$])
for i=1 to length(moves) do
moves[i][1] += 1
end for
elsif y=length(board) then -- extend down
board = append(board,board[1])
end if
if length(moves)=0 then exit end if
{y,x} = moves[$]
moves = moves[1..$-1]
end while
IupUpdate(canvas)
end procedure
 
function timer_cb(Ihandle /*ih*/)
integer y = floor(length(board)/2)+1,
x = floor(length(board[1])/2)+1
drop(y,x)
return IUP_DEFAULT
end function
 
function redraw_cb(Ihandle ih, integer /*posx*/, integer /*posy*/)
IupGLMakeCurrent(ih)
cdCanvasActivate(cddbuffer)
cdCanvasClear(cddbuffer)
for y=1 to length(board) do
for x=1 to length(board[1]) do
integer c = board[y][x]
if c!=0 then
integer colour = {CD_VIOLET,CD_RED,CD_BLUE}[c]
cdCanvasPixel(cddbuffer, x, y, colour)
end if
end for
end for
cdCanvasFlush(cddbuffer)
return IUP_DEFAULT
end function
 
function map_cb(Ihandle ih)
IupGLMakeCurrent(ih)
atom res = IupGetDouble(NULL, "SCREENDPI")/25.4
cddbuffer = cdCreateCanvas(CD_GL, "300x100 %g", {res})
cdCanvasSetBackground(cddbuffer, CD_PARCHMENT)
return IUP_DEFAULT
end function
 
procedure main()
IupOpen()
canvas = IupGLCanvas("RASTERSIZE=300x100")
IupSetCallbacks({canvas}, {"ACTION", Icallback("redraw_cb"),
"MAP_CB", Icallback("map_cb")})
dlg = IupDialog(canvas,"TITLE=\"Abelian sandpile model\"")
IupCloseOnEscape(dlg)
IupShow(dlg)
Ihandle timer = IupTimer(Icallback("timer_cb"), 10)
IupMainLoop()
IupClose()
end procedure
main()</lang>
 
=={{header|Rust}}==
7,820

edits