POISSON2D Function
Solves Poisson’s or Helmholtz’s equation on a two-dimensional rectangle using a fast Poisson solver based on the HODIE finite-difference scheme on a uniform mesh.
Usage
result = POISSON2D(rhs_pde, rhs_bc, coef_u, nx, ny, ax, bx, ay, by, bc_type)
Input Parameters
rhs_pde—Scalar string specifying the name of the user-supplied function to evaluate the right-hand side of the partial differential equation at a scalar value x and scalar value y.
rhs_bc—Scalar string specifying the name of the user-supplied function to evaluate the right-hand side of the boundary conditions, on side side, at scalar value x and scalar value y. The value of side will be one of the integer values shown in Table 6-2: Integer Values.
 
Integer Values
Integer
Side
0
right side
1
bottom side
2
left side
3
top side
coef_u—Value of the coefficient of u in the differential equation.
nx—Number of grid lines in the x-direction. nx must be at least 4. See the Discussion section for further restrictions on nx.
ny—Number of grid lines in the y-direction. ny must be at least 4. See the Discussion section for further restrictions on ny.
ax—The value of x along the left side of the domain.
bx—The value of x along the right side of the domain.
ay—The value of y along the bottom of the domain.
by—The value of y along the top of the domain.
bc_type—One-dimensional array of size 4 indicating the type of boundary condition on each side of the domain or that the solution is periodic. The sides are numbered as shown in Table 6-3: Side Numbering.
 
Side Numbering
Array
Side
Location
bc_type(0)
right
x = bx
bc_type(1)
bottom
y = ay
bc_type(2)
left
x = ax
bc_type(3)
top
y = by
The three possible boundary condition types are shown in Table 6-4: Boundary Condition Types.
 
Boundary Condition Types
Type
Condition
bc_type(i) = 1
Dirichlet condition. Value of u is given.
bc_type(i) = 2
Neuman condition. Value of du/dx is given (on the right or left sides) or du/dy (on the bottom or top of the domain).
bc_type(i) = 3
Periodic condition.
Returned Value
result—Two-dimensional array of size nx by ny containing solution at the grid points.
Input Keywords
Double—If present and nonzero, double precision is used.
Order—Order of accuracy of the finite-difference approximation. It can be either 2 or 4. Default: Order = 4
Discussion
Let c = coef_u, ax = ax, bx = bx, ay = ay, by = by, nx = nx and ny = ny.
POISSON2D is based on the code HFFT2D by Boisvert (1984). It solves the equation:
on the rectangular domain (ax, bx) × (ay, by) with a user-specified combination of Dirichlet (solution prescribed), Neumann (first-derivative prescribed), or periodic boundary conditions. The sides are numbered clockwise, starting with the right side, as shown in Figure 6-8: Side Numbering.
 
Figure 6-8: Side Numbering
When c = 0 and only Neumann or periodic boundary conditions are prescribed, then any constant may be added to the solution to obtain another solution to the problem. In this case, the solution of minimum  -norm is returned.
The solution is computed using either a second-or fourth-order accurate finite-difference approximation of the continuous equation. The resulting system of linear algebraic equations is solved using fast Fourier transform techniques. The algorithm relies on the fact that nx – 1 is highly composite (the product of small primes). For details of the algorithm, see Boisvert (1984). If nx – 1 is highly composite then the execution time of POISSON2D is proportional to nxny log2 nx. If evaluations of p(x, y) are inexpensive, then the difference in running time between Order = 2 and Order = 4 is small.
The grid spacing is the distance between the (uniformly spaced) grid lines. It is given by the formulas hx = (bxax)/(nx – 1) and hy = (byay)/(ny – 1). The grid spacings in the x and y directions must be the same, i.e., nx and ny must be such that hx is equal to hy. Also, as noted above, nx and ny must be at least 4. To increase the speed of the fast Fourier transform, nx – 1 should be the product of small primes. Good choices are 17, 33, and 65.
If –coef_u is nearly equal to an eigenvalue of the Laplacian with homogeneous boundary conditions, then the computed solution might have large errors.
Example
In this example, the equation:
with the boundary conditions:
on the bottom side and:
on the other three sides is solved. The domain is the rectangle [0, 1/4] × [0, 1/2]. The output of POISSON2D is a 17 × 33 table of values. The functions SPVALUE are used to print a different table of values.
FUNCTION rhs_pde,  x,  y
   ; Define the right side of the PDE
   f  =  (-2.0*SIN(x + 2.0*y) + 16.0*EXP(2.0*x + 3.0*y))   
   RETURN,  f
END
 
FUNCTION rhs_bc,  side,  x,  y
   ; Define the boundary conditions
   IF (side EQ 1) THEN $        
      ; Bottom side
      f  =  2.0*COS(x + 2.0*y) + 3.0*EXP(2.0*x + 3.0*y) $
   ELSE $                     
      ; All other sides, 0, 2, 3
      f  =  SIN(x + 2.0*y) + EXP(2.0*x + 3.0*y)
   RETURN,  f
END
 
PRO print_results, x, y, utable
   FOR j=0L, 4 DO FOR i=0L, 4 DO  $
      PRINT,  x(i),  y(j),  utable(i, j),  $
      ABS(utable(i,  j) - SIN(x(i) + 2.0*y(j)) - $
      EXP(2.0*x(i) + 3.0*y(j)))
END
nx  =  17
nxtable  =  5
ny  =  33
nytable  =  5
; Set rectangle size
ax  =  0.0
bx  =  0.25
ay  =  0.0
by  =  0.5
; Set boundary conditions
bc_type  =  [1, 2, 1, 1]
; Coefficient of u
coef_u  =  3.0
; Solve the PDE
u  =  POISSON2D('rhs_pde', 'rhs_bc', coef_u, nx, ny, ax, $
   bx, ay, by, bc_type)
; Set up for interpolation
xdata  =  ax + (bx - ax)*FINDGEN(nx)/(nx - 1)
ydata  =  ay + (by - ay)*FINDGEN(ny)/(ny - 1)
; Compute interpolant
sp  =  BSINTERP(xdata,  ydata,  u)
x  =  ax + (bx - ax)*FINDGEN(nxtable)/(nxtable - 1)
y  =  ay + (by - ay)*FINDGEN(nytable)/(nytable - 1)
utable  =  SPVALUE(x, y, sp)
; Print computed answer and absolute on nxtabl by nytabl grid
PRINT,'         X            Y            U         Error'
print_results, x, y, utable
This results in the following output:
         X            Y            U         Error
      0.00000      0.00000      1.00000      0.00000
    0.0625000      0.00000      1.19560  4.88758e-06
     0.125000      0.00000      1.40869  7.39098e-06
     0.187500      0.00000      1.64139  4.88758e-06
     0.250000      0.00000      1.89613  1.19209e-07
      0.00000     0.125000      1.70240  1.19209e-07
    0.0625000     0.125000      1.95615  6.55651e-06
     0.125000     0.125000      2.23451  9.53674e-06
     0.187500     0.125000      2.54067  6.67572e-06
     0.250000     0.125000      2.87830      0.00000
      0.00000     0.250000      2.59643  4.76837e-07
    0.0625000     0.250000      2.93217  9.05991e-06
     0.125000     0.250000      3.30337  1.31130e-05
     0.187500     0.250000      3.71482  8.82149e-06
     0.250000     0.250000      4.17198  2.38419e-07
      0.00000     0.375000      3.76186  2.38419e-07
    0.0625000     0.375000      4.21634  9.05991e-06
     0.125000     0.375000      4.72261  1.31130e-05
     0.187500     0.375000      5.28776  8.58307e-06
     0.250000     0.375000      5.91989  4.76837e-07
      0.00000     0.500000      5.32316  4.76837e-07
    0.0625000     0.500000      5.95199      0.00000
     0.125000     0.500000      6.65687  4.76837e-07
     0.187500     0.500000      7.44826      0.00000
     0.250000     0.500000      8.33804  1.43051e-06