Advertisement
Guest User

Untitled

a guest
Sep 17th, 2014
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. from __future__ import division
  2. from visual import *
  3. scene.width = 1024
  4. scene.height = 768
  5. scene.x = scene.y = 0
  6.  
  7. # constants
  8. oofpez = 9e9
  9. L = 9
  10. Q = 3e-10
  11. N = 20
  12. scalefactor = 1.0
  13. dz = L/N
  14. dQ = Q/N
  15. R = .5
  16.  
  17. # list of charges
  18. list_of_charges = []
  19. z = -L/2 + dz/2
  20. while z < L/2:
  21. list_of_charges.append(sphere(pos=(0,0,z), radius=.1, color=color.green))
  22. z = z+dz
  23. print(len(list_of_charges)) ## print number of items in list
  24.  
  25. ## uncomment these two lines to make program wait for a mouse click:
  26. ##print 'Click anywhere to continue'
  27. ##scene.mouse.getclick() ## wait for a mouse click, then continue
  28.  
  29. # list of arrows; tail of each arrow is at an observation location
  30. dzz = ((10/8)*L)/10
  31. list_of_arrows = []
  32. for z in arange(-5*L/8, 5*L/8+.001, dzz):
  33. for theta in arange(0,2*pi, pi/4):
  34. list_of_arrows.append(arrow(pos=(R*cos(theta), R*sin(theta),z),
  35. axis=(0,1,0)))
  36.  
  37. ## uncomment these two lines to make program wait for a mouse click:
  38. ##print('Click anywhere to continue')
  39. ##scene.mouse.getclick() ## wait for a mouse click, then continue
  40.  
  41. ###############################################################
  42. ## calculation of field: you need to complete this block of statements
  43.  
  44. for thisarrow in (list_of_arrows): ## take arrows one at a time
  45. rate(10)
  46.  
  47. # you need to add statements to do the following things
  48. # (a) set Enet (the field at tail of thisarrow) to a zero vector here
  49. Enet=vector(0,0,0)
  50. # (b) add indented statement to loop over list of charges
  51. for thischarge in (list_of_charges):
  52.  
  53.  
  54. # (c) add necessary program statements to calculate dE, the
  55. # field due to the current charge, at the observation location (tail of arrow)
  56. r=thisarrow.pos-thischarge.pos
  57. rmag=mag(r)
  58. rhat=r/rmag
  59. dE=oofpez*dQ*rhat/rmag**2
  60. # (d) add dE to Enet at this observation location
  61. Enet=Enet+dE
  62. # (e) after this code is added, uncomment the following two statements to display Enet
  63. thisarrow.axis = Enet*scalefactor
  64. thisarrow.color = color.magenta
  65.  
  66. list_of_narrows = []
  67. for y in arange(R/2, 5.1*R/2, R/2):
  68.  
  69. list_of_narrows.append(arrow(pos=(0, y,0),
  70. axis=(0,1,0)))
  71.  
  72. ## uncomment these two lines to make program wait for a mouse click:
  73. ##print('Click anywhere to continue')
  74. ##scene.mouse.getclick() ## wait for a mouse click, then continue
  75.  
  76. ###############################################################
  77. ## calculation of field: you need to complete this block of statements
  78.  
  79. for thisnarrow in (list_of_narrows): ## take arrows one at a time
  80. rate(10)
  81.  
  82. # you need to add statements to do the following things
  83. # (a) set Enet (the field at tail of thisarrow) to a zero vector here
  84. Enet=vector(0,0,0)
  85. # (b) add indented statement to loop over list of charges
  86. for thischarge in (list_of_charges):
  87.  
  88.  
  89. # (c) add necessary program statements to calculate dE, the
  90. # field due to the current charge, at the observation location (tail of arrow)
  91. r=thisnarrow.pos-thischarge.pos
  92. rmag=mag(r)
  93. rhat=r/rmag
  94. dE=oofpez*dQ*rhat/rmag**2
  95. # (d) add dE to Enet at this observation location
  96. Enet=Enet+dE
  97. # (e) after this code is added, uncomment the following two statements to display Enet
  98. thisnarrow.axis = Enet*scalefactor
  99. thisnarrow.color = color.magenta
  100. print(Enet, thisnarrow.pos)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement