calculate mass content of a group of particles through something like label

Asked by Leonard on 2019-08-14

Hi,
I'd like to calculate the mass content of a group of spheres, as far as I know, we can obtain a mass of one sphere by:
O.bodies[id].state.mass
I'd like to ask that if we can obtain the mass of a group of spheres like: appending some spheres with a same label, like:
O.append(sphere((x1,y1,z1),radius=radius,label="label1"))
O.append(sphere((x2,y2,z2),radius=radius,label="label1"))
O.append(sphere((x3,y3,z3),radius=radius,label="label2"))
O.append(sphere((x4,y4,z4),radius=radius,label="label2"))
then we can obtain the total mass of spheres with label1, and label2 respectively?
I have tried in Yade, it seems that we can not give each sphere a label, is there any way that we can classify the spheres to different groups so that we can just loop in one group of spheres to calculate the mass?
Thanks in advance!

Question information

Language:
English Edit question
Status:
Solved
For:
Yade Edit question
Assignee:
No assignee Edit question
Solved by:
gaoxuesong
Solved:
2019-08-15
Last query:
2019-08-15
Last reply:
2019-08-15
Best gaoxuesong (260582472-9) said : #1

Hi,

    The possible way i think is that using different lists to save the sphere's id of different groups and do loop in each list.
## group 1 ##
list1 = []
list1.append(O.append(sphere((x1,y1,z1),radius=radius1)))
...
## group 2 ##
list2 = []
list2.append(O.append(sphere((x2,y2,z2),radius=radius2)))
...
## calculate the mass of different groups ##
masstotal1 = 0
for each in list1:
    masstotal1 + = O.bodies[each].state.mass
masstotal2 = 0
for each in list2:
    masstotal2 + = O.bodies[each].state.mass

## ends ##

Cheers,
Xuesong

Leonard (z2521899293) said : #2

Hi xuesong,
Many thanks for your solution.
Your approach works very well, while one point should be noted that:
--->>The possible way i think is that using different lists to save the sphere's id of different groups and do loop in each list.
--->>## group 1 ##
--->>list1 = []
--->>list1.append(O.append(sphere((x1,y1,z1),radius=radius1)))
In this way, the list1 stores not the id of sphere, but the sphere body, so for the next code:
--->>## calculate the mass of different groups ##
--->>masstotal1 = 0
--->>for each in list1:
    --->>masstotal1 + = O.bodies[each].state.mass
May should be:
masstotal1 = 0
for each in list1:
    masstotal1 + = each.state.mass

Thanks again for your help, I just mention it more clearly for other beginners.

Leonard (z2521899293) said : #3

Thanks gaoxuesong, that solved my question.

Leonard (z2521899293) said : #4

Hi xuesong,
I am sorry that I make a wrong answer in #2.
You are right.
-->>list1.append(O.bodies.append(sphere((x1,y1,z1),radius=radius1)))
In this way, the list1 stores the id of the sphere.
The reason why I make the mistake is I did like this:
-->>s1=O.bodies.append(sphere((x1,y1,z1),radius=radius1))
-->>list1=[]
-->>list1.append(s1)
then in this way, the list1 stores the body of sphere.
Thank you again.
Cheers,
Leonard

gaoxuesong (260582472-9) said : #5

You are welcome. However, in the way as you said,
###
-->>s1=O.bodies.append(sphere((x1,y1,z1),radius=radius1))
-->>list1=[]
-->>list1.append(s1)
###
list1 also stores the id of a body. In this way, it stores the body.
-->>s1=O.bodies.append(sphere((x1,y1,z1),radius=radius1))
-->>list1=[]
-->>list1.append(O.bodies[s1])
###
one thing to be addressed is that the return value of the O.bodies.append() is the body's id not the body itself.
 best,
Xuesong