From 113a5b3047332355a94b35516641e6704ae5d502 Mon Sep 17 00:00:00 2001 From: mcflugen Date: Fri, 18 Oct 2024 12:04:41 -0600 Subject: [PATCH 1/7] remove notebook header, fix typo --- .../river_flow_dynamics_tutorial.ipynb | 23 +++---------------- 1 file changed, 3 insertions(+), 20 deletions(-) diff --git a/docs/source/tutorials/river_flow_dynamics/river_flow_dynamics_tutorial.ipynb b/docs/source/tutorials/river_flow_dynamics/river_flow_dynamics_tutorial.ipynb index 411bfaddd8..5c79746424 100644 --- a/docs/source/tutorials/river_flow_dynamics/river_flow_dynamics_tutorial.ipynb +++ b/docs/source/tutorials/river_flow_dynamics/river_flow_dynamics_tutorial.ipynb @@ -1,13 +1,5 @@ { "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "\n", - "" - ] - }, { "cell_type": "markdown", "metadata": {}, @@ -15,15 +7,6 @@ "# 2D Surface Water Flow component\n" ] }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "
\n", - "For more Landlab tutorials, click here: https://landlab.readthedocs.io/en/latest/user_guide/tutorials.html\n", - "
" - ] - }, { "cell_type": "markdown", "metadata": {}, @@ -66,7 +49,7 @@ "\n", "A semi-implicit, semi-Lagrangian, finite volume numerical approximation represents the depth averaged, 2D shallow-water equations described before. The water surface elevation, $\\eta$, is defined at the center of each computational volume (nodes). Water depth, $H$, and velocity components, $U$ and $V$, are defined at the midpoint of volume faces (links). The finite volume structure provides a control volume representation that is inherently mass conservative.\n", "\n", - "The combination of a semi-implciit water surface elevation solution and a semi-Lagrangian representation of advection provides the advantages of a stable solution and of time steps that exceed the CFL criterion. In the semi-implicit process, $\\eta$ in the momentum equations, and the velocity divergence in the continuity equation, are trated implicitly. The advective terms in the momentum equations, are discretized explicitly. See the cited literature for more details.\n", + "The combination of a semi-implciit water surface elevation solution and a semi-Lagrangian representation of advection provides the advantages of a stable solution and of time steps that exceed the CFL criterion. In the semi-implicit process, $\\eta$ in the momentum equations, and the velocity divergence in the continuity equation, are treated implicitly. The advective terms in the momentum equations, are discretized explicitly. See the cited literature for more details.\n", "\n", "### The component\n", "\n", @@ -708,9 +691,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.5" + "version": "3.12.0" } }, "nbformat": 4, - "nbformat_minor": 2 + "nbformat_minor": 4 } From 1458219d1fbf18f0307d3d6dc1492ffd3d77f0ee Mon Sep 17 00:00:00 2001 From: mcflugen Date: Fri, 18 Oct 2024 12:19:03 -0600 Subject: [PATCH 2/7] use new esri_ascii.load function --- .../river_flow_dynamics_tutorial.ipynb | 24 ++++--------------- 1 file changed, 4 insertions(+), 20 deletions(-) diff --git a/docs/source/tutorials/river_flow_dynamics/river_flow_dynamics_tutorial.ipynb b/docs/source/tutorials/river_flow_dynamics/river_flow_dynamics_tutorial.ipynb index 5c79746424..6c75e5db61 100644 --- a/docs/source/tutorials/river_flow_dynamics/river_flow_dynamics_tutorial.ipynb +++ b/docs/source/tutorials/river_flow_dynamics/river_flow_dynamics_tutorial.ipynb @@ -68,7 +68,7 @@ "\n", "from landlab import RasterModelGrid\n", "from landlab.components import river_flow_dynamics\n", - "from landlab.io import read_esri_ascii\n", + "from landlab.io import esri_ascii\n", "from landlab.plot.imshow import imshow_grid" ] }, @@ -365,7 +365,9 @@ "source": [ "# Getting the grid and some parameters\n", "asc_file = \"DEM-kootenai_37x50_1x1.asc\"\n", - "(grid, teDEM) = read_esri_ascii(asc_file, grid=None, reshape=False, name=None, halo=0)" + "with open(asc_file) as fp:\n", + " grid = esri_ascii.load(fp, name=\"topographic__elevation\")\n", + "te = grid.at_node[\"topographic__elevation\"]" ] }, { @@ -389,24 +391,6 @@ "dt = 1.0 # Timestep duration, [s]" ] }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Since the topography is provided by the DEM, we just need to assign it to our `\"topographic__elevation\"` field. It also provides the number of nodes and grid spacing, because they are inherent properties of the DEM." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# The grid represents a basic rectangular channel with slope equal to 0.01 m/m\n", - "te = grid.add_zeros(\"topographic__elevation\", at=\"node\")\n", - "te += teDEM" - ] - }, { "cell_type": "markdown", "metadata": {}, From 40356c7160efb581fad1e8f7393feb9e6634c40d Mon Sep 17 00:00:00 2001 From: mcflugen Date: Fri, 18 Oct 2024 12:25:05 -0600 Subject: [PATCH 3/7] more descriptive variable names --- .../river_flow_dynamics_tutorial.ipynb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/source/tutorials/river_flow_dynamics/river_flow_dynamics_tutorial.ipynb b/docs/source/tutorials/river_flow_dynamics/river_flow_dynamics_tutorial.ipynb index 6c75e5db61..12961b98f6 100644 --- a/docs/source/tutorials/river_flow_dynamics/river_flow_dynamics_tutorial.ipynb +++ b/docs/source/tutorials/river_flow_dynamics/river_flow_dynamics_tutorial.ipynb @@ -112,8 +112,8 @@ "outputs": [], "source": [ "# Basic parameters\n", - "n = 0.012 # Manning's roughness coefficient, [s/m^(1/3)]\n", - "S0 = 0.01 # Channel slope [m/m]\n", + "mannings_n = 0.012 # Manning's roughness coefficient, [s/m^(1/3)]\n", + "channel_slope = 0.01 # Channel slope [m/m]\n", "\n", "# Simulation parameters\n", "n_timesteps = 100 # Number of timesteps\n", @@ -156,7 +156,7 @@ "source": [ "# The grid represents a basic rectangular channel with slope equal to 0.01 m/m\n", "te = grid.add_zeros(\"topographic__elevation\", at=\"node\")\n", - "te += 1.0 - S0 * grid.x_of_node\n", + "te += 1.0 - channel_slope * grid.x_of_node\n", "te[grid.y_of_node > 1.5] = 2.5\n", "te[grid.y_of_node < 0.5] = 2.5" ] @@ -267,7 +267,7 @@ "rfd = river_flow_dynamics(\n", " grid,\n", " dt=dt,\n", - " mannings_n=n,\n", + " mannings_n=mannings_n,\n", " fixed_entry_nodes=fixed_entry_nodes,\n", " fixed_entry_links=fixed_entry_links,\n", " entry_nodes_h_values=entry_nodes_h_values,\n", @@ -384,7 +384,7 @@ "outputs": [], "source": [ "# Basic parameters\n", - "n = 0.012 # Manning's roughness coefficient, [s/m^(1/3)]\n", + "mannings_n = 0.012 # Manning's roughness coefficient, [s/m^(1/3)]\n", "\n", "# Simulation parameters\n", "n_timesteps = 75 # Number of timesteps\n", @@ -574,7 +574,7 @@ "rfd = river_flow_dynamics(\n", " grid,\n", " dt=dt,\n", - " mannings_n=n,\n", + " mannings_n=mannings_n,\n", " fixed_entry_nodes=fixed_entry_nodes,\n", " fixed_entry_links=fixed_entry_links,\n", " entry_nodes_h_values=entry_nodes_h_values,\n", From d6aaddf550353963b9a77e8f2cd0b22addc90f31 Mon Sep 17 00:00:00 2001 From: mcflugen Date: Fri, 18 Oct 2024 12:31:13 -0600 Subject: [PATCH 4/7] use grid.add_field --- .../river_flow_dynamics_tutorial.ipynb | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/docs/source/tutorials/river_flow_dynamics/river_flow_dynamics_tutorial.ipynb b/docs/source/tutorials/river_flow_dynamics/river_flow_dynamics_tutorial.ipynb index 12961b98f6..b7e537ee78 100644 --- a/docs/source/tutorials/river_flow_dynamics/river_flow_dynamics_tutorial.ipynb +++ b/docs/source/tutorials/river_flow_dynamics/river_flow_dynamics_tutorial.ipynb @@ -155,8 +155,7 @@ "outputs": [], "source": [ "# The grid represents a basic rectangular channel with slope equal to 0.01 m/m\n", - "te = grid.add_zeros(\"topographic__elevation\", at=\"node\")\n", - "te += 1.0 - channel_slope * grid.x_of_node\n", + "te = grid.add_field(\"topographic__elevation\", 1.0 - channel_slope * grid.x_of_node, at=\"node\")\n", "te[grid.y_of_node > 1.5] = 2.5\n", "te[grid.y_of_node < 0.5] = 2.5" ] @@ -198,8 +197,7 @@ "vel = grid.add_zeros(\"surface_water__velocity\", at=\"link\")\n", "\n", "# Calculating the initial water surface elevation from water depth and topographic elevation\n", - "wse = grid.add_zeros(\"surface_water__elevation\", at=\"node\")\n", - "wse += h + te" + "wse = grid.add_field(\"surface_water__elevation\", te, at=\"node\")" ] }, { @@ -428,8 +426,7 @@ "vel = grid.add_zeros(\"surface_water__velocity\", at=\"link\")\n", "\n", "# Calculating the initial water surface elevation from water depth and topographic elevation\n", - "wse = grid.add_zeros(\"surface_water__elevation\", at=\"node\")\n", - "wse += h + te" + "wse = grid.add_field(\"surface_water__elevation\", te, at=\"node\")" ] }, { From 01fc7ec44b52ce9514f1765c49160bc12d5d1024 Mon Sep 17 00:00:00 2001 From: mcflugen Date: Fri, 18 Oct 2024 12:33:47 -0600 Subject: [PATCH 5/7] use grid.imshow --- .../river_flow_dynamics_tutorial.ipynb | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/docs/source/tutorials/river_flow_dynamics/river_flow_dynamics_tutorial.ipynb b/docs/source/tutorials/river_flow_dynamics/river_flow_dynamics_tutorial.ipynb index b7e537ee78..5c0315a479 100644 --- a/docs/source/tutorials/river_flow_dynamics/river_flow_dynamics_tutorial.ipynb +++ b/docs/source/tutorials/river_flow_dynamics/river_flow_dynamics_tutorial.ipynb @@ -68,8 +68,7 @@ "\n", "from landlab import RasterModelGrid\n", "from landlab.components import river_flow_dynamics\n", - "from landlab.io import esri_ascii\n", - "from landlab.plot.imshow import imshow_grid" + "from landlab.io import esri_ascii" ] }, { @@ -174,7 +173,7 @@ "outputs": [], "source": [ "# Showing the topography\n", - "imshow_grid(grid, \"topographic__elevation\")" + "grid.imshow(\"topographic__elevation\")" ] }, { @@ -306,7 +305,7 @@ "\n", " if disp >= displayAnimationFreq:\n", " clear_output(wait=True) # This will clear the previous image\n", - " imshow_grid(grid, \"surface_water__depth\")\n", + " grid.imshow(\"surface_water__depth\")\n", " plt.show()\n", " disp = -1\n", "\n", @@ -326,7 +325,7 @@ "metadata": {}, "outputs": [], "source": [ - "imshow_grid(grid, \"surface_water__depth\")" + "grid.imshow(\"surface_water__depth\")" ] }, { @@ -342,7 +341,7 @@ "metadata": {}, "outputs": [], "source": [ - "imshow_grid(grid, \"surface_water__elevation\")" + "grid.imshow(\"surface_water__elevation\")" ] }, { @@ -403,7 +402,7 @@ "outputs": [], "source": [ "# Showing the topography\n", - "imshow_grid(grid, \"topographic__elevation\")" + "grid.imshow(\"topographic__elevation\")" ] }, { @@ -612,7 +611,7 @@ "\n", " if disp >= displayAnimationFreq:\n", " clear_output(wait=True) # This will clear the previous image\n", - " imshow_grid(grid, \"surface_water__depth\")\n", + " grid.imshow(\"surface_water__depth\")\n", " plt.show()\n", " disp = -1\n", "\n", @@ -632,7 +631,7 @@ "metadata": {}, "outputs": [], "source": [ - "imshow_grid(grid, \"surface_water__depth\")" + "grid.imshow(\"surface_water__depth\")" ] }, { From ee4b05a9cb92e3c486fe46a33a3dc435bad07928 Mon Sep 17 00:00:00 2001 From: mcflugen Date: Fri, 18 Oct 2024 12:47:14 -0600 Subject: [PATCH 6/7] use tqdm for progress bar --- .../river_flow_dynamics_tutorial.ipynb | 27 +++++-------------- 1 file changed, 6 insertions(+), 21 deletions(-) diff --git a/docs/source/tutorials/river_flow_dynamics/river_flow_dynamics_tutorial.ipynb b/docs/source/tutorials/river_flow_dynamics/river_flow_dynamics_tutorial.ipynb index 5c0315a479..9551ff24e5 100644 --- a/docs/source/tutorials/river_flow_dynamics/river_flow_dynamics_tutorial.ipynb +++ b/docs/source/tutorials/river_flow_dynamics/river_flow_dynamics_tutorial.ipynb @@ -65,6 +65,7 @@ "import matplotlib.pyplot as plt\n", "import numpy as np\n", "from IPython.display import clear_output\n", + "from tqdm import trange\n", "\n", "from landlab import RasterModelGrid\n", "from landlab.components import river_flow_dynamics\n", @@ -154,7 +155,9 @@ "outputs": [], "source": [ "# The grid represents a basic rectangular channel with slope equal to 0.01 m/m\n", - "te = grid.add_field(\"topographic__elevation\", 1.0 - channel_slope * grid.x_of_node, at=\"node\")\n", + "te = grid.add_field(\n", + " \"topographic__elevation\", 1.0 - channel_slope * grid.x_of_node, at=\"node\"\n", + ")\n", "te[grid.y_of_node > 1.5] = 2.5\n", "te[grid.y_of_node < 0.5] = 2.5" ] @@ -285,24 +288,15 @@ "metadata": {}, "outputs": [], "source": [ - "# The simulation may take a long time to run,\n", - "# so we added a progress report\n", - "progress0 = 0\n", - "\n", "# Set displayAnimation to True if you want to see how\n", "# the water moves throughout the channel\n", "displayAnimation = True\n", "displayAnimationFreq = 5\n", "disp = 0\n", "\n", - "for timestep in range(n_timesteps):\n", + "for timestep in trange(n_timesteps):\n", " rfd.run_one_step()\n", "\n", - " progress = int(((timestep + 1) / n_timesteps) * 100)\n", - " if progress > progress0 + 1:\n", - " print(\"\\r\" + f\"Progress: [{progress}%]\", end=\"\")\n", - " progress0 = progress\n", - "\n", " if disp >= displayAnimationFreq:\n", " clear_output(wait=True) # This will clear the previous image\n", " grid.imshow(\"surface_water__depth\")\n", @@ -591,24 +585,15 @@ "metadata": {}, "outputs": [], "source": [ - "# The simulation may take a long time to run,\n", - "# so we added a progress report\n", - "progress0 = 0\n", - "\n", "# Set displayAnimation to True if you want to see how\n", "# the water moves throughout the channel\n", "displayAnimation = True\n", "displayAnimationFreq = 5\n", "disp = 0\n", "\n", - "for timestep in range(n_timesteps):\n", + "for timestep in trange(n_timesteps):\n", " rfd.run_one_step()\n", "\n", - " progress = int(((timestep + 1) / n_timesteps) * 100)\n", - " if progress > progress0 + 1:\n", - " print(\"\\r\" + f\"Progress: [{progress}%]\", end=\"\")\n", - " progress0 = progress\n", - "\n", " if disp >= displayAnimationFreq:\n", " clear_output(wait=True) # This will clear the previous image\n", " grid.imshow(\"surface_water__depth\")\n", From 4e99471d9a7e5e437f4a5f8b25105737fab3af02 Mon Sep 17 00:00:00 2001 From: mcflugen Date: Fri, 18 Oct 2024 13:44:32 -0600 Subject: [PATCH 7/7] clean up plotting within loops --- .../river_flow_dynamics_tutorial.ipynb | 36 ++++++++----------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/docs/source/tutorials/river_flow_dynamics/river_flow_dynamics_tutorial.ipynb b/docs/source/tutorials/river_flow_dynamics/river_flow_dynamics_tutorial.ipynb index 9551ff24e5..c3e8f6094c 100644 --- a/docs/source/tutorials/river_flow_dynamics/river_flow_dynamics_tutorial.ipynb +++ b/docs/source/tutorials/river_flow_dynamics/river_flow_dynamics_tutorial.ipynb @@ -288,22 +288,18 @@ "metadata": {}, "outputs": [], "source": [ - "# Set displayAnimation to True if you want to see how\n", - "# the water moves throughout the channel\n", - "displayAnimation = True\n", - "displayAnimationFreq = 5\n", - "disp = 0\n", + "# Set the animation frequency to n_timesteps if you\n", + "# don't want to plot the water depth\n", + "# display_animation_freq = n_timesteps\n", + "display_animation_freq = 5\n", "\n", + "grid.imshow(\"surface_water__depth\", output=True)\n", "for timestep in trange(n_timesteps):\n", " rfd.run_one_step()\n", "\n", - " if disp >= displayAnimationFreq:\n", + " if timestep % display_animation_freq == 0:\n", " clear_output(wait=True) # This will clear the previous image\n", - " grid.imshow(\"surface_water__depth\")\n", - " plt.show()\n", - " disp = -1\n", - "\n", - " disp += 1" + " grid.imshow(\"surface_water__depth\", output=True)" ] }, { @@ -585,22 +581,18 @@ "metadata": {}, "outputs": [], "source": [ - "# Set displayAnimation to True if you want to see how\n", - "# the water moves throughout the channel\n", - "displayAnimation = True\n", - "displayAnimationFreq = 5\n", - "disp = 0\n", + "# Set the animation frequency to n_timesteps if you\n", + "# don't want to plot the water depth\n", + "# display_animation_freq = n_timesteps\n", + "display_animation_freq = 5\n", "\n", + "grid.imshow(\"surface_water__depth\", output=True)\n", "for timestep in trange(n_timesteps):\n", " rfd.run_one_step()\n", "\n", - " if disp >= displayAnimationFreq:\n", + " if timestep % display_animation_freq == 0:\n", " clear_output(wait=True) # This will clear the previous image\n", - " grid.imshow(\"surface_water__depth\")\n", - " plt.show()\n", - " disp = -1\n", - "\n", - " disp += 1" + " grid.imshow(\"surface_water__depth\", output=True)" ] }, {