#########################
'''mks.py - physical units for calculations
Physical quantities are represented here as dictionaries. One key
is 'coefficient', the other keys are metric units like 'm', 'sec',
'kg'. The value for each dimension key is the power to which that unit
is raised. For instance, force is in newtons, (kg m/sec^2), so the
representation for a newton is {'sec': -2, 'kg': 1, 'm': 1,
'coefficient': 1.0}.
When quantities are multiplied or divided, the powers of units are
correctly maintained. If one unit ends up being raised to a zeroth
power, that unit is discarded in the product or ratio. If there are
no units left at all, i.e. the result is dimensionless, just return
a float.
Many physical formulas apply transcendental functions to dimensionless
ratios of quantities. For instance, the voltage across a discharging
capacitor is exp(-t/(R*C)), where t, R, and C have dimensions
(seconds, ohms, and farads), but the ratio is dimensionless. So
returning a float in such cases is a desirable behavior.
There are also some handy units and conversions at the bottom of the
file.'''
coeff = 'coefficient'
UnitsMismatch = 'UnitsMismatch'
class Quantity:
def __init__(self,stuff,units=None):
if units == None:
stuff = stuff.copy()
c = stuff[coeff]
del stuff[coeff]
self.stuff = stuff
else:
c = 1. * stuff
self.stuff = units.copy()
for k in self.stuff.keys():
if self.stuff[k] == 0:
del self.stuff[k]
self.stuff[coeff] = c
def __repr__(self):
str = '<%g' % self.stuff[coeff]
for k in self.stuff.keys():
if k != coeff:
str = str + ' ' + k
if self.stuff[k] != 1:
str = str + '^' + `self.stuff[k]`
return str + '>'
def __add__(self,other):
self.unitsMatch(other)
units = self.stuff.copy()
del units[coeff]
return Quantity(self.stuff[coeff] + other.stuff[coeff],
units)
def __sub__(self,other):
self.unitsMatch(other)
units = self.stuff.copy()
del units[coeff]
return Quantity(self.stuff[coeff] - other.stuff[coeff],
units)
def __mul__(self,other):
c = 1. * self.stuff[coeff] * other.stuff[coeff]
u = self.multUnits(other, 1)
if u.keys() == [ ]:
return c
else:
return Quantity(c, u)
def __rmul__(self,other):
a = self.stuff.copy()
a[coeff] = other * a[coeff]
return Quantity(a)
def __div__(self,other):
try:
c = 1. * self.stuff[coeff] / other.stuff[coeff]
u = self.multUnits(other, -1)
except AttributeError:
c = 1. * self.stuff[coeff] / other
u = self.stuff.copy()
del u[coeff]
if u.keys() == [ ]:
return c
else:
return Quantity(c, u)
def __rdiv__(self,other):
a = self.stuff.copy()
b = other / a[coeff]
del a[coeff]
for k in a.keys():
a[k] = -a[k]
return Quantity(b, a)
def multUnits(self,other,n):
units1 = self.stuff.copy()
units2 = other.stuff.copy()
del units1[coeff]
del units2[coeff]
for k in units1.keys():
if k in units2.keys():
units1[k] = units1[k] + n * units2[k]
for k in units2.keys():
if k not in units1.keys():
units1[k] = n * units2[k]
for k in units1.keys():
if units1[k] == 0:
del units1[k]
return units1
def unitsMatch(self,other):
otherkeys = other.stuff.keys()
for k in self.stuff.keys():
if k not in otherkeys:
raise UnitsMismatch
if k != coeff and self.stuff[k] != other.stuff[k]:
raise UnitsMismatch
# Lotsa good stuff on units and measures at:
# http://aurora.rg.iupui.edu/UCUM/UCUM-tab.html
### Fundamental units
meter = Quantity(1,{'m':1})
kilogram = Quantity(1,{'kg':1})
second = Quantity(1,{'sec':1})
coulomb = Quantity(1,{'C':1})
Kilo = 1.e3
Mega = 1.e6
Giga = 1.e9
Tera = 1.e12
Centi = 1.e-2
Milli = 1.e-3
Micro = 1.e-6
Nano = 1.e-9
Pico = 1.e-12
Femto = 1.e-15
Atto = 1.e-18
### Conveniences and metric prefixes
meter2 = meter * meter
meter3 = meter2 * meter
# don't know the official metric names for these, if such exist
speed = meter / second
acceleration = speed / second
density = kilogram / meter3
liter = Milli * meter3
newton = kilogram * acceleration
joule = newton * meter
watt = joule / second
ampere = coulomb / second
volt = joule / coulomb
ohm = volt / ampere
farad = coulomb / volt
weber = volt * second
tesla = weber / meter2
pascal = newton / meter2
henry = weber / ampere
km = Kilo * meter
cm = Centi * meter
mm = Milli * meter
um = Micro * meter
nm = Nano * meter
gram = Milli * kilogram
mgram = Micro * kilogram
ugram = Nano * kilogram
msec = Milli * second
usec = Micro * second
nsec = Nano * second
psec = Pico * second
fsec = Femto * second
uF = Micro * farad
nF = Nano * farad
pF = Pico * farad
mH = Milli * henry
uH = Micro * henry
nH = Nano * henry
Kohm = Kilo * ohm
Mohm = Mega * ohm
### Some English measures
inch = .0254 * meter
foot = 12 * inch
yard = 3 * foot
furlong = 660 * foot
mile = 5280 * foot
gallon = 3.79 * liter
quart = .25 * gallon
pint = .5 * quart
cup = .5 * pint
fluid_ounce = .125 * cup
tablespoon = .5 * fluid_ounce
teaspoon = tablespoon / 3.
minute = 60 * second
hour = 60 * minute
day = 24 * hour
year = 365.25 * day
# There is some disagreement on years. There are Julian years (this is
# that), Gregorian years (365.2425), and tropical years (365.24219).
#################################
--
- - - - - - - - - - - - - - - - - - - - - - - -
Resistance is futile. Capacitance is efficacious.
Will Ware email: wware @ world.std.com
# Physical quantities with units
#
# Written by Konrad Hinsen <hin...@cnrs-orleans.fr>
# last revision: 1999-7-23
#
# hacked 1998/09/28 GPW: now removes __args__ from local dict after eval
# 1998/09/29 GPW: now supports conversions with offset
# (for temperature units)
# $Id: PhysicalQuantities.py,v 1.3.1.7 1999/01/11 21:49:36 gward Exp $
"""Physical quantities with units.
This module provides a data type that represents a physical
quantity together with its unit. It is possible to add and
subtract these quantities if the units are compatible, and
a quantity can be converted to another compatible unit.
Multiplication, subtraction, and raising to integer powers
is allowed without restriction, and the result will have
the correct unit. A quantity can be raised to a non-integer
power only if the result can be represented by integer powers
of the base units.
The values of physical constants are taken from the 1986
recommended values from CODATA. Other conversion factors
(e.g. for British units) come from various sources. I can't
guarantee for the correctness of all entries in the unit
table, so use this at your own risk!
"""
--
Bye,
Marc Saric
Max-Planck-Institut für molekulare Physiologie
Otto-Hahn-Strasse 11
44227 Dortmund
phone: +49 231 133 2168