如何在VPython 2.7.9中设计正确的循环以更新绕杆运转的球的位置?

时间:2017-10-24 00:45:46

标签: python physics vpython

我遇到了一个问题,我想设计一个代码来模拟在VPython 2.7.9中围绕非球形物体(如杆)的球。

要回答这个问题,重要的是要知道对于空间中的不规则物体(非球体的物体),你需要将它们切割成块并单独考虑它们以获得自己的质量。

在这个问题中,杆的质量均匀分布,并且由铁制成。杆的长度为20米,半径为0.1米。在它周围运行的粒子是你想要的任何质量,重要的是它根据它所拥有的质量对杆进行轨道运动。

我设计了一个无效的代码,但我无法发现问题(使用VIDLE来运行它):

from __future__ import division
from visual import *
from visual.graph import *

#Initializing
G = 6.67e-11 #Universal constant of gravitation


#Rod
L = 20 #Lenght of rod
rod_radius = 0.1
rod_pos = vector(-10,0,0)
rod_axis = vector(L,0,0)
rod_density = 7700
rod_volume = L*pi*(rod_radius**2)
rod_mass = rod_density*rod_volume
n = 1000 #Chunks the rod is cut into


#Chunks
chunk_lenght = L/n
chunk_volume = chunk_lenght*pi*(rod_radius**2)
chunk_mass = rod_density*chunk_volume
chunk_pos_x = -10 #initial position of the first chunk

#Particle
particle_pos = vector(0,5,0)
particle_mass = 1
r = particle_pos - vector(0,0,0)
particle_vel = sqrt(G*chunk_mass/mag(r))*norm(r) #velocity of the particle
particle_mom = particle_mass*particle_vel #momentum of the particle
particle_radius = 0.5


#Objects
rod = cylinder(pos=rod_pos, axis=rod_axis, radius=rod_radius, 
color=color.white)

particle = sphere(pos=particle_pos, radius=particle_radius, color=color.red, 
make_trail = True)


#Define Variables        
T = (2*pi*mag(r))/mag(particle_vel) #Period of a revolution

time = 0

dt = T/500

chunk_pos_list = [] #initializing list of positions of each chunk

r_list = [] #initializing list of distances from chunk to particle

M = 0

N = 0


#Main loop
while not(-10<=r.x <= 10 and -0.1<=r.y<=0.1 and -0.1<=r.z<=0.1):

    rate(1000)

    #Loop to generate list of the position of the chunks in the rod
    while chunk_pos_x <= 10:

        chunk_pos = vector(chunk_pos_x,0,0)

        chunk_pos_list.append(chunk_pos)

        chunk_pos_x = chunk_pos_x + chunk_lenght

    #Loop to generate list of initial distance "r" of the particle from each 
chunk of the rod
    while M<=n:

        r = chunk_pos_list[M]-particle.pos

        r_list.append(r)

        M = M + 1

    #Loop to generate update in position of the particle
    while N<=n+1:

        Force_G = ((-
G*particle_mass*chunk_mass)/mag2(r_list[N]))*norm(r_list[N])

        particle_mom = particle_mom + Force_G*dt

        particle_vel = particle_mom/particle_mass

        particle.pos = particle.pos + particle_vel*dt

        time = time + dt

        N = N + 1

        if N=n:

            N=0

1 个答案:

答案 0 :(得分:0)

在这里查看几个VPython演示程序,了解它们如何模拟轨道运行。

http://velasco.sites.truman.edu/vpython-demo/

特别关注&#34;反向r力轨道&#34;和#34;二进制星轨道&#34; vpython演示了解如何模拟轨道的想法。尝试在您的系统上运行这些演示程序。