并行化初值-边值问题(有限差分)

时间:2018-11-04 13:14:25

标签: python-3.x parallel-processing nested-loops

我正在运行一个模拟来求解对流扩散方程。我希望并行化计算偏导数的代码部分,以加快计算速度。这是我在做什么:

p1 = np.zeros((len(r), len(th)-1 ))  #The solution of the matrix

def func(i):
    pti = np.zeros(len(th)-1)
    for j in range (len(pti)):    

        abc = f(p1)  #Some function calculating the derivatives at each point
        pti[j] = p1[i][j] + dt*( abc )  #dt is some small float number

    return pti

#Setting the initial condition of the p1 matrix
for i in range(len(p1[:,0])):
    for j in range(len(p1[0])):


        p1[i][j] = 0.01

#Final loop calculating the integral by finite difference scheme

p = Pool(args.cores)
for k in range (0,args.iterations):  #This is integration in time


    p1=p.map(func,range(len(r)))   


print (p1)

我在这里面临的问题是我的p1矩阵在k的每次迭代之后都没有更新。最后,当我打印p1时,我得到了与初始化相同的矩阵。

此外,此代码的线性版本正在运行(但需要花费很长时间)。

1 个答案:

答案 0 :(得分:0)

好的,我自己解决了这个问题。显然是这样

p = Pool(args.cores)

在循环内

for k in range (0,args.iterations):

起到了作用。