Python speed issue.

Reply

Join Date: Feb 2009
Posts: 44
Reputation: breatheasier is an unknown quantity at this point 
Solved Threads: 2
breatheasier's Avatar
breatheasier breatheasier is offline Offline
Light Poster

Python speed issue.

 
0
  #1
Mar 10th, 2009
Hi, I have been writing a program that simulates the behaviour of a system of hard disks using the monte carlo method. I ran the program upto around 2 million steps and it's really slow(takes around 3 1/2 to 4 hours).

I first wrote it in vpython as I'm a complete beginner and I needed to see where I was going wrong!

However I'm trying to move away from the visual side now as that should help speed things up. I tested psyco over 5,000 steps and found that it was actually 4 seconds slower than without it!

I was just wondering if anyone could give me any pointers as to things that could obviously be optimised.

Here's the full code, Ignore the PairCorrelationFunction function as I didn't write that part.

  1. from visual import * #
  2.  
  3. from math import * #IMPORT LIBRARIES
  4.  
  5. from random import * #
  6. from numpy import array
  7.  
  8. import time
  9.  
  10. start = time.time()
  11.  
  12.  
  13. def PairCorrelationFunction_2D(x,y,S,rMax,dr):
  14. """Compute the two-dimensional pair correlation function, also known
  15. as the radial distribution function, for a set of circular particles
  16. contained in a square region of a plane. This simple function finds
  17. reference particles such that a circle of radius rMax drawn around the
  18. particle will fit entirely within the square, eliminating the need to
  19. compensate for edge effects. If no such particles exist, an error is
  20. returned. Try a smaller rMax...or write some code to handle edge effects! ;)
  21.  
  22. Arguments:
  23. x an array of x positions of centers of particles
  24. y an array of y positions of centers of particles
  25. S length of each side of the square region of the plane
  26. rMax outer diameter of largest annulus
  27. dr increment for increasing radius of annulus
  28.  
  29. Returns a tuple: (g, radii, interior_x, interior_y)
  30. g(r) a numpy array containing the correlation function g(r)
  31. radii a numpy array containing the radii of the
  32. annuli used to compute g(r)
  33. interior_x x coordinates of reference particles
  34. interior_y y coordinates of reference particles
  35. """
  36. from numpy import zeros, sqrt, where, pi, average, arange, histogram
  37. # Number of particles in ring/area of ring/number of reference particles/number density
  38. # area of ring = pi*(r_outer**2 - r_inner**2)
  39.  
  40. # Find particles which are close enough to the box center that a circle of radius
  41. # rMax will not cross any edge of the box
  42.  
  43. bools1 = x>1.1*rMax
  44. bools2 = x<(S-1.1*rMax)
  45. bools3 = y>rMax*1.1
  46. bools4 = y<(S-rMax*1.1)
  47. interior_indices, = where(bools1*bools2*bools3*bools4)
  48. num_interior_particles = len(interior_indices)
  49.  
  50. if num_interior_particles < 1:
  51. raise RuntimeError ("No particles found for which a circle of radius rMax\
  52. will lie entirely within a square of side length S. Decrease rMax\
  53. or increase the size of the square.")
  54.  
  55. edges = arange(0., rMax+1.1*dr, dr)
  56. num_increments = len(edges)-1
  57. g = zeros([num_interior_particles, num_increments])
  58. radii = zeros(num_increments)
  59. numberDensity = len(x)/S**2
  60.  
  61. # Compute pairwise correlation for each interior particle
  62. for p in range(num_interior_particles):
  63. index = interior_indices[p]
  64. d = sqrt((x[index]-x)**2 + (y[index]-y)**2)
  65. d[index] = 2*rMax
  66.  
  67. (result,bins) = histogram(d, bins=edges, normed=False, new=True)
  68. g[p,:] = result/numberDensity
  69.  
  70. # Average g(r) for all interior particles and compute radii
  71. g_average = zeros(num_increments)
  72. for i in range(num_increments):
  73. radii[i] = (edges[i] + edges[i+1])/2.
  74. rOuter = edges[i+1]
  75. rInner = edges[i]
  76. g_average[i] = average(g[:,i])/(pi*(rOuter**2 - rInner**2))
  77.  
  78. return (g_average, radii, x[interior_indices], y[interior_indices])
  79.  
  80. #my code starts here
  81. nlat=20 #HOW MANY DISKS ON THE SIDES OF THE BOX
  82.  
  83. r = 0.5 #DISK RADIUS
  84.  
  85. d = 2.0*r #DIAMETER
  86.  
  87. s = 1.22 #GAPS, ALTER TO CHANGE DENSITY
  88. S = s - 1
  89.  
  90. L = (nlat*d)+(nlat-1)*S #LENGTH OF SIDES OF BOX
  91. rMax = 5.0
  92. dr=0.08 # Radius for increasing the size of the bins
  93. S = L
  94. area = L**2
  95. pi = 3.141592654
  96. diskarea= pi*(r**2) * nlat**2
  97. density = diskarea/area
  98. cpd = 0.9069
  99. cpdf = (density / cpd)*100
  100.  
  101. print 'density = %f' % (density)
  102. print 'which is %f percent of the closest packed density' % (cpdf)
  103.  
  104.  
  105. scene.autoscale=0 #KEEP CAMERA STILL
  106.  
  107. scene = display(title='Monte Carlo Simulation of a Hard Disk Fluid',width=600, height=600,center=(L/2,L/2,0), background=(1,1,1))
  108.  
  109. #################################DRAW THE BOX#########################################
  110.  
  111.  
  112.  
  113. thk=0.3
  114.  
  115. s2 = L - thk
  116.  
  117. s3 = L + thk
  118.  
  119. wallR = box (pos=vector(L, L/2, 0), length=thk, height=s2, width=d, color = color.yellow)
  120.  
  121. wallL = box (pos=vector(0, L/2, 0), length=thk, height=s2, width=d, color = color.yellow)
  122.  
  123. wallB = box (pos=vector(L/2, 0, 0), length=s3, height=thk, width=d, color = color.yellow)
  124.  
  125. wallT = box (pos=vector(L/2, L, 0), length=s3, height=thk, width=d, color = color.yellow)
  126.  
  127. ###################################################################################
  128.  
  129.  
  130.  
  131.  
  132.  
  133. #######CREATE SQUARE ARRAY OF PARTICLES (NLAT BY NLAT)############
  134.  
  135. alldisks = []
  136.  
  137. for x in range(0,nlat):
  138.  
  139. for y in range(0,nlat):
  140.  
  141. disk=sphere(pos=(x*s,y*s), radius = r, color=color.red)
  142.  
  143. alldisks.append(disk)
  144.  
  145. disk.id = (x,y)
  146.  
  147. ####################################################
  148.  
  149.  
  150.  
  151.  
  152.  
  153. nstep=0.0 #SET FIRST STEP = 0
  154.  
  155. reject=0.0 #COUNT THE NUMBER OF TIMES A STEP IS REJECTED
  156.  
  157. maxpos=L #DEFINE BOUNDARY CONDITIONS
  158.  
  159.  
  160.  
  161. while (nstep<5000):####HOW MANY STEPS?####
  162.  
  163. nstep=nstep+1
  164.  
  165. p = choice(alldisks) #CHOOSE A PARTICLE,P, AT RANDOM FROM THE ARRAY
  166.  
  167.  
  168.  
  169. pnewx=p.x + 0.9*(random() -0.5) #GENERATE A STEP IN X CHANGE COEFFICIENT OF RANDOM TO GET 30% ACCEPTANCE
  170.  
  171. pnewy=p.y + 0.9*(random() -0.5) #GENERATE A STEP IN Y
  172.  
  173. ######PERIODIC BOUNDARY CONDITIONS############
  174.  
  175. if pnewx > L: #IF X GOES BEYOND THE RIGHT WALL
  176.  
  177. pnewx=pnewx - L #MOVE X POSITION TO OTHER SIDE
  178.  
  179.  
  180.  
  181. if pnewx < 0: #IF X FALLS BELOW THE LEFT WALL
  182.  
  183. pnewx=pnewx + L #MOVE X POSITION TO OTHER SIDE
  184.  
  185.  
  186.  
  187. if pnewy > L: #IF Y IS ABOVE BOX
  188.  
  189. pnewy=pnewy - L #MOVE TO THE BOTTOM
  190.  
  191.  
  192.  
  193. if pnewy < 0: #IF Y BELOW
  194.  
  195. pnewy=pnewy + L #TO TOP
  196.  
  197.  
  198. #################################################
  199.  
  200.  
  201.  
  202. rejected = False
  203.  
  204. for disk in alldisks:
  205.  
  206. distance=mag((vector(pnewx,pnewy))-disk.pos)#WORK OUT THE DISTANCE BETWEEN THE NEW POSITION FOR THE DISK AND ANY OTHER DISK
  207.  
  208. if (distance <= 2.0 * r) and p.id != disk.id: #if p overlaps any other disk
  209.  
  210. rejected = True
  211.  
  212. reject=reject+1 # COUNT HOW MANY TIMES THE STEP IS REJECTED
  213.  
  214. break
  215.  
  216.  
  217.  
  218. if not rejected:
  219.  
  220. p.x=pnewx #
  221.  
  222. p.y=pnewy #ACCEPT THE STEPS AS THE NEW POSITION FOR THE PARTICLE, P
  223.  
  224. if (nstep % 1800) == 0: #DUMP AFTER SO MANY STEPS
  225. x_list = []
  226. y_list = []
  227. for disk in alldisks:
  228. x_list.append(disk.x)
  229. y_list.append(disk.y)
  230. output = open('output''%s' % (nstep), 'w') #open file for outputting
  231. (g, radii, newx, newy) = PairCorrelationFunction_2D(array(x_list), array(y_list), S, rMax, dr) #CALCULATE PAIR CORRELATION FUNCTION FOR EACH DUMP
  232.  
  233. for index in range(len(g)):
  234. output.write( "%s %s\n" % (radii[index], g[index]))
  235.  
  236. output.close() #close file
  237. continue
  238.  
  239.  
  240. rejection = (reject/nstep) * 100 #percentage of steps rejected
  241. acceptance = 100 - rejection
  242. print 'acceptance rate was %f per cent' % (acceptance)
  243.  
  244. end = time.time()
  245. elapsed= end - start
  246. print "The simulation took", elapsed, "seconds to run"
Last edited by breatheasier; Mar 10th, 2009 at 6:17 pm. Reason: removed some pointless whitespace
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 122
Reputation: slate is an unknown quantity at this point 
Solved Threads: 30
slate slate is offline Offline
Junior Poster

Re: Python speed issue.

 
0
  #2
Mar 10th, 2009
It seems, that most of the time is spent with this line:
distance=mag((vector(pnewx,pnewy))-disk.pos)

This line is repeated nlat*5000 times.

If you compute the vector before the for disk in alldisks loop, you gain some speed. In my measurement appr. 30%.

Some random remarks:
Do not use star import (from anything import *). You cannot know which object from which module comes. You could accidently shadow some objects too.
Do not import anything inside a function.
If you want to use psyco, don't use mainlevel code. Everything should be put into functions and classes.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 122
Reputation: slate is an unknown quantity at this point 
Solved Threads: 30
slate slate is offline Offline
Junior Poster

Re: Python speed issue.

 
0
  #3
Mar 10th, 2009
Some words about the process profiling code.

To get profile data, you can use:
python -m profile script.py

If that does not give useful results (which is your case), you should but suspicious parts of the code into functions. In your case there are two points.:

The place where you set the p.x and p.y, because that is the point where the gui is updated, I think.
The other is the mentioned magnitude computation.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 44
Reputation: breatheasier is an unknown quantity at this point 
Solved Threads: 2
breatheasier's Avatar
breatheasier breatheasier is offline Offline
Light Poster

Re: Python speed issue.

 
0
  #4
Mar 10th, 2009
If i put the line outside the loop it only calculates the vector once, between one disk and another. It needs to loop through all the disks to ensure that none overlap.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 122
Reputation: slate is an unknown quantity at this point 
Solved Threads: 30
slate slate is offline Offline
Junior Poster

Re: Python speed issue.

 
1
  #5
Mar 10th, 2009
Maybe I was not clear enough.

instead of this:
  1. for disk in alldisks:
  2. distance=mag((vector(pnewx,pnewy))-disk.pos)

use this
  1. v=vector(pnewx,pnewy)
  2. for disk in alldisks:
  3. distance=mag(v-disk.pos)
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 44
Reputation: breatheasier is an unknown quantity at this point 
Solved Threads: 2
breatheasier's Avatar
breatheasier breatheasier is offline Offline
Light Poster

Re: Python speed issue.

 
0
  #6
Mar 10th, 2009
ah of course!
Also I'm happy to run the program without the graphical parts,
But how would I create an object(disk) with the attributes in the above program myself?
I'm still a beginner.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 122
Reputation: slate is an unknown quantity at this point 
Solved Threads: 30
slate slate is offline Offline
Junior Poster

Re: Python speed issue.

 
0
  #7
Mar 10th, 2009
If you ask, how to get rid of the visual module...
Well, I don't know.

Maybe numpy has some modules, which are modelling 2D objects.

Maybe the visual module can be told, not to display visuals
Try: scene.hide()
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 44
Reputation: breatheasier is an unknown quantity at this point 
Solved Threads: 2
breatheasier's Avatar
breatheasier breatheasier is offline Offline
Light Poster

Re: Python speed issue.

 
0
  #8
Mar 10th, 2009
That's great! Shaved another 10 seconds off my benchmark time.
Many thanks
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 122
Reputation: slate is an unknown quantity at this point 
Solved Threads: 30
slate slate is offline Offline
Junior Poster

Re: Python speed issue.

 
0
  #9
Mar 10th, 2009
Some other improvements:
  1. while (nstep<5000):####HOW MANY STEPS?####
  2. nstep+=1
  3. p = choice(alldisks) #CHOOSE A PARTICLE,P, AT RANDOM FROM THE ARRAY
  4. pnewx=p.x + 0.9*(random()-0.5) #GENERATE A STEP IN X CHANGE COEFFICIENT OF RANDOM TO GET 30% ACCEPTANCE
  5. pnewy=p.y + 0.9*(random()-0.5) #GENERATE A STEP IN Y
  6.  
  7. ######PERIODIC BOUNDARY CONDITIONS############
  8. if pnewx > L: #IF X GOES BEYOND THE RIGHT WALL
  9. pnewx-= L #MOVE X POSITION TO OTHER SIDE
  10. if pnewx < 0: #IF X FALLS BELOW THE LEFT WALL
  11. pnewx += L #MOVE X POSITION TO OTHER SIDE
  12. if pnewy > L: #IF Y IS ABOVE BOX
  13. pnewy-= L #MOVE TO THE BOTTOM
  14. if pnewy < 0: #IF Y BELOW
  15. pnewy+=L #TO TOP
  16. #################################################
  17.  
  18. rejected = False
  19. v=vector(pnewx,pnewy)
  20. for disk in alldisks:
  21. distance=mag(v-disk.pos)#WORK OUT THE DISTANCE BETWEEN THE NEW POSITION FOR THE DISK AND ANY OTHER DISK
  22. if (distance <= 2.0 * r) and p is not disk: #if p overlaps any other disk
  23. rejected = True
  24. reject+=1 # COUNT HOW MANY TIMES THE STEP IS REJECTED
  25. break
  26.  
  27. if not rejected:
  28. p.x=pnewx #
  29. p.y=pnewy #ACCEPT THE STEPS AS THE NEW POSITION FOR THE PARTICLE, P
  30.  
  31. if (nstep % 1800) == 0: #DUMP AFTER SO MANY STEPS
  32. x_list = [disk.x for disk in alldisks]
  33. y_list = [disk.y for disk in alldisks]
  34. output = open('output''%s' % (nstep), 'w') #open file for outputting
  35. (g, radii, newx, newy) = PairCorrelationFunction_2D(array(x_list), array(y_list), S, rMax, dr) #CALCULATE PAIR CORRELATION FUNCTION FOR EACH DUMP
  36. lg=len(g)
  37. for index in xrange(lg):
  38. output.write( "%s %s\n" % (radii[index], g[index]))
  39. output.close() #close file
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 44
Reputation: breatheasier is an unknown quantity at this point 
Solved Threads: 2
breatheasier's Avatar
breatheasier breatheasier is offline Offline
Light Poster

Re: Python speed issue.

 
0
  #10
Mar 10th, 2009
Thanks, implementing most of that sped things up a little apart from the lines:
  1. x_list = [disk.x for disk in alldisks]
  2. y_list = [disk.y for disk in alldisks]
Changing this seemed to slow it down a couple of seconds.

Here is my full code though now:
  1. from visual import * #
  2.  
  3. from math import sqrt #IMPORT LIBRARIES
  4.  
  5. from random import random, choice #
  6. from numpy import zeros, sqrt, where, pi, average, arange, histogram, array
  7.  
  8. import time
  9.  
  10. start = time.time()
  11.  
  12.  
  13. def PairCorrelationFunction_2D(x,y,S,rMax,dr):
  14. """Compute the two-dimensional pair correlation function, also known
  15. as the radial distribution function, for a set of circular particles
  16. contained in a square region of a plane. This simple function finds
  17. reference particles such that a circle of radius rMax drawn around the
  18. particle will fit entirely within the square, eliminating the need to
  19. compensate for edge effects. If no such particles exist, an error is
  20. returned. Try a smaller rMax...or write some code to handle edge effects! ;)
  21.  
  22. Arguments:
  23. x an array of x positions of centers of particles
  24. y an array of y positions of centers of particles
  25. S length of each side of the square region of the plane
  26. rMax outer diameter of largest annulus
  27. dr increment for increasing radius of annulus
  28.  
  29. Returns a tuple: (g, radii, interior_x, interior_y)
  30. g(r) a numpy array containing the correlation function g(r)
  31. radii a numpy array containing the radii of the
  32. annuli used to compute g(r)
  33. interior_x x coordinates of reference particles
  34. interior_y y coordinates of reference particles
  35. """
  36. from numpy import zeros, sqrt, where, pi, average, arange, histogram
  37. # Number of particles in ring/area of ring/number of reference particles/number density
  38. # area of ring = pi*(r_outer**2 - r_inner**2)
  39.  
  40. # Find particles which are close enough to the box center that a circle of radius
  41. # rMax will not cross any edge of the box
  42.  
  43. bools1 = x>1.1*rMax
  44. bools2 = x<(S-1.1*rMax)
  45. bools3 = y>rMax*1.1
  46. bools4 = y<(S-rMax*1.1)
  47. interior_indices, = where(bools1*bools2*bools3*bools4)
  48. num_interior_particles = len(interior_indices)
  49.  
  50. if num_interior_particles < 1:
  51. raise RuntimeError ("No particles found for which a circle of radius rMax\
  52. will lie entirely within a square of side length S. Decrease rMax\
  53. or increase the size of the square.")
  54.  
  55. edges = arange(0., rMax+1.1*dr, dr)
  56. num_increments = len(edges)-1
  57. g = zeros([num_interior_particles, num_increments])
  58. radii = zeros(num_increments)
  59. numberDensity = len(x)/S**2
  60.  
  61. # Compute pairwise correlation for each interior particle
  62. for p in range(num_interior_particles):
  63. index = interior_indices[p]
  64. d = sqrt((x[index]-x)**2 + (y[index]-y)**2)
  65. d[index] = 2*rMax
  66.  
  67. (result,bins) = histogram(d, bins=edges, normed=False, new=True)
  68. g[p,:] = result/numberDensity
  69.  
  70. # Average g(r) for all interior particles and compute radii
  71. g_average = zeros(num_increments)
  72. for i in range(num_increments):
  73. radii[i] = (edges[i] + edges[i+1])/2.
  74. rOuter = edges[i+1]
  75. rInner = edges[i]
  76. g_average[i] = average(g[:,i])/(pi*(rOuter**2 - rInner**2))
  77.  
  78. return (g_average, radii, x[interior_indices], y[interior_indices])
  79.  
  80. #my code starts here
  81. nlat=20 #HOW MANY DISKS ON THE SIDES OF THE BOX
  82.  
  83. r = 0.5 #DISK RADIUS
  84.  
  85. d = 2.0*r #DIAMETER
  86.  
  87. s = 1.22 #GAPS, ALTER TO CHANGE DENSITY
  88. S = s - 1
  89.  
  90. L = (nlat*d)+(nlat-1)*S #LENGTH OF SIDES OF BOX
  91. rMax = 5
  92. dr=0.1 # Radius for increasing the size of the bins
  93. S = L
  94. area = L**2
  95. pi = 3.141592654
  96. diskarea= pi*(r**2) * nlat**2
  97. density = diskarea/area
  98. cpd = 0.9069
  99. cpdf = (density / cpd)*100
  100.  
  101. print 'density = %f' % (density)
  102. print 'which is %f percent of the closest packed density' % (cpdf)
  103.  
  104.  
  105. scene.hide()
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113. #######CREATE SQUARE ARRAY OF PARTICLES (NLAT BY NLAT)############
  114.  
  115. alldisks = []
  116.  
  117. for x in range(0,nlat):
  118.  
  119. for y in range(0,nlat):
  120.  
  121. disk=sphere(pos=(x*s,y*s), radius = r, color=color.red)
  122.  
  123. alldisks.append(disk)
  124.  
  125. disk.id = (x,y)
  126.  
  127. ####################################################
  128.  
  129.  
  130.  
  131.  
  132.  
  133. nstep=0.0 #SET FIRST STEP = 0
  134.  
  135. reject=0.0 #COUNT THE NUMBER OF TIMES A STEP IS REJECTED
  136.  
  137. maxpos=L #DEFINE BOUNDARY CONDITIONS
  138.  
  139. print 'Running Simulation.....'
  140.  
  141. while (nstep<5000):####HOW MANY STEPS?####
  142.  
  143. nstep+=1
  144.  
  145. p = choice(alldisks) #CHOOSE A PARTICLE,P, AT RANDOM FROM THE ARRAY
  146.  
  147.  
  148.  
  149. pnewx=p.x + 0.9*(random() -0.5) #GENERATE A STEP IN X CHANGE COEFFICIENT OF RANDOM TO GET 30% ACCEPTANCE
  150.  
  151. pnewy=p.y + 0.9*(random() -0.5) #GENERATE A STEP IN Y
  152.  
  153. ######PERIODIC BOUNDARY CONDITIONS############
  154. if pnewx > L: #IF X GOES BEYOND THE RIGHT WALL
  155. pnewx-= L #MOVE X POSITION TO OTHER SIDE
  156. if pnewx < 0: #IF X FALLS BELOW THE LEFT WALL
  157. pnewx += L #MOVE X POSITION TO OTHER SIDE
  158. if pnewy > L: #IF Y IS ABOVE BOX
  159. pnewy-= L #MOVE TO THE BOTTOM
  160. if pnewy < 0: #IF Y BELOW
  161. pnewy+=L #TO TOP
  162. #################################################
  163.  
  164.  
  165.  
  166. rejected = False
  167. v=vector(pnewx,pnewy)
  168.  
  169. for disk in alldisks:
  170.  
  171. distance=mag(v-disk.pos)#WORK OUT THE DISTANCE BETWEEN THE NEW POSITION FOR THE DISK AND ANY OTHER DISK
  172.  
  173. if (distance <= 2.0 * r) and p.id != disk.id: #if p overlaps any other disk
  174.  
  175. rejected = True
  176.  
  177. reject+=1 # COUNT HOW MANY TIMES THE STEP IS REJECTED
  178.  
  179. break
  180.  
  181.  
  182.  
  183. if not rejected:
  184.  
  185. p.x=pnewx #
  186.  
  187. p.y=pnewy #ACCEPT THE STEPS AS THE NEW POSITION FOR THE PARTICLE, P
  188.  
  189. if (nstep % 1800) == 0: #DUMP AFTER SO MANY STEPS
  190. x_list = []
  191. y_list = []
  192. for disk in alldisks:
  193. x_list.append(disk.x)
  194. y_list.append(disk.y)
  195. output = open('output''%s' % (nstep), 'w') #open file for outputting
  196. (g, radii, newx, newy) = PairCorrelationFunction_2D(array(x_list), array(y_list), S, rMax, dr) #CALCULATE PAIR CORRELATION FUNCTION FOR EACH DUMP
  197. lg=len(g)
  198.  
  199. for index in range(lg):
  200. output.write( "%s %s\n" % (radii[index], g[index]))
  201.  
  202. output.close() #close file
  203. continue
  204.  
  205.  
  206. rejection = (reject/nstep) * 100 #percentage of steps rejected
  207. acceptance = 100 - rejection
  208. print 'acceptance rate was %f per cent' % (acceptance)
  209.  
  210. end = time.time()
  211. elapsed= end - start
  212. print "The simulation took", elapsed, "seconds to run"
Last edited by breatheasier; Mar 10th, 2009 at 9:38 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC