gpt4 book ai didi

python - 使用 Scipy 加速许多粒子的路径计算

转载 作者:行者123 更新时间:2023-11-28 19:03:53 25 4
gpt4 key购买 nike

我正在尝试使用 Python 计算许多粒子位置的演变。最终,我将在大约 100000 个时间步长内计算出许多粒子(大约 10000 个)。由于我正在不惜一切代价避免使用 Fortran,因此我正在努力加快这一过程。

我要解的方程是

d X_i/dt = u
d Y_i/dt = v

理想情况下,我会使用二维数组:一个在粒子之间切换,另一个在 xy 之间切换。因此,如果我有 100 个粒子,我就会有一个 100x2 数组。

部分问题是因为 scipy.integrate.odeint 只需要一维数组,所以我必须展平我的初始条件,将它拆分到我的导数函数 (RHS_im),然后输出的时候再压平,比较慢(这个占RHS_im调用的20%左右)。

我可以用丑陋的方式做到这一点。这是我提出的 MWE

import numpy as np
from scipy.interpolate import RegularGridInterpolator
from scipy.integrate import odeint

x=np.arange(2.5, 500, 5)
y=np.arange(2.5, 500, 5)

X, Y = np.meshgrid(x, y, indexing='xy')

U=np.full_like(X, -0.1)
V=0.2*np.sin(2*np.pi*X/200)

tend=100
dt=1
tsteps=np.arange(0, tend, dt)

#------
# Create interpolation
U_int = RegularGridInterpolator((x, y), U.T)
V_int = RegularGridInterpolator((x, y), V.T)
#------

#------
# Initial conditions
x0=np.linspace(200,300,5)
y0=np.linspace(200,300,5)
#------


#------
# Calculation for many
def RHS_im(XY, t):
X, Y = np.split(XY, 2, axis=0)
pts=np.array([X,Y]).T
return np.concatenate([ U_int(pts), V_int(pts) ])
XY0 = np.concatenate([x0, y0])
XY, info = odeint(RHS_im, XY0, tsteps[:-1], args=(), hmax=dt, hmin=dt, atol=0.1, full_output=True)
X, Y = np.split(XY, 2, axis=1)

有没有办法避免拆分过程?

此外,虽然我选择了odeint来集成这个系统,但我并不 promise 这个选择。如果有另一个函数可以更快地执行此操作(即使使用简单的欧拉方案),我会很容易地进行更改。我只是没有找到任何。 (插值方案也是如此,这大约是 RHS_im 所用时间的 80%)。

编辑

稍微改进了拆分过程(通过使用np.split),但是整个程序仍然需要改进。

最佳答案

感谢您对 MWE 的澄清。通过将 U_intV_int 组合成 UV_int 并更改 XY 的布局,我能够让 MWE 的运行速度提高大约 50%,这样可以使用 np.reshape 代替 np.split。这两个变化都有助于就地和连续地访问内存。

x_grid=np.arange(2.5, 500, 5)
y_grid=np.arange(2.5, 500, 5)

x_mesh, y_mesh = np.meshgrid(x_grid, y_grid, indexing='xy')

u_mesh=(np.full_like(x_mesh, -0.1))
v_mesh=(0.2*np.sin(2*np.pi*x_mesh/200))
uv_mesh = np.stack([u_mesh.T, v_mesh.T], axis=-1)

tend=100
dt=1
tsteps=np.arange(0, tend, dt)

#------
# Create interpolation
UV_int = RegularGridInterpolator((x_grid, y_grid), uv_mesh)
#------

#------
# Initial conditions
npart = 5
x0=np.linspace(200,300,npart)
y0=np.linspace(200,300,npart)

XY_pair = np.reshape(np.column_stack([x0, y0]), (2*npart))
#-----


def RHS_im(XY, t):
return np.reshape(UV_int(np.reshape(XY, (npart, 2))), (npart*2))

XY, info = odeint(RHS_im, XY_pair, tsteps[:-1], args=(), hmax=dt, hmin=dt, atol=0.1, full_output=True)

关于python - 使用 Scipy 加速许多粒子的路径计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49333043/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com