How Can I Improve My 2D Newtonian Gravity Simulator?

In summary: I might also try to make the program detect when masses collide, so I'll probably start with simple elastic collisions. Currently when two masses approach each other very closely, they both gain extremely high velocities, which makes it difficult to keep every mass in the viewing area.I might also try to make the program detect when masses collide, so I'll probably start with simple elastic collisions. Currently when two masses approach each other very closely, they both gain extremely high velocities, which makes it difficult to keep every mass in the viewing area.I'll probably start with simple elastic collisions
  • #1
Ben S
7
4
I recently put together a simple 2D gravity simulator and I'd like to get some feedback or suggestions you may have for future updates. I'm planning to make a 3D version and improve the animation (framerate, realism, etc).

This first simulation includes the Sun, Mercury, Venus, Earth, and Mars. The initial positions and velocities were taken from NASA's JPL site. Notice how the Earth's orbit on the purple line is ~365 days shown by the time display on the left. Planets are not to scale in this model because they would be too small to see.
beb441d71c06642f27607c82e81c6b0b.gif

The inputs for the above simulation are shown below.
C8efbc2.png

There are actually more inputs than this, but these are the most important ones. Additional inputs include the animation speed, viewing area (whether to include the full trajectory of every mass on the screen or to constantly update the viewing area size as shown in the final gif below), trajectory line visibility, and whether to show actual planet size or scaled up sizes for visibility.This simulation shows the Earth (largest grey circle), the Moon (following the yellow line), and a GSO satellite which orbits at the same rate as the Earth rotates. The simulation period is 7 days. The Earth and Moon are to scale, but the satellite size was increased so that it would be visible.
32607807233066b0e8ecfadcaf6b65d9.gif
This final simulation does not model a real system of planets like the two previous simulations. Instead, it starts with nine masses at equal distances from each other. All masses except the center mass are given an initial velocity in a counterclockwise direction around the center mass. It can produce some interesting patterns.
c366e46ba0d5d84ce8ac72ed95314238.gif
How it works
Variables are defined for the initial position (x and y), initial velocity (x and y), mass, and radius. For each planet, the acceleration vectors due to all other planets are calculated in the x and y directions using Newton's law of universal gravitation. Given a small time step, the change in velocity and position for each planet can be found using kinematic equations. I assumed the acceleration was nearly constant over the small time step. With this, the program outputs the position, velocity, and acceleration of every planet at distinct time steps over some period of time. The program is written in MATLAB.

Future work
  • Improve animation frame rate
  • Convert to 3D
  • Apply modern gravitational theories like general relativity
MATLAB code (check the 'read_me.txt')
https://www.dropbox.com/sh/q7pz43cmpxx9a48/AAA_zboKGLaH3XZ-crl_JRsma?dl=1
 
Last edited:
  • Like
Likes Drakkith
Astronomy news on Phys.org
  • #2
Welcome to PF!

Nice work. Was this a school project?

What have you done to validate your modeling?

Will you post the code?
 
  • #3
jedishrfu said:
Welcome to PF!

Nice work. Was this a school project?

What have you done to validate your modeling?

Will you post the code?

Thanks. I made this simulator for fun.

The only real validation that I've done was checking the orbital periods of the planets after inputting the initial positions and velocities of the planets using data recorded by NASA JPL. Some error is expected because the acceleration is assumed to be constant over each time step, but I might be able to calculate the maximum error in the position of each planet at each time step.

I'll see if I can attach the MATLAB code to the original post.
 
  • #4
I've made a very, very simple orbital simulator in another program, but I'd love to be able to use Matlab to do this (I'm taking a Matlab class right now)!
 
  • Like
Likes Ben S
  • #6
I learned today that the animation runs a lot smoother on my university's lab computers, so I added a function that allows you to set the number of planets in the simulation and it randomly generates initial positions and velocities. The animation ran smoothly with up to 200 planets so I might be able to add some interesting gifs or video soon. I'd like to try something similar to the last gif in the original post but with 100 masses and longer capture time.

I might also try to make the program detect when masses collide, so I'll probably start with simple elastic collisions. Currently when two masses approach each other very closely, they both gain extremely high velocities, which makes it difficult to keep every mass in the viewing area.
 
Last edited:
  • Like
Likes Drakkith
  • #7
Ben S said:
I'll probably start with simple elastic collisions

Inelastic collisions would be much more realistic and easier to implement.

Ben S said:
Currently when two masses approach each other very closely, they both gain extremely high velocities

Do you use an integrator with step size control?
 
  • #8
It's been a few weeks since the last post because I've been busy with school and searching for internships, but I made some progress on collision detecting. Know an internship position I would fit in? Let me know!

DrStupid said:
Inelastic collisions would be much more realistic and easier to implement.
Do you use an integrator with step size control?

I added an option that allows you to set how much kinetic energy is conserved in the collisions, from 0% to 100% conserved. With 0% KE conserved, the collisions are perfectly inelastic and the planets stick together. With 100% KE conserved, the collisions are elastic and the planets bounce off of each other. I used conservation of momentum to calculate the final velocities of each planet after a collision.

I don't use an integrator with step size control but I'll look into it.
 

1. What is a Newtonian Gravity Simulator?

A Newtonian Gravity Simulator is a computer program or tool that uses Newton's law of universal gravitation to calculate and simulate the gravitational forces between objects in a virtual environment.

2. How does a Newtonian Gravity Simulator work?

A Newtonian Gravity Simulator works by inputting the mass and position of objects in a virtual environment and then using Newton's law of universal gravitation to calculate the gravitational forces between them. These forces are then used to update the positions of the objects in the simulation over time.

3. What can a Newtonian Gravity Simulator be used for?

A Newtonian Gravity Simulator can be used for educational purposes to help students understand the concept of gravity and how it affects the motion of objects. It can also be used for scientific research and simulations, such as studying the orbits of planets or the interactions between celestial bodies.

4. Is a Newtonian Gravity Simulator accurate?

A Newtonian Gravity Simulator can provide accurate results as long as the input parameters, such as the masses and positions of objects, are accurate. However, it should be noted that this simulator only takes into account the gravitational forces and does not account for other factors such as air resistance or other forces acting on the objects.

5. Are there any limitations to a Newtonian Gravity Simulator?

Yes, there are limitations to a Newtonian Gravity Simulator. It is a simplified model that only takes into account the gravitational forces between objects. It does not consider other factors such as the shape or composition of objects, or the effects of relativity. It also cannot accurately simulate the behavior of systems with more than two objects.

Similar threads

  • Classical Physics
Replies
7
Views
814
  • Classical Physics
Replies
2
Views
1K
  • Astronomy and Astrophysics
Replies
6
Views
2K
  • Astronomy and Astrophysics
Replies
6
Views
1K
Replies
17
Views
2K
  • Astronomy and Astrophysics
Replies
4
Views
1K
  • Classical Physics
Replies
15
Views
2K
  • Programming and Computer Science
Replies
10
Views
3K
  • Astronomy and Astrophysics
Replies
7
Views
4K
  • Astronomy and Astrophysics
Replies
30
Views
4K
Back
Top