A question about the function

Asked by zhao dejin

Hello,
      I try to define a function to caculate the distance of the pheres after they dropped the plank, but there are some error in my code, could someone can help me ?
 Thanks a lot in advance.

  ZDJ
*******code *********
##makeCloud####

sp=pack.SpherePack()
sp.makeCloud((0,0,0.00025),(0.0027,0.01,0.00075),psdSizes=[0.0001, 0.00011, 0.00013, 0.00014, 0.00016, 0.00018,0.0002],psdCumm=[0, 0.1, 0.5, 0.8, 0.9, 0.95, 1],porosity=0.3)
sp.toSimulation(color=(0.8,0.8,0.8),material='steel')

###Function#####

def measureDis():
 for i in sp:
  cz = i.state.pos[2]
  czmax = max(cz)
  czmin = min(cz)
  Dis = czmax - czmin
  if unbalancedForce() < 0.3:
     print 'the distance is:',Dis

*******error in screen *********

AttributeError Traceback (most recent call last)
/usr/bin/yade in <module>()

/usr/bin/yade in measureDis()
     65 def measureDis():
     66 for i in sp:
---> 67 cz = i.state.pos[2]
     68 czmax = max(cz)
     69 czmin = min(cz)

AttributeError: 'tuple' object has no attribute 'state'

But when I change the code of function like this:

###Function#####

def measureDis():
 for i in sp:
  cz = O.bodies(i).state.pos[2]
  czmax = max(cz)
  czmin = min(cz)
  Dis = czmax - czmin
  if unbalancedForce() < 0.3:
     print 'the distance is:',Dis

The error is below
*******error in screen *********

TypeError Traceback (most recent call last)
/usr/bin/yade in <module>()

/usr/bin/yade in measureDis()
     65 def measureDis():
     66 for i in sp:
---> 67 cz = O.bodies(i).state.pos[2]
     68 czmax = max(cz)
     69 czmin = min(cz)

TypeError: 'BodyContainer' object is not callable

What should I do to correct it ? Thanks again.

Question information

Language:
English Edit question
Status:
Solved
For:
Yade Edit question
Assignee:
No assignee Edit question
Solved by:
Jan Stránský
Solved:
Last query:
Last reply:
Revision history for this message
Klaus Thoeni (klaus.thoeni) said :
#1

Hi,

try something like this if you want to loop over all bodies:

for i in O.bodies:
  i.state...

Alternatively, toSimulation is returning a list of ids, so you could also use:

ids = sp.toSimulation(color=(0.8,0.8,0.8),material='steel')

for i in ids:
  O.bodies[i].state...

HTH
Klaus

Revision history for this message
zhao dejin (515jingege) said :
#2

Hi, Klaus
     Thanks for your reply, and I have tried all your advice. but there are some other miatakes. first, I make a plank in the model, and let the sphere fall on the plank and caculate the thickness of the spheres . The first suggestion maybe not achieve it ,even I tried it ,there are some error :

             'float' object is not iterable

 When I tried the second advice ,there are similar mistaks like above.

  Thanks again.
   ZDJ

Revision history for this message
Klaus Thoeni (klaus.thoeni) said :
#3

This is another problem, since cz is a float (cz = O.bodies(i).state.pos[2] gives you one value) and you can't do max(cz) or max(cz). Didn't see this before.

Not sure what you want to calculate but I suggest you do it step by step in the ipython terminal first and check your variables.

HTH
Klaus

Revision history for this message
zhao dejin (515jingege) said :
#4

Thanks a lot Klaus,
    I just want to caculate the distance of the plank and the highest sphere, it means thickness. I will check your variables later and do you have any suggestion to relasize this function?

Best wishes to you

   ZDJ

Revision history for this message
Best Jan Stránský (honzik) said :
#5

Hi,

e.g.:

czmax = max(b.state.pos[2] for b in O.bodies if isinstance(b.shape,Spheres))

or similarly on more lines:
czmax = -1e20
for b in O.bodies:
   if not insinstance(b.shape,Sphere): # process only spheres
      continue
   cz = b.state.pos[2]
   czmax = max(cz,czmax)

cheers
Jan

Revision history for this message
zhao dejin (515jingege) said :
#6

Thanks Jan Stránský, that solved my question.