Advertisement
calcpage

CSH2012 FINAL tension.py

Jun 21st, 2013
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.44 KB | None | 0 0
  1. #!/usr/bin/python
  2. #tension.py    MrG     2013.0531
  3. #copied from http://vpython.erikthompson.com
  4. from __future__ import division # so 3/2 equals 1.5 instead of 1
  5. from visual import *
  6.  
  7. print"""
  8. A look at tension, static and kinetic friction.
  9.  
  10. This is a model of an experiment in which a book is placed on a flat table.
  11. The book has a string tied to it with the other hand hanging off the side
  12. of the table.  We will be adding weights to the string hanging off the edge
  13. until the book starts sliding.
  14.  
  15. In this experiment the program will already know the coefficient of static
  16. friction and the coefficient of kinetic friction.  When the user has added
  17. enough mass to overcome static friction the book will slide accordingly.
  18.  
  19. The use of a pulley (not shown) helps negate friction on the string as well
  20. as change the direction of force transmitted thru the string.  We are
  21. approximating the the mass and friction of the string and pulley to be zero.
  22. """
  23.  
  24. # SET UP THE WINDOW
  25. scene.width = 800
  26. scene.height = 600
  27.  
  28. scene.autoscale = 1 # let vpython pick the range this time
  29. scene.center = (1.5,.50,0) # center up and a little to the right of the origin
  30.  
  31. ##########################################################################################
  32. #
  33. # CREATE THE OBJECTS ON OUR INITIAL SCENE
  34. #
  35. ##########################################################################################
  36.  
  37. # CREATE A TABLE TO WORK ON
  38. table = frame() # we want to make a table object that is built from a table top and leg.
  39. tabletop = box(frame=table,pos=(0,1,0),size=(3,.2,1))
  40. tableleg = cylinder(frame=table,pos=(0,0,0),axis=(0,1,0),radius=.3) # big round leg in the middle              
  41.  
  42. # CREATE THE GROUND
  43. ground = box(size=(4,.2,1),pos=(0,-.1,0),color=color.yellow)
  44.  
  45. # CREATE OUR BOOK TO SLIDE
  46. book = box(pos=(0,1.15,0),size=(.3,.1,.2), color=color.green)
  47. book.mass = .1        # .1 kg
  48. book.velocity = vector(0,0,0)
  49. book.acceleration = vector(0,0,0)
  50. book.force = vector(0,0,0)
  51. book.moving = False     # it starts out at rest on the table
  52.  
  53. # ADD A STARTING MASS TO THE END OF THE STRING CREATED BELOW
  54. weight = box(pos=(1.55,.5,0),size=(.1,.1,.1),color=color.green)
  55. weight.mass = .02      # 20 grams = .020 kg
  56. # we want the size of the mass to be roughly proportional to its mass
  57. weight.size = vector(.1,weight.mass * 3,.1)  
  58. # we want all of the increased size to be placed on top istead of
  59. # half on top and half on the bottom so we adjust the position
  60. weight.pos.y = .5 + (weight.size.y / 2)    
  61. weight.velocity = vector(0,0,0)
  62. weight.acceleration = vector(0,0,0)
  63. weight.force = vector(0,0,0)
  64.  
  65. # TIE A STRING TO THE BOOK AND HANG THE STRING OVER THE TABLE TIED TO THE WEIGHT
  66. string = curve(pos=[book.pos, (1.55,1.1,0)], color=color.red ) # string goes from book to slightly over edge
  67. string.append( weight.pos ) # string goes straight down
  68.  
  69. # CREATE MASS OBJECTS TO CLICK AND LABEL DESCRIPTION
  70. mass1 = sphere(pos=(3,2.3,.2),radius=.1,color=color.red)
  71. mass1.mass = .001        # kg
  72. message = "Click to add 1.0 grams."
  73. mass1Label = label(pos=mass1.pos, text=message,yoffset=20)
  74.  
  75. mass2 = sphere(pos=(3,1.5,.2),radius=.13,color=color.blue)
  76. mass2.mass = .005         # kg
  77. message = "Click to add 5.0 grams."
  78. mass2Label = label(pos=mass2.pos, text=message,yoffset=20)
  79.  
  80. # CREATE A LABEL FOR MESSAGES TO USER
  81. mylabel = label(pos=(3.5,-1,0))
  82.  
  83. ##########################################################################################
  84. #
  85. # CREATE VARIABLES FOR THE FORCES ACTING IN OUR PROGRAM
  86. #
  87. ##########################################################################################
  88.  
  89. accelerationGravity = vector(0.0,-9.8,0.0)
  90.  
  91. gravityForceOnWeight = weight.mass * accelerationGravity
  92. weight.force = gravityForceOnWeight
  93.  
  94. gravityForceOnBook = book.mass * accelerationGravity
  95. normalForceOnBook = -1 * gravityForceOnBook
  96.  
  97. # The string pulls with the same force on both the book and weight.
  98. # If the weight isn't moving, then the string tension (force) is equal to the
  99. # force pulling the weight down (mass * gravity)
  100. string.forceTensionMagnitude = mag(weight.force)
  101.  
  102. coefficientOfKineticFriction = .20      # constant for calculating kinetic friction between book and table
  103. coefficientOfStaticFriction = .60       # constant for calculating static friction between book and table
  104.  
  105. directionBookIsBeingPulled = vector(1,0,0)
  106.  
  107. # FORMULA: magnitude of frictional force = coefficient * normal force
  108. #          direction of frictional force = the direction opposing motion
  109.  
  110. # static frictional force acts on book when it isn't moving
  111. # this is a maxiumum value.  It is the force that needs to be overcome
  112. forceStaticFrictionOnBook = coefficientOfStaticFriction * mag(normalForceOnBook) * directionBookIsBeingPulled * -1
  113. # kinetic frictional force acts on book when it is moving
  114. forceKineticFrictionOnBook = coefficientOfKineticFriction * mag(normalForceOnBook) * directionBookIsBeingPulled * -1
  115.  
  116. ##########################################################################################
  117. #
  118. # INITITIALIZE SOME MORE VARIABLES AND GO INTO OUR LOOP
  119. #
  120. ##########################################################################################
  121.  
  122. book.startXpos = book.pos.x     # just to keep track of some stats
  123. weight.startYpos = weight.pos.y # just to keep track of some stats
  124. distanceBookTraveled = 0        # just to keep track of some stats
  125. distanceWeightTraveled = 0      # just to keep track of some stats
  126. dt = .01    # seconds
  127. Finished = False
  128. while not Finished:
  129.     rate(100) # go thru loop no more than 100 times/sec
  130.     if scene.mouse.events: # detect mouse events
  131.         mouse = scene.mouse.getclick()
  132.         if mouse.pick == mass1 or mouse.pick==mass2: # was a mass clicked?
  133.             weight.mass += mouse.pick.mass  # add clicked mass to total mass
  134.             weight.size = vector(.1,weight.mass * 3,.1) # same code as above
  135.             weight.pos.y = .5 + (weight.size.y / 2)     # same code as above      
  136.  
  137.     # do we have enough force to move the book?
  138.     if not book.moving:
  139.         string.forceTensionMagnitude = mag(weight.mass * accelerationGravity)
  140.         if string.forceTensionMagnitude > mag(forceStaticFrictionOnBook):
  141.             book.moving = True # we have enough pulling force to overcome static friction
  142.  
  143.     if book.moving: # if so, we need to move everything
  144.         # how fast are the weight and mass accelerating
  145.         if weight.pos.y - weight.size.y/2 > 0: # if the weight hasn't landed
  146.             # Let's write the equations that we know
  147.             # 1. force on the weight = force pulling down - force pulling up
  148.             # 2. force on the book = force pulling right - force holding back
  149.             # otherwise written:
  150.             # 1. mass of the weight * acceleration = mass of the weight * gravity - tension
  151.             # 2. mass of the book * acceleration = tension - frictional force on book
  152.             # There are two unknowns (acceleration and tension)
  153.             # Using a bunch of algebra we get the acceleration
  154.  
  155.             # from equation 2: tension = mass of the book * acceleration + frictional force on book
  156.             # substitute that into equation 1:
  157.             # mass of the weight * gravity - (mass of the book * acceleration + frictional force on book) = mass of the weight * acceleration
  158.             # use algebra to isolate acceleration variable
  159.             # mass of the weight * acceleration + mass of the book * acceleration = mass of the weight * gravity - frictional force on book
  160.             # acceleration (mass of the weight + mass of the book) = mass of the weight * gravity - frictional force on book
  161.             # acceleration = (mass of the weight * gravity - frictional force on book) / (mass of the weight + mass of the book)
  162.             accelerationMagnitude = (weight.mass * 9.8 - mag(forceKineticFrictionOnBook)) / (weight.mass + book.mass)
  163.             weight.acceleration = accelerationMagnitude * vector(0,-1,0)
  164.             book.acceleration = accelerationMagnitude * vector(1,0,0)            
  165.         else: # weight has landed
  166.             book.acceleration = forceKineticFrictionOnBook / book.mass
  167.             string.forceTensionMagnitude = 0
  168.            
  169.         # move the book and weight according to their acceleration
  170.         book.velocity += book.acceleration * dt
  171.         book.pos += (book.velocity * dt + .5 * book.acceleration * dt**2)
  172.         distanceBookTraveled = book.pos.x - book.startXpos
  173.  
  174.         if weight.pos.y - weight.size.y/2 > 0: # if weight hasn't landed
  175.             weight.velocity += weight.acceleration * dt
  176.             weight.pos += (weight.velocity * dt + .5 * weight.acceleration * dt**2)
  177.             distanceWeightTraveled = weight.startYpos - weight.pos.y
  178.        
  179.         # keep string attached to end of book and weight
  180.         string.pos = [ book.pos, (1.55,1.1,0), weight.pos ]
  181.  
  182.         if book.velocity.x < 0:
  183.             Finished = True
  184.  
  185.     message = "String Tension: " + str(string.forceTensionMagnitude)
  186.     message += "\nMax Static Frictional Force: " + str(forceStaticFrictionOnBook)
  187.     message += "\nMax Kinetic Frictional Force: " + str(forceKineticFrictionOnBook)
  188.     message += "\nTotal mass on string: " + str(weight.mass*1000) + " grams."
  189.     message += "\nAcceleration of Book: " + str(book.acceleration)
  190.     message += "\nAcceleration of Weight: " + str(weight.acceleration)
  191.     message += "\nDistance Book Traveled: %.2f meters" % distanceBookTraveled
  192.     message += "\nDistance Weight Traveled: %.2f meters" % distanceWeightTraveled
  193.  
  194.     mylabel.text =  message
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement