Pr j Poisson

Embed Size (px)

Citation preview

  • 7/30/2019 Pr j Poisson

    1/7

    Iterative Solution of the Poisson Equation

    The purpose of this project is to develop an iterative solver for the Poissonequation on cartesian grids in multiple dimensions. In vector form this equation isgiven by:

    2u = f (1)

    where u(x) is the solution sought and f(x) is a known function. Boundary condi-tions must be specified on the entire perimeter of the domain in order to determinea unique solution to the equation. For simplicity we will use so-called Dirichletboundary conditions: the solution u is known on the boundary.

    In cartesian coordinates the Poisson equation takes the following form

    2u = (u) =

    uxx = f(x) 1Duxx + uyy = f(x, y) 2Duxx + uyy + uzz = f(x,y,z) 3D

    (2)

    In order to solve the problem numerically we need to replace the second orderpartial derivatives with second-order finite diffence approximations. For the one-dimensional case, for example, we would use:

    uxx =ui1 2ui + ui+1

    x2+ O(x2). (3)

    The differential equations become a system of linear algebraic equations that we

    will solve using Jacobi iterations.

    1

  • 7/30/2019 Pr j Poisson

    2/7

    1 The 1D Poisson solver

    r r r r r r r r r

    a

    1 2 i 2 i 1

    xi1

    i

    xi

    i + 1

    xi+1

    i + 2 M M + 1

    b

    Figure 1: 1D finite difference grid for the interval a x b using a uniform gridspacing x = xi+1 xi = (b a)/M. The index of the nodes are the integers belowthe line, and the corresponding abcissa are shown above.

    Figure 1 shows the discrete grid and the location of the nodes where the solutionis sought. Applying the differential equation at each one of the interior nodes weobtain the algebraic system:

    ui1 2ui + ui+1 = x2fi, for i = 2, 3, 4, . . . , M (4)

    It is very important to remember that the system has only (M1) unknowns sinceu1 and uM+1 are known from the boundary conditions; the unknowns ui are locatedonly at the interior nodes. The iterative solution proceeds by starting with a guessu

    (0)i which, in general, will not satisfy equation 4. The guess is then updated to

    enforce the equality 4 at point i

    u(n+1)i =

    u(n)i+1 + u

    (n)i+1 x

    2fi2 for i = 2, 3, 4, . . . , M (5)

    where the superscript n is the iteration index. This update is repeated until aconvergence criterion is reached, for example the correction drops below a specifiedtolerance:

    max2iM

    c(n)i = max

    2iM

    u(n+1)i u(n)i < (6)

    Theoretical analysis guarantees that the process will eventually reach a solution,and that the number of iteration needed to reach convergence scales as

    K ln

    ln

    1 2sin2 2M

    2M2

    2ln (7)

    The above can be used as a rough estimate to bound the iteration count.The program design shown in 2-3 can be used as an initial design plan. The

    implied division of labor allows the main program to control the various tasks witha fair amount of flexibility. The code should be designed by stages, and you shouldleverage your previous efforts and re-use the software you have already built andtested to complete the assignment.

    2

  • 7/30/2019 Pr j Poisson

    3/7

    program poissonsolver

    use grid ! geometry module

    use fileio ! file i/o module

    use solver ! new solver to be built

    use poissondata ! BC, Forcing and 1st guess module

    implicit none

    integer, parameter :: M =8 ! number of intervals

    integer, parameter :: Mp=M+1 ! number of pointsreal*8, parameter :: tol=1.d-9 ! error tolerance

    real*8 :: u(Mp) ! solution

    real*8 :: f(Mp) ! forcing function

    real*8 :: f(Mp) ! x-coordinates

    real*8 :: enorm ! error norms of the iteration process

    integer :: niter ! number of iterations allowed/performed

    call SetGrid(xg,dx,smin,smax,M) ! Define the grid

    call WriteAsciiVector(xg,Mp) ! save grid to file

    call DefineRhs(f,x,Mp) ! Define forcing function

    call FirstGuess(u,x,Mp) ! first Guess, can be simply u=0.

    call SetBC(u,x,Mp) ! Set Boundary conditions

    call IterativeSolver(u,f,dx,tol,enorm,niter,Mp) ! iterative solver

    call OutputSolution(u,Mp) ! Save solution to a file

    stop

    end program poissonsolver

    Figure 2: Outline of main program. The main program sets-up the problem (ge-ometry, forcing function, first guess and boundary conditions), calls the iterativesolver, and outputs the solution to a file. The solver takes the tolerance

    3

  • 7/30/2019 Pr j Poisson

    4/7

    module solver

    containssubroutine IterativeSolver(u,f,dx,tol,enorm,niter,Mp)

    implicit none

    integer, intent(in) :: Mp ! size of computational grid

    real*8, intent(in) :: f(Mp) ! forcing function

    real*8, intent(in) :: dx ! grid spacing

    real*8, intent(in) :: tol ! tolerance

    real*8, intent(out) :: enorm ! error norm reported

    integer, intent(inout) :: niter ! max number of iterations on input

    real*8, intent(inout) :: u(Mp) ! solution

    integer :: nitermax,it

    nitermax = niter ! max iterations alloweddo it = 1,nitermax

    call JacobiIteration(u,f,dx,enorm,Mp) ! perform single iteration

    if (enorm < tol) then

    exit ! solution has converged exit loop

    endif

    enddo

    if (enorm > tol .and. it > nitermax)then

    print *,Solution did not converge ! error warning

    endif

    niterm = it

    return

    end subroutine IterativeSolver

    ! JacobiIteration does a single update of solution u

    ! it returns an error norm measuring the maximum change:

    ! enorm = max_i |u_i^{n+1} - u_i|

    subroutine JacobiIteration(u,f,dx,enorm,Mp) ! perform single iteration

    implicit none

    ...

    return

    end subroutine JacobiIterationend module solver

    Figure 3: Outline of iterative solver. The iterative solver specified the tolerancedesired, and the maximum number of iterations allowed. It returns, aside fromthe solution, an error measure of the iteration error, and the number of iterationsnecessary to reach the prescribed error level. If the iteration fails to converge withinthe alloted iterations, an error message is printed. The subroutine JacobiIterationperforms a single update of the solution

    4

  • 7/30/2019 Pr j Poisson

    5/7

    1. No forcingDuring the development phase of the program, use f = 0, u1 = 1, anduM+1 = M + 1. The solution is then simply a straight line and is givenby ui = M(xi a) + 1, and the numerical solution should yield this exactsolution. Use the interval 1 x 1.

    2. Simple forcingOnce the code is working try the code on the following problem:

    u(1) = 6, f = 6x (8)

    whose solution is u = 6x3.

    3. Non-trivial problemOnce the code is working try your program on the following case:

    u(1) = cos

    e1

    f = ex [ex cos(ex) + sin(ex)]

    u(x) = cos (ex)

    The numerical solution is now only approximate. Try solving the equationsusing 25, 50, 100, 200, 400, and 800 points. For each case record the errorcommitted, and the number of iterations required to achieve convergence.Verify that the method is indeed second order accurate.

    2 The 2D Poisson solver

    Figure 4 refers to the two-dimensional computational grid. The finite differenceapproximation takes the form:

    ui1,j 2ui,j + ui+1,jx2

    +ui,j1 2ui,j + ui,j+1

    y2= fi,j, 2 i M, 2 j N (9)

    Again the unknown are located strictly in the interior of grid since u is known atthe boundaries from the boundary conditions, and hence there are (M 1)(N 1)unknowns. The iterative update of the Jacobi iteration can now be written as:

    u(n+1)i,j =

    u

    (n)i1,j + u

    (n)i+1,j

    y2 +

    u

    (n)i,j1 + u

    (n)i,j+1

    x2 x2y2fi,j

    2 (x2 + y2)(10)

    Again it can be shown that the number of iterations needed to reach a tolerancelevel scales as K max(M2, N2) ln .

    The programming tasks required is now to upgrade your one-dimensional iter-ative solver to two-dimensions following the same steps outlined before.

    5

  • 7/30/2019 Pr j Poisson

    6/7

    r r r r r r r r r

    r r r r r r r r r

    r r r r r r r r r

    r r r r r r r r r

    r r r r r r r r r

    r r r r r r r r r

    r r r r r r r r r

    r r r r r r r r r

    r r r r r r r r r

    1 2 i 2 i 1

    xi1

    i

    xi

    i + 1

    xi+1

    i + 2 M M + 11

    2

    .

    ..

    j 2

    j 1yj1

    jyj

    j + 1yj+1

    j + 2

    .

    ..

    N

    N + 1

    M + 1

    d d d

    d

    d

    (a, c)

    (b, d)

    Figure 4: 2D finite difference grid for the interval a x b using a uniform gridspacing x = xi+1 xi = (b a)/M, and y = yj+1 yj = (d c)/N. The nodesmust now be referred to with a 2-integer index (i, j). The stencil for one of thecomputational grids is shown in red.

    Development phaseUse a trivial case where the exact solution is just linear in each of the coor-dinate, say u = x + 2 y 3z, f = 0, to test your code during development.Another interesting solution is u = x2 y2. Use the domain |x| 2, and|y| 1 with M = 8 and N = 5. The boundary conditions can be lifted fromthe exact solution.

    Experimentation phaseFor this case the problems data, including the exact solution, is given by

    u = cos

    2 e(yx)

    + sin

    2 e

    (x+y)

    (11)

    f = e(x+y)

    cos

    2e(x+y)

    2e(x+y) sin

    2e(x+y)

    e(yx)

    sin

    2e(yx)

    +

    2e(yx) cos

    2e(yx)

    (12)

    Use the same geometry as for the earlier case, but experiment with changingthe number of points. This is somewhat of a demanding problem as thesolution starts oscillating fast as one approaches the north-eastern corner of

    6

  • 7/30/2019 Pr j Poisson

    7/7

    the domain, one can anticipate that to model a wavelength accurately atleast 8 points are needed, and hence y > 0.025. Again, confirm the secondorder convergence rate using (100 50), (200 100), (400 200), (800 400)and (1600 800) cells. Record the number of iterations needed to achieveconvergence, and the CPU-time consumed.

    3 The 3D Poisson solver

    This part is optional, and does not require much work given that its a simpleextension of the 2D code. It however drives home the message that the numberof unknowns, and hence the time to solution, grows very fast in three-dimensions.The 3D stencil for the finite difference approximation of the Laplace operator, 2,include 7 points on the computational grid, and is given by:

    ui1,j,k 2ui,j,k + ui+1,j,kx2

    +ui,j1,k 2ui,j,k + ui,j+1,k

    y2+

    ui,j,k1 2ui,j,k + ui,j,k+1z2

    = fi,j,k

    (2, 2, 2) (i,j,k) (M,N ,P) (13)

    where P is the number of intervals in the z-direction. Again we have assumed thatDirichlet boundary conditions are applied on all boundaries i = 1, M, j = 1, N,and k = 1, P. The total number of unknowns is then (M1)(N1)(P1). Noticethat the number of unknowns grows very quickly in 3D; for example, a 25 25 25cell discretization will have (25 1)3 unknowns or 13,824 unknowns. The Jacobiiterative solution takes the form:

    u(n+1)i,j,k = ax

    u

    (n)i1,j,k + u

    (n)i+1,j,k

    + ay

    u

    (n)i,j1,k + u

    (n)i,j+1,k

    + az

    u

    (n)i,j,k1 + u

    (n)i,j,k+1

    affi,j,k (14)

    ax =y2z2

    2 (x2 + y2 + z2)(15)

    ay =z2x2

    2 (x2 + y2 + z2)(16)

    az =x2y2

    2 (x2 + y2 + z2)(17)

    af =x2y2z2

    2 (x2 + y2 + z2)(18)

    7