提交 2342b5c6 编写于 作者: 三月三net's avatar 三月三net

Python超人-宇宙模拟器

上级 398a3b70
......@@ -99,28 +99,13 @@ class HalleyCometSim(HalleyCometSimBase):
"""
print(dt.strftime('%Y-%m-%d %H:%M:%S'))
# def set_bodies_position(self, dt):
# """
# 设置天体的位置(包含速度和加速度的信息)
# @param time_data:
# @return:
# """
# # t = self.start_time + time_data.total_days
# set_solar_system_celestial_position(self.bodies, dt, False)
def calc_simulator():
def calc_simulator(params):
from sim_scenes.func import calc_run
from simulators.calc_simulator import CalcSimulator, CalcContext
CalcSimulator.init(True)
params = HalleyCometParams(
start_time='1982-09-24 00:00:00',
init_velocity=[-2.836, 4.705, 8.85],
init_position=[0, -5 * AU, -10 * AU]
)
sim = HalleyCometSim(params)
sim.total_times = 0
sim.build()
......@@ -146,7 +131,7 @@ def calc_simulator():
set_solar_system_celestial_position(sim.bodies, t, False)
_dt = time_data.get_datetime(str(sim.start_time))
sim.show_clock(_dt)
# sim.show_clock(_dt)
# sim.set_bodies_position(_dt)
# 距离太阳远日点: 35.018 AU(40y310y)
# 距离太阳近日点: 0.580 AU(3y138d)
......@@ -161,7 +146,6 @@ def calc_simulator():
time_data = context.get_param("time_data")
# td = TimeData(sim.total_times * dt * UrsinaSimulator.INTERVAL_FATOR, BodyTimer.MIN_UNIT_SECONDS)
# sim.set_bodies_position(td)
context.put_param("years", time_data.years)
......@@ -169,63 +153,63 @@ def calc_simulator():
# 哈雷彗星离太阳最远的点称为 "aphelion of Halley's Comet"(远日点)
if not hasattr(sim, "comet_aphel"):
sim.comet_aphel = d_sun
sim.comet_aphel_dt = f'{time_data.years}y{time_data.days}d'
sim.comet_aphel_dt = current_dt(time_data)
elif d_sun > sim.comet_aphel:
sim.comet_aphel = d_sun
sim.comet_aphel_dt = f'{time_data.years}y{time_data.days}d'
print("year days:", time_data.years, time_data.days)
sim.comet_aphel_dt = current_dt(time_data)
# log(time_data)
# print("year days:", time_data.years, time_data.days)
# sim.comet_aphel_dt = dt.strftime("%Y-%m-%d")
# sim.set_bodies_position(td)
print("距离太阳远日点: %.3f AU(%s)" % (sim.comet_aphel / AU, sim.comet_aphel_dt))
print("距离太阳近日点: %.3f AU(%s)" % (sim.comet_peri / AU, sim.comet_peri_dt))
# print("远日点: %.3f AU(%s)" % (sim.comet_aphel / AU, sim.comet_aphel_dt))
# print("距离太阳近日点: %.3f AU(%s)" % (sim.comet_peri / AU, sim.comet_peri_dt))
# print("距离地球: %.3f AU" % (d_earth / AU))
# 哈雷彗星离太阳最近的点称为 "perihelion of Halley's Comet"(近日点:comet_peri),
if not hasattr(sim, "comet_peri"):
sim.comet_peri = d_sun
sim.comet_peri_dt = f'{time_data.years}y{time_data.days}d'
sim.comet_peri_dt = current_dt(time_data)
elif d_sun < sim.comet_peri:
sim.comet_peri = d_sun
sim.comet_peri_dt = f'{time_data.years}y{time_data.days}d'
print("距离太阳远日点: %.3f AU(%s)" % (sim.comet_aphel / AU,sim.comet_aphel_dt))
print("距离太阳近日点: %.3f AU(%s)" % (sim.comet_peri / AU,sim.comet_peri_dt))
sim.comet_peri_dt = current_dt(time_data)
# log(time_data)
# print("距离地球: %.3f AU" % (d_earth / AU))
# sim.comet_peri_dt = dt.strftime("%Y-%m-%d")
def current_dt(time_data: TimeData):
# f'{time_data.years}y{time_data.days}d'
return time_data.years, time_data.days, time_data.total_days
# sim.set_bodies_position(td)
# print("year days:", td.years, td.days)
# print("距离太阳: %.3f AU" % (d_sun / AU))
# print("距离地球: %.3f AU" % (d_earth / AU))
def log(time_data):
print(params)
comet_aphel = sim.comet_aphel / AU
comet_peri = sim.comet_peri / AU
diff_days = sim.comet_aphel_dt[2] - sim.comet_peri_dt[2]
print("远日点:%.3f AU, 近日点:%.3f AU, 天数: %i" % (comet_aphel, comet_peri, diff_days))
"""
哈雷彗星
根据最新信息,哈雷彗星的平均运行速度约为每小时 70,000 英里或每小时 126,000 公里 。
该速度表示彗星穿过太阳系的速度。 值得注意的是,截至 2023 年,所提供的信息都是准确的。
质量: 10^15kg
远日点: 35.1 AU(2023年12月9日)
近日点: 0.586 AU 上次通过近日点:1986年2月9日 下次通过近日点:2061年7月28日
平均密度: 0.6(估计的范围在0.2至1.5g/cm)
均密度: 1 g/cm³ -> 1✕10³ kg/m³
来源:https://www.cgmodel.com/model/500318.html
2023年12月9日 - 1986年2月9日 = 13817
35.1AU 0.586AU
"""
if abs(comet_peri - 0.586) < 0.01 and abs(comet_aphel - 35.1) < 0.1 and (diff_days - 13817) < 2:
print("OK ......",comet_peri,comet_aphel,diff_days)
# print("year days:", time_data.years, time_data.days)
# print("远日点: %.3f AU(%s)" % (sim.comet_aphel / AU, sim.comet_aphel_dt))
# print("近日点: %.3f AU(%s)" % (sim.comet_peri / AU, sim.comet_peri_dt))
# # 2023年12月9日 - 1986年2月9日 = 13817
# print("远\近日天数: %i 天 " % (sim.comet_aphel_dt[2] - sim.comet_peri_dt[2]))
def on_finished(context: CalcContext):
pass
print("on_finished", context)
# import matplotlib.pyplot as plt
# # 解决中文显示问题
# plt.rcParams['font.sans-serif'] = ['SimHei']
# plt.rcParams['axes.unicode_minus'] = False
# acc_values = context.params["acc_values"]
# vel_values = context.params["vel_values"]
# max_value = max(acc_values)
# min_value = min(acc_values)
# fig = plt.figure(figsize=(10, 6))
# ax1 = fig.add_subplot(111)
# ax1.plot(acc_values, 'blue', label='加速度')
# ax1.set_ylabel('加速度', fontdict={'weight': 'normal', 'size': 15, 'color': 'blue'})
# # ax1.set_title("加速度/速度", fontdict={'weight': 'normal', 'size': 15})
# plt.title("%s(%.4f) max:%.4f min:%.4f diff:%.4f" % (target.name,
# context.get_param("init_vel"), max_value * 1e6,
# min_value * 1e6, (max_value - min_value) * 1e6))
# l1 = ax1.legend(loc='lower left', labels=['加速度'])
#
# ax2 = ax1.twinx() # this is the important function
# ax2.plot(vel_values, 'red', label='速度')
# ax2.set_ylabel('速度', fontdict={'weight': 'normal', 'size': 15, 'color': 'red'})
# ax2.set_xlabel('Same')
#
# l2 = ax2.legend(loc='upper right', labels=['速度'])
# plt.show()
# print("on_finished", context)
time_data = context.get_param("time_data")
log(time_data)
CalcSimulator.on_ready_subscription(on_ready)
CalcSimulator.on_after_evolve_subscription(after_evolve)
......@@ -235,45 +219,30 @@ def calc_simulator():
calc_run(sim.bodies, SECONDS_PER_YEAR, evolve_next=evolve_next)
def ursina_simulator():
from sim_scenes.func import ursina_run, camera_look_at
from simulators.ursina.entities.body_timer import TimeData
from simulators.ursina.ursina_event import UrsinaEvent
if __name__ == '__main__':
params = HalleyCometParams(
start_time='1982-09-24 00:00:00',
init_velocity=[-2.836, 4.705, 8.85],
init_position=[0, -5 * AU, -10 * AU]
)
sim = HalleyCometSim(params)
sim.build()
def on_ready():
pass
def on_timer_changed(time_data: TimeData):
pass
# 订阅事件后,上面的函数功能才会起作用
# 运行前会触发 on_ready
UrsinaEvent.on_ready_subscription(on_ready)
# 运行中,每时每刻都会触发 on_timer_changed
UrsinaEvent.on_timer_changed_subscription(on_timer_changed)
# 使用 ursina 查看的运行效果
# 常用快捷键: P:运行和暂停 O:重新开始 I:显示天体轨迹
# position = 左-右+、上+下-、前+后-
ursina_run(sim.bodies, SECONDS_PER_YEAR,
# position=(-300000, 1500000, -100),
position=(0, 0.5 * AU, -5 * AU),
show_timer=True,
show_trail=True
# year days: 42 6
# 远日点: 35.086 AU((40, 346, 14946.8522))
# 近日点: 0.586 AU((3, 133, 1228.3418))
# 远\近日天数: 13718 天
# for x in range(-2,2):
# for y in range(-2, 2):
# for z in range(-2, 2):
idx = 0
r = 2
for x in range(-r, r+1):
for y in range(-r, r+1):
for z in range(-r, r+1):
params = HalleyCometParams(
start_time='1982-09-24 00:00:00',
init_velocity=[-2.836 + (x / 100), 4.705 + (y / 100), 8.85 + (z / 100)],
init_position=[0, -5 * AU, -10 * AU]
)
if __name__ == '__main__':
calc_simulator()
# calc_simulator(moon)
# calc_simulator(satellite)
# ursina_simulator()
idx += 1
print(f"Index:{idx} ---------------------------")
calc_simulator(params)
......@@ -171,7 +171,7 @@ class CalcSimulator(Simulator):
super().__init__(bodies_sys, CalcView)
def run(self, dt, **kwargs):
dt = dt * UrsinaSimulator.INTERVAL_FATOR
dt = dt * UrsinaSimulator.INTERVAL_FATOR * UrsinaSimulator.TIME_ADJ_FATOR
evolve_next = None
if "evolve_next" in kwargs:
evolve_next = kwargs["evolve_next"]
......
......@@ -35,6 +35,7 @@ class UrsinaSimulator(Simulator):
Ursina官网: https://www.ursinaengine.org/
"""
INTERVAL_FATOR = 0.01
TIME_ADJ_FATOR = 1.666
def __init__(self, bodies_sys: System):
# window.borderless = False
......@@ -233,7 +234,7 @@ class UrsinaSimulator(Simulator):
# interval_fator 能让更新天体运行状态(位置、速度)更精确
evolve_dt = evolve_dt * self.INTERVAL_FATOR
if run_speed_factor < 3:
evolve_dt *= 1.666 # 人为加入一个针对秒级计算的误差,保证模拟器的1秒和现实同步(不要求精确可以注释掉)
evolve_dt *= self.TIME_ADJ_FATOR # 人为加入一个针对秒级计算的误差,保证模拟器的1秒和现实同步(不要求精确可以注释掉)
evolve_args = {"evolve_dt": evolve_dt}
UrsinaEvent.on_before_evolving(evolve_args)
# if evolve_args["evolve_dt"] > 0:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册