[Numpy-discussion] How to speed up this function?

fsenkel at verizon.net fsenkel at verizon.net
Sun Dec 3 21:28:00 CST 2006

```Hello,

I'm taking a CFD class, one of the codes I wrote runs very slow. When I look at hotshot is says the function below is the problem. Since this is an explicit step, the for loops are only traversed once, so I think it's caused by memory usage, but I'm not sure if it's the local variables or the loop? I can vectorize the inner loop,  would declaring the data structures in the calling routine and passing them in be a better idea than using local storage?

I'm new at python and numpy, I need to look at how to get profiling information for the lines within a function.

Thank you,

Frank

PS
I tried to post this via google groups, but it didn't seem to go through, sorry if it ends up as multiple postings

def findw(wnext,wprior,phiprior,uprior,vprior):
#format here is x[i,j] where i's are rows, j's columns, use flipud() to get the
#print out consistent with the spacial up-down directions

#assign local names that are more
#inline with the class notation
w = wprior
phi = phiprior
u = uprior
v = vprior

#three of the BC are known so just set them
#symetry plane
wnext[0,0:gcols] = 0.0

#upper wall
wnext[gN,0:gcols] = 2.0/gdy**2 * (phi[gN,0:gcols] - phi[gN-1,0:gcols])

#inlet, off the walls
wnext[1:grows-1,0] = 0.0

upos = where(u>0)
vpos = where(v>0)

Sx = ones_like(u)
Sx[upos] = 0.0

Sy = ones_like(v)
Sy[vpos] = 0.0

uw = u*w
vw = v*w

#interior nodes
for j in range(1,gasizej-1):
for i in range(1,gasizei-1):

wnext[i,j] =( w[i,j] + gnu*gdt/gdx**2 * (w[i,j-1] - 2.0*w[i,j] + w[i,j+1]) +
gnu*gdt/gdy**2 * (w[i-1,j] - 2.0*w[i,j] + w[i+1,j]) -
(1.0 - Sx[i,j]) * gdt/gdx * (uw[i,j] - uw[i,j-1]) -
Sx[i,j] * gdt/gdx * (uw[i,j+1] - uw[i,j]) -
(1.0 - Sy[i,j]) * gdt/gdy * (vw[i,j] - vw[i-1,j]) -
Sy[i,j] * gdt/gdy * (vw[i+1,j] - vw[i,j]) )

##        print "***wnext****"
##        print "i: ", i, "j: ", j, "wnext[i,j]: ", wnext[i,j]

#final BC at outlet, off walls
wnext[1:grows-1,gM] = wnext[1:grows-1,gM-1]
```