Building a Solar System simulation with python

  • #1
TheJP78
3
0
Homework Statement:
Trying to start.
Relevant Equations:
Basic Verlet (https://en.wikipedia.org/wiki/Verlet_integration#Basic_St%C3%B6rmer%E2%80%93Verlet)

Newton's Gravitational Law
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
 

Answers and Replies

  • #2
berkeman
Mentor
64,432
15,778
Welcome to PF.

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
TheJP78
3
0
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: 34

Suggested for: Building a Solar System simulation with python

  • Last Post
Replies
11
Views
479
Replies
18
Views
230
Replies
1
Views
428
Replies
54
Views
662
  • Last Post
2
Replies
47
Views
1K
  • Last Post
Replies
10
Views
528
Replies
16
Views
184
  • Last Post
Replies
13
Views
549
  • Last Post
Replies
7
Views
356
  • Last Post
Replies
27
Views
723
Top