---
# System prepended metadata

title: '***Computational Hydraulics***'
tags: [HW2.1 & 2.2]

---

---
tags: HW2.1 & 2.2
---
# ***Computational Hydraulics***
## **Group 01:** N88071053 洪豪男 N88091011 黃彥鈞
## **HW2.1:** Please try (a) centrial scheme; (b) up-wind scheme, for the   traveling wave problem.
### (a) central scheme
$\quad\quad u_{i+1/2}^{n} = 
\frac{1}{2}\left(u_{i+1}^{n}+u_{i}^{n}\right)$

$\quad\quad 
u_{i-1/2}^{n} = 
\frac{1}{2}\left(u_{i}^{n}+u_{i-1}^{n}\right)$


```
%  Traveling wave (jump)      Central Scheme (Example 2.2)
%
%   u_t + a u_x = 0, a = 1.0
%
clear all;
%
a = 4.0;
x = -180:1:180;
dx = x(2) - x(1);
nx = length(x);
%
u = zeros(size(x));
%
%   Initial condition
%
A = 0.5; % amplitude
for ii = 1:length(u)
    if x(ii)>-180.0 & x(ii)<180.0
        agl = (x(ii)-30.)*2*pi/60.;
        tmp = sin(agl);
        u(ii) = A*tmp;
    else
        u(ii) = 0.0;
    end
end
%
figure(1);
plot(x,u,'b-o');
xlabel('x');
ylabel('u');
title('initial condition');
axis([-100 100 -1.8 1.8]);
%
u0 = u;
u_new = zeros(size(u));
%
stepNo = 0;
TotalStep = 200;
%
CFL = 0.8;
dt = CFL*dx/abs(a);
%
v = VideoWriter('central.avi');
open(v);
%
figure(2);
for ss = 1:TotalStep
    stepNo = stepNo + 1;
    for jj = 2:nx-1
        upls = 0.5*(u(jj)+u(jj+1));
        umns = 0.5*(u(jj)+u(jj-1));
        Fpls = a*upls;
        Fmns = a*umns;
        u_new(jj) = u(jj) - dt/dx*(Fpls-Fmns);
    end
%   B.C. (Periodic BC)
    u_new(1)  = u_new(nx-1);
    u_new(nx) = u_new(2);
%
    u = u_new;
%
    plot(x,u0,'-r',x,u,'b-');
    xlabel('x');
    ylabel('u');
    axis([-100 100 -1.8 1.8]);
    text(-10,1.6,['Step No.= ',num2str(stepNo)]);
    pause(0.01);
    frame = getframe(gcf);
    writeVideo(v,frame);
end
close(v);
%
```
https://youtu.be/ZyY5cXBxliA


### (b) up-wind scheme

$\quad\quad u_{i+1/2}^{n} = 
\left\{\begin{array}{left}
u_i^n\;\;\quad\hbox{for}\quad a >0\\
u_{i+1}^n\quad\hbox{for}\quad a <0
\end{array}\right.$


$\quad\quad u_{i-1/2}^{n} = 
\left\{\begin{array}{left}
u_{i-1}^n\quad\hbox{for}\quad a >0\\
u_{i}^n\;\;\quad\hbox{for}\quad a <0
\end{array}\right.$

```
%
%  Traveling wave (jump)      Upwind Scheme (Example 2.2)
%
%   u_t + a u_x = 0, a = 1.0
%
clear all;
%
a = 4.0;
x = -180:1:180;
dx = x(2) - x(1);
nx = length(x);
%
u = zeros(size(x));
%
%   Initial condition
%
A = 0.5; % amplitude
for ii = 1:length(u)
    if  x(ii)>-180.0 & x(ii)<180.0 
        agl = (x(ii)-30.)*2*pi/60.;
        tmp = sin(agl);
        u(ii) = A*tmp;
    else
        u(ii) = 0.0;
    end
end
%
 figure(1);
plot(x,u,'b-o');
xlabel('x');
ylabel('u');
title('initial condition');
axis([-100 100 -1.8 1.8]);
%
u0 = u;
u_new = zeros(size(u));
%
stepNo = 0;
TotalStep = 200;
%
CFL = 0.8;
dt = CFL*dx/abs(a);
%
v = VideoWriter('upwind.avi');
open(v);
%
 figure(2);
for ss = 1:TotalStep
    stepNo = stepNo + 1;
    for jj = 2:nx-1
        if a>=0
            upls = u(jj);
            umns = u(jj-1);
        else
            upls = u(jj+1);
            umns = u(jj);
        end
        Fpls = a*upls;
        Fmns = a*umns;
        u_new(jj) = u(jj) - dt/dx*(Fpls-Fmns);
    end
%   B.C. (Periodic BC)
    u_new(1)  = u_new(nx-1);
    u_new(nx) = u_new(2);
%
    u = u_new;
%
    plot(x,u0,'-r',x,u,'b-');
    xlabel('x');
    ylabel('u');
    axis([-100 100 -1.8 1.8]);
    text(-10,1.6,['Step No.= ',num2str(stepNo)]);
    pause(0.01);
    
    frame = getframe(gcf);
    writeVideo(v,frame);
end
close(v);
%
```
https://youtu.be/vSk9Ou6m0tM


## **HW2.2:** Please use the CFL number (a) $C=0.8\,$; and (b) $C=1.2\,$, for the above traveling wave problem. (PS either centrial scheme or up-wind scheme)

$C=\dfrac{|a|\,\Delta t}{\Delta x}<C_{\rm max}=1\Rightarrow\quad \Delta t=\dfrac{C\,\Delta x}{|a|}$

### (a) central scheme
$C=1.2$
https://youtu.be/aCySIU15Ius

### (b) up-wind scheme
$C=1.2$
https://youtu.be/LLbGH1JthbQ
