Self-Avoiding Random Walks

Simple Sampling Self-Avoiding Random Walk

In this example of program code we show the main loop of a simple sampling algorithm of self-avoiding random walks. The algorithm to perform the basic steps of the walker is the same as for the random walk. To implement the self-avoidance, we have an array (w) which mimics the underlying lattice. Each site the walker goes to will be labeled by the number the walker is assigned to. This allows several walkers to travel the lattice without resetting it. At each step the algorithm checks in the array w whether the walker has visited the site. If so, the walk terminates signaled by the flag occupancy. For a full walk we calculate the radius of gyration.

 

/*----------------------------------------------------------------------*/
/* Begin of Sample Loop */
/*----------------------------------------------------------------------*/

while ( sample < sample_size ) {

    /* ==== Reset the walker to the origin ==== */
    w[0][0] = xc;
    w[0][1] = yc;
    x = xc;
    y = yc;
    l = 0;
    occupancy = 0;
    walk++;
    return_code = r250( N,ran,mf);
    while ( (l < N) && (occupancy == 0) ) {
      d = ran[l] * 4;
      switch (d) {
         case 0: x++;
                 break;
         case 1: y++;
                 break;
         case 2: x--;
                 break;
         case 3: y--;
                 break;
       }
       if ( ( x < 0 ) || ( x == L ) || ( y < 0 ) || ( y == L ) ) {
           /* Random walker not on the lattice */
           exit(-1);
       }
       if ( g[x][y] < walk ) {
           g[x][y] = walk;
           l++;
           w[l][0] = x;
           w[l][1] = y;
           occupancy = 0;
       } else {
           occupancy = 1;
       }
}

/* ==== Now check if a SAW was generated. If yes, then ==== */
/* ==== we do the analysis, else we must try again ==== */

if ( l == N ) {
     /* ---- we can compute the end-to-end distance etc. ---- */
     x = xc - w[N-1][0];
     y = yc - w[N-1][1];
     end_to_end += x*x + y*y;
     cmx = 0;
     cmy = 0;
     for (i=0; i < N; i++) {
         cmx += w[i][0];
         cmy += w[i][1];
     }
     cmx /= N;
     cmy /= N;
     rad = 0;
     for (i=0; i < N; i++) {
         x = cmx - w[i][0];
         y = cmy - w[i][1];
         rad += x*x + y*y;
     }
     radius_of_gyration += rad;
     sample++;
}
}

previous page next page