Technische Universität Berlin SS 2015 Institut für Mathematik Prof. Dr. G. Bärwolff Sekr. MA 4-5 29.06.2015 6. Exercise sheet FV/FD-Methods for the solution of pde s Discussion: 13.7.15-17.7.15 1) Exercise Construct a Finite-Volume scheme to solve the 3D heat-conduction initial-boundary value problem 3) of exercise sheet 4 in spherical coordinates for Dirichlet bc and for the bc of problem 3) of sheet 4. Determine the numerical steady state solution for the Dirichlet bc u b = u(x, y, z) = 263 K + sin(arccos z x 2 + y 2 + z 2 )45 K on the boundary Γ = {(x, y, z) x 2 + y 2 + z 2 = 25 cm 2 }. Validate your approximation by solving the 1D problem of sheet 4 with the full 3D scheme. Solution: The following program solves the problem with the above noted Dirichlet boundary data. A ghost points r n+1 jk in r-direction is used and the ghost-value u n+1 jk is eliminated using the FV-approximation of the equation at the points r n jk and the interpolation of the boundary value u b by (u n+1 jk + u n jk )/2 = u b. Listing 1: source code 1 % hot potato problem 3D 2 % 3 n = 2 0 ; % z e n t r a l e Gitterpunkte in r a d i a l e r Richtung 4 m = 1 5 ; % z e n t r a l e Gitterpunkte vom Nordpol beginnend auf einem Laengenkreis 5 p = 4 ; % z e n t r a l e Gitterpunkte auf dem Aequator ( B r e i t e n k r e i s ) 6 R0 = 0. ; 7 R1 = 1. 0 / 2 0. ; % Radius der Kugel 8 drho = ( R1 R0 ) /n ; 9 dth = pi/m; 10 dphi = 2 pi/p ; 11 % 12 % Zentralpunkte der F i n i t e n Zellen 13 rho = linspace ( R0+drho /2,R1 drho /2,n ) ; 14 th = linspace ( dth /2,pi dth /2,m) ; 15 s i n t h = sin ( th ) ; 16 % Randpunkte der F i n i t e n Zellen 17 rhop = linspace ( R0, R1, n+1) ; 18 thp = linspace ( 0, pi,m+1) ; 19 sinthp = sin ( thp ) ; 20 for i =1:m 21 s i n t h i n v ( i ) = 1/ s i n t h ( i ) ; 22 end 23 for i =1:n 24 rho2 ( i ) = rho ( i ) ˆ 2 ; 25 end 26 for i =1:m 27 t h s i n t h ( i ) = th ( i ) s i n t h ( i ) ; 28 end 29 % Matrixaufbau Ar, Ath, Aphi
30 Ar = zeros ( n, n ) ; 31 Ath = zeros (m,m) ; 32 Aphi = zeros ( p, p ) ; 33 % 34 ntend = 9 0 0 ; 35 tau = 1. 0 / 1 0. ; 36 Ubound = 3 3 3 ; 37 U0 = 3 7 3. ; 38 Uu = 2 9 1. ; 39 a = 5.6/3600 1.0 e 4; 40 lambda = 0. 1 6 ; 41 alpha = 3 0 ; 42 % Skalierung 43 s k a l = 1. 0 ; 44 % 45 nmp = n m p ; 46 % Matrixaufbau Ar 47 Ualt = zeros (nmp, 1 ) ; 48 R = zeros (nmp, 1 ) ; 49 % 50 % Laplacian in Kugelkoordinaten 51 % 52 %U = [ u ( 1, 1, 1 ),..., u ( n, 1, 1 ), 53 % u ( 1, 2, 1 ),..., u ( n, 2, 1 ), 54 % u ( 1, 3, 1 ),..., u ( n, 3, 1 ), 55 % 56 % u ( 1,m, 1 ),..., u ( n,m, 1 ), 57 % u ( 1, 1, 2 ),..., u ( n, 1, 2 ), 58 % u ( 1, 2, 2 ),..., u ( n, 2, 2 ), 59 % u ( 1, 3, 2 ),..., u ( n, 3, 2 ), 60 % 61 % u ( 1,m, 2 ),..., u ( n,m, 2 ), 62 % 63 %..., u ( 1, 1, p ),..., u ( n, 1, p ), 64 %..., u ( 1, 2, p ),..., u ( n, 2, p ), 65 %..., u ( 1, 3, p ),..., u ( n, 3, p ), 66 % 67 %..., u ( 1,m, p ),..., u ( n,m, p ) ] 68 % 69 for i =2: n 1 70 Ar ( i, i ) = a ( rhop ( i +1) ˆ2 + rhop ( i ) ˆ 2 ) /drho ; 71 Ar ( i, i 1) = a rhop ( i ) ˆ2/ drho ; 72 Ar ( i, i +1) = a rhop ( i +1) ˆ2/ drho ; 73 end 74 Ar ( 1, 1 ) = a ( rhop ( 1 ) ˆ2 + rhop ( 2 ) ˆ 2 ) /drho ; 75 Ar ( 1, 2 ) = a rhop ( 2 ) ˆ2/ drho ; 76 %%% RB : u = u R, ( u {N+1} + u n ) /2 = u R 77 Ar ( n, n ) = a ( rhop ( n ) ˆ2 + 2 rhop ( n+1) ˆ 2 ) /drho ; 78 %%% h i e r muss die j e w e i l i g e RB an der Kugeloberflaeche eingebaut werden, 79 %%% d. h. U {N+ 1... } muss aus RB und Laplacian D i s k r e t i s i e r u n g am Punkt x {N... } e l i m i n i e r t werden 80 Ar ( n, n 1) = a rhop ( n ) ˆ2/ drho ; 81 % 82 for i =2:m 1 83 Ath ( i, i ) = a (sin ( thp ( i ) ) + sin ( thp ( i +1) ) ) /dth ; 84 Ath ( i, i 1) = a sin ( thp ( i ) ) /dth ; 85 Ath ( i, i +1) = a sin ( thp ( i +1) ) /dth ; 86 end 87 Ath ( 1, 1 ) = a (sin ( thp ( 1 ) ) + sin ( thp ( 2 ) ) ) /dth ; 88 Ath ( 1, 2 ) = a sin ( thp ( 2 ) ) /dth ; 89 Ath (m,m) = a (sin ( thp (m) ) + sin ( thp (m+1) ) ) /dth ; 90 Ath (m,m 1) = a sin ( thp (m) ) /dth ; 91 for i =2: p 1 92 Aphi ( i, i ) = a 2./ dphi ; 93 Aphi ( i, i 1) = a 1./ dphi ; 94 Aphi ( i, i +1) = a 1./ dphi ; 95 end 96 Aphi ( 1, 1 ) = a 2./ dphi ; 97 Aphi ( 1, 2 ) = a 1./ dphi ; 98 Aphi ( p, p ) = a 2./ dphi ; 99 Aphi ( p, p 1) = a 1./ dphi ;
100 Aphi ( 1, p ) = a 1./ dphi ; 101 Aphi ( p, 1 ) = a 1./ dphi ; 102 % 103 Idn = eye ( n, n ) ; 104 Idm = eye (m,m) ; 105 Idp = eye ( p, p ) ; 106 % 107 AAA = kron ( dphi Idp, kron ( dth diag ( s i n t h ), Ar ) )... 108 + kron ( dphi Idp, kron ( Ath, drho Idn ) )... 109 + kron ( kron ( Aphi, diag ( s i n t h i n v ) ), drho dth Idn ) ; 110 % 111 for i =1:nmp 112 Ualt ( i ) = U0 ; 113 end 114 Umax = U0 ; 115 nt = 0 ; 116 % r e c h t e S e i t e ( Beruecksichtigung des Q u e l lglieds ) 117 R = zeros (nmp, 1 ) ; 118 for i =1: n 119 for j =1:m 120 for k =1: p 121 ind = i + ( j 1) n + ( k 1) n m; 122 if ( i < n ) R( i ) = 0 ; end 123 if ( i == n ) 124 %%% h i e r muss die j e w e i l i g e RB an der Kugeloberflaeche eingebaut werden, 125 %%% d. h. die Loesungs unabhaengige r e c h t e S e i t e, die bei der Eliminierung von U {N+ 1... } 126 %%% aus RB und Laplacian D i s k r e t i s i e r u n g am Punkt x {N... } e n t s t e h t 127 R( ind ) = a dphi dth s i n t h ( j ) 2 rhop ( n+1) ˆ2/ drho ( 2 6 3. 0 + s i n t h ( j ) 45) ;... 128 end 129 end 130 end 131 % 132 % Loesung 133 U = AAA\R ; 134 % 135 % 136 U3 = reshape (U, n,m, p ) ; 137 % p l o t 138 mesh (U3 ( 1 : n, 1 :m, 1 ) ) 139 title ( Temperatur Feld \ phi = const. ) 140 zlabel ( T(\ rho, \ t h e t a ) ) 141 xlabel ( r ) 142 ylabel ( \ t h e t a ) 143 end 144 % P l o t The full 3D scheme is validated with the program 1 % hot potato problem 3D 2 % 3 n = 2 0 ; 4 m = 4 ; 5 p = 6 ; 6 R0 = 0. ; 7 R1 = 1. 0 / 2 0. ; 8 drho = ( R1 R0 ) /n ; 9 dth = pi/m; 10 dphi = 2 pi/p ; 11 % 12 % Zentralpunkte der F i n i t e n Zellen 13 rho = linspace ( R0+drho /2,R1 drho /2,n ) ; 14 th = linspace ( dth /2,pi dth /2,m) ; 15 s i n t h = sin ( th ) ; 16 % Randpunkte der F i n i t e n Zellen 17 rhop = linspace ( R0, R1, n+1) ; 18 thp = linspace ( 0, pi,m+1) ; 19 sinthp = sin ( thp ) ; 20 for i =1:m Listing 2: source code
21 s i n t h i n v ( i ) = 1/ s i n t h ( i ) ; 22 s i n t h i n v 2 ( i ) = 1/ s i n t h ( i ) ˆ 2 ; 23 end 24 for i =1:n 25 rho2 ( i ) = rho ( i ) ˆ 2 ; 26 rho2inv ( i ) = 1./ rho ( i ) ˆ 2 ; 27 end 28 for i =1:m 29 t h s i n t h ( i ) = th ( i ) s i n t h ( i ) ; 30 end 31 % Matrixaufbau Ar, Ath, Aphi 32 Ar = zeros ( n, n ) ; 33 Ath = zeros (m,m) ; 34 Aphi = zeros ( p, p ) ; 35 % 36 ntend = 9 0 0 ; 37 tau = 1. 0 / 1 0. ; 38 Ubound = 3 3 3 ; 39 U0 = 3 7 3. ; 40 Uu = 2 9 1. ; 41 a = 5.6/3600 1.0 e 4; 42 lambda = 0. 1 6 ; 43 alpha = 3 0 ; 44 % Skalierung 45 s k a l = 1. 0 ; 46 % 47 nmp = n m p ; 48 % Matrixaufbau Ar 49 Ualt = zeros (nmp, 1 ) ; 50 R = zeros (nmp, 1 ) ; 51 % 52 % Laplacian in Kugelkoordinaten 53 % 54 %U = [ u ( 1, 1, 1 ),..., u ( n, 1, 1 ), 55 % u ( 1, 2, 1 ),..., u ( n, 2, 1 ), 56 % u ( 1, 3, 1 ),..., u ( n, 3, 1 ), 57 % 58 % u ( 1,m, 1 ),..., u ( n,m, 1 ), 59 % u ( 1, 1, 2 ),..., u ( n, 1, 2 ), 60 % u ( 1, 2, 2 ),..., u ( n, 2, 2 ), 61 % u ( 1, 3, 2 ),..., u ( n, 3, 2 ), 62 % 63 % u ( 1,m, 2 ),..., u ( n,m, 2 ), 64 % 65 %..., u ( 1, 1, p ),..., u ( n, 1, p ), 66 %..., u ( 1, 2, p ),..., u ( n, 2, p ), 67 %..., u ( 1, 3, p ),..., u ( n, 3, p ), 68 % 69 %..., u ( 1,m, p ),..., u ( n,m, p ) ] 70 % 71 for i =2: n 1 72 Ar ( i, i ) = a ( rhop ( i +1) ˆ2 + rhop ( i ) ˆ 2 ) /drho rho2inv ( i ) ; 73 Ar ( i, i 1) = a rhop ( i ) ˆ2/ drho rho2inv ( i ) ; 74 Ar ( i, i +1) = a rhop ( i +1) ˆ2/ drho rho2inv ( i ) ; 75 end 76 Ar ( 1, 1 ) = a ( rhop ( 1 ) ˆ2 + rhop ( 2 ) ˆ 2 ) /drho rho2inv ( 1 ) ; 77 Ar ( 1, 2 ) = a rhop ( 2 ) ˆ2/ drho rho2inv ( 1 ) ; 78 Ar ( n, n ) = a ( rhop ( n ) ˆ2 + rhop ( n+1) ˆ 2 ) /drho rho2inv ( n )... 79 a rhop ( n+1) ˆ2/ drho ( lambda/drho ) /(lambda/drho + alpha ) rho2inv ( n ) ; 80 Ar ( n, n 1) = a rhop ( n ) ˆ2/ drho rho2inv ( n ) ; 81 % 82 for i =2:m 1 83 Ath ( i, i ) = a (sin ( thp ( i ) ) + sin ( thp ( i +1) ) ) /dth/sin ( th ( i ) ) ; 84 Ath ( i, i 1) = a sin ( thp ( i ) ) /dth/sin ( th ( i ) ) ; 85 Ath ( i, i +1) = a sin ( thp ( i +1) ) /dth/sin ( th ( i ) ) ; 86 end 87 Ath ( 1, 1 ) = a (sin ( thp ( 1 ) ) + sin ( thp ( 2 ) ) ) /dth/sin ( th ( 1 ) ) ; 88 Ath ( 1, 2 ) = a sin ( thp ( 2 ) ) /dth/sin ( th ( 1 ) ) ; 89 Ath (m,m) = a (sin ( thp (m) ) + sin ( thp (m+1) ) ) /dth/sin ( th (m) ) ; 90 Ath (m,m 1) = a sin ( thp (m) ) /dth/sin ( th (m) ) ; 91 for i =2: p 1
92 Aphi ( i, i ) = a 2./ dphi ; 93 Aphi ( i, i 1) = a 1./ dphi ; 94 Aphi ( i, i +1) = a 1./ dphi ; 95 end 96 Aphi ( 1, 1 ) = a 2./ dphi ; 97 Aphi ( 1, 2 ) = a 1./ dphi ; 98 Aphi ( p, p ) = a 2./ dphi ; 99 Aphi ( p, p 1) = a 1./ dphi ; 100 Aphi ( 1, p ) = a 1./ dphi ; 101 Aphi ( p, 1 ) = a 1./ dphi ; 102 % 103 Idn = eye ( n, n ) ; 104 Idm = eye (m,m) ; 105 Idp = eye ( p, p ) ; 106 % 107 AAA = kron ( Idp, kron ( Idm, Ar ) ) /drho... 108 + kron ( Idp, kron ( Ath, diag ( rho2inv ) Idn ) ) /dth... 109 + kron ( kron ( Aphi, diag ( s i n t h i n v 2 ) ),diag ( rho2inv ) Idn ) /dphi ; 110 % 111 u0 = ones (nmp, 1 ) U0 ; 112 % r e c h t e S e i t e ( Beruecksichtigung des Q u e l lglieds ) 113 R1 = zeros ( n, 1 ) ; 114 for i =1: n 115 if ( i < n ) R1 ( i ) = 0 ; end 116 if ( i == n ) 117 R1 ( n ) =... 118 + a rhop ( n+1) ˆ2 alpha/drho /( lambda/drho + alpha ) Uu/( rho ( n ) ˆ2 drho ) ; end 119 end 120 R = kron ( Idp ones ( p, 1 ),kron ( Idm ones (m, 1 ), R1 ) ) ; 121 % 122 Time min =80; %time in minutes 123 Time=Time min 60; % time in seconds 124 % matlab 125 odefun=@( t, x ) AAA x+r ; % function of r i g h t side 126 [ T,U]= ode23s (@( t, x ) odefun ( t, x ), [ 0, Time ], u0 ) ; %ode23s works f a s t e r f o r s t i f f 127 % 128 for i =1:length ( T ) 129 % f o r i =1:p 130 plot (U( i, 1 : n ) ) 131 end 132 133 % 2) Exercise Use characteristic l 0 length, time t 0 and temperature u c to write down the problem 3) of sheet 4 in a dimensionless form. Solution: With the characteristic values u c, t 0 and l 0 we define the dimensionless temperature, time and radius ū = u, t = t and r = r. u c t 0 l 0 For the heat conduction equation we get (ūu c ) ( tt 0 ) = a 1 ( rl 0 ) 2 For the boundary condition we come to ( rl 0 ) [( rl 0) 2 (ūu c) ( rl 0 ) λ (ūu c) ( rl 0 ) = α(ūu c u ) The initial condition in dimensionless form reads as ū = 373 K u c. ] ū t = at 0 l 2 0 λ ū αl 0 r = ū u. u c 1 ū r 2 [ r2 r r ].
3) Exercise Solve the 2D shallow water problem U t + F (U) x + G(U) y = S(U, B) (1) with the conservative variables U = h hu hv =: q 1 q 2 q 3, the flux function F (U) = hu hu 2 + 1 2 gh2 huv, G(U) = hv huv hv 2 + 1 2 gh2, and the source term S(U, B) = 0 hgb x hgb y h denotes the water height, hu, hv the water amount in the x- and y-direction, u and v are averaged velocities, and B describes the topography of the ground. The body force constant g is set to 1. We consider the spatial domain Ω =]0, 1[ 2 and the time interval [0, 5]. The initial values are { 1 x 1 h(x, y, 0) = 10, y 1 10, 0.5 otherwise and hu = hv = 0 on Ω. On the boundary we use homogeneous Neumann boundary conditions. Consider the cases B = 0 and. B(x, y) = x(1 x)/10. Use a conservative Finite-Volume method (for example Upwind, Lax-Friedrichs or Lax- Wendroff). The numerical Lax-Friedrichs-flux in 2D is for example F num (U, V ) = x 4τ (U V )+ 1 2 [F (U)+F (V )], G num(u, V ) = y 4τ (U V )+ 1 [G(U)+G(V )]. 2 Solution: The FV-method to solve the shallow water problem is implemented in the following program. Listing 3: source code 1 % shallow water 2D e x e r c i s e 6. 3 2 % 3 n = 3 0 ; % Anzahl innerer Gitterpunkte in x Richtung 4 m = 2 5 ; % Anzahl innerer Gitterpunkte in y Richtung 5 dx = 1/(n+1) ; 6 dy = 1/(m+1) ; 7 % 8 a =0; b =1; 9 c =0; d=1; 10 g = 9. 8 1 ; 11 % Zentralpunkte der F i n i t e n Zellen 12 xp = linspace ( a dx, b+dx, n+2) ; 13 x = linspace ( a dx/2, b+dx/2,n+1) ;
14 yp = linspace ( c dy, d+dy,m+2) ; 15 y = linspace ( c dy/2,d+dy/2,m+1) ; 16 % Anfangsbedingungen 17 % U( x, y, 1 ) =: h 18 % U( x, y, 2 ) =: hu 19 % U( x, y, 3 ) =: hv 20 % 21 for i =1: n+1 22 for j =1:m+1 23 U( i, j, 1 ) = 0. 5 ; 24 U( i, j, 2 ) = 0 ; 25 U( i, j, 3 ) = 0 ; 26 if ( x ( i ) < ( b a ) /10 && y ( j ) < ( d c ) /10) U( i, j, 1 ) = 1. 0 ; end 27 end 28 end 29 % 30 % Berechnung in Z e i t r i c h t u n g 31 % 32 tau = 1 / 1 0 0 0 0. ; 33 ntend = 1 0 00; 34 nt = 0 ; 35 while ( nt < ntend ) 36 % 37 % Randbedingungen 38 for k =1:3 39 % 40 for i =2:n 41 U( i, 1, k ) = U( i, 2, k ) ; 42 U( i,m+1,k ) = U( i,m, k ) ; 43 end 44 for j =2:m 45 U( 1, j, k ) = U( 2, j, k ) ; 46 U( n+1, j, k ) = U( n, j, k ) ; 47 end 48 % 49 for i =2:n 50 for j =2:m 51 % Lax F r i e d r i c h s Methode 52 % Un( i, j, k ) = U( i, j, k ) tau/dx ( dx/tau /4 (U( i, j, k ) U( i +1, j, k ) ) + 0. 5 ( F (U, i, j, k, g ) + F (U, i +1, j, k, g ) )... 53 % ( ( dx/tau /4 (U( i 1, j, k ) U( i, j, k ) ) + 0. 5 ( F (U, i 1, j, k, g ) + F (U, i, j, k, g ) ) ) ) )... 54 % tau/dy ( dy/tau /4 (U( i, j, k ) U( i, j +1,k ) ) + 0. 5 (G(U, i, j, k, g ) + G(U, i, j +1,k, g ) )... 55 % ( ( dy/tau /4 (U( i, j 1,k ) U( i, j, k ) ) + 0. 5 (G(U, i, j 1,k, g ) + G(U, i, j, k, g ) ) ) ) )... 56 U( i, j, k ) = 0. 2 5 (U( i +1, j, k ) + U( i 1, j, k ) + U( i, j +1,k ) + U( i, j 1,k ) )... 57 tau 0. 5 ( ( F (U, i +1, j, k, g ) F (U, i 1, j, k, g ) ) /dx... 58 + ( G(U, i, j +1,k, g ) G(U, i, j 1,k, g ) ) /dy )... 59 tau U( i, j, 1 ) g S ( x, y, i, j, k ) ; 60 end 61 end 62 % end k 63 end 64 nt = nt + 1 ; 65 mesh (U( 2 : n, 2 :m, 1 ) ) 66 title ( Wasserhoehe h ( x, y ) ) 67 zlabel ( h ( x, y ) ) 68 xlabel ( x ) 69 ylabel ( y ) 70 end 71 % p l o t 72 mesh (U( 2 : n, 2 :m, 1 ) ) 73 title ( Wasserhoehe h ( x, y ) ) 74 zlabel ( h ( x, y ) ) 75 xlabel ( x ) 76 ylabel ( y ) 77 % P l o t 78 79 function f = F (U, i, j, k, g ) 80 if ( k == 1) f = U( i, j, 2 ) ; end
81 if ( k == 2) f = U( i, j, 2 ) ˆ2/U( i, j, 1 ) + 0.5 g U( i, j, 1 ) ˆ 2 ; end 82 if ( k == 3) f = U( i, j, 2 ) U( i, j, 3 ) /U( i, j, 1 ) ; end 83 % endfunction 84 85 function g = G(U, i, j, k, g ) 86 if ( k == 1) g = U( i, j, 3 ) ; end 87 if ( k == 2) g = U( i, j, 2 ) U( i, j, 3 ) /U( i, j, 1 ) ; end 88 if ( k == 3) g = U( i, j, 3 ) ˆ2/U( i, j, 1 ) + 0.5 g U( i, j, 1 ) ˆ 2 ; end 89 % endfunction 90 91 function s = S ( x, y, i, j, k ) 92 if ( k == 1) s = 0. 0 ; end 93 if ( k == 2) s = 0. 1 0.2 x ( i ) ; end 94 if ( k == 3) s = 0. 0 ; end 95 % endfunction