Skip to content

Commit

Permalink
Update BMI notebooks (#52)
Browse files Browse the repository at this point in the history
* Use GitHub URLs for image paths

* Copy current ESPIn logo image instead of linking

* Update notebook for revisions to bmi-example-python
  • Loading branch information
mdpiper committed Aug 9, 2021
1 parent b3a9928 commit 470dca7
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 37 deletions.
101 changes: 70 additions & 31 deletions lessons/bmi/bmi-run-model-from-bmi.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"<a href=\"https://bmi.readthedocs.io\"><img src=\"../../media/bmi-logo-header-text.png\"></a>"
"<a href=\"https://bmi.readthedocs.io\"><img src=\"https://raw.githubusercontent.com/csdms/espin/main/media/bmi-logo-header-text.png\"></a>"
]
},
{
Expand Down Expand Up @@ -185,7 +185,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Next, get attributes of the grid on which the temperature variable is defined:"
"Next, get the identifier for the grid on which the temperature variable is defined:"
]
},
{
Expand All @@ -197,37 +197,75 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Grid id: 0\n",
"Grid rank: 2\n",
"Grid shape: (6, 8)\n",
"Grid spacing: [1.0, 1.0]\n",
"Grid type: uniform_rectilinear_grid\n"
"Grid id: 0\n"
]
}
],
"source": [
"grid_id = x.get_var_grid('plate_surface__temperature')\n",
"print('Grid id:', grid_id)\n",
"print('Grid rank:', x.get_grid_rank(grid_id))\n",
"print('Grid shape:', x.get_grid_shape(grid_id))\n",
"print('Grid spacing:', x.get_grid_spacing(grid_id))\n",
"print('Grid type:', x.get_grid_type(grid_id))"
"print('Grid id:', grid_id)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Through the model's BMI, zero out the initial temperature field, except for an impulse:"
"Then get the grid attributes:"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Grid type: uniform_rectilinear\n",
"Grid rank: 2\n",
"Grid shape: [6 8]\n",
"Grid spacing: [1. 1.]\n"
]
}
],
"source": [
"print('Grid type:', x.get_grid_type(grid_id))\n",
"\n",
"rank = x.get_grid_rank(grid_id)\n",
"print('Grid rank:', rank)\n",
"\n",
"shape = np.ndarray(rank, dtype=int)\n",
"x.get_grid_shape(grid_id, shape)\n",
"print('Grid shape:', shape)\n",
"\n",
"spacing = np.ndarray(rank, dtype=float)\n",
"x.get_grid_spacing(grid_id, spacing)\n",
"print('Grid spacing:', spacing)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"These commands are made somewhat un-Pythonic by the generic design of the BMI."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Through the model's BMI, zero out the initial temperature field, except for an impulse near the middle.\n",
"Note that *set_value* expects a one-dimensional array for input."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"temperature = np.zeros(x.get_grid_shape(grid_id))\n",
"temperature = np.zeros(shape)\n",
"temperature[3, 4] = 100.0\n",
"x.set_value('plate_surface__temperature', temperature)"
]
Expand All @@ -236,12 +274,12 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Check that the temperature field has been updated:"
"Check that the temperature field has been updated. Note that *get_value* expects a one-dimensional array to receive output."
]
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 11,
"metadata": {},
"outputs": [
{
Expand All @@ -258,8 +296,9 @@
}
],
"source": [
"initial_temperature = x.get_value('plate_surface__temperature')\n",
"print(initial_temperature)"
"temperature_flat = np.empty_like(temperature).flatten()\n",
"x.get_value('plate_surface__temperature', temperature_flat)\n",
"print(temperature_flat.reshape(shape))"
]
},
{
Expand All @@ -271,7 +310,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -287,7 +326,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 13,
"metadata": {},
"outputs": [
{
Expand All @@ -304,8 +343,8 @@
}
],
"source": [
"updated_temperature = x.get_value('plate_surface__temperature')\n",
"print(updated_temperature)"
"x.get_value('plate_surface__temperature', temperature_flat)\n",
"print(temperature_flat.reshape(shape))"
]
},
{
Expand All @@ -324,7 +363,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -342,7 +381,7 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 15,
"metadata": {},
"outputs": [
{
Expand All @@ -359,9 +398,9 @@
}
],
"source": [
"final_temperature = x.get_value('plate_surface__temperature')\n",
"np.set_printoptions(formatter={'float': '{: 5.1f}'.format})\n",
"print(final_temperature)"
"x.get_value('plate_surface__temperature', temperature_flat)\n",
"print(temperature_flat.reshape(shape))"
]
},
{
Expand All @@ -373,7 +412,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 16,
"metadata": {},
"outputs": [
{
Expand All @@ -385,7 +424,7 @@
}
],
"source": [
"print(final_temperature.sum())"
"print(temperature_flat.sum())"
]
},
{
Expand All @@ -397,7 +436,7 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -407,7 +446,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -421,7 +460,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.2"
"version": "3.9.6"
}
},
"nbformat": 4,
Expand Down
6 changes: 3 additions & 3 deletions lessons/bmi/bmi-run-model.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"<a href=\"https://bmi.readthedocs.io\"><img src=\"../../media/bmi-logo-header-text.png\"></a>"
"<a href=\"https://bmi.readthedocs.io\"><img src=\"https://raw.githubusercontent.com/csdms/espin/main/media/bmi-logo-header-text.png\"></a>"
]
},
{
Expand Down Expand Up @@ -273,7 +273,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -287,7 +287,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.2"
"version": "3.9.6"
}
},
"nbformat": 4,
Expand Down
6 changes: 3 additions & 3 deletions lessons/bmi/index.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"<a href=\"https://csdms.colorado.edu\"><img style=\"float: center; width: 75%\" src=\"../../media/ESPIn.png\"></a>"
"<a href=\"https://csdms.colorado.edu\"><img style=\"float: center; width: 75%\" src=\"https://raw.githubusercontent.com/csdms/espin/main/media/ESPIn.png\"></a>"
]
},
{
Expand Down Expand Up @@ -46,7 +46,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -60,7 +60,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.3"
"version": "3.9.6"
}
},
"nbformat": 4,
Expand Down
1 change: 0 additions & 1 deletion media/ESPIn.png

This file was deleted.

Binary file added media/ESPIn.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 470dca7

Please sign in to comment.