Hello,
I have written a code for 2 dimensional square lattice Random Walk and calculated its Mean square Displacement. Now, I have a problem of changing it to hexagonal lattice. In the code, I have 4 possible directions, Now I need to have 6 possible directions of moving particle. Does anyone know how should I change the directions? Here is the code:
Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <float.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/times.h>
#define TIME 1000 /* number of time steps */
#define FLAG 100
#define NP 10000 /* number of "particles" */
double rndm(void){
return(double)rand()/RAND_MAX;
}
int main()
{
FILE *myfilename;
int i,j,t;
int pos[NP+5][2];
double msd,xhelp;
int xdummy,xint;
/* Set up random number generator */
srand(time(NULL));
/* Set up output file */
if((myfilename=fopen("RandomWalkHexo.txt","w"))==NULL){
printf("It didn't succeed to open 'RandomWalkHexo.txt' file.\n");
printf("Exiting...\n");
exit(0);
}
fprintf(myfilename,"# Time| MSD \n");
fprintf(myfilename,"#=================\n");
/* Set initial position to zero */
for(i=0;i<NP;i++){
for(j=0;j<2;j++){
pos[i][j]=0;
}
}
/* Start of main loop */
xdummy=0;
for(t=1;t<=TIME;t++){
xdummy++;
for(i=0;i<NP;i++){
/* Move particle to the right or to the left */
xhelp=rndm();
if(xhelp<0.25){
pos[i][0]--;
}
else{
if(xhelp<0.50){
pos[i][1]--;
}
else{
if(xhelp<0.75){
pos[i][0]++;
}
else{
pos[i][1]++;
}
}
}
/*fprintf(myfilename,"pos[%d][0]= %d, pos[%d][1]= %d\n",i,pos[i][0],i,pos[i][1]);*/
}
if(xdummy==FLAG){
/* Calculate mean square displacement (MSD) */
msd=0.0;
for(i=0;i<NP;i++){
msd=msd+(pos[i][0]*pos[i][0])+(pos[i][1]*pos[i][1]);
/*fprintf(myfilename,"msd= %lf\n",msd);*/
}
msd=msd/((double)(NP));
/*fprintf(myfilename,"t= %5d, MSD= %10.5lf\n",t,msd);*/
fprintf(myfilename," %5d %10.5lf\n",t,msd);
xdummy=0;
}
}
fclose(myfilename);
}