pax_global_header00006660000000000000000000000064150711461670014522gustar00rootroot0000000000000052 comment=cf01e9e2f63c6ad8c59839574dcab9852592f40e hepunits-2.4.2/000077500000000000000000000000001507114616700133665ustar00rootroot00000000000000hepunits-2.4.2/.github/000077500000000000000000000000001507114616700147265ustar00rootroot00000000000000hepunits-2.4.2/.github/dependabot.yml000066400000000000000000000003411507114616700175540ustar00rootroot00000000000000version: 2 updates: # Maintain dependencies for GitHub Actions - package-ecosystem: "github-actions" directory: "/" schedule: interval: "monthly" groups: actions: patterns: - "*" hepunits-2.4.2/.github/workflows/000077500000000000000000000000001507114616700167635ustar00rootroot00000000000000hepunits-2.4.2/.github/workflows/cd.yml000066400000000000000000000014571507114616700201030ustar00rootroot00000000000000name: CD on: workflow_dispatch: push: branches: - main release: types: - published jobs: dist: runs-on: ubuntu-latest steps: - uses: actions/checkout@v5 with: fetch-depth: 0 - name: Build wheel and SDist run: pipx run build - uses: actions/upload-artifact@v4 with: path: dist/* - name: Check metadata run: pipx run twine check dist/* publish: needs: [dist] runs-on: ubuntu-latest if: github.event_name == 'release' && github.event.action == 'published' environment: pypi permissions: id-token: write steps: - uses: actions/download-artifact@v5 with: name: artifact path: dist - uses: pypa/gh-action-pypi-publish@release/v1 hepunits-2.4.2/.github/workflows/main.yml000066400000000000000000000024151507114616700204340ustar00rootroot00000000000000name: CI on: pull_request: push: branches: [main] concurrency: group: ${{ github.workflow }}-${{ github.ref }} cancel-in-progress: true jobs: pre-commit: name: Format runs-on: ubuntu-latest steps: - uses: actions/checkout@v5 - uses: actions/setup-python@v6 with: python-version: "3.x" - name: Check manifest uses: pre-commit/action@v3.0.1 checks: runs-on: ubuntu-latest strategy: fail-fast: false matrix: python-version: - "3.8" - "3.9" - "3.10" - "3.11" - "3.12" - "3.13" name: Check Python ${{ matrix.python-version }} steps: - uses: actions/checkout@v5 with: fetch-depth: 0 - uses: actions/setup-python@v6 with: python-version: ${{ matrix.python-version }} allow-prereleases: true - uses: astral-sh/setup-uv@v6 - name: Install package run: uv pip install --system -e .[test] - name: Test package run: python -m pytest --doctest-modules --cov=src/hepunits --cov-report=xml - name: Test coverage with Codecov uses: codecov/codecov-action@v5 env: CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} hepunits-2.4.2/.gitignore000066400000000000000000000013041507114616700153540ustar00rootroot00000000000000*.py[cod] # C extensions *.so # Packages *.egg *.egg-info dist build eggs .eggs parts bin var sdist wheelhouse develop-eggs .installed.cfg lib lib64 venv*/ pyvenv*/ # Installer logs pip-log.txt # Unit test / coverage reports .coverage .tox .coverage.* nosetests.xml coverage.xml htmlcov # Translations *.mo # Mr Developer .mr.developer.cfg .project .pydevproject .idea *.iml *.komodoproject .editorconfig # Complexity output/*.html output/*/index.html # Sphinx docs/_build .DS_Store *~ .*.sw[po] .build .ve .env .cache .pytest .bootstrap .appveyor.token *.bak .ipynb_checkpoints .pytest_cache /docs/html *.gv* /.mypy_cache/* /pip-wheel-metadata/* /src/hepunits/version.py /src/hepunits/_version.py hepunits-2.4.2/.pre-commit-config.yaml000066400000000000000000000027611507114616700176550ustar00rootroot00000000000000ci: autoupdate_commit_msg: "chore(deps): update pre-commit hooks" autofix_commit_msg: "style: pre-commit fixes" repos: - repo: https://github.com/pre-commit/pre-commit-hooks rev: v6.0.0 hooks: - id: debug-statements - id: end-of-file-fixer - id: mixed-line-ending - id: requirements-txt-fixer - id: trailing-whitespace - id: check-added-large-files - id: check-case-conflict - id: check-merge-conflict - id: check-symlinks - id: check-yaml - repo: https://github.com/astral-sh/ruff-pre-commit rev: "v0.13.3" hooks: - id: ruff args: ["--fix", "--show-fixes"] - id: ruff-format - repo: https://github.com/pre-commit/mirrors-mypy rev: v1.18.2 hooks: - id: mypy files: src args: [] additional_dependencies: [pint] - repo: https://github.com/codespell-project/codespell rev: v2.4.1 hooks: - id: codespell args: ["-L", "HEP"] - repo: https://github.com/rbubley/mirrors-prettier rev: "v3.6.2" hooks: - id: prettier types_or: [yaml, markdown, html, css, scss, javascript, json] - repo: https://github.com/adamchainz/blacken-docs rev: 1.20.0 hooks: - id: blacken-docs args: ["-E"] additional_dependencies: [black~=24.0] - repo: https://github.com/pre-commit/pygrep-hooks rev: v1.10.0 hooks: - id: rst-backticks - id: rst-directive-colons - id: rst-inline-touching-normal hepunits-2.4.2/CITATION.cff000066400000000000000000000010451507114616700152600ustar00rootroot00000000000000cff-version: 1.2.0 title: hepunits message: >- If you use this software, please cite it using the metadata from this file. type: software abstract: "Hepunits is a Python library for units and constants in the HEP system of units." authors: - family-names: Rodrigues given-names: Eduardo affiliation: University of Liverpool orcid: "https://orcid.org/0000-0003-2846-7625" doi: 10.5281/zenodo.7244906 repository-code: "https://github.com/scikit-hep/hepunits" keywords: - python - HEP units - scikit-hep license: "BSD-3-Clause" hepunits-2.4.2/LICENSE000066400000000000000000000027631507114616700144030ustar00rootroot00000000000000BSD 3-Clause License Copyright (c) 2018-2025, Eduardo Rodrigues. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. hepunits-2.4.2/README.rst000066400000000000000000000151361507114616700150630ustar00rootroot00000000000000``hepunits``: units and constants in the HEP system of units ============================================================ |Scikit-HEP| |PyPI version| |Conda-forge version| |Zenodo DOI| |GitHub Actions Status: CI| |Code Coverage| ``hepunits`` collects the most commonly used units and constants in the HEP System of Units, as derived from the basic units originally defined by the `CLHEP`_ project, which are *not* the same as the SI system of units: =================== ================== ==== Quantity Name Unit =================== ================== ==== Length millimeter mm Time nanosecond ns Energy Mega electron Volt MeV Positron charge eplus Temperature kelvin K Amount of substance mole mol Luminous intensity candela cd Plane angle radian rad Solid angle steradian sr =================== ================== ==== It is largely based on the international system of units (`SI`_) =================== ======== ==== Quantity Name Unit =================== ======== ==== Length meter m Time second s Mass kilogram kg Electric current ampere A Temperature kelvin K Amount of substance mole mol Luminous intensity candela cd =================== ======== ==== but augments it with handy definitions, changing the basic length and time units. This HEP system of units is in use in many software libraries such as GEANT4 and Gaudi. Note that many units are now *exact*, such as the speed of light in vacuum. The package is in agreement with the values in the 2020 Particle Data Group review. .. _CLHEP: http://proj-clhep.web.cern.ch/proj-clhep/ .. _SI: http://www.physics.nist.gov/cuu/Units/index.html Installation ------------ Install ``hepunits`` like any other Python package, typically: .. code-block:: bash python -m pip install hepunits The package is also available on `conda-forge`_, and installable with .. code-block:: bash conda install conda-forge::hepunits .. _conda-forge: https://github.com/conda-forge/hepunits-feedstock Getting started --------------- The package contains 2 core modules, ``constants`` and ``units``, whose names are self-explanatory. It may be more readable to import quantities explicitly from each of the modules though everything is available from the top-level as ``from hepunits import ...``. The module ``hepunits.constants`` contains 2 sorts of constants: physical constants and commonly used constants. The typical usage is the following: .. code-block:: pycon >>> from hepunits.constants import c_light >>> from hepunits.units import picosecond, micrometer >>> tau_Bs = 1.5 * picosecond # a particle lifetime, say the Bs meson's >>> ctau_Bs = c_light * tau_Bs # ctau of the particle, ~450 microns >>> print(ctau_Bs) # result in HEP units, so mm 0.449688687 >>> print(ctau_Bs / micrometer) # result in micrometers 449.688687 Typical usage of the ``hepunits.units`` module: .. code-block:: pycon >>> # add two quantities with length units and get the result in meters >>> from hepunits import units as u >>> (1 * u.meter + 5 * u.cm) / u.meter 1.05 >>> # the default result is, of course, in HEP units, so mm >>> 1 * u.meter + 5 * u.cm 1050.0 Fancier usage ~~~~~~~~~~~~~ When working with data the user should not need to know what units are used in their internal representation (it makes sense, though, and *is important*, to be consistent throughout the "data storages"!). These simple rules are enough - exemplified in the code below: - Dimensioned quantities in the "data stores" abide to the HEP system of units. - All definitions of dimensioned quantities are dimensioned by multiplying by the units, as in ``mass_window = 500 * keV``. - All output of dimensioned quantities is converted to the required units by dividing by the units, as in ``energy_resolution() / GeV``. For the sake of argument, let's consider below a function returning a dimensioned quantity. the function below stores a dimensioned quantity defined in keV (the actual value is represented in MeV, which is the standard unit) and the caller simply needs to ensure an explicit conversion to the desired unit dividing by it (GeV in the example): .. code-block:: pycon >>> from hepunits.units import keV, MeV, GeV >>> mass_window = 1 * GeV # define a 1 GeV mass window >>> def energy_resolution(): ... # returns the energy resolution of 500 keV ... return 500.0 * keV # numerical value is 0.5 ... >>> energy_resolution() / GeV # get the energy resolution in GeV 0.0005 Pint integration ~~~~~~~~~~~~~~~~ The package can interoperate with `Pint`_, which provides a more full-featured units and quantities system. Pint is an optional dependency of ``hepunits``. When Pint is installed, ``hepunits`` units and constants can be used to create Pint quantities, and Pint quantities can be converted to ``hepunits`` units, as shown below. .. code-block:: pycon >>> import pint >>> import hepunits >>> from hepunits.pint import to_clhep, from_clhep >>> ureg = pint.UnitRegistry() >>> g = 9.8 * ureg.meter / ureg.second**2 >>> g >>> to_clhep(g) 9.800000000000001e-15 >>> from_clhep(hepunits.c_light, ureg.meter / ureg.second) >>> from_clhep(hepunits.c_light, ureg.fathom / ureg.fortnight) .. _Pint: https://pint.readthedocs.io/ .. |Scikit-HEP| image:: https://scikit-hep.org/assets/images/Scikit--HEP-Project-blue.svg :target: https://scikit-hep.org .. |PyPI version| image:: https://img.shields.io/pypi/v/hepunits.svg :target: https://pypi.org/project/hepunits/ .. |Conda-forge version| image:: https://img.shields.io/conda/vn/conda-forge/hepunits.svg :target: https://github.com/conda-forge/hepunits-feedstock .. |Zenodo DOI| image:: https://zenodo.org/badge/DOI/10.5281/zenodo.7244906.svg :target: https://doi.org/10.5281/zenodo.7244906 .. |GitHub Actions Status: CI| image:: https://github.com/scikit-hep/hepunits/workflows/CI/badge.svg :target: https://github.com/scikit-hep/hepunits/actions .. |Code Coverage| image:: https://codecov.io/gh/scikit-hep/hepunits/graph/badge.svg?branch=main :target: https://codecov.io/gh/scikit-hep/hepunits?branch=main hepunits-2.4.2/codecov.yml000066400000000000000000000000441507114616700155310ustar00rootroot00000000000000codecov: require_ci_to_pass: true hepunits-2.4.2/noxfile.py000066400000000000000000000007351507114616700154110ustar00rootroot00000000000000import nox nox.needs_version = ">=2024.4.15" nox.options.default_venv_backend = "uv|virtualenv" @nox.session def lint(session: nox.Session) -> None: """ Run the linter. """ session.install("pre-commit") session.run("pre-commit", "run", "--all-files", *session.posargs) @nox.session def tests(session: nox.Session) -> None: """ Run the unit and regular tests. """ session.install("-e.[test]") session.run("pytest", *session.posargs) hepunits-2.4.2/pyproject.toml000066400000000000000000000061641507114616700163110ustar00rootroot00000000000000[build-system] requires = ["hatchling", "hatch-vcs"] build-backend = "hatchling.build" [project] name = "hepunits" description = "Units and constants in the HEP system of units" readme = "README.rst" requires-python = ">=3.8" authors = [ { name = "Eduardo Rodrigues", email = "eduardo.rodrigues@cern.ch" }, ] maintainers = [ { name = "Scikit-HEP", email = "scikit-hep-admins@googlegroups.com" }, ] keywords = [ "Constants", "HEP", "HEP system of units", "Units", ] classifiers = [ "Development Status :: 5 - Production/Stable", "Intended Audience :: Developers", "Intended Audience :: Science/Research", "License :: OSI Approved :: BSD License", "Operating System :: OS Independent", "Programming Language :: Python", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3 :: Only", "Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", "Programming Language :: Python :: 3.12", "Programming Language :: Python :: 3.13", "Topic :: Scientific/Engineering", ] dynamic = ["version"] [project.optional-dependencies] all = [ "pytest-cov>=2.8.0", "pytest>=6", "pint", ] dev = [ "pytest-cov>=2.8.0", "pytest>=6", "pint", ] test = [ "pytest-cov>=2.8.0", "pytest>=6", "pint", ] [project.urls] Homepage = "https://github.com/scikit-hep/hepunits" [tool.hatch] version.source = "vcs" build.hooks.vcs.version-file = "src/hepunits/_version.py" [tool.pytest.ini_options] minversion = "6.0" junit_family = "xunit2" testpaths = ["tests"] filterwarnings = [ "error", ] addopts = [ "-ra", "--showlocals", "--strict-markers", "--strict-config", ] xfail_strict = true log_cli_level = "DEBUG" [tool.check-manifest] ignore = [ "src/hepunits/_version.py", ".pre-commit-config.yaml", "noxfile.py", ] [tool.mypy] warn_unused_configs = true python_version = "3.8" files = ["src"] strict = true warn_unreachable = true enable_error_code = ["ignore-without-code", "redundant-expr", "truthy-bool"] [tool.ruff.lint] extend-select = [ "B", # flake8-bugbear "I", # isort "ARG", # flake8-unused-arguments "C4", # flake8-comprehensions "EM", # flake8-errmsg "ICN", # flake8-import-conventions "ISC", # flake8-implicit-str-concat "PGH", # pygrep-hooks "PIE", # flake8-pie "PL", # pylint "PT", # flake8-pytest-style "PTH", # flake8-use-pathlib "RET", # flake8-return "RUF", # Ruff-specific "SIM", # flake8-simplify "T20", # flake8-print "UP", # pyupgrade "YTT", # flake8-2020 ] ignore = [ "ISC001", # Conflicts with formatter ] [tool.ruff.lint.per-file-ignores] "tests/*" = [ "F403", "F405", "PLR2004", # Magic value in comparisons "PT013", # Okay to import approx from pytest "T20", ] [tool.repo-review] ignore = [ "RTD", # ReadTheDocs "PY004", # Has docs folder ] hepunits-2.4.2/src/000077500000000000000000000000001507114616700141555ustar00rootroot00000000000000hepunits-2.4.2/src/hepunits/000077500000000000000000000000001507114616700160145ustar00rootroot00000000000000hepunits-2.4.2/src/hepunits/__init__.py000066400000000000000000000166341507114616700201370ustar00rootroot00000000000000# Licensed under a 3-clause BSD style license, see LICENSE. # Convenient access to the version number from typing import List from . import constants, units from ._version import version as __version__ from .constants.constants import ( Avogadro, c_light, c_light_sq, e_sq, eminus, h_Planck, half_pi, hbar, hbar_Planck, hbarc, hbarc_sq, k_Boltzmann, pi, pi_sq, two_pi, ) from .units.prefixes import ( atto, centi, deca, deci, exa, exbi, femto, gibi, giga, googol, hecto, kibi, kilo, mebi, mega, micro, milli, nano, pebi, peta, pico, quecto, quetta, ronna, ronto, tebi, tera, yobi, yocto, yotta, zebi, zepto, zetta, ) from .units.units import ( GJ, GW, MJ, MV, MW, A, Bq, C, Ci, EeV, F, G, GBq, GeV, GHz, Gs, Gy, H, Hz, J, K, MBq, MeV, MGy, MHz, N, Pa, PeV, Qg, Rg, Sv, T, TeV, THz, V, W, Wb, ZeV, ab, ampere, angstrom, atm, atmosphere, attobarn, attosecond, bar, barn, becquerel, candela, cd, centimeter, centimeter2, centimeter3, cm, cm2, cm3, coulomb, curie, d, day, deg, degree, dyne, e_SI, electronvolt, electronvolt2, eplus, erg, eV, eV2, exaelectronvolt, farad, fb, femtobarn, femtometer, femtosecond, fermi, fm, fm2, fm3, fs, g, gauss, gigabecquerel, gigaelectronvolt, gigahertz, gigajoule, gigawatt, gram, gray, h, henry, hertz, hour, invab, invfb, invmb, invnb, invpb, invub, joule, kBq, kelvin, keV, kG, kg, kGs, kGy, kHz, kilobecquerel, kiloelectronvolt, kilogauss, kilogram, kilogray, kilohertz, kilojoule, kilometer, kilometer2, kilometer3, kilovolt, kilowatt, kJ, km, km2, km3, kV, kW, lm, lumen, lux, lx, m, m2, m3, mA, mb, mbar, mCi, megabecquerel, megaelectronvolt, megagray, megahertz, megajoule, megavolt, megawatt, meter, meter2, meter3, mF, mg, mGy, microampere, microbarn, microcurie, microfarad, microgray, micrometer, micron, microsecond, microweber, milliampere, millibar, millibarn, millicurie, millifarad, milligram, milligray, millimeter, millimeter2, millimeter3, milliradian, millisecond, milliweber, minute, mm, mm2, mm3, mol, mole, mrad, ms, mWb, nA, nanoampere, nanobarn, nanocurie, nanofarad, nanometer, nanosecond, nanoweber, nb, nCi, newton, nF, ns, nWb, ohm, pascal, pb, petaelectronvolt, pF, picobarn, picofarad, picosecond, ps, quettagram, rad, radian, rg, ronnagram, rontogram, s, second, sievert, sr, steradian, teraelectronvolt, terahertz, tesla, uA, ub, uCi, uF, uGy, us, uWb, volt, watt, weber, y, year, yoctosecond, ys, zeptosecond, zettaelectronvolt, zs, ) # Units and constants directly available __all__ = ( "GJ", "GW", "MJ", "MV", "MW", "A", "Avogadro", "Bq", "C", "Ci", "EeV", "F", "G", "GBq", "GHz", "GeV", "Gs", "Gy", "H", "Hz", "J", "K", "MBq", "MGy", "MHz", "MeV", "N", "Pa", "PeV", "Qg", "Rg", "Sv", "T", "THz", "TeV", "V", "W", "Wb", "ZeV", "__version__", "ab", "ampere", "angstrom", "atm", "atmosphere", "atto", "attobarn", "attosecond", "bar", "barn", "becquerel", "c_light", "c_light_sq", "candela", "cd", "centi", "centimeter", "centimeter2", "centimeter3", "cm", "cm2", "cm3", "constants", "coulomb", "curie", "d", "day", "deca", "deci", "deg", "degree", "dyne", "eV", "eV2", "e_SI", "e_sq", "electronvolt", "electronvolt2", "eminus", "eplus", "eplus", "erg", "exa", "exaelectronvolt", "exbi", "farad", "fb", "femto", "femtobarn", "femtometer", "femtosecond", "fermi", "fm", "fm2", "fm3", "fs", "g", "gauss", "gibi", "giga", "gigabecquerel", "gigaelectronvolt", "gigahertz", "gigajoule", "gigawatt", "googol", "gram", "gray", "h", "h_Planck", "half_pi", "hbar", "hbar_Planck", "hbarc", "hbarc_sq", "hecto", "henry", "hertz", "hour", "invab", "invfb", "invmb", "invnb", "invpb", "invub", "joule", "joule", "kBq", "kG", "kGs", "kGy", "kHz", "kJ", "kV", "kW", "k_Boltzmann", "keV", "kelvin", "kelvin", "kg", "kibi", "kilo", "kilobecquerel", "kiloelectronvolt", "kilogauss", "kilogram", "kilogray", "kilohertz", "kilojoule", "kilometer", "kilometer2", "kilometer3", "kilovolt", "kilowatt", "km", "km2", "km3", "lm", "lumen", "lux", "lx", "m", "m", "m2", "m3", "mA", "mCi", "mF", "mGy", "mWb", "mb", "mbar", "mebi", "mega", "megabecquerel", "megaelectronvolt", "megagray", "megahertz", "megajoule", "megavolt", "megawatt", "meter", "meter2", "meter3", "mg", "micro", "microampere", "microbarn", "microcurie", "microfarad", "microgray", "micrometer", "micron", "microsecond", "microweber", "milli", "milliampere", "millibar", "millibarn", "millicurie", "millifarad", "milligram", "milligray", "millimeter", "millimeter2", "millimeter3", "milliradian", "millisecond", "milliweber", "minute", "mm", "mm2", "mm3", "mol", "mole", "mole", "mrad", "ms", "nA", "nCi", "nF", "nWb", "nano", "nanoampere", "nanobarn", "nanocurie", "nanofarad", "nanometer", "nanosecond", "nanoweber", "nb", "newton", "ns", "ohm", "pF", "pascal", "pb", "pebi", "peta", "petaelectronvolt", "pi", "pi_sq", "pico", "picobarn", "picofarad", "picosecond", "ps", "quecto", "quetta", "quettagram", "rad", "radian", "rg", "ronna", "ronnagram", "ronto", "rontogram", "s", "s", "second", "sievert", "sr", "steradian", "tebi", "tera", "teraelectronvolt", "terahertz", "tesla", "two_pi", "uA", "uCi", "uF", "uGy", "uWb", "ub", "units", "us", "volt", "watt", "weber", "y", "year", "yobi", "yocto", "yoctosecond", "yotta", "ys", "zebi", "zepto", "zeptosecond", "zetta", "zettaelectronvolt", "zs", ) def __dir__() -> List[str]: return list(__all__) hepunits-2.4.2/src/hepunits/_version.pyi000066400000000000000000000001221507114616700203560ustar00rootroot00000000000000version: str version_tuple: tuple[int, int, int] | tuple[int, int, int, str, str] hepunits-2.4.2/src/hepunits/constants/000077500000000000000000000000001507114616700200305ustar00rootroot00000000000000hepunits-2.4.2/src/hepunits/constants/__init__.py000066400000000000000000000014721507114616700221450ustar00rootroot00000000000000# Licensed under a 3-clause BSD style license, see LICENSE. from typing import List from . import constants from .constants import ( Avogadro, c_light, c_light_sq, e_SI, e_sq, electronvolt, eminus, eplus, h_Planck, half_pi, hbar, hbar_Planck, hbarc, hbarc_sq, joule, k_Boltzmann, kelvin, m, mole, pi, pi_sq, s, two_pi, ) __all__ = ( "Avogadro", "c_light", "c_light_sq", "constants", "e_SI", "e_sq", "electronvolt", "eminus", "eplus", "h_Planck", "half_pi", "hbar", "hbar_Planck", "hbarc", "hbarc_sq", "joule", "k_Boltzmann", "kelvin", "m", "mole", "pi", "pi_sq", "s", "two_pi", ) def __dir__() -> List[str]: return list(__all__) hepunits-2.4.2/src/hepunits/constants/constants.py000066400000000000000000000052251507114616700224220ustar00rootroot00000000000000# Licensed under a 3-clause BSD style license, see LICENSE. """ Physical and other handy constants ================================== This module `hepunits.constants` contains 2 sorts of constants: * Physical constants. * Common and/or handy constants. All constants are computed in the HEP System of Units as defined in the `hepunits.units` module. Typical use case:: >>> from hepunits.constants import c_light >>> from hepunits.units import picosecond, micrometer >>> tau_Bs = 1.5 * picosecond # a particle lifetime, say the Bs meson's >>> ctau_Bs = c_light * tau_Bs # ctau of the particle, ~450 microns >>> print ctau_Bs # result in HEP units, so mm ;-) 0.449688687 >>> print ctau_Bs / micrometer # result in micrometers 449.688687 """ # ----------------------------------------------------------------------------- # Import statements # ----------------------------------------------------------------------------- from math import pi from typing import List from ..units.units import e_SI, electronvolt, eplus, joule, kelvin, m, mole, s __all__ = ( "Avogadro", "c_light", "c_light_sq", "e_SI", "e_sq", "electronvolt", "eminus", "eplus", "h_Planck", "half_pi", "hbar", "hbar_Planck", "hbarc", "hbarc_sq", "joule", "k_Boltzmann", "kelvin", "m", "mole", "pi", "pi_sq", "s", "two_pi", ) # ----------------------------------------------------------------------------- # Mathematical constants # ----------------------------------------------------------------------------- two_pi = 2 * pi half_pi = pi / 2 pi_sq = pi * pi # ----------------------------------------------------------------------------- # Physical constants # ----------------------------------------------------------------------------- # Speed of light in vacuum (exact value, taken from PDG 2022) c_light = 299792458 * m / s c_light_sq = c_light * c_light # Electron charge eminus = -eplus # electron charge e_sq = eplus * eplus # Avogadro constant (exact value, taken from PDG 2022) Avogadro = 6.02214076e23 / mole # Planck constant (exact value, taken from PDG 2022) # h_Planck = 6.62607015e-34 * joule * s # Simplify the units to avoid floating errors, using joule = electronvolt / e_SI, # noticing also that electronvolt * s = 1000.0 to rearrange h_Planck = 6.62607015e-34 * electronvolt * s / e_SI hbar_Planck = h_Planck / two_pi hbar = hbar_Planck hbarc = hbar_Planck * c_light hbarc_sq = hbarc * hbarc # Boltzmann constant (exact value, taken from PDG 2022) k_Boltzmann = 1.380649e-23 * joule / kelvin def __dir__() -> List[str]: return list(__all__) hepunits-2.4.2/src/hepunits/pint.py000066400000000000000000000054271507114616700173500ustar00rootroot00000000000000# Licensed under a 3-clause BSD style license, see LICENSE. """ Conversion routines between CLHEP, the HEP System of Units, and Pint units. CLHEP adopts the approach where all the quantities are stored in the base unit system, effectively dimensionless. Pint offers to store both the magnitude and the dimensionality of the unit, which is helpful in deducing and/or validating the resulting unit of formulas. This module offers conversion routines between Pint's default base unit system and CLHEP. """ from __future__ import annotations try: import pint except ImportError as exc: msg = "Pint is required to use hepunits.pint." raise ImportError(msg) from exc # TODO: support more unit conversions _clhep_base_units = { "[length]": "millimeter", "[time]": "nanosecond", "[mass]": "MeV * millimeter**-2 * nanosecond**2", "[current]": "elementary_charge / nanosecond", } def _unit_from(val: pint.Quantity | pint.Unit) -> pint.Unit: """Extract the dimensionality from a Pint Quantity or Unit.""" # Grabbing the type is a quick way to be in the correct unit registry # see e.g. https://github.com/hgrecco/pint/issues/2207 unit = type(val.units) if isinstance(val, pint.Quantity) else type(val) out = unit("dimensionless") for dim, exponent in val.dimensionality.items(): if dim not in _clhep_base_units: msg = f"Unsupported dimension {dim} in {val}" raise ValueError(msg) out *= unit(_clhep_base_units[dim]) ** exponent return out def to_clhep(val: pint.Quantity | pint.Unit) -> float: """ Convert a Pint Quantity or Unit to CLHEP base units. Parameters ---------- val : pint.Quantity or pint.Unit The value to convert. Returns ------- float The value in CLHEP base units (dimensionless). Examples -------- >>> ureg = pint.UnitRegistry() >>> g = 9.8 * ureg.meter / ureg.second**2 >>> g >>> to_clhep(g) 9.800000000000001e-15 """ clhep_unit = _unit_from(val) q = (1.0 * val) if isinstance(val, pint.Unit) else val return q.to(clhep_unit).magnitude # type: ignore[no-any-return] def from_clhep(val: float, unit: pint.Unit) -> pint.Quantity: """ Convert a value in CLHEP base units to a Pint Quantity. Parameters ---------- val : float The value in CLHEP base units (dimensionless). unit : pint.Unit The desired output unit. Returns ------- pint.Quantity The value in the desired unit. Examples -------- >>> ureg = pint.UnitRegistry() >>> from_clhep(hepunits.c_light, ureg.meter / ureg.second) """ clhep_unit = _unit_from(unit) return (val * clhep_unit).to(unit) hepunits-2.4.2/src/hepunits/py.typed000066400000000000000000000000001507114616700175010ustar00rootroot00000000000000hepunits-2.4.2/src/hepunits/units/000077500000000000000000000000001507114616700171565ustar00rootroot00000000000000hepunits-2.4.2/src/hepunits/units/__init__.py000066400000000000000000000153601507114616700212740ustar00rootroot00000000000000# Licensed under a 3-clause BSD style license, see LICENSE. from typing import List from . import prefixes, units from .prefixes import ( atto, centi, deca, deci, exa, exbi, femto, gibi, giga, googol, hecto, kibi, kilo, mebi, mega, micro, milli, nano, pebi, peta, pico, quecto, quetta, ronna, ronto, tebi, tera, yobi, yocto, yotta, zebi, zepto, zetta, ) from .units import ( GJ, GW, MJ, MV, MW, A, Bq, C, Ci, EeV, F, G, GBq, GeV, GHz, Gs, Gy, H, Hz, J, K, MBq, MeV, MGy, MHz, N, Pa, PeV, Qg, Rg, Sv, T, TeV, THz, V, W, Wb, ZeV, ab, ampere, angstrom, atm, atmosphere, attobarn, attosecond, bar, barn, becquerel, candela, cd, centimeter, centimeter2, centimeter3, cm, cm2, cm3, coulomb, curie, d, day, deg, degree, dyne, e_SI, electronvolt, electronvolt2, eplus, erg, eV, eV2, exaelectronvolt, farad, fb, femtobarn, femtometer, femtosecond, fermi, fm, fm2, fm3, fs, g, gauss, gigabecquerel, gigaelectronvolt, gigahertz, gigajoule, gigawatt, gram, gray, h, henry, hertz, hour, invab, invfb, invmb, invnb, invpb, invub, joule, kBq, kelvin, keV, kG, kg, kGs, kGy, kHz, kilobecquerel, kiloelectronvolt, kilogauss, kilogram, kilogray, kilohertz, kilojoule, kilometer, kilometer2, kilometer3, kilovolt, kilowatt, kJ, km, km2, km3, kV, kW, lm, lumen, lux, lx, m, m2, m3, mA, mb, mbar, mCi, megabecquerel, megaelectronvolt, megagray, megahertz, megajoule, megavolt, megawatt, meter, meter2, meter3, mF, mg, mGy, microampere, microbarn, microcurie, microfarad, microgray, micrometer, micron, microsecond, microweber, milliampere, millibar, millibarn, millicurie, millifarad, milligram, milligray, millimeter, millimeter2, millimeter3, milliradian, millisecond, milliweber, minute, mm, mm2, mm3, mol, mole, mrad, ms, mWb, nA, nanoampere, nanobarn, nanocurie, nanofarad, nanometer, nanosecond, nanoweber, nb, nCi, newton, nF, ns, nWb, ohm, pascal, pb, petaelectronvolt, pF, picobarn, picofarad, picosecond, ps, quettagram, rad, radian, rg, ronnagram, rontogram, s, second, sievert, sr, steradian, teraelectronvolt, terahertz, tesla, uA, ub, uCi, uF, uGy, us, uWb, volt, watt, weber, y, year, yoctosecond, ys, zeptosecond, zettaelectronvolt, zs, ) __all__ = ( "GJ", "GW", "MJ", "MV", "MW", "A", "Bq", "C", "Ci", "EeV", "F", "G", "GBq", "GHz", "GeV", "Gs", "Gy", "H", "Hz", "J", "K", "MBq", "MGy", "MHz", "MeV", "N", "Pa", "PeV", "Qg", "Rg", "Sv", "T", "THz", "TeV", "V", "W", "Wb", "ZeV", "ab", "ampere", "angstrom", "atm", "atmosphere", "atto", "attobarn", "attosecond", "bar", "barn", "becquerel", "candela", "cd", "centi", "centimeter", "centimeter2", "centimeter3", "cm", "cm2", "cm3", "coulomb", "curie", "d", "day", "deca", "deci", "deg", "degree", "dyne", "eV", "eV2", "e_SI", "electronvolt", "electronvolt2", "eplus", "erg", "exa", "exaelectronvolt", "exbi", "farad", "fb", "femto", "femtobarn", "femtometer", "femtosecond", "fermi", "fm", "fm2", "fm3", "fs", "g", "gauss", "gibi", "giga", "gigabecquerel", "gigaelectronvolt", "gigahertz", "gigajoule", "gigawatt", "googol", "gram", "gray", "h", "hecto", "henry", "hertz", "hour", "invab", "invfb", "invmb", "invnb", "invpb", "invub", "joule", "kBq", "kG", "kGs", "kGy", "kHz", "kJ", "kV", "kW", "keV", "kelvin", "kg", "kibi", "kilo", "kilobecquerel", "kiloelectronvolt", "kilogauss", "kilogram", "kilogray", "kilohertz", "kilojoule", "kilometer", "kilometer2", "kilometer3", "kilovolt", "kilowatt", "km", "km2", "km3", "lm", "lumen", "lux", "lx", "m", "m2", "m3", "mA", "mCi", "mF", "mGy", "mWb", "mb", "mbar", "mebi", "mega", "megabecquerel", "megaelectronvolt", "megagray", "megahertz", "megajoule", "megavolt", "megawatt", "meter", "meter2", "meter3", "mg", "micro", "microampere", "microbarn", "microcurie", "microfarad", "microgray", "micrometer", "micron", "microsecond", "microweber", "milli", "milliampere", "millibar", "millibarn", "millicurie", "millifarad", "milligram", "milligray", "millimeter", "millimeter2", "millimeter3", "milliradian", "millisecond", "milliweber", "minute", "mm", "mm2", "mm3", "mol", "mole", "mrad", "ms", "nA", "nCi", "nF", "nWb", "nano", "nanoampere", "nanobarn", "nanocurie", "nanofarad", "nanometer", "nanosecond", "nanoweber", "nb", "newton", "ns", "ohm", "pF", "pascal", "pb", "pebi", "peta", "petaelectronvolt", "pico", "picobarn", "picofarad", "picosecond", "prefixes", "ps", "quecto", "quetta", "quettagram", "rad", "radian", "rg", "ronna", "ronnagram", "ronto", "rontogram", "s", "second", "sievert", "sr", "steradian", "tebi", "tera", "teraelectronvolt", "terahertz", "tesla", "uA", "uCi", "uF", "uGy", "uWb", "ub", "units", "us", "volt", "watt", "weber", "y", "year", "yobi", "yocto", "yoctosecond", "yotta", "ys", "zebi", "zepto", "zeptosecond", "zetta", "zettaelectronvolt", "zs", ) def __dir__() -> List[str]: return list(__all__) hepunits-2.4.2/src/hepunits/units/prefixes.py000066400000000000000000000036201507114616700213560ustar00rootroot00000000000000# Licensed under a 3-clause BSD style license, see LICENSE. """ Common unit prefixes ==================== There are 2 types of prefixes: * SI prefixes [SI_prefixes]_. * Binary prefixes [Binary_prefixes]_. **References** .. [SI_prefixes] https://en.wikipedia.org/wiki/International_System_of_Units#Prefixes. .. [Binary_prefixes] https://en.wikipedia.org/wiki/Unit_prefix#Binary_prefixes. """ __all__ = ( "atto", "centi", "deca", "deci", "exa", "exbi", "femto", "gibi", "giga", "googol", "hecto", "kibi", "kilo", "mebi", "mega", "micro", "milli", "nano", "pebi", "peta", "pico", "quecto", "quetta", "ronna", "ronto", "tebi", "tera", "yobi", "yocto", "yotta", "zebi", "zepto", "zetta", ) # ----------------------------------------------------------------------------- # SI prefixes # ----------------------------------------------------------------------------- from typing import List quetta = 1.0e30 ronna = 1.0e27 yotta = 1.0e24 zetta = 1.0e21 exa = 1.0e18 peta = 1.0e15 tera = 1.0e12 giga = 1.0e9 mega = 1.0e6 kilo = 1.0e3 hecto = 1.0e2 deca = 1.0e1 deci = 1.0e-1 centi = 1.0e-2 milli = 1.0e-3 micro = 1.0e-6 nano = 1.0e-9 pico = 1.0e-12 femto = 1.0e-15 atto = 1.0e-18 zepto = 1.0e-21 yocto = 1.0e-24 ronto = 1.0e-27 quecto = 1.0e-30 # ----------------------------------------------------------------------------- # Binary prefixes # ----------------------------------------------------------------------------- kibi = 2.0**10 mebi = 2.0**20 gibi = 2.0**30 tebi = 2.0**40 pebi = 2.0**50 exbi = 2.0**60 zebi = 2.0**70 yobi = 2.0**80 # ----------------------------------------------------------------------------- # Miscellaneous prefixes # ----------------------------------------------------------------------------- googol = 1.0e100 def __dir__() -> List[str]: return list(__all__) hepunits-2.4.2/src/hepunits/units/units.py000066400000000000000000000323051507114616700206750ustar00rootroot00000000000000# Licensed under a 3-clause BSD style license, see LICENSE. """ Units in the HEP system of units ================================ This module `hepunits.units` contains 2 submodules for: * Basic physical units and derived units (`hepunits.units.units`). * Commonly-used unit prefixes (`hepunits.units.prefixes`). All units are provided in the HEP System of Units as defined below. In HEP the System of Units consists of the basic units originally defined by the [CLHEP]_ project: =================== ================== ==== Quantity Name Unit =================== ================== ==== Length millimeter mm Time nanosecond ns Energy Mega electron Volt MeV Positron charge eplus Temperature kelvin K Amount of substance mole mol Luminous intensity candela cd Plane angle radian rad Solid angle steradian sr =================== ================== ==== It is largely based on the international system of units ([SI]_) =================== ======== ==== Quantity Name Unit =================== ======== ==== Length meter m Time second s Mass kilogram kg Electric current ampere A Temperature kelvin K Amount of substance mole mol Luminous intensity candela cd =================== ======== ==== but augments it with handy definitions, changing the basic length and time units. Typical use cases:: >>> # add two quantities with length units and get the result in meters >>> from hepunits import units as u >>> (1 * u.meter + 5 * u.cm) / u.meter 1.05 >>> from hepunits.units import MeV, GeV >>> massWindow = 100 * MeV # define a 100 MeV mass window >>> def energy_resolution(): ... # returns the energy resolution of 100 MeV ... return 100 * MeV ... >>> energy_resolution() / GeV # get the energy resolution in GeV 0.1 **References** .. [CLHEP] http://proj-clhep.web.cern.ch/proj-clhep/. .. [SI] http://www.physics.nist.gov/cuu/Units/index.html. """ from math import pi as _pi from typing import List from . import prefixes as _pre # -------------------------------------------------------------------- # Units of length # --------------- millimeter = 1.0 millimeter2 = millimeter * millimeter millimeter3 = millimeter * millimeter * millimeter mm = millimeter mm2 = millimeter2 mm3 = millimeter3 meter = _pre.kilo * millimeter meter2 = meter * meter meter3 = meter * meter * meter m = meter m2 = meter2 m3 = meter3 centimeter = _pre.centi * meter centimeter2 = centimeter * centimeter centimeter3 = centimeter * centimeter * centimeter cm = centimeter cm2 = centimeter2 cm3 = centimeter3 kilometer = _pre.kilo * meter kilometer2 = kilometer * kilometer kilometer3 = kilometer * kilometer * kilometer km = kilometer km2 = kilometer2 km3 = kilometer3 micrometer = _pre.micro * meter micron = micrometer nanometer = _pre.nano * meter angstrom = 1e-10 * meter femtometer = _pre.femto * meter fermi = femtometer fm = femtometer fm2 = femtometer * femtometer fm3 = femtometer * femtometer * femtometer barn = 1.0e-28 * meter2 millibarn = _pre.milli * barn microbarn = _pre.micro * barn nanobarn = _pre.nano * barn picobarn = _pre.pico * barn femtobarn = _pre.femto * barn attobarn = _pre.atto * barn mb = millibarn ub = microbarn nb = nanobarn pb = picobarn fb = femtobarn ab = attobarn invmb = 1.0 / millibarn invub = 1.0 / microbarn invnb = 1.0 / nanobarn invpb = 1.0 / picobarn invfb = 1.0 / femtobarn invab = 1.0 / attobarn # -------------------------------------------------------------------- # Units of time # -------------------------------------------------------------------- nanosecond = 1.0 ns = nanosecond second = _pre.giga * nanosecond millisecond = _pre.milli * second microsecond = _pre.micro * second picosecond = _pre.pico * second femtosecond = _pre.femto * second attosecond = _pre.atto * second zeptosecond = _pre.zepto * second yoctosecond = _pre.yocto * second s = second ms = millisecond us = microsecond ps = picosecond fs = femtosecond # shortcut "as = attosecond" not possible since "as" is a reserved word ;-) zs = zeptosecond ys = yoctosecond minute = 60 * second hour = 60 * minute day = 24 * hour year = 365.25 * day h = hour d = day y = year hertz = 1.0 / second kilohertz = _pre.kilo * hertz megahertz = _pre.mega * hertz gigahertz = _pre.giga * hertz terahertz = _pre.tera * hertz Hz = hertz kHz = kilohertz MHz = megahertz GHz = gigahertz THz = terahertz # -------------------------------------------------------------------- # Units of energy # -------------------------------------------------------------------- megaelectronvolt = 1.0 electronvolt = _pre.micro * megaelectronvolt zettaelectronvolt = _pre.zetta * electronvolt exaelectronvolt = _pre.exa * electronvolt petaelectronvolt = _pre.peta * electronvolt teraelectronvolt = _pre.tera * electronvolt gigaelectronvolt = _pre.giga * electronvolt kiloelectronvolt = _pre.kilo * electronvolt ZeV = zettaelectronvolt EeV = exaelectronvolt PeV = petaelectronvolt TeV = teraelectronvolt GeV = gigaelectronvolt MeV = megaelectronvolt keV = kiloelectronvolt eV = electronvolt electronvolt2 = electronvolt * electronvolt eV2 = electronvolt2 # -------------------------------------------------------------------- # Units of electric charge # -------------------------------------------------------------------- eplus = 1.0 # positron charge # -------------------------------------------------------------------- # Units of temperature # -------------------------------------------------------------------- kelvin = 1.0 K = kelvin # -------------------------------------------------------------------- # Units of amount of substance # -------------------------------------------------------------------- mole = 1.0 mol = mole # -------------------------------------------------------------------- # Units of luminous intensity # -------------------------------------------------------------------- candela = 1.0 cd = candela # -------------------------------------------------------------------- # Units of angles # -------------------------------------------------------------------- radian = 1.0 # plane angle steradian = 1.0 # solid angle rad = radian sr = steradian milliradian = _pre.milli * radian mrad = milliradian degree = (_pi / 180.0) * radian deg = degree # -------------------------------------------------------------------- # Derived units # -------------------------------------------------------------------- # Positron charge e_SI = 1.602176634e-19 # exact value, taken from PDG 2022 # Electric charge # -------------------------------------------------------------------- coulomb = eplus / e_SI C = coulomb # Electric current ampere = coulomb / second milliampere = _pre.milli * ampere microampere = _pre.micro * ampere nanoampere = _pre.nano * ampere A = ampere mA = milliampere uA = microampere nA = nanoampere # Energy joule = electronvolt / e_SI # joule = 6.24151e+12 * MeV gigajoule = _pre.giga * joule megajoule = _pre.mega * joule kilojoule = _pre.kilo * joule J = joule GJ = gigajoule MJ = megajoule kJ = kilojoule erg = 1.0e-7 * joule # Power watt = joule / second gigawatt = _pre.giga * watt megawatt = _pre.mega * watt kilowatt = _pre.kilo * watt W = watt GW = gigawatt MW = megawatt kW = kilowatt # Force newton = joule / meter N = newton dyne = 1.0e-5 * newton # Pressure pascal = newton / meter2 Pa = pascal bar = 1.0e5 * pascal millibar = _pre.milli * bar mbar = millibar atmosphere = 101325.0 * pascal atm = atmosphere # Mass [E][T^2][L^-2] kilogram = joule * second * second / meter2 gram = _pre.milli * kilogram quettagram = _pre.quetta * gram ronnagram = _pre.ronna * gram milligram = _pre.milli * gram rontogram = _pre.ronto * gram Qg = quettagram Rg = ronnagram kg = kilogram g = gram mg = milligram rg = rontogram # Electric potential megavolt = megaelectronvolt / eplus volt = _pre.micro * megavolt kilovolt = _pre.kilo * volt MV = megavolt kV = kilovolt V = volt # Electric capacitance farad = coulomb / volt millifarad = _pre.milli * farad microfarad = _pre.micro * farad nanofarad = _pre.nano * farad picofarad = _pre.pico * farad F = farad mF = millifarad uF = microfarad nF = nanofarad pF = picofarad # Electric resistance ohm = volt / ampere # Magnetic Field tesla = volt * second / meter2 T = tesla gauss = 1.0e-4 * tesla kilogauss = _pre.kilo * gauss G = gauss Gs = gauss kG = kilogauss kGs = kilogauss # Magnetic Flux weber = volt * second milliweber = _pre.milli * weber microweber = _pre.micro * weber nanoweber = _pre.nano * weber Wb = weber mWb = milliweber uWb = microweber nWb = nanoweber # Inductance henry = weber / ampere H = henry # -------------------------------------------------------------------- # Units derived from luminous intensity # -------------------------------------------------------------------- # Luminous flux lumen = candela * steradian lm = lumen # Illuminance, i.e. amount of luminous flux per unit area lux = lumen / meter2 lx = lux # -------------------------------------------------------------------- # Units for radiation # -------------------------------------------------------------------- # Activity becquerel = 1.0 / second kilobecquerel = _pre.kilo * becquerel megabecquerel = _pre.mega * becquerel gigabecquerel = _pre.giga * becquerel Bq = becquerel kBq = kilobecquerel MBq = megabecquerel GBq = gigabecquerel curie = 3.7e10 * becquerel millicurie = _pre.milli * curie microcurie = _pre.micro * curie nanocurie = _pre.nano * curie Ci = curie mCi = millicurie uCi = microcurie nCi = nanocurie # Absorbed dose gray = joule / kilogram megagray = _pre.mega * gray kilogray = _pre.kilo * gray milligray = _pre.milli * gray microgray = _pre.micro * gray Gy = gray MGy = megagray kGy = kilogray mGy = milligray uGy = microgray # Dose equivalent sievert = joule / kilogram Sv = sievert __all__ = ( "GJ", "GW", "MJ", "MV", "MW", "MW", "A", "Bq", "C", "Ci", "EeV", "F", "G", "GBq", "GHz", "GeV", "Gs", "Gy", "H", "Hz", "J", "K", "MBq", "MGy", "MHz", "MeV", "N", "Pa", "PeV", "Qg", "Rg", "Sv", "T", "THz", "TeV", "V", "W", "Wb", "ZeV", "ab", "ampere", "angstrom", "atm", "atmosphere", "attobarn", "attosecond", "bar", "barn", "becquerel", "candela", "cd", "centimeter", "centimeter2", "centimeter3", "cm", "cm2", "cm3", "coulomb", "curie", "d", "day", "deg", "degree", "dyne", "eV", "eV2", "e_SI", "electronvolt", "electronvolt2", "eplus", "erg", "exaelectronvolt", "farad", "fb", "femtobarn", "femtometer", "femtosecond", "fermi", "fm", "fm2", "fm3", "fs", "g", "gauss", "gigabecquerel", "gigaelectronvolt", "gigahertz", "gigajoule", "gigawatt", "gram", "gray", "h", "henry", "hertz", "hour", "invab", "invfb", "invmb", "invnb", "invpb", "invub", "joule", "kBq", "kG", "kGs", "kGy", "kHz", "kJ", "kV", "kW", "keV", "kelvin", "kg", "kilobecquerel", "kiloelectronvolt", "kilogauss", "kilogram", "kilogray", "kilohertz", "kilojoule", "kilometer", "kilometer2", "kilometer3", "kilovolt", "kilowatt", "km", "km2", "km3", "lm", "lumen", "lux", "lx", "m", "m2", "m3", "mA", "mCi", "mF", "mGy", "mWb", "mb", "mbar", "megabecquerel", "megaelectronvolt", "megagray", "megahertz", "megajoule", "megavolt", "megawatt", "meter", "meter2", "meter3", "mg", "microampere", "microbarn", "microcurie", "microfarad", "microgray", "micrometer", "micron", "microsecond", "microweber", "milliampere", "millibar", "millibarn", "millicurie", "millifarad", "milligram", "milligray", "millimeter", "millimeter2", "millimeter3", "milliradian", "millisecond", "milliweber", "minute", "mm", "mm2", "mm3", "mol", "mole", "mrad", "ms", "nA", "nCi", "nF", "nWb", "nanoampere", "nanobarn", "nanocurie", "nanofarad", "nanometer", "nanosecond", "nanoweber", "nb", "newton", "ns", "ohm", "pF", "pascal", "pb", "petaelectronvolt", "picobarn", "picofarad", "picosecond", "ps", "quettagram", "rad", "radian", "rg", "ronnagram", "rontogram", "s", "second", "sievert", "sr", "steradian", "teraelectronvolt", "terahertz", "tesla", "uA", "uCi", "uF", "uGy", "uWb", "ub", "us", "volt", "watt", "weber", "y", "year", "yoctosecond", "ys", "zeptosecond", "zettaelectronvolt", "zs", ) def __dir__() -> List[str]: return list(__all__) hepunits-2.4.2/tests/000077500000000000000000000000001507114616700145305ustar00rootroot00000000000000hepunits-2.4.2/tests/constants/000077500000000000000000000000001507114616700165445ustar00rootroot00000000000000hepunits-2.4.2/tests/constants/test_constants.py000066400000000000000000000011771507114616700221770ustar00rootroot00000000000000#!/usr/bin/env python # Licensed under a 3-clause BSD style license, see LICENSE. """ Tests for the hepunits.constants.constants module. """ from pytest import approx from hepunits import * from hepunits import THz, eV, nanometer, s def test_constants(): assert pi_sq == two_pi * half_pi assert Avogadro == 6.02214076e23 assert c_light / (m / s) == 299792458 assert hbarc_sq / c_light_sq == approx((h_Planck / two_pi) ** 2) assert hbar / (eV * s) == hbar / 1.0e3 # wavelength of 555-ish nanometres (green light) has a frequency of 540 THz assert c_light / (555.17121851852 * nanometer) == approx(540 * THz) hepunits-2.4.2/tests/test_missing_all.py000066400000000000000000000021451507114616700204440ustar00rootroot00000000000000import pytest import hepunits def filter_module(item: str) -> bool: if item == "__version__": return True if item.startswith("_"): return False return item not in {"List"} @pytest.mark.parametrize( "module", [ hepunits.units, hepunits.constants, hepunits.constants.constants, hepunits.units.prefixes, hepunits.units.units, ], ) def test_missing_all(module): assert set(dir(module)) == set(module.__all__) full_module = {it for it in module.__dict__ if filter_module(it)} assert full_module == set(module.__all__) def test_exported(): expr = set(hepunits.units.__all__) - (set(hepunits.__all__) | {"prefixes"}) assert not expr expr = set(hepunits.constants.__all__) - set(hepunits.__all__) assert not expr expr = set(hepunits.constants.constants.__all__) - set(hepunits.constants.__all__) assert not expr expr = set(hepunits.units.prefixes.__all__) - set(hepunits.units.__all__) assert not expr expr = set(hepunits.units.units.__all__) - set(hepunits.units.__all__) assert not expr hepunits-2.4.2/tests/test_pint.py000066400000000000000000000033331507114616700171150ustar00rootroot00000000000000#!/usr/bin/env python # Licensed under a 3-clause BSD style license, see LICENSE. """ Tests for the hepunits.pint module. """ import pint import pytest from pytest import approx import hepunits from hepunits.pint import from_clhep, to_clhep def test_pint_constants(): ureg = pint.UnitRegistry() # These three constants set the relationship between mass, length, time, and charge # TODO: check all 7 SI defining constants (hence also supporting more unit conversions) pint_c = to_clhep(1 * ureg.speed_of_light) assert pint_c == approx(hepunits.c_light, rel=1e-15) pint_h = to_clhep(1 * ureg.planck_constant) assert pint_h == approx(hepunits.h_Planck, rel=1e-15) pint_e = to_clhep(1 * ureg.elementary_charge) assert pint_e == approx(1.0, rel=1e-15) def test_pint_roundtrip(): ureg = pint.UnitRegistry() assert to_clhep(3 * ureg.mm) == approx(3.0) assert to_clhep(3 * ureg.cm) == approx(30.0) assert to_clhep(2 * ureg.ohm) == approx(2.0 * hepunits.ohm) assert to_clhep(ureg.coulomb) == approx(hepunits.coulomb) assert from_clhep(hepunits.c_light, ureg.meter / ureg.second).m == approx( (1.0 * ureg.c).to(ureg.meter / ureg.second).m, rel=1e-15 ) assert from_clhep(hepunits.tesla, ureg.tesla).m == approx( (1 * ureg.tesla).m, rel=1e-15 ) def test_unsupported_dimension(): ureg = pint.UnitRegistry() with pytest.raises(ValueError, match="Unsupported dimension"): to_clhep(1 * ureg.kelvin) def test_consistent_registry(): ureg = pint.UnitRegistry() a = 1 * hepunits.mm b = 3 * ureg.nanosecond assert a * to_clhep(b) == 3 * hepunits.mm * hepunits.nanosecond assert from_clhep(a, ureg.mm) * b == 3 * ureg.mm * ureg.nanosecond hepunits-2.4.2/tests/units/000077500000000000000000000000001507114616700156725ustar00rootroot00000000000000hepunits-2.4.2/tests/units/test_prefixes.py000066400000000000000000000007261507114616700211350ustar00rootroot00000000000000#!/usr/bin/env python # Licensed under a 3-clause BSD style license, see LICENSE. """ Tests for the hepunits.units.prefixes module. """ from math import log from pytest import approx from hepunits import kibi, mega, micro, tebi, yocto, yotta def test_prefixes_e6(): assert 4 * mega == 1.0 / 0.25 / micro def test_prefixes_e24(): assert yotta * yocto == approx(1.0) def test_prefixes_binary(): assert log(kibi, 2) == 10 assert log(tebi, 2) == 40 hepunits-2.4.2/tests/units/test_units.py000066400000000000000000000045031507114616700204470ustar00rootroot00000000000000#!/usr/bin/env python # Licensed under a 3-clause BSD style license, see LICENSE. """ Tests for the hepunits.units.units module. """ from pytest import approx from hepunits import * from hepunits import two_pi def test_length(): assert 3 * mm == approx(m * 0.003) assert 3 * mm == approx(cm * 0.3) assert 3 * mm == approx(km * 0.000003) assert 3 * mm == approx(micrometer * 3000) assert 3 * mm == approx(nanometer * 3000000) assert 3 * mm == approx(angstrom * 30000000) assert 3 * mm == approx(fermi * 3e12) def test_area(): assert (3 * mm) ** 2 == approx(9 * mm2) assert (3 * cm) ** 2 == approx(9 * cm2) assert (3 * km) ** 2 == approx(9 * km2) assert 2e-28 * meter2 == approx(2 * barn) def test_crosssection(): assert 3e-31 * barn == approx(9 * millibarn) assert 3e-34 * barn == approx(9 * microbarn) assert 3e-37 * barn == approx(9 * nanobarn) assert 3e-40 * barn == approx(9 * picobarn) assert 1 * barn == approx(100 * fm2) def test_luminosity(): assert invpb / invfb == approx(1.0e-3) def test_volume(): assert (3 * mm) ** 3 == approx(27 * mm3) assert (3 * cm) ** 3 == approx(27 * cm3) assert (3 * m) ** 3 == approx(27 * m3) def test_time(): assert 3 * ns == approx(3e-9 * s) assert 3 * ns == approx(3e-6 * ms) assert 3 * ns == approx(3e-3 * microsecond) assert 3 * ns == approx(3000 * picosecond) assert 3 * ns == approx(3000000 * femtosecond) assert day == 24 * 60 * minute def test_frequency(): assert second**-1 == approx(Hz) assert 1000 * hertz == approx(kHz) assert 1000000 * hertz == approx(MHz) assert 1000000000 * hertz == approx(GHz) def test_energy(): assert 1e3 * eV == approx(keV) assert 1e6 * eV == approx(MeV) assert 1e9 * eV == approx(GeV) assert 1e12 * eV == approx(TeV) assert 1e15 * eV == approx(PeV) assert 1e18 * eV == approx(EeV) def test_angle(): assert 360.0 * degree == two_pi * radian def test_magnetic_field(): assert 10 * gauss == approx(1 * milli * tesla) def test_electricity(): assert 1 * mega * joule / second == approx(1 * MW) def test_radiation_units(): assert gray == sievert # equal in terms of value assert 1 * curie == 37 * giga * becquerel def test_magnetic_flux(): assert weber == tesla * meter2 assert weber == ohm * coulomb