2017-08-07 71 views
0

我从这里找到了很多次解决我的问题的解决方案,但这一次我完全感到困惑。我不知道我的代码有什么问题。Vpython greyscreen crash

我做了一个代码,用Vpython创建带有带电粒子的盒子。当我启动程序时,我只能看到一个灰色的屏幕,程序崩溃。没有错误信息,没有。

from visual import * 
from random import * 

def electronizer(num): 
    list = [] 
    electron_charge = -1.60217662e-19 
    electron_mass = 9.10938356e-31 
    for i in range(num): 
     another_list = [] 
     e = sphere(pos=(random(), random(),random()), radius=2.818e-15, 
color=color.cyan) 
     e.v = vector(random(), random(), random()) 
     another_list.append(e) 
     another_list.append(e.v) 
     another_list.append(electron_charge) 
     another_list.append(electron_mass) 
     list.append(another_list) 
    return list 

def protonizer(num): 
    list = [] 
    proton_charge = 1.60217662e-19 
    proton_mass = 1.6726219e-27 
    for i in range(num): 
     another_list = [] 
     p = sphere(pos=(random(), random(),random()), radius=0.8408739e-15, color=color.red) 
     p.v = vector(random(), random(), random()) 
     another_list.append(p) 
     another_list.append(p.v) 
     another_list.append(proton_charge) 
     another_list.append(proton_mass) 
     list.append(another_list) 
    return list 

def cross(a, b): 
    c = vector(a[1]*b[2] - a[2]*b[1], 
     a[2]*b[0] - a[0]*b[2], 
     a[0]*b[1] - a[1]*b[0]) 

    return c 

def positioner(work_list): 
    k = 8.9875517873681764e3 #Nm2/C2 
    G = 6.674e-11 # Nm2/kg2 
    vac_perm = 1.2566370614e-6 # H/m 
    pi = 3.14159265 
    dt = 0.1e-3 
    constant = 1 
    force = vector(0,0,0) 
    for i in range(len(work_list)): 
     for j in range(len(work_list)): 
      if i != j: 
       r = work_list[i][0].pos - work_list[j][0].pos 
       r_mag = mag(r) 
       r_norm = norm(r) 
       F = k * ((work_list[i][2] * work_list[j][2])/(r_mag**2)) * r_norm 
       force += F 

       B = constant*(vac_perm/4*pi) * (cross(work_list[j][2] * work_list[j][1], norm(r)))/r_mag**2 
       F = cross(work_list[i][2] * work_list[i][1], B) 
       force += F 

       F = -(G * work_list[i][3] * work_list[j][3])/r_mag**2 * r_norm 
       force += F 

     acceleration = force/work_list[i][3] 
     difference_in_velocity = acceleration * dt 
     work_list[i][1] += difference_in_velocity 
     difference_in_position = work_list[i][1] * dt 
     work_list[i][0].pos += difference_in_position 

     if abs(work_list[i][0].pos[0]) > 2.5e-6: 
      work_list[i][1][0] = -work_list[i][1][0] 
     elif abs(work_list[i][0][1]) > 2.5e-6: 
      work_list[i][1][1] = -work_list[i][1][1] 
     elif abs(work_list[i][0][2]) > 2.5e-6: 
      work_list[i][1][2] = -work_list[i][1][2] 
return work_list 

box = box(pos=(0, 0, 0), length = 5e-6, width = 5e-6, height = 5e-6, opacity = 0.5) 

protons_num = raw_input("number of protons: ") 
electrons_num = raw_input("number of electrons: ") 
list_of_electrons = electronizer(int(electrons_num)) 
list_of_protons = protonizer(int(protons_num)) 
work_list = list_of_electrons + list_of_protons 

while True: 
    work_list = positioner(work_list) 

回答

0

您应该在VPython.org论坛上提问您的问题,VPython专家会在这里讨论您的问题并回答您的问题。你应该提到你正在使用哪个操作系统,以及你正在使用哪个版本的python。从你的代码中,我发现你使用的是经典的VPython。 VPython 7的更新版本刚刚问世,但VPython语法已更改。