← Back to team overview

yade-users team mailing list archive

Re: [Question #701028]: Triaxial test cylindrical membrane created away from the pack

 

Question #701028 on Yade changed:
https://answers.launchpad.net/yade/+question/701028

    Status: Open => Answered

Jan Stránský proposed the following answer:
> e = (top[0].state.displ()[2] - bot[0].state.displ()[2]) / (height-rParticle*2*bcCoeff)
> IndexError: list index out of range
> Why does this error occur? I don't understand meaning of some of these errors.

list index out of range means that you are trying to access elements of the list, which are not there.
Try in python:
###
l = ["a","b","c"]
l[0] # "a"
l[1] # "b"
l[2] # "c"
l[3] # IndexError
###

This basic python exceptions are pretty self-explanatory. At the error line, you have a few index access:
top[0]
bot[0]
(...)[2]
And one of them is the source of the error.

The easiest way how to debug the problem is to:
- print the variables you try access
- split the line into more pieces, then Python tells you exactly where the problem is, e.g.:
###
# original line
# e = (top[0].state.displ()[2] - bot[0].state.displ()[2]) / (height-rParticle*2*bcCoeff)
# new code
top0 = top[0] # if the error is here, you know exactly what is going on and it is clear that "top" is empty list
top0displ = top0.state.displ()
top0displ2 = top0displ[2]
# same for bot
numerator = top0displ2 - bot0displ2
denominator = height-rParticle*2*bcCoeff
e = numerator / denominator
###

> bot = [O.bodies[s] for s in spheres if O.bodies[s].state.pos[2]<rParticle*bcCoeff]
> top = [O.bodies[s] for s in spheres if O.bodies[s].state.pos[2]>height-rParticle*bcCoeff]

E.g. I suspect these lines creates empty lists. Then if you do top[0],
you get IndexError

> This is the updated code:

please try to make it MWE [2]
W = working. Python needs indentation.

>> You can have some checking beforehand or try-except construction to
prevent the error.
> Also can you please explain this soln that you gave for the previous reply.

see [3]

Cheers
Jan

[2] https://www.yade-dem.org/wiki/Howtoask
[3] https://docs.python.org/3/tutorial/errors.html

-- 
You received this question notification because your team yade-users is
an answer contact for Yade.