help in Vpython needed...
paiseh
bakutek at hotmail.com
Mon Mar 8 23:24:50 EST 2004
Below is a source code for Vpython. I need to do some modification
and sincerely hope you all can help....
Now, there are 2 compartments (upper and lower) and there are 5
green balls in random motion.
I need to modify such that the 5 balls move only in the upper
compartment. Also, i need another 20 balls that only move in the
lower compartment.
Pls help......
With regards
Wee Nam
#########################################
# Import the library(s)
#########################################
from visual import *
from random import uniform
from visual.graph import *
##########################################
# Create Wall(s)
##########################################
thk = 0.3
side = 4.0
s2 = 2*side - thk
s3 = 2*side + thk
wallR = box (pos=vector( side, 0, 0), length=thk, height=s2,
width=s3, color = color.red)
wallL = box (pos=vector(-side, 0, 0), length=thk, height=s2,
width=s3, color = color.red)
wallB = box (pos=vector(0, -side, 0), length=s3, height=thk,
width=s3, color = color.blue)
wallT = box (pos=vector(0, side, 0), length=s3, height=thk,
width=s3, color = color.blue)
wallBK = box(pos=vector(0, 0, -side), length=s2, height=s2,
width=thk, color = (0.7,0.7,0.7))
wallM = box (pos=vector(0, -0.25*side, 0), length=s3, height=thk,
width=s3, color = color.blue)
##########################################
# Create Ball(s)
##########################################
no_particles=5
ball_radius=0.5
maxpos=side-.5*thk-ball_radius
maxv=1.0
D2=(2*ball_radius)**2
ball_list=[]
p_list=[]
pos_list=[]
for i in arange(no_particles):
ball = sphere(color = color.green, radius=ball_radius)
p = maxv*vector(uniform(-1,1),uniform(-1,1),uniform(-1,1))
p_list.append(p)
position=maxv*vector(uniform(-1,1),uniform(-1,1),uniform(-1,1))
pos_list.append(position)
ball.pos=vector(position)
ball_list.append(ball)
p_array=array(p_list)
pos_array=array(pos_list)
###########################################
# Setup Histogram Plot
###########################################
graphwindow=gdisplay(xtitle='v_x',ytitle='N',ymax=no_particles/2)
velocity_dist=ghistogram(bins=arange
(0,2*maxv,maxv/5),accumulate=1,average=1)
expected_distribution=gcurve(color=color.green)
for vx in arange(0,2*maxv,maxv/20):
expected_distribution.plot(pos = (vx,.27*no_particles*exp(-
vx**2/maxv**2*3/2)))
##########################################
# Time loop for moving Ball(s)
###########################################
ltriang=fromfunction(lambda i,j: less_equal(j,i),
[no_particles,no_particles])
dt = 0.05
while 1:
rate(100)
######################################
# Move Particles
######################################
pos_array=pos_array+p_array*dt
######################################
# Check wall collisions
######################################
putmask(p_array,greater(pos_array,maxpos),-p_array)
putmask(pos_array,greater(pos_array,maxpos),2*maxpos-pos_array)
putmask(p_array,less_equal(pos_array,-maxpos),-p_array)
putmask(pos_array,less_equal(pos_array,-maxpos),-2*maxpos-
pos_array)
######################################
# Check particle collisions
######################################
separation=pos_array-pos_array[:,NewAxis]
sepmag2=add.reduce(separation*separation,-1)
putmask(sepmag2,ltriang,4*D2)
hit=less_equal(sepmag2,D2)
hit_list=sort(nonzero(hit.flat))
for ij in hit_list:
i, j = divmod(ij,no_particles)
sepmag=sqrt(sepmag2[i,j])
direction=separation[i,j]/sepmag
pi=dot(p_array[i],direction)
pj=dot(p_array[j],direction)
exchange=pj-pi
p_array[i]=p_array[i]+exchange*direction
p_array[j]=p_array[j]-exchange*direction
overlap=2*ball_radius-sepmag
pos_array[i]=pos_array[i]-overlap*direction
pos_array[j]=pos_array[j]+overlap*direction
######################################
# Update position and velocity
# of balls for plotting
######################################
for i in arange(len(ball_list)):
ball_list[i].pos=vector(pos_array[i])
ball_list[i].velocity=vector(p_array[i])
#######################################
# Plot the x velocity histogram
#######################################
velocity_dist.plot(data=abs(p_array[:,0]))
More information about the Python-list
mailing list