Iteratve Soluton of the Posson Equaton The purpose of ths project s to develop an teratve solver for the Posson equaton on cartesan grds n multple dmensons. In vector form ths equaton s gven by: 2 u = f (1) where u(x) s the soluton sought and f(x) s a known functon. Boundary condtons must be specfed on the entre permeter of the doman n order to determne a unque soluton to the equaton. For smplcty we wll use so-called Drchlet boundary condtons: the soluton u s known on the boundary. In cartesan coordnates the Posson equaton takes the followng form u xx = f(x) 2 u = ( u) = u xx + u yy = f(x, y) u xx + u yy + u zz = f(x, y, z) In order to solve the problem numercally we need to replace the second order partal dervatves wth second-order fnte dffence approxmatons. For the onedmensonal case, for example, we would use: 1D 2D 3D u xx = u 1 2u + u +1 x 2 + O( x 2 ). (3) The dfferental equatons become a system of lnear algebrac equatons that we wll solve usng Jacob teratons. (2) 1
1 The 1D Posson solver a x 1 x x +1 b 1 2 2 1 + 1 + 2 M M + 1 Fgure 1: 1D fnte dfference grd for the nterval a x b usng a unform grd spacng x = x +1 x = (b a)/m. The ndex of the nodes are the ntegers below the lne, and the correspondng abcssa are shown above. Fgure 1 shows the dscrete grd and the locaton of the nodes where the soluton s sought. Applyng the dfferental equaton at each one of the nteror nodes we obtan the algebrac system: u 1 2u + u +1 = x 2 f, for = 2, 3, 4,..., M (4) It s very mportant to remember that the system has only (M 1) unknowns snce u 1 and u M+1 are known from the boundary condtons; the unknowns u are located only at the nteror nodes. The teratve soluton proceeds by startng wth a guess whch, n general, wll not satsfy equaton 4. The guess s then updated to enforce the equalty 4 at pont u (0) u (n+1) = u(n) +1 + u (n) +1 x 2 f 2 for = 2, 3, 4,..., M (5) where the superscrpt n s the teraton ndex. Ths update s repeated untl a convergence crteron s reached, for example the correcton drops below a specfed tolerance: max 2 M c (n) = max 2 M u (n+1) u (n) < ɛ (6) Theoretcal analyss guarantees that the process wll eventually reach a soluton, and that the number of teraton needed to reach convergence scales as K ln ɛ ln ( 1 2 sn 2 ) 2M 2 ln ɛ (7) π π 2 2M The above can be used as a rough estmate to bound the teraton count. The program desgn shown n 2-3 can be used as an ntal desgn plan. The mpled dvson of labor allows the man program to control the varous tasks wth a far amount of flexblty. The code should be desgned by stages, and you should leverage your prevous efforts and re-use the software you have already bult and tested to complete the assgnment. 2
program possonsolver use grd! geometry module use fleo! fle /o module use solver! new solver to be bult use possondata! BC, Forcng and 1st guess module mplct none nteger, parameter :: M =8! number of ntervals nteger, parameter :: Mp=M+1! number of ponts real*8, parameter :: tol=1.d-9! error tolerance real*8 :: u(mp)! soluton real*8 :: f(mp)! forcng functon real*8 :: f(mp)! x-coordnates real*8 :: enorm! error norms of the teraton process nteger :: nter! number of teratons allowed/performed call SetGrd(xg,dx,smn,smax,M)! Defne the grd call WrteAscVector(xg,Mp)! save grd to fle call DefneRhs(f,x,Mp)! Defne forcng functon call FrstGuess(u,x,Mp)! frst Guess, can be smply u=0. call SetBC(u,x,Mp)! Set Boundary condtons call IteratveSolver(u,f,dx,tol,enorm,nter,Mp)! teratve solver call OutputSoluton(u,Mp)! Save soluton to a fle stop end program possonsolver Fgure 2: Outlne of man program. The man program sets-up the problem (geometry, forcng functon, frst guess and boundary condtons), calls the teratve solver, and outputs the soluton to a fle. The solver takes the tolerance 3
module solver contans subroutne IteratveSolver(u,f,dx,tol,enorm,nter,Mp) mplct none nteger, ntent(n) :: Mp! sze of computatonal grd real*8, ntent(n) :: f(mp)! forcng functon real*8, ntent(n) :: dx! grd spacng real*8, ntent(n) :: tol! tolerance real*8, ntent(out) :: enorm! error norm reported nteger, ntent(nout) :: nter! max number of teratons on nput real*8, ntent(nout) :: u(mp)! soluton nteger :: ntermax,t ntermax = nter! max teratons allowed do t = 1,ntermax call JacobIteraton(u,f,dx,enorm,Mp)! perform sngle teraton f (enorm < tol) then ext! soluton has converged ext loop endf enddo f (enorm > tol.and. t > ntermax)then prnt *, Soluton dd not converge! error warnng endf nterm = t return end subroutne IteratveSolver! JacobIteraton does a sngle update of soluton u! t returns an error norm measurng the maxmum change:! enorm = max_ u_^{n+1} - u_ subroutne JacobIteraton(u,f,dx,enorm,Mp)! perform sngle teraton mplct none... return end subroutne JacobIteraton end module solver Fgure 3: Outlne of teratve solver. The teratve solver specfed the tolerance desred, and the maxmum number of teratons allowed. It returns, asde from the soluton, an error measure of the teraton error, and the number of teratons necessary to reach the prescrbed error level. If the teraton fals to converge wthn the alloted teratons, an error message s prnted. The subroutne JacobIteraton performs a sngle update of the soluton 4
1. No forcng Durng the development phase of the program, use f = 0, u 1 = 1, and u M+1 = M + 1. The soluton s then smply a straght lne and s gven by u = M(x a) + 1, and the numercal soluton should yeld ths exact soluton. Use the nterval 1 x 1. 2. Smple forcng Once the code s workng try the code on the followng problem: whose soluton s u = 6x 3. u(±1) = ±6, f = 6x (8) 3. Non-trval problem Once the code s workng try your program on the followng case: u(±1) = cos ( πe ±1) f = πe x [e x cos(πe x ) + sn(πe x )] u(x) = cos (πe x ) The numercal soluton s now only approxmate. Try solvng the equatons usng 25, 50, 100, 200, 400, and 800 ponts. For each case record the error commtted, and the number of teratons requred to acheve convergence. Verfy that the method s ndeed second order accurate. 2 The 2D Posson solver Fgure 4 refers to the two-dmensonal computatonal grd. The fnte dfference approxmaton takes the form: u 1,j 2u,j + u +1,j x 2 + u,j 1 2u,j + u,j+1 y 2 = f,j, 2 M, 2 j N (9) Agan the unknown are located strctly n the nteror of grd snce u s known at the boundares from the boundary condtons, and hence there are (M 1)(N 1) unknowns. The teratve update of the Jacob teraton can now be wrtten as: u (n+1),j = ( (n) u 1,j + u (n) ) +1,j y 2 + ( u (n),j 1 + u,j+1) (n) x 2 x 2 y 2 f,j 2 ( x 2 + y 2 ) (10) Agan t can be shown that the number of teratons needed to reach a tolerance level scales as K max(m 2, N 2 ) ln ɛ. The programmng tasks requred s now to upgrade your one-dmensonal teratve solver to two-dmensons followng the same steps outlned before. 5
N + 1 (b, d) N. j + 2 y j+1 j + 1 y j j y j 1 j 1 j 2. 2 1 1 2 2 1 + 1 + 2 M M + 1 (a, c) x 1 x x +1 Fgure 4: 2D fnte dfference grd for the nterval a x b usng a unform grd spacng x = x +1 x = (b a)/m, and y = y j+1 y j = (d c)/n. The nodes must now be referred to wth a 2-nteger ndex (, j). The stencl for one of the computatonal grds s shown n red. Development phase Use a trval case where the exact soluton s just lnear n each of the coordnate, say u = x + 2 y 3z, f = 0, to test your code durng development. Another nterestng soluton s u = x 2 y 2. Use the doman x 2, and y 1 wth M = 8 and N = 5. The boundary condtons can be lfted from the exact soluton. Expermentaton phase For ths case the problem s data, ncludng the exact soluton, s gven by ( ) π u = cos 2 e(y x) ( ) π + sn 2 e(x+y) ( ) π f = πe [cos (x+y) 2 e(x+y) π ( )] π 2 e(x+y) sn 2 e(x+y) ( ) π πe [sn (y x) 2 e(y x) + π ( )] π 2 e(y x) cos 2 e(y x) (11) (12) Use the same geometry as for the earler case, but experment wth changng the number of ponts. Ths s somewhat of a demandng problem as the soluton starts oscllatng fast as one approaches the north-eastern corner of 6
the doman, one can antcpate that to model a wavelength accurately at least 8 ponts are needed, and hence y > 0.025. Agan, confrm the second order convergence rate usng (100 50), (200 100), (400 200), (800 400) and (1600 800) cells. Record the number of teratons needed to acheve convergence, and the CPU-tme consumed. 3 The 3D Posson solver Ths part s optonal, and does not requre much work gven that ts a smple extenson of the 2D code. It however drves home the message that the number of unknowns, and hence the tme to soluton, grows very fast n three-dmensons. The 3D stencl for the fnte dfference approxmaton of the Laplace operator, 2, nclude 7 ponts on the computatonal grd, and s gven by: u 1,j,k 2u,j,k + u +1,j,k + u,j 1,k 2u,j,k + u,j+1,k x 2 y 2 + u,j,k 1 2u,j,k + u,j,k+1 z 2 = f,j,k (2, 2, 2) (, j, k) (M, N, P ) (13) where P s the number of ntervals n the z-drecton. Agan we have assumed that Drchlet boundary condtons are appled on all boundares = 1, M, j = 1, N, and k = 1, P. The total number of unknowns s then (M 1)(N 1)(P 1). Notce that the number of unknowns grows very quckly n 3D; for example, a 25 25 25 cell dscretzaton wll have (25 1) 3 unknowns or 13,824 unknowns. The Jacob teratve soluton takes the form: u (n+1) ( (n),j,k = a x u 1,j,k + ) ( u(n) (n) +1,j,k + ay u,j 1,k + ) ( u(n) (n),j+1,k + az u,j,k 1 + ) u(n),j,k+1 a f f,j,k (14) a x = y 2 z 2 (15) a y = z 2 x 2 (16) a z = x 2 y 2 (17) a f = x 2 y 2 z 2 (18) 7