its very long!
Code:
/**
** gcc 1007189_prog2.c -lm
********************************************/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define EXPECTED_ARGS 3
/*****************************************
** stop function calculates whether
** particle should aggregate
** i.e. is any of its neighbours
** contain a particle
**
** returns 0 to continue, 1 to stop
**
** starts by assuming 0 (continue)
** then checks through all circumstances
** when particle is on the boundary and
** checks whether any of the neighbours
** contain a particle.
**
** If xpos or ypos are outside the lattice
** for any reason it will continue
**
** NOTE REQUIRES dim > 0 !!
******************************************/
int stop(int dim, int xpos, int ypos, int sites[dim+1][dim+1])
{
int stop = 0;
if(xpos == 0 && ypos == 0)
{
if(sites[0][1]!=0 || sites[1][0]!=0)
{
stop = 1;
}
}
if(xpos == 0 && ypos == dim)
{
if(sites[0][dim-1]!=0 || sites[1][dim]!=0)
{
stop = 1;
}
}
if(xpos == dim-1 && ypos == 0)
{
if(sites[dim-1][0]!=0 || sites [dim][1]!=0)
{
stop =1;
}
}
if(xpos == dim && ypos == dim)
{
if(sites[dim-1][dim]!=0 || sites [dim][dim-1]!=0)
{
stop =1;
}
}
if(xpos == 0 && ypos > 0 && ypos < dim)
{
if(sites[1][ypos]!=0 || sites[0][ypos+1]!=0 || sites[0][ypos-1]!=0)
{
stop=1;
}
}
if(ypos == 0 && xpos > 0 && xpos < dim)
{
if(sites[xpos][1]!=0 || sites[xpos+1][0]!=0 || sites[xpos-1][0]!=0)
{
stop=1;
}
}
if(xpos == dim && ypos > 0 && ypos < dim)
{
if(sites[dim-1][ypos]!=0 || sites [dim][ypos+1]!=0 || sites[dim][ypos-1]!=0)
{
stop=1;
}
}
if(ypos == dim && xpos > 0 && xpos < dim)
{
if(sites[xpos][dim-1]!=0 || sites [xpos+1][dim]!=0 || sites[xpos-1][dim]!=0)
{
stop=1;
}
}
if(xpos>0 && xpos<dim && ypos>0 && ypos<dim)
{
if(sites[xpos][ypos-1]!=0 || sites[xpos][ypos+1]!=0 || sites[xpos-1][ypos]!=0 || sites[xpos+1][ypos]!=0)
{
stop=1;
}
}
return stop;
}
int main(int argc, char* argv[])
{
int dim, valid_input, particles;
int i=0, xpos, ypos, rnd;
int xsum, ysum, dim_count;
int j, k;
double xCoM, yCoM;
FILE *pos_output, *dim_output;
/* Get command line input */
if(argc == EXPECTED_ARGS)
{
valid_input = sscanf(argv[1], "%ld", &dim);
valid_input = valid_input && sscanf(argv[2],"%ld", &particles);
}
else
{
printf("***Enter program name followed by width of lattice followed by no. of particles***\n");
valid_input = 0;
}
/* Checks dim > 1 */
if(dim < 2)
{
printf("*** Width of lattive must be greater than or equal to 2 ***\n");
valid_input = 0;
}
/* Checks particles isn't too large or small */
if(particles > (dim+1)*(dim+1)-1 || particles < 1)
{
printf("*** Particles must be less than (width+1)^2 and greater than 0 ***\n");
valid_input = 0;
}
/* Final validation */
if(!valid_input)
{
printf("*** Input Validation failed! ***\n");
return(EXIT_FAILURE);
}
/* Declare sites[][] which will say whether each site is occupy
** and declares all values to be zero */
int sites[dim+1][dim+1];
for(j=0; j<dim+1; j++)
{
for(k=0;k<dim+1;k++)
{
sites[j][k]=0;
}
}
/* Places seed particle at centre (or one of the 4 sites close to the centre) */
j = (int) ((double) (dim) /(double) (2));
sites[j][j] = 1;
/* Seed rand numbers */
srand((int) time(NULL));
/* Does the whole diffusion process for desired number of particles */
while(i<particles)
{
rnd = (rand() % 4);
if(rnd == 0)
{
ypos = 0;
xpos = (int) ( (dim-1) * (double) rand() / (double) (RAND_MAX) );
}
if(rnd == 1)
{
xpos = 0;
ypos = (int) ( (dim-1) * (double) rand() / (double) (RAND_MAX) ) +1;
}
if(rnd == 2)
{
ypos = dim;
xpos = (int) ( (dim-1) * (double) rand() / (double) (RAND_MAX) );
}
if(rnd == 3)
{
xpos = dim;
ypos = (int) ( (dim-1) * (double) rand() / (double) (RAND_MAX) )+1;
}
/* If site chosen above is occupied, repeats until non-occupied site is chosen */
while(sites[xpos][ypos])
{
rnd = (rand() % 4);
if(rnd == 0)
{
ypos = 0;
xpos = (int) ( (dim)* (double) rand() / (double) (RAND_MAX) );
}
if(rnd == 1)
{
xpos = 0;
ypos = (int) ( (dim)* (double) rand() / (double) (RAND_MAX) );
}
if(rnd == 2)
{
ypos = dim;
xpos = (int) ( (dim)* (double) rand() / (double) (RAND_MAX) );
}
if(rnd == 3)
{
xpos = dim;
ypos = (int) ( (dim) * (double) rand() / (double) (RAND_MAX) );
}
}
/* Moves particle at random until aggregation */
while(!stop(dim, xpos, ypos, sites))
{
rnd = (rand() % 12);
if(xpos == 0 && ypos == 0)
{
xpos = rnd < 6 ? xpos+1 : xpos;
ypos = rnd >= 6 ? ypos+1 : ypos;
}
if(xpos == 0 && ypos == dim)
{
xpos = rnd < 6 ? xpos+1 : xpos;
ypos = rnd >=6 ? ypos-1 : ypos;
}
if(xpos == dim && ypos == 0)
{
xpos = rnd < 6 ? xpos-1 : xpos;
ypos = rnd >=6 ? ypos+1 : ypos;
}
if(xpos == dim && ypos == dim)
{
xpos = rnd < 6 ? xpos-1 : xpos;
ypos = rnd >=6 ? ypos-1 : ypos;
}
if(xpos == 0 && ypos > 0 && ypos < dim)
{
xpos = rnd < 4 ? xpos+1 : xpos;
ypos = rnd >= 4 && rnd < 8 ? ypos-1 : ypos;
ypos = rnd >=8 ? ypos+1 : ypos;
}
if(ypos == 0 && xpos > 0 && xpos < dim)
{
ypos = rnd < 4 ? ypos+1 : ypos;
xpos = rnd >= 4 && rnd < 8 ? xpos-1 : xpos;
xpos = rnd >=8 ? xpos+1 : xpos;
}
if(xpos == dim && ypos > 0 && ypos < dim)
{
xpos = rnd < 4 ? xpos-1 : xpos;
ypos = rnd >=4 && rnd < 8 ? ypos-1 : ypos;
ypos = rnd >=8 ? ypos+1 : ypos;
}
if(ypos == dim && xpos > 0 && xpos < dim)
{
ypos = rnd < 4 ? ypos-1 : ypos;
xpos = rnd >= 4 && rnd < 8 ? xpos-1 : xpos;
xpos = rnd >=8 ? xpos+1 : xpos;
}
if(xpos > 0 && xpos < dim && ypos > 0 && ypos < dim)
{
xpos = rnd < 3 ? xpos-1 : xpos;
xpos = rnd >=3 && rnd < 6 ? xpos+1 : xpos;
ypos = rnd >=6 && rnd < 9 ? ypos-1 : ypos;
ypos = rnd >=9 ? ypos+1 : ypos;
}
}
/* Tells sites[][] that this position is now occupied */
sites[xpos][ypos] = 1;
i++;
}
/* Calculates Centre of mass (CoM) */
xsum = 0;
ysum = 0;
for(i=0; i<dim+1; i++)
{
for(j=0; j<dim+1; j++)
{
if(sites[i][j] == 1)
{
xsum = xsum + i;
ysum = ysum + j;
}
}
}
double a = (double) xsum;
double b = (double) ysum;
double d = (double) dim;
printf("xsum = %ld \nysum = %ld \n", xsum, ysum);
xCoM = a / d;
yCoM = b / d;
printf("CoM = ( %lf.3 , %lf.3 )\n", xCoM, yCoM);
pos_output = fopen("1007189_proj2_1.out","w");
if(pos_output != (FILE*) NULL)
{
for(i=0; i<dim+1; i++)
{
for(j=0; j<dim+1; j++)
{
fprintf(pos_output, "%ld %ld %ld\n", i, j, sites[i][j]);
}
}
fclose(pos_output);
}
/* Declares frac_dim[], which stores the number of particles
** within a given distance of the CoM */
int frac_dim[1 + dim/2];
for(i=0; i< 1 + dim/2; i++)
{
dim_count = 0;
for(j=0; j<dim+1; j++)
{
for(k=0; k<dim+1; k++)
{
if( (j-xCoM)*(j-xCoM) + (k-yCoM)*(k-yCoM) < i*i && sites[i][j] == 1)
{
dim_count = dim_count + 1;
}
}
}
frac_dim[i] = dim_count;
}
dim_output = fopen("1007189_proj2_2.out","w");
if(dim_output != (FILE*) NULL)
{
for(i=0; i< 1 + dim/2; i++)
{
{
fprintf(dim_output, "%ld %ld\n", i, frac_dim[i]);
}
}
fclose(dim_output);
}
}