Skip to content

Commit 0c6a4b1

Browse files
committed
added comparison of Newton and Yukawa potential
1 parent ba0ab6e commit 0c6a4b1

File tree

8 files changed

+397
-16
lines changed

8 files changed

+397
-16
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ To get started with these simulations:
2828
python solar_system.py
2929
```
3030

31+
## Yukawa Potential
32+
33+
Yukawa potential (a potential which has an exponential decay in addition to the standard Newtonian gravitational force) is also simulated between the Sun and the Earth, but the change in orbit is minimal (no change up to 7 significant digits).
34+
3135
## Contributing
3236

3337
Contributions to the Orbits Simulation project are welcome. Whether it's through submitting bug reports, proposing new features, or contributing to the code, your help is appreciated. For major changes, please open an issue first to discuss what you would like to change.

body_data_extraction.py

100644100755
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
#!/usr/bin/env python3
2+
'''
3+
/***************************/
4+
/* body_data_extraction.py */
5+
/* Version 1.0 */
6+
/* 2024/08/06 */
7+
/***************************/
8+
'''
19
import json
210
import re
311
import requests

data_input/bodies_2Dplane.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
"position": [ 1.496e11, 0.0, 0.0 ],
3838
"velocity": [ 0.0, 2.978e4, 0.0 ]
3939
},
40-
4140
"mars": {
4241
"label": "Mars",
4342
"mass": 6.4171e23,

data_input/bodies_3D.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262
1.184224839788195
6363
]
6464
},
65-
6665
"mars": {
6766
"label": "Mars",
6867
"mass": 6.417099999999999e+23,

data_input/bodies_yukawa.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"bodies" : ["sun", "earth"],
3+
"reference_date": "2024-01-01 0:00:00",
4+
"sun": {
5+
"label": "Sun",
6+
"mass": 1.989e+30,
7+
"scale": 1.5,
8+
"color": [1, 1, 0],
9+
"radius": 6.96342e+8,
10+
"position": [ 0.0, 0.0, 0.0 ],
11+
"velocity": [ 0.0, 0.0, 0.0 ]
12+
},
13+
"earth": {
14+
"label": "Earth",
15+
"mass": 5.97219e+24,
16+
"scale": 2.7,
17+
"color": [0, 0, 1],
18+
"radius": 6371010.0,
19+
"position": [ -24810993259.6539, 144994861273.6719, -8215203.670851886 ],
20+
"velocity": [ -29841.463655186788, -5126.262286859616, 1.184224839788195 ]
21+
}
22+
}

solar_system.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
#!/usr/bin/env python3
2+
'''
3+
/**********************/
4+
/* solar_system.py */
5+
/* Version 1.0 */
6+
/* 2024/08/03 */
7+
/**********************/
8+
'''
19
import argparse
210
import json
311
import matplotlib.pyplot as plt
@@ -18,6 +26,7 @@
1826
animation_format='mp4', # animation format (mp4 or gif)
1927
fps=30, # frame per second
2028
useRK45=True, # use RK45
29+
step=86400, # integration step 1 day
2130
verbose=True, # verbose output
2231
use_yukawa=False, # use Yukawa potential
2332
yukawa_coeff=1.5e15, # Yukawa potential coefficient
@@ -28,8 +37,7 @@
2837

2938

3039
cst = SimpleNamespace(
31-
G=6.67430e-11,
32-
day_in_seconds=86400
40+
G=6.67430e-11
3341
)
3442

3543

@@ -67,7 +75,7 @@ def runge_kutta4(f, y0, t0, tf, dt, args=()):
6775

6876

6977
def gravitational_force_newton(m1, m2, r_ij):
70-
"""Compute the gravitational force between two masses using Newtwon
78+
"""Compute the gravitational force between two masses using Newton
7179
gravitational law."""
7280
r = np.linalg.norm(r_ij)
7381
return cst.G * m1 * m2 * r_ij / r**3
@@ -77,8 +85,8 @@ def gravitational_force_yukawa(m1, m2, r_ij):
7785
"""Compute the gravitational force between two masses using Yukawa
7886
gravitational law."""
7987
r = np.linalg.norm(r_ij)
80-
return cst.G * m1 * m2 * r_ij / r**3 * (1 + r_ij / cfg.yukawa_coeff) * \
81-
np.exp(-r_ij / cfg.yukawa_coeff)
88+
return cst.G * m1 * m2 * r_ij / r**3 * (1 + r / cfg.yukawa_coeff) * \
89+
np.exp(-r / cfg.yukawa_coeff)
8290

8391

8492
gravitational_force = gravitational_force_yukawa if cfg.use_yukawa else \
@@ -113,8 +121,8 @@ class SolarSystemSimulation:
113121
def __init__(self, outfile):
114122
self._bodies = []
115123
self._num_frames = 30
116-
self.outfile = outfile
117-
self.perc = None
124+
self._outfile = outfile
125+
self._perc = None
118126

119127
@property
120128
def bodies(self):
@@ -132,7 +140,7 @@ def create_bodies(self):
132140
position=b.position, velocity=b.velocity))
133141

134142
def compute(self):
135-
t_eval = np.arange(0, cfg.time_span[1], cst.day_in_seconds)
143+
t_eval = np.arange(0, cfg.time_span[1], cfg.step)
136144
# extract masses
137145
masses = np.array([body.mass for body in self._bodies])
138146
# flatten initial conditions using the `position` #
@@ -146,15 +154,15 @@ def compute(self):
146154
solution = solve_ivp(n_body_problem, cfg.time_span,
147155
initial_conditions, args=(masses,),
148156
method='RK45', t_eval=t_eval,
149-
max_step=cst.day_in_seconds)
157+
max_step=cfg.step)
150158
self._positions = solution.y[:num_bodies * 3].reshape(
151159
(num_bodies, 3, -1))
152160
self._velocities = solution.y[num_bodies * 3:].reshape(
153161
(num_bodies, 3, -1))
154162
else:
155163
t, y = runge_kutta4(n_body_problem, initial_conditions,
156164
cfg.time_span[0], cfg.time_span[1],
157-
cst.day_in_seconds, args=(masses,))
165+
cfg.step, args=(masses,))
158166
self._positions = y[:num_bodies * 3].reshape((num_bodies, 3, -1))
159167
self._velocities = y[num_bodies * 3:].reshape((num_bodies, 3, -1))
160168

@@ -202,7 +210,7 @@ def format_func(value, tick_number):
202210
plt.tight_layout()
203211

204212
def animate(self):
205-
self.perc = 0
213+
self._perc = 0
206214
if cfg.save_anim:
207215
fig = plt.figure(figsize=(12, 8), dpi=300)
208216
else:
@@ -245,8 +253,8 @@ def animate(self):
245253
def animate_frame(frame):
246254
if cfg.verbose:
247255
perc = (frame + 1) / self._num_frames * 100
248-
if perc // 10 > self.perc // 10:
249-
self.perc = perc
256+
if perc // 10 > self._perc // 10:
257+
self._perc = perc
250258
print(f"completed {int(perc)}% of the animation")
251259
for i in range(len(self._bodies)):
252260
# Extract the positions for body i at the current frame
@@ -264,7 +272,7 @@ def animate_frame(frame):
264272
fig, animate_frame, frames=self._num_frames,
265273
interval=1000 / cfg.fps, blit=True)
266274
if cfg.save_anim:
267-
base, ext = self.outfile.rsplit('.', 1)
275+
base, ext = self._outfile.rsplit('.', 1)
268276
animation_format = cfg.animation_format
269277
outfile_a = f"{base}.{animation_format}"
270278
if animation_format == 'mp4':

solar_system_body.py

100644100755
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
#!/usr/bin/env python3
2+
'''
3+
/************************/
4+
/* solar_system_body.py */
5+
/* Version 1.0 */
6+
/* 2024/08/03 */
7+
/************************/
8+
'''
19
import numpy as np
210
import sys
311

0 commit comments

Comments
 (0)