Skip to content

Commit

Permalink
Finish up
Browse files Browse the repository at this point in the history
  • Loading branch information
visr committed Sep 19, 2024
1 parent e019f62 commit b15cfb5
Show file tree
Hide file tree
Showing 5 changed files with 249 additions and 399 deletions.
1 change: 0 additions & 1 deletion docs/_quarto.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ website:
- tutorial/natural-flow.ipynb
- tutorial/irrigation-demand.ipynb
- tutorial/reservoir.ipynb
- tutorial/public-water-supply.ipynb

- title: "How-to guides"
contents:
Expand Down
82 changes: 23 additions & 59 deletions docs/tutorial/irrigation-demand.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"metadata": {},
"outputs": [],
"source": [
"import subprocess # For running the model\n",
"from pathlib import Path\n",
"\n",
"import matplotlib.pyplot as plt\n",
Expand Down Expand Up @@ -178,7 +177,7 @@
"outputs": [],
"source": [
"irrigation = model.user_demand.add(\n",
" Node(7, Point(-1.5, 1.0), name=\"irrigation\"),\n",
" Node(7, Point(-1.5, 0.5), name=\"irrigation\"),\n",
" [\n",
" user_demand.Time(\n",
" demand=[0.0, 0.0, 10, 12, 12, 0.0],\n",
Expand Down Expand Up @@ -237,14 +236,14 @@
"metadata": {},
"outputs": [],
"source": [
"model.edge.add(main, diversion_basin)\n",
"model.edge.add(minor, confluence)\n",
"model.edge.add(diversion_basin, irrigation)\n",
"model.edge.add(main, diversion_basin, name=\"main\")\n",
"model.edge.add(minor, confluence, name=\"minor\")\n",
"model.edge.add(diversion_basin, irrigation, name=\"irrigation\")\n",
"model.edge.add(irrigation, confluence)\n",
"model.edge.add(diversion_basin, diversion_weir)\n",
"model.edge.add(diversion_basin, diversion_weir, name=\"not diverted\")\n",
"model.edge.add(diversion_weir, confluence)\n",
"model.edge.add(confluence, weir)\n",
"model.edge.add(weir, sea)"
"model.edge.add(weir, sea, name=\"sea\")"
]
},
{
Expand All @@ -253,7 +252,7 @@
"metadata": {},
"outputs": [],
"source": [
"toml_path = base_dir / \"Crystal_1.1/ribasim.toml\"\n",
"toml_path = base_dir / \"Crystal-2/ribasim.toml\"\n",
"model.write(toml_path)\n",
"cli_path = \"ribasim\""
]
Expand All @@ -264,7 +263,7 @@
"source": [
"### Plot model and run\n",
"Plot the schematization and run the model.\n",
"This time the new outputs should be written in a new folder called `Crystal_1.2`:"
"This time the new outputs should be written in a new folder called `Crystal-2`:"
]
},
{
Expand Down Expand Up @@ -296,21 +295,6 @@
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"model.plot()\n",
"\n",
"toml_path = base_dir / \"Crystal_1.2/ribasim.toml\"\n",
"model.write(toml_path)\n",
"cli_path = \"ribasim\"\n",
"\n",
"subprocess.run([cli_path, toml_path], check=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -325,25 +309,22 @@
"metadata": {},
"outputs": [],
"source": [
"df_basin = pd.read_feather(base_dir / \"Crystal_1.1/results/basin.arrow\")\n",
"df_basin = pd.read_feather(base_dir / \"Crystal-2/results/basin.arrow\")\n",
"\n",
"# Create pivot tables and plot for basin data\n",
"df_basin_wide = df_basin.pivot_table(\n",
" index=\"time\", columns=\"node_id\", values=[\"storage\", \"level\"]\n",
")\n",
"\n",
"# Skip the first timestep as it is the initialization step\n",
"df_basin_wide = df_basin_wide.iloc[1:]\n",
"\n",
"df_basin_div = df_basin_wide.xs(\"Div\", axis=1, level=1, drop_level=False)\n",
"df_basin_conf = df_basin_wide.xs(\"Conf\", axis=1, level=1, drop_level=False)\n",
"df_basin_div = df_basin_wide.loc[:, pd.IndexSlice[:, diversion_basin.node_id]]\n",
"df_basin_conf = df_basin_wide.loc[:, pd.IndexSlice[:, confluence.node_id]]\n",
"\n",
"\n",
"def plot_basin_data(\n",
" ax, ax_twin, df_basin, level_color=\"b\", storage_color=\"r\", title=\"Basin\"\n",
"):\n",
" # Plot level data\n",
" for idx, column in enumerate(df_basin[\"level\"].columns):\n",
" for column in df_basin[\"level\"].columns:\n",
" ax.plot(\n",
" df_basin.index,\n",
" df_basin[\"level\"][column],\n",
Expand All @@ -353,7 +334,7 @@
" )\n",
"\n",
" # Plot storage data\n",
" for idx, column in enumerate(df_basin[\"storage\"].columns):\n",
" for column in df_basin[\"storage\"].columns:\n",
" ax_twin.plot(\n",
" df_basin.index,\n",
" df_basin[\"storage\"][column],\n",
Expand Down Expand Up @@ -381,11 +362,11 @@
"\n",
"# Plot Div basin data\n",
"ax2 = ax1.twinx() # Secondary y-axis for storage\n",
"plot_basin_data(ax1, ax2, df_basin_div, title=\"Div Basin Level and Storage over Time\")\n",
"plot_basin_data(ax1, ax2, df_basin_div, title=\"Diversion Basin level and storage\")\n",
"\n",
"# Plot Conf basin data\n",
"ax4 = ax3.twinx() # Secondary y-axis for storage\n",
"plot_basin_data(ax3, ax4, df_basin_conf, title=\"Conf Basin Level and Storage over Time\")\n",
"plot_basin_data(ax3, ax4, df_basin_conf, title=\"Confluence Basin level and storage\")\n",
"\n",
"# Common X label\n",
"ax3.set_xlabel(\"Time\")\n",
Expand All @@ -397,14 +378,11 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"@fig-sim3 illustrates the water levels and storage capacities for each basin.\n",
"At the diverted section, where the profile is narrower than at the confluence, we anticipate lower storage and water levels compared to the confluence section.\n",
"The figure above illustrates the water levels and storage capacities for each Basin.\n",
"\n",
"When compared to the natural flow conditions, where no water is abstracted for irrigation (See Crystal 1.1), there is a noticeable decrease in both storage and water levels at the confluence downstream.\n",
"When compared to the natural flow conditions, where no water is abstracted for irrigation (See Crystal 1), there is a noticeable decrease in both storage and water levels at the confluence downstream.\n",
"This reduction is attributed to the irrigation demand upstream with no return flow, which decreases the amount of available water in the main river, resulting in lower water levels at the confluence.\n",
"\n",
"![Simulated basin levels and storages](https://s3.deltares.nl/ribasim/doc-image/quickstart/Simulated-basin-levels-and-storages.png){fig-align=\"left\" #fig-sim3}\n",
"\n",
"### Plot and compare the flow results\n",
"Plot the flow results in an interactive plotting tool."
]
Expand All @@ -415,40 +393,26 @@
"metadata": {},
"outputs": [],
"source": [
"df_flow = pd.read_feather(base_dir / \"Crystal_1.2/results/flow.arrow\")\n",
"df_flow[\"edge\"] = list(zip(df_flow.from_node_id, df_flow.to_node_id))\n",
"df_flow[\"name\"] = df_flow[\"edge\"].map(edge_names)\n",
"df_flow = pd.read_feather(base_dir / \"Crystal-2/results/flow.arrow\")\n",
"# Add the edge names and then remove unnamed edges\n",
"df_flow[\"name\"] = model.edge.df[\"name\"].loc[df_flow[\"edge_id\"]].to_numpy()\n",
"df_flow = df_flow[df_flow[\"name\"].astype(bool)]\n",
"\n",
"# Plot the flow data, interactive plot with Plotly\n",
"pivot_flow = df_flow.pivot_table(\n",
" index=\"time\", columns=\"name\", values=\"flow_rate\"\n",
").reset_index()\n",
"fig = px.line(\n",
" pivot_flow, x=\"time\", y=pivot_flow.columns[1:], title=\"Flow Over Time [m3/s]\"\n",
")\n",
"fig = px.line(pivot_flow, x=\"time\", y=pivot_flow.columns[1:], title=\"Flow [m3/s]\")\n",
"\n",
"fig.update_layout(legend_title_text=\"Edge\")\n",
"fig.write_html(base_dir / \"Crystal_1.2/plot_edges.html\")\n",
"fig.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The plot will be saved as an HTML file, which can be viewed by dragging the file into an internet browser (@fig-sim4).\n",
"\n",
"![Simulated flows of each edge](https://s3.deltares.nl/ribasim/doc-image/quickstart/Simulated-flows-of-each-edge.png){fig-align=\"left\" #fig-sim4}\n",
"\n",
"When selecting only the flow demanded by the User Demand node, or in other words the supply for irrigation increases at times when it is required (@fig-sim5) and the return flow remains zero, as the assumption defined before was that there is no drain.\n",
"\n",
"![Supplied irrigation and return flow](https://s3.deltares.nl/ribasim/doc-image/quickstart/Supplied-irrigation-and-return-flow.png){fig-align=\"left\" #fig-sim5}\n",
"\n",
"@fig-sim6 shows the flow to the ocean (Terminal).\n",
"Compared to Crystal 1.1 the flow has decreased during the irrigated period.\n",
"Indicating the impact of irrigation without any drain.\n",
"\n",
"![Simulated flow to Terminal](https://s3.deltares.nl/ribasim/doc-image/quickstart/Simulated-flow-to-Terminal.png){fig-align=\"left\" #fig-sim6}"
"Try toggling the edges on and off by clicking on them in the edges."
]
}
],
Expand Down
35 changes: 15 additions & 20 deletions docs/tutorial/natural-flow.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -294,10 +294,10 @@
"metadata": {},
"outputs": [],
"source": [
"model.edge.add(main, confluence)\n",
"model.edge.add(minor, confluence)\n",
"model.edge.add(main, confluence, name=\"main\")\n",
"model.edge.add(minor, confluence, name=\"minor\")\n",
"model.edge.add(confluence, weir)\n",
"model.edge.add(weir, sea)"
"model.edge.add(weir, sea, name=\"sea\")"
]
},
{
Expand All @@ -322,7 +322,7 @@
"metadata": {},
"source": [
"Write the model configuration to the `TOML` file.\n",
"Name the output file `Crystal_1.1/ribasim.toml`:"
"Name the output file `Crystal-1/ribasim.toml`:"
]
},
{
Expand All @@ -331,7 +331,7 @@
"metadata": {},
"outputs": [],
"source": [
"toml_path = base_dir / \"Crystal_1.1/ribasim.toml\"\n",
"toml_path = base_dir / \"Crystal-1/ribasim.toml\"\n",
"model.write(toml_path)\n",
"cli_path = \"ribasim\""
]
Expand All @@ -340,7 +340,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"After running `model.write` a subfolder `Crystal_1.1` is created, which contains the model input data and configuration:\n",
"After running `model.write` a subfolder `Crystal-1` is created, which contains the model input data and configuration:\n",
"\n",
"- ribasim.toml: The model configuration\n",
"- database.gpkg: A GeoPackage containing the network geometry and input data of the nodes used."
Expand Down Expand Up @@ -373,7 +373,7 @@
"Now run the model. You can open a terminal and run it from there. For example:\n",
"\n",
"```bash\n",
"ribasim Crystal_1.1/ribasim.toml\n",
"ribasim Crystal-1/ribasim.toml\n",
"```\n",
"\n",
"From Python you can run it with:\n",
Expand Down Expand Up @@ -404,16 +404,13 @@
"metadata": {},
"outputs": [],
"source": [
"df_basin = pd.read_feather(base_dir / \"Crystal_1.1/results/basin.arrow\")\n",
"df_basin = pd.read_feather(base_dir / \"Crystal-1/results/basin.arrow\")\n",
"\n",
"# Create pivot tables and plot for Basin data\n",
"df_basin_wide = df_basin.pivot_table(\n",
" index=\"time\", columns=\"node_id\", values=[\"storage\", \"level\"]\n",
")\n",
"\n",
"# Skip the first timestep as it is the initialization step\n",
"df_basin_wide = df_basin_wide.iloc[1:]\n",
"\n",
"# Plot level and storage on the same graph with dual y-axes\n",
"fig, ax1 = plt.subplots(figsize=(12, 6))\n",
"\n",
Expand All @@ -432,7 +429,7 @@
"ax2.tick_params(axis=\"y\", labelcolor=color)\n",
"\n",
"fig.tight_layout() # Adjust layout to fit labels\n",
"plt.title(\"Basin Level and Storage Over Time\")\n",
"plt.title(\"Basin level and storage\")\n",
"plt.show()"
]
},
Expand All @@ -456,15 +453,13 @@
"source": [
"# Plot flow data\n",
"# Read the flow results\n",
"df_flow = pd.read_feather(base_dir / \"Crystal_1.1/results/flow.arrow\")\n",
"# Create 'edge' and 'flow_m3d' columns\n",
"df_flow[\"edge\"] = list(zip(df_flow.from_node_id, df_flow.to_node_id))\n",
"df_flow = pd.read_feather(base_dir / \"Crystal-1/results/flow.arrow\")\n",
"# Add the edge names and then remove unnamed edges\n",
"df_flow[\"name\"] = model.edge.df[\"name\"].loc[df_flow[\"edge_id\"]].to_numpy()\n",
"df_flow = df_flow[df_flow[\"name\"].astype(bool)]\n",
"\n",
"# Create a pivot table\n",
"pivot_flow = df_flow.pivot_table(index=\"time\", columns=\"edge\", values=\"flow_rate\")\n",
"\n",
"# Skip the first timestep\n",
"pivot_flow = pivot_flow.iloc[1:]\n",
"pivot_flow = df_flow.pivot_table(index=\"time\", columns=\"name\", values=\"flow_rate\")\n",
"\n",
"line_styles = [\"-\", \"--\", \"-\", \"-.\"]\n",
"num_styles = len(line_styles)\n",
Expand All @@ -479,7 +474,7 @@
"ax.set_xlabel(\"Time\")\n",
"ax.set_ylabel(\"Flow [m³/s]\")\n",
"ax.legend(bbox_to_anchor=(1.15, 1), title=\"Edge\")\n",
"plt.title(\"Flow Over Time\")\n",
"plt.title(\"Flow\")\n",
"plt.grid(True)\n",
"plt.show()"
]
Expand Down
Loading

0 comments on commit b15cfb5

Please sign in to comment.