{ "cells": [ { "cell_type": "markdown", "id": "e0929fae", "metadata": {}, "source": [ "# Unit conversion example for O$_2$sclpy" ] }, { "cell_type": "markdown", "id": "27ed45fb", "metadata": {}, "source": [ "See the O$_2$sclpy documentation at\n", "https://awsteiner.org/code/o2sclpy for more information." ] }, { "cell_type": "code", "execution_count": 1, "id": "770f6e3c", "metadata": {}, "outputs": [], "source": [ "import o2sclpy\n", "import matplotlib.pyplot as plot\n", "import numpy\n", "import sys\n", "\n", "plots=True\n", "if 'pytest' in sys.modules:\n", " plots=False" ] }, { "cell_type": "markdown", "id": "1302c3d8", "metadata": {}, "source": [ "Link the O$_2$scl library:" ] }, { "cell_type": "code", "execution_count": 2, "id": "f2d3c2ad", "metadata": {}, "outputs": [], "source": [ "link=o2sclpy.linker()\n", "link.link_o2scl()" ] }, { "cell_type": "markdown", "id": "b2c798dd", "metadata": {}, "source": [ "Get a copy (a pointer to) the O$_2$scl unit conversion object:" ] }, { "cell_type": "code", "execution_count": 3, "id": "ac9764d7", "metadata": {}, "outputs": [], "source": [ "cu=link.o2scl_settings.get_convert_units()" ] }, { "cell_type": "markdown", "id": "97360abc", "metadata": {}, "source": [ "By default, conversions are allowed to presume that $\\hbar=c=k_B=1$.\n", "This code converts 2 $\\mathrm{MeV}$ to $1/\\mathrm{fm}$:" ] }, { "cell_type": "code", "execution_count": 4, "id": "70669f2d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.013546e-02\n" ] } ], "source": [ "val=cu.convert('MeV','1/fm',2.0)\n", "print('Conversion from MeV to 1/fm: %7.6e' % val)" ] }, { "cell_type": "markdown", "id": "235485f7", "metadata": {}, "source": [ "Add a new unit, a Bethe, defined to be $ 10^{51}~\\mathrm{erg} $" ] }, { "cell_type": "code", "execution_count": 5, "id": "937507f8", "metadata": {}, "outputs": [], "source": [ "cu.add_unit(b'Bethe',cu.convert('erg','kg*m^2/s^2',1.0e51),\n", " b'fifty one ergs',2,1,-2)" ] }, { "cell_type": "markdown", "id": "8d36e6e8", "metadata": {}, "source": [ "Now use the new unit conversion" ] }, { "cell_type": "code", "execution_count": 6, "id": "e9c9c57a", "metadata": {}, "outputs": [], "source": [ "print(cu.convert('erg','Bethe',3.0e53))" ] }, { "cell_type": "markdown", "id": "a3111478", "metadata": {}, "source": [ "Unicode is supported. Set a unit named α to refer to 3 Newtons per Kelvin" ] }, { "cell_type": "code", "execution_count": 7, "id": "f6ec63e5", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "9.999999999999999e+43 b'fifty one ergs' b'Bethe'\n" ] } ], "source": [ "cu.add_unit('α',3.0,'alpha unit',1,1,-2,-1)" ] }, { "cell_type": "markdown", "id": "d6c4980c", "metadata": {}, "source": [ "Print current unit table" ] }, { "cell_type": "code", "execution_count": 9, "id": "1787e74b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "SI-like: label m kg s K A mol cd value\n", "--------------- -- -- -- -- -- --- -- ------------\n", " rad 0 0 0 0 0 0 0 1.000000e+00\n", " sr 0 0 0 0 0 0 0 1.000000e+00\n", " J 2 1 -2 0 0 0 0 1.000000e+00\n", " N 1 1 -2 0 0 0 0 1.000000e+00\n", " Pa -1 1 -2 0 0 0 0 1.000000e+00\n", " W 2 1 -3 0 0 0 0 1.000000e+00\n", " C 0 1 0 0 1 0 0 1.000000e+00\n", " V 2 1 -3 0 -1 0 0 1.000000e+00\n", " ohm 2 1 -3 0 -2 0 0 1.000000e+00\n", " S -2 -1 3 0 2 0 0 1.000000e+00\n", " F -2 -1 4 0 2 0 0 1.000000e+00\n", " Wb 2 1 -2 0 -1 0 0 1.000000e+00\n", " H 2 1 -2 0 -2 0 0 1.000000e+00\n", " T 0 1 -2 0 -1 0 0 1.000000e+00\n", " Hz 0 0 -1 0 0 0 0 1.000000e+00\n", " lm 0 0 0 0 0 0 1 1.000000e+00\n", " lx -2 0 0 0 0 0 1 1.000000e+00\n", " Bq 0 0 -1 0 0 0 0 1.000000e+00\n", " Gy 2 0 -2 0 0 0 0 1.000000e+00\n", " Sv 2 0 -2 0 0 0 0 1.000000e+00\n", " kat 0 0 -1 0 0 1 0 1.000000e+00\n", " l 3 0 0 0 0 0 0 1.000000e-03\n", " L 3 0 0 0 0 0 0 1.000000e-03\n", " Da 0 1 0 0 0 0 0 1.660539e-27\n", " eV 2 1 -2 0 0 0 0 1.602177e-19\n", "\n", "SI prefixes: prefix value\n", "------------------- ------------\n", " Y 1.000000e+24\n", " Z 1.000000e+21\n", " E 1.000000e+18\n", " P 1.000000e+15\n", " T 1.000000e+12\n", " G 1.000000e+09\n", " M 1.000000e+06\n", " k 1.000000e+03\n", " h 1.000000e+02\n", " da 1.000000e+01\n", " d 1.000000e-01\n", " c 1.000000e-02\n", " m 1.000000e-03\n", " mu 1.000000e-06\n", " μ 1.000000e-06\n", " n 1.000000e-09\n", " p 1.000000e-12\n", " f 1.000000e-15\n", " a 1.000000e-18\n", " z 1.000000e-21\n", " y 1.000000e-24\n", "\n", "Other: label m kg s K A mol cd value full name\n", "--------------- -- -- -- -- -- --- -- ------------ ---------------------\n", " ft 1 0 0 0 0 0 0 3.048000e-01 foot\n", " foot 1 0 0 0 0 0 0 3.048000e-01 foot\n", " in 1 0 0 0 0 0 0 2.540000e-02 inch\n", " yd 1 0 0 0 0 0 0 9.144000e-01 yard\n", " mi 1 0 0 0 0 0 0 1.609344e+03 mile\n", " nmi 1 0 0 0 0 0 0 1.852000e+03 nautical mile\n", " fathom 1 0 0 0 0 0 0 1.828800e+00 fathom\n", " angstrom 1 0 0 0 0 0 0 1.000000e-10 angstrom\n", " mil 1 0 0 0 0 0 0 2.540000e-05 mil\n", " point 1 0 0 0 0 0 0 3.527778e-04 point\n", " texpoint 1 0 0 0 0 0 0 3.514598e-04 texpoint\n", " micron 1 0 0 0 0 0 0 1.000000e-06 micron\n", " AU 1 0 0 0 0 0 0 1.495979e+11 astronomical unit\n", " au 1 0 0 0 0 0 0 1.495979e+11 astronomical unit\n", " ly 1 0 0 0 0 0 0 9.460730e+15 light year\n", " lyr 1 0 0 0 0 0 0 9.460730e+15 light year\n", " Gpc 1 0 0 0 0 0 0 3.085678e+25 gigaparsec\n", " Mpc 1 0 0 0 0 0 0 3.085678e+22 megaparsec\n", " kpc 1 0 0 0 0 0 0 3.085678e+19 kiloparsec\n", " pc 1 0 0 0 0 0 0 3.085678e+16 parsec\n", " fermi 1 0 0 0 0 0 0 1.000000e-15 fermi\n", " hectare 2 0 0 0 0 0 0 1.000000e+04 hectare\n", " ha 2 0 0 0 0 0 0 1.000000e+04 hectare\n", " acre 2 0 0 0 0 0 0 4.046856e+03 acre\n", " barn 2 0 0 0 0 0 0 1.000000e-28 barn\n", " us_gallon 3 0 0 0 0 0 0 3.785412e-03 gallon\n", " quart 3 0 0 0 0 0 0 9.463529e-04 quart\n", " pint 3 0 0 0 0 0 0 4.731765e-04 pint\n", " cup 3 0 0 0 0 0 0 2.365882e-04 cup\n", " tbsp 3 0 0 0 0 0 0 1.478676e-05 tablespoon\n", " tsp 3 0 0 0 0 0 0 4.928922e-06 teaspoon\n", " ca_gallon 3 0 0 0 0 0 0 4.546090e-03 canadian gallon\n", " uk_gallon 3 0 0 0 0 0 0 4.546092e-03 uk gallon\n", " Msun 0 1 0 0 0 0 0 1.988410e+30 solar mass\n", " Msolar 0 1 0 0 0 0 0 1.988410e+30 solar mass\n", " pound 0 1 0 0 0 0 0 4.535924e-01 pound\n", " ounce 0 1 0 0 0 0 0 2.834952e-02 ounce\n", " tonne 0 1 0 0 0 0 0 1.000000e+03 (metric) tonne\n", " uk_ton 0 1 0 0 0 0 0 1.016047e+03 uk ton\n", " troy_ounce 0 1 0 0 0 0 0 3.110347e-02 troy ounce\n", " carat 0 1 0 0 0 0 0 2.000000e-04 carat\n", " knot 1 0 -1 0 0 0 0 5.144444e-01 knot\n", " c 1 0 -1 0 0 0 0 2.997925e+08 speed of light\n", " cal 2 1 -2 0 0 0 0 4.186800e+00 calorie\n", " btu 2 1 -2 0 0 0 0 1.055056e+03 btu\n", " erg 2 1 -2 0 0 0 0 1.000000e-07 erg\n", " therm 2 1 -3 0 0 0 0 1.055060e+08 therm\n", " horsepower 2 1 -3 0 0 0 0 7.457000e+02 horsepower\n", " hp 2 1 -3 0 0 0 0 7.457000e+02 horsepower\n", " atm -1 2 -2 0 0 0 0 1.013250e+05 atmosphere\n", " bar -1 1 -2 0 0 0 0 1.000000e+05 bar\n", " torr -1 1 -2 0 0 0 0 1.333224e+02 torr\n", " psi -1 1 -2 0 0 0 0 6.894757e+03 psi\n", " yr 0 0 1 0 0 0 0 3.155693e+07 year (tropical)\n", " wk 0 0 1 0 0 0 0 6.048000e+05 week\n", " d 0 0 1 0 0 0 0 8.640000e+04 day\n", " deg 0 0 0 0 0 0 0 1.745329e-02 degree\n", " ° 0 0 0 0 0 0 0 1.745329e-02 degree\n", " ′ 0 0 0 0 0 0 0 2.908882e-04 minute (fraction of a degree)\n", " ″ 0 0 0 0 0 0 0 4.848137e-06 second (fraction of a degree)\n", " hr 0 0 1 0 0 0 0 3.600000e+03 hour\n", " min 0 0 1 0 0 0 0 6.000000e+01 minute\n", " curie 0 0 -1 0 0 0 0 3.700000e+10 curie\n", " dyne 1 1 -2 0 0 0 0 1.000000e-05 dyne\n", " poise -1 1 -1 0 0 0 0 1.000000e-01 poise\n", " kB 2 1 -2 -1 0 0 0 1.380649e-23 Boltzmann's constant\n", " hbar 2 1 -1 0 0 0 0 1.054572e-34 reduced Planck constant\n", " Planck 2 1 -1 0 0 0 0 6.626070e-34 Planck constant\n", " GNewton 3 -1 -2 0 0 0 0 6.674300e-11 gravitational constant\n", " G 0 1 -2 0 -1 0 0 1.000000e-04 gauss\n", " NA 0 0 0 0 0 -1 0 6.022141e+23 Avogadro's number\n", " 2 1 -2 0 0 0 0 1.000000e+44 \n" ] } ], "source": [ "cu.print_units_cout()" ] }, { "cell_type": "markdown", "id": "4e9cd81e", "metadata": {}, "source": [ "Show that our unit named α works" ] }, { "cell_type": "code", "execution_count": null, "id": "3839097e", "metadata": {}, "outputs": [], "source": [ "print(cu.convert('N/K','α',27))" ] }, { "cell_type": "markdown", "id": "79391d8b", "metadata": {}, "source": [ "Now, remove the use of natural units" ] }, { "cell_type": "code", "execution_count": null, "id": "0df57bc9", "metadata": {}, "outputs": [], "source": [ "cu.set_natural_units(False,False,False)" ] }, { "cell_type": "code", "execution_count": null, "id": "cb38b9eb", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "b9ba2c22", "metadata": { "lines_to_next_cell": 2 }, "outputs": [], "source": [] } ], "metadata": { "jupytext": { "cell_metadata_filter": "-all", "main_language": "python", "notebook_metadata_filter": "-all" }, "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.7" } }, "nbformat": 4, "nbformat_minor": 5 }