From 96e9a74dd056a1ae7a741123359a5bcacdb93e62 Mon Sep 17 00:00:00 2001 From: chrishavlin Date: Wed, 26 Jul 2023 15:06:57 -0500 Subject: [PATCH] add units example --- examples/ex_001_load_VBRc_structure.ipynb | 170 +++++++++++++++++++++- 1 file changed, 169 insertions(+), 1 deletion(-) diff --git a/examples/ex_001_load_VBRc_structure.ipynb b/examples/ex_001_load_VBRc_structure.ipynb index d71cab4..7516e9f 100644 --- a/examples/ex_001_load_VBRc_structure.ipynb +++ b/examples/ex_001_load_VBRc_structure.ipynb @@ -53,7 +53,7 @@ "name": "stderr", "output_type": "stream", "text": [ - "pyVBRc : [INFO ] 2023-07-26 13:51:22,065: /home/chavlin/src/vbrProjects/pyVBRc/pyVBRc/sample_data/VBRc_sample_LUT.mat loaded.\n" + "pyVBRc : [INFO ] 2023-07-26 15:06:33,017: /home/chavlin/src/vbrProjects/pyVBRc/pyVBRc/sample_data/VBRc_sample_LUT.mat loaded.\n" ] } ], @@ -216,6 +216,174 @@ "plt.ylabel(\"Q$^{-1}$, andrade pseudo-period\")\n", "f.savefig('andrade_psp_T_dep.png')" ] + }, + { + "cell_type": "markdown", + "id": "ecfac287-8cec-4d52-9707-4162cc782a0a", + "metadata": {}, + "source": [ + "## units\n", + "\n", + "When loading a `VBR` structure generated by newer versions of the `VBRc` (>=v1.0.0), the arrays that get loaded in will be `unyt` arrays:" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "054230e5-0392-4113-8652-917b94d847e9", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "pyVBRc : [WARNING ] 2023-07-26 15:06:34,117: Ch2o in SV has unsupported units (ppm), using nondimensional\n", + "pyVBRc : [INFO ] 2023-07-26 15:06:34,123: /home/chavlin/src/vbrProjects/pyVBRc/pyVBRc/sample_data/VBRc_sample_LUT_v1pt0pt0.mat loaded.\n" + ] + } + ], + "source": [ + "from pyVBRc.sample_data import get_sample_filename\n", + "from pyVBRc.vbrc_structure import VBRCstruct\n", + "\n", + "file = get_sample_filename(\"VBRc_sample_LUT_v1pt0pt0.mat\")\n", + "vbr = VBRCstruct(file)" + ] + }, + { + "cell_type": "markdown", + "id": "ef18d5a0-7675-40ae-92a3-ea7020bcae72", + "metadata": {}, + "source": [ + "Inspecting the first 3 elements of the density array, for example:" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "e9025117-c7fb-4593-9e75-eda9066ec135", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "unyt_array([3300., 3300., 3300.], 'kg/m**3')" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "vbr.input.SV.rho[1:4,0,0]" + ] + }, + { + "cell_type": "markdown", + "id": "9bf3691c-3b1c-4b7b-a7ef-da48361f3fe6", + "metadata": {}, + "source": [ + "To change the units, " + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "27ddb8ee-1f88-465c-af3f-3361a575122f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "unyt_array([3.3, 3.3, 3.3], 'g/cm**3')" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "vbr.input.SV.rho[1:4,0,0].to('g/cm**3')" + ] + }, + { + "cell_type": "markdown", + "id": "4629c955-5e18-4827-b19b-3e420560b4e8", + "metadata": {}, + "source": [ + "`unyt` arrays will track and convert units throuhgout operations. For example, to calculate the hydrostaic pressure at 10 km depth for a single density value:" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "4cd341a9-21fd-4711-94b7-435144262b56", + "metadata": {}, + "outputs": [], + "source": [ + "from unyt import unyt_quantity, unyt_array " + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "0d645a0d-d132-43d8-8a80-ccf199b1bd3d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "unyt_quantity(3.234e+08, 'kg/(m*s**2)')" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "P = vbr.input.SV.rho[1,0,0] * unyt_quantity(9.8, 'm/s**2') * unyt_quantity(10, 'km')\n", + "P" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "6623f460-2bf2-462c-ba87-bc58f3436d61", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "unyt_quantity(0.3234, 'GPa')" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "P.to('GPa')" + ] + }, + { + "cell_type": "markdown", + "id": "55079833-f25c-4901-b0cc-1cc0a118e29e", + "metadata": {}, + "source": [ + "check out the [full `unyt` docs](https://unyt.readthedocs.io/en/stable/) for more on working with `unyt` arrays and quantities" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1ebaa242-faae-4f48-91fc-cd8b0ec9f83c", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": {