1

I have written an 2d N-body simulation in Python which allows collisions between the bodies.

A body is modeled as a circle whose area is proportional to its mass. Each time the sim advances by one time-step, I identify which bodies have overlapping radii and I replace them by one particle (respecting conservation of momentum and treating the collision as inelastic).

This worked well for N<100 but for higher N I encountered (not systematically, but depending on the initial conditions of the sim) a weird error: the number of particles N actually increased (and quickly exceeded the maximum integer value Python can handle).

I believe what is occuring is, after the new positions of the bodies have been calculated, it happens that (at least) 4 bodies mutually overlap. Each of the 6 pairs creates a new body and but we also delete the 4 bodies,so N increased by os we have 6-4=2. But the 6 new bodies are spawned in roughly the same positions as the original 4 bodies so now we have even more overlap (N can now increase by potentially (6 choose 2)-6=9 bodies). Hence why N can grow very fast.

How can I avoid this error?

The only thing I can think of is to reduce the radii of the bodies to reduce the change of 4 particles mutually overlapping. But this does not definitively fix the problem.

math_lover
  • 886
  • 7
  • 20
  • 1
    Each time-step, build up a list of overlapping bodies - "overlap groups", if you will. **After** you've generated this list from all the bodies in the simulation, go through it and create a single new body from each set of overlapping bodies, then delete those overlapping ones. Doing it in distinct steps like this should prevent previously calculated overlaps interfering with later calculated overlaps within the same time-step. – hnefatl Sep 23 '17 at 11:19
  • 1
    You need to detect what the *first collision* is and update your simulation up to that point and perform the collision. And repeat. This is an event-driven simulation. When you get that to work, you can think of optimizations but not earlier. – Pierre de Buyl Sep 29 '17 at 13:02
  • @PierredeBuyl : indeed the simulation is event-driven. The issue is I have absolutely no way of telling which collision happens first, because the resolution of the simulation is limited by the size of the time-step. – math_lover Sep 30 '17 at 14:54
  • "collision detection" is used in simulations of hard rods/disks/spheres since some time. For disks with constant velocity, for instance, it is easy to compute the collision time for every pair. I don't have a specific reference and I dont' know of an open-source code doing this, but google for "hard disk" simulation should turn up interesting results. – Pierre de Buyl Sep 30 '17 at 16:49
  • This article https://www.sciencedirect.com/science/article/pii/S0019103599962437 deals with large-scale n-body simulations and is thorougly treated. There is a section on pages 48-49 of the article called "Collision Detection and Resolution" that describes precisely what you're looking for. – Gregor Hartl Watters Oct 30 '22 at 22:45

0 Answers0