提交 34e69c06 编写于 作者: 三月三net's avatar 三月三net

太阳系三体模拟器

上级 7a15cb1e
......@@ -20,7 +20,8 @@ class Body(metaclass=ABCMeta):
def __init__(self, name, mass, init_position, init_velocity,
density=5e3, color=(125 / 255, 125 / 255, 125 / 255),
texture=None, size_scale=1.0, distance_scale=1.0):
texture=None, size_scale=1.0, distance_scale=1.0,
rotation_speed=None):
"""
天体类
:param name: 天体名称
......@@ -32,11 +33,14 @@ class Body(metaclass=ABCMeta):
:param texture: 纹理图片
:param size_scale: 尺寸缩放
:param distance_scale: 距离缩放
:param rotation_speed: 自旋速度(度/小时)
"""
self.__his_pos = []
self.__his_vel = []
self.__his_acc = []
self.__his_reserved_num = 100
# 是否忽略质量(如果为True,则计算引力)
self.ignore_mass = False
if name is None:
name = getattr(self.__class__, '__name__')
......@@ -51,6 +55,7 @@ class Body(metaclass=ABCMeta):
self.__velocity = self.init_velocity
self.__density = density
self.__rotation_speed = rotation_speed
self.color = color
self.texture = texture
......@@ -194,6 +199,14 @@ class Body(metaclass=ABCMeta):
"""
return self.__mass
@property
def rotation_speed(self):
"""
自旋速度(度/小时)
:return:
"""
return self.__rotation_speed
@property
def density(self):
"""
......
......@@ -14,7 +14,7 @@ class Earth(Body):
地球
------------------------
转轴倾角: 23.44°
自转周期: 23小时56分4秒(恒星日)
自转周期: 23.93 小时,自转角速度约为 15 度/小时
远日点距离: 152097701 km
近日点距离: 147098074 km
 逃逸速度: 11.186 km/s
......@@ -26,7 +26,8 @@ class Earth(Body):
def __init__(self, name="Earth", mass=5.97237e24,
init_position=[1.12 * AU, 0, 0],
init_velocity=[0, 29.79, 0],
texture="earth1.jpg", size_scale=1.0, distance_scale=1.0):
texture="earth1.jpg", size_scale=1.0, distance_scale=1.0,
rotation_speed=15):
params = {
"name": name,
"mass": mass,
......@@ -36,7 +37,8 @@ class Earth(Body):
"color": (1, 89, 162),
"texture": texture,
"size_scale": size_scale,
"distance_scale": distance_scale
"distance_scale": distance_scale,
"rotation_speed": rotation_speed
}
super().__init__(**params)
......
......@@ -14,7 +14,7 @@ class Jupiter(Body):
木星
------------------------
转轴倾角: 3.13°
自转周期: 9小时55分30秒(赤道略短,两极略长)
自转周期: 9.93 小时,自转角速度约为 36.2537 度/小时 = 360/(9.93)
远日点距离: 5.4588 天文单位
近日点距离: 4.9501 天文单位
逃逸速度: 59.5 km/s
......@@ -26,7 +26,8 @@ class Jupiter(Body):
def __init__(self, name="Jupiter", mass=1.8982e27,
init_position=[5.2 * AU, 0, 0],
init_velocity=[0, 13.06, 0],
texture="jupiter1.jpg", size_scale=1.0, distance_scale=1.0):
texture="jupiter1.jpg", size_scale=1.0, distance_scale=1.0,
rotation_speed=36.2537):
params = {
"name": name,
"mass": mass,
......@@ -36,7 +37,8 @@ class Jupiter(Body):
"color": (173, 121, 92),
"texture": texture,
"size_scale": size_scale,
"distance_scale": distance_scale
"distance_scale": distance_scale,
"rotation_speed": rotation_speed
}
super().__init__(**params)
......
......@@ -14,7 +14,7 @@ class Mars(Body):
火星
------------------------
转轴倾角: 25.19°
自转周期: 24小时37分22.7秒
自转周期: 24.62 小时,自转角速度约为 14.6223 度/小时 = 360/(24.62)
远日点距离: 1.666 天文单位
近日点距离: 1.382 天文单位
逃逸速度: 5.027 km/s
......@@ -25,7 +25,8 @@ class Mars(Body):
def __init__(self, name="Mars", mass=6.4171e23,
init_position=[1.5 * AU, 0, 0],
init_velocity=[0, 24.13, 0],
texture="mars.jpg", size_scale=1.0, distance_scale=1.0):
texture="mars.jpg", size_scale=1.0, distance_scale=1.0,
rotation_speed=14.6223):
params = {
"name": name,
"mass": mass,
......@@ -35,7 +36,8 @@ class Mars(Body):
"color": (213, 97, 59),
"texture": texture,
"size_scale": size_scale,
"distance_scale": distance_scale
"distance_scale": distance_scale,
"rotation_speed": rotation_speed
}
super().__init__(**params)
......
......@@ -14,7 +14,7 @@ class Mercury(Body):
水星
------------------------
转轴倾角: 0.034°
自转周期: 58.65
自转周期: 58.65 地球日,自转角速度约为 0.2558 度/小时 = 360/(58.65*24)
远日点距离: 0.466697 天文单位
近日点距离: 0.307499 天文单位
逃逸速度: 4.25 km/s
......@@ -26,7 +26,8 @@ class Mercury(Body):
def __init__(self, name="Mercury", mass=3.3011e23,
init_position=[0.4 * AU, 0, 0],
init_velocity=[0, 47.87, 0],
texture="mercury.jpg", size_scale=1.0, distance_scale=1.0):
texture="mercury.jpg", size_scale=1.0, distance_scale=1.0,
rotation_speed=0.2558):
params = {
"name": name,
"mass": mass,
......@@ -36,7 +37,8 @@ class Mercury(Body):
"color": (1, 89, 162),
"texture": texture,
"size_scale": size_scale,
"distance_scale": distance_scale
"distance_scale": distance_scale,
"rotation_speed": rotation_speed
}
super().__init__(**params)
......
......@@ -14,8 +14,8 @@ class Moon(Body):
"""
月球
------------------------
 自转周期: 27.32天(自西向东逆时针方向自转)
距地距离约 363104 至 405696 km
 自转周期: 27.32 地球日,自转角速度约为 0.5487 度/小时 = 360/(27.32*24)
距地距离约: 363104 至 405696 km
 逃逸速度: 2.4 km/s
 公转速度: 1.023 km/s + (地球)29.79 km/s
 天体质量: 7.342✕10²² kg
......@@ -25,7 +25,8 @@ class Moon(Body):
def __init__(self, name="Moon", mass=7.342e22,
init_position=[363104 + 1.12 * AU, 0, 0],
init_velocity=[0, 29.79 + 1.023, 0],
texture="moon.jpg", size_scale=1.0, distance_scale=1.0):
texture="moon.jpg", size_scale=1.0, distance_scale=1.0,
rotation_speed=0.5487):
params = {
"name": name,
"mass": mass,
......@@ -35,7 +36,8 @@ class Moon(Body):
"color": (162, 162, 162),
"texture": texture,
"size_scale": size_scale,
"distance_scale": distance_scale
"distance_scale": distance_scale,
"rotation_speed": rotation_speed
}
super().__init__(**params)
......
......@@ -14,7 +14,7 @@ class Neptune(Body):
海王星
------------------------
自转轴倾角: 28.32°
自转周期: 16h 6min 36s
自转周期: 16.11 小时,自转角速度约为 22.3463 度/小时 = 360/(16.11)
远日点距离: 30.33 天文单位
近日点距离: 29.81 天文单位
逃逸速度: 23.5 km/s
......@@ -26,7 +26,8 @@ class Neptune(Body):
def __init__(self, name="Neptune", mass=1.0241e26,
init_position=[30 * AU, 0, 0],
init_velocity=[0, 5.43, 0],
texture="neptune.jpg", size_scale=1.0, distance_scale=1.0):
texture="neptune.jpg", size_scale=1.0, distance_scale=1.0,
rotation_speed=22.3463):
params = {
"name": name,
"mass": mass,
......@@ -36,7 +37,8 @@ class Neptune(Body):
"color": (93, 118, 203),
"texture": texture,
"size_scale": size_scale,
"distance_scale": distance_scale
"distance_scale": distance_scale,
"rotation_speed": rotation_speed
}
super().__init__(**params)
......
......@@ -14,7 +14,7 @@ class Pluto(Body):
冥王星
------------------------
转轴倾角: 119.591±0.014°
自转周期: 6日9小时17分36秒(逆自转)
自转周期: 6.39 地球日,自转角速度约为 -2.3474 度/小时(逆时针自转) = 360/(6.39*24)
远日点距离: 49.305 天文单位(73.760 亿千米)
近日点距离: 29.658 天文单位(44.368 亿千米)
逃逸速度: 1.212 km/s
......@@ -26,7 +26,8 @@ class Pluto(Body):
def __init__(self, name="Pluto", mass=1.303e22,
init_position=[40 * AU, 0, 0],
init_velocity=[0, 4.7, 0],
texture="pluto2.jpg", size_scale=1.0, distance_scale=1.0):
texture="pluto2.jpg", size_scale=1.0, distance_scale=1.0,
rotation_speed=-2.3474):
params = {
"name": name,
"mass": mass,
......@@ -36,7 +37,8 @@ class Pluto(Body):
"color": (67, 28, 7),
"texture": texture,
"size_scale": size_scale,
"distance_scale": distance_scale
"distance_scale": distance_scale,
"rotation_speed": rotation_speed
}
super().__init__(**params)
......
......@@ -14,7 +14,7 @@ class Saturn(Body):
土星
------------------------
自转倾角: 26.73 度
自转周期: 10小时33分38秒
自转周期: 10.66 小时,自转角速度约为 33.7711 度/小时 = 360/(10.66)
远日点距离: 10.1238 天文单位
近日点距离: 9.0412 天文单位
逃逸速度: 35.49 km/s
......@@ -26,7 +26,8 @@ class Saturn(Body):
def __init__(self, name="Saturn", mass=5.6834e26,
init_position=[10 * AU, 0, 0],
init_velocity=[0, 9.64, 0],
texture="saturn.jpg", size_scale=1.0, distance_scale=1.0):
texture="saturn.jpg", size_scale=1.0, distance_scale=1.0,
rotation_speed=33.7711):
params = {
"name": name,
"mass": mass,
......@@ -36,7 +37,8 @@ class Saturn(Body):
"color": (219, 189, 159),
"texture": texture,
"size_scale": size_scale,
"distance_scale": distance_scale
"distance_scale": distance_scale,
"rotation_speed": rotation_speed
}
super().__init__(**params)
......
......@@ -13,6 +13,7 @@ class Sun(Body):
"""
太阳
------------------------
自转周期: 24.47 地球日,自转角速度约为 0.6130 度/小时 = 360/(24.47*24)
天体质量: 1.9891×10³⁰ kg
平均密度: 1.408×10³ kg/m³
"""
......@@ -20,7 +21,8 @@ class Sun(Body):
def __init__(self, name="Sun", mass=1.9891e30,
init_position=[0, 0, 0],
init_velocity=[0, 0, 0],
texture="sun2.jpg", size_scale=1.0, distance_scale=1.0):
texture="sun2.jpg", size_scale=1.0, distance_scale=1.0,
rotation_speed=0.6130):
params = {
"name": name,
"mass": mass,
......@@ -30,7 +32,8 @@ class Sun(Body):
"color": (170, 98, 25),
"texture": texture,
"size_scale": size_scale,
"distance_scale": distance_scale
"distance_scale": distance_scale,
"rotation_speed": rotation_speed
}
super().__init__(**params)
......
......@@ -14,7 +14,7 @@ class Uranus(Body):
天王星
------------------------
转轴倾角: 97.77°
自转周期: 17时14分24秒
自转周期: 17.24 小时,自转角速度约为 -20.8816 度/小时(逆时针自转) = 360/(17.24)
远日点距离: 20.11 天文单位
近日点距离: 18.33 天文单位
逃逸速度: 21.3 km/s
......@@ -26,7 +26,8 @@ class Uranus(Body):
def __init__(self, name="Uranus", mass=8.681e25,
init_position=[19 * AU, 0, 0],
init_velocity=[0, 6.81, 0],
texture="uranus.jpg", size_scale=1.0, distance_scale=1.0):
texture="uranus.jpg", size_scale=1.0, distance_scale=1.0,
rotation_speed=-20.8816):
params = {
"name": name,
"mass": mass,
......@@ -36,7 +37,8 @@ class Uranus(Body):
"color": (94, 124, 193),
"texture": texture,
"size_scale": size_scale,
"distance_scale": distance_scale
"distance_scale": distance_scale,
"rotation_speed": rotation_speed
}
super().__init__(**params)
......
......@@ -14,7 +14,7 @@ class Venus(Body):
金星
------------------------
轨道倾角: 3.39458 度
自转周期: 243
自转周期: 243 地球日,自转角速度约为 -0.0617 度/小时(逆时针自转) = 360/(243*24)
远日点距离: 0.728213 天文单位
近日点距离: 0.718440天文单位
逃逸速度: 10.36 km/s
......@@ -26,7 +26,8 @@ class Venus(Body):
def __init__(self, name="Venus", mass=4.8675e24,
init_position=[0.72 * AU, 0, 0],
init_velocity=[0, 35, 0],
texture="venus.jpg", size_scale=1.0, distance_scale=1.0):
texture="venus.jpg", size_scale=1.0, distance_scale=1.0,
rotation_speed=-0.0617):
params = {
"name": name,
"mass": mass,
......@@ -36,7 +37,8 @@ class Venus(Body):
"color": (173, 81, 5),
"texture": texture,
"size_scale": size_scale,
"distance_scale": distance_scale
"distance_scale": distance_scale,
"rotation_speed": rotation_speed
}
super().__init__(**params)
......
......@@ -91,10 +91,14 @@ class System(object):
# self.bodies = list(filter(valid_body, self.bodies))
for body1 in self.bodies:
if body1.ignore_mass:
continue
if not valid_body(body1):
continue
acceleration = np.zeros(3)
for body2 in self.bodies:
if body2.ignore_mass:
continue
if self.max_distance > 0:
if calculate_distance(body1.position) > self.max_distance: # 超过了max_distance距离,则消失
body1.appeared = False
......
......@@ -16,14 +16,17 @@ if __name__ == '__main__':
地球、月球
"""
# 地球的Y方向初始速度
EARTH_INIT_VELOCITY = -0.02 # 200m/s
EARTH_INIT_VELOCITY = 0 # 200m/s
sun = Sun(init_position=[0, AU, 0], init_velocity=[0, 0, 0], size_scale=1e1)
sun.ignore_mass = True
bodies = [
# sun,
Earth(init_position=[0, 0, 0],
init_velocity=[0, EARTH_INIT_VELOCITY, 0], size_scale=1e1), # 地球放大 10 倍,距离保持不变
Moon(init_position=[363104, 0, 0],
Moon(init_position=[363104, 0, 0], # 距地距离约: 363104 至 405696 km
init_velocity=[0, EARTH_INIT_VELOCITY + 1.023, 0], size_scale=1e1) # 月球放大 10 倍,距离保持不变
]
# mayavi_run(bodies, SECONDS_PER_HALF_DAY / 2, view_azimuth=-45)
# 使用 ursina 查看的运行效果
ursina_run(bodies, SECONDS_PER_HALF_DAY / 2, position=(0, 0, 0))
ursina_run(bodies, SECONDS_PER_DAY, position=(0, 0, 0))
......@@ -44,6 +44,7 @@ class Simulator(metaclass=ABCMeta):
self.bodies_sys.evolve(dt)
for idx, view in enumerate(self.body_views):
body = self.bodies_sys.bodies[idx]
body.dt = dt
view.appeared = body.appeared
if not view.appeared:
view.disappear()
......
......@@ -139,9 +139,11 @@ class UrsinaSimulator(Simulator):
def run(self, dt, **kwargs):
from ursina import EditorCamera, PointLight, SpotLight, AmbientLight, DirectionalLight
self.evolve_dt = dt
# 设定时间间隔为1秒
self.interval = datetime.timedelta(seconds=0.01)
# 设定时间间隔为0.01秒
interval = 0.01
self.evolve_dt = dt * interval
self.interval = datetime.timedelta(seconds=interval)
self.last_time = datetime.datetime.now() - datetime.timedelta(seconds=2)
if "light" in kwargs:
if kwargs["light"]:
......
......@@ -22,7 +22,7 @@ from simulators.views.body_view import BodyView
import numpy as np
import math
SCALE_FACTOR = 5e-7
SCALE_FACTOR = 5e-6
class UrsinaPlayer(FirstPersonController):
......@@ -73,8 +73,7 @@ class UrsinaPlayer(FirstPersonController):
class Planet(Entity):
def __init__(self, body_view: BodyView):
self.body_view = body_view
# 旋转速度和大小成反比(未使用真实数据)
self.rotspeed = 30000 / self.body_view.raduis # random.uniform(1.0, 2.0)
self.rotation_speed = self.body_view.body.rotation_speed
self.rotMode = 'x' # random.choice(["x", "y", "z"])
self.name = body_view.name
......@@ -101,6 +100,22 @@ class Planet(Entity):
self.y = pos[2]
self.z = pos[0]
dt = 0
if hasattr(self.body_view.body, "dt"):
dt = self.body_view.body.dt
if self.rotation_speed is None or dt == 0:
self.rotspeed = 0
# 旋转速度和大小成反比(未使用真实数据)
# self.rotspeed = 30000 / self.body_view.raduis # random.uniform(1.0, 2.0)
else:
# 4.60e-6 是通过月球保持一面面对地球,调整得到
# self.rotspeed = self.rotation_speed * dt * 4.60e-6
# (self.rotation_speed * dt * 4.60e-6),(self.rotation_speed * (dt / 3600))/60
self.rotspeed = self.rotation_speed * (dt / 3600) / 2.4 # / 60 / 24
# self.rotspeed = self.rotation_speed * (dt / 3600) / 3.65e7
# rotation_speed 度/小时 dt 秒 = (dt / 3600)小时
self.rotation_y -= self.rotspeed
# def input(self, key):
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册