GithubHelp home page GithubHelp logo

solar-system's Introduction

Solar-System

2D Simulation of the Solar System by numerically solving the equations of motion for the planets.

To simulate the planets' motion, we first need to find the equation of motion and solve it. We could directly use the Keplerian solution of the motion but i'd rather use a numerical method and considering the precession of Mercury's perihelion

The equation of motion is given with Newton's second law $\displaystyle \sum_k \vec{F}_k = m \vec{a} \Leftrightarrow \sum_k \vec{F}_k = \frac{GMm}{r^3}\vec{r}$.

In cartesian coordinates, the equation becomes 2 equations :

  • $\displaystyle \frac{dv_x}{dt} = \frac{d^2x}{dt^2} = -\frac{GM}{r^2} \cos(\theta)$ where $\displaystyle \cos(\theta) = \frac{x}{r}$
  • $\displaystyle \frac{dv_y}{dt} = \frac{d^2y}{dt^2} = -\frac{GM}{r^2} \sin(\theta)$ where $\displaystyle \sin(\theta) = \frac{y}{r}$

where $r = \sqrt{x^2 + y^2}$ which is the radial distance between the planet and the star. The final system of equations becomes :

  • $\displaystyle \frac{d^2x}{dt^2} = -GM\frac{x}{(x^2 + y^2)^{3/2}} $
  • $\displaystyle \frac{d^2y}{dt^2} = -GM\frac{y}{(x^2 + y^2)^{3/2}}$

For Mercury, the precession is $\dot{\phi} =$ 531.7 seconds of arc per century $(\approx 1.5\times 10^{-15} rad/s)$, but we'll arbitrary take $\dot{\phi} = 10^{-7} rad/s$ the system of equation becomes :

  • $\displaystyle \frac{dv_x}{dt} = -GM \frac{x}{r^3}$ + $\displaystyle \dot{\phi} \frac{dy}{dt}$
  • $\displaystyle \frac{dv_y}{dt} = -GM \frac{y}{r^3}$ - $\displaystyle \dot{\phi} \frac{dx}{dt}$

I first need to give the differents parameters for each planet (Name, distance from the Sun, ellipticity, color and the size) :

 params = {
'Mercury': (57.91e9, 0.2056, 'gray', 0.13),
'Venus': (108.2e9, 0.0068, 'yellow', 0.5),
'Earth': (1.496e11, 0.0167, 'royalblue', 0.5),
'Mars': (227.9e9, 0.0934, 'red', 0.4),
'Jupiter': (778.3e9, 0.049, 'orange', 1.5),
'Saturn': (1.42e12, 0.056, 'gold', 1.3),
'Uranus': (2.87e12, 0.046, 'lightseagreen', 0.8),
'Neptune': (4.5e12, 0.010, 'blue', 0.8)
}

Once the system of equations is defined, I need to choose the initial conditions for each planet based on the parameters. The initial conditions are determined by the initial distance, which is the semi-major axis $a(1-e)$, and the initial velocity, given by $\displaystyle v = \sqrt{\frac{GM}{a}}$.

initial_conditions = {planet: [a * (1 - e), 0, 0, v(a, planet)] for planet, (a, e, _, _) in params.items()}

Then, we numerically solve the equations of motion for each planet using odeint from the scipy library. We create a large yellow dot representing the Sun and place it at the center of the plot.

The goal is to generate the motion of the planets and plot their trajectories to clearly observe their orbits. We also include a timer in years (based on Earth's orbit). For each time step, we plot the new position of each planet $(x=$ solution[i, 0], $y=$ solution[i, 1] $)$ and their trajectories by keeping a few points plotted during the animation.

def animate(i):
for (planet, solution, line, planet_point, trail) in zip(params, solutions.values(), lines, planets, trails):
    x = solution[i, 0]
    y = solution[i, 1]
    line.set_data(x, y)
    planet_point.set_data(x, y)
    trail.set_data(solution[:i, 0], solution[:i, 1])
sun.set_offsets([0, 0])

# Update time in years
years_elapsed = t_values[i] / (365.25 * 24 * 60 * 60)  # Convert to years
time_text.set_text(f'Time elapsed: {years_elapsed:.2f} years')

return lines + planets + trails + [sun, time_text]

We animate the motion using FuncAnimation from the matplotlib.animation library. Here are screenshots of the simulation

image image image

solar-system's People

Contributors

hugogw avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.