python物理引擎模拟三体_三体世界的模拟

1 '''三体问题求解及可视化,去掉了动图模块'''

2 '''Coworker:聂嘉颖,张瀚文'''

3 importnumpy as np4 from numpy importarange5 importmatplotlib.pyplot as plt6 from mpl_toolkits.mplot3d importAxes3D7 from math importsqrt8

9

10 #得到太阳对行星夹角的cot值

11 defsun_height(x0, y0, z0, x1, y1, z1):12 a0 = x1 -x013 b0 = y1 -y014 c0 = z1 -z015 return a0 / sqrt(b0 ** 2 + c0 ** 2)16

17

18 #得到两星体间的距离

19 defdistants(x0, y0, z0, x1, y1, z1):20 return sqrt((x0 - x1) ** 2 + (y0 - y1) ** 2 + (z0 - z1) ** 2)21

22

23 #计算某一瞬时某一星体对行星的辐射强度

24 defsun_heat(d, sun_temperature, x0, y0, z0, x1, y1, z1):25 theta = d/distants(x0, y0, z0, x1, y1, z1)26 earth_temperature = 0.5 * sqrt(theta) *sun_temperature27 #print(earth_temperature)

28 returnearth_temperature29

30 #定义星体质量

31 #star_4选定为行星,其余为恒星

32 star_1_mass = 3e30 #kg

33 star_2_mass = 2e30 #kg

34 star_3_mass = 3e30 #kg

35 star_4_mass = 2e30 #kg

36

37 diameter0 = 1.0392e10 #米

38 diameter1 = 1.0392e10 #米

39 diameter2 = 1.00392e11 #米

40

41 #定义恒星表面温度

42 sun_temperature0 = 60. #K

43 sun_temperature1 = 600. #K

44 sun_temperature2 = 60. #K

45

46 #引力常数

47 gravitational_constant = 6.67e-11 #m3 / kg s2

48

49 #行星运动总时长(按地球年计算)

50 #每两小时计算一次星体位置

51 end_time = 16 * 365.26 * 24. * 3600. #s

52 h = 2. * 3600 #s

53 num_steps = int(end_time /h)54 tpoints =arange(0, end_time, h)55

56

57 defthree_body_problem():58 '''主程序,计算星体位置和表面温度'''

59 positions = np.zeros([num_steps + 1, 4, 3]) #m

60 velocities = np.zeros([num_steps + 1, 4, 3]) #m / s

61

62 positions[0] = np.array([[1., 3., 2.], [6., -5., 4.], [7., 8., -7.], [7., -2., 9.]]) * 1e11 #m

63 velocities[0] = np.array([[-2., 0.5, 5.], [7., 0.5, 2.], [4., -0.5, 3.], [-10., 4., -2.]]) * 1e3 #m/s

64 positions[0] = np.array([[1., 3., 2.], [3., -5., 1.], [2., 8., -7.], [-3., -2., 9.]]) * 1e11 #m

65 velocities[0] = np.array([[0, 0., 0.], [0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]) * 1e3 #m/s

66

67 defacceleration(positions):68 a = np.zeros([4, 3])69 a[0] = gravitational_constant * star_2_mass / np.linalg.norm(positions[0] - positions[1]) ** 3 *(70 positions[1] - positions[0]) + gravitational_constant * star_3_mass /np.linalg.norm(71 positions[0] - positions[2]) ** 3 * (positions[2] -positions[72 0]) + gravitational_constant * star_4_mass / np.linalg.norm(positions[0] - positions[3]) ** 3 *(73 positions[3] -positions[0])74 a[1] = gravitational_constant * star_1_mass / np.linalg.norm(positions[1] - positions[0]) ** 3 *(75 positions[0] - positions[1]) + gravitational_constant * star_3_mass /np.linalg.norm(76 positions[1] - positions[2]) ** 3 * (positions[2] -positions[77 1]) + gravitational_constant * star_4_mass / np.linalg.norm(positions[1] - positions[3]) ** 3 *(78 positions[3] - positions[1])79 a[2] = gravitational_constant * star_1_mass / np.linalg.norm(positions[2] - positions[0]) ** 3 *(80 positions[0] - positions[2]) + gravitational_constant * star_2_mass /np.linalg.norm(81 positions[2] - positions[1]) ** 3 * (positions[1] -positions[82 2]) + gravitational_constant * star_4_mass / np.linalg.norm(positions[2] - positions[3]) ** 3 *(83 positions[3] - positions[2])84 a[3] = gravitational_constant * star_1_mass / np.linalg.norm(positions[3] - positions[0]) ** 3 *(85 positions[0] - positions[3]) + gravitational_constant * star_2_mass /np.linalg.norm(86 positions[3] - positions[1]) ** 3 * (positions[1] -positions[87 3]) + gravitational_constant * star_3_mass / np.linalg.norm(positions[3] - positions[2]) ** 3 *(88 positions[2] - positions[3])89 returna90

91 defvelocity(p, t, velo):92 v = np.zeros([4, 3])93 v[0] = acceleration(p)[0] * t +velo[0]94 v[1] = acceleration(p)[1] * t + velo[1]95 v[2] = acceleration(p)[2] * t + velo[2]96 v[3] = acceleration(p)[3] * t + velo[3]97 returnvelocities[step]98

99 heat_sum, t = [0.1], [0]100

101 per_0 =0102 for step inrange(num_steps):103 #四阶龙格库塔法求星体位置

104 j1 = h *velocity(positions[step], h, velocities[step])105 j2 = h * velocity(positions[step] + 0.5 * j1, h + 0.5 *h, velocities[step])106 j3 = h * velocity(positions[step] + 0.5 * j2, h + 0.5 *h, velocities[step])107 j4 = h * velocity(positions[step] + j3, h +h, velocities[step])108 positions[step + 1] = positions[step] + (j1 + 2 * j2 + 2 * j3 + j4) / 6

109 velocities[step + 1] = velocities[step] + h * acceleration(positions[step + 1])110

111 #从 positions 中取出坐标值

112 x0, y0, z0 = positions[step, 0, 0], positions[step, 0, 1], positions[step, 0, 2]113 x1, y1, z1 = positions[step, 1, 0], positions[step, 1, 1], positions[step, 1, 2]114 x2, y2, z2 = positions[step, 2, 0], positions[step, 2, 1], positions[step, 2, 2]115 x3, y3, z3 = positions[step, 3, 0], positions[step, 3, 1], positions[step, 3, 2]116

117 #计算三个太阳对行星表面的总辐射强度

118 heat0 =sun_heat(diameter0, sun_temperature0, x0, y0, z0, x3, y3, z3)119 heat1 =sun_heat(diameter1, sun_temperature1, x1, y1, z1, x3, y3, z3)120 heat2 =sun_heat(diameter2, sun_temperature2, x2, y2, z2, x3, y3, z3)121 heat_sum.append(heat0 + heat1 +heat2)122

123 per = int(100 * step /num_steps)124 if per >per_0:125 print(per, '%')126 per_0 =per127

128 t.append(step)129

130 returnpositions, t, heat_sum131

132 positions, t, heat_sum =three_body_problem()133

134

135 empty, extinction_line, frozen_line =[], [], []136

137 for i inrange(len(t)):138 empty.append(0)139 extinction_line.append(100)140 frozen_line.append(40)141

142

143 fig, ax =plt.subplots()144 fig.set_tight_layout(True)145 plt.plot(t, heat_sum, 'b')146 plt.plot(t, empty, 'g')147 plt.plot(t, extinction_line, 'r')148 plt.plot(t, frozen_line, 'r')149

150 ax.set_xlabel('X')151 ax.set_xlim(0, len(t)+10e3)152 ax.set_ylabel('Y')153 plt.show()154

155 print("\033[1;31;47m---Begin ploting image---\033[0m")156

157 fig =plt.figure()158 ax2 =Axes3D(fig)159

160 ax2.plot(positions[:, 0, 0], positions[:, 0, 1], positions[:, 0, 2], color='b')161 ax2.plot(positions[:, 1, 0], positions[:, 1, 1], positions[:, 1, 2], color='y')162 ax2.plot(positions[:, 2, 0], positions[:, 2, 1], positions[:, 2, 2], color='b')163 ax2.plot(positions[:, 3, 0], positions[:, 3, 1], positions[:, 3, 2], color='r')164

165 zoom = 1.2e12

166 ax2.set_xlabel('X')167 #ax2.set_xlim3d(-zoom, zoom + 3.e12)

168 ax2.set_ylabel('Y')169 #ax2.set_ylim3d(-zoom, zoom)

170 ax2.set_zlabel('Z')171 #ax2.set_zlim3d(-zoom, zoom + 2.e12)

172 print("100 %")173

174 plt.show()

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
三体问题是指三个天体之间相互作用的问题,可以用牛顿万有引力定律来描述。为了模拟三体问题,我们需要定义每个天体的质量、初始位置和速度,并使用数值积分方法来计算它们的运动轨迹。以下是一个使用 Python 模拟三体问题的示例代码: ```python import numpy as np import matplotlib.pyplot as plt # 定义天体质量、初始位置和速度 m1, m2, m3 = 1, 1, 1 G = 1 r1 = np.array([-0.5, 0, 0]) r2 = np.array([0.5, 0, 0]) r3 = np.array([0, 1, 0]) v1 = np.array([0.5, 0.5, 0]) v2 = np.array([-0.5, -0.5, 0]) v3 = np.array([0, 0, 0]) # 定义运动方程 def f(r): x1, y1, z1, x2, y2, z2, x3, y3, z3 = r r12 = np.sqrt((x1 - x2)**2 + (y1 - y2)**2 + (z1 - z2)**2) r13 = np.sqrt((x1 - x3)**2 + (y1 - y3)**2 + (z1 - z3)**2) r23 = np.sqrt((x2 - x3)**2 + (y2 - y3)**2 + (z2 - z3)**2) ax1 = G * m2 * (x2 - x1) / r12**3 + G * m3 * (x3 - x1) / r13**3 ay1 = G * m2 * (y2 - y1) / r12**3 + G * m3 * (y3 - y1) / r13**3 az1 = G * m2 * (z2 - z1) / r12**3 + G * m3 * (z3 - z1) / r13**3 ax2 = G * m1 * (x1 - x2) / r12**3 + G * m3 * (x3 - x2) / r23**3 ay2 = G * m1 * (y1 - y2) / r12**3 + G * m3 * (y3 - y2) / r23**3 az2 = G * m1 * (z1 - z2) / r12**3 + G * m3 * (z3 - z2) / r23**3 ax3 = G * m1 * (x1 - x3) / r13**3 + G * m2 * (x2 - x3) / r23**3 ay3 = G * m1 * (y1 - y3) / r13**3 + G * m2 * (y2 - y3) / r23**3 az3 = G * m1 * (z1 - z3) / r13**3 + G * m2 * (z2 - z3) / r23**3 return np.array([v1[0], v1[1], v1[2], v2[0], v2[1], v2[2], v3[0], v3[1], v3[2], ax1, ay1, az1, ax2, ay2, az2, ax3, ay3, az3]) # 使用 Runge-Kutta 数值积分方法计算运动轨迹 r = np.array([r1[0], r1[1], r1[2], r2[0], r2[1], r2[2], r3[0], r3[1], r3[2], v1[0], v1[1], v1[2], v2[0], v2[1], v2[2], v3[0], v3[1], v3[2]]) dt = 0.01 t = np.arange(0, 100, dt) x1, y1, z1, x2, y2, z2, x3, y3, z3 = np.zeros((9, len(t))) for i in range(len(t)): x1[i], y1[i], z1[i], x2[i], y2[i], z2[i], x3[i], y3[i], z3[i], vx1, vy1, vz1, vx2, vy2, vz2, vx3, vy3, vz3 = r k1 = dt * f(r) k2 = dt * f(r + 0.5 * k1) k3 = dt * f(r + 0.5 * k2) k4 = dt * f(r + k3) r += (k1 + 2 * k2 + 2 * k3 + k4) / 6 # 绘制运动轨迹 fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.plot(x1, y1, z1, label='Body 1') ax.plot(x2, y2, z2, label='Body 2') ax.plot(x3, y3, z3, label='Body 3') ax.set_xlabel('X') ax.set_ylabel('Y') ax.set_zlabel('Z') ax.legend() plt.show() ``` 在上述代码中,我们使用了 Runge-Kutta 数值积分方法来计算天体的运动轨迹。具体来说,我们将运动方程表示为 $f(r)$,其中 $r$ 是一个包含位置和速度的向量,然后使用 Runge-Kutta 方法迭代计算 $r$ 的值。最后,我们使用 Matplotlib 库绘制了三个天体的运动轨迹。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值