Direct Methods for 1D problems Numerical Methodsgtryggva/CFD-Course/2011-Lecture-21.pdf · Direct...
Transcript of Direct Methods for 1D problems Numerical Methodsgtryggva/CFD-Course/2011-Lecture-21.pdf · Direct...
Computational Fluid Dynamics
Numerical Methods!for!
Elliptic Equations-III!
Grétar Tryggvason!Spring 2011!
http://www.nd.edu/~gtryggva/CFD-Course/!Computational Fluid Dynamics
Examples of elliptic equations!Direct Methods for 1D problems!Elementary Iterative Methods!Note on Boundary Conditions!SOR on vector computers!Iteration as Time Integration!Convergence of Iterative Methods—elementary considerations!Multigrid methods!Convergence of Iterative Methods—Formal Discussion!ADI for elliptic equations !!Krylov Methods!Fast Direct Method!Resources!
Computational Fluid Dynamics
ADI Methods for Elliptic Equations!
Computational Fluid Dynamics Alternating-Direction Implicit - 1!
ADI for elliptic equation is analogous to ADI in parabolic!equation!
∂f∂t
= α ∂2 f∂x2
+∂2 f∂y2
⎡
⎣⎢
⎤
⎦⎥ − S
�
fi, jn+1 − fi, j
n = αΔt δxx f + δyy f[ ] + S
In discrete form!
and take it to the limit to obtain the steady solution.!
⎟⎠⎞⎜
⎝⎛ =∂∂ 0tf
Computational Fluid Dynamics Alternating-Direction Implicit - 2!
ADI for!
�
fi, jn+1 − fi, j
n = αΔt δxx f + δyy f[ ] − Sis written as!
�
f n +1/ 2 − f n = αΔt2h2
fi+1, jn +1/ 2 − 2 f i, j
n +1/ 2 + f i−1, jn +1/ 2( ) + f i, j +1
n − 2 f i, jn + f i, j−1
n( )[ ] − ′ S i, j
�
f n +1 − f n +1/ 2 = αΔt2h2
fi+1, jn +1/ 2 − 2 f i, j
n +1/ 2 + f i−1, jn +1/ 2( ) + f i, j +1
n +1 − 2 f i, jn +1 + f i, j−1
n +1( )[ ] − ′ ′ S i, j
or!
�
1− ρnδxx( ) f n +1/ 2 = 1+ ρnδyy( ) f n − ′ S i, j
�
1− ρnδyy( ) f n +1 = 1+ ρnδxx( ) f n +1/ 2 − ′ ′ S i, j
⎟⎠⎞⎜
⎝⎛ Δ=
2n
ntαρ
Computational Fluid Dynamics Alternating-Direction Implicit - 3!
Convergence of ADI!
- Iteration parameter usually varies with iteration!- For example (Wachspress)!
lower bound eigenvalue!
2n
ntΔ= αρ
( ) ( )nk
bab
ht
h
nkkk ,,1,
2
1/1
22 =⎟⎠⎞⎜
⎝⎛=Δ=
−−αρ
ab upper bound eigenvalue!
- Comparison with SOR is difficult!- ADI can be efficient if appropriate parameters are found.!
Computational Fluid Dynamics
Advanced iterative!
methods!Krylov methods!
Computational Fluid Dynamics
Iterative methods:!
Stationary Iterative Methods!!Jacobi, Gauss-Seidel, SOR!
Non Stationary Iterative Methods!!Conjugate Gradient (CG)!
!Minimal Residual (MINRES)!!Generalized Minimal Residual (GMRES)!!And Many Others!!
Computational Fluid Dynamics
Most of these methods are formulized as minimization techniques, where the following function is minimized!
�
F = xTAx − xT b
The search direction and the step are selected in a “best” way!
Computational Fluid Dynamics
Many methods work only for symmetric systems. While the pressure equation is usually symmetric, implicit methods usually do not lead to symmetric matrices.!
Usually the system is PRECONDITIONED to make it better behaved.!
Computational Fluid Dynamics
Preconditioning!
Iterative methods generally converge faster if we have a matrix A that is “close” to the identity matrix I!
�
Ax = b
�
A−1Ax =A−1b
�
Ix = x =A−1b�
A−1A = I
If the inverse of the matrix A was known, then the solution could be written down in a straight forward way!
Computational Fluid Dynamics
And should be easier to solve!
�
Ax = b�
MAx =Mb
If we have a matrix M that is in some way close to the inverse of A, then the system !
Has the same solution as the original system!
Computational Fluid Dynamics
Finding the “best” preconditioner is an active research topic, but the simples choice is:!
�
mi, j =ai,i if i = j0 otherwise⎧ ⎨ ⎩
Jacobi preconditioner!
A = D + L + LT
M = (D + L)D−1(D + L)T
M (β) =1
2 − β(
1β
D + L)(1β
D)−1(1β
D + L)T
Symmetric Successive Overrelaxation (SSOR) Preconditioner!
Computational Fluid Dynamics
Other choices include the so-called Incomplete Cholesky factorization or even multigrid methods!
In general it appears that finding an effective preconditioner is more important than exactly what Krylov method we use!
Computational Fluid Dynamics
r = b − Axα = (r,r) / ( Ar,r)x = x +αr
Steepest Decent (for symmetric positive A)!
Find the residual and correct the guess by going in the direction of the residual!
Notice that we do not need A separately, only the product of A and a vector!
x(i) ith iterate r(i) residual at ith step αi multiple of the search
direction vector
Computational Fluid Dynamics
1 22 1
⎡
⎣⎢
⎤
⎦⎥
xy
⎡
⎣⎢⎢
⎤
⎦⎥⎥= 5
4⎡
⎣⎢
⎤
⎦⎥
x0
y0
⎡
⎣⎢⎢
⎤
⎦⎥⎥= 0
0⎡
⎣⎢
⎤
⎦⎥
r = b − Axα = (r,r) / ( Ar,r)x = x +αr
xy
⎡
⎣⎢⎢
⎤
⎦⎥⎥= 1
2⎡
⎣⎢
⎤
⎦⎥
>> b = [ 5 4]!>> A=[1 2! 2 1]!>> x=[0 0]!
>> r=b-A*x = [5 4]!>> x=x+dot(r,r)/dot(A*r,r)*r = [1.6942 1.3554]!
>> r=b-A*x= [ 0.5950 -0.7438]!>> x=x+dot(r,r)/dot(A*r,r)*r = [1.0687 2.1373]!
>> r=b-A*x = [-0.3433 -0.2746]!>> x=x+dot(r,r)/dot(A*r,r)*r =[0.9523 2.0443]!
>> r=b-A*x = [ -0.0409 0.0511]!>> x=x+dot(r,r)/dot(A*r,r)*r = [ 0.9953 1.9906]!
>> r=b-A*x =[0.0236 0.0189]!>> x=x+dot(r,r)/dot(A*r,r)*r =[ 1.0033 1.9970]!
Computational Fluid Dynamics The conjugate gradient method!
Compute r(0) = b - A x(0) for some initial guess x(0) for i=1,2,… solve M z(i-1) = r(i-1) ρi-1 = r(i-1)T z(i-i) if i = 1 p(i)=z0 else βi -1 = ρi-1 / ρi-2 p(i)= z(i-1)- βi -1 p(i-1) end if q = Ap(i) αi = ρi-1 /p(i) q(i) x(i)= x(i-1)+ αi p(i) r(i)= r(i-1)- αi q(i) Check convergence; continue if needed end
x(i) ith iterate r(i) residual at ith step p(i) search direction αi multiple of the search
direction vector
One of the oldest non-stationary iterative method is the conjugate gradient method!
For M=I we have the unpreconditioned version!
Computational Fluid Dynamics
Generally iterative methods generate a sequence of approximations that are used to construct a new approximation. Ideally, we only need to keep a few approximations, but one of the more popular technique, GMRES, requires all the previous iterates. This leads to the restarted GMRES!
Computational Fluid Dynamics
At the present time there does not seem to be a “best” Krylov method. In addition to the relatively simple early methods like the conjugate gradient method, GMRES is fairly popular (particularly the restarted version) and BiCGSTAB has been used by a number of people.!
Computational Fluid Dynamics
http://www.netlib.org/templates/Templates.html!
From: Templates for the Solution of Linear Systems: Building Blocks for Iterative Methods!
Preconditioned !Bi-CGSTAB!
Computational Fluid Dynamics
Books:!
Barrett, Berry, Chan, Dellel, Donato, Dongarra, Eijkhout, Pozo, Romine, and Van der Vorst!Templates for the Solution of Linear Systems:!Building Blocks for Iterative Methods!http://www.netlib.org/templates/Templates.html!
Y. Saad. Iterative methods for sparse linear systems (2nd edition)!http://www-users.cs.umn.edu/~saad/books.html!
Computational Fluid Dynamics
Fast Direct Methods!
Computational Fluid Dynamics
Although iterative methods are the dominant technique for solutions of elliptic equations in CFD, Fast Direct Methods exists for special cases. The methods require simple domains (rectangles), simple equations (separable), and simple boundary conditions (periodic, or the derivative or the function equal to zero at each boundary)!
�
∂ 2 f∂x 2
+ ∂ 2 f∂y 2
= S
�
∂∂xa(x,y)∂f
∂x+ ∂∂yb(x,y)∂f
∂y= S
separable!
non-separable!
Computational Fluid Dynamics
The fast Fourier transform!
�
f j ≡ ˆ f lei 2π
Nl j
l=1
N
∑
Can be evaluated in 2N log2N operations!
inverse!
�
ˆ f l ≡ f je− i 2π
Nlj
l=1
N
∑
The Cooley-Tukey algorithm!
Computational Fluid Dynamics
Write!
�
Δfk, j ≡ fk+1, j + fk−1, j + fk, j+1 + fk, j−1 − 4 fk, j( ) = bk, j
Take the double Fourier Transform!
�
fk, j ≡ ˆ f l,mei 2π
Nkl + jm( )
m=1
N
∑l=1
N
∑
�
Δfk, j ≡ ˆ f l ,mei 2π
Nkl + jm( )
ei 2π
Nl
+ e−i 2π
Nl
+ ei 2π
Nm
+ e− i 2π
Nm− 4
⎛
⎝ ⎜
⎞
⎠ ⎟
m=1
N
∑l=1
N
∑
�
2cos 2πNl + 2cos 2π
Nl − 4
⎛ ⎝ ⎜
⎞ ⎠ ⎟
Computational Fluid Dynamics
Have!
bk , j = b̂l ,mei 2πN
kl+ jm( )
m=1
N
∑l=1
N
∑
Δfk , j = −2 f̂l ,mei 2πN
kl+ jm( )2 − cos 2π
Nl + cos 2π
Nm⎛
⎝⎜⎞⎠⎟m=1
N
∑l=1
N
∑
�
Δfk, j = bk, j
f̂l ,m =−b̂l ,m
2 2 − cos 2πNl + cos 2π
Nm⎛
⎝⎜⎞⎠⎟
also!
but!
Solve:!
Computational Fluid Dynamics
The algorithm is!
1 Find by FFT!2 Find as shown before!3 find by FFT!
�
ˆ b l ,m
�
ˆ f l ,m
�
fk, j
Computational Fluid Dynamics
As outlined the method is applicable to periodic boundaries only. Other simple boundary conditions can be handled by simple changes (using cosine or sine series).!
Other fast direct methods, such as Cyclic Reduction, are based on similar ideas!
See FISHPACK!
Computational Fluid Dynamics
subroutine hwscrt (a,b,m,mbdcnd,bda,bdb,c,d,n,nbdcnd,bdc,bdd, 1 elmbda,f,idimf,pertrb,ierror,w) c * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * c * f i s h p a k * c * * c * a package of fortran subprograms for the solution of * c * separable elliptic partial differential equations * c * (version 3.1 , october 1980) * c * by * c * john adams, paul swarztrauber and roland sweet * c * of * c * the national center for atmospheric research * c * boulder, colorado (80307) u.s.a. * c * which is sponsored by * c * the national science foundation * c * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * c * * * * * * * * * purpose * * * * * * * * * * * * * * * * * * c subroutine hwscrt solves the standard five-point finite c difference approximation to the helmholtz equation in cartesian c coordinates: c c (d/dx)(du/dx) + (d/dy)(du/dy) + lambda*u = f(x,y).
Computational Fluid Dynamics
Resources!
Computational Fluid Dynamics
A large number of pre-written software packages for the solution of elliptic equations is available!
Linear Equations (iterative methods).! pcg - Preconditioned Conjugate Gradients Method.! bicg - BiConjugate Gradients Method.! bicgstab - BiConjugate Gradients Stabilized Method.! cgs - Conjugate Gradients Squared Method.! gmres - Generalized Minimum Residual Method.! qmr - Quasi-Minimal Residual Method.!
In MATLAB:!
Computational Fluid Dynamics
MUDPACK!http://www.scd.ucar.edu/css/software/mudpack/!FISHPACK!http://www.scd.ucar.edu/css/software/fishpack/!
Multigrid website!http://www.mgnet.org/!
Netlib is a collection of mathematical software, papers and databases. !
http://www.netlib.org/!
Computational Fluid Dynamics
Examples of elliptic equations!Direct Methods for 1D problems!Elementary Iterative Methods!Note on Boundary Conditions!SOR on vector computers!Iteration as Time Integration!Convergence of Iterative Methods—elementary considerations!Multigrid methods!Convergence of Iterative Methods—Formal Discussion!ADI for elliptic equations !!Krylov Methods!Fast Direct Method!Resources!
Computational Fluid Dynamics
The Advection-Diffusion equation!
The Cell Reynolds number!
Computational Fluid Dynamics
�
UΔt2D
≤1 & DΔth2
≤ 12
�
∂f∂t
+U ∂f∂x
= D∂ 2 f∂x 2
�
f jn+1 − f j
n
Δt+U
f j+1n − f j−1
n
2h= D
f j+1n − 2 f j
n + f j−1n
h2
1D Advection/diffusion equation!
Forward in time/centered in space (FTCS)!
Stability limits!R =
ULD
�
Δt = 2DU
& Δt = h2
2D
�
Δt→ 0 For high and low D!
Computational Fluid Dynamics
�
∂f∂t
+U ∂f∂x
= D∂ 2 f∂x 2
�
O Δt,h2( )FTCS!
�
UΔt2D
≤1 & DΔth2
≤ 12
�
O Δt,h( )Upwind!
�
UΔth
+ 2DΔth2
≤1
�
O Δt 2,h2( )L-W!
�
UΔth
⎛ ⎝ ⎜
⎞ ⎠ ⎟ 2
≤ 2 DΔth2
⎛ ⎝ ⎜
⎞ ⎠ ⎟ ≤1
�
O Δt 2,h2( )C-N! Unconditionally stable!
Computational Fluid Dynamics
Steady state solution to the advection/diffusion equation!
U ∂f∂x
= D∂ 2 f∂x2
Exact solution!
�
f =exp RL x /L( ) −1exp RL( ) −1
�
RL = ULD
�
f = 0
�
f =1
�
U
�
L
Computational Fluid Dynamics
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
RL=1!RL=5!RL=10!RL=20!
Computational Fluid Dynamics
�
U ∂f∂x
= D∂ 2 f∂x 2
�
Uf j+1 − f j−12h
= Df j+1 − 2 f j + f j−1
h2
Centered difference approximation!
Numerical solution of:!
�
Uf j − f j−1h
= Df j+1 − 2 f j + f j−1
h2
Upwind!
Computational Fluid Dynamics
Ufj+1 − f j−12h
= Df j+1 − 2 f j + f j−1
h2
Uh2
2hDfj+1 − fj−1( ) = fj+1 − 2 fj + fj−1
R =UhD
Where:!
Rearrange:!
Rearrange:!
R− 2( ) fj+1 + 4 fj − R + 2( ) fj−1 = 0
Centered difference approximation!
Computational Fluid Dynamics
fj = qj
Divide by !
Solution!
Substitute:!
R− 2( )qj+1 + 4qj − R+ 2( )qj−1 = 0
R− 2( )q2 + 4q1 − R+ 2( ) = 0
q j−1
R− 2( ) fj+1 + 4 fj − R + 2( ) fj−1 = 0
Computational Fluid Dynamics
Solving for q gives two solutions:!
q1 =1 and!
The general solution is:!
R− 2( )q2 + 4q1 − R+ 2( ) = 0
q2 =2 + R2 − R
fj = C1q1j + C2q2
j
fj = C1 + C22 + R2 − R
⎛ ⎝
⎞ ⎠
jor!
Computational Fluid Dynamics
Apply the boundary conditions!
f0 = C1 + C22 + R2 − R
⎛ ⎝
⎞ ⎠
0
= C1 + C2 = 0
fN = C1 + C22 + R2 − R
⎛ ⎝
⎞ ⎠
N
= 1
fj =
2 + R2 − R
⎛ ⎝
⎞ ⎠
j
−1
2 + R2 − R
⎛ ⎝
⎞ ⎠
N +1
−1
The final solution is:!
Computational Fluid Dynamics
�
Uf j − f j−1h
= Df j+1 − 2 f j + f j−1
h2
�
R + 2( ) f j − R +1( ) f j−1 − f j+1 = 0R =
UhD
�
f j = q j
�
q2 − R + 2( )q1 + R +1( ) = 0
�
f j =1− 1+ R( ) j
1− 1+ R( )NSolution!
Upwind!
Try solutions!
giving!
or!
Computational Fluid Dynamics
fj =
2 + R2 − R
⎛ ⎝
⎞ ⎠
j
−1
2 + R2 − R
⎛ ⎝
⎞ ⎠
N +1
−1
�
f =exp RL x /L( ) −1exp RL( ) −1
�
RL = ULD
R =UhD
Exact solution!
Centered differences!
Upwind!
�
f j =1− 1+ R( ) j
1− 1+ R( )N
Computational Fluid Dynamics
Upwind!
Exact!Centered!
Computational Fluid Dynamics
Upwind!
Exact!Centered!
Computational Fluid Dynamics
Upwind!
Exact!Centered!
Computational Fluid Dynamics
When centered differencing is used for the advection/diffusion equation, oscillations may appear when the Cell Reynolds number is higher than 2. For upwinding, no oscillations appear. In most cases the oscillations are small and the cell Reynolds number is frequently allowed to be higher than 2 with relatively minor effects on the result.!
�
R = UhD
< 2
Computational Fluid Dynamics
2D example!
�
∂f∂t
+U ∂f∂x
+V ∂f∂y
= D ∂ 2 f∂x 2
+ ∂ 2 f∂y 2
⎛
⎝ ⎜
⎞
⎠ ⎟
�
f =1Flow!
�
f = 0
�
f = 0
Compuitations using centered differences on a 32 by 32 grid!
Computational Fluid Dynamics
D=0.02!t=1.5088!
Recell=3.2258!
Computational Fluid Dynamics
D=0.01!t=1.5088!
Recell=6.4516!
Computational Fluid Dynamics
D=0.005!t=1.5088!
Recell=12.9032!
Computational Fluid Dynamics
D=0.02!t=1.50!
0
5
10
15
0
5
10
150
0.5
1
1.5
05
1015
2025
30
05
1015
2025
300
0.5
1
1.5
Recell=3.2258! Recell=6.6716!
Fine grid! Coarser grid!