Search code examples
python-3.xparallel-processingnested-loops

Parallellizing Intial-boundary value problem (Finite difference)


I am running a simulation to solve the advection diffusion equation. I wish to parallelize the part of the code where I calculate the partial derivatives so as to speed up my computation. Here is what I am doing:

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)

The problem that I am facing here is that my p1 matrix is not updating after each iteration in k. In the end when I print p1 I get the same matrix that I initialized.

Also, the linear version of this code is working (but it takes too long).


Solution

  • Okay I solved this myself. Apparently putting the line

    p = Pool(args.cores)
    

    inside the loop

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

    does the trick.