我目前正在尝试模拟一个弹跳的盒子中的许多粒子。
我已经考虑了@ kalhartt的建议,这是用于初始化框内粒子的改进代码:
import numpy as np
import scipy.spatial.distance as d
import matplotlib.pyplot as plt
# 2D container parameters
# Actual container is 50x50 but chose 49x49 to account for particle radius.
limit_x = 20
limit_y = 20
#Number and radius of particles
number_of_particles = 350
radius = 1
def force_init(n):
# equivalent to np.array(list(range(number_of_particles)))
count = np.linspace(0, number_of_particles-1, number_of_particles)
x = (count + 2) % (limit_x-1) + radius
y = (count + 2) / (limit_x-1) + radius
return np.column_stack((x, y))
position = force_init(number_of_particles)
velocity = np.random.randn(number_of_particles, 2)
初始化的头寸如下:
我初始化粒子后,我想在每个时间步更新它们。更新代码立即遵循前面的代码,如下所示:
# Updating
while np.amax(abs(velocity)) > 0.01:
# Assume that velocity slowly dying out
position += velocity
velocity *= 0.995
#Get pair-wise distance matrix
pair_dist = d.cdist(position, position)
pair_d = pair_dist<=4
#If pdist [i,j] is <=4 then the particles are too close and so treat as collision
for i in range(len(pair_d)):
for j in range(i):
# Only looking at upper triangular matrix (not inc. diagonal)
if pair_d[i,j] ==True:
# If two particles are too close then swap velocities
# It's a bad hack but it'll work for now.
vel_1 = velocity[j][:]
velocity[j] = velocity[i][:]*0.9
velocity[i] = vel_1*0.9
# Masks for particles beyond the boundary
xmax = position[:, 0] > limit_x
xmin = position[:, 0] < 0
ymax = position[:, 1] > limit_y
ymin = position[:, 1] < 0
# flip velocity and assume that it looses 10% of energy
velocity[xmax | xmin, 0] *= -0.9
velocity[ymax | ymin, 1] *= -0.9
# Force maximum positions of being +/- 2*radius from edge
position[xmax, 0] = limit_x-2*radius
position[xmin, 0] = 2*radius
position[ymax, 0] = limit_y-2*radius
position[ymin, 0] = 2*radius
更新它并让它运行完成后,我得到了这个结果:
这比以前好多了,但仍然有太近的补丁 - 例如:
太靠近了。我认为更新工作......并且感谢@kalhartt我的代码更好更快(我学到了一些关于numpy ...... props @kalhartt的东西),但我仍然不知道它在哪里搞砸了。我已经尝试用最后的成对距离或最后的position +=velocity
来改变实际更新的顺序,但无济于事。我添加了* 0.9以使整个事物更快地消失并且我用4来尝试它以确保2 *半径(= 2)不是太紧的标准......但似乎没有任何效果。
任何和所有帮助将不胜感激。
答案 0 :(得分:3)
只有两个错别字挡在你的路上。首先for i in range(len(positions)/2):
只迭代一半以上的粒子。这就是为什么一半的粒子保持在x界限(如果你观察大的迭代它更清晰)。其次,第二个y条件应该是最小值(我假设)position[i][1] < 0
。以下块用于为我绑定粒子(我没有使用碰撞代码进行测试,因此可能存在问题)。
for i in range(len(position)):
if position[i][0] > limit_x or position[i][0] < 0:
velocity[i][0] = -velocity[i][0]
if position[i][1] > limit_y or position[i][1] < 0:
velocity[i][1] = -velocity[i][1]
顺便说一句,尽可能利用numpy来消除循环。它更快,更有效,而且在我看来更具可读性。例如,force_init
看起来像这样:
def force_init(n):
# equivalent to np.array(list(range(number_of_particles)))
count = np.linspace(0, number_of_particles-1, number_of_particles)
x = (count * 2) % limit_x + radius
y = (count * 2) / limit_x + radius
return np.column_stack((x, y))
你的边界条件如下:
while np.amax(abs(velocity)) > 0.01:
position += velocity
velocity *= 0.995
# Masks for particles beyond the boundary
xmax = position[:, 0] > limit_x
xmin = position[:, 0] < 0
ymax = position[:, 1] > limit_y
ymin = position[:, 1] < 0
# flip velocity
velocity[xmax | xmin, 0] *= -1
velocity[ymax | ymin, 1] *= -1
最后注意,用position[xmax, 0] = limit_x; position[xmin, 0] = 0
之类的方法将位置硬夹到边界框可能是个好主意。可能存在速度较小且盒子外部的粒子将被反射但在下一次迭代中不会进入内部的情况。所以它只会坐在盒子外面永远反映出来。
编辑:碰撞
碰撞检测是一个更难的问题,但让我们看看我们能做些什么。让我们来看看你当前的实现。
pair_dist = d.cdist(position, position)
pair_d = pair_dist<=4
for i in range(len(pair_d)):
for j in range(i):
# Only looking at upper triangular matrix (not inc. diagonal)
if pair_d[i,j] ==True:
# If two particles are too close then swap velocities
# It's a bad hack but it'll work for now.
vel_1 = velocity[j][:]
velocity[j] = velocity[i][:]*0.9
velocity[i] = vel_1*0.9
总的来说,一个非常好的方法,cdist
将有效地计算距离
在各组点之间,您可以找到哪些点与pair_d = pair_dist<=4
发生冲突。
嵌套for循环是第一个问题。我们需要迭代True
pair_d
j > i
的值for j in range(i)
。首先,您的代码实际上使用j < i
迭代下三角区域,以便0
,在此实例中并不特别重要,因为i,j对不会重复。但是Numpy有两个我们可以使用的内置函数,np.triu让我们将对角线下面的所有值设置为pair_dist = d.cdist(position, position)
pair_d = pair_dist<=4
for i in range(len(pair_d)):
for j in range(i+1, len(pair_d)):
if pair_d[i, j]:
...
,而np.nonzero将给出矩阵中非零元素的索引。所以这个:
pair_dist = d.cdist(position, position)
pair_d = np.triu(pair_dist<=4, k=1) # k=1 to exclude the diagonal
for i, j in zip(*np.nonzero(pair_d)):
...
相当于
position[j] - position[i]
第二个问题(正如您所指出的)是速度只是切换和缩放而不是反射。我们真正想要做的是否定并沿着连接它们的轴缩放每个粒子速度的分量。请注意,要做到这一点,我们需要连接它们的向量cdist
和连接它们的向量的长度(我们已经计算过)。所以不幸的是,cdist
计算的一部分会重复出现。让我们使用diff
退出,而不是自己做。这里的目标是创建两个数组norm
和diff[i][j]
,其中diff
是一个从粒子i指向j的向量(所以norm[i][j]
是一个3D数组)和{{1粒子i和j之间的距离。我们可以像numpy这样做:
nop = number_of_particles
# Give pos a 3rd index so we can use np.repeat below
# equivalent to `pos3d = np.array([ position ])
pos3d = position.reshape(1, nop, 2)
# 3D arras with a repeated index so we can form combinations
# diff_i[i][j] = position[i] (for all j)
# diff_j[i][j] = position[j] (for all i)
diff_i = np.repeat(pos3d, nop, axis=1).reshape(nop, nop, 2)
diff_j = np.repeat(pos3d, nop, axis=0)
# diff[i][j] = vector pointing from position[i] to position[j]
diff = diff_j - diff_i
# norm[i][j] = sqrt( diff[i][j]**2 )
norm = np.linalg.norm(diff, axis=2)
# check for collisions and take the region above the diagonal
collided = np.triu(norm < radius, k=1)
for i, j in zip(*np.nonzero(collided)):
# unit vector from i to j
unit = diff[i][j] / norm[i][j]
# flip velocity
velocity[i] -= 1.9 * np.dot(unit, velocity[i]) * unit
velocity[j] -= 1.9 * np.dot(unit, velocity[j]) * unit
# push particle j to be radius units from i
# This isn't particularly effective when 3+ points are close together
position[j] += (radius - norm[i][j]) * unit
...
由于这篇文章已经足够长了,我的修改代码here is a gist。