1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/* BEGINNING OF DECLARING FUNCTIONS */
void initialise(int L,float rho,int n[]); /* Sets up our ASEP system */
int dynamics(int TIME,int L,float p,int n[],float J); /* Creates a moving system */
void output1(int L,int n[],float rho,int HIST); /* Outputs the initial system to a file */
void output2(int L,int n[],int TIME,float p,float J,int HIST); /* Outputs the moving system to a file */
void output3(float rho,float J,int L,int TIME,int HIST);
int rnd1(void); /* Random number generator between 0 and 1 */
int rnd2(int range); /* Random sites generator */
void seedrnd(void); /* Seeds random numbers */
/* END OF DECLARING FUNCTIONS */
/* BEGINNING OF MAIN PROGRAM */
int main()
{
float J;
int HIST;
int n;
int L; /* Number of sites */
int TIME; /* Run time */
float rho; /* Density */
float p; /* Probability */
seedrnd();
printf("How many times would you like to run the program?:");
scanf("%i",&HIST);
printf("How long would you like to run your system?:");
scanf("%i",&TIME);
printf("Enter a value for L (number of sites):");
scanf("%i",&L);
printf("Enter a value for p (probability):");
scanf("%f",&p);
for(rho=0;rho<21;rho++)
{
J=0;
for(n=0;n<HIST;n++)
{
int n[L]; /* Array of size L */
{
initialise(L,rho,n);
output1(L,n,rho,HIST);
}
{
J=dynamics(TIME,L,p,n,J);
output2(L,n,TIME,p,J,HIST);
}
}
output3(rho,J,L,TIME,HIST);
}
return(0);
}
/* END OF MAIN PROGRAM */
/* BEGINNING OF DEFINING FUNCTIONS */
void initialise(int L,float rho,int n[])
{
int j;
double result1;
for (j=0;j<L;j++)
{
result1=(double)rnd1()/RAND_MAX; /* Pick a random number between 0 and 1 */
if(result1<(rho/20)) /* If the random number is less than rho */
{
n[j]=1; /* then there is a particle on the site */
}
else
{
n[j]=0; /* otherwise there is no particle on the site */
}
}
}
int dynamics(int TIME,int L,float p,int n[],float J)
{
int t;
int i;
double result2;
for(t=0;t<L*TIME;t++)
{
i=rnd2(L); /* Picks a random site */
result2=(double)rnd1()/RAND_MAX; /* Pick a random number between 0 and 1 */
if(((n[i]==1) && (n[(i+1)%L]==0)) && (result2<p)) /* If the site has a particle on it and the next site is vacant
and the random number chosen between 0 and 1 is less than p */
{
n[i]=0; /* then set that site equal to 0 */
n[(i+1)%L]=1; /* and the next site equal to 1 (so basically the particle has moved) */
J=J+1;
}
else
{
continue; /* otherwise do nothing and carry on with the loop */
}
}
return(J);
}
void output1(int L,int n[],float rho,int HIST)
{
int j;
{
FILE *fp1;
fp1=fopen("file1.txt","w");
if(fp1)
{
fprintf(fp1,"rho = ");
fprintf(fp1,"%f\t",rho);
fprintf(fp1,"HIST = ");
fprintf(fp1,"%i\n",HIST);
fprintf(fp1,"site\t\tnumber of particles\n");
for (j=0;j<L;j++)
{
fprintf(fp1,"%i",j);
fprintf(fp1,"\t\t");
fprintf(fp1,"%i\n",n[j]);
}
}
fclose(fp1);
}
}
void output2(int L,int n[],int TIME,float p,float J,int HIST)
{
int i;
{
FILE *fp2;
fp2=fopen("file2.txt","w");
if(fp2)
{
fprintf(fp2,"Run time: ");
fprintf(fp2,"%i\t",TIME);
fprintf(fp2,"p = ");
fprintf(fp2,"%f\t",p);
fprintf(fp2,"site\t\tnumber of particles\n");
for(i=0;i<L;i++)
{
fprintf(fp2,"%i",i);
fprintf(fp2,"\t\t");
fprintf(fp2,"%i\n",n[i]);
}
}
fclose(fp2);
}
}
void output3(float rho,float J,int L,int TIME,int HIST)
{
FILE *fp3;
fp3=fopen("file3.txt","a");
if(fp3)
{
fprintf(fp3,"rho\t\t");
fprintf(fp3,"Current\n");
fprintf(fp3,"%f\t\t",rho/20);
fprintf(fp3,"%f\n",J/(L*TIME*HIST));
}
}
int rnd1(void)
{
int r;
r=rand();
return(r);
}
int rnd2(int range)
{
int s;
s=rand()%range;
return(s);
}
void seedrnd(void)
{
srand((unsigned)time(NULL));
}
/* END OF DEFINING FUNCTIONS */
|