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

  ViewVC Help
Powered by ViewVC 1.1.26