programming assessed work 2

10
Programming Skills Assessed Work 2 Candidate Number: 20825 April 29, 2015 1 Diffusion in One Dimension This program will perform M = 100 random walks through L = 10 sites, labelled from i =0,...,L - 1, and print the average path length, which is 45.02 in this case 1.1 C Code #include <stdio.h> int ranInt ( int max ); // returns a random integer from 0 to max -1 int rHop ( int curSite , int numSites ); // increments current site with periodic boundaries int lHop ( int curSite , int numSites ); // decrements current site with periodic boundaries main () { //VARIABLE DECLARATIONS int i =0; // current site , allowed from 0 to L-1 int L =10; // number of sites int pos [ L ]; // array indexing the sites from 0 to L-1, incrementing each time that site is visited int visit =0; // increments each time a site is visited for the first time int tempRan ; // temporary storage of random integer int j ; // for-loop index int k ; // averaging index float M =100; // number of paths to be averaged float tot =0; // running total for averaging int seed =3; // sets up random number generator srandom ( seed ); for ( k =0; k < M ; k ++) // averaging loop { //INITIALISE for ( j =0; j < L ; j ++) { pos [ j ]=0; } 1

Upload: daniel-carpenter

Post on 14-Dec-2015

221 views

Category:

Documents


0 download

DESCRIPTION

University of Bath

TRANSCRIPT

Page 1: Programming Assessed Work 2

Programming Skills Assessed Work 2

Candidate Number: 20825

April 29, 2015

1 Diffusion in One Dimension

This program will perform M = 100 random walks through L = 10 sites,labelled from i = 0, . . . , L− 1, and print the average path length, which is 45.02in this case

1.1 C Code

#include <stdio.h>

int ranInt(int max); // returns a random integer from 0 to max -1

int rHop(int curSite , int numSites); // increments current site

with periodic boundaries

int lHop(int curSite , int numSites); // decrements current site

with periodic boundaries

main()

{

// VARIABLE DECLARATIONS

int i=0; // current site , allowed from 0 to L-1

int L=10; // number of sites

int pos[L]; // array indexing the sites from 0 to L-1,

incrementing each time that site is visited

int visit =0; // increments each time a site is visited for the

first time

int tempRan; // temporary storage of random integer

int j; // for -loop index

int k; // averaging index

float M=100; // number of paths to be averaged

float tot=0; // running total for averaging

int seed =3; // sets up random number generator

srandom(seed);

for(k=0;k<M;k++) // averaging loop

{

// INITIALISE

for(j=0;j<L;j++)

{

pos[j]=0;

}

1

Page 2: Programming Assessed Work 2

visit =0;

// RANDOM WALK

for(j=0;visit <L;j++) // loop terminates when particle has

visited all L sites

{

pos[i]++;

if(pos[i]==1) visit ++;

tempRan=ranInt (2); // the particle can move in two directions

if (tempRan ==0) i=lHop(i,L);

if (tempRan ==1) i=rHop(i,L);

}

tot+=j-1; // j is the number of hops in total , including the

hop after the last site was visited

}

printf("%f\n", tot/M);

}

// FUNCTION DEFINITIONS

int ranInt(int max) // returns a random integer from 0 to max -1

{

return random () % max;

}

int rHop(int curSite , int numSites) // increments current site

with periodic boundaries

{

if(curSite ==numSites -1)

{

curSite =0;

}

else

{

curSite ++;

}

return curSite;

}

int lHop(int curSite , int numSites) // decrements current site

with periodic boundaries

{

if(curSite ==0)

{

curSite=numSites -1;

}

else

{

curSite --;

}

return curSite;

}

2

Page 3: Programming Assessed Work 2

Figure 1: a line graph showing length of path taken for different environments

1.2 Discussion and Interpretation

Figure 1 shows a few key properties expected in this problem. Firstly, the graphintercepts at the origin, because it would take no time to explore an environmentof size zero

Secondly, there is an exponential relationship between the number of sitesL and the path length

Proof. To demonstrate why this must be true, consider how the mean pathlength in this problem would increase as the environment grew larger

For example, between L1 = 10 and L2 = 20, the shortest possible pathsdouble, and therefore the mean length must increase by more than double

This means that it is reasonable to suggest that the path length J will satisfy

dJ

dL∝ ∆L.

This is a property satisfied uniquely by the exponential function

These results were generated using a similar program that looped over Lfrom 1 to 50 and printed the results to a text file, which was then turned intoa graph using the online service Plotly c© 2015

3

Page 4: Programming Assessed Work 2

2 Diffusion in Three Dimensions

Here the average path length is 10261.4 to explore the all 103 sites

2.1 C Code

#include <stdio.h>

int ranInt(int max); // returns a random integer from 0 to max -1

int rHop(int curSite , int numSites); // increments current site

with periodic boundaries

int lHop(int curSite , int numSites); // decrements current site

with periodic boundaries

main()

{

// VARIABLE DECLARATIONS

int i1=0, i2=0, i3=0; // coordinates of current site , allowed

from 0 to L-1

int L=10; // number of sites per coordinate axis

int pos[L][L][L]; // array indexing the sites from 0 to L-1,

incrementing each time that site is visited

int visit =0; // increments each time a site is visited for the

first time

int tempRan; // temporary storage of random integer

int j, j1, j2, j3; // for -loop indices

int k; // averaging index

float M=100; // number of paths to be averaged

float tot=0; // running total for averaging

int seed =3; // sets up random number generator

srandom(seed);

for(k=0;k<M;k++) // averaging loop

{

// INITIALISE

for(j1=0;j1 <L;j1++)

{

for(j2=0;j2 <L;j2++)

{

for(j3=0;j3 <L;j3++)

{

pos[j1][j2][j3]=0;

}

}

}

visit =0;

// RANDOM WALK

for(j=0;visit <L*L*L;j++) // loop terminates when particle has

visited all L^3 sites

{

pos[i1][i2][i3]++;

if(pos[i1][i2][i3]==1)

{

4

Page 5: Programming Assessed Work 2

visit ++;

}

tempRan=ranInt (6); // the particle can travel in six

directions

if (tempRan ==0) i1=lHop(i1,L);

if (tempRan ==1) i1=rHop(i1,L);

if (tempRan ==2) i2=lHop(i2,L);

if (tempRan ==3) i2=rHop(i2,L);

if (tempRan ==4) i3=lHop(i3,L);

if (tempRan ==5) i3=rHop(i3,L);

}

tot+=j-1; // j is the number of hops in total , including the

hop after the last site was visited

}

printf("%f\n", tot/M);

}

// FUNCTION DEFINITIONS

int ranInt(int max) // returns a random integer between 1 and

max

{

return random () % max;

}

int rHop(int curSite , int numSites) // increments current site

with periodic boundaries

{

if(curSite ==numSites -1)

{

curSite =0;

}

else

{

curSite ++;

}

return curSite;

}

int lHop(int curSite , int numSites) // decrements curent site

with periodic boundaries

{

if(curSite ==0)

{

curSite=numSites -1;

}

else

{

curSite --;

}

return curSite;

}

5

Page 6: Programming Assessed Work 2

Figure 2: a line graph showing length of path taken for different environments

2.2 Discussion and Interpretation

Figure 2 has the same shape as Figure 1, but with a much steeper gradient,because of the additional dimensions

The average path length for L = 3 sites per axis (giving 33 = 27 sites intotal) is 119. The corresponding value in the previous question, for a line oflength L = 27 was 354

The corresponding values are lower at every point in this question becausethere are more directions to choose from, so the space is explored more quickly

A periodic line (a 1D object) can be happily represented in 2D as a circle,but a periodic 3D cube cannot be visualised without intersecting itself

Despite this, it is certainly true that every point on the cube of side lengthL can be mapped to a corresponding line of length L3, which could be answeredcompletely using the tools in the previous question, provided that all six possibleneighbours are handled appropriately at each site.

Therefore, it is reasonable to expect that these questions would have similarresults

6

Page 7: Programming Assessed Work 2

3 Diffusion on a Network

There are B sites in each of the A clusters in this network. The clusters areconnected in a periodic line, that can be moved along only when in sites i = 0and i = 1 in each cluster, and only left and right respectively. Each site isconnected to every other within the same cluster

3.1 C Code

#include <stdio.h>

int ranInt(int max); // returns a random integer from 0 to max -1

int rHop(int curCluster , int numClusters); // increments current

cluster with periodic boundaries

int lHop(int curCluster , int numClusters); // decrements current

cluster with periodic boundaries

int siteHop(int curSite , int numSites); // returns a random new

site that is different to the old site

main()

{

// VARIABLE DECLARATIONS

int c=0; // current cluster , allowed from 0 to A-1

int i=0; // current site within cluster , allowed from 0 to B-1

int A=5; // number of clusters

int B=4; // number of sites in a cluster

int pos[A][B]; // indexes sites and their clusters

int visit =0; // increments each time a site in a cluster is

visited for the first time

int tempRan; // temporary storage of random integer

int j, a, b; // for -loop indices

int k; // averaging index

float tot=0; // running total for averaging

float M=100;

int seed =3; // sets up random number generator

srandom(seed);

for(k=0;k<M;k++) // averaging loop

{

// INITIALISE

for(a=0;a<A;a++)

{

for(b=0;b<B;b++)

{

pos[a][b]=0;

}

}

visit =0;

// RANDOM WALK

for(j=0;visit <A*B;j++)

{

pos[c][i]++;

7

Page 8: Programming Assessed Work 2

if(pos[c][i]==1)

{

visit ++;

}

if(i>1) // then particle cannot change cluster

{

tempRan =0;

}

else // particle will either change cluster or hop within

cluster

{

tempRan=ranInt (2); // if current site is 0 or 1, particle

either changes cluster or hops within cluster , with

equal chance

}

if(tempRan ==0) // then particle hops to site from 0 to B-1

within cluster

{

i=siteHop(i,B);

}

if(tempRan ==1) // then particle hops between clusters

{

if(i==0)

{

c=lHop(c, A);

}

if(i==1)

{

c=rHop(c, A);

}

}

}

tot+=j-1; // j is the number of hops in total , including the

hop after the last site was visited

}

printf("%f\n", tot/M);

}

// FUNCTION DEFINITIONS

int ranInt(int max) // returns a random integer between 1 and

max

{

return random () % max;

}

int rHop(int curCluster , int numClusters) // increments current

cluster with periodic boundaries

{

if(curCluster == numClusters -1)

{

curCluster =0;

}

else

{

curCluster ++;

}

return curCluster;

}

8

Page 9: Programming Assessed Work 2

int lHop(int curCluster , int numClusters) // decrements current

cluster with periodic boundaries

{

if(curCluster ==0)

{

curCluster=numClusters -1;

}

else

{

curCluster --;

}

return curCluster;

}

int siteHop(int oldSite , int numSites) // returns a random new

site that is different to the old site

{

int newSite=ranInt(numSites -1);

if(newSite >= oldSite) // then shift up the new site so that the

particle doesn ’t hop to it’s old site

{

newSite ++;

}

return newSite;

}

9

Page 10: Programming Assessed Work 2

Figure 3: a heat map showing length of path taken for different environments

3.2 Discussion and Interpretation

Figure 3 shows how both the number of clusters, A, and the number of sites percluster, B, determine the path length

Both quantities demonstrate the exponential pattern already observed, al-though examining the corners of the heat map shows that B contributes morethan A to the length of path taken

However, surprisingly, the graph is almost symmetric about the diagonal,meaning that A and B contribute a very similar amount

B contributes more than A because, as B grows larger, it becomes less likelyat any moment for the particle to be able to change cluster, and hence inevitablythe full environment

On the other hand, A acts like L in question one, only there is a randomamount of time between hops

10