我正在尝试插入一些以这种格式给出的向量点(只取完整数据的一部分):
x y u(velocity in x dir) v(velocity in y direction)
0.96875 -0.96875 0.002359896607482337 0.0023598966074823385
0.96875 -0.90625 0.006989000319266727 0.021102925775399232
0.96875 -0.84375 0.011349520668258234 0.05786870001152182
0.96875 -0.78125 0.015273885302853092 0.11124433123043967
到目前为止,我能够对这些矢量点进行插值,scipy.interpolate
以获取特定点的速度(u 和 v),并将其应用于公式以研究其运动。问题是,我实际上应该处理 60,000 个点并将其绘制为不同时间步长的图表(例如 t=0.1,t=0.2..)。该代码运行良好,但我对我当前的算法并不满意,因为绘制 1000 个粒子至少需要 3 分钟,而且更不用说绘制 60,000 个粒子需要多少时间了。我现在拥有的代码如下:
def estimate_next_position(self, type=1):
for subplot in self.subplots:
row, col = self.subplot_position[subplot][0][0],self.subplot_position[subplot][0][1]
self.time += 0.1
for particle_type in self.type:
self.calculation(particle_type, self.h)
self.plot(particle_type, row, col)
def calculation(self,particles, time_step):
for i, particle in enumerate(particles):
lang = np.random.normal(loc=0, scale=1, size=2)
x, y = particle[0], particle[1]
u, v = self.bilinear_interpolation(x,y)
# Euler's equation using lambda function
euler = lambda coordinate, random, velocity: coordinate + 2 * np.sqrt(2 * self.D) * np.sqrt(time_step) * random + velocity * time_step
next_pos_x = euler(x, lang[0], u)
next_pos_y = euler(y, lang[1], v)
# Creates a "wall" to avoid the particles from moving to places it shouldnt
next_pos_x, next_pos_y = self.boundary_conditions(next_pos_x, next_pos_y)
# Reassign the current particle from substance 1 (in this case), to a new position
particles[i] = (next_pos_x, next_pos_y)
def bilinear_interpolation(self,x,y):
xx = x
yy = y
xx, yy = np.meshgrid(xx, yy)
points = np.transpose(np.vstack((self.x_data, self.y_data)))
u_interp = interpolate.griddata(points, self.u_data, (xx, yy), method='linear')
v_interp = interpolate.griddata(points, self.v_data, (xx, yy), method='linear')
return float(u_interp), float(v_interp)
多变的
self.type = Handle different types of substance
euler = calculation I am applying
u, v = velocity vector at a specific point
particles = a list of x,y coordinate of the point (eg [(-1,1), (0.01, 0.34),...])
bilinear_interpolation = 2D interpolation of the vector velocity data
我看到我面临的主要问题是我实际上是在迭代数千个粒子,在循环本身内插入向量点,这使得它非常慢。有什么办法可以优化这个,或者如果可能的话,很高兴知道其他一些可以帮助解决这个问题的库或函数。