{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from IPython.core.display import HTML\n", "css_file = './custom.css'\n", "HTML(open(css_file, \"r\").read())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "###### Content provided under a Creative Commons Attribution license, CC-BY 4.0; code under MIT License. (c)2014 [David I. Ketcheson](http://davidketcheson.info)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### version 0.1 - May 2014" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Hyperbolic Conservation Laws" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\\begin{equation*}\n", "\\newcommand{Dx}{\\Delta x}\n", "\\newcommand{Dt}{\\Delta t}\n", "\\newcommand{imh}{{i-1/2}}\n", "\\newcommand{iph}{{i+1/2}}\n", "\\end{equation*}\n", "Many models of wave phenomena are governed by *hyperbolic conservation laws*. In this short course, we will learn about hyperbolic conservation laws and their numerical solution." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Conservation of mass" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Imagine a fluid flowing in a narrow tube. We'll use $q$ to indicate the density of the fluid and $u$ to indicate its velocity. Both of these are functions of space and time: $q = q(x,t)$; $u=u(x,t)$. The total mass in the section of tube $[x_1,x_2]$ is\n", "\n", "\\begin{equation}\n", "\\int_{x_1}^{x_2} q(x,t) dx.\n", "\\end{equation}\n", "\n", "This total mass can change in time due to fluid flowing in or out of this section of the tube. We call the rate of flow the *flux*, and represent it with the function $f(q)$. Thus the net rate of flow of mass into (or out of) the interval $[x_1,x_2]$ at time $t$ is\n", "\n", "$$f(q(x_1,t)) - f(q(x_2,t)).$$\n", "\n", "We just said that this rate of flow must equal the time rate of change of total mass; i.e.\n", "\n", "$$\\frac{d}{dt} \\int_{x_1}^{x_2} q(x,t) dx = f(q(x_1,t)) - f(q(x_2,t)).$$\n", "\n", "Now since $\\int_{x_1}^{x_2} \\frac{\\partial}{\\partial x} f(q) dx = f(q(x_2,t)) - f(q(x_1,t))$, we can rewrite this as\n", "\n", "$$\\frac{d}{dt} \\int_{x_1}^{x_2} q(x,t) dx = -\\int_{x_1}^{x_2} \\frac{\\partial}{\\partial x} f(q) dx.$$\n", "\n", "Under certain smoothness assumptions on $q$, we can move the time derivative inside the integral. We'll also put everything on the left side, to obtain\n", "\n", "$$\\int_{x_1}^{x_2} \\left(\\frac{\\partial}{\\partial t}q(x,t) + \\frac{\\partial}{\\partial x} f(q)\\right) dx = 0.$$\n", "\n", "Since this integral is zero for *any* choice of $x_1,x_2$, it must be that the integrand (the expression in parentheses) is actually zero *everywhere*! Therefore we can write the **differential conservation law**\n", "\n", "$$q_t + f_x = 0.$$\n", "\n", "Here and throughout the course, we use subscripts to denote partial derivatives.\n", "This equation expresses the fact that the total mass is conserved -- since locally the mass can change only due to a net inflow or outflow." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Advection" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In order to solve the conservation law above, we need an expression for the flux, $f$. The rate of flow is just mass times velocity: $f=u q$. Thus we obtain the **continuity equation**\n", "\n", "$$q_t + (uq)_x = 0.$$\n", "\n", "In general, we need another equation to determine the velocity $u(x,t)$. In [Lesson 4](Lesson_04_Fluid_dynamics.ipynb) we'll look at the full equations of fluid dynamics, but for now let's consider the simplest case, in which all of the fluid flows at a single, constant velocity $u(x,t)=a$. Then the continuity equation becomes the **advection equation**\n", "\n", "$$q_t + a q_x = 0.$$\n", "\n", "This equation has a very simple solution. If we are given the density $q(x,0)=q_0(x)$ at time zero, then the solution is just\n", "\n", "$$q(x,t) = q_0(x-at).$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's plot the solution of the advection equation on the interval $[0,1]$ for the initial condition\n", "$$q_0(x) = e^{-2(x-1/2)^2}.$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First, let's import all the modules we'll need." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "from matplotlib import animation\n", "from clawpack.visclaw.JSAnimation import IPython_display" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next, we'll set up a grid and the initial condition:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [], "source": [ "x = np.linspace(0,1,1000) # Spatial grid\n", "t = np.linspace(0,1) # Temporal grid\n", "a = 1.0 # Advection speed\n", "\n", "def q_0(x): # Initial condition\n", " return np.exp(-200.*(x-0.2)**2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally, let's make an animation of the solution. It will take a few moments to run this code. For now, you don't need to worry about understanding all of the plotting code below. Just play with the animation until you have a feel for how the solution behaves." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "
\n", " \n", "
\n", " \n", "
\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", "\n", " \n", " \n", " Once \n", " Loop \n", " Reflect \n", "
\n", "\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fig = plt.figure(figsize=(8,4)) # Create an empty figure\n", "ax = plt.axes()\n", "line, = ax.plot([], [],linewidth=2) # Create an empty line plot\n", "plt.axis((0,1,-0.1,1.1)) # Set the bounds of the plot\n", "\n", "def plot_q(t):\n", " line.set_data(x,q_0(x-a*t)) # Replace the line plot with the solution at time t\n", " \n", "animation.FuncAnimation(fig, plot_q, frames=t) # Animate the solution" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As you can see, the initial pulse just moves to the right at speed $a$ as time advances. This isn't very interesting, but it captures the most important feature of hyperbolic equations: waves travel at finite speed." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Characteristics" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice that the solution value is constant along the line $x-at=x_0$, in the $x-t$ plane, for each value of $x_0$. These lines are called **characteristics**; they are the trajectories along which solution information is transmitted. The value $a$ is referred to as the **characteristic velocity**. The code below plots some of these characteristics.\n", "\n", "When we learn about more complicated conservation laws, we'll see that information still travels along characteristics, but those characteristics aren't necessarily straight lines." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(0, 1)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" }, { "data": { "image/png": [ "iVBORw0KGgoAAAANSUhEUgAAAeYAAAEACAYAAABmjtVQAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\n", "AAALEgAACxIB0t1+/AAAGTtJREFUeJzt3X+o5fdd5/Hna5JUpmTbrmToH8lI1JTZutBsFNNGNzjS\n", "gmkFS+qSUn8sWqlhSUogSqMi2yDRbf5YcGtLKd2mhECaP3SL2aVMcdBBU5pq2CRNaxIyqWXzQ0p/\n", "qBTpHw2+949zTr29uXPvuefHPe/v9zwf8IG5c78593O+ue/7nO85Z86kqpAkST0c2/QGJEnSvzLM\n", "kiQ1YpglSWrEMEuS1IhhliSpEcMsSVIjB4Y5yT1JvprkiX2O+WCSZ5I8nuSa1W5RkqTtMc8V8yeA\n", "Gy70ySRvA66qqtcBvw58ZEV7kyRp6xwY5qr6K+Af9jnk54B7p8d+HnhNkteuZnuSJG2XVTzHfDnw\n", "3I6PnweuWMHtSpK0dS5e0e1k18ff8z6fSXzfT0nS1qmq3X080CqumF8ATu74+Irp70mSpENaxRXz\n", "g8CtwANJ3gT8Y1V9da8DF/mTg+aX5M6qunPT+xg7z/P6eY7Xz3O8OklOAL8JvAf4JPAB4P8tensH\n", "hjnJJ4GfAi5L8hzwfuASgKr6aFV9OsnbkpwH/hn41UU3I0nSUOwR5Kur6rnp5xa+3QPDXFXvmuOY\n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7kny1SRP7HPM4Zq34r9QvZI3JHEtfY6vY/qX75m8QYXneMXn\n", "eMdxfw78H+DnN73vIa05v49fw+SNVq6YfnzZpvc9tDXneb4T+G+zcwx8A7h403sfymLyJjTXAE9c\n", "4POHbt6qr5hX/oYkepkDz3FVfa7+9e/6fZ4L/L1yXdA838cA7wX+mMk7Eelw5jnHvwD8SVU9D1BV\n", "Xz/iPY7BPOf574FXTX/9KuAbVfXSEe5x0Krqr4B/2OeQQzdv1WFe6RuSaE/znOOdfg349Fp3ND4H\n", "nuMklzP5AfeR6W/5Yo3Dmef7+HXA9yf5iySPJPnlI9vdeMxznj8G/PskLzL59w5uO6K9bYtDN++g\n", "vy51WPP+cNr916f8oTa/uc9Vkp9m8v6+P7m+7YzSPOf4D4HfqqpKEl7+Pa39zXOOLwF+FHgz8Erg\n", "c0kerqpn1rqzcZnnPP8O8FhVnU7yw8CfJbm6qr615r1tk0M1b9Vh9g1J1m+ec8z0BV8fY/LObfs9\n", "zKKXm+cc/xiTv7sPk+fl3prkO1X14NFscfDmOcfPAV+vqm8D307yl8DVTP4lO81nnvP8E8DvA1TV\n", "s0n+jsk/GPHIkexw/A7dvFU/lP3dNyRJ8gomb0iy+wfVg8B/BrjQG5JoXwee4yQ/APwv4Jeq6vwG\n", "9jh0B57jqvqhqvrBqvpBJs8z/xejfCjz/Kz4U+A/JrkoySuZvHDmb494n0M3z3l+CngLwPS5z1NM\n", "/jUtrcahm7fSK+byDUnWbp5zDPxX4N8CH5le0X2nqq7d1J6HZs5zrCXM+bPiqSRngC8A/wJ8rKoM\n", "8yHM+b38B8AnkjzO5GLtfVX1zY1temCSfBL4KeCyJM8B72fyNMzCzfMNRiRJamTlbzAiSZIWZ5gl\n", "SWrEMEuS1IhhliSpEcMsSVIjhlmSpEYMsyRJjRhmSZIa+f8AlyF7zPx0nQAAAABJRU5ErkJggg==\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "fig = plt.figure(figsize=(8,4))\n", "ax = plt.axes()\n", "\n", "for x_0 in np.linspace(0,1,10):\n", " ax.plot(x,(x-x_0)/a,'-k')\n", "plt.ylim(0,1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## A finite volume method for advection" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can easily solve the advection equation exactly. But the advection equation is a prototype for more complicated conservation laws that we will only be able to solve approximately by using numerical methods. In order to better understand these methods, we will discuss them first in the context of the advection equation." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For simplicity, we'll suppose that we wish to solve the advection equation on the interval $[0,1]$. We introduce a set of equally spaced *grid cells* of width $\\Dx$, and write $x_i$ to mean the center of cell $i$. Thus the first cell is the interval $[0,\\Dx]$ and $x_1=\\Dx/2$. We will also write $x_\\imh$ or $x_\\iph$ to denote the left or right boundary of cell $i$, respectively.\n", "\n", "We write $Q_i$ to denote the *average* value of the solution over cell $i$:\n", "\n", "$$Q_i = \\frac{1}{\\Dx} \\int_{x_\\imh}^{x_\\iph} q \\ dx.$$\n", "\n", "The simplest finite volume method is obtained by supposing that the solution is actually *equal* to $Q_i$ over all of cell $i$." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![](./figures/finite_volume.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Suppose $a>0$. Then the flux into cell $i$ from the left is $a Q_{i-1}$ and the flux out of cell $i$ to the right is $a Q_i$. Then our integral conservation law reads\n", "\n", "$$Q_i'(t) = -\\frac{a}{\\Dx}\\left(Q_i - Q_{i-1}\\right).$$\n", "\n", "Applying a forward difference in time we obtain the *upwind method*\n", "\n", "$$Q^{n+1}_i = Q^n_i -\\frac{a}{\\Dx}\\left(Q_i - Q_{i-1}\\right).$$\n", "\n", "We call this the upwind method because the solution behaves as if it were being blown by a wind to the right, and the method uses the value $Q_{i-1}$ from the upwind direction.\n", "\n", "Here is a bit of Python code to solve the advection equation using the upwind method." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "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ud8/v7ptvwGcVnbN\n", "/T4+4DOkETM3A58su+Z+Hh/p09b38u/ezcAZZdfcwbF9kzQLwPOkT1gf7TVbfBGTmVkFDcW8w2Zm\n", "1l8OdzOzCnK4m5lVkMPdzKyCHO5mZhXkcDczqyCHu5lZBTnczcwq6P8D4igGjQtUaXMAAAAASUVO\n", "RK5CYII=\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "a = 1.0 # advection speed\n", "\n", "m = 50 # number of cells\n", "dx = 1./m # Size of 1 grid cell\n", "x = np.arange(-dx/2, 1.+dx/2, dx) # Cell centers, including ghost cells\n", "\n", "t = 0. # Initial time\n", "T = 0.5 # Final time\n", "dt = 0.8 * dx / a # Time step\n", "\n", "Q = np.exp(-200*(x-0.2)**2) # Initial data\n", "Qnew = np.empty(Q.shape)\n", "\n", "while t < T:\n", " \n", " # Extrapolation at boundaries:\n", " Qnew[0] = Q[1]\n", " Qnew[-1] = Q[-2]\n", " \n", " for i in range(1,len(x)):\n", " Qnew[i] = Q[i] - a*dt/dx * (Q[i]-Q[i-1])\n", " \n", " Q = Qnew.copy()\n", " t = t + dt\n", " \n", "plt.plot(x,Q,linewidth = 2)\n", "plt.title('t = '+str(t));" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice how we set up a grid that contains an extra cell at each end, outside of the problem domain $[0,1]$. These are called **ghost cells** and are often useful in handling the solution at the grid boundaries." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![](./figures/ghost-cell.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " The technique we have used to set the ghost cell values above, by copying the last value inside the grid to the ghost cells, is known as **zero-order extrapolation**. It is useful for allowing waves to pass out of the domain (so-called *non-reflecting* boundaries). Note that we don't actually need the ghost cell at the right end for the upwind method, but for other methods we will." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The upwind method is simple, but it is not very accurate. Notice how the computed solution becomes wider and shorter over time. This behavior is referred to as *dissipation*." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now do the following with the code above:\n", "\n", "1. Set $m=1000$ or more and notice that it takes some time to compute the solution. Rewrite the inner loop (over $i$) as a single line with no loop, using numpy slicing. For large values of $m$, the code with slicing is much faster.\n", "1. Notice that the last step of the simulation goes past time $T$. Modify the code so that the last step is adjusted to exactly reach $T$.\n", "2. Change the code so that animation of the solution versus time is plotted. You will want to accumulate frames of the solution in a list and then use the same kind of code we used above to animate the exact solution.\n", "3. Add some code to plot the exact solution.\n", "\n", "*Extra credit*: change the left boundary condition so that there is a sinusoidal wave coming in from the left:\n", "$$u(0,t) = \\sin(20 \\pi t).$$\n", "What do you notice about the sinusoid as it moves into the domain?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "After making it through the exercise above, you should feel pretty comfortable with the basics of scientific programming in Python." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The CFL condition" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Take a look at the line of code that sets the time step:\n", "```python\n", " dt = 0.8 * dx / a \n", "```\n", "You might be wondering where that formula came from. Rearranging that equation, we have\n", "$$a \\frac{\\Delta t}{\\Delta x} = 0.8.$$\n", "The quantity $\\nu = a \\frac{\\Delta t}{\\Delta x}$ is the distance the exact solution moves during each time step, in units of grid cells. It is referred to as the *CFL number* or just the *Courant number* after the authors Courant, Friedrichs and Lewy who [established its importance](http://www.stat.uchicago.edu/~lekheng/courses/302/classics/courant-friedrichs-lewy.pdf). Try the following values of $\\nu$ in the code above, and compare the results with those you obtained already using $\\nu=0.8$.\n", "1. $\\nu = 1.0$\n", "2. $\\nu = 1.5$\n", "3. $\\nu = 0.1$\n", "\n", "Finally, try setting $a$ to a negative value. What happens?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The results you have observed can be explained as follows. Over a time step of size $\\Dt$, the solution moves by an amount $a \\Dt$. So $q(x_i,t_n)$ should be given exactly by $q(x_i-a\\Dt,t_{n-1})$. This is referred to as the *domain of dependence* of the solution.\n", "\n", "The upwind method uses the values $Q_i^{n-1}$ and $Q_{i-1}^{n-1}$ to compute $Q_i^n$. The points $(x_{i-1},t_n)$ and $(x_i,t_n)$ (as well as the locations of solution values they depend, and the ones those depend on, and so forth) are the *numerical domain of dependence*.\n", "\n", "For this to work, the true domain of dependence $x_i-a\\Dt$ must lie within the numerical domain of dependence, as $\\Dt,\\Dx \\to 0$. This is known as the [CFL condition](http://en.wikipedia.org/wiki/Courant%E2%80%93Friedrichs%E2%80%93Lewy_condition).\n", "\n", "For the upwind method, that means that we must have\n", "$$x_i - \\Dx \\le x_i - a \\Dt \\le x_i$$\n", "or in other words\n", "$$0 \\le a \\frac{\\Dt}{\\Dx} \\le 1.$$\n", "\n", "If the CFL condition is violated, the information that is used by the numerical method doesn't include the true information that influences the exact solution, so the numerical solution cannot be convergent." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The Lax-Friedrichs method" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The upwind method gets its name from the fact that it uses the value $U_{i-1}$ and not $U_{i+1}$. Assuming $a>0$, the correct solution value $U_i^{n+1}$ should come from a point to the left of $x_i$ at time $t_n$ (i.e., the wind blows to the right, so $x_{i-1}$ is *upwind* of $x_i$).\n", "\n", "This bias is fine for the advection equation, where we know everything moves in the same direction. But for more complicated conservation laws, things may move in either direction. It will be useful to have a method that uses information from both directions. The simplest such method is known as the **Lax-Friedrichs** method. For the conservation law $q_t + f(q)_x$, this method is\n", "\n", "$$Q_i^{n+1} = \\frac{1}{2}(Q_{i-1}^n + Q_{i+1}^n) - \\frac{\\Dt}{2\\Dx}\\left(f(Q_{i+1}^n) - f(Q_{i-1}^n)\\right).$$\n", "\n", "Notice that the flux difference term clearly approximates $f(q)_x$. Meanwhile, the value of $q$ itself is approximated by taking the average of two neighboring values. This average makes this method dissipative too (but it ensures that the solution is stable)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. What does the CFL condition imply for the time step when using the Lax-Friedrichs method?\n", "\n", "2. In the cell below, implement the Lax-Friedrichs method for advection." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Extra credit*: Compute the norm of the difference between the approximate and exact solution. How does it change if you decrease $\\Dx$?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Accuracy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The methods we have used so far (i.e., the *upwind method* and the *Lax-Friedrichs method*) are both dissipative. Furthermore, both of these methods are only *first order accurate*, meaning that if we reduce the values of $\\Dt$ and $\\Dx$ by a factor of two, the overall error decreases only by a factor of two. In [Lesson 3](Lesson_03_High-resolution_methods.ipynb), we will learn about more accurate methods. But first, in [Lesson 2](Lesson_02_Traffic.ipynb) we'll look at a model for traffic flow." ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.9" } }, "nbformat": 4, "nbformat_minor": 0 }