太阳系仿真-我的计算哪里出错了?

时间:2019-05-16 15:56:21

标签: python tkinter python-3.6 simulation physics

我正在使用物理方程式,x和y坐标以及使用2D列表的字典为项目进行2D太阳系仿真。我正在使用tkinter画布来构造动画。

最初,行星“地球”似乎以非常缓慢的加速度消失在屏幕上,然后不久就以巨大的加速度消失了。我看不到问题。

以前,我只有地球绕太阳旋转,这以类似的方式成功地使用了方程,并通过计算出的位移的x和y分量在画布上移动了地球。字典按awk -F'/' ' NR==FNR { old[NR] = $2 new[NR] = $3 next } { gsub(old[FNR],new[FNR]) } 1' filterfile originalfile fuu bir foolar 排序。如有必要,将计算出的值存储或添加到此字典中的某些值。这是我正在使用的代码:

{(-body):[[x, y], [x velocity, y velocity], mass, [change in x displacement, change in y displacement]}

我希望这些物体同时绕太阳公转,但是当物体离屏幕很远时,我会收到此错误消息:

G = 6.67384 * 10 ** -11                                              
scale = 10 ** 13                                                                             
speed = 1                 
global user_status
screen = Tk()
screen.title('Solar System' + ' - ' + user_status)
screen.geometry('1300x700')

ani = Canvas(screen, width=1300, height=700)
ani.pack()
ani.create_rectangle(0, 0, 1300, 700, fill='Black')

sunx = 636
suny = 343
sun = ani.create_oval(sunx-10, suny-10, sunx+10, suny+10, fill='yellow')

earthx = 746
earthy = 343

moonx = 747
moony = 343

bodies = {'sun':  [[sunx, suny],     [0, 0],                         1.989 * 10 ** 30 * speed / scale,      [0, 0]],
          'earth':[[earthx, earthy], [0, 347.3833062 * 1.033447099], 5.972 * 10 ** 24 * speed / scale,      [0, 0]],
          'moon': [[moonx, moony],   [0, 360],                       7.34767309 * 10 ** 22 * speed / scale, [0, 0]]
          }

body_names = []
for Ω in bodies.keys():
    body_names.append(Ω)

moon = ani.create_oval(moonx - 4, moony - 4, moonx + 4, moony + 4, fill='grey70')
earth = ani.create_oval(earthx-6, earthy-6, earthx+6, earthy+6, fill='sky blue')

timestep = 0.0001

while True:
    for i in range(len(body_names)):
        body1 = body_names[i]
        x1 = bodies[body1][0][0]
        y1 = bodies[body1][0][1]
        total_Fx = 0
        total_Fy = 0
        body1_mass = bodies[body1][2]
        for j in range(len(body_names)):
            body2 = body_names[j]
            if body1 != body2:
                x2 = bodies[body2][0][0]
                y2 = bodies[body2][0][1]
                body2_mass = bodies[body2][2]

                r = sqrt(((x1 - x2) ** 2) + ((y1 - y2) ** 2))
                rx = (x1 - x2)
                angle = (acos(rx/r))

                F = (G * body1_mass * body2_mass) / (r ** 2)
                Fx = F * cos(angle)
                Fy = F * sin(angle)

                total_Fx += Fx
                total_Fy += Fy

        ax = (total_Fx / body1_mass)
        ay = (total_Fy / body1_mass)

        ux = bodies[body1][1][0]
        uy = bodies[body1][1][1]

        vx = ux - (ax * timestep)
        if x1 <= sunx:
            vy = uy + (ay * timestep)
        else:
            vy = uy - (ay * timestep)

        sx = vx * timestep * speed
        sy = vy * timestep * speed

        bodies[body1][3][0] = sx
        bodies[body1][3][1] = sy

        bodies[body1][1][0] += vx
        bodies[body1][1][1] += vy

        bodies[body1][0][0] = x1 + sx
        bodies[body1][0][1] = y1 + sy
        print(bodies[body1][1], body1)

    move_e_x = bodies['earth'][3][0]
    move_e_y = bodies['earth'][3][1]
    ani.move(earth, move_e_x, move_e_y)

    move_m_x = bodies['moon'][3][0]
    move_m_y = bodies['moon'][3][1]
    ani.move(moon, move_m_x, move_m_y)

    ani.update()

我知道这可能不是很高效的代码,但是我所需要的只是如何使用这种方法的一些帮助。该问题似乎发生在位移或速度计算中。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

初始速度被接收两次:在每个循环中,都会调用和使用初始速度,因此将自身速度和新速度相加在一起,导致每次将其增加一倍以上。

现在将计算新增加的速度,然后仅将其添加到初始速度一次。行星现在运行。目前有9个绕太阳运行的天体。