#include #include #include #include /* The cells are stored in a 1-dimensional array of length n*n. The square geometry has to be calculated on the fly: x = i%n y = i/n where i in [0..n*n[. There are also macros to find the index of any direct neighbour of a cell with index i. These macros incorporate periodic boundary conditions on all sides. (The macros take i in [0..n*n[ and n as arguments.) */ /* ------------------------------------------------------------ */ #define up( x, n ) ( ( ((x)/(n)+1 )%(n) )*(n) + (x)%(n) ) #define dn( x, n ) ( ( ((x)/(n)-1+n)%(n) )*(n) + (x)%(n) ) #define lf( x, n ) ( ( (x)/(n) )*(n) + ( (x)-1+(n) )%(n) ) #define rt( x, n ) ( ( (x)/(n) )*(n) + ( (x)+1 )%(n) ) /* ------------------------------------------------------------ */ /* Wrapper function - supply better random number generator if desired */ long rnd( long i ) { return rand()%i; } /* Distributes m chips on a square of dimension (n x n) and returns the allocated matrix as pointer to long. */ long * init( long m, long n ) { long *data = (long *)malloc( n*n*sizeof( long ) ); long i; long ctr = 0; if( m > n*n ) { fprintf( stderr, "Too many chips (%ld) for field (%ld)\n", m, n*n ); exit( EXIT_FAILURE ); } if( !data ) { fprintf( stderr, "malloc( %ld ) failed\n", n*n ); exit( EXIT_FAILURE ); } for( i=0; i \n" ); exit( EXIT_SUCCESS ); } /* ------------------------------------------------------------ */ /* Helper function - used to find the correlation lenght using Newton */ double eqn( double xi, long n, double *c ) { long i; double z, res = 0.0; for( i=0; i 1.0e-4 && itr++ < 15 ) { de = ( eqn( x+1.0e-6, n/2, dc ) - err )/1.0e-6; x -= err/de; err = eqn( x, n/2, dc ); } /* Finally: output */ printf( "# corr t: %ld xi: %f\n", t, x ); for( i=0; i