Segmentation fault (core dumped) after erase some bodies

Asked by Leonard on 2019-09-11

Hi,
I'd like to delete particles which has connection to other particles, I read some answers and follows the suggestion in [1].
The outcome is unstable, when I use "example one" to generate particles, Segmentation fault (core dumped) occurs. While when I comment "example one" and use "example two" to generate particles, it works well. Here is the MWE:

from yade import pack, plot

########## example one which leads to Segmentation fault#####
for i in range(10):
    O.bodies.append(sphere((0, 0, i), radius=1, color=[1, 1, 1]))

O.bodies.append(sphere((5,5,5),radius=1,color=[1,1,1]))
######## example two which works well #####
# O.bodies.append(sphere((0,0,0),radius=1,color=[1,1,1]))
# O.bodies.append(sphere((0,1,0),radius=1,color=[1,1,1]))
# O.bodies.append(sphere((0,2,0),radius=1,color=[1,1,1]))
# O.bodies.append(sphere((3,0,0),radius=1,color=[1,1,1]))
# O.bodies.append(sphere((3,1,1),radius=1,color=[1,1,1]))
# O.bodies.append(sphere((5,5,5),radius=1,color=[1,1,1]))

O.engines=[
    ForceResetter(),
    InsertionSortCollider([Bo1_Sphere_Aabb()]),
    InteractionLoop(
        [Ig2_Sphere_Sphere_ScGeom()],
        [Ip2_FrictMat_FrictMat_FrictPhys()],
        [Law2_ScGeom_FrictPhys_CundallStrack()]
    ),
    NewtonIntegrator(damping=0.4),
]

Ids=[]
for i in O.bodies:
    Ids.append(i.id)

O.step()

BodiesToBeDel=[]
def findBodiesToBeDel():
    for i in Ids:
        for j in Ids:
            if i>=j:
                continue
            else:
                if O.interactions.has(i,j):
                    BodiesToBeDel.append(O.bodies[i])
                    BodiesToBeDel.append(O.bodies[j])
findBodiesToBeDel()
def eraseOverlapBodies():
    for i in BodiesToBeDel:
        O.bodies.erase(i.id)

eraseOverlapBodies()

My yade version is 2018.02b on Ubuntu 18.04.
Do you know what is the problem?
many thanks
Leonard
[1]https://answers.launchpad.net/yade/+question/211937

Question information

Language:
English Edit question
Status:
Solved
For:
Yade Edit question
Assignee:
No assignee Edit question
Solved by:
Jan Stránský
Solved:
2019-09-13
Last query:
2019-09-13
Last reply:
2019-09-12
Best Jan Stránský (honzik) said : #1

Hi Leonard,

after findBodiesToBeDel, some bodies BodiesToBeDel multiple times (each body may be associated with multiple interactions).
Then in findBodiesToBeDel, you erase some bodies, but then you try to erase the same ID (which would be OK), but you try to get the id as "i.id", but "i" was already erased and (probably, did not investigated it more) was deleted in C++, ending in segmentation fault.

Try it with set using ids, something like:
###
IdsToBeDel = set()
def findBodiesToBeDel():
    for i in Ids:
        for j in Ids:
            if i>=j:
                continue
            else:
                if O.interactions.has(i,j):
                    IdsToBeDel.add(i)
                    IdsToBeDel.add(j)
findBodiesToBeDel()
def eraseOverlapBodies():
    for i in IdsToBeDel:
        O.bodies.erase(i)
eraseOverlapBodies()
###

cheers
Jan

Leonard (z2521899293) said : #2

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