diff --git a/Guide_to_limiters.ipynb b/Guide_to_limiters.ipynb index 92f8bbe..1d3a104 100644 --- a/Guide_to_limiters.ipynb +++ b/Guide_to_limiters.ipynb @@ -11,7 +11,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "#An illustrated guide to limiters\n", + "# An illustrated guide to limiters\n", "## Or: how to interpolate non-smooth data without creating wiggles" ] }, @@ -26,7 +26,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "##Table of contents\n", + "## Table of contents\n", "- [Motivation: interpolation and wiggles](#Interpolation-and-wiggles)\n", "- [The simplest limiter: Minmod](#The-simplest-limiter:-Minmod)\n", "- [Other TVD limiters](#TVD-limiters)\n", @@ -36,9 +36,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "%matplotlib inline\n", @@ -51,9 +49,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "import mpld3 # Skip this cell if you don't have mpld3 installed\n", @@ -65,7 +61,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "##Interpolation and wiggles" + "## Interpolation and wiggles" ] }, { @@ -78,9 +74,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "k = 5\n", @@ -103,9 +97,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "def piecewise_constant_interp(x,y,xx):\n", @@ -118,7 +110,6 @@ "yy = piecewise_constant_interp(x,y,xx)\n", "plt.figure(figsize=size)\n", "plt.plot(xx,yy,'-k',lw=2)\n", - "plt.hold(True)\n", "plt.plot(x,y,'or',markersize=10,alpha=0.5)\n", "plt.axis( (-k, k, -0.1, 2.1) );\n", "plt.title('Piecewise-constant approximation',fontsize=20);" @@ -146,9 +137,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "def piecewise_linear_interp(x,y,xx, fd='centered'):\n", @@ -170,7 +159,6 @@ " for i, fd in enumerate( ('centered','forward','backward') ):\n", " yy = piecewise_linear_interp(x,y,xx,fd=fd)\n", " ax[i].plot(xx,yy,'-k',lw=2)\n", - " ax[i].hold(True)\n", " ax[i].plot(x,y,'or',markersize=10,alpha=0.5)\n", " ax[i].axis( axis );\n", " ax[i].text(.5,.9,fd,\n", @@ -206,9 +194,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "y = np.sin(x/2.)+1. + 2.*(x>0)\n", @@ -243,9 +229,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "def pw_minmod(x,y,xx):\n", @@ -268,15 +252,12 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "yy = pw_minmod(x,y,xx)\n", "plt.figure(figsize=size)\n", "plt.plot(xx,yy,'-k',lw=2)\n", - "plt.hold(True)\n", "plt.plot(x,y,'or',markersize=10,alpha=0.5)\n", "plt.axis( (-4,4,-0.5,4.8) );\n", "plt.title('Minmod approximation',fontsize=20);" @@ -292,9 +273,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "from matplotlib.patches import Rectangle\n", @@ -303,7 +282,6 @@ "yy = pw_minmod(x,y,xx)\n", "plt.figure(figsize=(width,6))\n", "plt.plot(xx,yy,'-k',lw=2)\n", - "plt.hold(True)\n", "plt.plot(x,y,'or',markersize=10,alpha=0.5)\n", "plt.axis( (-4,4,-0.1,4.1) );\n", "plt.title('minmod approximation',fontsize=20);\n", @@ -314,7 +292,7 @@ " currentAxis = plt.gca()\n", " currentAxis.add_patch(Rectangle((x_avgs[0], y_avgs[0]), \n", " x_avgs[1]-x_avgs[0], y_avgs[1]-y_avgs[0], \n", - " facecolor=\"grey\",alpha=0.2))" + " facecolor=\"grey\",alpha=0.5))" ] }, { @@ -328,7 +306,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "##Total variation" + "## Total variation" ] }, { @@ -371,9 +349,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "def phi(theta,limiter):\n", @@ -408,9 +384,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "y = np.sin(x/2.)+1. + 2.*(x>0)\n", @@ -418,7 +392,6 @@ "for i, limiter in enumerate( ('minmod', 'vanleer','superbee','MC') ):\n", " yy = pw_limited(x,y,xx,limiter=limiter)\n", " ax[i].plot(xx,yy,'-k',lw=2)\n", - " ax[i].hold(True)\n", " ax[i].plot(x,y,'or',markersize=10,alpha=0.5)\n", " ax[i].axis( (-4,4,-0.1,4.4) );\n", " ax[i].text(.8,.2,limiter,\n", @@ -439,19 +412,17 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "#Interactive comparison\n", + "# Interactive comparison\n", "In each region, these limiter take three data points and give back a linear interpolant. It's illuminating to compare their behavior on a single set of 3 points. Note that the interactive plot below doesn't work on nbviewer; you'll need to download and run the notebook yourself." ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ - "from IPython.html.widgets import interact, FloatSlider, RadioButtons\n", + "from ipywidgets import interact, FloatSlider, RadioButtons\n", "from IPython.display import display\n", "%matplotlib inline" ] @@ -459,9 +430,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "xx = np.linspace(-0.5,0.5)\n", @@ -477,18 +446,16 @@ " theta = y3\n", " else:\n", " theta = y3/(-y1)\n", - " ax.hold(True)\n", " forward_slope = y3\n", " backward_slope = -y1\n", " plt.fill_between(xx,xx*forward_slope,xx*backward_slope,color='k',alpha=0.2,zorder=0)\n", " for limiter in ('minmod', 'vanleer','superbee','MC'):\n", " sigma = phi(np.array(theta),limiter)*(-y1)\n", " ax.plot(xx,sigma*xx,alpha=0.5,lw=2)\n", - " ax.legend( ('minmod', 'vanleer','superbee','MC'), loc='best' )\n", + " ax.legend( ('','minmod', 'vanleer','superbee','MC'), loc='best' )\n", " ax.plot(x,y,'ok',markersize=15,alpha=0.5)\n", - " ax.hold(False)\n", "\n", - " return fig\n", + " #return fig\n", "\n", "interact(compare_limiters,y1=FloatSlider(min=-1., max=1., step=0.1, value=-0.3,description='$y_{i-1}$',labelcolor='k'),#,orientation='vertical'),\n", " y3=FloatSlider(min=-1., max=1., step=0.1, value=0.8,description='$y_{i+1}$'));#,orientation='vertical'));" @@ -520,9 +487,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "# Note: uses PyWENO v. 0.11.2\n", @@ -533,9 +498,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "import mpld3 # Skip this cell if you don't have mpld3 installed\n", @@ -548,9 +511,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "weno_order = 5 # must be odd\n", @@ -568,9 +529,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "def stencil_interpolant(x,y,n,offset):\n", @@ -584,12 +543,10 @@ " fig, axis = plt.subplots(figsize=size)\n", " xc = np.linspace(-0.5,0.5)\n", " xx = np.linspace(-(k-1),k-1)\n", - " plt.hold(True)\n", " for i, interpolant in enumerate(interpolants):\n", " axis.plot(xx,interpolant(xx),'-'+color[i])\n", " axis.plot(xc,interpolant(xc),'-'+color[i],linewidth=5,alpha=0.5)\n", " axis.plot(x,y,'ok')\n", - " axis.hold(False)\n", " axis.axis((-(k-.5),k-.5,-0.5,2.1));" ] }, @@ -610,9 +567,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "p_opt = stencil_interpolant(x,y,5,0)\n", @@ -623,15 +578,13 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "fig, ax = plt.subplots(3,1,figsize=(width,10))\n", "names = ['left','right','center']\n", "p = []\n", - "for i in range(k):\n", + "for i in range(int(k)):\n", " p.append(stencil_interpolant(x,y,k,i))\n", " plot_interpolants(x,y,[p[i]],axis=ax[i],color=[colors[i]])\n", " ax[i].set_title(names[i]+' interpolant')" @@ -647,9 +600,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "plot_interpolants(x,y,p+[p_opt],color='brgk')" @@ -673,9 +624,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "def compute_opt_weights(k,xi):\n", @@ -719,9 +668,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "def compute_smoothness_indicators(y,k):\n", @@ -753,9 +700,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "def compute_weights(gamma, beta, epsilon=1.e-6):\n", @@ -771,9 +716,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "q = {}\n", @@ -792,9 +735,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "plot_interpolants(x,y,p+[p_opt],color=['b','r','g','k'])\n", @@ -815,9 +756,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "styles = { 'left' : 'b', 'center' : 'r', 'right' : 'g'}\n", @@ -878,9 +817,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "%matplotlib inline\n", @@ -899,9 +836,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "y=np.array( (1,1,1,0,0) )\n", @@ -934,9 +869,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "from clawpack import pyclaw\n", @@ -1000,9 +933,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "#This cell may take a few seconds to run\n", @@ -1067,32 +998,43 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "3.9", "language": "python", - "name": "python2" + "name": "3.9" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.9" + "pygments_lexer": "ipython3", + "version": "3.9.10" + }, + "toc": { + "base_numbering": 1, + "nav_menu": {}, + "number_sections": true, + "sideBar": true, + "skip_h1_title": false, + "title_cell": "Table of Contents", + "title_sidebar": "Contents", + "toc_cell": false, + "toc_position": {}, + "toc_section_display": true, + "toc_window_display": false } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Lesson_03_High-resolution_methods.ipynb b/Lesson_03_High-resolution_methods.ipynb index f848e47..6ca64e0 100644 --- a/Lesson_03_High-resolution_methods.ipynb +++ b/Lesson_03_High-resolution_methods.ipynb @@ -212,7 +212,7 @@ "source": [ "We'll use these interface values to approximate the flux, based on the **Lax-Friedrichs flux**:\n", "\n", - "$$F_\\imh = \\frac{1}{2} \\left( f(q^-_\\imh) + f(q^+_\\imh) - \\frac{\\Dt}{\\Dx} (q^+_\\imh - q^-_\\imh)\\right)$$\n", + "$$F_\\imh = \\frac{1}{2} \\left( f(q^-_\\imh) + f(q^+_\\imh) - \\frac{\\Dx}{\\Dt} (q^+_\\imh - q^-_\\imh)\\right)$$\n", "\n", "This provides second-order accuracy in space. We also need to make the method second-order accurate in time. We can do so by using a second-order Runge--Kutta method. Then the full method is\n", "\n", diff --git a/Lesson_04_Fluid_dynamics.ipynb b/Lesson_04_Fluid_dynamics.ipynb index 2216085..90b9249 100644 --- a/Lesson_04_Fluid_dynamics.ipynb +++ b/Lesson_04_Fluid_dynamics.ipynb @@ -227,7 +227,7 @@ "\n", "Like the momentum flux, the energy flux involves both bulk transport ($Eu$) and transport due to pressure ($pu$):\n", "\n", - "$$E_t + (u(E+p)) = 0.$$" + "$$E_t + (u(E+p))_x = 0.$$" ] }, {