Skip to content

Commit 984762c

Browse files
authored
Toggle solutions in pymt notebooks (#78)
* Add solution cells to notebooks * Delete solutions notebooks
1 parent 862fc8b commit 984762c

11 files changed

+258
-2056
lines changed

lessons/pymt/01_model_setup.ipynb

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@
123123
"cell_type": "code",
124124
"execution_count": null,
125125
"metadata": {
126-
"scrolled": false,
127126
"solution2": "shown"
128127
},
129128
"outputs": [],
@@ -278,7 +277,7 @@
278277
"solution2_first": true
279278
},
280279
"source": [
281-
"## Exercise: Setup a simulation using non-default values\n",
280+
"## Exercise 1: Setup a simulation using non-default values\n",
282281
"\n",
283282
"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."
284283
]
@@ -292,14 +291,39 @@
292291
"# Your code here"
293292
]
294293
},
294+
{
295+
"cell_type": "markdown",
296+
"metadata": {},
297+
"source": [
298+
"<details>\n",
299+
" <summary><b>Click here to see a solution</b></summary>\n",
300+
" \n",
301+
"```python\n",
302+
"from pymt import MODELS\n",
303+
"\n",
304+
"hydrotrend = MODELS.Hydrotrend()\n",
305+
"dict(hydrotrend.parameters)\n",
306+
"\n",
307+
"hydrotrend.setup(\"hydrotrend-sim-0\", hydraulic_conductivity=200.0)\n",
308+
"\n",
309+
"dict(hydrotrend.parameters)[\"hydraulic_conductivity\"]\n",
310+
"\n",
311+
"ls hydrotrend-sim-0\n",
312+
"\n",
313+
"cat hydrotrend-sim-0/HYDRO.IN\n",
314+
"```\n",
315+
" \n",
316+
"</details>"
317+
]
318+
},
295319
{
296320
"cell_type": "markdown",
297321
"metadata": {
298322
"solution2": "shown",
299323
"solution2_first": true
300324
},
301325
"source": [
302-
"## Exercise: As a bonus, set up a series of simulations\n",
326+
"## Exercise 2: As a bonus, set up a series of simulations\n",
303327
"\n",
304328
"Pull parameters from a uniform distribution, which could be part of a sensitivity study."
305329
]
@@ -312,11 +336,39 @@
312336
"source": [
313337
"# Your code here"
314338
]
339+
},
340+
{
341+
"cell_type": "markdown",
342+
"metadata": {},
343+
"source": [
344+
"<details>\n",
345+
" <summary><b>Click here to see a solution</b></summary>\n",
346+
"\n",
347+
"```python\n",
348+
"import numpy as np\n",
349+
"\n",
350+
"hydrotrend = MODELS.Hydrotrend()\n",
351+
"\n",
352+
"values = np.linspace(50., 200.)\n",
353+
"for sim_no, value in enumerate(values):\n",
354+
" hydrotrend.setup(f\"hydrotrend-sim-{sim_no}\", hydraulic_conductivity=value)\n",
355+
" \n",
356+
"cat hydrotrend-sim-12/HYDRO.IN | grep \"hydraulic conductivity\"\n",
357+
"\n",
358+
"print(values[12])\n",
359+
"\n",
360+
"cat hydrotrend-sim-24/HYDRO.IN | grep \"hydraulic conductivity\"\n",
361+
"\n",
362+
"print(values[24])\n",
363+
"```\n",
364+
" \n",
365+
"</details>"
366+
]
315367
}
316368
],
317369
"metadata": {
318370
"kernelspec": {
319-
"display_name": "Python 3",
371+
"display_name": "Python 3 (ipykernel)",
320372
"language": "python",
321373
"name": "python3"
322374
},
@@ -330,7 +382,7 @@
330382
"name": "python",
331383
"nbconvert_exporter": "python",
332384
"pygments_lexer": "ipython3",
333-
"version": "3.9.4"
385+
"version": "3.9.7"
334386
}
335387
},
336388
"nbformat": 4,

lessons/pymt/02_run_a_model.ipynb

Lines changed: 119 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -221,15 +221,13 @@
221221
"solution2_first": true
222222
},
223223
"source": [
224-
"## Exercise 1\n",
225-
"\n",
226-
"### Run for 100 years, calculate 100 year means\n",
224+
"## Exercise 1: Calculate 100 year means\n",
227225
"\n",
228226
"For this case study, we will run a simulation for 100 years at daily time-step.\n",
229227
"This means you run Hydrotrend for 36,500 days total. Later on we will change\n",
230228
"other input parameters but, for now, we'll just stick with the defaults.\n",
231229
"\n",
232-
"Calculate mean water discharge Q, mean suspended load Qs, mean sediment concentration Cs, and mean bedload Qb.\n",
230+
"Calculate mean water discharge $Q$, mean suspended load $Q_s$, mean sediment concentration Cs, and mean bedload $Q_b$.\n",
233231
"\n",
234232
"*Note that all values are reported as daily averages. What are the units?*"
235233
]
@@ -243,14 +241,61 @@
243241
"# Your code here"
244242
]
245243
},
244+
{
245+
"cell_type": "markdown",
246+
"metadata": {},
247+
"source": [
248+
"<details>\n",
249+
" <summary><b>Click here to see a solution</b></summary>\n",
250+
"\n",
251+
"```python\n",
252+
"from pymt import MODELS\n",
253+
"\n",
254+
"hydrotrend = MODELS.Hydrotrend()\n",
255+
"config_file, config_dir = hydrotrend.setup(\"hydrotrend-default\", run_duration=100)\n",
256+
"\n",
257+
"hydrotrend.initialize(config_file, dir=config_dir)\n",
258+
"\n",
259+
"n_steps = int(hydrotrend.end_time / hydrotrend.time_step)\n",
260+
"\n",
261+
"q = np.empty(n_steps)\n",
262+
"qs = np.empty(n_steps)\n",
263+
"cs = np.empty(n_steps)\n",
264+
"qb = np.empty(n_steps)\n",
265+
"\n",
266+
"for i in tqdm(range(n_steps)):\n",
267+
" hydrotrend.update()\n",
268+
" \n",
269+
" q[i] = hydrotrend.get_value(\"channel_exit_water__volume_flow_rate\")\n",
270+
" qs[i] = hydrotrend.get_value(\"channel_exit_water_sediment~suspended__mass_flow_rate\")\n",
271+
" cs[i] = hydrotrend.get_value(\"channel_exit_water_sediment~suspended__mass_concentration\")\n",
272+
" qb[i] = hydrotrend.get_value(\"channel_exit_water_sediment~bedload__mass_flow_rate\")\n",
273+
"\n",
274+
"(\n",
275+
" (q.mean(), hydrotrend.var_units(\"channel_exit_water__volume_flow_rate\")),\n",
276+
" (cs.mean(), hydrotrend.var_units(\"channel_exit_water_sediment~suspended__mass_flow_rate\")),\n",
277+
" (qs.mean(), hydrotrend.var_units(\"channel_exit_water_sediment~suspended__mass_concentration\")),\n",
278+
" (qb.mean(), hydrotrend.var_units(\"channel_exit_water_sediment~bedload__mass_flow_rate\"))\n",
279+
")\n",
280+
"\n",
281+
"hydrotrend.var_units(\"channel_exit_water__volume_flow_rate\")\n",
282+
"\n",
283+
"print(f\"River discharge: {q.mean()} m3 / s\")\n",
284+
"print(f\"Suspended load: {qs.mean()} kg / s\")\n",
285+
"print(f\"bedload: {qb.mean()} kg / s\")\n",
286+
"```\n",
287+
"\n",
288+
"</details>"
289+
]
290+
},
246291
{
247292
"cell_type": "markdown",
248293
"metadata": {
249294
"solution2": "shown",
250295
"solution2_first": true
251296
},
252297
"source": [
253-
"**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"
298+
"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"
254299
]
255300
},
256301
{
@@ -262,18 +307,36 @@
262307
"# Your code here"
263308
]
264309
},
310+
{
311+
"cell_type": "markdown",
312+
"metadata": {},
313+
"source": [
314+
"<details>\n",
315+
" <summary><b>Click here to see a solution</b></summary>\n",
316+
"\n",
317+
"```python\n",
318+
"flood_day = q.argmax()\n",
319+
"flood_year = flood_day // 365\n",
320+
"plt.plot(q[flood_year * 365: (flood_year + 1) * 365])\n",
321+
"\n",
322+
"print(f\"Year of max flood: {flood_year}\")\n",
323+
"print(f\"Day of year of max flood: {flood_day % 365}\")\n",
324+
"```\n",
325+
"\n",
326+
"</details>"
327+
]
328+
},
265329
{
266330
"cell_type": "markdown",
267331
"metadata": {
268332
"solution2": "shown",
269333
"solution2_first": true
270334
},
271335
"source": [
272-
"## Exercise 2\n",
273-
"\n",
274-
"### How does a river system respond to climate change? A few simple scenarios for the coming century.\n",
336+
"## Exercise 2: How does a river system respond to climate change?\n",
337+
"### A few simple scenarios for the coming century.\n",
275338
"\n",
276-
"#### 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?"
339+
"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?"
277340
]
278341
},
279342
{
@@ -284,11 +347,56 @@
284347
"source": [
285348
"# Your code here"
286349
]
350+
},
351+
{
352+
"cell_type": "markdown",
353+
"metadata": {},
354+
"source": [
355+
"<details>\n",
356+
" <summary><b>Click here to see a solution</b></summary>\n",
357+
"\n",
358+
"```python\n",
359+
"from pymt import MODELS\n",
360+
"\n",
361+
"hydrotrend = MODELS.Hydrotrend()\n",
362+
"\n",
363+
"config_file, config_folder = hydrotrend.setup(run_duration=50, change_in_mean_annual_temperature=4.0 / 50.0)\n",
364+
"hydrotrend.initialize(config_file, config_folder)\n",
365+
"\n",
366+
"n_steps = int(hydrotrend.end_time / hydrotrend.time_step)\n",
367+
"\n",
368+
"q = np.empty(n_steps)\n",
369+
"qs = np.empty(n_steps)\n",
370+
"cs = np.empty(n_steps)\n",
371+
"qb = np.empty(n_steps)\n",
372+
"\n",
373+
"for i in tqdm(range(n_steps)):\n",
374+
" hydrotrend.update()\n",
375+
" \n",
376+
" q[i] = hydrotrend.get_value(\"channel_exit_water__volume_flow_rate\")\n",
377+
" qs[i] = hydrotrend.get_value(\"channel_exit_water_sediment~suspended__mass_flow_rate\")\n",
378+
" cs[i] = hydrotrend.get_value(\"channel_exit_water_sediment~suspended__mass_concentration\")\n",
379+
" qb[i] = hydrotrend.get_value(\"channel_exit_water_sediment~bedload__mass_flow_rate\")\n",
380+
" \n",
381+
"(\n",
382+
" (q.mean(), hydrotrend.var_units(\"channel_exit_water__volume_flow_rate\")),\n",
383+
" (qs.mean(), hydrotrend.var_units(\"channel_exit_water_sediment~suspended__mass_flow_rate\")),\n",
384+
" (cs.mean(), hydrotrend.var_units(\"channel_exit_water_sediment~suspended__mass_concentration\")),\n",
385+
" (qb.mean(), hydrotrend.var_units(\"channel_exit_water_sediment~bedload__mass_flow_rate\"))\n",
386+
")\n",
387+
"\n",
388+
"print(f\"River discharge: {q.mean()} m3 / s\")\n",
389+
"print(f\"Suspended load: {qs.mean()} kg / s\")\n",
390+
"print(f\"bedload: {qb.mean()} kg / s\")\n",
391+
"```\n",
392+
"\n",
393+
"</details>"
394+
]
287395
}
288396
],
289397
"metadata": {
290398
"kernelspec": {
291-
"display_name": "Python 3",
399+
"display_name": "Python 3 (ipykernel)",
292400
"language": "python",
293401
"name": "python3"
294402
},
@@ -302,7 +410,7 @@
302410
"name": "python",
303411
"nbconvert_exporter": "python",
304412
"pygments_lexer": "ipython3",
305-
"version": "3.9.4"
413+
"version": "3.9.7"
306414
}
307415
},
308416
"nbformat": 4,

lessons/pymt/03_unit_conversion.ipynb

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,24 @@
155155
"# Your code here"
156156
]
157157
},
158+
{
159+
"cell_type": "markdown",
160+
"metadata": {},
161+
"source": [
162+
"<details>\n",
163+
" <summary><b>Click here to see a solution</b></summary>\n",
164+
"\n",
165+
"```python\n",
166+
"meters = unit_system.Unit(\"m\")\n",
167+
"feet = unit_system.Unit(\"feet\")\n",
168+
"\n",
169+
"feet_to_meters = feet.to(meters)\n",
170+
"feet_to_meters(1.0)\n",
171+
"```\n",
172+
"\n",
173+
"</details>"
174+
]
175+
},
158176
{
159177
"cell_type": "markdown",
160178
"metadata": {},
@@ -215,11 +233,25 @@
215233
"source": [
216234
"# Your code here"
217235
]
236+
},
237+
{
238+
"cell_type": "markdown",
239+
"metadata": {},
240+
"source": [
241+
"<details>\n",
242+
" <summary><b>Click here to see a solution</b></summary>\n",
243+
"\n",
244+
"```python\n",
245+
"hydrotrend.get_value(\"channel_exit_water__volume_flow_rate\", units=\"acre.feet hr-1\")\n",
246+
"```\n",
247+
"\n",
248+
"</details>"
249+
]
218250
}
219251
],
220252
"metadata": {
221253
"kernelspec": {
222-
"display_name": "Python 3",
254+
"display_name": "Python 3 (ipykernel)",
223255
"language": "python",
224256
"name": "python3"
225257
},
@@ -233,7 +265,7 @@
233265
"name": "python",
234266
"nbconvert_exporter": "python",
235267
"pygments_lexer": "ipython3",
236-
"version": "3.9.4"
268+
"version": "3.9.7"
237269
}
238270
},
239271
"nbformat": 4,

0 commit comments

Comments
 (0)