Download Finite difference method - Mathematik, TU Dortmund

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Finite difference method
Principle: derivatives in the partial differential equation are approximated
by linear combinations of function values at the grid points
1D:
Ω = (0, X),
grid points
0
x0 x1
ui ≈ u(xi ),
xi = i∆x
xi
1
i = 0, 1, . . . , N
mesh size
xi xi+1
∆x =
X
N
X
xN 1 xN
First-order derivatives
∂u
(x̄)
∂x
u(x̄ + ∆x) − u(x̄)
u(x̄) − u(x̄ − ∆x)
= lim
∆x→0
∆x→0
∆x
∆x
u(x̄ + ∆x) − u(x̄ − ∆x)
(by definition)
= lim
∆x→0
2∆x
=
lim
Approximation of first-order derivatives
Geometric interpretation
u
forward bakward
entral
≈
ui+1 −ui
∆x
forward difference
≈
ui −ui−1
∆x
backward difference
≈
ui+1 −ui−1
2∆x
central difference
∂u
∂x i
exat
x x
∂u
∂x i
∂u
∂x i
x ∞
xi 1 xi xi+1
P
Taylor series expansion
u(x) =
n=0
T1 :
ui+1
T2 :
ui−1
(x−xi )n
n!
∂nu
∂xn i
,
u ∈ C ∞ ([0, X])
(∆x)3 ∂ 3 u
(∆x)2 ∂ 2 u
+ ...
= ui + ∆x
+
+
2
3
2
∂x
6
∂x
i
i
i
∂u
(∆x)2 ∂ 2 u
(∆x)3 ∂ 3 u
= ui − ∆x
−
+
+ ...
∂x i
2
∂x2 i
6
∂x3 i
∂u
∂x
Analysis of truncation errors
Accuracy of finite difference approximations
T1
T2
⇒
⇒
∂u
∂x
∂u
∂x
i
i
ui+1 − ui ∆x
−
=
∆x
2
forward difference
truncation error O(∆x)
ui − ui−1 ∆x
+
=
∆x
2
backward difference
T1 − T2
⇒
∂u
∂x
i
∂2u
∂x2
∂2u
∂x2
i
i
(∆x)2
−
6
(∆x)2
−
6
∂3u
∂x3
∂3u
∂x3
truncation error O(∆x)
ui+1 − ui−1 (∆x)2
−
=
2∆x
6
central difference
∂3u
∂x3
+ ...
i
truncation error O(∆x)2
Leading truncation error
ǫτ = αm (∆x)m + αm+1 (∆x)m+1 + . . . ≈ αm (∆x)m
+ ...
i
+ ...
i
Approximation of second-order derivatives
Central difference scheme
T1 + T2
⇒
∂2u
∂x2
=
i
ui+1 − 2ui + ui−1
2
+
O(∆x)
(∆x)2
Alternative derivation
2 ∂ ∂u
∂ u
=
= lim
∂x2 i
∂x ∂x i ∆x→0
≈
Variable coefficients
∂f
≈
∂x i
=
ui+1 −ui
∆x
∂u
∂x i+1/2
−
∆x
∂u
∂x i−1/2
i−1
− ui −u
ui+1 − 2ui + ui−1
∆x
=
∆x
(∆x)2
f (x) = d(x) ∂u
∂x
diffusive flux
−ui
i−1
− di−1/2 ui −u
fi+1/2 − fi−1/2
di+1/2 ui+1
∆x
∆x
=
∆x
∆x
di+1/2 ui+1 − (di+1/2 + di−1/2 )ui + di−1/2 ui−1
(∆x)2
Approximation of mixed derivatives
2D:
∂2u
∂x∂y
=
∂
∂x
=
“
∂u
∂y
∂u
∂y
”
=
∂
∂y
−
“
∂u
∂x
∂u
∂y
”
y
„
∂2u
∂x∂y
„
∂u
∂y
«
=
ui+1,j+1 − ui+1,j−1
+ O(∆y)2
2∆y
„
∂u
∂y
«
=
ui−1,j+1 − ui−1,j−1
+ O(∆y)2
2∆y
«
i,j
2∆x
i−1,j
+ O(∆x)2
yj +1
yj
i+1,j
i−1,j
i+1,j
yj 1
xi 1
xi
xi+1
Second-order difference approximation
2 ∂ u
ui+1,j+1 − ui+1,j−1 − ui−1,j+1 + ui−1,j−1
=
+ O[(∆x)2 , (∆y)2 ]
∂x∂y i,j
4∆x∆y
x
One-sided finite differences
?
x0
x1
x2
∂u
∂x
=
0
u1 − u0
+ O(∆x) forward difference
∆x
backward/central difference approximations
would need u−1 which is not available
Polynomial fitting
u(x) = u0 + x
∂u
∂x
x2
+
2
0
∂2u
∂x2
x3
+
6
0
∂u
≈ b + 2cx,
∂x
u(x) ≈ a + bx + cx2 ,
∂3u
+ ...
∂x3 0
∂u
≈b
∂x 0
approximate u by a polynomial and differentiate it to obtain the derivatives
c∆x2 = u1 − u0 − b∆x
u0 = a
u1 = a + b∆x + c∆x2
u2 = a + 2b∆x + 4c∆x2
⇒
b=
−3u0 + 4u1 − u2
2∆x
Analysis of the truncation error
One-sided approximation
ui+1
ui+2
∂u
∂x
≈
i
αui + βui+1 + γui+2
∆x
(∆x)2 ∂ 2 u
(∆x)3 ∂ 3 u
∂u
+
+
+ ...
= ui + ∆x
∂x i
2
∂x2 i
6
∂x3 i
2 3 2
3
∂u
(2∆x)
(2∆x)
∂ u
∂ u
= ui + 2∆x
+
+
+ ...
∂x i
2
∂x2 i
6
∂x3 i
αui +βui+1 +γui+2
∆x
=
α+β+γ
∆x ui
Second-order accurate if
α = − 23 ,
β = 2,
+ (β + 2γ)
∂u
∂x i
+
α + β + γ = 0,
γ = − 21
⇒
∂u
∂x i
∆x
2 (β
+ 4γ)
β + 2γ = 1,
=
∂2u
∂x2
i
+ O(∆x2 )
β + 4γ = 0
−3ui +4ui+1 −ui+2
2∆x
+ O(∆x2 )
Application to second-order derivatives
One-sided approximation
ui+1
ui+2
αui + βui+1 + γui+2
∆x2
≈
i
(∆x)3 ∂ 3 u
(∆x)2 ∂ 2 u
∂u
+
+ ...
+
= ui + ∆x
∂x i
2
∂x2 i
6
∂x3 i
2 3 2
3
∂u
(2∆x)
(2∆x)
∂ u
∂ u
= ui + 2∆x
+
+
+ ...
∂x i
2
∂x2 i
6
∂x3 i
αui +βui+1 +γui+2
∆x2
=
α+β+γ
∆x2 ui
First-order accurate if
α = 1,
∂2u
∂x2
β = −2,
+
β+2γ
∆x
∂u
∂x i
α + β + γ = 0,
γ=1
⇒
∂u
∂x i
=
+
β+4γ
2
∂2u
∂x2
β + 2γ = 0,
ui −2ui+1 +ui+2
∆x2
i
+ O(∆x)
β + 4γ = 2
+ O(∆x)
High-order approximations
∂u
∂x
∂u
∂x
∂u
∂x
∂2u
∂x2
=
2ui+1 + 3ui − 6ui−1 + ui−2
+ O(∆x)3
6∆x
=
−ui+2 + 6ui+1 − 3ui − 2ui−1
+ O(∆x)3
6∆x
forward difference
=
−ui+2 + 8ui+1 − 8ui−1 + ui−2
+ O(∆x)4
12∆x
central difference
i
i
i
=
i
backward difference
−ui+2 + 16ui+1 − 30ui + 16ui−1 − ui−2
+ O(∆x)4
2
12(∆x)
central difference
Pros and cons of high-order difference schemes
⊖ more grid points, fill-in, considerable overhead cost
⊕ high resolution, reasonable accuracy on coarse grids
Criterion: total computational cost to achieve a prescribed accuracy
Example: 1D Poisson equation
Boundary value problem
∂2u
− 2 =f
∂x
One-dimensional mesh
ui ≈ u(xi ),
in Ω = (0, 1),
0
x0
fi = f (xi )
x1
xi 1
xi = i∆x,
u(0) = u(1) = 0
xi
1
xi+1
∆x =
xN 1 xN
1
,
N
i = 0, 1, . . . , N
Central difference approximation
O(∆x)2

i+1
 − ui−1 −2ui +u
= fi ,
∀i = 1, . . . , N − 1
(∆x)2

u0 = uN = 0
Dirichlet boundary conditions
Result: the original PDE is replaced by a linear system for nodal values
Example: 1D Poisson equation
Linear system for the central difference scheme

u0 −2u1 +u2

i
=
1
−

(∆x)2




2 +u3

− u1 −2u

(∆x)2
 i=2
3 +u4
i=3
− u2 −2u
2
(∆x)




...




uN −2 −2uN −1 +uN

i=N −1
Matrix form
Au = F
(∆x)2
A ∈ RN −1×N −1

2 −1
−1 2 −1

1 


−1
2
−1
A=
,

(∆x)2 
... 
−1 2

= f1
= f2
= f3
= fN −1
u, F ∈ RN −1



u=

u1
u2
u3
·
uN −1



,




F =

f1
f2
f3
·
fN −1
The matrix A is tridiagonal and symmetric positive definite ⇒ invertible.





Other types of boundary conditions
Dirichlet-Neumann BC
u(0) =
∂u
∂x (1)
uN +1 − uN −1
=0
2∆x
u0 = 0,
=0
⇒
uN +1 = uN −1
central difference
Extra equation for the last node
−
uN −1 − 2uN + uN +1
= fN
(∆x)2
Extended linear system

−→
A ∈ RN ×N
Au = F
2 −1
 −1 2 −1
1 
−1 2 −1

A=

...
(∆x)2 

−1
−uN −1 + uN
1
=
fN
(∆x)2
2




,

2 −1 
−1 1

u, F ∈ RN



u=

u
u1
u2
u3
·
N −1
uN




,


The matrix A remains tridiagonal and symmetric positive definite.




F =

f
f1
f2
f3
·
N −1
1
2 fN







Other types of boundary conditions
Non-homogeneous Dirichlet BC
u0 = g0
⇒
u(0) = g0
2u1 − u2
g0
=
f
+
1
(∆x)2
(∆x)2
∂u
∂x (1)
Non-homogeneous Neumann BC
uN +1 − uN −1
= g1
2∆x
−
⇒
uN −1 − 2uN + uN +1
= fN
(∆x)2
Non-homogeneous Robin BC
uN +1 − uN −1
+ αuN = g2
2∆x
−
only F changes
uN −1 − 2uN + uN +1
= fN
(∆x)2
= g1
⇒
−→
only F changes
uN +1 = uN −1 + 2∆xg1
−→
∂u
∂x (1)
first equation
−uN −1 + uN
1
g1
=
f
+
N
(∆x)2
2
∆x
+ αu(1) = g2
A and F change
uN +1 = uN −1 − 2∆xαuN + 2∆xg2
−uN −1 + (1 + α∆x)uN
1
g2
=
f
+
N
(∆x)2
2
∆x
Example: 2D Poisson equation
y
Boundary value problem

 − ∂ 2 u2 − ∂ 2 u2 = f in Ω = (0, 1) × (0, 1)
∂x
∂y

u=0
ui,j ≈ u(xi , yj ),
N=
ui,0 = ui,N = u0,j = uN,j = 0
h
h
yj 1
0
xi 1
(xi , yj ) = (ih, jh),
Central difference approximation
O(h2 )

+ui+1,j +ui,j+1
 − ui−1,j +ui,j−1 −4ui,j
= fi,j ,
h2

ve-point stenil
yj
1
h
fi,j = f (xi , yj ),
h
yj +1
on Γ = ∂Ω
Uniform mesh: ∆x = ∆y = h,
h
1
xi
xi+1
1
x
i, j = 0, 1, . . . , N
∀i, j = 1, . . . , N − 1
∀i, j = 0, 1, . . . , N
Example: 2D Poisson equation
A ∈ R(N −1)
2
×(N −1)2
u, F ∈ R(N −1)
2
Linear system
Au = F
row-by-row
node numbering
u = [u1,1 . . . uN −1,1 u1,2 . . . uN −1,2 u1,3 . . . uN −1,N −1 ]T
F = [f1,1 . . . fN −1,1 f1,2 . . . fN −1,2 f1,3 . . . fN −1,N −1 ]T

B −I
 −I B −I

... ...
A=

−I



I=


1
1
·
1
1







,
B −I 
−I B

4 −1
 −1 4 −1

... ...
B=

−1




4 −1 
−1 4
The matrix A is sparse, block-tridiagonal
(for the above numbering) and SPD.
cond2 (A) =
|λmax |
= O(h−2 )
|λmin |
Caution: convergence of iterative solvers deteriorates as the mesh is refined
Treatment of complex geometries
2D Poisson equation

 − ∂ 2 u2 − ∂ 2 u2 = f
∂x
∂y

u = g0
Γ
in Ω
on Γ
Difference equation
u1 + u2 − 4u0 + u3 + u4
= f0
−
h2
3
Ω
Γ
δ
Ω
4
Q
0 Q
R
2
h
P
1
stencil of Q
curvilinear boundary
Linear interpolation
u4 (h − δ) + u0 δ
h
δ
= g0 (R) ⇒ u4 = −u0
+ g0 (R)
h
h−δ
h−δ
h
δ
Substitution yields −u1 + u2 − 4 + h−δ u0 + u3 = h2 f0 + g0 (R) h−δ
u(R) =
Neumann and Robin BC are even more difficult to implement
Grid transformations
Purpose: to provide a simple treatment of curvilinear boundaries
d
nozzle
diret mapping
d
retangle
P
P
inverse mapping
y
a
x
physial domain
body-tted grid
b
a
b
omputational domain
artesian grid
The original PDE must be rewritten in terms of (ξ, η) instead of (x, y) and
discretized in the computational domain rather than the physical one.
Derivative transformations
∂u ∂u
,
,...
∂x ∂y
|
{z
}
difficult to compute
−→
∂u ∂u
,
,...
∂ξ ∂η
|
{z
}
easy to compute
PDE transformations for a direct mapping
Direct mapping
Chain rule
ξ = ξ(x, y),
η = η(x, y)
∂u ∂ξ
∂u
∂u ∂η
=
+
,
∂x
∂ξ ∂x ∂η ∂x
∂2u
∂u ∂ 2 ξ
∂u ∂ 2 η
=
+
∂x2
∂ξ ∂x2
∂η ∂x2
∂2u
∂u ∂ 2 ξ
∂u ∂ 2 η
=
+
∂y 2
∂ξ ∂y 2
∂η ∂y 2
∂u ∂ξ
∂u
∂u ∂η
=
+
∂y
∂ξ ∂y
∂η ∂y
2
2
∂ 2 u ∂η
∂ 2 u ∂ξ ∂η
∂ 2 u ∂ξ
+ 2
+2
+ 2
∂ξ∂η ∂x ∂x
∂ξ
∂x
∂η
∂x
2
2
∂ 2 u ∂η
∂ 2 u ∂ξ ∂η ∂ 2 u ∂ξ
+ 2
+2
+ 2
∂ξ∂η ∂y ∂y
∂ξ
∂y
∂η
∂y
−∆u = f
turns into
Example: 2D Poisson equation
" " 2 #
2 #
2
2
2
2
∂ u
∂ξ
∂η
∂ξ ∂η
∂ξ
∂η
∂ u
∂ 2 u ∂ξ ∂η
− 2
+
+
+
− 2
−2
∂ξ
∂x
∂y
∂η
∂x
∂y
∂ξ∂η ∂x ∂x ∂y ∂y
2
2
2
2
∂u ∂ ξ
∂ ξ
∂ η
∂u ∂ η
transformed equations
−
+
+
−
=
f
contain many more terms
∂ξ ∂x2
∂y 2
∂η ∂x2
∂y 2
The metrics need to be determined (approximated by finite differences)
PDE transformations for an inverse mapping
Inverse mapping
x = x(ξ, η)
Metrics transformations
∂ξ ∂ξ ∂η ∂η
,
,
,
∂x ∂y ∂x ∂y
|
{z
}
−→
unknown
Chain rule
∂u
∂ξ
=
∂u
∂η
=
where J =
y = y(ξ, η)
∂u ∂x
∂x ∂ξ
+
∂u ∂x
∂x ∂η
+
∂(x,y)
∂(ξ,η)
∂u ∂y
∂y ∂ξ
∂u ∂y
∂y ∂η
⇒


∂x ∂x ∂y ∂y
,
,
,
∂ξ ∂η ∂ξ ∂η
|
{z
}
known
∂u
∂ξ
∂u
∂η


=
∂y
∂ξ
∂y
∂η
∂x
∂ξ
∂x
∂η
|
{z
J


∂u
∂x
∂u
∂y
}


is the Jacobian which can be inverted using Cramer’s rule
Derivative transformations
∂u
1
∂u ∂y
∂u ∂y
=
−
,
∂x
det J ∂ξ ∂η
∂η ∂ξ
∂u
1
=
∂y
det J
∂u ∂x ∂u ∂x
−
∂η ∂ξ
∂ξ ∂η
Direct versus inverse mapping
Total differentials for both coordinate systems
ξ = ξ(x, y)
η = η(x, y)
x = x(ξ, η)
y = y(ξ, η)
⇒


⇒
⇒
∂ξ
∂x dx
+
dη =
∂η
∂x dx
+
∂η
∂y dy
dx =
∂x
∂ξ dξ
+
∂x
∂η dη
∂y
∂ξ dξ
+
dξ =
dy =
∂ξ
∂x
∂η
∂x
∂ξ
∂y
∂η
∂y


=
∂x
∂ξ
∂y
∂ξ
∂ξ
∂y dy
∂y
∂η dη
∂x
∂η
∂y
∂η
−1





=
Relationship between the direct and inverse metrics
∂ξ
1 ∂y
=
,
∂x
det J ∂η
∂η
1 ∂y
=−
,
∂x
det J ∂ξ
dξ
dη
dx
dy




=
=

1 
det J
∂ξ
∂x
∂η
∂x
∂x
∂ξ
∂y
∂ξ
∂y
∂η
− ∂y
∂ξ
∂ξ
1 ∂x
=−
,
∂y
det J ∂η
∂ξ
∂y
∂η
∂y
∂x
∂η
∂y
∂η




− ∂x
∂η
∂x
∂ξ

dx
dy
dξ
dη





∂η
1 ∂x
=
∂y
det J ∂ξ