A binary star system simulation lets you watch two stars orbit their common centre of mass, and it is one of the most rewarding beginner projects in computational physics. You will build a small program that steps the stars forward in time under Newtonian gravity, then plot their paths to see the familiar figure-eight, ellipse, or chaotic dance that emerges. The physics is simple enough to write in a few dozen lines, but the behaviour is rich enough to keep you experimenting for hours. This guide walks you through the whole process, from setting up your tools to checking that your simulation is physically believable.
What You’ll Need
- A computer with Python installed (any recent version is fine).
- A code editor or a notebook environment such as Jupyter.
- The NumPy library for fast array maths and Matplotlib for plotting.
- Basic familiarity with variables, loops, and functions.
- Optional: a physics textbook or lecture notes on Newtonian gravity and the two-body problem.
Step-by-Step
- Set up your environment. Install Python if you have not already, then install NumPy and Matplotlib using your package manager. In a notebook, run a quick import and print statement to confirm everything works before writing any physics.
- Choose your units. Pick a consistent unit system so the numbers stay manageable. A common choice is to measure mass in solar masses, distance in astronomical units, and time in years, with the gravitational constant set to a convenient value. Alternatively, work in SI units and accept very large and very small numbers. Whichever you choose, write the units down and stick to them.
- Define the initial state. You need the mass, position, and velocity of each star. Start simple: two stars of equal mass placed on opposite sides of the origin, each moving perpendicular to the line joining them with the same speed. This gives a circular orbit and is easy to verify by eye.
- Write the force function. For each star, compute the gravitational force from the other star using Newton’s law of gravitation. The force depends on the product of the masses and the inverse square of the separation. Be careful with the direction: the force on each star points toward the other, so the vector difference matters.
- Choose an integrator. The simplest option is the Euler method, but it drifts badly and can make orbits spiral outward. A better choice is the velocity Verlet or leapfrog integrator, which conserves energy much more reliably for orbital problems. Implement one of these and keep the time step small relative to the orbital period.
- Run the simulation loop. At each step, compute the forces, update the velocities, then update the positions. Store the positions at regular intervals so you can plot the trajectory later. Stop after a fixed number of steps or when the stars have completed several orbits.
- Plot the results. Draw the path of each star on the same axes to see the orbit shape. Also plot the total energy and the distance between the stars over time. If the energy drifts steadily, your time step is too large or your integrator is unsuitable.
- Experiment with parameters. Change the mass ratio, the initial separation, or the speed. Try an elliptical orbit by giving one star a tangential speed different from the circular value. Try a highly unequal mass ratio to see the small star whip around the large one.
- Check conservation laws. The total energy and total momentum should stay nearly constant. The centre of mass should move in a straight line at constant velocity. If any of these fail, revisit your force calculation and integrator.
- Add visual polish. Animate the two stars moving around each other, colour them by mass, and draw a trail behind each one. A simple animation loop with Matplotlib is enough for a clear demonstration.
Common Mistakes
Many beginners use the Euler integrator and then wonder why the orbit slowly spirals outward. The fix is to switch to velocity Verlet or another symplectic method. Another frequent error is forgetting that the force on each star points toward the other, which flips the sign and sends the stars flying apart. A third mistake is using a time step that is too large; even a good integrator will fail if the step is a significant fraction of the orbital period. Finally, mixing unit systems (for example, metres for distance but years for time) produces nonsense numbers that are hard to debug.
Tips
Start with a circular orbit because you can check the result against a simple formula for the orbital speed. Keep your code modular: separate the force calculation, the integrator, and the plotting so you can swap pieces easily. Use dimensionless units to keep numbers near one, which makes the simulation easier to read and less prone to floating-point issues. Save your initial conditions to a file so you can reproduce a run exactly. When you change one parameter, change only that one, so you can see its effect clearly.
Troubleshooting
If the stars fly apart immediately, check the sign of the force and whether your initial velocity is pointing in the right direction. If the orbit decays, reduce the time step or switch to a better integrator. If the plot looks empty, confirm that you are storing positions and that the axes limits cover the orbit. If the animation is slow, reduce the number of frames or plot fewer trail points. If the energy drifts, plot it over time to see whether the drift is gradual (time step too large) or sudden (a bug in the force calculation).
Frequently Asked Questions
Do I need to include the stars’ sizes? No. For orbital dynamics, point masses are an excellent approximation unless the stars are close enough to interact tidally or merge.
Can I simulate more than two stars? Yes, but the motion becomes chaotic and much harder to verify. Start with two and add a third only after your two-body simulation is stable.
What is the best integrator for orbits? Velocity Verlet or leapfrog are popular because they conserve energy well over long runs. The Euler method is simple but unreliable for orbits.
How small should the time step be? A good rule of thumb is to use at least several dozen steps per orbit. If the orbit looks jagged or the energy drifts, halve the step and compare.
Why does my centre of mass move? The total momentum should be zero if you start with equal and opposite momenta. If the centre of mass drifts, your initial velocities do not balance, or there is a sign error in the force.
Can I use real astronomical data? Yes. You can look up the masses and orbital parameters of known binary systems and use them as initial conditions, but remember that real orbits can be eccentric and inclined.