Length of O.bodies after erasing a body

Asked by Nishant

Hi,

I am learning Yade and I have an issue with erase function. This erases the body and the corresponding interactions, as discussed here https://answers.launchpad.net/yade/+question/216109

However, the length of bodies remain the same. Sample example below:

O.bodies.append([sphere((0.0,0,0.0),1)])
O.bodies.append([sphere((0.0,0,2.0),1)])
len(O.bodies)

This gives 2 as answer.

But next I want to erase one of them:

O.bodies.erase(1)
len(O.bodies)

This again gives two and counts the non-existent bodies, but should give 1, no?

A way to solve this was described here: https://answers.launchpad.net/yade/+question/258987
But, I am interested in applying a constant strain field to some bodies and looping over len(O.bodies) would create problems.

Would be nice if experts shed some light.
Thanks,

Nishant

Question information

Language:
English Edit question
Status:
Solved
For:
Yade Edit question
Assignee:
No assignee Edit question
Solved by:
Nishant
Solved:
Last query:
Last reply:
Revision history for this message
Jérôme Duriez (jduriez) said :
#1

Hello,

I confirm the behavior you describe and understand your reaction.

As a possible workaround remark before other answers, and because your "looping over len(O.bodies)" made me think you might eventually be just interested in looping over bodies, let me precise that looping directly over bodies with "for b in O.bodies:" (instead "for i in range(len(O.bodies)):", maybe) gracefully skips non-existing bodies.

(And sorry if this does not help, here)

Revision history for this message
Nishant (nishantk) said :
#2

Hi Jérôme,

Thanks for the reply.

I agree with your workaround solution - for b in O.bodies - would solve the problem.

Is there a particular reason that the simulation continues with empty body fields?

Deleting the body (particles or walls), erasing the interactions (Yade does these two) as well as deleting the empty body field
would make easier to simulate/track chain of processes.

For example - initial uniaxial compression with a wall, then erasing the wall (body and interactions) and performing a cone penetration etc.

Thanks

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

Hi Nishant,

> Is there a particular reason that the simulation continues with empty body fields?

There are several reasons, e.g.:
1) performacne. O.bodies is internally std::vector, which is "cheap" for access by index, looping and appending to the end. It is "expensive" for "hard deleting" items from the middle, therefore the deleted item is just set to None/nullptr.
2) as a consequence of current behavior, O.bodies[b.id] is always b, which some YADE internals depends on and which would not be true with "renumbering"

cheers
Jan

Revision history for this message
Nishant (nishantk) said :
#4

Dear Jan and Jérôme,

Your solution and clarification solve my problem.

Thanks,
Nishant