[IPython-user] ipython + visual

Ryan Krauss ryanlists at gmail.com
Mon Feb 27 18:24:33 CST 2006


Is there a way to use Ipython with the visual module (from Vpython)? 
I need to run several demos using visual and there is a noticable lag
when it has to load scipy everytime (and I would just much prefer to
use Ipython than a regular python shell).

Below is a script that runs fine from the ipython prompt, but when I
close the visual window, Ipython exits.

Thanks,

Ryan

==================================
from visual import *

from scipy import arange,zeros_like, sin, cos, zeros, sqrt
from pylab import plot, figure, cla, show
from scipy.integrate import odeint
import time

tic=time.time

m=5.0
L=2.0
g=9.81

def invpend_rootlocus(s,K):
    return 1.0/3.0*m*L**2*s**2-m*g*L/2.0+K


def rkstep(y1,y2,T,kp=0,kd=0,thetad=0.0):
    tau=kp*(thetad-y1)-kd*y2
    f1 = y2
    f2 = 3*(tau-g*L*m*sin(y1)/2.0)/(L**2*m)
    g11 = T*y2
    g12 = 3*T*(tau-g*L*m*sin(y1)/2.0)/(L**2*m)
    g21 = T*(y2+0.5*g12)
    g22 = 3*T*(tau-g*L*m*sin(y1+0.5*g11)/2.0)/(L**2*m)
    g31 = T*(y2+0.5*g22)
    g32 = 3*T*(tau-g*L*m*sin(y1+0.5*g21)/2.0)/(L**2*m)
    g41 = T*(y2+g32)
    g42 = 3*T*(tau-g*L*m*sin(y1+g31)/2.0)/(L**2*m)
    dy1 = (g41+2*g31+2*g21+g11)/6.0
    dy2 = (g42+2*g32+2*g22+g12)/6.0
    return dy1, dy2

scene.title = 'Pendulum'
scene.height = scene.width = 600

g = 9.8
L1 = 0.5 # physical length of upper assembly; distance between axles
d=0.1
gap=d

\section{Runge-Kutta Derivation}
\begin{maxima}
    \parseopts{lhs='f1(y1,y2)'}
    f1(y1,y2):=y2
\end{maxima}
\begin{maxima}
    \parseopts{lhs='f2(y1,y2)'}
%    f2(y1,y2):=-4*(y1+y1^3)
    f2(y1,y2):=(tau-m*g*L/2*sin(y1))*3/(m*L^2)
\end{maxima}
\begin{maxima}
    \parseopts{lhs='g11'}
    g11(y1,y2):=T*f1(y1,y2)
\end{maxima}
\begin{maxima}
    \parseopts{lhs='g12'}
    g12(y1,y2):=T*f2(y1,y2)
\end{maxima}
\begin{maxima}
    \parseopts{lhs='g21'}
    g21(y1,y2):=T*f1(y1+0.5*g11,y2+0.5*g12)
\end{maxima}
\begin{maxima}
    \parseopts{lhs='g22'}
    g22(y1,y2):=T*f2(y1+0.5*g11,y2+0.5*g12)
\end{maxima}
\begin{maxima}
    \parseopts{lhs='g31'}
    g31(y1,y2):=T*f1(y1+0.5*g21,y2+0.5*g22)
\end{maxima}
\begin{maxima}
    \parseopts{lhs='g32'}
    g32(y1,y2):=T*f2(y1+0.5*g21,y2+0.5*g22)
\end{maxima}
\begin{maxima}
    \parseopts{lhs='g41'}
    g41(y1,y2):=T*f1(y1+g31,y2+g32)
\end{maxima}
\begin{maxima}
    \parseopts{lhs='g42'}
    g42(y1,y2):=T*f2(y1+g31,y2+g32)
\end{maxima}
\begin{maxima}
    \parseopts{lhs='dy1'}
    dy1:1/6*(g11+2*g21+2*g31+g41);
\end{maxima}
\begin{maxima}
    \parseopts{lhs='dy2'}
        dy2:1/6*(g12+2*g22+2*g32+g42);
\end{maxima}
\begin{maxima-noout}
	with_stdout ("test.f", [fortran(apply ("=", ['f1,
f1(y1,y2)])),fortran(apply ("=", ['f2, f2(y1,y2)])),fortran(apply
("=", ['g11, g11(y1,y2)])),fortran(apply ("=", ['g12,
g12(y1,y2)])),fortran(apply ("=", ['g21, g21(y1,y2)])),fortran(apply
("=", ['g22, g22(y1,y2)])),fortran(apply ("=", ['g31,
g31(y1,y2)])),fortran(apply ("=", ['g32, g32(y1,y2)])),fortran(apply
("=", ['g41, g41(y1,y2)])),fortran(apply ("=", ['g42,
g42(y1,y2)])),fortran(apply ("=", ['dy1, dy1])),fortran(apply ("=",
['dy2, dy2]))]);
\end{maxima-noout}
top = vector(0,0,0) # top of inner bar of U-shaped upper assembly
scene.center = top#-(0,L1/3,0)#-vector(0,(L1+L2)/2.,0)

axle = cylinder(pos=top, axis=(0,0,0.1), radius=d/4., color=color.yellow)

frame1 = frame(pos=top)
frame1.axis = (0,0,1)
theta=pi-0.1#0.5
thetadot=0
frame1.rotate(axis=(0,0,1), angle=pi+theta)
bar1 = box(frame=frame1, pos=(0,L1/2,0),axis=(0,L1,0),
length=L1,width=d,height=d, color=color.red)

#pointer = arrow(pos=(1,0,0), axis=(0.2,0,0), shaftwidth=0.05)
#pointer2 = arrow(pos=(0,1,0), axis=(0,0.2,0), shaftwidth=0.05,color=color.red)
#pointer = arrow(frame=frame1,pos=(1,0,0), axis=(0.2,0,0),
shaftwidth=0.05, color=color.green)
#pointer2 = arrow(frame=frame1,pos=(0,1,0), axis=(0,0.2,0),
shaftwidth=0.05,color=color.blue)

scene.autoscale = 0

#dt = 0.001
#dt = 0.01
dt = 0.03
t = 0.
ts=20
tv=arange(0,ts+dt,dt)
thv=zeros_like(tv)
thdotv=zeros_like(tv)
x=0
#theta=pi-0.001#0.5

thv[0]=theta
thdotv[0]=thetadot
#kp=100.0
#kd=30.0
kp=100
kd=0
thetades=pi
while t<ts:
    t1=tic()
    rate(1./dt)
    dy1,dy2=rkstep(theta,thetadot,dt,kp,kd,thetades)
    theta+=dy1
    thetadot+=dy2
    frame1.rotate(axis=(0,0,1), angle=dy1)
    t += dt
    x+= 1
    thv[x]=theta
    thdotv[x]=thetadot
    t4=tic()
#    print('total time='+str(t4-t1))

print('done with while loop')




More information about the IPython-user mailing list