Python Building a Solar System simulation with python

AI Thread Summary
The discussion centers on creating a solar system simulation in Python 3.9 using the Basic Verlet method. The user is tasked with dividing the simulation into three parts: Sun-Earth, Earth-Moon, and the other planets. Key steps include defining an acceleration function that calculates gravitational forces based on positions and velocities, and implementing the Verlet integration method for orbital calculations. The user has made initial attempts at coding the acceleration function, which computes the gravitational pull of the Sun on the Earth, and is preparing to extend this to include the Moon and other planets. The user is encouraged to share their code for further assistance and is working under a deadline of August 16. The simulation does not require animation but needs to plot the elliptical orbits of the planets and the Moon around the Earth.
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: 170
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

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