Thread: Random Walk Hexogonal lattice

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    20

    Question Random Walk Hexogonal lattice

    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);
    
    }

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Think of a hexagonal matrix as a regular matrix, except every odd row is shifted:
    Code:
    . . . . .
     . . . . .
    . . . . .
     . . . . .
    So the six directions you can travel depend on whether you're on an odd row or even row. You'll have to work the X and Y coordinate displacements for each direction.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    20
    nonnob, thank you for your respond. It helped me. Should I illustrate X, Y coordinate displacement in degrees? I am struggling of illustrating the coordinates in the code itself.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    No. You just need to define the 6 directions. How about a clockwise system.
    Row [0], [2], [4]...
    direction 0 - row--
    direction 1 - col++
    direction 2 - row++
    direction 3 - row++, col--
    direction 4 - col--
    direction 5 - row--, col--

    Row [1], [3], [5]...
    direction 0 - row--, col++
    direction 1 - col++
    direction 2 - row++, col++
    direction 3 - row++
    direction 4 - col--
    direction 5 - row--

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random Walk Simulation in 1D for 10 or more particles
    By SalamuB in forum C Programming
    Replies: 1
    Last Post: 02-06-2012, 12:48 PM
  2. Using 2 sets of arrays for a random walk 2D program
    By sashadem in forum C Programming
    Replies: 1
    Last Post: 10-31-2011, 01:48 AM
  3. Random walk solution
    By skiabox in forum C Programming
    Replies: 5
    Last Post: 11-01-2010, 10:36 AM
  4. Code for random walk in 1D
    By LLcoolc++ in forum C Programming
    Replies: 5
    Last Post: 10-18-2009, 09:33 AM
  5. Help with a random walk
    By pxleyes in forum C Programming
    Replies: 11
    Last Post: 02-27-2004, 09:11 PM