Percolation/Bond percolation: Difference between revisions

Content added Content deleted
(→‎{{header|Tcl}}: Added zkl)
(→‎{{header|zkl}}: brain dead programmer)
Line 990: Line 990:
{{trans|C}}
{{trans|C}}
<lang zkl>// cell states
<lang zkl>// cell states
const FILL =1;
const FILLED=1; // and odd
const RWALL=2; // right wall
const RWALL =2; // right wall
const BWALL=4; // bottom wall
const BWALL =4; // bottom wall
fcn P(p,wall){ (0.0).random(1)<p and wall }
fcn P(p,wall){ (0.0).random(1)<p and wall or 0 }


fcn makeGrid(m,n,p){
fcn makeGrid(m,n,p){
Line 999: Line 999:
// Bottom row is also required by drippage
// Bottom row is also required by drippage
grid:=Data(m*(n+2));
grid:=Data(m*(n+2));
do(m){ grid.write(BWALL + RWALL); }
do(m){ grid.write(BWALL + RWALL); } // grid is topped with walls
do(n){
do(n){
do(m-1){ grid.write( P(p,BWALL) + P(p,RWALL) ) }
do(m-1){ grid.write( P(p,BWALL) + P(p,RWALL) ) }
grid.write(RWALL + P(p,BWALL));
grid.write(RWALL + P(p,BWALL)); // right border is all right wall, as is left border
}
}
do(m){ grid.write(0); }
do(m){ grid.write(0); } // for drips off the bottom of grid
grid
grid
}
}
fcn show(grid,m,n){ n+=1;
fcn show(grid,m,n){ n+=1;
println("+--"*m,"+");
println("+--"*m,"+");
foreach i in ([1..n]){
foreach i in ([1..n]){ y:=i*m;
print(i==n and " " or "|");
print(i==n and " " or "|"); // bottom row is special, otherwise always have left wall
foreach j in (m){ c:=grid[i*m + j];
foreach j in (m){ c:=grid[y + j];
print(c.bitAnd(FILL) and "[]" or " ", c.bitAnd(RWALL) and "|" or " ");
print(c.bitAnd(FILLED) and "**" or " ", c.bitAnd(RWALL)and"|"or" ");
}
}
println();
println();
if(i==n) return();
if(i==n) return(); // nothing under the bottom row


foreach j in (m){ print((grid[i*m + j].bitAnd(BWALL)) and "+--" or "+ "); }
foreach j in (m){ print((grid[y + j].bitAnd(BWALL)) and "+--" or "+ "); }
println("+");
println("+");
}
}
}
}
fcn fill(grid,x,m){
fcn fill(grid,x,m){
if(grid[x].bitAnd(FILL)) return(False);
if(grid[x].isOdd) return(False); // aka .bitAnd(FILLED) aka already been here
grid[x]=grid[x].bitOr(FILL);
grid[x]+=FILLED;
if(x+m>=grid.len()) return(True); // success: reached bottom row
if(x+m>=grid.len()) return(True); // success: reached bottom row
return((not grid[x] .bitAnd(BWALL) and fill(grid,x + m,m) ) +
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) ) +
( 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) ) +
( 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) ));
( not grid[x - m].bitAnd(BWALL) and fill(grid,x - m,m) )); // up

}
}
fcn percolate(grid,m){
fcn percolate(grid,m){
x:=m; i:=0; while(i<m and not fill(grid,x,m)){ x+=1; i+=1; }
i:=0; while(i<m and not fill(grid,i+m,m)){ i+=1; } // pour juice on top row
return(i<m); // percolated through the grid?
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);
println("Did liquid percolate: ",percolate(grid,10)); show(grid,10,10);


Line 1,049: Line 1,047:
Did liquid percolate: True
Did liquid percolate: True
+--+--+--+--+--+--+--+--+--+--+
+--+--+--+--+--+--+--+--+--+--+
|[] [] [] [] [] [] []|[] []|[]|
|** ** | | |
+--+ + + + + +--+ +--+ +
+--+ +--+--+ + + + + + +
|[] |[] []|[]|[] [] |[]|
| **| | | | |
+ + +--+ +--+--+ + +--+ +
+ + + +--+ +--+--+ +--+--+
|[] |[] [] [] [] []|
| ** ** | |
+ +--+--+ +--+ +--+ + +--+
+--+--+ + + + +--+ + +--+
|[] | |[] [] [] [] []|
| |**| | | |
+ +--+--+ +--+ + +--+--+--+
+ + + + +--+ + +--+ + +
|[] [] [] [] [] [] []|
| ** **| |** **| | |
+ + + + +--+ + +--+ +--+
+ +--+--+ +--+ + +--+ + +
| | [] [] [] [] [] [] []|
| | |**| |**|** ** |
+--+--+ + + +--+ + + + +
+ + + + +--+ +--+ + + +
|[] | [] []|[] []|
| | |** ** ** **|** |
+ + + +--+--+ + +--+ + +
+--+--+--+--+ +--+--+ +--+--+
| | |[] [] [] []|
| | |** **| ** | |
+--+--+ + + + +--+ + + +
+ + +--+ + + + + +--+--+
| |[] [] []|[]|
| |** **| |**| | |
+ +--+ +--+--+--+ + +--+ +
+ +--+ +--+--+--+--+ + + +
| [] [] |[] [] []|[]|
| | | **| |
+ + + + + +--+--+ +--+ +
+ + + + + + +--+ + + +
[] []
**
Running 10,000 tests for each case:
Running 10,000 tests for each case:
p=0.0: 1.0000
p=0.0: 1.0000
p=0.1: 1.0000
p=0.1: 1.0000
p=0.2: 0.9733
p=0.2: 1.0000
p=0.3: 0.6934
p=0.3: 0.9978
p=0.4: 0.2035
p=0.4: 0.9163
p=0.5: 0.0192
p=0.5: 0.5017
p=0.6: 0.0010
p=0.6: 0.0890
p=0.7: 0.0000
p=0.7: 0.0033
p=0.8: 0.0000
p=0.8: 0.0000
p=0.9: 0.0000
p=0.9: 0.0000