Download as pdf or txt
Download as pdf or txt
You are on page 1of 81

CIV 6745

Computational Method in Water Engineering


Recap of last week

• Linear advection
• Riemann problem
• Inviscid Burgers’ equation
• Finite volume method
CIV 6745
Computational Method in Water Engineering
• Week 4: Finite volume method to solve the shallow water equations
1- The Shallow Water equations (SWE)
2- General Riemann problem solution
3- Finite Volume (shock-capturing) method
4- MATLAB Finite Volume code
5- More “approximate” approach to estimate the local Riemann
Problem solution
6- Coursework
Recommended reading
• Toro E F. Riemann Solvers and Numerical Methods for Fluid Dynamics:
A Practical Introduction. Third Edition. Springer-Verlag Berlin
Heidelberg, 2009, 721 pages.

• Toro E F. Shock-Capturing Methods for Free-Surface Shallow Flows.


Wiley and Sons Ltd, Chichester, 2001, 314 pages.
1- The Shallow Water equations (SWE)
1.1 Mass and momentum conservation equations – a system of two PDEs

Mass conservation  𝜕𝑡 ℎ + 𝜕𝑥 𝑞 = 0 h q

𝑞2 ℎ2
Momentum conservation  𝜕𝑡 𝑞 + 𝜕𝑥 +𝑔 = 𝑔ℎ(𝑆0 − 𝑆𝑓 )
ℎ 2

 𝜕𝑡 = 𝜕/ 𝜕𝑡 and 𝜕𝑥 = 𝜕/ 𝜕𝑥 are partial derivatives with respect to 𝑡 and 𝑥, and 𝑔


is gravity constant.
 ℎ = ℎ(𝑥, 𝑡) is the water depth variable and 𝑞 = 𝑞(𝑥, 𝑡) is the flow rate per unit
width, which is therefore related to ℎ by: 𝑞 = ℎ𝑢 (𝑢 denoting the flow velocity).
1- The Shallow Water equations (SWE)
1.1 Mass and momentum conservation equations – two PDEs in a form of a system of
equations.

Mass conservation  𝜕𝑡 ℎ + 𝜕𝑥 𝑞 = 0 h q

𝑞2 ℎ2
Momentum conservation  𝜕𝑡 𝑞 + 𝜕𝑥 + 𝑔 = 𝑔ℎ(𝑆0 − 𝑆𝑓 )
ℎ 2

 𝑆0 = −𝜕𝑥 𝑧 is the bed slope source term or the topography gradients.


 𝑆𝑓 is the source term representing friction effects.

 We will assume a water flow in a “flat” and “frictionless” channel in order to have 𝑆0 =
𝑆𝑓 = 0 for simplicity.
1- The Shallow Water equations (SWE)
1.2 Conservative matrix form – two PDEs in a form of a system of
equations. Denoting by:
𝑞 0

𝐔 = ; 𝐅=𝐅 𝐔 = 𝑞2 ℎ2 ; 𝐒 = 𝐒(𝐔) = 𝑔ℎ(𝑆 − 𝑆 )
𝑞 +𝑔 0 𝑓
ℎ 2

𝜕𝑡 𝐔 + 𝜕𝑥 𝐅(𝐔) = 0
1- The Shallow Water equations (SWE)
1.2 Conservative matrix form – two PDEs in a form of a system of
equations. Denoting by:
𝑞 0

𝐔 = ; 𝐅=𝐅 𝐔 = 𝑞2 ℎ2 ; 𝐒 = 𝐒(𝐔) = 𝑔ℎ(𝑆 − 𝑆 )
𝑞 +𝑔 0 𝑓
ℎ 2
Flow vector,
or the vector Flux vector Source terms
of unknowns vector
(assumed to
be zero)

𝜕𝑡 𝐔 + 𝜕𝑥 𝐅(𝐔) = 0
1- The Shallow Water equations (SWE)
1.3 Quasi-linear form – as for the scalar case, we can the following quasi-linear form for the
system case:
𝜕𝑡 𝐔 + 𝐀 𝐔 𝜕𝑥 𝐔 = 0

With the Jacobian matrix 𝐀 obtained as:

𝜕𝐅 𝜕𝑓1 /𝜕𝑢1 𝜕𝑓1 /𝜕𝑢2


𝐀 𝐔 = =
𝜕𝐔 𝜕𝑓2 /𝜕𝑢1 𝜕𝑓2 /𝜕𝑢2

𝑞2 ℎ2 𝑞2 ℎ2
𝜕𝑓1 𝜕(𝑞) 𝜕𝑓1 𝜕(𝑞) 𝜕𝑓2 𝜕 ℎ +𝑔 2 𝜕𝑓2 𝜕 ℎ +𝑔 2
With = = 0; = = 1; = = 𝑔ℎ − 𝑢2 ; = = 2𝑢
𝜕𝑢1 𝜕ℎ 𝜕𝑢2 𝜕𝑞 𝜕𝑢1 𝜕ℎ 𝜕𝑢2 𝜕𝑞
1- The Shallow Water equations (SWE)
1.3 Quasi-linear form – as for the scalar case, we can the
following quasi-linear form for the system case:
𝜕𝑡 𝐔 + 𝐀 𝐔 𝜕𝑥 𝐔 = 0

𝜕𝐅 0 1
𝐀 𝐔 = = 2
𝜕𝐔 𝑐 − 𝑢2 2𝑢

Note that the 𝑐 = 𝑔ℎ is called wave celerity.


1- The Shallow Water equations (SWE)
1.4 Characteristic speeds – the matrix 𝐀 is
diagonalisable (with two real eigenvalues) yielding to
two characteristic speeds:

λ1 = 𝑢 + 𝑐 and λ2 = 𝑢 − 𝑐

This means that we now have to consider the


presence of two distinct characteristic curves in the
(𝑥 − 𝑡) plane (which intersect).
1- The Shallow Water equations (SWE)
1.5 The dam-break problem – consider a 1D horizontal channel (frictionless).
Assume two sections are separated by a vertical gate.
Wall or dam
ℎ𝐿

𝑢𝐿 = 0
ℎ𝑅

𝑢𝑅 = 0
1- The Shallow Water equations (SWE)
1.5 The dam-break problem – consider a 1D horizontal channel (frictionless).
Assume two sections are separated by a vertical gate.

The fluid in each section is a uniform (constant) depth and at rest (zero velocity);
thus at 𝑡 = 0𝑠,

ℎ𝐿 𝑥 < 0
ℎ 𝑥, 0 = ቊ
ℎ𝑅 𝑥 > 0

Assume that the gate/dam is removed (at t = 0s) instantenously (at high speed).
Two wave emerge:
1- The Shallow Water equations (SWE)

Depression
Shock
1- The Shallow Water equations (SWE)
• Left travelling wave reducing the depth of water (gradually) as it propagates into
the fluid at rest
Rarefaction/depression wave
• Right travelling wave increasing the depth of water as it propagates through the
fluid at rest
Shock/bore wave

This idealized situation (i.e. flat + frictionless dam-break case) can be exactly solved
by the shallow water equations (i.e. one particular scenario from the general
Riemann problem solution).
1- The Shallow Water equations (SWE)
𝑡

𝑥
ℎ𝐿

ℎ𝑚 𝑞𝑚

ℎ𝑅 𝑞𝐿 𝑞𝑅

𝑥 𝑥
ℎ𝐿
Note: 𝑡 = 10𝑠
1- The mathematics of this exact ℎ𝑚
solution is complex and quite out of
ℎ𝑅
scope. Therefore, you will be provided
this exact solution as a MATLAB code
that does the job for you. For example,
for the previous case, it outputs the
following. 𝐔𝑚
𝐔𝐿
𝐔𝑅
2- You will have to use these depth (h)
and discharge (h*u = q) to later validate
your numerical solution to the dam-
break problem.
𝑞𝑚

𝑞𝐿 𝑞𝑅
2- General Riemann problem solution
2.1 Background – Consider discontinuous initial water states, as seen in the dam-
break problem. Generally, 4 wave combinations could form, one of which is the
rarefaction-shock scenario associated with the dam-break problem.
2- General Riemann problem solution
𝑡 = 0𝑠
𝑡 = 10𝑠

+5 𝑥 < 0
ℎ 𝑥, 0 = 10 𝑚 𝑢 𝑥, 0 = ቊ Shock-shock
−5 𝑥 > 0

𝑡 = 0𝑠 𝑡 = 10𝑠
2- General Riemann problem solution

𝑡 = 10𝑠

−5 𝑥 < 0 Rarefaction-
ℎ 𝑥, 0 = 10 𝑚 𝑢 𝑥, 0 = ቊ
+5 𝑥 > 0 rarefaction

𝑡 = 10𝑠
2- General Riemann problem solution

𝑡 = 10𝑠

ℎ𝐿 = 5 𝑥 < 0
ℎ 𝑥, 0 = ቊ Shock-rarefaction
ℎ𝑅 = 10 𝑥 > 0

𝑡 = 10𝑠
2- General Riemann problem solution
2.2 What do you need to know – these four combinations of wave patterns arising
from the (discontinuous) initial condition (or IVP):

𝐔𝐿 𝑥 < 0 ℎ
𝐔 𝑥, 0 = ቊ 𝑤𝑖𝑡ℎ 𝐔 = = 𝑓𝑙𝑜𝑤 𝑣𝑒𝑐𝑡𝑜𝑟
𝐔𝑅 𝑥 > 0 𝑞

1- Are very important to understand the LOCAL wave patterns involved in the the
finite volume method for solving the shallow water equations (or shallow water
flows, more generally).

2- Can be used as analytical solution to validate the finite volume method.


2- General Riemann problem solution
2.3 MATLAB code – You will be given the two following m-scripts:

1- Open the one in blue


highlight

You don’t have to understand


the second function.
function Test_Wave_Scenarios % Scenario #2 : 1-shock and 2-shock
%
% % hl = 10;
figure(1) % hr= 10;
% ul = 5;
% set the output time % ur = -5;
%
Time = 10; % Scenario #3 : 1-Rarefacation and 2-shock
% %
% Domain [xmin; xmax], position of the % hl = 10;
% hr= 1;
discontinuity x0 and number of points N % ul = 0;
% % ur = 0;
xmin = -200; %
xmax = 200; % Scenario #4 : 1-shock and 2-Rarefacation
x0 = 0; hl = 5;
N = 400; hr= 10;
ul = 0;
ur = 0;
% Scenario #1 : 1-Rarefacation and 2-
Rarefaction
%
% hl = 10;
% hr= 10;
% ul =-5; Uncomment (i.e. remove the “%”) the
% ur = 5; scenario details (i.e. initial condition) you
wish to explore.
% Coding extras to gradually plot the solution
T = 0.01;
dt = .1;

while T < Time

[x,h,u] = Exact_Solution_SWE(hl,ul,hr,ur,x0,xmin,xmax,N,T);

pause(0.05)

T = T + dt;
%
if T-Time >= 0
Don’t touch! T=T-dt;
dt=Time-T;
T=T+dt;
[x,h,u] = Exact_Solution_SWE(hl,ul,hr,ur,x0,xmin,xmax,N,T);

end
end

Save the exact solution for each scenario under


save('Exact_10s.mat','x','h','u') different name: e.g. use 'Exact4_10s.mat'
Call the “Test_Wave_Scenarios” in the
command window (or run it)
Make sure you keep (and save
this file for future use)
To see what is in the file: use “load filename” and then the “who” command.
To see what is in the file: use “load filename” and then the “who” command.

Now you can plot the exact depth (h) and


discharge (q = hu) solutions, using the
plot command…
Part of your coursework will be to do the same for
the other scenarios in order to have an analytical
solution to compare with the numerical solution.

Please make sure that you know how to use these


functions and to save their outputs (careful
naming).
2- General Riemann problem solution
2.3 Local Riemann Problem Solution (RPS)

U
UL 𝐔𝐿𝑅 𝐔𝐿𝑅 = 𝐔RPS (𝑥 = 0, 𝑡)
UR
Exact solution of the GENERAL
x Riemann problem evaluated
0 (LOCALLY) at 𝑥 = 0.

𝐔 𝑥<0 ℎ
𝐔 𝑥, 0 = ቊ 𝐿 with 𝐔 = = 𝑓𝑙𝑜𝑤 𝑣𝑒𝑐𝑡𝑜𝑟
𝐔𝑅 𝑥 > 0 𝑞
2- General Riemann problem solution
2.3 Local Riemann Problem Solution (RPS)

To obtain the evaluation of the local Riemann problem U𝐿𝑅 :

1- Simply recall and open the function “Exact_Solution_SWE.m”

2- Copy from “ function [h_m,u_m,xl1,x1m,xm2,x2r,a_max,c_L,c_R]= Global_RPS(h_L,u_L,h_R,u_R)”

3- Open a new m-script and past it there, with the interest of only keeping [h_m,u_m] as output
arguments.
Scroll
down
Select and copy
Fit it to purpose:
Rename it as
“Loc_RPS” and
have two output
arguments
“[h_m,u_m]”
As for the scalar case, you will also
need it for the flux estimation between
two discontinuous states between two
computational cells

Keep in mind where you have


saved it, as you will need it for
future use!
3- Finite Volume (shock-capturing) method
3.1 Same PDEs type and IVP as for the scalar case
Over 𝑥𝑚𝑖𝑛; 𝑥𝑚𝑎𝑥 and during 𝑡; 𝑡 + ∆𝑡 we are solving the PDEs,

𝑞

𝜕𝑡 𝐔 + 𝜕𝑥 𝐅 𝐔 = 0, Where 𝐔 = and 𝐅 = 𝐅 𝐔 = 𝑞2 ℎ2
𝑞 + 𝑔
ℎ 2

Assuming piecewise-constant initial values in the form:


UL
𝐔𝐋 𝑖𝑓 𝑥 < 𝑥𝑖+1Τ2 UR
𝐔 𝑥, 𝑡 = 0 = ൝
𝐔𝐑 𝑖𝑓 𝑥 > 𝑥𝑖+1Τ2
x
𝑥𝑖+1Τ2
3- Finite Volume (shock-capturing) method
3.2 Discretisation into piecewise constant elementary volumes
Consider a 1D spatial domain [𝑥𝑚𝑖𝑛 , 𝑥𝑚𝑎𝑥 ], which needs to be decomposed into 𝑁
computational cells 𝐼𝑖 = 𝐼𝑖 𝑖=1,…,𝑁 = {𝐼1 , … , 𝐼𝑁 } of equal size ∆𝑥.
∆𝑥

𝒙𝟏/𝟐 = 𝒙𝒎𝒊𝒏 𝒙𝑵+𝟏/𝟐 = 𝒙𝒎𝒂𝒙

Define the cells:  Ii i 1,..., N Cell I i   xi 1/ 2 ; xi 1/ 2 

xi 1/ 2  xi 1/ 2 MATLAB function for 1D mesh generation:


Centred at: xi  % Need to take x_min and x_max as well as to number of
2 % cells N as inputs. It must outputs a vector x of length N
Of length: x  xi 1/ 2  xi 1/ 2 and x_interface of length N+1.
3- Finite Volume (shock-capturing) method
3.2 Discretisation into piecewise constant elementary volumes

The solution state at present time 𝑡 is now a


set of constant data per cell, in the form of
(𝑡)
𝐔𝑖 over cells
𝑖
𝐔(𝑥, 𝑡)
𝐔𝑖
𝐔𝑖−1
𝐔𝑖+1
𝐔𝑖−2
𝐔𝑖+2


𝐼𝑖−1 𝐼𝑖 𝐼𝑖+1
𝑥

𝑡2

𝑡1
… …
𝐼𝑖 𝑥
𝐔(𝑥, 𝑡)
𝐔𝑖
𝐔𝑖−1
𝐔𝑖+1
𝐔𝑖−2
𝐔𝑖+2


𝐼𝑖−1 𝐼𝑖 𝐼𝑖+1
𝑥

𝑡 + ∆𝑡

𝑡
… …
𝐼𝑖 𝑥
𝐔(𝑥, 𝑡)
𝐔𝑖
𝐔𝑖−1
𝐔𝑖+1
𝐔𝑖−2
𝐔𝑖+2


𝐼𝑖−1 𝐼𝑖 𝐼𝑖+1
𝑥

𝑡 + ∆𝑡 𝑅𝑃𝑆
𝑅𝑃𝑆
𝐔𝑖− 𝐔𝑖+1 Τ2
1 Τ2

𝑡
… …
𝐼𝑖 𝑥
𝐔(𝑥, 𝑡)
𝐔𝑖
𝐔𝑖−1
𝐔𝑖+1
𝐔𝑖−2
𝐔𝑖+2
… 𝑅𝑃𝑆


𝐅𝑖− 𝑅𝑃𝑆
1 Τ2 𝐅𝑖+1 Τ2

𝐼𝑖−1 𝐼𝑖 𝐼𝑖+1
𝑥

𝑡 + ∆𝑡 𝑅𝑃𝑆
𝑅𝑃𝑆
𝐔𝑖− 𝐔𝑖+1 Τ2
1 Τ2

𝑡
… …
𝐼𝑖 𝑥
𝐔(𝑥, 𝑡 + ∆𝑡)
𝐔𝑖
𝐔𝑖−1
𝐔𝑖+1
𝐔𝑖−2
𝐔𝑖+2


𝐼𝑖−1 𝐼𝑖 𝐼𝑖+1
𝑥

𝑡2

Now using 𝐯𝐞𝐜𝐭𝐨𝐫𝐬 instead of scalars


𝐿𝑅𝑃
𝑢𝑖−1Τ2
𝐿𝑅𝑃
𝑢𝑖+1Τ2

𝑡1
… ∆𝑡 RPS … (𝑡)
(𝑡+∆𝑡) (𝑡) RPS
𝐔𝑖 = 𝐔𝑖 − 𝐅𝑖+1Τ2 − 𝐅𝑖−1Τ2
𝐼 𝑖 𝑥

∆𝑥
Over each cell 𝐼𝑖 you should:
𝑡 RPS
(1) Solve the Local Riemann problems 𝐔𝐿𝑅 𝐔𝑖+1 , 𝐔𝑖𝑡 to evaluate 𝐅𝑖+1/2
𝑡 𝑡1 RPS
(2) Solve the Local Riemann problems 𝐔𝐿𝑅 𝐔𝑖 , 𝐔𝑖−1 to evaluate 𝐅𝑖−1/2
(3) Update your solution {𝐔𝑖 }𝑖 to time 𝑡 + ∆𝑡 as:
∆𝑡 RPS RPS (𝑡)
𝐔𝑖𝑡+∆𝑡 = 𝐔𝑖𝑡 − 𝐅𝑖+1Τ2 − 𝐅𝑖− 1Τ2
∆𝑥
(4) Repeat the process until my solution reaches my chosen end time…
3- Finite Volume (shock-capturing) method
𝑡
𝐔𝒎 𝐔𝒎

3.3 Numerical flux estimation


The only key ingredient remaining! 𝑥
𝑡
𝐔𝒎 𝐔𝒎
𝐑𝐏𝐒 RPS
𝐅𝑖+1Τ2 = 𝐅(𝐔𝑳𝑹 = 𝐔𝑖+𝟏/𝟐 (𝑥𝑖+1Τ2 )) , with
𝑥
𝐑𝐏𝐒
𝐔𝐢+𝟏/𝟐 (𝐱/𝐭) : solution of the Riemann
𝑅𝑃𝑆
problem arising from the local solution 𝐔𝐿𝑅 = 𝐔𝒊+𝟏/𝟐 𝟎 = 𝐔𝑚
discontinuities 𝐔 𝐋 = 𝐔𝐢

𝐔𝐋 = 𝐔𝐢 𝑖𝑓 𝑥 < 𝑥𝑖+1Τ2 𝐔𝐑 = 𝐔𝐢+𝟏


൝ (RP) 𝐔
𝐔𝐑 = 𝐔𝐢+𝟏 𝑖𝑓 𝑥 > 𝑥𝑖+1Τ2

𝑥𝑖+1Τ2 𝑥
3- Finite Volume (shock-capturing) method
3.3 Numerical flux estimation
We are going to deal with this computationally:

1- Recall the function “Loc_RPS” that we have saved before.

2- Set u_L = 𝑢𝑖 = 𝑞𝑖 /ℎ𝑖 and u_R = 𝑢𝑖+1 = 𝑞𝑖+1 /ℎ𝑖+1 . Also h_L = ℎ𝑖 and h_R = ℎ𝑖+1
Call the function as: [h_LR,u_LR]= Loc_RPS(h_L,u_L,h_R,u_R)

3- Evaluate the Flux 𝐅 for the vector U_LR = [h_LR,u_LR], i.e. 𝐅i+1/2 = 𝐅(𝐔𝐋𝐑 )

4- Repeat the same process to evaluate 𝐅i−1/2


4- MATLAB Finite
Volume Code
4.1 Aim
Program in MATLAB the Finite Volume
discrete conservative formulation of Exact depth and discharge profiles at 𝑡 = 10s,
the shallow water equations. emerging from the initial condition:
Give the following initial conditions 10 𝑚 𝑥<0
ℎ 𝑥, 𝑡 = 0 = ቊ
relative to a dam-break problem, and 5𝑚 𝑥>0
compare your results with an 𝑞 𝑥, 𝑡 = 0 = 0 𝑚2 /𝑠
analytical solution.

10 m

5m

-200 0 + 200
4- MATLAB Finite Volume code
4.1.1 Recall the function “Generate_1D_Mesh” and modify it to be vectorial
function [x_interface,x,dx, U_now] = Generate_1D_Mesh(x_min,x_max,N)

L = x_max-x_min; % Domain length


dx = L/N; % Length of a cell or distance between interface points

% Allocation of the size of the output vectors


x = zeros(1,N);
U_now = zeros(2,N);
x_interface = zeros(1,N+1);

% Interface points
for i = 1:N+1
x_interface(i) = x_min + (i-1)*dx; % simple but important
end

% Cell centers
for i = 1:N
x(i) = 0.5*(x_interface(i)+x_interface(i+1));
U_now(:,i) = Initial_Condition(x(i));
end
4- MATLAB Finite Volume code
4.1.2 Recall the function “Initial_Condition” and modify it to be vectorial

function U0 = Initial_Condition(x)

% Output: column vector U0 = [h0 q0]

x0 = 0;

% Set the discharge to zero all over


q0 = 0;

% Need to consider discontinuous water state at x0 = 0


if x < x0
h0 = 10;
elseif x > x0
h0 = 1;
end

U0 = [h0; q0];
4- MATLAB Finite Volume code
4.1.3 Make a flux function relative to the case of the shallow water equations
function F_out = Flux_SWE(U_in)
global g % be careful that “g” is initiated earlier
% This is the F(U) function aimed to evaluate the Flux as function of the
% state variables "h" and "q".
% Input: U_in = [h_in; q_in]
% Output: F_out in terms of the relationship: F = [q; q^2/h + g*h^2/2]

h_in = U_in(1,:);
q_in = U_in(2,:);

f1_out = q_in;
f2_out = q_in^2/h_in + (g/2)*h_in^2;

F_out = [f1_out; f2_out];


4- MATLAB Finite Volume code
4.1.4 Make sure that the function “Loc_RPS.m” is in the same directory.
4- MATLAB Finite Volume code
4.1.5 Bring in a “Main.m” script (or create it) so you re-edit to fit the vector case
It is better to treat Main.m as a “function” rather than just a script. Because, I
need to define the variable “g = 9.81” as constant and make it global (i.e.
visible to all other functions that need it). Otherwise, MATLAB will get lost on
how to see “g”

function Main % changed


global g % Changed, must take "g" global so it is seen by other functions

g = 9.81; % constant due to gravity


Changed to -200
% 1- 1D domain data:
x_min = -200; % changed
x_max = 200;

% 2- Number of Computational cells


N = 300;
Changed to a vector of two columns and N rows.

% 3- Initial data at t = 0s and mesh information


[x,dx,U_now] = Generate_1D_Mesh(x_min,x_max,N); % changed

% 4- Time step, time now and desired output time must be invovled:
dt = 0.05;
t_now = 0; Changed to 0.05
T_out = 10; % changed
Changed to 10s

% 5- Need to also initate a vector of (U_i)i = 1,...,N at time t + dt (future)


U_future = U_now; % just for a start to give it a size! % changed

Changed to a vectors of two columns and N rows.


while t_now <= T_out
% Illutrate the state variables (U_i)i = 1,...,N on the cell centres
subplot(2,1,1)
plot(x,U_now(1,:),'or');
xlabel('x')
ylabel('h(x)')
%
subplot(2,1,2) You can skip these lines… Just to
plot(x,U_now(2,:),'or'); visualise your solution at certain present
xlabel('x') time…
ylabel('q(x)')
pause(0.01)

% keep track of time:


t_now = t_now + dt
% Update the state variables at each cell (excluding boundaries)
for i = 2:N-1
% flux evaluation at xi+1/2
U_L = U_now(:,i);
U_R = U_now(:,i+1);
% Depth
h_L = U_L(1,:); Vectorial
h_R = U_R(1,:);
% Veloctiy
q_L = U_L(2,:);
q_R = U_R(2,:);
%
u_L = q_L/h_L;
u_R = q_R/h_R;
Vectorial
%
[h_LR,u_LR]= Loc_RPS(h_L,u_L,h_R,u_R); U_LR = [h_LR; h_LR*u_LR];

%
F_out = Flux_SWE(U_LR);

Vectorial PS. Once F_out is evaluated, you can overwrite the same
temporary variable! Good practice to save memory…
% flux evaluation at xi-1/2 Same but with different Left and right
U_L = U_now(:,i-1); states
U_R = U_now(:,i);
% Depth
h_L = U_L(1,:);
h_R = U_R(1,:);
% Veloctiy
q_L = U_L(2,:);
q_R = U_R(2,:);
%
u_L = q_L/h_L;
u_R = q_R/h_R;

%
[h_LR,u_LR]= Loc_RPS(h_L,u_L,h_R,u_R); U_LR = [h_LR; h_LR*u_LR];
%
F_in = Flux_SWE(U_LR);
% Update step:
U_future(:,i) = U_now(:,i) - dt/dx*(F_out - F_in);
end

% Boundaries treatment
U_future(:,1) = U_future(:,2);
U_future(:,N) = U_future(:,N-1);

% redo the process (i.e actualise U_now)


Save numerical results to prepare them
U_now = U_future; % Matrix equality (note)
for comparison with analytical solution.
end

% Save the resutls for later analysis


h_num = U_now(1,:);
q_num = U_now(2,:);
save('FV_resutls_10s','x','h_num','q_num','T_out');
The file you saved will appear

To see what’s inside it, press


“who” in the command window
4- MATLAB Finite Volume code
4.1.6 Comparison between exact and numerical solutions
Go to the
directory
Click, explore +
improve…
5- More “approximate” approach to estimate
the local Riemann Problem solution
5.1 The impracticability of using the exact Local Riemann Problem Solution (LRPS)
function [h_m,u_m]= Loc_RPS(h_L,u_L,h_R,u_R)
% Solve analyticaly the riemann problem for the Saint Venant equations
% (flat bottom) by considering the following zones :
% (i.e. for a rarefaction shock configu_Ration)
%

z1=fzero(@f,10); Need to iterate to find the zero of a nonlinear equation,


. EACH TIME THE FUNCTION Loc_RPS is called. Thus,
. there is a risk of divergence or finding the wrong value!
.
function y = f(z1)
global b c

y = -1/sqrt(2)*(z1-1)*((z1+1)/z1)^0.5 - 1/sqrt(2)*(z1/b-1)*((z1/b + 1)/(z1/b))^0.5 * sqrt(b) - c; For example …


Change the initial condition so
that h0 = 1 m downstream the
dam. Then, run the code and see
how the numerical solution will
blow up!
Aborted
5- More “approximate” approach to estimate
the local Riemann Problem solution
5.2 Approximate Riemann solvers
To ensure stability, many “approximate” Riemann solvers have been designed:

• Some are approximate Riemann state solvers as we have seen, i.e. 𝐅i+1/2 =
𝐅(𝐔i+1/2 (0)) with 𝐔𝐢+𝟏/𝟐 (x/t) is the approximate solution.

• Some others approximate the flux Riemann solvers directly, i.e. 𝐅i+1/2 is the
direct flux approximated.

In any case, the approximation is EXPLICIT. That is, given the input states 𝐔𝐿 and
𝐔R , 𝐅LR is directly and simply calculated by these states (no need to iterate)
5- More “approximate” approach to estimate
the local Riemann Problem solution
5.3 Example of the Local Lax Friedrich approximate Riemann solver

1
𝐅LR = 𝐅 LLF 𝐔L , 𝐔R = 𝐅 𝐔L + 𝐅 𝐔R − 𝑎𝑚𝑎𝑥 𝐔R − 𝐔L
2
𝑤𝑖𝑡ℎ
𝑎𝑚𝑎𝑥 = max |𝑢𝐿 − 𝑐𝐿 , |𝑢𝑅 − 𝑐𝑅 , |𝑢𝐿 + 𝑐𝐿 , |𝑢𝑅 + 𝑐𝑅

𝑎𝑛𝑑 𝑐𝐾 = 𝑔ℎ𝐾 (𝐾 = 𝐿, 𝑅) is the celerity and 𝑢𝐾 is the velocity.


5- More “approximate” approach to estimate
the local Riemann Problem solution
5.4 MATLAB function for the LLF flux solver

function F = Flux_LLF(hl,ul,hr,ur)
global g
%
cl = (g*hl)^0.5;
cr = (g*hr)^0.5;
%
a_max = max([abs(ul-cl),abs(ur-cr),abs(ul+cl),abs(ur+cr)]);

%
Ur = [hr;hr*ur];
Ul =[hl;hl*ul];

F = 0.5*(Flux_SWE(Ur) + Flux_SWE(Ul) - a_max * (Ur - Ul));


function F = Flux_LLF(hl,ul,hr,ur)
global g
%
cl = (g*hl)^0.5;
cr = (g*hr)^0.5;
%
a_max = max([abs(ul-cl),abs(ur-cr),abs(ul+cl),abs(ur+cr)]);
%
Ur = [hr;hr*ur];
Ul =[hl;hl*ul];

F = 0.5*(Flux_SWE(Ur) + Flux_SWE(Ul) - a_max * (Ur - Ul));


5.4 Modify the main function “Main.m” so that only F = Flux_LLF(hl,ul,hr,ur) is called when estimation interface fluxes
% Update the state variables at each cell (excluding boundaries)
for i = 2:N-1
% flux evaluation at xi+1/2
U_L = U_now(:,i);
U_R = U_now(:,i+1);
% Depth
h_L = U_L(1,:);
h_R = U_R(1,:);
% Veloctiy
q_L = U_L(2,:);
q_R = U_R(2,:);
%
u_L = q_L/h_L;
u_R = q_R/h_R;

% Comment here
% [h_LR,u_LR]= Loc_RPS(h_L,u_L,h_R,u_R); U_LR = [h_LR; h_LR*u_LR];

% Comment here
% F_out = Flux_SWE(U_LR);

F_out = Flux_LLF(h_L,u_L,h_R,u_R);
% flux evaluation at xi-1/2
U_L = U_now(:,i-1);
U_R = U_now(:,i);
% Depth
h_L = U_L(1,:);
h_R = U_R(1,:);
% Veloctiy
q_L = U_L(2,:);
q_R = U_R(2,:);
%
u_L = q_L/h_L;
u_R = q_R/h_R;

% again, comment here


% [h_LR,u_LR]= Loc_RPS(h_L,u_L,h_R,u_R); U_LR = [h_LR; h_LR*u_LR];
% comment here
% F_in = Flux_SWE(U_LR);

F_in = Flux_LLF(h_L,u_L,h_R,u_R);

% Update step:
U_future(:,i) = U_now(:,i) - dt/dx*(F_out - F_in);
end
Then you compare them!
6- Coursework
6.1 Dam-break problem

6.1.1 Extract and save the exact solution of the dam-break problem at 𝑡 =
10𝑠 for the initial conditions:

10 𝑚 𝑥<0
ℎ 𝑥, 𝑡 = 0 = ቊ and 𝑞 𝑥, 0 = 0 𝑚2 /𝑠
5𝑚 𝑥>0
6- Coursework
6.1 Dam-break problem

6.1.2 Find the Coursework 4 folder in MOLE and download


‘Exact3_10s_1m_case.mat’. This is the exact solution of the dam-break
problem at 𝑡 = 10𝑠 for the initial conditions:

10 𝑚 𝑥<0
ℎ 𝑥, 𝑡 = 0 = ቊ and 𝑞 𝑥, 0 = 0 𝑚2 /𝑠
1𝑚 𝑥>0
6- Coursework
6.1 Dam-break problem

6.1.3 Create you own Finite Volume MATLAB code solving the shallow water
equations. Use this code to get an approximate numerical solution to the
dam-break problems defined in 6.1.1.

Ps. Use only the Local Lax Friedrich approximate Riemann flux.
6- Coursework
6.1 Dam-break problem

6.1.4 Compare the outcomes (at 𝑡 = 10𝑠) of the numerical solutions with
the exact solution.

Ps. Make use of MATLAB figures and commands.


6- Coursework

You are expected to construct a structured, well-documented MATLAB code and to write
up a short memo explaining your methods/selections for coding it. Use can use Equations
and Figures as necessary and provide a copy of your code(s) as an appendix. There is no
particular format to follow, just make sure your explanations are clear and well presented.
Your coursework should be submitted to MOLE before the deadline (05 Mar. noon)
including a PDF report and your source codes. Please follow this format in naming your
folder: “Kesserwani_CW4”.

You might also like