
Percolation Simulation

This is a simulation of aspects of mathematical percolation theory.

For other percolation simulations, see Category:Percolation Simulations, or:
1D finite grid simulation
Mean run density 
2D finite grid simulations
Site percolation | Bond percolation | Mean cluster density

Given an 



M
×
N


{\displaystyle M\times N}

 rectangular array of cells numbered 




c
e
l
l

[
0..
M
−
1
,
0..
N
−
1
]


{\displaystyle \mathrm {cell} [0..M-1,0..N-1]}

assume 



M


{\displaystyle M}

 is horizontal and 



N


{\displaystyle N}

 is downwards.

Assume that the probability of any cell being filled is a constant 



p


{\displaystyle p}

 where

 



0.0
≤
p
≤
1.0


{\displaystyle 0.0\leq p\leq 1.0}


The task

Simulate creating the array of cells with probability 



p


{\displaystyle p}

 and then
testing if there is a route through adjacent filled cells from any on row 



0


{\displaystyle 0}

 to any on row 



N


{\displaystyle N}

, i.e. testing for site percolation.

Given 



p


{\displaystyle p}

 repeat the percolation 



t


{\displaystyle t}

 times to estimate the proportion of times that the fluid can percolate to the bottom for any given 



p


{\displaystyle p}

.

Show how the probability of percolating through the random grid changes with 



p


{\displaystyle p}

 going from 



0.0


{\displaystyle 0.0}

 to 



1.0


{\displaystyle 1.0}

 in 



0.1


{\displaystyle 0.1}

 increments and with the number of repetitions to estimate the fraction at any given 



p


{\displaystyle p}

 as 



t
>=
100


{\displaystyle t>=100}

.

Use an 



M
=
15
,
N
=
15


{\displaystyle M=15,N=15}

 grid of cells for all cases.

Optionally depict a percolation through a cell grid graphically.

Show all output on this page.

with javascript_semantics
string grid
integer m, n, last, lastrow
 
enum SOLID = '#', EMPTY=' ', WET = 'v'
 
procedure make_grid(integer x, y, atom p)
    m = x
    n = y
    grid = repeat('\n',x*(y+1)+1)
    last = length(grid)
    lastrow = last-n
    for i=0 to x-1 do
        for j=1 to y do
            grid[1+i*(y+1)+j] = iff(rnd()<p?EMPTY:SOLID)
        end for
    end for
end procedure
 
function ff(integer i) -- flood_fill
    if i<=0 or i>=last or grid[i]!=EMPTY then return 0 end if
    grid[i] = WET
    return i>=lastrow or ff(i+m+1) or ff(i+1) or ff(i-1) or ff(i-m-1)
end function
 
function percolate()
    for i=2 to m+1 do
        if ff(i) then return true end if
    end for
    return false
end function
 
procedure main()
    make_grid(15,15,0.55)
    {} = percolate()
    printf(1,"%dx%d grid:%s",{15,15,grid})
    puts(1,"\nrunning 10,000 tests for each case:\n")
    for ip=0 to 10 do
        atom p = ip/10
        integer count = 0
        for i=1 to 10000 do
            make_grid(15, 15, p)
            count += percolate()
        end for
        printf(1,"p=%.1f:  %6.4f\n", {p, count/10000})
    end for
end procedure
main()
