-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolarsystem.py
More file actions
90 lines (71 loc) · 2.21 KB
/
solarsystem.py
File metadata and controls
90 lines (71 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!env python
from astropy.time import Time, TimeDelta
from astropy.coordinates import get_body, HeliocentricTrueEcliptic
import numpy as np
from numpy.linalg import norm
import yaml
def get_body_coord(name, t):
"""
Return body position (in heliocentric cartesian coordinates) at time t (query astropy)
"""
body = get_body(name, t)
body = body.transform_to(HeliocentricTrueEcliptic())
lon = body.lon
lat = body.lat
R = body.distance
x = R * np.cos(lat) * np.cos(lon)
y = R * np.cos(lat) * np.sin(lon)
z = R * np.sin(lat)
return np.asarray([x.m, y.m, z.m], dtype=np.float64)
def get_body_vel(name, t):
"""
Return body velocity vector with finite difference (dt = 1sec)
"""
x0 = get_body_coord(name, t)
x1 = get_body_coord(name, t + TimeDelta(1, format="sec"))
return x1 - x0
t = Time.now()
with open("masses.yaml") as f:
masses = yaml.safe_load(f)
class Planet:
def __init__(self, x, v, m):
self.x = x
self.v = v
self.m = m
class Body:
def __init__(self, name, mass, t=None):
if t is None:
t = Time.now()
self.name = name
self.mass = mass
try:
self.x = get_body_coord(name, t)
self.v = get_body_vel(name, t)
except KeyError:
self.x = None
self.v = None
# raise RuntimeError(
# f"Cannot find {name} position and velocity, did you misspell it?"
# )
# Force sun to center and 0 out velocity
if self.x is None:
raise ValueError(
f"Cannot find '{name}' position and velocity, did you misspell it?"
)
if name == "sun":
self.x[:] = np.zeros((3))
self.v[:] = np.zeros((3))
print(
f"{name:10}: mass: {self.mass:10.2e} kg, velocity: {norm(self.v):10.2e} m/s"
)
solarsystem = {}
for b in masses.keys():
solarsystem[b] = Body(b, float(masses[b]))
planets = {}
for name in solarsystem.keys():
if name != "sun":
x = solarsystem[name].x
v = solarsystem[name].v
m = solarsystem[name].mass
planets[name] = Planet(x, v, m)
m_sun = solarsystem["sun"].mass