computational fluid dynamics accuracygtryggva/cfd-course2017/lecture-2-2017.pdfcomputational fluid...
TRANSCRIPT
Computational Fluid Dynamics
Computational Fluid Dynamics
http://www.nd.edu/~gtryggva/CFD-Course/
Grétar Tryggvason
Lecture 2January 23, 2017
Computational Fluid Dynamics
Accuracy
Computational Fluid Dynamics
It is clear that although the numerical solution is qualitatively similar to the analytical solution, there are significant quantitative differences.
The derivation of the numerical approximations for the derivatives showed that the error depends on the size of h and Δt.
First we test for different Δt.
Number of time steps= T/Δt
Computational Fluid Dynamics
m=11; time=0.50
2 4 6 8 10 12 14 16 18 20-2
-1.5
-1
-0.5
0
0.5
1
1.5
2
Evolution forU=1; D=0.05; k=1
N=21Δt=0.05
Exact
Numerical
Accuracy
Computational Fluid Dynamics
Repeat with a smaller time-stepΔt=0.025
N=21
2 4 6 8 10 12 14 16 18 20-2
-1.5
-1
-0.5
0
0.5
1
1.5
2
m=21; time=0.50
Exact
Numerical
AccuracyComputational Fluid Dynamics
Repeat with a smaller time-stepΔt=0.0125
N=21
m=41; time=0.50
2 4 6 8 10 12 14 16 18 20-2
-1.5
-1
-0.5
0
0.5
1
1.5
2
Exact
Numerical
Accuracy
Computational Fluid Dynamics
How accurate solution can we obtain?
Take Δt = 0.0005 and N=200
Computational Fluid Dynamics
m=1001; time=0.50
20 40 60 80 100 120 140 160 180 200-2
-1.5
-1
-0.5
0
0.5
1
1.5
2U=1; D=0.05; k=1
N=200Δt=0.0005
Exact
Numerical
Very fine spatial resolution and a small time step
Accuracy
Computational Fluid DynamicsAccuracy
Examine the spatial accuracy by taking a very small time step, Δt = 0.0005 and vary the number of grid points, N, used to resolve the spatial direction.
The grid size is h = L/N where L = 1 for our case
Quantifying the ErrorOrder of Accuracy
Computational Fluid Dynamics
E = h ( fj − fexact )2
j=1
N
∑
N=11; E = 0.1633N =21; E = 0.0403N =41; E = 0.0096N =61; E = 0.0041N =81; E = 0.0022N =101; E = 0.0015N =121; E = 0.0011N =161; E = 9.2600e-04
Exact
Numericaltime=0.50
Accuracy
Computational Fluid Dynamics
100 101 10210-4
10-3
10-2
10-1
100Accuracy. Effect ofspatial resolution
1/h
dt=0.0005N=11 to N=161
E
time=0.50
AccuracyComputational Fluid Dynamics
�
E = Ch2 = C 1h⎛ ⎝
⎞ ⎠
−2
�
lnE = ln C 1h⎛ ⎝
⎞ ⎠
−2⎛
⎝ ⎜ ⎞
⎠ ⎟ = lnC − 2ln 1
h⎛ ⎝
⎞ ⎠
On a log-log plot, the E versus (1/h) curve should therefore have a slope -2
If the error is of second order:
Taking the log:
Accuracy
Computational Fluid Dynamics
100 101 10210-4
10-3
10-2
10-1
100Accuracy. Effect ofspatial resolution
21
1/h
dt=0.0005
N=11 to N=161
E
time=0.50
AccuracyComputational Fluid Dynamics
Why is does the error deviate from the line for the highest values of N?
Number of Steps, N = 1/h
Erro
r
Total
Roundoff
Truncation
Accuracy
Computational Fluid Dynamics
Finite difference approximations by Taylor expansion
Approximating a partial differential equation
Showed, by numerical experiments, that accuracy increases as the resolution is increased
Showed that the error behaves in a way that should be predictable
SummaryComputational Fluid Dynamics
Stability
Computational Fluid Dynamics
As long as accuracy is reasonable, integration at larger time steps is more efficient and desirable.
Can we increase the time step indefinitely?
Let’s repeat the 1-D advection-diffusion equation with larges time step.
Use Δt = 0.2, instead of Δt = 0.05
StabilityComputational Fluid Dynamics
Evolution forU=1; D=0.05; k=1
N=21Δt=0.2
Exact
Numerical
Stability
Computational Fluid Dynamics
Instead of decaying as it should, the amplitude of the numerical solution keeps increasing.
Indeed, if we continued the calculations, we would eventually produce numbers larger than the computer can handle. This results in an “overflow” or “NaN” (Not a Number).
StabilityComputational Fluid Dynamics
Ordinary Differential EquationStability
Computational Fluid DynamicsODE—Example
Take: dfdt
= − f
f t( ) = e−tThe exact solution is
with initial condition f (0) = 1
f n+1 = (1 − Δt) f nf n+1 = f n − f nΔtForward Euler
�
f n+1
f n≤1 only if
�
Δt ≤ 2
Computational Fluid Dynamics
0 5 10 15 20-5
-4
-3
-2
-1
0
1
2
3
4
5
Δt=0.5Δt=1.5
Δt=2.5
�
f n+1
f n≤1 only if
�
Δt ≤ 2
f n+1 = (1 − Δt) f n
ODE—Example
Computational Fluid Dynamics
�
f n+1
f n= 1(1+ Δt)
f n+1 = f n − f n+1ΔtBackward Euler
�
f n+1 = (1−Δt) f n = (1−Δt)2 f n−1
= ………… = (1−Δt)n f 1
However
for all Δt
�
Δt ≤1
�
f n+1
f n≤1
Obviously, f oscillates unless
ODE—ExampleComputational Fluid Dynamics
Stability Analysis of theAdvection-Diffusion Equation:
Von Neumann Method
Computational Fluid Dynamics
Generally, stability analysis of the full nonlinear system of equations is too involved to be practical, and we study a model problem that in some way mimics the full equations.The linear advection-diffusion equation is one such model equation, and we will apply von Neumann's method to check the stability of a simple finite difference approximation to that equation.
StabilityComputational Fluid Dynamics
2
2
xf
Dxf
Utf
∂∂
∂∂
∂∂ =+
21111
1 22 h
fffD
hff
Utff n
jnj
nj
nj
nj
nj
nj −+−++ +−
=−
+Δ−
Consider the 1-D advection-diffusion equation:
In finite-difference form:
Stability
�
f jn
= ε jn
Look at the evolution of a small perturbation
Computational Fluid Dynamics
22
21111
1
hD
hU
t
nj
nj
nj
nj
nj
nj
nj −+−++ +−
=−
+Δ− εεεεεεε
The evolution of the perturbation is governed by:
Write the error as a wave (expand as a Fourier series):
∑∞
−∞=
==k
ikxnkj
nnj
jex εεε )(
Dropping the subscriptjikxnn
j eεε =
Stability
�
eikx = coskx + isinkxRecall:
Computational Fluid Dynamics
ε jn = εneikx j
ε j+1n = εneikx j+1 = εneik (x j + h) = εneikx j eikh
The error at node j is:
The error at j+1 and j -1 can be written as
ε j−1n = ε neikx j−1 = ε neik (x j −h) = ε neikx j e−ikh
Stability
Computational Fluid Dynamics
ε j+1n = ε neikx j eikh
ε n+1eikx j − ε neikx j
Δt+U εn
2h(eikheikx j − e− ikheikx j ) =
Dε n
h2(eikheikx j − 2eikx j + e− ikheikx j )
Substituting
ε j−1n = ε neikx j e−ikh
ε jn+1 − ε j
n
Δt+U
ε j+1n − ε j−1
n
2h= D
ε j+1n − 2ε j
n + ε j−1n
h2
ε jn = εneikx j
into
yields
StabilityComputational Fluid Dynamics
ε n+1 − εn
Δ t+U
ε n
2h(eikh − e− ikh) = D
ε n
h2(eikh − 2 + e−ikh )
The equation for the error is:
)2()(2
1 2
1ikhikhikhikh
n
n
eehtDee
htU −−
+
+−Δ+−Δ−=εε
Solving for the ratio of the errors:
Stability
Computational Fluid Dynamics
)2()(2
1 2
1ikhikhikhikh
n
n
eehtDee
htU −−
+
+−Δ+−Δ−=εε
�
=1−UΔth
isinkh + DΔth2
2(coskh −1)
�
=1− 4 DΔth2
sin2 k h2− iUΔt
hsinkh
Dividing by the error amplitude at n:amplification factor
Stability
�
eikh + e−ikh = 2coskh; eikh − e− ikh = i2sinkh;Using:
�
2sin2θ =1− cos2θ
Computational Fluid Dynamics
�
εn+1
εn=1− 4 DΔt
h2sin2 k h
2− iUΔt
hsinkh
ε n+1
ε n≤ 1
Stability requires that
Since the amplification factor is a complex number, and k, the wave number of the error, can be anything, the determination of the stability limit is slightly involved.
We will look at two special cases: (a) U = 0 and (b) D = 0
The ratio of the error amplitude at n+1 and n is:
Stability
Computational Fluid Dynamics
ε n+1
ε n= 1− 4
DΔth2
sin2 kh2
Since the amplification factor is always less than 1, and we find that it is bigger than -1 if
sin2 () ≤ 1
(a) Consider first the case when U = 0, so the problem reduces to a pure diffusion
�
DΔth2
≤12
�
−1≤1− 4 DΔth2
≤1
StabilityComputational Fluid Dynamics
Since the amplification factor has the form 1+i() the absolute value of this complex number is always larger than unity and the method is unconditionally unstable for this case.
�
εn+1
εn=1− iUΔt
hsinkh
(b) Consider now the other limit where D = 0 and we have a pure advection problem.
�
iUΔthsinkh
1
ε n+1
ε n
Stability
Computational Fluid Dynamics
For the general case we must investigate the stability condition in more detail. We will not do so here, but simply quote the results:
Notice that high velocity and low viscosity lead to instability according to the second restriction.
Δ tDh2
≤12
U2ΔtD
≤ 2and
StabilityComputational Fluid Dynamics
ε i, jn = ε nei(kxi +ly j )
41
2 ≤ΔhtD
4|)||(| 2
≤Δ+D
tVUand
�
DΔth2
≤ 16
and
�
(|U |+ |V |+ |W |)2ΔtD
≤ 8
For a two-dimensional problem, assume an error of the form
A stability analysis gives:
For a three-dimensional problem we get:
Stability
Computational Fluid Dynamics
Convergence – the solution to the finite-difference equation approaches the true solution to the PDE having the same initial and boundary conditions as the mesh is refined.
Lax’s Equivalence TheoremGiven a properly posed initial value problem and a finite-difference approximation to it that satisfies the consistency condition, stability is the necessary and sufficient condition for convergence.
Stability – Now you know!
StabilityComputational Fluid Dynamics
The Modified Equation
Computational Fluid Dynamics
Using the finite difference approximation, we are effectively solving an equation that is slightly different than the original partial differential equations.
Does the finite difference equation approach the partial differential equation in the limit of zero Δt and h?
ConsistencyComputational Fluid Dynamics
Consider the 1-D advection-diffusion equation
and its finite-difference approximation
�
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
∂ f∂t
+U∂ f∂x
= D∂ 2 f∂x2
The discrepancy between the two equations can be found by deriving the modified equation.
Consistency
Computational Fluid Dynamics
Substituting
�
f jn+1 − f j
n
Δt= ∂f (t)
∂t+ ∂ 2 f (t)
∂t 2Δt2
+
�
f j+1n − f j−1
n
2h= ∂f (x)
∂x+ ∂ 3 f (x)
∂x 3h2
6+
�
f j+1n − 2 f j
n + f j−1n
h2= ∂ 2 f (x)
∂x 2+ ∂ 4 f (x)
∂x 4h2
12+
into the finite difference equationfjn+1 − fj
n
Δt+U
fj+1n − fj−1
n
2h= D
f j+1n − 2 fj
n + fj−1n
h2
ConsistencyComputational Fluid Dynamics
�
∂f∂t
+U ∂f∂x
−D∂ 2 f∂x 2
= −∂2 f (t)∂t 2
Δt2−U ∂ 3 f (x)
∂x 3h2
6+ D∂ 4 f (x)
∂x 4h2
12+
In this case, the error goes to zero as h→0 and Δt→0, so the approximation is said to be CONSISTENT
Results in the Modified Equation:
Original Equation Error terms
�
∂f∂t
+ U ∂f∂x
−D∂ 2 f∂x 2
= O(Δt,h2 )
Shorthand:
Consistency
Computational Fluid Dynamics
Although most finite difference approximations are consistent, innocent-looking modifications can sometimes lead to approximations that are not!
The Frankel-Dufort is an example of an non-consistent scheme.
You will examine it in the homework
ConsistencyComputational Fluid Dynamics
HW: Examine the Frankel-Dufort method
�
∂f∂t
= D∂ 2 f∂x 2
Solve the diffusion equation
Using the Leapfrog time integration method and standard finite-difference approximation for the spatial derivative gives:
�
f jn+1 − f j
n−1
2Δt= Dh2
f j+1n − 2 f j
n + f j−1n[ ]
Leapfrog method
Consistency
Computational Fluid Dynamics
This gives:�
f jn+1 − f j
n−1
2Δt= Dh2
f j+1n − 2 f j
n + f j−1n[ ]
�
f jn =
12f jn +1 + f j
n−1( )
�
f jn+1 = f j
n−1 + 2Δt D
h2f j+1n − f j
n+1 − f jn−1 + f j−1
n( ).Which is easily solved for f at the new time step
Now modify it slightly:
In the HW, you will examine the error!
Replace by:
ConsistencyComputational Fluid Dynamics
The MODIFIED EQUATION is obtained by substituting the expression for the finite difference approximations, including the error terms, into the finite difference equation. For a CONSISTENT finite difference approximation the error terms go to zero as h→0 and Δt→0. The modified equation can often be used to infer the nature of the error of the finite difference scheme. More about that later.
Consistency
Computational Fluid Dynamics
Two-Dimensional Advection-
Diffusion Equation
Computational Fluid Dynamics
∂f∂t
+U∂f∂x
+ V∂f∂y
= D∂ 2 f∂x 2
+∂ 2 f∂y2
⎛ ⎝ ⎜ ⎞
⎠
We will use the model equation:
to demonstrate how to solve a partial equation (initial value problem) numerically.
The extension to two-dimensions is relatively straight forward, once the one-dimensional problem is fully understood.
Multidimensional Equations
Computational Fluid Dynamics
For a two-dimensional flow discretize the variables on a two-dimensional grid
fi , j = f (x,y)
fi+1, j = f (x + h, y)
fi , j+1 = f (x, y + h)(x, y)
i -1 i i+1
j+1
j
j-1
Multidimensional EquationsComputational Fluid Dynamics
fi, jn+1 − fi, j
n
Δt= −U
fi+1, jn − fi−1, j
n
2h
⎛
⎝⎜
⎞
⎠⎟ −V
fi, j+1n − fi, j−1
n
2h
⎛
⎝⎜
⎞
⎠⎟ +
Dfi+1, j
n + fi−1, jn − 2 fi, j
n
h2 +fi, j+1
n + fi, j−1n − 2 fi, j
n
h2
⎛
⎝⎜
⎞
⎠⎟
∂ f∂t
+U∂ f∂x
+V∂ f∂ y
= D∂ 2 f∂x2 +
∂ 2 f∂ y2
⎛
⎝⎜⎞
⎠⎟
The discrete equation is:
Multidimensional Equations
Computational Fluid Dynamics
fi, jn+1 = fi, j
n + Δt −Ufi+1, j
n − fi−1, jn
2h
⎛
⎝⎜
⎞
⎠⎟ −V
fi, j+1n − fi, j−1
n
2h
⎛
⎝⎜
⎞
⎠⎟ +
⎡
⎣⎢⎢
Dfi+1, j
n + fi−1, jn + fi, j+1
n + fi, j−1n − 4 fi, j
n
h2
⎛
⎝⎜
⎞
⎠⎟⎤
⎦⎥⎥
Solve for fi , jn+1
fi, jn+1 = fi, j
n −ΔtU2h
fi+1, jn − fi−1, j
n( ) − ΔtV2h
fi, j+1n − fi, j−1
n( )+ΔtDh2 fi+1, j
n + fi−1, jn + fi, j+1
n + fi, j−1n − 4 fi, j
n( )
or
Multidimensional Equations
Δ tDh2
≤14
(|U | + | V |) 2ΔtD
≤ 4andA stability analysis gives:
Accuracy: O Δt,h2( )
Computational Fluid Dynamics
Example
Computational Fluid Dynamics
f=1.0
∂f∂x
= 0∂f∂y
= 0
f=0.0
f=0.0
∂ f∂t
+U∂ f∂x
+V∂ f∂ y
= D∂ 2 f∂x2 +
∂ 2 f∂ y2
⎛
⎝⎜⎞
⎠⎟
U=-1V=0
Uniform flow through the domain
Multidimensional EquationsComputational Fluid Dynamics
i=1 i=2 i=NXj=1j=2
j=NY
fi , jstored at each grid point
�
fi, jn +1 = fi, j
n −ΔtU2h
fi+1, jn − fi−1, j
n( ) +ΔtDh2
fi+1, jn + fi−1, j
n + fi, j+1n + fi, j−1
n − 4 fi, jn( )
∂ f∂n
�
for
given on the boundary
Multidimensional Equations
Computational Fluid Dynamics
Boundary conditionsWhere f is given, we simply specify its value
Where the normal derivative is specified, we approximate the value at the boundary by one-sided differences
∂ f∂ y
= 0At the i=1 boundary, for example,
∂ f∂ y
≈fi,2
n − fi,1n
h= 0and by using
we find that: fi,2
n = fi,1n
Multidimensional EquationsComputational Fluid Dynamics
% two-dimensional unsteady diffusion by the FTCS scheme %------------------------------------------------------------ n=32;m=32;nstep=120;D=0.025;length=2.0;h=length/(n-1); dt=1.0*0.125*h*h/D;f=zeros(n,m);fo=zeros(n,m);time=0.0; u=-0.0; v=-1.0; f(12:21,n)=1.0; for l=1:nstep,l,time hold off;mesh(f); axis([0 n 0 m 0 1.5]);pause; fo=f; for i=2:n-1, for j=2:m-1 f(i,j)=fo(i,j)-(0.5*dt*u/h)*(fo(i+1,j)-fo(i-1,j))-...
(0.5*dt*v/h)*(fo(i,j+1)-fo(i,j-1))+... (D*dt/h^2)*(fo(i+1,j)+ fo(i,j+1)+fo(i-1,j)+fo(i,j -1)-4*fo(i,j)); end,end for i=1:n, f(i,1)=f(i,2);end;for j=1:m,f(1,j)=f(2,j);f(m,j)=f(m-1,j);end; time=time+dt; end;
Multidimensional Equations
Computational Fluid Dynamics
The unsteady evolution of the solution
Multidimensional EquationsComputational Fluid Dynamics
Multidimensional Boundary Value
Problems(Steady-State)
Computational Fluid Dynamics
∂ 2 f∂x2
+∂ 2 f∂y2
= S
This equation has a solution if f or is specified on the boundary
Boundary Value Problems
Consider the Poisson Equation:
Use standard finite differences to discretize:
�
fi+1, j + f i−1, j − 2 fi, jh2
+f i, j−1 + f i, j+1 − 2 f i, j
h2= Si, j
∂f ∂n
Computational Fluid Dynamics
�
fi , j
= 14
f i+1, j + fi−1, j + f i, j−1 + f i, j+1 − h2Si, j( )
Solve for fi,j:
fi+1, j + fi−1, j + fi, j−1 + fi, j+1 − 4 fi, j
h2 = S
can be written as
For uniform grids:
fi+1, j + fi−1, j − 2 fi, j
h2 +fi, j−1 + fi, j+1 − 2 fi, j
h2 = Si, j
Boundary Value Problems
Computational Fluid Dynamics
�
fi , j
α+1 = 14
f i+1, jα + f i−1, j
α + fi, j−1α + f i, j+1
α − h2Si, j( )
Solve for fi,j and use the right hand side to compute a new value. Denote the old values by α and the new ones with α+1
This iteration process—Jacobi iteration—is very robust but many iterations are required to reach an accurate solution.
Boundary Value ProblemsComputational Fluid Dynamics
The iteration must be carried out until the solution is sufficiently accurate. To measure the error, define the residual:
Ri, j =
fi+1, j + fi−1, j + fi, j−1 + fi, j+1 − 4 fi, j
h2 − Si, j
At steady-state the residual should be zero. The point-wise residual or the average absolute residual can be used, depending on the problem. Often, simpler criteria, such as the change from one iteration to the next is used
Boundary Value Problems
Computational Fluid Dynamics
Although the Jacobi iteration is a very robust iteration technique, it converges VERY slowly.
We therefore seek a way to ACCELERATE the convergence to steady-state, making use of the fact that it is only the steady-state that is of interest.
Here we introduce the Gauss-Seidler method and the Successive Over-Relaxation (SOR) method.
Boundary Value ProblemsComputational Fluid Dynamics
�
fi, jα+1 = 1
4( f i+1, j
α + f i−1, jα+1 + fi, j+1
α + f i, j−1α+1 − h2Si, j )
The Jacobi iteration can be improved somewhat by using new values as soon as they become available.
j
j-1
j+1
i i+1i-1
for j=1:mfor i=1:n iterateendend
From a programming point of view, Gauss-Seidler iteration is even simpler than Jacobi iteration since only one vector with f values is needed.
Boundary Value Problems
Computational Fluid Dynamics
fi, jα+1 =
β4( fi+1, j
α + fi−1, jα+1 + fi, j+1
α + fi, j−1α+1 − h2Si, j )
+ (1−β ) fi, jα
The Gauss-Seidler iteration can be accelerated even further by various acceleration techniques. The simplest one is the Successive Over-Relaxation (SOR) iteration
The SOR iteration is very simple to program, just as the Gauss-Seidler iteration. The user must select the coefficient. It must be bounded by 1<β<2. β=1.5 is usually a good starting value.
Boundary Value ProblemsComputational Fluid Dynamics
Example
Computational Fluid Dynamics
5 10 15 20 25 30 35 40
5
10
15
20
25
30
35
40
f=1.0 f=0.0
�
∂ 2 f∂x 2
+∂ 2 f∂y2
= 0
Boundary Value ProblemsComputational Fluid Dynamics
% two-dimensional steady-state problem by SORn=40;m=40;iterations=5000;length=2.0;h=length/(n-1);T=zeros(n,m);bb=1.7;T(10:n-10,1)=1.0; for l=1:iterations, for i=2:n-1, for j=2:m-1 T(i,j)=bb*0.25*(T(i+1,j)+... T(i,j+1)+T(i-1,j)+T(i,j-1))+(1.0-bb)*T(i,j); end,end % find residual res=0; for i=2:n-1, for j=2:m-1 res=res+abs(T(i+1,j)+... T(i,j+1)+T(i-1,j)+T(i,j-1)-4*T(i,j))/h^2; end,end l,res/((m-2)*(n-2)) % Print iteration and residual if (res/((m-2)*(n-2)) < 0.001), break,endend;contour(T);
Boundary Value Problems
Computational Fluid Dynamics
Average absolute error: 0.001 Number of iterations
Jacobi: 1989Gauss-Seidler: 986
SOR (β =1.5): 320SOR (β = 1.7): 162SOR (β = 1.9): 91SOR (β = 1.95): 202
The program is easily modified for the Jacobi and the Gauss-Seidler iteration:
Boundary Value ProblemsComputational Fluid Dynamics
5 10 15 20 25 30 35 40
5
10
15
20
25
30
35
40
The converged solution:
�
∂ 2 f∂x 2
+∂ 2 f∂y2
= 0
Boundary Value Problems
010
2030
40
0
10
20
30
400
0.2
0.4
0.6
0.8
1
Computational Fluid Dynamics
Accuracy—smaller time step and finer resolution should get us the exact solution
Stability—introduced the von Neumann method. Fairly mechanical process, we will provide more insight by the finite volume point of view
The modified equations helps us see how the approximate and the exact equation differ and if the former is consistent with the latter
Multidimensional advection-diffusion equation. Essentially the same as the one-dimensional problem
Iterative methods for boundary value problems. Elementary approaches to steady state problems
Summary