Skip to content

Commit

Permalink
pyre.unit incorporation (#7)
Browse files Browse the repository at this point in the history
* pyre.unit incorporation

* port singleton over
---------

Co-authored-by: Kyle Ma <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 18, 2024
1 parent 7a74f35 commit f257789
Show file tree
Hide file tree
Showing 22 changed files with 997 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/histogram/_units.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import numpy as N
from pyre.units import unit, parser, length
from histogram.utils.units import length, unit, parser
import sys


Expand Down
36 changes: 36 additions & 0 deletions src/histogram/utils/Singleton.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env python
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# Michael A.G. Aivazis
# California Institute of Technology
# (C) 1998-2005 All Rights Reserved
#
# <LicenseText>
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#


# adapted from GvR's Singleton implementation


class Singleton(object):
def __new__(cls, *args, **kwds):
it = cls.__dict__.get("__it__")
if it is not None:
return it

cls.__it__ = it = object.__new__(cls)
it.init(*args, **kwds)
return it

def init(self, *args, **kwds):
"""constructor substitute for initializing the singleton"""
return


# version
__id__ = "$Id: Singleton.py,v 1.1.1.1 2006-11-27 00:10:08 aivazis Exp $"

# End of file
96 changes: 96 additions & 0 deletions src/histogram/utils/units/SI.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/usr/bin/env python
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# Michael A.G. Aivazis
# California Institute of Technology
# (C) 1998-2005 All Rights Reserved
#
# <LicenseText>
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#

from .unit import unit, dimensionless


# basic SI units

meter = unit(1.0, (1, 0, 0, 0, 0, 0, 0))
kilogram = unit(1.0, (0, 1, 0, 0, 0, 0, 0))
second = unit(1.0, (0, 0, 1, 0, 0, 0, 0))
ampere = unit(1.0, (0, 0, 0, 1, 0, 0, 0))
kelvin = unit(1.0, (0, 0, 0, 0, 1, 0, 0))
mole = unit(1.0, (0, 0, 0, 0, 0, 1, 0))
candela = unit(1.0, (0, 0, 0, 0, 0, 0, 1))

# the 22 derived SI units with special names

radian = dimensionless # plane angle
steradian = dimensionless # solid angle

hertz = 1 / second # frequency

newton = meter * kilogram / second**2 # force
pascal = newton / meter**2 # pressure
joule = newton * meter # work, heat
watt = joule / second # power, radiant flux

coulomb = ampere * second # electric charge
volt = watt / ampere # electric potential difference
farad = coulomb / volt # capacitance
ohm = volt / ampere # electric resistance
siemens = ampere / volt # electric conductance
weber = volt * second # magnetic flux
tesla = weber / meter**2 # magnetic flux density
henry = weber / ampere # inductance

celcius = kelvin # Celcius temperature

lumen = candela * steradian # luminus flux
lux = lumen / meter**2 # illuminance

becquerel = 1 / second # radioactivity
gray = joule / kilogram # absorbed dose
sievert = joule / kilogram # dose equivalent
katal = mole / second # catalytic activity

# prefixes

yotta = 1e24
zetta = 1e21
exa = 1e18
peta = 1e15
tera = 1e12
giga = 1e9
mega = 1e6
kilo = 1000
hecto = 100
deka = 10
deci = 0.1
centi = 0.01
milli = 0.001
micro = 1e-6
nano = 1e-9
pico = 1e-12
femto = 1e-15
atto = 1e-18
zepto = 1e-21
yocto = 1e-24

# binary prefixes
# Thanks to Elaine Chapin for pointing them out

kibi = 2**10
mebi = kibi**2
gibi = kibi**3
tebi = kibi**4
pebi = kibi**5
exbi = kibi**6


# version
__id__ = "$Id: SI.py,v 1.1.1.1 2006-11-27 00:10:06 aivazis Exp $"

#
# End of file
28 changes: 28 additions & 0 deletions src/histogram/utils/units/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env python
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# Michael A.G. Aivazis
# California Institute of Technology
# (C) 1998-2005 All Rights Reserved
#
# <LicenseText>
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#

"""
See physics.nist.gov/ccu/Units for details
"""


def parser():
from . import unitparser

return unitparser.parser()


# version
__id__ = "$Id: __init__.py,v 1.1.1.1 2006-11-27 00:10:06 aivazis Exp $"

# End of file
28 changes: 28 additions & 0 deletions src/histogram/utils/units/angle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env python
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# Michael A.G. Aivazis
# California Institute of Technology
# (C) 1998-2005 All Rights Reserved
#
# <LicenseText>
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#

from math import pi
from .SI import radian

degree = pi / 180 * radian
arcminute = degree / 60
arcsecond = arcminute / 60

deg = degree
rad = radian

# version
__id__ = "$Id: angle.py,v 1.1.1.1 2006-11-27 00:10:08 aivazis Exp $"

#
# End of file
38 changes: 38 additions & 0 deletions src/histogram/utils/units/area.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env python
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# Michael A.G. Aivazis
# California Institute of Technology
# (C) 1998-2005 All Rights Reserved
#
# <LicenseText>
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#

from .length import meter, centimeter, inch, foot, mile


#
# Definitions of common area units
# Data taken from Appendix F of Halliday, Resnick, Walker, "Fundamentals of Physics",
# fourth edition, John Willey and Sons, 1993

square_meter = meter**2
square_centimeter = centimeter**2

square_foot = foot**2
square_inch = inch**2
square_mile = mile**2

acre = 43560 * square_foot
hectare = 10000 * square_meter

barn = 1e-28 * square_meter

# version
__id__ = "$Id: area.py,v 1.1.1.1 2006-11-27 00:10:08 aivazis Exp $"

#
# End of file
21 changes: 21 additions & 0 deletions src/histogram/utils/units/density.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env python
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# Michael A.G. Aivazis
# California Institute of Technology
# (C) 1998-2005 All Rights Reserved
#
# <LicenseText>
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#

from .mass import *
from .volume import *

# version
__id__ = "$Id: density.py,v 1.1.1.1 2006-11-27 00:10:08 aivazis Exp $"

#
# End of file
61 changes: 61 additions & 0 deletions src/histogram/utils/units/energy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/env python
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# Michael A.G. Aivazis
# California Institute of Technology
# (C) 1998-2005 All Rights Reserved
#
# <LicenseText>
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#

from .SI import joule, kilo, mega, giga, milli


#
# Definitions of common energy units
#
# Data taken from
#
# Appendix F of Halliday, Resnick, Walker, "Fundamentals of Physics",
# fourth edition, John Willey and Sons, 1993
#
# The NIST Reference on Constants, Units and Uncertainty,
# http://physics.nist.gov/cuu
#


Btu = 1055 * joule
erg = 1e-7 * joule
foot_pound = 1.356 * joule
horse_power_hour = 2.685e6 * joule

calorie = 4.1858 * joule
Calorie = 1000 * calorie
kilowatt_hour = 3.6e6 * joule

electron_volt = 1.60218e-19 * joule


# aliases

J = joule
kJ = kilo * joule
MJ = mega * joule

eV = electron_volt
meV = milli * eV
MeV = mega * eV
GeV = giga * eV

cal = calorie
kcal = kilo * calorie


# version
__id__ = "$Id: energy.py,v 1.1.1.1 2006-11-27 00:10:08 aivazis Exp $"

#
# End of file
25 changes: 25 additions & 0 deletions src/histogram/utils/units/force.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env python
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# Michael A.G. Aivazis
# California Institute of Technology
# (C) 1998-2005 All Rights Reserved
#
# <LicenseText>
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#


from .SI import meter, second


gal = 0.01 * meter / second**2


# version
__id__ = "$Id: force.py,v 1.1.1.1 2006-11-27 00:10:08 aivazis Exp $"

#
# End of file
Loading

0 comments on commit f257789

Please sign in to comment.