Beruflich Dokumente
Kultur Dokumente
Problem 3.1
(x, t) D R+ ,
(1)
(2)
(3)
(4)
a) Write a finite volume MATLAB code for the Burgers equation (1) using the Godunov flux functions. Compute the numerical solution for the initial conditions (2), (3) and (4), using outflow boundary conditions.
Plot the solutions at appropiate time T (when the solution structure is well developed).
Solution. From the theory in the lecture notes, we know that the Godunov scheme is on the form
t n
n
Ujn+1 = Ujn
Fj+1/2 Fj1/2
x
where Ujn is an approximation to the cell average of cell (i, n) and (at least in the case of the Burgers
equation, by eq. (4.15))
n
n
n
Fj+1/2
= F (Ujn , Uj+1
) = max(f (max(Ujn , 0)), f (min(Uj+1
, 0))
(since the flux in the case of Burgers has a unique minimum at u = 0).
We write the following MATLAB program, see Figure 1
1
f u n c t i o n [ u , x ] = ex31 (N, T, u0 )
% Compute t h e s o l u t i o n t o t h e B u r g e r s e q u a t i o n a t time T
% u s i n g t h e Godunov f l u x
%
N t h e number o f s p a t i a l c e l l s t o u s e
%
T t h e f i n a l time .
%
u0 t h e i n i t i a l v a l u e
11
% This i s t h e B u r g e r s f l u x
f = @( u ) ( 0 . 5 u . 2 ) ;
% The d i f f e r e n t i a l o f t h e B u r g e r s f l u x
Df = @( u ) ( u ) ;
13
15
% The Gudonov f l u x
F = @(U, V) max( f (max(U, 0 ) ) , f ( min (V, 0 ) ) ) ;
17
19
% We a r e computing on t h e i n t e r v a l [ x1 , x2 ] :
x1 = 0 ;
x2 = 1 ;
21
23
% C r e a t e t h e g r i d , n o t i c e we w i l l u s e g h o s t c e l l s , s o we j u s t pad t h e
% x a r r a y a t both ends with t h e p r e v i o u s v a l u e .
x = [ x1 l i n s p a c e ( x1 , x2 , N) x2 ] ;
25
d e l t a X = x ( 3 )x ( 2 ) ;
27
29
31
33
35
% Setup a r r a y t o s t o r e s o l u t i o n .
u = zeros ( size (x) ) ;
u P r e v i o u s = u0 ( x ) ;
% Need t o obey t h e CFL c o n d i t i o n :
% ( Note : This must be updated e v e r y t i m e s t e p ! )
% ( Note : We u s e max ( 1 , . . . ) t o e n s u r e we don t d i v i d e by 0 ) .
d e l t a T = 0 . 5 d e l t a X / (max ( 1 , max( abs ( Df ( u P r e v i o u s ) ) ) ) ) ;
37
t = 0;
39
41
43
w h i l e t <= T
% Update t h e i n t e r n a l v a l u e s o f u
u ( 2 : end 1) = u P r e v i o u s ( 2 : end 1) . . .
d e l t a T / d e l t a X (F( u P r e v i o u s ( 2 : end 1) , u P r e v i o u s ( 3 : end ) )
F( u P r e v i o u s ( 1 : end 2) , u P r e v i o u s ( 2 : end 1) ) ) ;
45
47
49
51
53
% Update t h e c u r r e n t time
t = t + deltaT ;
55
57
uPrevious = u ;
59
end
ex31.m
...
b) Use the exact solution to calculate L1 (D) and L (D) errors. Plot L1 (D) and L (D) errors vs numbers
of cells (with number of cells 100, 200, 400, 800, 1600).
Solution. We know that the solution of (2) is a moving shock
(
1 x < 1/2t
u(x, t) =
0 otherwise.
and for (3) we get a rarefaction wave
x<0
0
u(x, t) = x/t 0 < x < t < 2 ,
1
otherwise.
and for (4) we get the solution from the previous exercise set (2.3d) that
0
x<0
1
t < x < 1 + t/2
0
otherwise.
and the solution after t = 2 will be
t>2
0
u(x, t) = x/t
x<0
0 < x < (2t)1/2
otherwise.
We use the following code and obtain the figures shown in Figure 2.
2
f u n c t i o n ex31b ( u0 , u , T)
% Compute c o n v e r g e n c e r a t e s f o r
% time T
i n i t i a l data u0 and e x a c t s o l u t i o n u , a t
% These a r e t h e r e s o l u t i o n s we a r e g o i n g t o compute f o r
r e s o l u t i o n s = [ 1 0 0 200 400 800 1 6 0 0 ] ;
10
% W i l l i n t h e end c o n t a i n t h e e r r o r s f o r each r e s o l u t i o n
errorL1 = [ ] ;
errorLinf = [ ] ;
12
14
16
dx = x ( 3 )x ( 2 ) ;
18
e x a c t = u ( x , T) ;
20
end
26
l o g l o g ( r e s o l u t i o n s , e r r o r L 1 , b )
h o l d ( on )
l o g l o g ( r e s o l u t i o n s , e r r o r L i n f , r . )
l e g e n d ( L 1 , L\ i n f t y ) ;
xlabel ( resolution ) ;
ylabel ( error ) ;
28
30
ex31b.m
we run this as
4
Problem 3.2
(x, t) D R+ ,
here D = (2, 3) (NOT the same as in previous exercise) for the following initial conditions:
2 if x < 0;
U (x, 0) = 2
if 0 < x < 1;
0
if x > 1.
(5)
(6)
f u n c t i o n [ uApprox , mesh , r u n t i m e ] = e x 3 2 a s o l v e r ( s o l v e r T y p e , p l o t S o l u t i o n , r e s o l u t i o n )
%% S e t c o n f i g u r a t i o n s
conf = Configuration () ;
11
13
15
c o n f . model = Model . B u r g e r s ;
conf . s o l v e r = solverType ;
c o n f . t i m e I n t = @ T i m e I n t e g r a t i o n . FE ;
c o n f . tMax = 0 . 2 ;
c o n f . CFL = 0 . 4 ;
c o n f . bc = Mesh .BC. Neumann ;
c o n f . mesh = Mesh . C a r t e s i a n ( [ 2 , 3 ] , r e s o l u t i o n ) ;
c o n f . i n i t i a l = @( x ) 2(x<0) + 2 ( x>0) . ( x<1) ;
17
19
21
%% Run s o l v e r
t i c ; % Start timer
soln = runSolver ( conf ) ;
r u n t i m e = t o c ; % end t i m e r
if
23
25
27
plotSolution
%% D i s p l a y data
Plot . p lo t So lu ti o n ( soln ) ;
end
[ t , U] = s o l n . g e t F i n a l ( ) ;
uApprox = c o n f . model . g e t V a r i a b l e ( s o l n , U, 1 ) ;
29
mesh = c o n f . mesh ;
31
end
ex32a solver.m
1
% L i s t o f s o l v e r s we w i l l run f o r
s o l v e r s = { Flux . B u r g e r s . Godunov , Flux . B u r g e r s . Roe , Flux . LaxFr , . . .
Flux . Rusanov , Flux . B u r g e r s . EngOsh } ;
f o r n=1: l e n g t h ( s o l v e r s )
e x 3 2 a s o l v e r ( s o l v e r s {n } , 1 , 1 0 0 ) ;
end
ex32a.m
Figure 3: Plots for the Burgers equation with different numerical fluxes at T = 0.20 with 100 mesh points.
b) Plot L1 (D) and L (D) errors versus numbers of cells and versus computational time. Which of the fluxes
is the most efficient?
Solution. We first find that the exact solution to this initial value problem (for t < 1/2) is given as
2 x < 2t
2
2t x < t + 1
0
otherwise
We use the following two programs to generate the error convergence
1
11
% These a r e t h e r e s o l u t i o n s we a r e g o i n g t o compute f o r
r e s o l u t i o n s = [ 1 0 0 200 400 800 1 6 0 0 ] ;
13
15
17
19
% W i l l i n t h e end c o n t a i n t h e e r r o r s f o r each r e s o l u t i o n
errorL1 = [ ] ;
errorLinf = [ ] ;
21
23
25
% W i l l a t t h e end c o n t a i n t h e run t i m e s
runTimes = [ ] ;
for resolution = resolutions
[ uApproximate , mesh , runTime ] = e x 3 2 a s o l v e r ( s o l v e r , 0 , r e s o l u t i o n ) ;
27
dx = ( ba ) / r e s o l u t i o n ;
29
% We f i r s t u s e t h e b u i l t i n f u n c t i o n e va lF unc t o e v a l u a t e t h e e x a c t
% s o l u t i o n on t h e mesh , then we remove t h e e x t r a g h o s t c e l l s . Both
% o f t h e s e f u n c t i o n s a r e a p a r t o f Compack
e x a c t = mesh . r e m o v e G h o s t C e l l s ( mesh . eva lF un c (@( x ) u ( x , T) , 1 ) ) ;
31
33
end
41
l o g l o g ( r e s o l u t i o n s , e r r o r L 1 , b )
h o l d ( on )
l o g l o g ( r e s o l u t i o n s , e r r o r L i n f , r . )
l e g e n d ( L 1 , L\ i n f t y ) ;
xlabel ( resolution ) ;
ylabel ( error ) ;
t i t l e ( s o l v e r . name )
43
45
47
49
% For o p t i o n a l fun , we d e t e r m i n e t h e c o n v e r g e n c e r a t e
p o l y f i t ( log ( r e s o l u t i o n s ) , log ( errorL1 ) , 1)
51
53
55
57
figure ()
l o g l o g ( runTimes , e r r o r L 1 , b )
h o l d ( on )
l o g l o g ( runTimes , e r r o r L i n f , r . )
l e g e n d ( L 1 , L\ i n f t y ) ;
10
59
x l a b e l ( ru n t i m e ) ;
ylabel ( error ) ;
t i t l e ( s o l v e r . name )
ex32b solver.m
% L i s t o f s o l v e r s we w i l l run f o r
s o l v e r s = { Flux . B u r g e r s . Godunov , Flux . B u r g e r s . Roe , Flux . LaxFr , . . .
Flux . Rusanov , Flux . B u r g e r s . EngOsh } ;
f o r n=1: l e n g t h ( s o l v e r s )
figure ()
e x 3 2 b s o l v e r ( s o l v e r s {n } ) ;
end
ex32b.m
and get the plots in figure Figure 4 and Figure 5
11
Figure 4: Convergence plots for the Burgers equation with different numerical fluxes at T = 0.20.
12
Figure 5: Convergence plots vs runtime for the Burgers equation with different numerical fluxes at T = 0.20.
13
Problem 3.3
Non-convex flux
Ut + f (U )x = 0
(7)
U2
U 2 + (1 U )2
0.1 if x < 0;
0.9 if x > 0.
(8)
p r o p e r t i e s ( SetAccess = private )
name = B u c k l e y L e v e r e t t
nelem = 1
end
9
11
13
methods
f u n c t i o n r e t = f ( obj , U, d )
% Evaluate f
r e t = U. 2 . / (U.2+(1 U) . 2 ) ;
end
15
f u n c t i o n r e t = d f ( obj , U, d )
% This w i l l compute t h e d e r i v a t i v e o f f ,
% i t w i l l make our l i v e s e a s i e r
r e t = ( 2 U . ( ( U.2+(1 U) . 2 ) )U. 2 . ( 2 U2(1U) ) ) . / ( ( U.2+(1 U. 2 ) ) . 2 ) ;
end
17
19
21
end
methods ( A c c e s s=p r o t e c t e d )
f u n c t i o n r e t = maxEigRect ( obj , U, d )
% In the s c a l a r case , t h i s should r e t u r n the a b s o l u t e
% value of the d i f f e r e n t i a l of the f l u x f u n c t i o n
23
25
27
29
d i s p ( s p r i n t f ( %f \n , max( r e t ) ) ) ;
31
end
33
35
37
end
end
BuckleyLeverett.m
We make the following program to test all the different numerical fluxes:
14
f u n c t i o n e x 3 3 a s o l v e r ( solverType , r e s o l u t i o n )
%% S e t c o n f i g u r a t i o n s
conf = Configuration () ;
11
13
15
c o n f . model = B u c k l e y L e v e r e t t ;
conf . s o l v e r = solverType ;
c o n f . t i m e I n t = @ T i m e I n t e g r a t i o n . FE ;
c o n f . tMax = 0 . 8 ;
c o n f . CFL = 0 . 2 ;
c o n f . bc = Mesh .BC. Neumann ;
c o n f . mesh = Mesh . C a r t e s i a n ( [ 3 , 3 ] , r e s o l u t i o n ) ;
c o n f . i n i t i a l = @( x ) 0 . 1 ( x<0) + . 9 ( x>=0) ;
17
19
%% Run s o l v e r
soln = runSolver ( conf ) ;
21
Plot . p lo t So lu ti on ( soln ) ;
23
end
ex33a solver.m
Compack has a standard implementation for Lax-Friedrich numerical flux, and the Rusanov flux, which
means that they will work out of the box for this non-convex flux.
Godunov
n
Fj+1/2
=F
n
Ujn , Uj+1
min n f ()
U n U
n
if Ujn Uj+1
f ()
U n max
U n
n
if Uj+1
Ujn .
j+1
j+1
10
12
BurgersGodunov.m
relies on Exercise 4.1 in the notes:
Exercise 4.1
Computing the flux (9) can be complicated, since an optimization problem has to be
solved. Show that in the special case where the flux function f has a single minimum at
the point and no local maxima,
the formula (9) can be simplified
to
,
n
n
n
n
n
Fj+1/2 = F (Uj , Uj+1 ) = max f max Uj , , f min Uj+1 ,
.
Note that strictly convex functions have this property. The formulas for the case of a flux
with a single maximum and no minima are obtained analogously.
15
(9)
Figure 6: The non-convex flux f . Notice that it has a local maxima at 1, but that within the region [0.1, 0.9]
we have no local maxima.0
where the minima of the flux for Burgers is at 0. However, our flux function does have a local maxima
at 1 (see Figure 6). However, notice that our initial data is in the interval [0.1, 0.9], so we know that the
solution should be within [0.1, 0.9] for the standard L bounds for entropy solutions (Thm 3.9). Hence
we may use exercise 4.1 directly, and hence the Godunov flux for Burgers equation. 1
1 One could also argue physically that the Buckeley-Leverett flux represents some sort of concentration, and hence the values
must be within [0, 1].
16
Roe
=F
Roe
n
Ujn , Uj+1
where
f (Ujn )
n
f (Uj+1
)
( f (U n
Aj+1/2 =
n
j+1 )f (Uj )
n
n
Uj+1 Uj
f 0 (Ujn )
if Aj+1/2 0
if Aj+1/2 < 0.
(10)
n
if Uj+1
6= Ujn
n
if Uj+1
= Ujn .
properties
name = B u c k l e y L e v e r e t t R o e
end
11
13
15
methods
f u n c t i o n r e t = F( obj , d , Ul , Ur , v a r a r g i n )
% F i r s t we must compute A:
i f Ul == Ur
A = o b j . d f ( Ul , d ) ;
else
A = ( o b j . f ( Ur , d ) o b j . f ( Ul , d ) ) / ( Ur Ul ) ;
end
% Then we have t h e two c a s e s we must c h e c k .
i f A >= 0
r e t = o b j . f ( Ul , d ) ;
else
r e t = o b j . f ( Ur , d ) ;
end
17
19
21
23
25
end
end
end
BuckleyLeverettRoe.m
Engquist-Osher
2
2
n
Uj+1
|f 0 ()| d.
(11)
Ujn
Now, we can use the simplified version in eq. (4.35) in the notes, which reads
n
n
F EO Ujn , Uj+1
= f + Ujn + f Uj+1
.
as for our initial data f has only a single minima and no local maxima A quick look at the implementation
for Burgers in Compack, we see that it also uses this form, so we may use this implementation.
We use the following program to run all the fluxes and get the output in Figure 7. Notice that we get a
compound wave structure, with a shock followed by a rarefaction wave.
17
Figure 7: Plots for the Godunov flux with different numerical fluxes at T = 0.20.
18
b) Prove that if the flux function f is monotone on some interval [UL , UR ] then the Godunov, Roe and
Engquist-Osher numerical flux approximations are equivalent.
Solution. For the rest of the argument, we assume f is non-decreasing, the other case is treated in a
similar fashion.
First we look at the Godunov scheme. If f is monotone, then again we may appeal to exercise 4.1 to see
that the numerical flux is given as
FG = F (UL , UR ) = max f (max (UL , )) , f (min (UR , )) .
If UL > UR , then also UL (since f (UR ) f (UL )), and we are left with
FG = f (UL )
if UR > UL , then UR > , so min(UR , ) = , and since is the minima for f , we know that f (UL ) f ()
and we are left with
FG = f (UL )
also in this case.
The Roe numerical flux is given as
(
FRoe = F Roe UL , UR ) =
where
(
Aj+1/2 =
f (UL ) if Aj+1/2 0
f (UR ) if Aj+1/2 < 0.
f (UR )f (UL )
UR UL
f 0 (UL )
(12)
if UR 6= UL
if UR = UL .
If UL = UR , then Aj+1/2 = f 0 (UL ), and since f was non-decreasing, we know that Aj+1/2 0, hence we
get
FRoe = f (UL )
(UL )
Otherwise Aj+1/2 = f (UURR)f
0 since f is non-decreasing (either UL > UR , then one gets a negative
UL
sign in both part of the fraction, hence positive, or UR > UL in which case both parts are positive), and
we are again left with
FRoe = f (UL ).
Problem 3.4
Monotonicity
3. Roe
for the conservation law
(x, t) R R+
ut + f (u)x = 0
assuming the CFL condition
t
1.
x
The Lax-Friedrich flux is given as
max |f 0 (u)|
Solution. Lax-Friedrich:
n
f (Ujn ) + f (Uj+1
)
x
n
n
U n Ujn .
Fj+1/2
= F LxF Ujn , Uj+1
=
2
2t j+1
Differentiating, we find
F LxF
f 0 (a)
x
(a, b) =
+
,
x1
2
2t
and
F LxF
f 0 (b)
x
(a, b) =
.
x1
2
2t
t
x
1 max |f 0 (u)|
x
t
x2 (a, b) = 2 2t =
2
2t
adding the two together, we get
F
F
x
x1 (b, c) + x2 (a, b) = t
which was what we wanted.
Now we have to show that
F
x1
0 and
a 7 F (a, b)
b 7 F (a, b)
F
x2
0. We compute
t
F
= f 0 (a)/2 +
x1
2x
If f 0 (a) 0 then we are done, otherwise the CFL condition implies that
f 0 (a) = |f 0 (a)|
t
2x
hence we get
f 0 (a)/2 +
t
0.
2x
20
(13)
F
is treated similarly.
The case for x
2
Engquist-Osher: The Engquist-Osher flux is given as
n
n
Fj+1/2
= F EO Ujn , Uj+1
=
and
n
Uj+1
(14)
|f 0 ()| d.
Ujn
F EO
(a, b) = f 0 (b)/2 |f 0 (b)|/2
x1
To see that
F
x1
F EO
(a, b) = f 0 (a)/2 + |f 0 (a)|/2
x1
and
EO
n
f (Ujn ) + f (Uj+1
)
1
2
2
F EO
x1 (a, b)
0, observe that if f 0 (a) 0, then this clearly holds, otherwise |f 0 (a)| = f 0 (a) so
F EO
x2 (a, b)
F
F
0
0
0
0
x1 (b, c) + x2 (a, b) = |f (a)/2 + |f (a)|/2| + |f (a)/2 |f (a)|/2|.
Now, notice that in the above expression, only one of the terms will be non-zero for a fixed a (if f 0 (a) 0, then
the second term vanishes, otherwise the first term vanishes). So we are left with
|f 0 (a)/2 + |f 0 (a)|/2| + |f 0 (a)/2 |f 0 (a)|/2| |f 0 (a)|
t
x
n
j+1 )f (Uj )
n U n
Uj+1
j
f 0 (Ujn )
and
n
n
Fj+1/2
= F Roe Ujn , Uj+1
n
if Uj+1
6= Ujn
n
if Uj+1
= Ujn .
(
f (Ujn )
=
n
f (Uj+1
)
if
if
Aj+1/2 0
Aj+1/2 < 0.
(15)
(16)
t Roe
(F
(b, c) F Roe (a, b)).
x
Recall that a scheme is monotone if H is non-decreasing in each of its arguments. To see that this in general is
not non-decreasing, let f (u) = u2 /2, and let 0 > a > b > c, then A(b, c) > 0 and A(a, b) > 0 so
F Roe
(b, c) = f 0 (b) = b
x1
and
So
F Roe
(a, b) = f 0 (b) = b
x2
H
(a, b, c) < 0
c
21