Skip to content

PeTar: do not reuse the id of a removed particle - #1290

Open
KainanP wants to merge 1 commit into
amusecode:mainfrom
KainanP:petar-unique-particle-ids
Open

KainanP wants to merge 1 commit into
amusecode:mainfrom
KainanP:petar-unique-particle-ids

Conversation

@KainanP

@KainanP KainanP commented Nov 29, 2026 •

Copy link
Copy Markdown

Fixes #1288.

new_particle in src/amuse_petar/interface.cc numbered a new particle n_real_glb + 1. Removing a particle lowers n_real_glb, so the next id handed out can belong to a particle that is still live. Two AMUSE indices then point at the same PeTar particle (it is looked up through id_adr_map), and the other particle can't be reached anymore.

New ids now come from a counter that only ever goes up: last_particle_id, kept next to the other interface flags and reset in initialize_code. That means reset() still starts at 1. The existing id_offset check is unchanged. id_adr_map is a std::map, so ids with gaps are fine.

Test. TestPetar.test_removed_particle_id_not_reused adds 10 particles, evolves, removes the third, evolves, adds one more, and evolves again. It checks that the indices are unique, that there are 10 particles, and that the masses match.

  • Before: the indices come back as [1, 2, 4, 5, 6, 7, 8, 9, 10, 10], the old particle 10 reports the new mass, and the test fails.
  • After: it passes.

I built with ./setup install amuse-framework amuse-petar in a conda env using OpenMPI 4.1, the same as the PeTar CI job. pytest tests/test_petar.py -k "not noci" gives 10 passed and 1 skipped. flake8 is clean on the test file. I could not check the multi-worker noci test locally: it did not finish within 400 s. CI skips it as well.

AI use: the fix, the test and this description were written with Claude Code (Claude Opus 5.5), which also reproduced the duplicate ids and checked that the test fails before and passes after.

🤖 Generated with Claude Code

new_particle numbered a new particle n_real_glb+1. After a removal
n_real_glb drops, so that id can still belong to a live particle: two
AMUSE indices then address one PeTar particle, and the other particle
can no longer be reached. Number new particles from a counter that
never decreases (reset in initialize_code).

Fixes amusecode#1288

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Backlog

Development

Successfully merging this pull request may close these issues.

After a star is removed, the next star added to PeTar takes a live star's id

1 participant