/[escript]/trunk/escript/py_src/unitsSI.py
ViewVC logotype

Annotation of /trunk/escript/py_src/unitsSI.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3981 - (hide annotations)
Fri Sep 21 02:47:54 2012 UTC (6 years, 7 months ago) by jfenwick
File MIME type: text/x-python
File size: 11990 byte(s)
First pass of updating copyright notices
1 gross 2398
2 jfenwick 3981 ##############################################################################
3 gross 2398 #
4 jfenwick 3911 # Copyright (c) 2003-2012 by University of Queensland
5 jfenwick 3981 # http://www.uq.edu.au
6 gross 2398 #
7     # Primary Business: Queensland, Australia
8     # Licensed under the Open Software License version 3.0
9     # http://www.opensource.org/licenses/osl-3.0.php
10     #
11 jfenwick 3981 # Development until 2012 by Earth Systems Science Computational Center (ESSCC)
12     # Development since 2012 by School of Earth Sciences
13     #
14     ##############################################################################
15 gross 2398
16 jfenwick 3911 __copyright__="""Copyright (c) 2003-2012 by University of Queensland
17 jfenwick 3981 http://www.uq.edu.au
18 gross 2398 Primary Business: Queensland, Australia"""
19     __license__="""Licensed under the Open Software License version 3.0
20     http://www.opensource.org/licenses/osl-3.0.php"""
21     __url__="https://launchpad.net/escript-finley"
22     __author__="Lutz Gross, l.gross@uq.edu.au"
23    
24 jfenwick 2625 ## :file unitsSI.py
25 gross 2398
26     """
27 gross 2403 some tools supporting physical units and conversion
28 gross 2398
29 jfenwick 2625 :var __author__: name of author
30     :var __copyright__: copyrights
31     :var __license__: licence agreement
32     :var __url__: url entry point on documentation
33     :var __version__: version
34     :var __date__: date of the version
35 gross 2398
36 jfenwick 2625 :var Yotta : prefix yotta, symbol: Y
37     :var Zetta : prefix zetta, symbol: Z
38     :var Exa : prefix exa, symbol: E
39     :var Peta : prefix peta, symbol: P
40     :var Tera : prefix tera, symbol: T
41     :var Giga : prefix giga, symbol: G
42     :var Mega : prefix mega, symbol: M
43     :var Kilo : prefix kilo, symbol: k
44     :var Hecto : prefix hecto, symbol: h
45     :var Deca : prefix deca, symbol: da
46     :var Deci : prefix deci, symbol: d
47     :var Centi : prefix centi, symbol: c
48     :var Milli : prefix milli, symbol: m
49     :var Micro : prefix micro, symbol: mu
50     :var Nano : prefix nano, symbol: n
51     :var Pico : prefix pico, symbol: p
52     :var Femto : prefix femto, symbol: f
53     :var Atto : prefix atto, symbol: a
54     :var Zepto : prefix zepto, symbol: z
55     :var Yocto : prefix yocto, symbol: y
56 gross 2398
57 jfenwick 2625 :var km : unit of kilo meter
58     :var m : unit of meter
59     :var cm : unit of centi meter
60     :var mm : unit of milli meter
61     :var sec: unit of second
62     :var minute : unit of minute
63     :var h : unit of hour
64 gross 2905 :var hour : unit of hour
65 jfenwick 2625 :var day : unit of day
66     :var yr : unit of year
67     :var Myr : unit of mega year
68     :var Gyr : unit of giga year
69     :var gram : unit of gram
70     :var kg : unit of kilo gram
71     :var lb : unit of pound
72     :var ton : metric ton
73     :var A : unit of Ampere
74     :var Hz: unit of Hertz (frequenacy)
75     :var N: unit of Newton (force)
76     :var Pa: unit of Pascal (pressure, stress)
77 gross 2962 :var bar: unit of bar (pressure)
78 jfenwick 2625 :var atm: unit of atmosphere (pressure)
79     :var J: unit of Joule (energy, work)
80     :var W: unit of Watt (power)
81     :var C: unit of Coulomb (electric charge)
82     :var V: unit of Volt (electric potential)
83     :var F: unit of Farad (capacitance)
84     :var Ohm: unit of Ohm (electric resistance)
85     :var K : unit of Kelvin (temperature)
86     :var Mol : unit of Mole (temperature)
87     :var Celsius: unit of Celsius (temperature)
88     :var Fahrenheit : unit of Fahrenheit (temperature)
89     :var Poise : unit of Poise (dynamic viscosity)
90     :var R_Earth_equator: Earth's equatorial radius
91     :var R_Earth_poles: Earth's polar radius
92     :var R_Earth: Earth's radius
93     :var v_light: speed of light
94     :var pi: value of pi accurate to 10 decimal places
95 gross 2398 """
96 gross 2647 from math import pi
97 gross 2399 class Unit(object):
98     """
99 gross 2403 a general class to define a physical unit and convert from this unit to an appropriate SI unit.
100    
101 jfenwick 2625 `Unit` object have a dual purpose: Firstly physical units can be combined through *,/ and ** to form new physical units or to add prefixes such as
102 gross 2403 Milli to m to form mm=Milli*m. Moreover, a given floating point number x (or any other arithmetic object) can be converted from the physical unit to
103     the SI system, eg. 10*mm to create the value for 10mm which is the float number 0.01 in the SI system. In addition, a value in the SI unit can be
104     converted back to the given unit, eg. to express 0.01m in physical units of mm use 0.01/mm which will return 10.
105 gross 2399 """
106     def __init__(self, name, longname,a ,b ):
107     """
108 gross 2403 initializes the physical unit
109 gross 2399
110 jfenwick 2625 :param name: short name of the physical unit or prefix
111     :type name: ``str``
112     :param longname: long name of the physical unit or prefix
113     :type longname: ``str``
114     :param a: absolute value in transformation
115     :type a: ``float``
116     :param b: slop in translation
117     :type b: ``float``
118 gross 2399 """
119     self.setName(name)
120     self.setLongName(longname)
121     self.__a=a
122     self.__b=b
123    
124     def __str__(self):
125     return self.getName()
126    
127     def getName(self):
128     """
129 gross 2403 Returns the name of the physical unit
130 gross 2399
131 jfenwick 2625 :return: name of the physical unit
132     :rtype: ``str``
133 gross 2399 """
134     return self.__name
135    
136     def setName(self, name):
137     """
138 gross 2403 Sets the name of the physical unit
139 gross 2399
140 jfenwick 2625 :param name: new name of the physical unit
141     :type name: ``str``
142 gross 2399 """
143     self.__name=name
144    
145     def getLongName(self):
146     """
147 gross 2403 Returns the long name of the physical unit
148 gross 2399
149 jfenwick 2625 :return: name of the physical unit
150     :rtype: ``str``
151 gross 2399 """
152     return self.__longname
153    
154     def setLongName(self, name):
155     """
156 gross 2403 Sets the long name of the physical unit
157 gross 2399
158 jfenwick 2625 :param name: new long name of the physical unit
159     :type name: ``str``
160 gross 2399 """
161     self.__longname=name
162    
163     def __call__(self,x):
164     """
165 gross 2403 Converts a value x in the physical unit self to SI
166 gross 2399
167 jfenwick 2625 :param x: value to convert
168     :type x: an arithmetic object
169 gross 2399 """
170     return self.__b*x+self.__a
171    
172     def __mul__(self,other):
173     """
174 jfenwick 2625 Performs self*other operation for two `Unit` objects
175 gross 2399
176 jfenwick 2625 :param other: an other physical unit
177     :type other: `Unit`
178     :rtype: `Unit` or ``NotImplemented``
179 gross 2399 """
180     if isinstance(other, Unit):
181     a=self(other(0.))
182     b=(self(other(1.))-a)
183 gross 2403 if isinstance(other, _PowUnit) or isinstance(self,_DivUnit) or isinstance(self, _PowUnit):
184     return _ProdUnit(self.getName()+" "+other.getName(),self.getLongName()+"*"+other.getLongName(),a ,b)
185 gross 2399 else:
186 gross 2403 return _ProdUnit(self.getName()+other.getName(),self.getLongName()+"*"+other.getLongName(),a ,b)
187 gross 2399 else:
188     return NotImplemented
189    
190     def __rmul__(self,other):
191     """
192     Performs other*self operation
193    
194 jfenwick 2625 :param other: an other `Unit` or an arithmetic object. if other is a arithmetic object such as ``float`` other is assumed to be given in the physical unit ``self`` and is converted into the corresponding SI unit.
195     :type other: `Unit` or
196     :rtype: `Unit` of or an arithmetic object
197 gross 2399 """
198     if isinstance(other, Unit):
199     a=other(self(0.))
200     b=(other(self(1.))-a)
201 gross 2403 if isinstance(other, _PowUnit) or isinstance(self, _PowUnit) or isinstance(other, _DivUnit):
202     return _ProdUnit(other.getName()+" "+self.getName(),other.getLongName()+"*"+self.getLongName(),a ,b)
203 gross 2399 else:
204 gross 2403 return _ProdUnit(other.getName()+self.getName(),other.getLongName()+"*"+self.getLongName(),a ,b)
205 gross 2399 else:
206     return self(other)
207    
208 jfenwick 3892 #Need this for python2
209     def __div__(self, other):
210     return self.__truediv__(other)
211    
212     def __truediv__(self,other):
213 gross 2399 """
214 jfenwick 2625 Performs self*other operation for two `Unit` objects
215 gross 2399
216 jfenwick 2625 :param other: an other physical unit
217     :type other: `Unit`
218     :rtype: `Unit` or ``NotImplemented``
219 gross 2399 """
220     if isinstance(other, Unit):
221     if abs(self(0.))+abs(other(0.))>0:
222 jfenwick 3771 raise ValueError("Division of physical units requires 0 absolute values")
223 gross 2403 if isinstance(other, (_ProdUnit, _DivUnit)):
224 gross 2399 # X/(c*d) or X/(c/d) requires brackets:
225 gross 2403 return _DivUnit(self.getName()+"/("+other.getName()+")",self.getLongName()+"/("+other.getLongName()+")",0 , self(1.)/other(1.))
226 gross 2399 else:
227 gross 2403 return _DivUnit(self.getName()+"/"+other.getName(),self.getLongName()+"/"+other.getLongName(),0 , self(1.)/other(1.))
228 gross 2399 else:
229     return NotImplemented
230 jfenwick 3892
231     #Need this for python2
232     def __rdiv__(self, other):
233     return self.__rtruediv__(other)
234    
235     def __rtruediv__(self,other):
236 gross 2399 """
237     Performs other/self operation
238    
239 jfenwick 2625 :param other: an other `Unit` or an arithmetic object
240     :type other: `Unit` or an arithmetic object
241     :rtype: `Unit` or an arithmetic object
242 gross 2399 """
243     if isinstance(other, Unit):
244     if abs(self(0.))+abs(other(0.))>0:
245 jfenwick 3771 raise ValueError("Division of physical units requires 0 absolute values")
246 gross 2403 if isinstance(self, (_ProdUnit, _DivUnit)):
247 gross 2399 # X/(a*b) or X/(a/b) requires brackets:
248 gross 2403 return _DivUnit(other.getName()+"/("+self.getName()+")",other.getLongName()+"/("+self.getLongName()+")",0 , other(1.)/self(1.))
249 gross 2399 else:
250 gross 2403 return _DivUnit(other.getName()+"/"+self.getName(),other.getLongName()+"/"+self.getLongName(),0 , other(1.)/self(1.))
251 gross 2399 else:
252     return (other-self(0.))/(self(1.)-self(0.))
253     def __pow__(self,other):
254     """
255     Performs self**other operation
256    
257 jfenwick 2625 :param other: an exponent
258     :type other: ``int`` or ``float``
259     :rtype: `Unit`
260 gross 2399 """
261     if isinstance(other, float) or isinstance(other, int):
262     if abs(self(0.))>0:
263 jfenwick 3771 raise ValueError("Power of physical unit requires 0 absolute values")
264 gross 2403 if isinstance(self, (_ProdUnit, _DivUnit, _PowUnit)):
265     return _PowUnit("("+self.getName()+")^%s"%other, "("+self.getLongName()+")^%s"%other, 0., self(1.)**other)
266 gross 2399 else:
267 gross 2403 return _PowUnit(self.getName()+"^%s"%other, self.getLongName()+"^%s"%other, 0., self(1.)**other)
268 gross 2399 else:
269     return NotImplemented
270    
271 gross 2403 class _ProdUnit(Unit):
272 gross 2399 pass
273 gross 2403 class _DivUnit(Unit):
274 gross 2399 pass
275 gross 2403 class _PowUnit(Unit):
276 gross 2399 pass
277 gross 2403
278 gross 2398 #
279     # prefixes:
280     #
281 gross 2403 Yotta=Unit("Y","Yotta",0.,1.e24)
282     Zetta=Unit("Z","Zetta",0.,1.e21)
283     Exa=Unit("E","Exa",0.,1.e18)
284     Peta=Unit("P","Peta",0.,1.e15)
285     Tera=Unit("T","Tera",0.,1.e12)
286     Giga=Unit("G","Giga",0.,1.e9)
287     Mega=Unit("M","Mega",0.,1.e6)
288     Kilo=Unit("k","Kilo",0.,1.e3)
289     Hecto=Unit("h","Hecto",0.,1.e2)
290     Deca=Unit("da","Deca",0.,1.e1)
291     one=Unit("1","1",0.,1.)
292     Deci=Unit("d","Deci",0.,1.e-1)
293     Centi=Unit("c","Centi",0.,1.e-2)
294     Milli=Unit("m","Milli",0.,1.e-3)
295     Micro=Unit("mu","Micro",0.,1.e-6)
296     Nano=Unit("n","Nano",0.,1.e-9)
297     Pico=Unit("p","Pico",0.,1.e-12)
298     Femto=Unit("f","Femto",0.,1.e-15)
299     Atto=Unit("a","Atto",0.,1.e-18)
300     Zepto=Unit("z","Zepto",0.,1.e-21)
301     Yocto=Unit("y","Yocto",0.,1.e-24)
302 gross 2398 #
303     # length
304     #
305 gross 2403 m=Unit("m","meter",0.,1.)
306 gross 2398 km=Kilo*m
307     cm=Centi*m
308     mm=Milli*m
309 gross 3485 liter=(Deci*m)**3
310 gross 3648 ft=Unit("ft", "feet", 0., 0.3048 * m)
311     Barrel=Unit("bbl","barrel", 0., 0.158987294928 * m**3)
312     Mscf=Unit("MSCF", "thousand standard cubic feet", 0., 28.31685 * m**3)
313 gross 3566 Mcf=1000. * ft**3
314 gross 2398 #
315     # time
316     #
317 gross 2403 sec=Unit("sec","second",0.,1.)
318     minute=Unit("min","minute",0.,60.)
319 gross 3648 hour=Unit("h","hour",0.,60.* minute)
320 gross 2905 h=hour
321 gross 3648 day=Unit("d","day",0., 24. * h)
322     yr=Unit("yr","year",0.,365.2425 * day)
323 gross 2905 year=yr
324 gross 2398 Myr=Mega*yr
325     Gyr=Giga*yr
326     #
327     # mass
328     #
329 gross 2403 kg=Unit("kg","kg",0.,1.)
330 gross 2398 gram=Milli*kg
331 gross 3648 lb=Unit("lb","pound",0.,0.45359237 * kg)
332 gross 2398 ton=Kilo*kg
333     #
334     # electric current
335     #
336 gross 2403 A=Unit("A","Ampere",0.,1.)
337 gross 2398 #
338 gross 2403 # Temperature
339     #
340     K=Unit("K","Kelvin",0.,1.)
341     Celsius=Unit("C","Celsius",273.15,1.)
342     Fahrenheit=Unit("F","Fahrenheit",459.67*5./9.,5./9.)
343     #
344 gross 2398 # others
345     #
346 gross 2499 Mol=Unit("mole","Mole",0.,1.)
347 gross 2403 Hz=one/sec
348     N = Unit("N","Newton",0.,1.)
349     Pa = Unit("Pa","Pascal",0.,1.)
350 gross 2962 bar=100*Kilo*Pa
351 gross 3648 atm= Unit("atm","atmosphere",0.,101325.024 * Pa)
352     psi= Unit("psi","Psi",0.,6894.75728034 * Pa)
353 gross 2403 J = Unit("J","Joule",0.,1.)
354     W= Unit("W","Watt",0.,1.)
355     C=Unit("C","Coulomb",0.,1.)
356     V = Unit("V","Volt",0.,1.)
357     F = Unit("F","Farad",0.,1.)
358     Ohm=Unit("Ohm","Ohm",0.,1.)
359 gross 2647 RAD=Unit("RAD","rad",0.,1.)
360     DEG=Unit("Ohm","Ohm",0.,pi/180.)
361 gross 2398 #
362 gross 2499 # Derived
363     #
364     Poise= gram/cm/sec
365 gross 3485 cPoise=Poise*Centi
366     mPoise=Poise*Milli
367 gross 3018 Darcy= 9.869233e-13*m**2
368 gross 3485 mDarcy=Darcy*Milli
369 gross 2499 #
370 gross 2398 # some constants
371     #
372     R_Earth_equator=6378.1370*km
373     R_Earth_poles=6356.7523*km
374     R_Earth=(R_Earth_equator+R_Earth_poles)/2
375     v_light=299792458.*m/sec
376 gross 3485

  ViewVC Help
Powered by ViewVC 1.1.26