SHARE
TWEET

CSH2012 FINAL spring.py

calcpage Jun 21st, 2013 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python
  2. #spring.py    MrG     2013.0531
  3. #copied from http://vpython.erikthompson.com
  4. from __future__ import division
  5. from visual import *
  6.  
  7. print"""
  8. Spring Force:  Hooke's Law
  9. Force = -Spring Constant * Spring Displacement
  10. F = -kx
  11.  
  12. The program models the motion of weight attached to a spring
  13. after the weight is pulled (stretching the spring) and released.
  14. Friction is ignored in this model so once started the system
  15. never stops oscillating back and forth.
  16. """
  17.  
  18. ##########################################################################################
  19. #
  20. # INITIALIZE WINDOW & DECLARATIONS
  21. #
  22. ##########################################################################################
  23.  
  24. scene.range = vector(1,1,1)
  25. scene.center = vector(0,0,0)
  26. scene.width = 800
  27. scene.height = 600
  28.  
  29.  
  30. ##########################################################################################
  31. #
  32. # CREATE SPRING, WEIGHT & LABEL OBJECTS
  33. #
  34. ##########################################################################################
  35.  
  36. relaxedlength = vector(.60,0,0) # length of spring when it isn't stretched or compressed
  37. spring = helix(pos=(-.75,0,0),axis=relaxedlength, radius=.1,coils=8,thickness=.01,color=color.green)
  38. spring.constant = 2 # k
  39.  
  40. weight = box(pos=(0,0,0),size=(.3,.3,.3),color=color.yellow)
  41. weight.mass = 10 # kg
  42. weight.velocity = vector(0,0,0)
  43. weight.acceleration = vector(0,0,0)
  44. weight.force = vector(0,0,0)
  45.  
  46. frictionlessSurface = box(size=(2,.02,.5),pos=(0,-.16,0))
  47. wall = box(size=(.04,.5,.3),pos=(-.77,.1,0),color=color.red)
  48.  
  49. mylabel = label(pos=(0,.4,0))
  50. mylabel.text = "DRAG WEIGHT TO\nSTART"                
  51.  
  52.  
  53. ##########################################################################################
  54. #
  55. # WAIT FOR USER TO DRAG THE WEIGHT
  56. #
  57. ##########################################################################################
  58.  
  59. pick = 0
  60. weightmoved = False
  61. while not weightmoved:
  62.     if scene.mouse.events:
  63.         mouse = scene.mouse.getevent() # obtain drag or drop event
  64.         if mouse.drag and mouse.pick == weight: # if clicked on the weight
  65.             drag_pos = mouse.pickpos # where on the ball the mouse was
  66.             pick = mouse.pick # pick is now the weight object (nonzero)
  67.             scene.cursor.visible = 0 # make cursor invisible
  68.         elif mouse.drop: # released the mouse button at end of drag
  69.             pick = None # end dragging (None is False)
  70.             scene.cursor.visible = 1 # cursor visible again
  71.             weightmoved = True
  72.     if pick:
  73.         new_pos = scene.mouse.project(normal=(0,0,1)) # project onto xy plane
  74.         if new_pos != drag_pos: # if the mouse has moved since last position
  75.             pick.pos.x += new_pos.x - drag_pos.x # offset for where the weight was clicked
  76.             # uncomment next 2 lines to limit range spring can be pulled or compressed
  77.             #if pick.pos.x < -.35: pick.pos.x = -.35
  78.             #if pick.pos.x > .35:  pick.pos.x = .35                
  79.             spring.displacement = pick.pos
  80.             spring.axis = relaxedlength + spring.displacement
  81.             drag_pos = new_pos # update drag position
  82.             message = "Springs behave according to Hooke's Law only if"
  83.             message += "\nthey aren't stretched or compressed too far."
  84.             message += "\nTry no more than about 35 centimeters for realistic results."
  85.             message += "\ndisplacement: %.2f meters" % spring.displacement.x
  86.             mylabel.text = message
  87.  
  88. ##########################################################################################
  89. #
  90. # PULL AND RELEASE THE WEIGHT, THEN GO THRU THE LOOP
  91. #
  92. ##########################################################################################
  93.  
  94. spring.displacement = weight.pos # the weight starts at (0,0,0) and is attached to spring
  95. spring.axis = relaxedlength + spring.displacement
  96.  
  97. finished = False # this will always be false so it's an infinite loop
  98. dt = .01    # seconds
  99. seconds = 0 # total time
  100. while not finished:
  101.     rate(100)
  102.     seconds += dt
  103.    
  104.     # Calculate the spring force using Hooke's Law
  105.     SpringForce = -spring.constant * spring.displacement
  106.  
  107.     # The spring force acts on the weight
  108.     weight.force = SpringForce
  109.  
  110.     # move the weight from the applied force using
  111.     # Newton's 2nd law and the position equations
  112.     weight.acceleration = weight.force/weight.mass
  113.     weight.velocity += weight.acceleration * dt
  114.     weight.pos += weight.velocity + .5 * weight.acceleration * dt**2
  115.  
  116.     # calculate the new spring displacement
  117.     spring.displacement = weight.pos
  118.  
  119.     # update the length of the spring
  120.     spring.axis = relaxedlength + spring.displacement
  121.  
  122.     # message to the user
  123.     message = "VPython Video Tutorial: Spring Force"
  124.     message += "\ndisplacement: %.1f" % spring.displacement.x
  125.     message += "\nvelocity: %.3f" % weight.velocity.x  
  126.     message += "\nacceleration: %.2f" % weight.acceleration.x
  127.     message += "\nforce: % .1f" % weight.force.x
  128.     mylabel.text = message
RAW Paste Data
Pastebin PRO Summer Special!
Get 60% OFF on Pastebin PRO accounts!
Top