Percolation/Bond percolation: Difference between revisions
Content added Content deleted
(Rewritten the D entry on the base of the C entry) |
(Removed unused variable mn from C entry.) |
||
Line 36: | Line 36: | ||
c_t *cells, *start, *end; |
c_t *cells, *start, *end; |
||
int m, n |
int m, n; |
||
void make_grid(double p, int x, int y) |
void make_grid(double p, int x, int y) |
||
{ |
{ |
||
int i, j, thresh = RAND_MAX * p; |
|||
m = x, n = y; |
|||
// Allocate two addition rows to avoid checking bounds. |
|||
// Bottom row is also required by drippage |
|||
start = realloc(start, m * (n + 2) * sizeof(c_t)); |
|||
cells = start + m; |
|||
for (i = 0; i < m; i++) |
|||
start[i] = BWALL | RWALL; |
|||
for (i = 0, end = cells; i < y; i++) { |
|||
for (j = x; --j; ) |
|||
*end++ = (rand() < thresh ? BWALL : 0) |
|||
|(rand() < thresh ? RWALL : 0); |
|||
*end++ = RWALL | (rand() < thresh ? BWALL: 0); |
|||
} |
|||
} |
|||
memset(end, 0, sizeof(c_t) * m); |
|||
} |
} |
||
void show_grid(void) |
void show_grid(void) |
||
{ |
{ |
||
int i, j; |
|||
for (j = 0; j < m; j++) printf("+--"); |
|||
puts("+"); |
|||
for (i = 0; i <= n; i++) { |
|||
putchar(i == n ? ' ' : '|'); |
|||
for (j = 0; j < m; j++) { |
|||
printf((cells[i*m + j] & FILL) ? "[]" : " "); |
|||
putchar((cells[i*m + j] & RWALL) ? '|' : ' '); |
|||
} |
|||
} |
|||
putchar('\n'); |
|||
if (i == n) return; |
|||
for (j = 0; j < m; j++) |
|||
printf((cells[i*m + j] & BWALL) ? "+--" : "+ "); |
|||
puts("+"); |
|||
} |
|||
} |
|||
} |
} |
||
int fill(c_t *p) |
int fill(c_t *p) |
||
{ |
{ |
||
if ((*p & FILL)) return 0; |
|||
*p |= FILL; |
|||
if (p >= end) return 1; // success: reached bottom row |
|||
return ( !(p[ 0] & BWALL) && fill(p + m) ) || |
|||
( !(p[ 0] & RWALL) && fill(p + 1) ) || |
|||
( !(p[-1] & RWALL) && fill(p - 1) ) || |
|||
( !(p[-m] & BWALL) && fill(p - m) ); |
|||
} |
} |
||
int percolate(void) |
int percolate(void) |
||
{ |
{ |
||
int i; |
|||
for (i = 0; i < m && !fill(cells + i); i++); |
|||
return i < m; |
|||
} |
} |
||
int main(void) |
int main(void) |
||
{ |
{ |
||
make_grid(.5, 10, 10); |
|||
percolate(); |
|||
show_grid(); |
|||
int cnt, i, p; |
|||
puts("\nrunning 10x10 grids 10000 times for each p:"); |
|||
for (p = 1; p < 10; p++) { |
|||
for (cnt = i = 0; i < 10000; i++) { |
|||
make_grid(p / 10., 10, 10); |
|||
cnt += percolate(); |
|||
//show_grid(); // don't |
|||
} |
|||
} |
|||
printf("p = %3g: %.4f\n", p / 10., (double)cnt / i); |
|||
} |
|||
} |
|||
free(start); |
|||
return 0; |
|||
}</lang> |
}</lang> |
||
{{out}} |
{{out}} |
||
Line 160: | Line 160: | ||
p = 0.9: 0.0000 |
p = 0.9: 0.0000 |
||
</pre> |
</pre> |
||
=={{header|D}}== |
=={{header|D}}== |
||
{{trans|C}} |
{{trans|C}} |