零重力2d空间中粒子的优化引力计算 [英] Optimizing gravitation calculation for particles in a zero gravity 2d space

查看:109
本文介绍了零重力2d空间中粒子的优化引力计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用python创建了一个小的粒子可视化视图. 我计算零重力下2D空间中的粒子运动. 由于每个粒子都会根据粒子的质量和距离吸引所有其他粒子.

I've have created a small visualisation of particles in python. I'm caclulation the movement of particels in a 2D space with zero gravity. As each particle attracts all other particles based on the particle mass and distance.

我在pygame中进行了可视化,一切都按计划进行(带有计算),但是我需要极大地优化计算.如今,该系统可以以令人满意的帧速率计算约100-150个粒子.我将所有计算放在一个单独的线程中,这给了我更多但不是我想要的东西.

I made a visualsation in pygame and everything works as plan (with the caluclation), however i need to optimize the calculation extreamly. Today the system can calculate about 100-150 particles in a deacent framerate. I put all the calculation in a seperate thread that gave me some more but not nearly what i want.

我看过scipy和numpy,但由于我既不是科学家也不是Mathguru,所以我很困惑.看起来我处在正确的轨道上,但是我不知道如何做.

I've looked at scipy and numpy but since I'm no scientist or mathguru i just get confused. It looks like I'm on the right track but i have no clue howto.

我需要计算一个循环中必须要吸引的所有粒子上的所有引力. 而且由于我需要查找是否发生了碰撞,因此我必须重新进行同样的操作.

I need to calculate all the attraction on all particles i have to a loop in a loop. And since I need to find if any have collided, i have to do the same all over again.

编写这样的代码让我很伤心.

It breaks my heart to write that kind of code....

Numpy可以计算带有数组的数组,但是我还没有发现任何可以计算来自同一/另一个数组的所有项目的东西.有一个吗? 如果是这样,我可以创建并耦合两个数组并更快地进行计算,并且必须有一个函数可以从两个值匹配的数组中获取索引(Collitiondetect iow)

Numpy has the ability to calculate array with array, however i haven't found any what to calculate all items in array with all the items from same/another array. Is there one? If so i could create and couple of arrays and calculate much faster and there must be a function to get index from to 2 arrays where their values match (Collitiondetect iow)

这是今天的吸引力/冲突计算:

Here is todays attraction/collsion calculation:

class Particle:
    def __init__(self):
        self.x = random.randint(10,790)
        self.y = random.randint(10,590)
        self.speedx = 0.0
        self.speedy = 0.0
        self.mass = 4

#Attraction    
for p in Particles:
    for p2 in Particles:
        if p != p2:
            xdiff = P.x - P2.x
            ydiff = P.y - P2.y
            dist = math.sqrt((xdiff**2)+(ydiff**2))
            force = 0.125*(p.mass*p2.mass)/(dist**2)
            acceleration = force / p.mass
            xc = xdiff/dist
            yc = ydiff/dist
            P.speedx -= acceleration * xc
            P.speedy -= acceleration * yc
for p in Particles:
    p.x += p.speedx
    p.y += p.speedy

#Collision
for P in Particles:
   for P2 in Particles:
        if p != P2:
            Distance = math.sqrt(  ((p.x-P2.x)**2)  +  ((p.y-P2.y)**2)  )
            if Distance < (p.radius+P2.radius):
                p.speedx = ((p.mass*p.speedx)+(P2.mass*P2.speedx))/(p.mass+P2.mass)
                p.speedy = ((p.mass*p.speedy)+(P2.mass*P2.speedy))/(p.mass+P2.mass)
                p.x = ((p.mass*p.x)+(P2.mass*P2.x))/(p.mass+P2.mass)
                p.y = ((p.mass*p.y)+(P2.mass*P2.y))/(p.mass+P2.mass)
                p.mass += P2.mass
                p.radius = math.sqrt(p.mass)
                Particles.remove(P2)

推荐答案

您可以首先尝试使用复数:在这种形式主义中,相关的引力和动力学公式非常简单,并且还可以相当快(因为NumPy可以在内部进行计算,而不是分别处理x和y坐标).例如,在z和z'处的两个粒子之间的作用力很简单:

You can first try to work with complex numbers: the relevant gravitation and dynamics formulas are very simple in this formalism, and can also be quite fast (because NumPy can do the calculation internally, instead of you handling separately x and y coordinates). For instance, the force between two particules at z and z' is simply:

(z-z')/abs(z-z')**3

对于所有z/z'对,NumPy都可以非常快速地计算出这样的数量.例如,所有zz'值的矩阵都可以简单地从坐标的一维数组Z作为Z-Z[:, numpy.newaxis]获得(对角线项[z = z']在计算1/abs(z-z')**3时确实需要特别注意:设置为零).

NumPy can calculate such a quantity very quickly, for all z/z' pairs. For instance, the matrix of all z-z' values is simply obtained from the 1D array Z of coordinates as Z-Z[:, numpy.newaxis] (the diagonal terms [z=z'] do require some special care, when calculating 1/abs(z-z')**3: they should be set to zero).

关于时间演化,您当然可以使用 SciPy的快速微分方程子程序:它们比逐步进行Euler积分要精确得多.

As for the time evolution, you can certainly use SciPy's fast differential equation routines: they are much more precise than the step by step Euler integration.

在任何情况下,深入研究NumPy都是非常有用的,特别是如果您打算进行科学计算时,因为NumPy非常快.

In any case, delving into NumPy would be very useful, especially if you plan to do scientific calculations, as NumPy is very fast.

这篇关于零重力2d空间中粒子的优化引力计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆