Building a Solar System simulation with python

In summary, the student is trying to create a simulation of a solar system, but is having difficulty with the acceleration function.
  • #1
TheJP78
3
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
  • #2
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.
 
  • #3
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: 128

FAQ: Building a Solar System simulation with python

How can I create a realistic model of the solar system using python?

To create a realistic model of the solar system using python, you can start by researching the positions and orbits of the celestial bodies in our solar system. Then, use python's mathematical and graphical libraries to plot and animate the movements of these bodies. You can also incorporate data from NASA or other sources to add more accuracy to your simulation.

What are the necessary tools and libraries for building a solar system simulation with python?

Some essential tools and libraries for building a solar system simulation with python include NumPy for mathematical calculations, Matplotlib for data visualization, and PyGame for creating interactive animations. You may also need to install additional libraries for specific features, such as using NASA's data or incorporating gravity into your simulation.

Can I customize the features and settings of my solar system simulation with python?

Yes, one of the advantages of building a solar system simulation with python is the ability to customize and adjust various features and settings. For example, you can change the scale of the simulation, add or remove planets, adjust the speed of their movements, and change the appearance of the celestial bodies.

Is it possible to add other elements, such as asteroids or comets, to my solar system simulation?

Yes, you can add other elements to your solar system simulation using python. You can either manually add their positions and orbits or use data from NASA or other sources. You can also implement different gravitational forces to simulate the interactions between these elements and the planets in your simulation.

How can I make my solar system simulation more interactive and engaging?

There are several ways to make your solar system simulation more interactive and engaging. You can add user inputs to control the movements and settings of the simulation, add sound effects or music, incorporate real-time data from NASA, or even turn your simulation into a game by adding challenges or objectives for the user to complete.

Similar threads

Replies
2
Views
3K
Replies
24
Views
4K
Replies
2
Views
8K
Replies
4
Views
2K
Replies
7
Views
3K
Replies
18
Views
6K
Replies
3
Views
3K
Replies
1
Views
3K
Back
Top