Python 用内微分方程并行化for循环

Python 用内微分方程并行化for循环,python,for-loop,parallel-processing,differential-equations,Python,For Loop,Parallel Processing,Differential Equations,我有一个Python3代码。我想并行化一个很长的for循环,其中一个函数(solve_ivp)集成了一个微分方程组。循环在初始条件之上,包含在称为“esemble”的矩阵行中 我已经尝试过使用Numba,但问题是scipy中名为“solve_ivp”的函数不受Numba支持 你知道我如何将这段代码并行化以使其更快吗 注意:我需要跟踪所有的轨迹(正如solve_ivp的参数中t_eval=tt所指出的) 你完全正确,我现在添加Python标记!请提供一份报告。目前,尚不清楚esemble(集成?)

我有一个Python3代码。我想并行化一个很长的for循环,其中一个函数(solve_ivp)集成了一个微分方程组。循环在初始条件之上,包含在称为“esemble”的矩阵行中

我已经尝试过使用Numba,但问题是scipy中名为“solve_ivp”的函数不受Numba支持

你知道我如何将这段代码并行化以使其更快吗

注意:我需要跟踪所有的轨迹(正如solve_ivp的参数中t_eval=tt所指出的)


你完全正确,我现在添加Python标记!请提供一份报告。目前,尚不清楚
esemble
(集成?)的形状是什么,也不清楚如何使用/存储结果
xx00
。请参阅。但要检查参数的有用性。对于标准测试问题,1e-4的步长可能太小,请考虑将其减小到1e-3甚至1e-2。请注意,采样点序列的步长不影响内部步长控制,内部步长控制仅由atol和rtol参数控制。@UmbertoTomasini您还可以为esemble、N、x0等添加示例值吗。?它们不必是现实的,只要足以让代码运行即可。您还可以在
lorenz96(t,x)
函数中使用Numba。solve_ivp不支持低级可调用,但这应该会提供显著的加速。
mm=100000 #number of initial conditions
m=500000  #interval of integration
step=0.0001  #integration step
N=36
F=8
#----------------------------------------
#differential equations to be integrated
def lorenz96(t,x):
"""Lorenz 96 model."""
# Compute state derivatives
d = np.zeros(N)

# First the 3 edge cases: i=1,2,N
d[0] = (x[1] - x[N-2]) * x[N-1] - x[0]
d[1] = (x[2] - x[N-1]) * x[0] - x[1]
d[N-1] = (x[0] - x[N-3]) * x[N-2] - x[N-1]
# Then the general case
for i in range(2, N-1):
    d[i] = (x[i+1] - x[i-2]) * x[i-1] - x[i]
# Add the forcing term
d = d + F               

# Return the state derivatives
return d
#----------------------------------------
#x0 part (initial transient)

x0 = np.zeros(N)
for i in range(0, N):
    x0[i]=np.random.uniform()*(2*0.4) 
esemble = np.zeros((mm,N))

#time lag iniziale
lag = int((50.0)/step)
tlag = np.arange(0.0, float(lag*step), step)    #time steps

b=0
x = solve_ivp(lorenz96,[0.0,float(lag*step)], x0, t_eval=tlag)

for j in range(0,N):
    x0[j]=x.y[j,lag-1] #end lag as CI
    esemble[0,j] = x.y[j,lag-1]

#---------------------------------------
#Creating the esemble of initial conditions from a long run of the differential system
m_es = int((10.0)/step) # distanza membri esemble
t_es = np.arange(0.0, float(m_es*step), step)

for ll in range(1,mm): 

    x = solve_ivp(lorenz96,[0.0,float(m_es*step)], x0, t_eval=t_es)
    for j in range(0,N):        
        x0[j]=x.y[j,m_es-1] #end as CI for next cycle
        esemble[ll,j] = x.y[j,m_es-1]

### Heading---This the loop to be parallelized ##

Gamma_psi=np.zeros((2,m))#array which will contain the result of the integration along the time interval (0, m*step)

for i in range(mm): #pick the ensemble member I

    xx0 = np.zeros(N) 

    for j in range(0,N):
        xx0[j]=esemble[i,j] #pick the start for each ensemble member I

    #FORWARD m STEPS
    tt = np.arange(0.0, float(m*step), step) #interval of integration   
    xx00 = solve_ivp(lorenz96,[0.0,float(m*step)], xx0, t_eval=tt)

    #saving result for just two dynamical variables
    loc=1
    for p in range(0,m):    #cycle over time steps 

        Gamma_psi[0,p] = Gamma_psi[0,p]+ (xx00.y[loc,p])
        Gamma_psi[1,p] = Gamma_psi[1,p]+ (xx00.y[loc-1,p] )

Gamma_psi = Gamma_psi/(float(mm)) #NB
#---------------------------------------------