提交 6a718659 编写于 作者: 三月三net's avatar 三月三net

Python超人-宇宙模拟器

上级 e2269f18
...@@ -117,6 +117,7 @@ def ursina_run(bodies, ...@@ -117,6 +117,7 @@ def ursina_run(bodies,
show_exit_button=True, show_exit_button=True,
grid_position=None, grid_position=None,
grid_scale=None, grid_scale=None,
gravity_works=True,
show_trail=False, show_trail=False,
show_name=False, show_name=False,
show_timer=False, show_timer=False,
...@@ -135,6 +136,7 @@ def ursina_run(bodies, ...@@ -135,6 +136,7 @@ def ursina_run(bodies,
@param show_camera_info: 是否显示摄像机信息面板 @param show_camera_info: 是否显示摄像机信息面板
@param show_control_info: 是否显示控制信息面板 @param show_control_info: 是否显示控制信息面板
@param show_exit_button: 是否显示模拟器关闭按钮 @param show_exit_button: 是否显示模拟器关闭按钮
@param gravity_works: 万有引力是否启用
@param show_trail: 是否显示拖尾 @param show_trail: 是否显示拖尾
@param show_name: 是否显示天体名称 @param show_name: 是否显示天体名称
@param show_timer: 是否显示计时器 @param show_timer: 是否显示计时器
...@@ -171,7 +173,7 @@ def ursina_run(bodies, ...@@ -171,7 +173,7 @@ def ursina_run(bodies,
def callback_update(): def callback_update():
UrsinaEvent.on_application_run() UrsinaEvent.on_application_run()
for ursina_view in simulator.ursina_views: for ursina_view in simulator.ursina_views:
simulator.check_and_evolve() simulator.check_and_evolve(gravity_works)
if ursina_view.appeared: if ursina_view.appeared:
ursina_view.update() ursina_view.update()
......
# -*- coding:utf-8 -*-
# title :模拟太阳系给天体真实时间和位置
# description :模拟太阳系给天体真实时间和位置
# author :Python超人
# date :2023-07-23
# link :https://gitcode.net/pythoncr/
# python_version :3.8
# ==============================================================================
import numpy as np
from bodies import Sun, Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune, Pluto, Moon, Asteroids
from common.consts import SECONDS_PER_WEEK, SECONDS_PER_DAY, SECONDS_PER_YEAR, AU
from sim_scenes.func import mayavi_run, ursina_run
from simulators.ursina.entities.body_timer import TimeData
from simulators.ursina.ui.control_ui import ControlUI
from simulators.ursina.ursina_event import UrsinaEvent
from astropy.coordinates import get_body_barycentric
from astropy.time import Time
def get_bodies_positions(planet_names="sun,mercury,venus,earth,moon,mars,jupiter,saturn,uranus,neptune", time=None):
if time is None:
time = Time.now()
planets = planet_names.split(",")
positions = {}
for planet in planets:
try:
position = get_body_barycentric(planet, time)
positions[planet] = position
print(planet, position)
except Exception as e:
print(planet, str(e))
return positions
def get_bodies_names(bodies):
names = ""
for body in bodies:
names += body.__class__.__name__ + ","
return names[0:-1]
current_time = Time.now()
if __name__ == '__main__':
# 八大行星:木星(♃)、土星(♄)、天王星(♅)、海王星(♆)、地球(⊕)、金星(♀)、火星(♂)、水星(☿)
# 排列顺序
# 1、体积:(以地球为1)木星 :土星 :天王星 :海王星 :地球 :金星 :火星 :水星 = 1330:745:65:60:1:0.86:0.15:0.056
# 2、质量:(以地球为1)木星 :土星 :天王星 :海王星 :地球 :金星 :火星 :水星 = 318:95:14.53:17.15:1:0.8:0.11:0.0553
# 3、离太阳从近到远的顺序:水星、金星、地球、火星、木星、土星、天王星、海王星
# =====================================================================
# 以下展示的效果为太阳系真实的距离
# 由于宇宙空间尺度非常大,如果按照实际的天体大小,则无法看到天体,因此需要对天体的尺寸进行放大
sun = Sun(name="太阳", size_scale=0.4e2) # 太阳放大 80 倍,距离保持不变
bodies = [
sun,
Mercury(name="水星", size_scale=3e3), # 水星
Venus(name="金星", size_scale=3e3), # 金星
Earth(name="地球", size_scale=3e3), # 地球
Moon(name="月球", size_scale=3e3), # 月球
Mars(name="火星", size_scale=3e3), # 火星
Jupiter(name="木星", size_scale=6e2), # 木星
Saturn(name="土星", size_scale=6e2), # 土星
Uranus(name="天王星", size_scale=10e2), # 天王星
Neptune(name="海王星", size_scale=10e2), # 海王星
]
# pip install -i http://pypi.douban.com/simple/ --trusted-host=pypi.douban.com de423
names = get_bodies_names(bodies)
def get_body_position(body, positions):
position = positions.get(body.__class__.__name__, None)
if position is None:
return [0, 0, 0]
# return [position.x.value * AU, position.y.value * AU, position.z.value * AU]
return [position.x.value * AU, position.z.value * AU, position.y.value * AU]
def on_ready():
# 运行前触发
pass
def on_timer_changed(time_data: TimeData):
t = current_time + time_data.total_days
positions = get_bodies_positions(names, t)
for body in bodies:
position = get_body_position(body, positions)
body.position = np.array(position)
dt = time_data.get_datetime(str(current_time))
# print(time_data.get_datetime(str(current_time)))
ControlUI.current_ui.show_message(dt.strftime('%Y-%m-%d %H:%M:%S'), font="simsun.ttc", close_time=-1)
# 运行中,每时每刻都会触发 on_timer_changed
UrsinaEvent.on_timer_changed_subscription(on_timer_changed)
# 运行前会触发 on_ready
UrsinaEvent.on_ready_subscription(on_ready)
# 使用 ursina 查看的运行效果
# 常用快捷键: P:运行和暂停 O:重新开始 I:显示天体轨迹
# position = 左-右+、上+下-、前+后-
ursina_run(bodies, SECONDS_PER_WEEK,
position=(0, 2 * AU, -11 * AU),
gravity_works=False, # 关闭万有引力的计算
show_grid=False,
show_timer=True)
...@@ -35,13 +35,17 @@ class Simulator(metaclass=ABCMeta): ...@@ -35,13 +35,17 @@ class Simulator(metaclass=ABCMeta):
view = viewer_type(body, self.bodies_sys) view = viewer_type(body, self.bodies_sys)
self.body_views.append(view) self.body_views.append(view)
def evolve(self, dt: int): def evolve(self, dt: int, gravity_works=True):
""" """
单位:秒,按时间差进行演变,值越小越精确,但演变速度会慢。 单位:秒,按时间差进行演变,值越小越精确,但演变速度会慢。
@param dt: 时间差(秒) @param dt: 时间差(秒)
@param gravity_works: 万有引力是否起作用
@return: @return:
""" """
self.bodies_sys.evolve(dt) if gravity_works:
self.bodies_sys.evolve(dt)
for idx, view in enumerate(self.body_views): for idx, view in enumerate(self.body_views):
body = self.bodies_sys.bodies[idx] body = self.bodies_sys.bodies[idx]
body.dt = dt body.dt = dt
......
...@@ -59,6 +59,17 @@ class TimeData: ...@@ -59,6 +59,17 @@ class TimeData:
else: else:
self.time_text = f'{self.hours:02d}:{self.minutes:02d}:{self.seconds:02d}' self.time_text = f'{self.hours:02d}:{self.minutes:02d}:{self.seconds:02d}'
def get_datetime(self, init_datetime):
import datetime
# UTC_format = "%Y-%m-%dT%H:%M:%S.%fZ"
UTC = datetime.datetime.strptime(init_datetime + "Z", "%Y-%m-%d %H:%M:%S.%fZ")
# BJS_format = "%Y-%m-%d %H:%M:%S"
BJS = UTC + datetime.timedelta(hours=8+self.total_hours)
# BJS = BJS.strftime(BJS_format)
# dt = datetime(init_datetime)
return BJS
@property @property
def total_minutes(self): def total_minutes(self):
return self.total_seconds / 60 return self.total_seconds / 60
......
...@@ -63,17 +63,22 @@ class UiPanel(WindowPanel): ...@@ -63,17 +63,22 @@ class UiPanel(WindowPanel):
""" """
pass pass
def show_message(self, message, close_time=3): def show_message(self, message, font=None, close_time=3):
""" """
显示消息框 显示消息框
@param message: 消息内容 @param message: 消息内容
@param font: 字体
@param close_time: 定义显示消息框关闭时间 @param close_time: 定义显示消息框关闭时间
@return: @return:
""" """
if hasattr(self, "last_message_box"): if hasattr(self, "last_message_box"):
destroy(self.last_message_box) destroy(self.last_message_box)
if font is None:
font = UrsinaConfig.CN_FONT
# else:
# font = None
# 创建消息框 # 创建消息框
message_box = Text(text=message, font=UrsinaConfig.CN_FONT, background=True, origin=(0, 0), y=.25) message_box = Text(text=message, font=font, background=True, origin=(0, 0), y=.25)
self.last_message_box = message_box self.last_message_box = message_box
......
...@@ -210,7 +210,7 @@ class UrsinaSimulator(Simulator): ...@@ -210,7 +210,7 @@ class UrsinaSimulator(Simulator):
self.last_time = now self.last_time = now
return is_expired return is_expired
def check_and_evolve(self): def check_and_evolve(self, gravity_works=True):
if self.check_interval_expired(): if self.check_interval_expired():
# 获取配置中的运行速度的因子 # 获取配置中的运行速度的因子
run_speed_factor = float(UrsinaConfig.run_speed_factor) run_speed_factor = float(UrsinaConfig.run_speed_factor)
...@@ -227,7 +227,7 @@ class UrsinaSimulator(Simulator): ...@@ -227,7 +227,7 @@ class UrsinaSimulator(Simulator):
evolve_args = {"evolve_dt": evolve_dt} evolve_args = {"evolve_dt": evolve_dt}
UrsinaEvent.on_before_evolving(evolve_args) UrsinaEvent.on_before_evolving(evolve_args)
# if evolve_args["evolve_dt"] > 0: # if evolve_args["evolve_dt"] > 0:
super().evolve(evolve_args["evolve_dt"]) super().evolve(evolve_args["evolve_dt"], gravity_works=gravity_works)
if self.show_timer or self.timer_enabled: if self.show_timer or self.timer_enabled:
timer = BodyTimer() timer = BodyTimer()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册