Professional Documents
Culture Documents
/usr/bin/python
# drivenpend.py
# Physics 338: The Damped Driven Pendulum.
# Integrate Newton's laws for the damped, driven pendulum and illustrate
# the results with a simulation and a phase space plot.
# This version includes sliders for controlling the various parameters
# $Id: drivenpend.py,v 1.8 2003/11/26 15:51:36 doughera Exp $
#
-- Andrew Dougherty
from visual.graph import *
from visual.controls import *
#####################################################################
# DISPLAY SETTINGS:
# Set fancy_display to '0' to show only the phase space window. This
# runs faster, but you have to manually stop, edit, and restart the
# program to change the constants. Set it to '1' to show everything.
fancy_display = 1
# Another way to speed up or slow things down is with the rate()
# command in the main loop near the bottom.
# Set these to the Horizontal and vertical resolution of your screen.
hres = 1024
vres = 768
#####################################################################
# Physical Constants, but
omega0 = 1.0
#
Q
= 2.0
a0
= 1.4
#
omega = 0.67 * omega0 #
xk1 = dt * v
vk1 = dt * accel(x, v, t)
xk2 = dt * (v + vk1/2.0)
vk2 = dt * accel(x + xk1/2.0,
xk3 = dt * (v + vk2/2.0)
vk3 = dt * accel(x + xk2/2.0,
xk4 = dt * (v + vk3)
vk4 = dt * accel(x + xk3, v +
xnew = x + (xk1 + 2.0 * xk2 +
vnew = v + (vk1 + 2.0 * vk2 +
return [xnew, vnew]
v + vk1/2.0, t + dt/2)
v + vk2/2.0, t + dt/2)
vk3, t + dt)
2.0 * xk3 + xk4) / 6.0
2.0 * vk3 + vk4) / 6.0
0
0
= 0
= 0
def finishup():
global t
t = T_stop
#################################
# Set up for the various windows.
#################################
###########
# First, the pendulum visualization:
###########
if fancy_display == 1:
window1 = display(title='Chaotic Pendulum',
width= vres/2, height=vres/2, x=0, y=0)
pivot = sphere(display=window1, pos=(0,0,0), radius = 0.01,
color=color.yellow)
# Here's the pendulum.
bob = sphere(display=window1, radius = 0.05, pos=(0,-1,0),
color = color.red)
pendulum = cylinder(display=window1, pos=(0,0,0), axis=bob.pos,
radius=0.005, color=color.cyan)
# Make a small dot to show the forcing
driver = sphere(display=window1, radius = 0.02, pos=(0,-1,0),
color = color.yellow)
print 'The yellow dot indicates the forcing.'
# Finally, an invisible box just to keep Visual from autoscaling
# unintelligently
box(display=window1, pos=(0,0,0), length=2.2, width=0, height=2.2,
color=color.black)
###########
# Second, the phase space plot:
###########
window2 = gdisplay(title="Phase Space Plot", xtitle='position',
ytitle='velocity',width= hres/2, height= vres/2,
x=0, y=vres/2,
xmin = -3.2, xmax = 3.2, ymin = -3.2, ymax = 3.2)
myplot = gdots(display=window2, color=color.cyan)
if fancy_display == 1:
window3 = gdisplay(title="Poincare Section", xtitle='position',
ytitle='velocity',width=hres/2, height=vres/2,
x = hres/2, y = vres/2,
xmin = -3.2, xmax = 3.2, ymin = -3.2, ymax = 3.2)
poincare = gdots(display=window3, color=color.cyan)
# Put a time ticker in the upper-right-hand corner of the graph.
clk = label(pos=(3,2), text='0', display=window2.display)
# Generate an initial plot (This allocates all the points for the plot
# correctly. We'll later just reuse these same points over and over again.)
print "Initializing plot ... "
for n in arange(ntrail):
myplot.plot(pos=(x, v))
if fancy_display == 1:
for n in arange(npoincare):
poincare.plot(pos=(x,v))
print "Done."
##########
# Lastly, the control window. (Coordinates in this window go from
# (-100,-100) to (100,100)
##########
if fancy_display == 1:
ctrl = controls(title="Pendulum Controls", width=vres/2, height = vres/2,
x = hres/2, y = 0)
# Each click on the slider increments the variable by ss.value.
# Step size slider
yctl = -10 # y-coordinate of control slider
# Integrate forward.
n = 0 # Counts which of our 'ntrail' dots to use for the plot
np = 0
print "Done."
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
$Log: drivenpend.py,v $
Revision 1.8 2003/11/26 15:51:36 doughera
Don't make the Poincare section plot window unless fancy_display == 1.
Revision 1.7 2003/11/17 16:39:05 doughera
Changed Poincare section color to cyan -- white was getting lost
on the graph axes.
Revision 1.6 2003/11/17 13:58:26 doughera
Added Poincare section plot.
Made window positioning contingent upon resolution of screen
(but you must set hres and vres manually).
Revision 1.5 2003/11/14 14:08:18 doughera
Add small yellow dot to show amplitude and phase of forcing.
Revision 1.4 2003/11/13 16:51:09 doughera
Fixed initial constants to reasonable values for this assignment.
Added additional comments.
Set rate initially at a value suitable for interpreting the graphs.
#
#
#
#
#
#
#
#
#
#
#
#
#