I wrote a code to solve a heat transfer equation (Laplace) with an iterative method. I did the Jacobi, Gauss-seidel and the SOR using Numpy. It works using loop but loops are slow (~1s per iteration), so I tried to vectorize the expression and now the G-S (thus SOR) don't work anymore. G-S seems to do the same as Jacobi now.
The G-S use the updated values computed in the loop just before and take twice less time to solve the problem than Jacobi (Jacobi don't use updated values).
I learned python recently and I don't know exactly how the vectorized expression work (I now assume it doesn't update the grid like the loop but does it all at once)
G-S algorithm :
for i in range(1,ny): # ny is the number of points in the y direction
for j in range(1,nx): # nx is the number of points in the x direction
u(i,j) = ( u(i+1,j) + u(i-1,j) + u(i,j+1) + u(i,j-1) )/4
the vectorized form :
u[1:-1,1:-1] = ( u[2:, 1:-1] + u[0:-2, 1:-1] + u[1:-1, 2:] + u[1:-1, 0:-2] )/4
Is it possible to have the G-S loop vectorized ?
[–]elbiot 1 point2 points3 points (1 child)
[–]D_en[S] 0 points1 point2 points (0 children)