Hi,
I'm making a program in VPython to simulate the motion of particles. It's fairly simple in theory but I'm having some problems with my lists of particles.
What I'm trying to do is make sure that the particles do not overlap, and regenerate a position if they do overlap. When I try to define the distance between to particles, I get the error "TypeError:list indices must be integers". I've heavily commented the code in hope that you can understand what is going on. Any help would be greatly appreciated, I'm not an experienced programmer.
from visual import * #
from math import * #IMPORT LIBRARIES
from random import * #
r = 0.5 #DISK RADIUS
d = 2*r #DIAMETER
s = 1.7 #GAPS
L = (20*2*r*s) #LENGTH OF SIDES OF BOX
scene.autoscale=0 #KEEP CAMERA STILL
scene = display(title='Monte Carlo Simulation of a Hard Disk Fluid',width=600, height=600,center=(0,0,0), background=(1,1,1))
#################################DRAW THE BOX#########################################
side=L/2
thk=0.3
s2 = 2*side - thk
s3 = 2*side + thk
wallR = box (pos=vector( side, 0, 0), length=thk, height=s2, width=d, color = color.yellow)
wallL = box (pos=vector(-side, 0, 0), length=thk, height=s2, width=d, color = color.yellow)
wallB = box (pos=vector(0, -side, 0), length=s3, height=thk, width=d, color = color.yellow)
wallT = box (pos=vector(0, side, 0), length=s3, height=thk, width=d, color = color.yellow)
###################################################################################
#######CREATE SQUARE ARRAY OF PARTICLES############
square1_list = []
for x in arange(-10,10):
for y in arange (-10, 10):
disk=sphere(pos=(x*s,y*s), radius = r, color=(1,0,1))
square1_list.append(disk)
###################################################
#######CREATE ANOTHER SQUARE ARRAY OF PARTICLES############
square2_list = []
for x in arange(-9.5,9.5):
for y in arange(-9.5,9.5):
disk=sphere(pos=(x*s,y*s), radius = r, colour=(0,1,1))
square2_list.append(disk)
#########################################################
array=[] #
array.extend(square1_list) #COMBINE ARRAYS
array.extend(square2_list) #
for disk in array: #
disk.color=color.red #COLOUR DISKS TO CHECK THE ARRAYS HAVE BEEN COMBINES
nstep=0 #SET FIRST STEP = 0
maxpos=L/2-disk.radius #DEFINE BOUNDARY CONDITIONS
while (nstep<10): #CHOOSE NUMBER OF STEPS
for disk in array:
disk.x=disk.x + (random() -0.5) #MOVE DISK RANDOMLY IN X a
disk.y=disk.y + (random() -0.5) #MOVE DISK RANDOMLY IN Y b
##########CHECK FOR COLLISIONS WITH WALLS############
if disk.pos.x > maxpos: #IF X GOES BEYOND THE RIGHT WALL
disk.pos.x=disk.pos.x - L #MOVE X POSITION TO OTHER SIDE
if disk.pos.x < -maxpos: #IF X FALLS BELOW THE LEFT WALL
disk.pos.x=disk.pos.x + L #MOVE X POSITION TO OTHER SIDE
if disk.pos.y > maxpos: #IF Y IS ABOVE BOX
disk.pos.y=disk.pos.y - L #MOVE TO THE BOTTOM
if disk.pos.y < -maxpos: #IF Y BELOW
disk.pos.y=disk.pos.y + L #TO TOP
#####################################################
#struggling with the algorithm for rejecting a step if a disk overlaps another
#what im trying to do:
for j in array:
distance=mag(array[disk].pos-array[j].pos)
#if distance between 2 disks<disks diameter
#reject the movement of the disks made earlier(marked a and b), go back to the configuration before that step.
#still count this as a step.
#else continue
nstep=nstep+1 #ADD 1 STEP