From 20f12243b9d01cacb2ee27796ab647ecb9a9d7d2 Mon Sep 17 00:00:00 2001 From: David Ketcheson Date: Mon, 22 May 2017 11:35:11 +0300 Subject: [PATCH] Improve presentation of Indtroduction in PDF. - Resolve issues with t=0 (see #75) - Make all code fit on page by shortening lines and reducing code font size - Avoid using top-level header (#) since that gets converted to a new chapter --- Introduction.ipynb | 50 ++++++++++++++++++---------------- exact_solvers/shallow_water.py | 8 +++++- riemann.tplx | 18 +++++++++++- 3 files changed, 50 insertions(+), 26 deletions(-) diff --git a/Introduction.ipynb b/Introduction.ipynb index bb7e9c2a..fd66ec28 100644 --- a/Introduction.ipynb +++ b/Introduction.ipynb @@ -36,6 +36,7 @@ "source": [ "%matplotlib inline\n", "from exact_solvers import shallow_water\n", + "demo_plot = shallow_water.make_demo_plot_function\n", "from ipywidgets import widgets, fixed\n", "from ipywidgets import interact" ] @@ -43,13 +44,12 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ - "interact(shallow_water.make_demo_plot_function(h_l=3., h_r=1., u_l=0., u_r=0),\n", - " t=widgets.FloatSlider(min=0.001,max=0.6,step=0.1,value=0.), fig=fixed(0))" + "interact(demo_plot(h_l=3., h_r=1., u_l=0., u_r=0),\n", + " t=widgets.FloatSlider(min=0., max=0.6, step=0.1,\n", + " value=0.), fig=fixed(0));" ] }, { @@ -114,8 +114,9 @@ "metadata": {}, "outputs": [], "source": [ - "interact(shallow_water.make_demo_plot_function(h_l=1., h_r=3., u_l=0., u_r=0),\n", - " t=widgets.FloatSlider(min=0.001,max=0.6,step=0.1,value=0.), fig=fixed(0));" + "interact(demo_plot(h_l=1., h_r=3., u_l=0., u_r=0),\n", + " t=widgets.FloatSlider(min=0.0, max=0.6, step=0.1,\n", + " value=0.), fig=fixed(0));" ] }, { @@ -131,8 +132,9 @@ "metadata": {}, "outputs": [], "source": [ - "interact(shallow_water.make_demo_plot_function(h_l=1., h_r=1., u_l=0.8, u_r=-0.8),\n", - " t=widgets.FloatSlider(min=0.001,max=0.6,step=0.1,value=0.), fig=fixed(0));" + "interact(demo_plot(h_l=1., h_r=1., u_l=0.8, u_r=-0.8),\n", + " t=widgets.FloatSlider(min=0., max=0.6, step=0.1,\n", + " value=0.), fig=fixed(0));" ] }, { @@ -148,8 +150,9 @@ "metadata": {}, "outputs": [], "source": [ - "interact(shallow_water.make_demo_plot_function(h_l=1., h_r=1., u_l=-0.8, u_r=0.8),\n", - " t=widgets.FloatSlider(min=0.001,max=0.6,step=0.1,value=0.), fig=fixed(0));" + "interact(demo_plot(h_l=1., h_r=1., u_l=-0.8, u_r=0.8),\n", + " t=widgets.FloatSlider(min=0.0, max=0.6, step=0.1,\n", + " value=0.), fig=fixed(0));" ] }, { @@ -218,7 +221,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# A linear example: acoustic waves\n", + "## A linear example: acoustic waves\n", "\n", "We end this introduction with a sample linear system. Acoustic waves in one dimension can be modeled by the system of PDEs\n", "\n", @@ -261,7 +264,11 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "tags": [ + "hide" + ] + }, "outputs": [], "source": [ "from IPython.core.display import display, HTML\n", @@ -281,22 +288,17 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "tags": [ + "hide" + ] + }, "outputs": [], "source": [ "from IPython.core.display import display, HTML\n", "app = open('phase_plane_shallow_water_verysmall.html','r').read()\n", "display(HTML(app))" ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [] } ], "metadata": { @@ -315,7 +317,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", - "version": "2.7.12" + "version": "2.7.13" } }, "nbformat": 4, diff --git a/exact_solvers/shallow_water.py b/exact_solvers/shallow_water.py index 2f3aed49..c1c7075d 100644 --- a/exact_solvers/shallow_water.py +++ b/exact_solvers/shallow_water.py @@ -427,7 +427,13 @@ def reval_rho_u(x): num_vars = len(primitive_variables) def plot_shallow_water_demo(t=0.5, fig=0): - q = np.array(reval(x/t)) + if t == 0: + q = np.zeros((2,len(x))) + q[0,:] = q_l[0]*(x<=0) + q_r[0]*(x>0) + q[1,:] = q_l[1]*(x<=0) + q_r[1]*(x>0) + else: + q = np.array(reval(x/t)) + if t<0.02: q[1] = np.where(x<0, q_l[1], q_r[1]) diff --git a/riemann.tplx b/riemann.tplx index 4551a57a..f48aad5c 100644 --- a/riemann.tplx +++ b/riemann.tplx @@ -30,7 +30,6 @@ ((* block title *)) \title{The Riemann Problem for Hyperbolic PDEs: Theory and Approximate Solvers} -\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\},fontsize=\small} ((* endblock title *)) ((* block maketitle *)) @@ -47,3 +46,20 @@ ((( super() ))) ((*- endif -*)) ((*- endblock any_cell -*)) + +((* block input scoped *)) + ((( add_my_prompt(cell.source | highlight_code(strip_verbatim=True), cell, 'In ', 'incolor') ))) +((* endblock input *)) + +% Purpose: Renders an output/input prompt +((* macro add_my_prompt(text, cell, prompt, prompt_color) -*)) + ((*- if cell.execution_count is defined -*)) + ((*- set execution_count = "" ~ (cell.execution_count | replace(None, " ")) -*)) + ((*- else -*)) + ((*- set execution_count = " " -*)) + ((*- endif -*)) + ((*- set indention = " " * (execution_count | length + 7) -*)) +\begin{Verbatim}[fontsize=\small,commandchars=\\\{\}] +((( text | add_prompts(first='{\color{' ~ prompt_color ~ '}' ~ prompt ~ '[{\\color{' ~ prompt_color ~ '}' ~ execution_count ~ '}]:} ', cont=indention) ))) +\end{Verbatim} +((*- endmacro *))