Skip to content

Commit

Permalink
Toggle solutions in pymt notebooks (#78)
Browse files Browse the repository at this point in the history
* Add solution cells to notebooks

* Delete solutions notebooks
  • Loading branch information
mdpiper committed Jun 30, 2022
1 parent 862fc8b commit 984762c
Show file tree
Hide file tree
Showing 11 changed files with 258 additions and 2,056 deletions.
62 changes: 57 additions & 5 deletions lessons/pymt/01_model_setup.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false,
"solution2": "shown"
},
"outputs": [],
Expand Down Expand Up @@ -278,7 +277,7 @@
"solution2_first": true
},
"source": [
"## Exercise: Setup a simulation using non-default values\n",
"## Exercise 1: Setup a simulation using non-default values\n",
"\n",
"Use a model's `setup` method to create a set of input files using non-default values and verify the input files were created correctly."
]
Expand All @@ -292,14 +291,39 @@
"# Your code here"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<details>\n",
" <summary><b>Click here to see a solution</b></summary>\n",
" \n",
"```python\n",
"from pymt import MODELS\n",
"\n",
"hydrotrend = MODELS.Hydrotrend()\n",
"dict(hydrotrend.parameters)\n",
"\n",
"hydrotrend.setup(\"hydrotrend-sim-0\", hydraulic_conductivity=200.0)\n",
"\n",
"dict(hydrotrend.parameters)[\"hydraulic_conductivity\"]\n",
"\n",
"ls hydrotrend-sim-0\n",
"\n",
"cat hydrotrend-sim-0/HYDRO.IN\n",
"```\n",
" \n",
"</details>"
]
},
{
"cell_type": "markdown",
"metadata": {
"solution2": "shown",
"solution2_first": true
},
"source": [
"## Exercise: As a bonus, set up a series of simulations\n",
"## Exercise 2: As a bonus, set up a series of simulations\n",
"\n",
"Pull parameters from a uniform distribution, which could be part of a sensitivity study."
]
Expand All @@ -312,11 +336,39 @@
"source": [
"# Your code here"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<details>\n",
" <summary><b>Click here to see a solution</b></summary>\n",
"\n",
"```python\n",
"import numpy as np\n",
"\n",
"hydrotrend = MODELS.Hydrotrend()\n",
"\n",
"values = np.linspace(50., 200.)\n",
"for sim_no, value in enumerate(values):\n",
" hydrotrend.setup(f\"hydrotrend-sim-{sim_no}\", hydraulic_conductivity=value)\n",
" \n",
"cat hydrotrend-sim-12/HYDRO.IN | grep \"hydraulic conductivity\"\n",
"\n",
"print(values[12])\n",
"\n",
"cat hydrotrend-sim-24/HYDRO.IN | grep \"hydraulic conductivity\"\n",
"\n",
"print(values[24])\n",
"```\n",
" \n",
"</details>"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -330,7 +382,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.4"
"version": "3.9.7"
}
},
"nbformat": 4,
Expand Down
130 changes: 119 additions & 11 deletions lessons/pymt/02_run_a_model.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -221,15 +221,13 @@
"solution2_first": true
},
"source": [
"## Exercise 1\n",
"\n",
"### Run for 100 years, calculate 100 year means\n",
"## Exercise 1: Calculate 100 year means\n",
"\n",
"For this case study, we will run a simulation for 100 years at daily time-step.\n",
"This means you run Hydrotrend for 36,500 days total. Later on we will change\n",
"other input parameters but, for now, we'll just stick with the defaults.\n",
"\n",
"Calculate mean water discharge Q, mean suspended load Qs, mean sediment concentration Cs, and mean bedload Qb.\n",
"Calculate mean water discharge $Q$, mean suspended load $Q_s$, mean sediment concentration Cs, and mean bedload $Q_b$.\n",
"\n",
"*Note that all values are reported as daily averages. What are the units?*"
]
Expand All @@ -243,14 +241,61 @@
"# Your code here"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<details>\n",
" <summary><b>Click here to see a solution</b></summary>\n",
"\n",
"```python\n",
"from pymt import MODELS\n",
"\n",
"hydrotrend = MODELS.Hydrotrend()\n",
"config_file, config_dir = hydrotrend.setup(\"hydrotrend-default\", run_duration=100)\n",
"\n",
"hydrotrend.initialize(config_file, dir=config_dir)\n",
"\n",
"n_steps = int(hydrotrend.end_time / hydrotrend.time_step)\n",
"\n",
"q = np.empty(n_steps)\n",
"qs = np.empty(n_steps)\n",
"cs = np.empty(n_steps)\n",
"qb = np.empty(n_steps)\n",
"\n",
"for i in tqdm(range(n_steps)):\n",
" hydrotrend.update()\n",
" \n",
" q[i] = hydrotrend.get_value(\"channel_exit_water__volume_flow_rate\")\n",
" qs[i] = hydrotrend.get_value(\"channel_exit_water_sediment~suspended__mass_flow_rate\")\n",
" cs[i] = hydrotrend.get_value(\"channel_exit_water_sediment~suspended__mass_concentration\")\n",
" qb[i] = hydrotrend.get_value(\"channel_exit_water_sediment~bedload__mass_flow_rate\")\n",
"\n",
"(\n",
" (q.mean(), hydrotrend.var_units(\"channel_exit_water__volume_flow_rate\")),\n",
" (cs.mean(), hydrotrend.var_units(\"channel_exit_water_sediment~suspended__mass_flow_rate\")),\n",
" (qs.mean(), hydrotrend.var_units(\"channel_exit_water_sediment~suspended__mass_concentration\")),\n",
" (qb.mean(), hydrotrend.var_units(\"channel_exit_water_sediment~bedload__mass_flow_rate\"))\n",
")\n",
"\n",
"hydrotrend.var_units(\"channel_exit_water__volume_flow_rate\")\n",
"\n",
"print(f\"River discharge: {q.mean()} m3 / s\")\n",
"print(f\"Suspended load: {qs.mean()} kg / s\")\n",
"print(f\"bedload: {qb.mean()} kg / s\")\n",
"```\n",
"\n",
"</details>"
]
},
{
"cell_type": "markdown",
"metadata": {
"solution2": "shown",
"solution2_first": true
},
"source": [
"**Identify the highest flood event for this simulation. Is this the 50-year flood? Plot the year of Q-data which includes the flood.**\n"
"Identify the highest flood event for this simulation. Is this the 50-year flood? Plot the year of Q-data which includes the flood.\n"
]
},
{
Expand All @@ -262,18 +307,36 @@
"# Your code here"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<details>\n",
" <summary><b>Click here to see a solution</b></summary>\n",
"\n",
"```python\n",
"flood_day = q.argmax()\n",
"flood_year = flood_day // 365\n",
"plt.plot(q[flood_year * 365: (flood_year + 1) * 365])\n",
"\n",
"print(f\"Year of max flood: {flood_year}\")\n",
"print(f\"Day of year of max flood: {flood_day % 365}\")\n",
"```\n",
"\n",
"</details>"
]
},
{
"cell_type": "markdown",
"metadata": {
"solution2": "shown",
"solution2_first": true
},
"source": [
"## Exercise 2\n",
"\n",
"### How does a river system respond to climate change? A few simple scenarios for the coming century.\n",
"## Exercise 2: How does a river system respond to climate change?\n",
"### A few simple scenarios for the coming century.\n",
"\n",
"#### What happens to river discharge, suspended load and bedload if the mean annual temperature in this specific river basin increases by 4 °C over the next 50 years?"
"What happens to river discharge, suspended load and bedload if the mean annual temperature in this specific river basin increases by 4 °C over the next 50 years?"
]
},
{
Expand All @@ -284,11 +347,56 @@
"source": [
"# Your code here"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<details>\n",
" <summary><b>Click here to see a solution</b></summary>\n",
"\n",
"```python\n",
"from pymt import MODELS\n",
"\n",
"hydrotrend = MODELS.Hydrotrend()\n",
"\n",
"config_file, config_folder = hydrotrend.setup(run_duration=50, change_in_mean_annual_temperature=4.0 / 50.0)\n",
"hydrotrend.initialize(config_file, config_folder)\n",
"\n",
"n_steps = int(hydrotrend.end_time / hydrotrend.time_step)\n",
"\n",
"q = np.empty(n_steps)\n",
"qs = np.empty(n_steps)\n",
"cs = np.empty(n_steps)\n",
"qb = np.empty(n_steps)\n",
"\n",
"for i in tqdm(range(n_steps)):\n",
" hydrotrend.update()\n",
" \n",
" q[i] = hydrotrend.get_value(\"channel_exit_water__volume_flow_rate\")\n",
" qs[i] = hydrotrend.get_value(\"channel_exit_water_sediment~suspended__mass_flow_rate\")\n",
" cs[i] = hydrotrend.get_value(\"channel_exit_water_sediment~suspended__mass_concentration\")\n",
" qb[i] = hydrotrend.get_value(\"channel_exit_water_sediment~bedload__mass_flow_rate\")\n",
" \n",
"(\n",
" (q.mean(), hydrotrend.var_units(\"channel_exit_water__volume_flow_rate\")),\n",
" (qs.mean(), hydrotrend.var_units(\"channel_exit_water_sediment~suspended__mass_flow_rate\")),\n",
" (cs.mean(), hydrotrend.var_units(\"channel_exit_water_sediment~suspended__mass_concentration\")),\n",
" (qb.mean(), hydrotrend.var_units(\"channel_exit_water_sediment~bedload__mass_flow_rate\"))\n",
")\n",
"\n",
"print(f\"River discharge: {q.mean()} m3 / s\")\n",
"print(f\"Suspended load: {qs.mean()} kg / s\")\n",
"print(f\"bedload: {qb.mean()} kg / s\")\n",
"```\n",
"\n",
"</details>"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -302,7 +410,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.4"
"version": "3.9.7"
}
},
"nbformat": 4,
Expand Down
36 changes: 34 additions & 2 deletions lessons/pymt/03_unit_conversion.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,24 @@
"# Your code here"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<details>\n",
" <summary><b>Click here to see a solution</b></summary>\n",
"\n",
"```python\n",
"meters = unit_system.Unit(\"m\")\n",
"feet = unit_system.Unit(\"feet\")\n",
"\n",
"feet_to_meters = feet.to(meters)\n",
"feet_to_meters(1.0)\n",
"```\n",
"\n",
"</details>"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -215,11 +233,25 @@
"source": [
"# Your code here"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<details>\n",
" <summary><b>Click here to see a solution</b></summary>\n",
"\n",
"```python\n",
"hydrotrend.get_value(\"channel_exit_water__volume_flow_rate\", units=\"acre.feet hr-1\")\n",
"```\n",
"\n",
"</details>"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -233,7 +265,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.4"
"version": "3.9.7"
}
},
"nbformat": 4,
Expand Down
Loading

0 comments on commit 984762c

Please sign in to comment.