一个基于经典力学和万有引力定律的简化宇宙模型实现思路,使用Python代码模拟天体运动:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
# 天体类定义
class CelestialBody:
def __init__(self, mass, position, velocity):
self.mass = mass # 质量 (kg)
self.pos = np.array(position, dtype='float64') # 位置矢量 (m)
self.vel = np.array(velocity, dtype='float64') # 速度矢量 (m/s)
# 物理常数
G = 6.67430e-11 # 引力常数 (m³·kg⁻¹·s⁻²)
dt = 3600 * 24 # 时间步长 (1天)
# 初始化天体系统 (示例:太阳-地球-月球系统)
bodies = [
CelestialBody(1.9885e30, [0, 0], [0, 0]), # 太阳
CelestialBody(5.972e24, [1.496e11, 0], [0, 29.78e3]), # 地球
CelestialBody(7.342e22, [1.496e11 + 3.844e8, 0], [0, 1.022e3]) # 月球
]
# 初始化绘图
fig, ax = plt.subplots()
points = [ax.plot([], [], 'o', markersize=np.log10(body.mass/1e24))[0] for body in bodies]
def update(frame):
# 计算引力相互作用
for i, body in enumerate(bodies):
acceleration = np.zeros(2)
for j, other in enumerate(bodies):
if i != j:
r = other.pos - body.pos
distance = np.linalg.norm(r)
acceleration += G * other.mass * r / (distance**3)
# 更新速度和位置(使用半隐式欧拉法)
body.vel += acceleration * dt
body.pos += body.vel * dt
# 更新图形
for point, body in zip(points, bodies):
point.set_data(body.pos[0], body.pos[1])
ax.set_xlim(-2e11, 2e11)
ax.set_ylim(-2e11, 2e11)
return points
# 创建动画
ani = FuncAnimation(fig, update, frames=200, interval=50)
plt.show()
主要实现原理:
- 物理基础:基于牛顿万有引力定律和运动定律
- 数值积分:使用半隐式欧拉法进行近似计算
- 对象建模:每个天体存储质量、位置和速度信息
- 交互计算:实时计算天体间的引力作用
扩展方向建议:
- 增加相对论修正(广义相对论效应)
- 加入碰撞检测与合并机制
- 实现三维空间模拟
- 添加恒星演化模型(核聚变、超新星爆发)
- 引入暗物质和暗能量模型
- 使用GPU加速计算(如CUDA)
注意事项:
- 数值稳定性:长时间模拟可能积累误差
- 性能优化:N体问题复杂度为O(n²)
- 尺度问题:需处理天文数字级的大数
- 相对论效应:高速/强引力场需考虑时空弯曲
实际宇宙建模需要结合:
- 广义相对论
- 量子场论
- 粒子物理学
- 流体动力学
- 宇宙学原理(如FLRW度规)
建议学习路线:
- 经典力学 → 2. 微分方程数值解 → 3. 计算物理学 → 4. 天体物理学 → 5. 宇宙学