1# file: bratu2dnpy.py 2 3def bratu2d(alpha, x, f): 4 # get 'exp' from numpy 5 from numpy import exp 6 # setup 5-points stencil 7 u = x[1:-1, 1:-1] # center 8 uN = x[1:-1, :-2] # north 9 uS = x[1:-1, 2: ] # south 10 uW = x[ :-2, 1:-1] # west 11 uE = x[2:, 1:-1] # east 12 # compute nonlinear function 13 nx, ny = x.shape 14 hx = 1.0/(nx-1) # x grid spacing 15 hy = 1.0/(ny-1) # y grid spacing 16 f[:,:] = x 17 f[1:-1, 1:-1] = \ 18 (2*u - uE - uW) * (hy/hx) \ 19 + (2*u - uN - uS) * (hx/hy) \ 20 - alpha * exp(u) * (hx*hy) 21