sum(array,1)给出' nan'在Python中

时间:2014-01-29 06:47:17

标签: python sockets file-io multidimensional-array physics

首先我知道nan代表“不是数字”,但我不确定我的代码中是如何得到无效数字的。我正在做的是使用python脚本读取一个文件的矢量列表(x,y,z),然后将其转换为一个长数组的值,但如果我不使用该文件,我做一个为生成随机数的循环我没有得到任何'nan'。

在此之后我使用牛顿万有引力计算星星的位置,F = GMm / r ^ 2来计算位置,然后数据通过套接字服务器发送到我开发用于观看模拟的c#可视化软件。不幸的是,我的python脚本进行计算只会让你很麻烦。

    poslist = []
    plist = []
    mlist = []
    lineList = []
    coords = []

    with open("Hyades Vectors.txt", "r") as text_file:
        content = text_file.readlines()
        #remove /n
        for i in range(len(content)):
            for char in "\n":
                line = content[i].replace(char,"")
            lineList.append(line)
        lines = array(lineList)

        #split "   " within each line
        for i in range(len(lines)):
            coords.append(lines[i].split("   "))
        coords = array(coords)

        #convert coords string to integer
        for i in range(len(coords)):
            x = np.float(coords[i,0])
            y = np.float(coords[i,1])
            z = np.float(coords[i,2])
            poslist.append((x,y,z))

    pos = array(poslist)    

经常在第二次经历这个循环之后发送nan'

        vcm = sum(p)/sum(m) #velocity of centre mass
        p = p-m*vcm #make total initial momentum equal zero
        Myr = 8.4
        dt = 1
        pos = pos-(p/m)*(dt/2.) #initial half-step

        finished = False
        while not finished:         # or NBodyVis.Oppenned() == False

            r = pos-pos[:,newaxis] #all pairs of star-to-star vectors
            for n in range(Nstars):
                r[n,n] = 1e6 #otherwise the self-forces are infinite
            rmag = sqrt(sum(square(r),-1)) #star-to star scalar distances
            F = G*m*m[:,newaxis]*r/rmag[:,:,newaxis]**3 # all force pairs

            for n in range(Nstars):
                F[n,n] = 5  # no self-forces
            p = p+sum(F,1)*dt           #sum(F,1) is where i get a nan!!!!!!!!!!!!!!!!

            pos -= (p/m)*dt

            if Time <= 0:
                finished = True
            else:
                Time -= 1

我做错了什么??????我不完全理解nans,但是如果我的可视化软件是读南,我就不能拥有它们,因为那时没有任何东西可以用于视觉效果。我知道错误是总和(F,1)我去了并打印了所有内容,直到我得到了一个纳,这就是在哪里,但是如何从总结中得到一个纳米。以下是我正在阅读的文本文件的哪一部分:

    51.48855   4.74229   -85.24499
    121.87149   11.44572   -140.79644
    59.81673   68.8417   18.76767
    31.95567   37.23007   6.59515
    29.81066   34.76371   6.18374
    41.35333   49.52844   14.12314
    32.10481   38.46982   7.96628
    48.13239   60.4019   37.45474
    26.37793   34.53385   15.9054
    76.02468   103.98826   25.96607
    51.52072   71.17618   32.09829

请帮助

0 个答案:

没有答案