CodeCodeCode

Spring through damping medium

Mar 29th, 2011
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.70 KB | None | 0 0
  1. from __future__ import division
  2. from visual.graph import *
  3.  
  4. scene.y = 400 # moves render box 400 down.
  5.  
  6.  
  7. # Constants
  8. pi = 3.14159
  9. L0 = .4 # equilibrium length
  10. g = 0 # no grav
  11. k = 17.6 # spring const
  12. b = 1.8 # dat const
  13.  
  14. scene.center = vector(0,-.1,0)
  15.  
  16. # Objects
  17. ceiling = box(pos=vector(0,0,0), size=(.3,0.005,0.005))
  18. block=box(pos=vector(0,-0.1,0), size=(.02,0.02,0.02), color=color.yellow)
  19. spring = cylinder(pos=ceiling.pos, axis=block.pos, radius=.005)
  20.  
  21. # Initial values
  22. block.m = 6.8
  23. block.v = vector(0,0,0) #block V_i = 0
  24. block.p = block.m * block.v
  25. block.pos = vector(0,-L0-0.2,0) # initial position 0.05m from equilibrium
  26.  
  27. deltat = .0001
  28. t = 0
  29. W = 0
  30. displacement = 0
  31. Kgraph = gcurve(color=color.cyan)
  32. Ugraph = gcurve(color=color.yellow)
  33. KplusUgraph = gcurve(color=color.red)
  34. Wgraph = gcurve(color=color.green)
  35.  
  36. # Loop for repetitive calculations
  37. scene.autoscale=0
  38. while t < 10:
  39.     Fnet = -(((block.pos-vector(0,-L0,0)) * k)) - ((block.p / block.m) * b) #force spring on block
  40.     displacement = (mag(block.p) / block.m) * deltat
  41.     block.p = block.p + Fnet * deltat # updates the momentum
  42.     block.pos = block.pos + block.p / block.m * deltat # updates the position
  43.     spring.axis = block.pos #updates the spring axis so it stays on the block
  44.     t = t + deltat
  45.     pmag = mag(block.p)
  46.  
  47.     K = (pmag**2)*.5/block.m #kinetic energy ((1/2)*|p|^2) / m = K
  48.     U = ((mag(block.pos)-L0)**2)*.5*k #potential energy (|r| - L0)^2 * (1/2) * k = U
  49.     W = W - displacement*(mag(block.p)/block.m)*b #work dr*(|p|/m)*b (ini)
  50.     Kgraph.plot(pos=(t,K))
  51.     Ugraph.plot(pos=(t,U))
  52.     KplusUgraph.plot(pos=(t,K+U))
  53.     Wgraph.plot(pos=(t,W))
  54.     E = K + U
  55.     #print E,'--------',U
Advertisement
Add Comment
Please, Sign In to add comment