Percolation/Bond percolation: Difference between revisions

→‎{{header|zkl}}: brain dead programmer
(→‎{{header|Tcl}}: Added zkl)
(→‎{{header|zkl}}: brain dead programmer)
Line 990:
{{trans|C}}
<lang zkl>// cell states
const FILL FILLED=1; // and odd
const RWALL =2; // right wall
const BWALL =4; // bottom wall
fcn P(p,wall){ (0.0).random(1)<p and wall or 0 }
 
fcn makeGrid(m,n,p){
Line 999:
// Bottom row is also required by drippage
grid:=Data(m*(n+2));
do(m){ grid.write(BWALL + RWALL); } // grid is topped with walls
do(n){
do(m-1){ grid.write( P(p,BWALL) + P(p,RWALL) ) }
grid.write(RWALL + P(p,BWALL)); // right border is all right wall, as is left border
}
do(m){ grid.write(0); } // for drips off the bottom of grid
grid
}
fcn show(grid,m,n){ n+=1;
println("+--"*m,"+");
foreach i in ([1..n]){ y:=i*m;
print(i==n and " " or "|"); // bottom row is special, otherwise always have left wall
foreach j in (m){ c:=grid[i*my + j];
print(c.bitAnd(FILLFILLED) and "[]**" or " ", c.bitAnd(RWALL) and "|" or " ");
}
println();
if(i==n) return(); // nothing under the bottom row
 
foreach j in (m){ print((grid[i*my + j].bitAnd(BWALL)) and "+--" or "+ "); }
println("+");
}
}
fcn fill(grid,x,m){
if(grid[x].bitAnd(FILL)isOdd) return(False); // aka .bitAnd(FILLED) aka already been here
grid[x]+=grid[x].bitOr(FILL)FILLED;
if(x+m>=grid.len()) return(True); // success: reached bottom row
return(( not grid[x] .bitAnd(BWALL) and fill(grid,x + m,m) ) +or // down
( not grid[x] .bitAnd(RWALL) and fill(grid,x + 1,m) ) +or // right
( not grid[x - 1].bitAnd(RWALL) and fill(grid,x - 1,m) ) +or // left
( not grid[x - m].bitAnd(BWALL) and fill(grid,x - m,m) )); // up
 
}
fcn percolate(grid,m){
x:=m; i:=0; while(i<m and not fill(grid,xi+m,m)){ x+=1; i+=1; } // pour juice on top row
return(i<m); // percolated through the grid?
}</lang>
}
<lang zkl>grid:=makeGrid(10,10,0.40);
grid:=makeGrid(10,10,0.40);
println("Did liquid percolate: ",percolate(grid,10)); show(grid,10,10);
 
Line 1,049 ⟶ 1,047:
Did liquid percolate: True
+--+--+--+--+--+--+--+--+--+--+
|[]** ** [] [] [] [] [] []|[] [] |[] |
+--+ +--+--+ + + + +--+ +--+ +
|[] **| |[] [] |[]|[] [] |[] |
+ + + +--+ +--+--+ + +--+ --+
|[] ** ** | |[] [] [] [] []|
+ +--+--+ +-- + + +--+ + +--+
|[] |**| | |[] [] [] [] [] |
+ +-- +-- + +--+ + +--+-- +-- +
|[] ** []**| |** **| [] [] [] [] []| |
+ + --+ --+ +--+ + +--+ +-- +
| | |**| [] []|**|** ** [] [] [] [] []|
+--+-- + + + +--+ +--+ + + +
|[] | |** ** ** **|** [] []|[] []|
+ --+ + --+--+--+ + --+--+ + --+--+
| | |[] |** **| ** []| [] []|
+--+--+ + +--+ + +--+ + + +--+--+
| |** **| |**| |[] [] []|[]|
+ +--+ +--+--+--+--+ + +--+ +
| [] [] |[] [] []|[] **| |
+ + + + + +-- +--+ +-- + +
[]** []
Running 10,000 tests for each case:
p=0.0: 1.0000
p=0.1: 1.0000
p=0.2: 01.97330000
p=0.3: 0.69349978
p=0.4: 0.20359163
p=0.5: 0.01925017
p=0.6: 0.00100890
p=0.7: 0.00000033
p=0.8: 0.0000
p=0.9: 0.0000
Anonymous user