Você está na página 1de 10

Programming Skills Assessed Work 2

Candidate Number: 20825


April 29, 2015

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 ) ; // i n c r e m e n t s current site
with p e r i o d i c b o u n d a r i e s
int lHop ( int curSite , int numSites ) ; // d e c r e m e n t s current site
with p e r i o d i c b o u n d a r i e s
main ()
{
// V A R I A B L E D E C L A R A T I O N S
int i =0; // current site , allowed from 0 to L -1
int L =10; // number of sites
int pos [ L ]; // array i n d e x i n g the sites from 0 to L -1 ,
i n c r e m e n t i n g each time that site is visited
int visit =0; // i n c r e m e n t s each time a site is visited for the
first time
int tempRan ; // t e m p o r a r y storage of random integer
int j ; // for - loop index
int k ; // a v e r a g i n g index
float M =100; // number of paths to be a v e r a g e d
float tot =0; // running total for a v e r a g i n g
int seed =3; // sets up random number g e n e r a t o r
srandom ( seed ) ;
for ( k =0; k < M ; k ++) // a v e r a g i n g loop
{
// I N I T I A L I S E
for ( j =0; j < L ; j ++)
{
pos [ j ]=0;
}

visit =0;
// RANDOM WALK
for ( j =0; visit < L ; j ++) // loop t e r m i n a t e s when p a r t i c l e has
visited all L sites
{
pos [ i ]++;
if ( pos [ i ]==1) visit ++;
tempRan = ranInt (2) ; // the p a r t i c l e can move in two d i r e c t i o n s
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 , i n c l u d i n g the
hop after the last site was visited
}
printf ( " % f \ n " , tot / M ) ;
}
// F U N C T I O N D E F I N I T I O N S
int ranInt ( int max ) // returns a random integer from 0 to max -1
{
return random () % max ;
}
int rHop ( int curSite , int numSites ) // i n c r e m e n t s current site
with p e r i o d i c b o u n d a r i e s
{
if ( curSite == numSites -1)
{
curSite =0;
}
else
{
curSite ++;
}
return curSite ;
}
int lHop ( int curSite , int numSites ) // d e c r e m e n t s current site
with p e r i o d i c b o u n d a r i e s
{
if ( curSite ==0)
{
curSite = numSites -1;
}
else
{
curSite - -;
}
return curSite ;
}

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 graph
intercepts at the origin, because it would take no time to explore an environment
of size zero
Secondly, there is an exponential relationship between the number of sites
L and the path length
Proof. To demonstrate why this must be true, consider how the mean path
length in this problem would increase as the environment grew larger
For example, between L1 = 10 and L2 = 20, the shortest possible paths
double, 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
L.
dL
This is a property satisfied uniquely by the exponential function
These results were generated using a similar program that looped over L
from 1 to 50 and printed the results to a text file, which was then turned into
c 2015
a graph using the online service Plotly

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 ) ; // i n c r e m e n t s current site
with p e r i o d i c b o u n d a r i e s
int lHop ( int curSite , int numSites ) ; // d e c r e m e n t s current site
with p e r i o d i c b o u n d a r i e s
main ()
{
// V A R I A B L E D E C L A R A T I O N S
int i1 =0 , i2 =0 , i3 =0; // c o o r d i n a t e s of current site , allowed
from 0 to L -1
int L =10; // number of sites per c o o r d i n a t e axis
int pos [ L ][ L ][ L ]; // array i n d e x i n g the sites from 0 to L -1 ,
i n c r e m e n t i n g each time that site is visited
int visit =0; // i n c r e m e n t s each time a site is visited for the
first time
int tempRan ; // t e m p o r a r y storage of random integer
int j , j1 , j2 , j3 ; // for - loop indices
int k ; // a v e r a g i n g index
float M =100; // number of paths to be a v e r a g e d
float tot =0; // running total for a v e r a g i n g
int seed =3; // sets up random number g e n e r a t o r
srandom ( seed ) ;
for ( k =0; k < M ; k ++) // a v e r a g i n g loop
{
// I N I T I A L I S E
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 t e r m i n a t e s when p a r t i c l e has
visited all L ^3 sites
{
pos [ i1 ][ i2 ][ i3 ]++;
if ( pos [ i1 ][ i2 ][ i3 ]==1)
{

visit ++;
}
tempRan = ranInt (6) ; // the p a r t i c l e can travel in six
directions
if
if
if
if
if
if

( tempRan ==0)
( tempRan ==1)
( tempRan ==2)
( tempRan ==3)
( tempRan ==4)
( tempRan ==5)

i1 = lHop ( i1 , L ) ;
i1 = rHop ( i1 , L ) ;
i2 = lHop ( i2 , L ) ;
i2 = rHop ( i2 , L ) ;
i3 = lHop ( i3 , L ) ;
i3 = rHop ( i3 , L ) ;

}
tot += j -1; // j is the number of hops in total , i n c l u d i n g the
hop after the last site was visited
}
printf ( " % f \ n " , tot / M ) ;
}
// F U N C T I O N D E F I N I T I O N S
int ranInt ( int max ) // returns a random integer between 1 and
max
{
return random () % max ;
}
int rHop ( int curSite , int numSites ) // i n c r e m e n t s current site
with p e r i o d i c b o u n d a r i e s
{
if ( curSite == numSites -1)
{
curSite =0;
}
else
{
curSite ++;
}
return curSite ;
}
int lHop ( int curSite , int numSites ) // d e c r e m e n t s curent site
with p e r i o d i c b o u n d a r i e s
{
if ( curSite ==0)
{
curSite = numSites -1;
}
else
{
curSite - -;
}
return curSite ;
}

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 in
total) is 119. The corresponding value in the previous question, for a line of
length L = 27 was 354
The corresponding values are lower at every point in this question because
there 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 length
L can be mapped to a corresponding line of length L3 , which could be answered
completely using the tools in the previous question, provided that all six possible
neighbours are handled appropriately at each site.
Therefore, it is reasonable to expect that these questions would have similar
results

Diffusion on a Network

There are B sites in each of the A clusters in this network. The clusters are
connected in a periodic line, that can be moved along only when in sites i = 0
and i = 1 in each cluster, and only left and right respectively. Each site is
connected 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 ) ; // i n c r e m e n t s current
cluster with p e r i od i c b o u n d a r i e s
int lHop ( int curCluster , int numClusters ) ; // d e c r e m e n t s current
cluster with p e r i od i c b o u n d a r i e s
int siteHop ( int curSite , int numSites ) ; // returns a random new
site that is d i f f e r e n t to the old site
main ()
{
// V A R I A B L E D E C L A R A T I O N S
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
int
int
int

A =5; // number of c l u s t e r s
B =4; // number of sites in a cluster
pos [ A ][ B ]; // indexes sites and their c l u s t e r s
visit =0; // i n c r e m e n t s each time a site in a cluster is
visited for the first time

int tempRan ; // t e m p o r a r y storage of random integer


int j , a , b ; // for - loop indices
int k ; // a v e r a g i n g index
float tot =0; // running total for a v e r a g i n g
float M =100;
int seed =3; // sets up random number g e n e r a t o r
srandom ( seed ) ;
for ( k =0; k < M ; k ++) // a v e r a g i n g loop
{
// I N I T I A L I S E
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 ]++;

if ( pos [ c ][ i ]==1)
{
visit ++;
}
if (i >1) // then p a r t i c l e cannot change cluster
{
tempRan =0;
}
else // p a r t i c l e will either change cluster or hop within
cluster
{
tempRan = ranInt (2) ; // if current site is 0 or 1 , p a r t i c l e
either changes cluster or hops within cluster , with
equal chance
}
if ( tempRan ==0) // then p a r t i c l e hops to site from 0 to B -1
within cluster
{
i = siteHop (i , B ) ;
}
if ( tempRan ==1) // then p a r t i c l e hops between c l u s t e r s
{
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 , i n c l u d i n g the
hop after the last site was visited
}
printf ( " % f \ n " , tot / M ) ;
}
// F U N C T I O N D E F I N I T I O N S
int ranInt ( int max ) // returns a random integer between 1 and
max
{
return random () % max ;
}
int rHop ( int curCluster , int numClusters ) // i n c r e m e n t s current
cluster with p e r i o di c b o u n d a r i e s
{
if ( curCluster == numClusters -1)
{
curCluster =0;
}
else
{
curCluster ++;
}
return curCluster ;
}

int lHop ( int curCluster , int numClusters ) // d e c r e m e n t s current


cluster with p e r i o di c b o u n d a r i e s
{
if ( curCluster ==0)
{
curCluster = numClusters -1;
}
else
{
curCluster - -;
}
return curCluster ;
}
int siteHop ( int oldSite , int numSites ) // returns a random new
site that is d i f f e r e n t to the old site
{
int newSite = ranInt ( numSites -1) ;
if ( newSite >= oldSite ) // then shift up the new site so that the
p a r t i c l e doesn t hop to it s old site
{
newSite ++;
}
return newSite ;
}

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 per
cluster, B, determine the path length
Both quantities demonstrate the exponential pattern already observed, although examining the corners of the heat map shows that B contributes more
than 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 likely
at any moment for the particle to be able to change cluster, and hence inevitably
the full environment
On the other hand, A acts like L in question one, only there is a random
amount of time between hops

10

Você também pode gostar