Skip to content

Kimani-Mburu/Three-Body-Problem---Python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Three-Body Problem Simulation Code Explanation

This document explains the Python code for simulating and visualizing the three-body problem using Tkinter and Matplotlib.

Table of Contents

  1. Overview
  2. Code Structure
  3. Class: ThreeBodyProblem
  4. Class: ThreeBodyAnimator
  5. Main Function

Overview

The three-body problem is a classical problem in physics and mathematics that involves predicting the motion of three bodies interacting through gravitational forces. This code simulates the three-body problem and provides a real-time visualization using Tkinter and Matplotlib.

Code Structure

The code is structured into two main classes and a main function:

  1. ThreeBodyProblem: Handles the physics calculations and solving the differential equations.
  2. ThreeBodyAnimator: Manages the visualization and animation using Tkinter and Matplotlib.
  3. main(): Sets up the simulation parameters and runs the program.

Class: ThreeBodyProblem

This class encapsulates the physics of the three-body problem.

Key Methods:

__init__(self, masses, initial_conditions, time_span)

  • Initializes the problem with masses of the bodies, initial conditions, and the time span for simulation.
  • Sets the gravitational constant G to 1.0 for simplicity.

equations_of_motion(self, t, state)

  • Defines the differential equations governing the motion of the three bodies.
  • Calculates the gravitational forces between each pair of bodies.
  • Returns the velocities and accelerations for each body.

solve(self, time_points)

  • Uses scipy.integrate.solve_ivp to solve the differential equations.
  • Returns the solution containing the positions and velocities of the bodies over time.

Class: ThreeBodyAnimator

This class handles the visualization of the simulation using Tkinter and Matplotlib.

Key Methods:

__init__(self, solution)

  • Sets up the Tkinter window and embeds a Matplotlib figure in it.
  • Initializes the plot and canvas for animation.

init_animation(self)

  • Sets up the initial state of the animation plot.
  • Defines the axes limits, labels, and title.

update_animation(self, frame)

  • Updates the plot for each frame of the animation.
  • Draws the trajectories and current positions of the three bodies.
  • Updates the time display.

animate(self)

  • Creates the animation using matplotlib.animation.FuncAnimation.
  • Runs the Tkinter main loop to display the animation.

Main Function

The main() function ties everything together:

  1. Defines the simulation parameters:

    • Masses of the three bodies
    • Initial conditions (positions and velocities)
    • Time span and number of time points
  2. Creates an instance of ThreeBodyProblem and solves the equations.

  3. Creates an instance of ThreeBodyAnimator and starts the animation.

Running the Simulation

To run the simulation:

  1. Ensure all required libraries are installed (numpy, matplotlib, scipy, tkinter).
  2. Run the script in a Python environment.
  3. A Tkinter window will open, displaying the real-time animation of the three-body system.

The animation shows the trajectories of the three bodies and their current positions, with a time display indicating the progression of the simulation.

This code provides a flexible foundation for exploring the three-body problem, allowing for easy modification of initial conditions and masses to observe different behaviors of the system.

About

Three Body Problem

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages