python - "IndexError: list index out of range" For planetary simulator -
traceback (most recent call last): file "n:\starry\planetary orbits ver 8.py", line 121 updatedisplay(pos) # updates 3d displays position of masses file "n:\starry\planetary orbits ver 8.py", line 100, in updatedisplay stars[i].pos = pos[i] indexerror: list index out of range
i've been getting error , can't find out why, i've checked see numpy arrays , lists have values in them, shown below in python shell:
number of stars: 2 please enter x position of star: 0 please enter y position of star: 0 please enter z position of star: 0 please enter radius of star: 10 please enter mass of star: 5000000 please enter x speed of star: 0 please enter y speed of star: 0 please enter z speed of star: 0 please enter x position of star: 100 please enter y position of star: 0 please enter z position of star: 0 please enter radius of star: 10 please enter mass of star: 5000000 please enter x speed of star: 0 please enter y speed of star: 0 please enter z speed of star: 0 [(0, 0, 0), (100, 0, 0)] # printed positionlist # [(0, 0, 0), (0, 0, 0)] # printed momentumlist # [5000000, 5000000] # printed masslist # [10, 10] # printed radiuslist # [[ 0 0 0] # printed pos array# [100 0 0]] [[0 0 0] # printed momentum array # [0 0 0]] [[5000000] # printed masses array # [5000000]] [10 10] # printed radii array #
these values show items going lists , arrays, surely code select item inside array?
for in range(nstars): updatedisplay(pos)
here full program reference:
nstars = int(input("number of stars: ")) # change have more or fewer stars g = 6.7e-11 # universal gravitational constant # typical values msun = 2e30 rsun = 2e9 vsun = 0.8*sqrt(g*msun/rsun) dt = 10.0 #reprensents change in time nsteps = 0 time = clock() nhits = 0 #~~~~~~~~~~~~~~~~~~~~~~~~~~~ global data ~~~~~~~~~~~~~~~~~~~~~~~~~~~# stars = [] colors = [color.red, color.green, color.blue, color.yellow, color.cyan, color.magenta] positionlist = [] momentumlist = [] masslist = [] radiuslist = [] pos = none radii = none masses = none f = none momentum = none #~~~~~~~~~~~~~~~~~~~~~~~~~~~ global data ~~~~~~~~~~~~~~~~~~~~~~~~~~~# def calculations(speedx, speedy, speedz, x, y, z, mass, radius): px = mass*(speedx) py = mass*(speedy) pz = mass*(speedz) positionlist.append((x,y,z)) momentumlist.append((px,py,pz)) masslist.append(mass) radiuslist.append(radius) def starcreation(stars,x,y,z,radius): stars = stars+[sphere(pos=(x,y,z), radius=radius, color=colors[i % 6], make_trail=true, interval=10)] def datacollection(): x = input("please enter x position of star: ") y = input("please enter y position of star: ") z = input("please enter z position of star: ") radius = input("please enter radius of star: ") starcreation(stars,x,y,z,radius) mass = input("please enter mass of star: ") speedx = input("please enter x speed of star: ") speedy = input("please enter y speed of star: ") speedz = input("please enter z speed of star: ") calculations(speedx, speedy, speedz, x, y, z, mass, radius) def momenta(momentum, masses): vcm = sum(momentum)/sum(masses) # velocity of center of mass momentum = momentum-masses*vcm # make total initial momentum equal 0 def listtoarray(positionlist, momentumlist, masslist, radiuslist): global pos pos = array(positionlist) global momentum momentum = array(momentumlist) global masses masses = array(masslist) masses.shape = (nstars,1) # numeric python: (1 nstars) vs. (nstars 1) global radii radii = array(radiuslist) momenta(momentum, masses) def forces(pos, radii, masses): # compute forces on stars r = pos-pos[:,newaxis] # pairs of star-to-star vectors (where r relative position vector n in range(nstars): r[n,n] = 1e6 # otherwise self-forces infinite rmag = sqrt(sum(square(r),-1)) # star-to-star scalar distances hit = less_equal(rmag,radii+radii[:,newaxis])-identity(nstars) hitlist = sort(nonzero(hit.flat)[0]).tolist() # 1,2 encoded 1*nstars+2 global f f = g*masses*masses[:,newaxis]*r/rmag[:,:,newaxis]**3 # force pairs def selfforces(f): f[n,n] = 0 def updatemomentapositions(momentum, pos, masses): momentum = momentum+sum(f,1)*dt pos = pos+(momentum/masses)*dt def updatedisplay(pos): stars[i].pos = pos[i] #~~~~~~~~~~~~~~~~~~~~~~~~~ actual proagram ~~~~~~~~~~~~~~~~~~~~~~~~~# in range(nstars): datacollection() listtoarray(positionlist, momentumlist, masslist, radiuslist) print (positionlist) print (momentumlist) print (masslist) print (radiuslist) print (pos) print (momentum) print (masses) print (radii) while true: rate(100) # no more 100 loops per second on fast computers forces(pos, radii, masses)# computes forces between masses n in range(nstars): selfforces(f) # no self forces updatemomentapositions(momentum, pos, masses) # updates momentum , positions of stars in range(nstars): updatedisplay(pos) # updates 3d displays position of masses
your issue never add stars stars
when call starcreation
function. reassign local stars
list shadows global stars
line :
stars = stars+[sphere(pos=(x,y,z), radius=radius, color=colors[i % 6], make_trail=true, interval=10)]
you have 2 solutions issue :
- you can modify global
stars
list inplace via eitherstars+=[sphere(...)]
orstars.append(sphere(...))
- or tell python stars global variable , shouldn't shadowed, :
.
def starcreation(stars,x,y,z,radius): global stars stars = stars+[sphere(pos=(x,y,z), radius=radius, color=colors[i % 6], make_trail=true, interval=10)]
edit : mistake assumed didn't exist , sorry.
Comments
Post a Comment