diff --git a/src/amuse_petar/interface.cc b/src/amuse_petar/interface.cc index 2c1ed6796a..96ddf47bea 100644 --- a/src/amuse_petar/interface.cc +++ b/src/amuse_petar/interface.cc @@ -25,6 +25,10 @@ extern "C" { static bool particle_list_change_flag=true; static bool initial_particle_flag=false; + // id of the last particle added by new_particle; it never decreases, so + // the id of a removed particle is not handed out again + static PS::S64 last_particle_id=0; + // common int initialize_code() { @@ -35,6 +39,7 @@ extern "C" { char **argv=NULL; n_particle_in_interrupt_connected_cluster_glb = 0; + last_particle_id = 0; //No second MPI init //ptr->initialFDPS(argc,argv); @@ -138,11 +143,10 @@ extern "C" { ptr->read_data_flag = true; PS::S64 id_offset = ptr->input_parameters.id_offset.value; - PS::S64 n_glb = ptr->stat.n_real_glb; PS::S64 n_loc = ptr->stat.n_real_loc; #ifdef INTERFACE_DEBUG assert(n_loc == ptr->system_soft.getNumberOfParticleLocal()); - assert(n_glb == ptr->system_soft.getNumberOfParticleGlobal()); + assert(ptr->stat.n_real_glb == ptr->system_soft.getNumberOfParticleGlobal()); #endif if(ptr->my_rank==0) { @@ -157,8 +161,10 @@ extern "C" { p.radius = radius; if (ptr->initial_parameters_flag) p.calcRSearch(ptr->input_parameters.dt_soft.value); - p.id = n_glb+1; + // not n_real_glb+1: after a removal that id may still be in use + p.id = last_particle_id+1; if (p.id>=id_offset) return -1; + last_particle_id = p.id; p.group_data.artificial.setParticleTypeToSingle(); p.rank_org = ptr->my_rank; diff --git a/src/amuse_petar/tests/test_petar.py b/src/amuse_petar/tests/test_petar.py index ee4327439e..e4e35995ca 100644 --- a/src/amuse_petar/tests/test_petar.py +++ b/src/amuse_petar/tests/test_petar.py @@ -121,3 +121,36 @@ def test_adding_particles(self): request.wait() self.assertEqual(p.x, particles.x) self.assertEqual(p.vx, particles.vx) + + def test_removed_particle_id_not_reused(self): + # issue #1288: a particle added after a removal took the id of a + # live particle, so two AMUSE keys addressed the same PeTar particle + petar = Petar() + particles = new_plummer_model(10) + particles.mass = numpy.arange(1, 11) / 55.0 | nbody_system.mass + particles.radius = 0 | nbody_system.length + petar.particles.add_particles(particles) + petar.evolve_model(0.01 | nbody_system.time) + + removed = particles[2:3] + petar.particles.remove_particles(removed) + particles.remove_particle(removed[0]) + petar.evolve_model(0.02 | nbody_system.time) + + new = datamodel.Particles(1) + new.mass = 0.5 | nbody_system.mass + new.position = [5, 5, 5] | nbody_system.length + new.velocity = [0, 0, 0] | nbody_system.speed + new.radius = 0 | nbody_system.length + particles.add_particles(new) + petar.particles.add_particles(new) + petar.evolve_model(0.03 | nbody_system.time) + + index = petar.particles.get_all_indices_in_store() + self.assertEqual(len(set(index)), len(index)) + self.assertEqual(petar.get_number_of_particles(), 10) + self.assertAlmostRelativeEqual( + numpy.sort(petar.particles.mass.value_in(nbody_system.mass)), + numpy.sort(particles.mass.value_in(nbody_system.mass)), + ) + petar.stop()