Skip to main content

Physical units as global variables for simple numerical propagation

Project description

physunits

Simple package with common physical unit definitions.

Installation

Add this package to your existing python environment with pip pip install physunits, or alternatively follow your distribution specific instructions.

Usage

Example 1

An example usage is:

>> from physunits.length import *
>> print(f"{1000 * cm / um:.2f} is how many microns there are in a thousand centimeters")
10000000.00 is how many microns there are in a thousand centimeters

Note that in the above, only the physunits.length module is imported (new feature in version 1.0.0), so no other untits like time units, or pressure units will be available. It is possible to import all available units, as shown in the following examples. If only a subset of the supported units is desired, manually importing them as

from physunits.length import mm, nm, um

Example 2

physunits includes the most widely used unit prefixes, but you can generate other units, for example:

>> from physunits.time_units import s
>> minute = 60 * s
>> hour = 60 * minute
>> day = 24 * hour
>> print(f'There are approximately {365*day / hour} hours in a year.')
There are approximately 8760.0 hours in a year.

Units are simple float objects meant to make physics code more readable, and physical units more tracktable. is good enough.

Another example where physunits may be useful is illustrated by the following script dealing with a voltage trace simulation:

import numpy as np
import matplotlib.pyplot as plt
from physunits import *

f = 2.12*kHz
omega = 2*np.pi*f
t = np.linspace(-670*us, 410*us, 2**10)
my_voltage = (1.5*mV) * np.sin(omega*t)

# Now plot voltage in mV vs time in ms
plt.plot(t/ms, my_voltage/mV)
plt.xlabel(r't (ms)')
plt.ylabel(r'V (mV)')
plt.show()

which outputs the following plot: example_plot

Description

All units are referenced to the SI. This means that base, derived, freedom (imperial), and other units including prefixes are always relative to the SI base units. An example where this is manifest is that when imported, the unit kg takes on the numerical value 1.0, regardless of the prefix indicating the numerical factor of 1000.

Unit Supported
length fm, pm, nm, um, mm, cm, m, km, inch, ft, yd
time ps, ns, us, ms, s, minute, hour, day, week
mass ng, ug, mg, g, kg, lb, oz
temperature nK, uK, mK, K
angle* deg, rad, mrad
frequency mHz, Hz, kHz, MHz, GHz, THz
voltage pV, nV, uV, mV, V, kV
charge C, mC, uC, nC
current nA, uA, mA, A
resistance Ohm, mOhm, kOhm, MOhm, GOhm
capacitance F, mF, uF, nF, pF
inductance H, mH, uH, nH
magnetic field strength T, mT, uT, nT, pT
energy** mJ, J, kJ, meV, eV, keV, MeV, GeV, TeV
power nW, uW, mW, W, kW, MW
pressure mPa, Pa, kPa, MPa, atm, psi, Torr, mBar, Bar
rel concentration ppm, ppb

If you want your favorite unit to be added, please open an issue, or fork and submit a pull request. physunits is always thriving to improve.

  • We all know that if there was an SI unit for angles, it would be the radian, so in physunits, it takes the value rad = 1.0 , and degrees are defined relative to it. This works nicely in all trig functions. Special thanks to chrisjbillington for pointing this out.

** While the electronvolt (eV) is not a part of the SI, the SI recognizes its use. Furthermore, the latest SI redefinition fixed the value of the electron charge constant, effectively fixing the conversion between eV and J.

Contact and support

Please report, fork, test, contribute, or create an issue directly on the project's repository.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

physunits-1.0.3.tar.gz (34.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

physunits-1.0.3-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file physunits-1.0.3.tar.gz.

File metadata

  • Download URL: physunits-1.0.3.tar.gz
  • Upload date:
  • Size: 34.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for physunits-1.0.3.tar.gz
Algorithm Hash digest
SHA256 7a24b994a7317fc953cba873f59fa7dbc24b69077f3392ea80db8a0111de484c
MD5 775539061e1b71acb89e871cd0e66ce1
BLAKE2b-256 548659239befcdc7ce2ab960375265580831dc05ab874bab01872fb18ef3fd73

See more details on using hashes here.

File details

Details for the file physunits-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: physunits-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for physunits-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 d20f373a379a29d359c2a94363186fe8e66e7d9ebaadb9c0ee3d55d0f5a46154
MD5 0926705ca9fb68250b288e36542d105f
BLAKE2b-256 40e081544e9e123e6fef7b07bf42b8a41c397a6848f93c9f15310d78201bdcaa

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page