-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a HOWTO notebook depicting how to manage units in PySDM (#1443)
Co-authored-by: Agnieszka Żaba <[email protected]> Co-authored-by: Sylwester Arabas <[email protected]>
- Loading branch information
1 parent
b3a18cf
commit e6e8aab
Showing
5 changed files
with
186 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
175 changes: 175 additions & 0 deletions
175
examples/PySDM_examples/_HOWTOs/dimensional_analysis.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "c1925b9647eb39e9", | ||
"metadata": {}, | ||
"source": [ | ||
"[![preview notebook](https://img.shields.io/static/v1?label=render%20on&logo=github&color=87ce3e&message=GitHub)](https://github.com/open-atmos/PySDM/blob/main/examples/PySDM_examples/howtos/units.ipynb)\n", | ||
"[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/open-atmos/PySDM.git/main?urlpath=lab/tree/examples/PySDM_examples/howtos/units.ipynb)\n", | ||
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/open-atmos/PySDM/blob/main/examples/PySDM_examples/howtos/units.ipynb)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "57a5c59c4cb8a1ec", | ||
"metadata": {}, | ||
"source": [ | ||
"### PySDM dimensional analysis HOWTO\n", | ||
"- PySDM depends on the [Pint](https://pint.readthedocs.io/en/stable/) package which offers dimensional analysis (physical units checks) of Python code \n", | ||
"- this improves code readibility with expressions such as `p = 1000 * si.hPa`\n", | ||
"- using [Pint](https://pint.readthedocs.io/en/stable/), `si` is an instance of [`pint.UnitRegistry`](https://pint.readthedocs.io/en/stable/api/base.html#pint.UnitRegistry) \n", | ||
"- however, for performance reasons, by default PySDM uses a custom drop-in-replacement [`FakeUnitRegistry`](https://open-atmos.github.io/PySDM/PySDM/physics/impl/fake_unit_registry.html#FakeUnitRegistry)\n", | ||
"- this way, we keep the readibility advantage, while not incurring any performance overhead\n", | ||
"- moreover, this makes the code potentially Numba JIT-compilable!\n", | ||
"- we also provide a way to leverage the dimensional analysis benefit for testing purposes\n", | ||
"- to this end, the test code can use the [`DimensionalAnalysis`](https://open-atmos.github.io/PySDM/PySDM/physics/dimensional_analysis.html#DimensionalAnalysis) context manager\n", | ||
"- code below demonstrate how a single unit-equipped function can be used with and without unit checks " | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"id": "97dab670c5cf8d8b", | ||
"metadata": { | ||
"ExecuteTime": { | ||
"end_time": "2024-12-04T13:27:07.009798Z", | ||
"start_time": "2024-12-04T13:27:07.003077Z" | ||
} | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"import sys\n", | ||
"if 'google.colab' in sys.modules:\n", | ||
" !pip --quiet install open-atmos-jupyter-utils\n", | ||
" from open_atmos_jupyter_utils import pip_install_on_colab\n", | ||
" pip_install_on_colab('PySDM-examples')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "19a8f1dc-a5ea-461b-9019-50d25dfa7bc6", | ||
"metadata": {}, | ||
"source": [ | ||
"#### sample physics-related code" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"id": "6ea31783d8e0e849", | ||
"metadata": { | ||
"ExecuteTime": { | ||
"end_time": "2024-12-04T13:27:08.240983Z", | ||
"start_time": "2024-12-04T13:27:07.013763Z" | ||
} | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"from PySDM import physics\n", | ||
"\n", | ||
"def code():\n", | ||
" si = physics.si\n", | ||
"\n", | ||
" p = 1000 * si.hPa\n", | ||
" T = 300 * si.K\n", | ||
" R = 286 * si.J / si.K / si.kg\n", | ||
"\n", | ||
" rho = p / R / T\n", | ||
" return rho" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "183bcca3-5546-4676-a6bc-de3a1dd76ccb", | ||
"metadata": {}, | ||
"source": [ | ||
"#### sample unit-unaware usage (default)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"id": "287ae3f109f11bb6", | ||
"metadata": { | ||
"ExecuteTime": { | ||
"end_time": "2024-12-04T13:27:08.341648Z", | ||
"start_time": "2024-12-04T13:27:08.339564Z" | ||
} | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"1.2\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"result_unit_unaware = code()\n", | ||
"print(f\"{result_unit_unaware:.2g}\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "da1669cf-653b-4138-9ff9-8fac303ad8c4", | ||
"metadata": {}, | ||
"source": [ | ||
"#### sample unit-aware usage (e.g., for testing)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"id": "85bf8e7bc2ca98a", | ||
"metadata": { | ||
"ExecuteTime": { | ||
"end_time": "2024-12-04T13:27:08.671915Z", | ||
"start_time": "2024-12-04T13:27:08.402327Z" | ||
} | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"0.012 hectopascal * kilogram / joule\n", | ||
"1.2 kilogram / meter ** 3\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"from PySDM.physics.dimensional_analysis import DimensionalAnalysis\n", | ||
"\n", | ||
"with DimensionalAnalysis():\n", | ||
" result_unit_aware = code()\n", | ||
"\n", | ||
"assert result_unit_aware.check(\"[mass] / [volume]\")\n", | ||
"print(f\"{result_unit_aware:.2g}\")\n", | ||
"print(f\"{result_unit_aware.to_base_units():.2g}\")" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.9.2" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters