top of page

Simulating Colliding Galaxy Clusters in 2D with Python

  • Writer: Chaitanya Singh
    Chaitanya Singh
  • Jul 22
  • 3 min read

This demonstration brings two miniature “galaxy clusters” together under gravity, letting you watch how streams and tails of particles form as they collide. Below is a straightforward explanation—with all mathematical relationships written out in plain text—so you can focus on the physics without wrestling with code snippets or special formatting.


1. Starting Configuration

  1. We imagine a square region of size 200 by 200 units.

  2. Two massive cores, each weighing 3,000 mass units, sit at points one quarter and three quarters along the horizontal axis, both centered vertically.

  3. Around each core are 1,000 lightweight particles. Each particle is placed at a random distance between 5 and 20 units from its core, at a random angle between zero and 360 degrees.

  4. To mimic circular motion, each particle’s initial speed is set so that the tangential speed equals the square root of (G times core mass divided by the particle’s distance). Here G, the gravitational constant, is 200.

  5. Finally, both clusters (and their particles) are given a uniform drift of plus or minus 10 units along the horizontal axis so that they head straight toward each other. The cores themselves also carry this drift to ensure a head‑on encounter.


2. Computing Gravitational Pull

Every particle feels a pull from every other particle. For any given particle “i,” we calculate its acceleration by summing over all other particles “j”:

  • Take the vector difference between particle i’s position and particle j’s position.

  • Compute the squared distance, add a small softening constant of 25 (that is, 5 squared) to avoid infinite forces at very close range.

  • The acceleration contribution from particle j is then G times j’s mass, divided by the distance cubed, and multiplied by the vector difference.

  • We sum those contributions over all j not equal to i.

Because the two cores have mass 3,000 and every other particle has mass 1, each interacts appropriately in the same calculation.


3. Advancing Time Smoothly

To move positions and velocities forward in time, we use a two‑step approach that keeps total energy from drifting too much:

  1. Position update

    • New position equals old position plus old velocity times the time step, plus half the old acceleration times the time step squared.

  2. Recompute acceleration at the new positions.

  3. Velocity update

    • New velocity equals old velocity plus half the sum of old and new accelerations, all times the time step.

The time step chosen is 0.01 units. This method strikes a good balance between simplicity and energy conservation during close encounters.


4. Visual Presentation

  • All particles appear as tiny white dots against a black background, simulating stars in deep space.

  • An animation loop advances the system for 500 frames, pausing 20 milliseconds between each. The result is roughly ten seconds of smooth collision animation.


5. What You’ll Observe

  • Tidal Tails and Bridges: Streams of particles peel away from each cluster as they interpenetrate, forming elegant filaments.

  • Slingshot Encounters: Some particles briefly gain extra speed when they pass close to a core or another particle, illustrating chaotic energy exchanges.

  • Final Merger: After most of the drama, the two cores settle into one merged system, surrounded by a diffuse halo of scattered particles.


6. Ideas for Further Exploration

  • Three‑Dimensional Extension: Add a vertical coordinate and see the full volumetric collision.

  • Variable Time Steps: Let the time step shrink automatically during especially close approaches to capture finer details.

  • Smooth Mass Distributions: Replace the point‑mass cores with extended density profiles (for example, Plummer spheres) to model real galaxy clusters more accurately.

  • Hardware Acceleration: Offload the many‑body force calculations to a GPU for simulations with tens of thousands of particles.


Feel free to adjust the gravitational constant, core masses, softening length, particle count, or initial drifts. Each tweak unveils new dynamical behaviors.

Comments


LinkedIn

  • LinkedIn

© 2025 by Chaitanya Singh

bottom of page