How to resolve the algorithm Percolation/Site percolation step by step in the zkl programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Percolation/Site percolation step by step in the zkl programming language

Table of Contents

Problem Statement

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 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.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Percolation/Site percolation step by step in the zkl programming language

Source code in the zkl programming language

fcn makeGrid(m,n,p){
   grid:=Data((m+1)*(n+1));  // first row and right edges are buffers
   grid.write(" "*m); grid.write("\r");
   do(n){
      do(m){ grid.write(((0.0).random(1)
      grid.write("\n");
   }
   grid
}
fcn ff(grid,x,m){ // walk across row looking for a porous cell
   if(grid[x]!=43) return(0); // '+' == 43 ASCII == porous
   grid[x]="#";
   return(x+m>=grid.len() or 
	  ff(grid,x+m,m) or ff(grid,x+1,m) or ff(grid,x-1,m) or ff(grid,x-m,m));
}
fcn percolate(grid,m){
   x:=m+1; i:=0; while(i
   return(i
}
 
grid:=makeGrid(15,15,0.60);
println("Did liquid percolate: ",percolate(grid,15));
println("15x15 grid:\n",grid.text);

println("Running 10,000 tests for each case:");
foreach p in ([0.0 .. 1.0, 0.1]){
   cnt:=0.0; do(10000){ cnt+=percolate(makeGrid(15,15,p),15); }
   "p=%.1f:  %.4f".fmt(p, cnt/10000).println();
}

  

You may also check:How to resolve the algorithm Function definition step by step in the Axe programming language
You may also check:How to resolve the algorithm Magic constant step by step in the Julia programming language
You may also check:How to resolve the algorithm Mutual recursion step by step in the Prolog programming language
You may also check:How to resolve the algorithm String concatenation step by step in the Elena programming language
You may also check:How to resolve the algorithm Maze solving step by step in the Racket programming language