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, mn;
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;
int i, j, thresh = RAND_MAX * p;
m = x, n = y, mn = m * n;
m = x, n = y;


// Allocate two addition rows to avoid checking bounds.
// Allocate two addition rows to avoid checking bounds.
// Bottom row is also required by drippage
// Bottom row is also required by drippage
start = realloc(start, m * (n + 2) * sizeof(c_t));
start = realloc(start, m * (n + 2) * sizeof(c_t));
cells = start + m;
cells = start + m;


for (i = 0; i < m; i++)
for (i = 0; i < m; i++)
start[i] = BWALL | RWALL;
start[i] = BWALL | RWALL;


for (i = 0, end = cells; i < y; i++) {
for (i = 0, end = cells; i < y; i++) {
for (j = x; --j; )
for (j = x; --j; )
*end++ = (rand() < thresh ? BWALL : 0)
*end++ = (rand() < thresh ? BWALL : 0)
|(rand() < thresh ? RWALL : 0);
|(rand() < thresh ? RWALL : 0);
*end++ = RWALL | (rand() < thresh ? BWALL: 0);
*end++ = RWALL | (rand() < thresh ? BWALL: 0);
}
}
memset(end, 0, sizeof(c_t) * m);
memset(end, 0, sizeof(c_t) * m);
}
}


void show_grid(void)
void show_grid(void)
{
{
int i, j;
int i, j;


for (j = 0; j < m; j++) printf("+--");
for (j = 0; j < m; j++) printf("+--");
puts("+");
puts("+");


for (i = 0; i <= n; i++) {
for (i = 0; i <= n; i++) {
putchar(i == n ? ' ' : '|');
putchar(i == n ? ' ' : '|');
for (j = 0; j < m; j++) {
for (j = 0; j < m; j++) {
printf((cells[i*m + j] & FILL) ? "[]" : " ");
printf((cells[i*m + j] & FILL) ? "[]" : " ");
putchar((cells[i*m + j] & RWALL) ? '|' : ' ');
putchar((cells[i*m + j] & RWALL) ? '|' : ' ');
}
}
putchar('\n');
putchar('\n');


if (i == n) return;
if (i == n) return;


for (j = 0; j < m; j++)
for (j = 0; j < m; j++)
printf((cells[i*m + j] & BWALL) ? "+--" : "+ ");
printf((cells[i*m + j] & BWALL) ? "+--" : "+ ");
puts("+");
puts("+");
}
}
}
}


int fill(c_t *p)
int fill(c_t *p)
{
{
if ((*p & FILL)) return 0;
if ((*p & FILL)) return 0;
*p |= FILL;
*p |= FILL;
if (p >= end) return 1; // success: reached bottom row
if (p >= end) return 1; // success: reached bottom row


return ( !(p[ 0] & BWALL) && fill(p + m) ) ||
return ( !(p[ 0] & BWALL) && fill(p + m) ) ||
( !(p[ 0] & RWALL) && fill(p + 1) ) ||
( !(p[ 0] & RWALL) && fill(p + 1) ) ||
( !(p[-1] & RWALL) && fill(p - 1) ) ||
( !(p[-1] & RWALL) && fill(p - 1) ) ||
( !(p[-m] & BWALL) && fill(p - m) );
( !(p[-m] & BWALL) && fill(p - m) );
}
}


int percolate(void)
int percolate(void)
{
{
int i;
int i;
for (i = 0; i < m && !fill(cells + i); i++);
for (i = 0; i < m && !fill(cells + i); i++);


return i < m;
return i < m;
}
}


int main(void)
int main(void)
{
{
make_grid(.5, 10, 10);
make_grid(.5, 10, 10);
percolate();
percolate();
show_grid();
show_grid();


int cnt, i, p;
int cnt, i, p;


puts("\nrunning 10x10 grids 10000 times for each p:");
puts("\nrunning 10x10 grids 10000 times for each p:");
for (p = 1; p < 10; p++) {
for (p = 1; p < 10; p++) {
for (cnt = i = 0; i < 10000; i++) {
for (cnt = i = 0; i < 10000; i++) {
make_grid(p / 10., 10, 10);
make_grid(p / 10., 10, 10);
cnt += percolate();
cnt += percolate();
//show_grid(); // don't
//show_grid(); // don't
}
}
printf("p = %3g: %.4f\n", p / 10., (double)cnt / i);
printf("p = %3g: %.4f\n", p / 10., (double)cnt / i);
}
}


free(start);
free(start);
return 0;
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}}