Building a Solar System simulation with python

Click For Summary
SUMMARY

The discussion centers on creating a solar system simulation using Python 3.9, specifically employing the Basic Verlet method for calculating celestial mechanics. The user is tasked with implementing a function called acceleration(r, v) to determine the gravitational forces acting on the Earth and Moon relative to the Sun. Key components include defining the acceleration due to gravity with the formula -G * M_sun * (x/(mod_r**3)) and using Verlet integration for position updates. The user seeks assistance in correctly defining the acceleration function and implementing the simulation for multiple celestial bodies.

PREREQUISITES
  • Python 3.9 programming
  • Basic understanding of gravitational physics
  • Familiarity with Verlet integration method
  • Experience with NumPy for numerical calculations
NEXT STEPS
  • Implement the Verlet integration method for the Earth-Sun system
  • Extend the simulation to include the Moon's orbit around the Earth
  • Research gravitational force calculations for multiple bodies
  • Explore visualization techniques for plotting orbits using Matplotlib
USEFUL FOR

Students in physics or computer science, aspiring game developers, and anyone interested in simulating celestial mechanics using Python.

TheJP78
Messages
3
Reaction score
0
Hey'all. First of all, I'm not fluent in English, so forgive me for the spelling mistakes. So, I'm trying to make a simulation of a solar system using python 3.9. It's not complicated, but my teacher wants me to do it using the Basic Verlet method, and that's what is bugging me. He told me do do divide the code in three steps: Sun-Earth, Earth-Moon, and then the rest of the planets. He also told me the basics to initialize the code:
1. creat a function called acceleration(r, v) that contains the position r(x0, y0, x1, y1, ...) and the velocity v(x0, y0, x1, y1, ...);
2. use the acc(r, v) funtion and the verlet method to calculate the position of the Earth relatively to the sun (the sun will be located in r=v=0);
3. the same case as the above, but now for the earth-moon and the other planets.

He wrote a short instruction for me (linked bellow). My problem is to define the acceleration function. I found a similar code on stack overflow (https://stackoverflow.com/questions...in-python-resulting-in-particles-running-away) and tried to modify it, but didn't work.

If anyone could help me I will be forever grateful.
20220712_105837.jpg
 
Technology news on Phys.org
Welcome to PF.

TheJP78 said:
tried to modify it, but didn't work.

It would be best if you showed us what you have tried, and describe it in your own words. Posting a whiteboard picture of your instructor's hints helps a little, but we need to see your own work before we can offer tutorial help (that's in the PF rules).

So please post your code that you tried (using code tags) and describe what you think the code should be doing. To use code tags, put [ code ] at the beginining and [ /code ] at the end (but leave out the spaces).

Thanks.
 
Hi, sorry for the delay. I didn't get much done since my code depends of the acc function. I've spoke to my professor again and he told me how to build the function for the earth-sun system (to introduce the other planets and the moon i will have to add an extra factor G*M*((ri - rj)/mod_(ri-rj)**3) to the accelerations in x and y, and other x, y components do the r vector (x1, y1, x2, y2, etc.)):

Code:
import numpy as np
from math import sqrt

def acc(r):
    x0 = r[0] #initial
    y0 = r[1] #conditions
    mod_r0 = sqrt(x0**2 + y0**2) #module of the r vector
    a0x = -G * M_sul * (x0/(mod_r0**3)) #acceleration due to the x component
    a0y = -G * M_sul * (y0/(mod_r0**3)) #acceleration due to the y component
    acctot = np.array(a0x, a0y) #acceleration vector
    return acctot

M_sun is the mass of the sun and G is 6,67408.10^-11 N.kg²/m².

The acceleration will be due to the sun, but when I introduce the moon I will have to consider the force that the moon exerts on the Earth as well.

Now I have to implement the verlet integration method to calculate the orbit of the Earth around the sun and, later on, the other planets orbits and the moon orbit. I think will be something like this:

Code:
r[i] = r[0] + v[0]*dt + a[r[0]]*(dt**2)*0.5 #earth-sun
r[i+1] = 2*r[i] - r[i-1] + a[r[0]]*(dt**2) #other bodies

My professor said the implementation is almost the same as the Euler and Runge-Kutta methods, so I will try to do that way.

Right now, this is all I have done because I have other subjects that are demanding my time. But I will work on it on the weekends to see what more I can do (I have till August 16 to deliver this simulation).

Other thing to add is that I don't need to do an animation of the 8 planets orbiting the sun. It can be just a plot with the eliptics of each one plus the moon orbiting around the earth.

P.S: He also gives me some more instructions, explaining me how to build the function using an example of the pendulum.
 

Attachments

  • 20220714_105206.jpg
    20220714_105206.jpg
    40.6 KB · Views: 182

Similar threads

  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 24 ·
Replies
24
Views
5K
  • · Replies 2 ·
Replies
2
Views
8K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 18 ·
Replies
18
Views
6K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
18
Views
2K
  • · Replies 10 ·
Replies
10
Views
4K