Anonymous user
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
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[
print(c.bitAnd(
}
println();
if(i==n) return(); // nothing under the bottom row
foreach j in (m){ print((grid[
println("+");
}
}
fcn fill(grid,x,m){
if(grid[x].
grid[x]+=
if(x+m>=grid.len()) return(True); // success: reached bottom row
return(( not grid[x] .bitAnd(BWALL) and fill(grid,x + m,m) )
( not grid[x] .bitAnd(RWALL) and fill(grid,x + 1,m) )
( not grid[x - 1].bitAnd(RWALL) and fill(grid,x - 1,m) )
( not grid[x - m].bitAnd(BWALL) and fill(grid,x - m,m) )); // up
}
fcn percolate(grid,m){
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:
p=0.3: 0.
p=0.4: 0.
p=0.5: 0.
p=0.6: 0.
p=0.7: 0.
p=0.8: 0.0000
p=0.9: 0.0000
|