damped driven pend

Download Damped Driven Pend

If you can't read please download the document

Upload: walter-jerezano

Post on 16-Aug-2015

215 views

Category:

Documents


1 download

DESCRIPTION

Programacion

TRANSCRIPT

#!/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 Doughertyfrom 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 = 1024vres = 768###################################################################### Physical Constants, but in convenient computational formomega0 = 1.0 # Natural angular frequency of undamped oscillatorQ= 2.0a0 = 1.4 # Convenient shorthand for external forcing F0/momega= 0.67 * omega0# Driving frequency# Derived physical constantsT0 = 2 * pi / omega0 # Natural period of undamped oscillatorgamma =omega0 / Q # Damping coefficientT_driving = 2 * pi / omega# Period of the external forcing.# Initial conditionst = 0.0x = 0.0v = 0.0# Time step.Adjust as needed to achieve the desired precision# and smoothness of plot.dt = 0.01 #smaller than about T0/200, but rounded for nice display.T_transient = 0 # 10.0 * Q * T0 # Adjust this to skip plotting# initial transient.T_stop = T_transient + 5000 * T0ntrail = 4000# Length of trail of 'dots' to leave behind.npoincare = 40# Acceleration function.def accel(x, v, t):"accel(x,v,t): acceleration for the damped driven pendulum."a = -omega0**2 * sin(x) - gamma * v + a0 * cos(omega * t)return a# 4th Order Runge-Kuttadef rk4(x, v, t, dt):"rk4(x,v,t,dt): Advance one time step using 4th-order Runge Kutta."xk1 = dt * vvk1 = dt * accel(x, v, t)xk2 = dt * (v + vk1/2.0)vk2 = dt * accel(x + xk1/2.0, v + vk1/2.0, t + dt/2)xk3 = dt * (v + vk2/2.0)vk3 = dt * accel(x + xk2/2.0, v + vk2/2.0, t + dt/2)xk4 = dt * (v + vk3)vk4 = dt * accel(x + xk3, v + vk3, t + dt)xnew = x + (xk1 + 2.0 * xk2 + 2.0 * xk3 + xk4) / 6.0vnew = v + (vk1 + 2.0 * vk2 + 2.0 * vk3 + vk4) / 6.0return [xnew, vnew]# Call-back routines -- these are called when the various buttons in# the control window are pressed.def sets_step(ss):s_stepbox.text = 'step size: %f' % ss.valuedef increments_step(ss, amt):ss.value = ss.value * amtsets_step(ss)def setdrivingforce(sf):global a0a0 = sf.valuea0box.text = 'a0: %f' % a0def incrementdrivingforce(sf, fstep):sf.value = sf.value + fstepsetdrivingforce(sf)def setdrivingfrequency(sw):global omegaglobal T_drivingomega = sw.valueT_driving = 2 * pi / omegaomegabox.text = 'omega: %f' % omegadef incrementdrivingfrequency(sw, fstep):sw.value = sw.value + fstepsetdrivingfrequency(sw)def cleartrail():for n in arange(ntrail):myplot.dots[n].pos[0] = 0myplot.dots[n].pos[1] = 0for n in arange(npoincare):poincare.dots[n].pos[0] = 0poincare.dots[n].pos[1] = 0def finishup():global tt = 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 forcingdriver = 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# unintelligentlybox(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 slideryctl = -10# y-coordinate of control sliderss = slider(min = 0, max = 1, pos=(-50, yctl), length = 100,axis=(1,0,0), action=lambda: sets_step(ss))# ... and up and down buttonsbssup = button(pos=(60, yctl), text = '>', width = 10, height = 10,action = lambda: increments_step(ss, 10))bssdown = button(pos=(-60, yctl), text = '', width = 10, height = 10,action = lambda: incrementdrivingforce(sf, ss.value))bsfdown = button(pos=(-60, yctl), text = '', width = 10, height = 10,action = lambda: incrementdrivingfrequency(sw, ss.value))bswdown = button(pos=(-60, yctl), text = '