<jupyter_start><jupyter_text>AG Dynamics of the Earth    Jupyter notebooks    <NAME>      Dynamic systems: 9. Shallow-water equations 2D advection equation revisited (finite volumes)----*<NAME>,Geophysics Section,Institute of Geological Sciences,Freie Universität Berlin,Germany*<jupyter_code>import numpy as np
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>---- 2DConsider a partial differential equation of the form:$$\frac{\partial h}{\partial t}+ u \frac{\partial h}{\partial x} + v \frac{\partial h}{\partial y}= 0$$with- $h$ [m] height,- $t$ [s] time,- $x,y$ [m] $x$- and $y$ distance,- $u,v$ [m/s] $x$ and $y$ advection velocities.We need an initial condition $h(x,y,t=0)$ to solve the problem.We reformulate the 2D problem to a (pseudo-)non-linear flux problem:$$\frac{\partial h}{\partial t}+ \frac{\partial (hu)}{\partial x} + \frac{\partial (hv)}{\partial y}= 0$$and we identify the terms $hu$ and $hv$ as flux terms:$$\begin{array}{rcl}f(u) &=& h u \\g(v) &=& h v\end{array}$$Define model domain $x \in [xmin,xmax]$ with $nx$ points,$y \in [ymin,ymax]$ with $ny$ points,maximum time $T$,velocities $u,v$ as constant,and spatial ($dx,dy$) as well time ($dt$) increments. Check **Courant number**!<jupyter_code>nx = 101
ny = 51
xmin = 0;xmax = 10
ymin = 0;ymax = 10
T    = 5
x = np.linspace(xmin,xmax,nx)
y = np.linspace(ymin,ymax,ny)
X,Y = np.meshgrid(x,y,indexing='ij')
dt = 0.05
dx = (x.max()-x.min()) / (nx-1)
dy = (y.max()-y.min()) / (ny-1)
u  = 1.0 # and test with u=1!!!
v  = 0.3
g  = 9.81
mu = 2
sigma = 1

print("dx: ",dx," dt: ",dt," u: ",u," v: ",v)
#print(x)
Courant = u*dt/dx
print ('Courant number: ',Courant)<jupyter_output>dx:  0.1  dt:  0.05  u:  1.0  v:  0.3
Courant number:  0.5<jupyter_text>---- 1. ExampleUse a Gaussian function in 2D as initial condition:$$h(x,y) = \exp(-\frac{(x-\mu)^2+(y-\mu)^2}{\sigma^2})$$with- $\mu=2$ mean,- $\sigma=1$ standard deviation.<jupyter_code># Finite volume with Rusanov flux scheme
# start time
time   = 0
dtplot = 1.00
tplot  = dtplot
# initial values
h = np.exp(-((X-mu)**2+(Y-mu)**2)/sigma**2)
plt.figure(figsize=(7,7))
plt.xlabel('x [m]')
plt.ylabel('y [m]')
plt.title('Advection of Gauss function in 2D')
plt.contour(X,Y,h,levels=[0.5,0.8])
# solution
while (time < T):
    time = time + dt
    ### PERIODIC BC ###
    h[0,:]  = h[-2,:]
    h[-1,:] = h[1,:]
    h[:,0]  = h[:,-2]
    h[:,-1] = h[:,1]
    
    Fhup = h[1:,:]*u
    Fhum = h[:-1,:]*u
    Fhvp = h[:,1:]*v
    Fhvm = h[:,:-1]*v
    #print(Fhup.shape,Fhum.shape)
    #print(Fhvp.shape,Fhvm.shape)
    Rhx  = (Fhup+Fhum)/2 - u/2*(h[1:,:]-h[:-1,:])
    Rhy  = (Fhvp+Fhvm)/2 - v/2*(h[:,1:]-h[:,:-1])
    #print(Rhx.shape,Rhy.shape)
    h[1:-1,1:-1] = h[1:-1,1:-1] - dt/dx*(Rhx[1:,1:-1]-Rhx[:-1,1:-1]) \
                                - dt/dy*(Rhy[1:-1,1:]-Rhy[1:-1,:-1])
    if (time > tplot):
        plt.contour(X,Y,h,levels=[0.5,0.8])
        tplot = tplot + dtplot<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.22804054054054054, 'fraction_numerical': 0.037162162162162164, 'mean_word_length': 3.5624036979969183, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '315768', 'license': 'MIT', 'n_tokens_mistral': 1339, 'n_tokens_neox': 1273, 'n_words': 295, 'path': '.ipynb_checkpoints/Dynamics_lab09_advection2D_FiniteVolume_LLF-checkpoint.ipynb', 'repo_name': 'georgkaufmann/Lecture_Dynamics', 'size': '2976'}