Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming assignment # 3 Numerical Methods for PDEs Spring 2007 Jim E. Jones.

Similar presentations


Presentation on theme: "Programming assignment # 3 Numerical Methods for PDEs Spring 2007 Jim E. Jones."— Presentation transcript:

1 Programming assignment # 3 Numerical Methods for PDEs Spring 2007 Jim E. Jones

2 Programming Assignment #3 Will be like assignment #1, except –The diffusion coefficient will vary and the discretization will be done by finite volume method –We’ll replace the Gauss-Seidel solver for the linear system with something more effective To start, modify your code (or mine) from assignment #1 to solve the PDE …

3 Use finite volumes and Gauss-Seidel (to start). Solving the pressure equation for porous media flow Keep problem size as input. I’m showing a small problem for illustration.

4 Unknowns are at interior grid points. Solving the pressure equation for porous media flow

5 Assume that K is defined by a function of x & y Solving the pressure equation for porous media flow

6 Defining the diffusion coefficient (x=0,y=0) (x=1,y=1) Set up vectors x & y so that unknown (i,j) is located in the physical space at (x(i),y(j)). Then K for that gridpoint is defined by: K=DiffFun(x(i),y(j)) function k=DiffFun(x,y) if (x >=.5) k=100 elseif (y >=.5) k=.01 else k=1 end Index space (i=4,j=2) Physical space (.75,.25)

7 Use the K value at volume centers in defining the finite volume equations Setting up the equations

8 Finite Volume discretization equations p i,j p i-1,j Get an equation for i,j by integrating the PDE over the volume V i,j Integrating source term over the volume

9 Use harmonic averaging to define the K’s KiKi K i-1 pipi p i-1 The effective diffusion coefficient is the harmonic average of the 2.

10 Some comments of implementation Again we don’t want to store all of A because it is sparse (has lots of zero entries). But its not just 4’s and 1’s as before. For each gridpoint we need to store 5 entries: an,as,ae,aw,ac

11 The equation at gridpoint i,j an(i,j)*u(i,j+1)+as(i,j)*u(i,j-1)+aw(i,j)*u(i-1,j)+ae(i,j)*u(i+1,j)+ac(i,j)*u(i,j)=b(i,j) So computing the residual at interior points looks like for i=2:n for j=2:n r(i,j)=b(i,j)-(an(i,j)*u(i,j+1)+as(i,j)*u(i,j-1)+ … aw(i,j)*u(i-1,j)+ae(i,j)*u(i+1,j)+ac(i,j)*u(i,j)) end

12 Finite Volume discretization equations p i,j p i-1,j Get an equation for i,j by integrating the PDE over the volume V i,j Integrating source term over the volume

13 Start with Gauss-Seidel Modify the code from assignment #1 to use the finite volume discretization. When you are confident that this is working, switch from Gauss-Seidel to Conjugate Gradient method to solve the linear system.

14 Conjugate gradient algorithm To solve Au=f, starting from initial guess u 0 r 0 =f-Au 0 for k=1,2,3,…  k-1 = (r k-1 T r k-1 ) if k=1 p 1 = r 0 else  k-1 =  k-1 /  k-2 p k =r k-1 +  k-1 p k-1 end if q k =Ap k  k =  k-1 /(p k T q k ) u k =u k-1 +  k p k r k =r k-1 -  k q k Check convergence end for

15 Conjugate gradient algorithm To solve Au=f, starting from initial guess u 0 r 0 =f-Au 0 for k=1,2,3,…  k-1 = (r k-1 T r k-1 ) if k=1 p 1 = r 0 else  k-1 =  k-1 /  k-2 p k =r k-1 +  k-1 p k-1 end if q k =Ap k  k =  k-1 /(p k T q k ) u k =u k-1 +  k p k r k =r k-1 -  k q k Check convergence end for We’ll be talking about this next week. A good reference is: An Introduction to the Conjugate Gradient Method Without the Agonizing Pain, by Jonathan Shewchuk. Available online at: www.cs.cmu.edu/~jrs/jrspapers.html

16 Conjugate gradient algorithm To solve Au=f, starting from initial guess u 0 r 0 =f-Au 0 for k=1,2,3,…  k-1 = (r k-1 T r k-1 ) if k=1 p 1 = r 0 else  k-1 =  k-1 /  k-2 p k =r k-1 +  k-1 p k-1 end if q k =Ap k  k =  k-1 /(p k T q k ) u k =u k-1 +  k p k r k =r k-1 -  k q k Check convergence end for Each iteration requires: One matrix vector product Three vector updates Two inner products

17 Conjugate gradient algorithm To solve Au=f, starting from initial guess u 0 r 0 =f-Au 0 for k=1,2,3,…  k-1 = (r k-1 T r k-1 ) if k=1 p 1 = r 0 else  k-1 =  k-1 /  k-2 p k =r k-1 +  k-1 p k-1 end if q k =Ap k  k =  k-1 /(p k T q k ) u k =u k-1 +  k p k r k =r k-1 -  k q k Check convergence end for Need to store 4 vectors: r,p,q,u Need to store 4 constants ,  and 2  ’s

18 Programming Assignment #3 (due Mar 24) Problem 2 K=1 K=.01 K=100 Problem 1 K=1

19 Programming Assignment #3 (due Mar 24) Use your code to investigate these issues How does the variable K effect the computed solution. This is best seen by comparing plots of the solution to the two problems. How does the number of conjugate gradient iterations required compare to the number of Gauss-Seidel iterations required for the same tolerance.

20 Upcoming Schedule March M W 12 14 19 21 26 28 April M W 2 4 9 11 16 18 23 25 Programming assignment #3 due March 26 Take home portion of exam handed out March 28 Take home due and in class exam April 2 Programming assignment #4 due April 9 Final Project presentations April 23 & 25


Download ppt "Programming assignment # 3 Numerical Methods for PDEs Spring 2007 Jim E. Jones."

Similar presentations


Ads by Google