/[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 2403 - (hide annotations)
Wed Apr 29 06:08:18 2009 UTC (10 years, 5 months ago) by gross
File MIME type: text/x-python
File size: 10993 byte(s)
units support Celsius, Farenheit and kelvin now
1 gross 2398
2     ########################################################
3     #
4     # Copyright (c) 2003-2008 by University of Queensland
5     # 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     __copyright__="""Copyright (c) 2003-2008 by University of Queensland
15     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     ## @file unitsSI.py
24    
25     """
26 gross 2403 some tools supporting physical units and conversion
27 gross 2398
28     @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    
35     @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    
56     @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     @var day : unit of day
64     @var yr : unit of year
65     @var Myr : unit of mega year
66     @var Gyr : unit of giga year
67     @var gram : unit of gram
68     @var kg : unit of kilo gram
69     @var lb : unit of pound
70     @var ton : metric ton
71     @var A : unit of Ampere
72 gross 2403 @var Hz: unit of Hertz (frequenacy)
73 gross 2398 @var N: unit of Newton (force)
74     @var Pa: unit of Pascal (pressure, stress)
75     @var atm: unit of atmosphere (pressure)
76     @var J: unit of Joule (energy, work)
77     @var W: unit of Watt (power)
78     @var C: unit of Coulomb (electric charge)
79     @var V: unit of Volt (electric potential)
80 gross 2403 @var F: unit of Farad (capacitance)
81 gross 2398 @var Ohm: unit of Ohm (electric resistance)
82 gross 2403 @var K : unit of Kelvin (temperature)
83     @var Celsius: unit of Celsius (temperature)
84     @var Fahrenheit : unit of Fahrenheit (temperature)
85 gross 2398 @var R_Earth_equator: Earth's equatorial radius
86     @var R_Earth_poles: Earth's polar radius
87     @var R_Earth: Earth's radius
88     @var v_light: speed of light
89     """
90 gross 2399 class Unit(object):
91     """
92 gross 2403 a general class to define a physical unit and convert from this unit to an appropriate SI unit.
93    
94     L{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
95     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
96     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
97     converted back to the given unit, eg. to express 0.01m in physical units of mm use 0.01/mm which will return 10.
98 gross 2399 """
99     def __init__(self, name, longname,a ,b ):
100     """
101 gross 2403 initializes the physical unit
102 gross 2399
103 gross 2403 @param name: short name of the physical unit or prefix
104 gross 2399 @type name: C{str}
105 gross 2403 @param longname: long name of the physical unit or prefix
106 gross 2399 @type longname: C{str}
107     @param a: absolute value in transformation
108     @type a: C{float}
109     @param b: slop in translation
110     @type b: C{float}
111     """
112     self.setName(name)
113     self.setLongName(longname)
114     self.__a=a
115     self.__b=b
116    
117     def __str__(self):
118     return self.getName()
119    
120     def getName(self):
121     """
122 gross 2403 Returns the name of the physical unit
123 gross 2399
124 gross 2403 @return: name of the physical unit
125 gross 2399 @rtype: C{str}
126     """
127     return self.__name
128    
129     def setName(self, name):
130     """
131 gross 2403 Sets the name of the physical unit
132 gross 2399
133 gross 2403 @param name: new name of the physical unit
134 gross 2399 @type name: C{str}
135     """
136     self.__name=name
137    
138     def getLongName(self):
139     """
140 gross 2403 Returns the long name of the physical unit
141 gross 2399
142 gross 2403 @return: name of the physical unit
143 gross 2399 @rtype: C{str}
144     """
145     return self.__longname
146    
147     def setLongName(self, name):
148     """
149 gross 2403 Sets the long name of the physical unit
150 gross 2399
151 gross 2403 @param name: new long name of the physical unit
152 gross 2399 @type name: C{str}
153     """
154     self.__longname=name
155    
156     def __call__(self,x):
157     """
158 gross 2403 Converts a value x in the physical unit self to SI
159 gross 2399
160     @param x: value to convert
161     @type x: an arithmetic object
162     """
163     return self.__b*x+self.__a
164    
165     def __mul__(self,other):
166     """
167     Performs self*other operation for two L{Unit} objects
168    
169 gross 2403 @param other: an other physical unit
170 gross 2399 @type other: L{Unit}
171     @rtype: L{Unit} or C{NotImplemented}
172     """
173     if isinstance(other, Unit):
174     a=self(other(0.))
175     b=(self(other(1.))-a)
176 gross 2403 if isinstance(other, _PowUnit) or isinstance(self,_DivUnit) or isinstance(self, _PowUnit):
177     return _ProdUnit(self.getName()+" "+other.getName(),self.getLongName()+"*"+other.getLongName(),a ,b)
178 gross 2399 else:
179 gross 2403 return _ProdUnit(self.getName()+other.getName(),self.getLongName()+"*"+other.getLongName(),a ,b)
180 gross 2399 else:
181     return NotImplemented
182    
183     def __rmul__(self,other):
184     """
185     Performs other*self operation
186    
187 gross 2403 @param other: an other L{Unit} or an arithmetic object. if other is a arithmetic object such as C{float} other is assumed to be given in the
188     physical unit C{self} and is converted into the corresponding SI unit.
189     @type other: L{Unit} or
190     @rtype: L{Unit} of or an arithmetic object
191 gross 2399 """
192     if isinstance(other, Unit):
193     a=other(self(0.))
194     b=(other(self(1.))-a)
195 gross 2403 if isinstance(other, _PowUnit) or isinstance(self, _PowUnit) or isinstance(other, _DivUnit):
196     return _ProdUnit(other.getName()+" "+self.getName(),other.getLongName()+"*"+self.getLongName(),a ,b)
197 gross 2399 else:
198 gross 2403 return _ProdUnit(other.getName()+self.getName(),other.getLongName()+"*"+self.getLongName(),a ,b)
199 gross 2399 else:
200     return self(other)
201    
202     def __div__(self,other):
203     """
204     Performs self*other operation for two L{Unit} objects
205    
206 gross 2403 @param other: an other physical unit
207 gross 2399 @type other: L{Unit}
208     @rtype: L{Unit} or C{NotImplemented}
209     """
210     if isinstance(other, Unit):
211     if abs(self(0.))+abs(other(0.))>0:
212 gross 2403 raise ValueError,"Division of physical units requires 0 absolute values"
213     if isinstance(other, (_ProdUnit, _DivUnit)):
214 gross 2399 # X/(c*d) or X/(c/d) requires brackets:
215 gross 2403 return _DivUnit(self.getName()+"/("+other.getName()+")",self.getLongName()+"/("+other.getLongName()+")",0 , self(1.)/other(1.))
216 gross 2399 else:
217 gross 2403 return _DivUnit(self.getName()+"/"+other.getName(),self.getLongName()+"/"+other.getLongName(),0 , self(1.)/other(1.))
218 gross 2399 else:
219     return NotImplemented
220     def __rdiv__(self,other):
221     """
222     Performs other/self operation
223    
224     @param other: an other L{Unit} or an arithmetic object
225     @type other: L{Unit} or an arithmetic object
226     @rtype: L{Unit} or an arithmetic object
227     """
228     if isinstance(other, Unit):
229     if abs(self(0.))+abs(other(0.))>0:
230 gross 2403 raise ValueError,"Division of physical units requires 0 absolute values"
231     if isinstance(self, (_ProdUnit, _DivUnit)):
232 gross 2399 # X/(a*b) or X/(a/b) requires brackets:
233 gross 2403 return _DivUnit(other.getName()+"/("+self.getName()+")",other.getLongName()+"/("+self.getLongName()+")",0 , other(1.)/self(1.))
234 gross 2399 else:
235 gross 2403 return _DivUnit(other.getName()+"/"+self.getName(),other.getLongName()+"/"+self.getLongName(),0 , other(1.)/self(1.))
236 gross 2399 else:
237     return (other-self(0.))/(self(1.)-self(0.))
238     def __pow__(self,other):
239     """
240     Performs self**other operation
241    
242     @param other: an exponent
243     @type other: C{int} or C{float}
244     @rtype: L{Unit}
245     """
246     if isinstance(other, float) or isinstance(other, int):
247     if abs(self(0.))>0:
248 gross 2403 raise ValueError,"Power of physical unit requires 0 absolute values"
249     if isinstance(self, (_ProdUnit, _DivUnit, _PowUnit)):
250     return _PowUnit("("+self.getName()+")^%s"%other, "("+self.getLongName()+")^%s"%other, 0., self(1.)**other)
251 gross 2399 else:
252 gross 2403 return _PowUnit(self.getName()+"^%s"%other, self.getLongName()+"^%s"%other, 0., self(1.)**other)
253 gross 2399 else:
254     return NotImplemented
255    
256 gross 2403 class _ProdUnit(Unit):
257 gross 2399 pass
258 gross 2403 class _DivUnit(Unit):
259 gross 2399 pass
260 gross 2403 class _PowUnit(Unit):
261 gross 2399 pass
262 gross 2403
263 gross 2398 #
264     # prefixes:
265     #
266 gross 2403 Yotta=Unit("Y","Yotta",0.,1.e24)
267     Zetta=Unit("Z","Zetta",0.,1.e21)
268     Exa=Unit("E","Exa",0.,1.e18)
269     Peta=Unit("P","Peta",0.,1.e15)
270     Tera=Unit("T","Tera",0.,1.e12)
271     Giga=Unit("G","Giga",0.,1.e9)
272     Mega=Unit("M","Mega",0.,1.e6)
273     Kilo=Unit("k","Kilo",0.,1.e3)
274     Hecto=Unit("h","Hecto",0.,1.e2)
275     Deca=Unit("da","Deca",0.,1.e1)
276     one=Unit("1","1",0.,1.)
277     Deci=Unit("d","Deci",0.,1.e-1)
278     Centi=Unit("c","Centi",0.,1.e-2)
279     Milli=Unit("m","Milli",0.,1.e-3)
280     Micro=Unit("mu","Micro",0.,1.e-6)
281     Nano=Unit("n","Nano",0.,1.e-9)
282     Pico=Unit("p","Pico",0.,1.e-12)
283     Femto=Unit("f","Femto",0.,1.e-15)
284     Atto=Unit("a","Atto",0.,1.e-18)
285     Zepto=Unit("z","Zepto",0.,1.e-21)
286     Yocto=Unit("y","Yocto",0.,1.e-24)
287 gross 2398 #
288     # length
289     #
290 gross 2403 m=Unit("m","meter",0.,1.)
291 gross 2398 km=Kilo*m
292     cm=Centi*m
293     mm=Milli*m
294     #
295     # time
296     #
297 gross 2403 sec=Unit("sec","second",0.,1.)
298     minute=Unit("min","minute",0.,60.)
299     h=Unit("h","hour",0.,60.*60.)
300     day=Unit("d","day",0.,60.*60.*24.)
301     yr=Unit("yr","year",0.,60.*60.*24.*365.2425)
302 gross 2398 Myr=Mega*yr
303     Gyr=Giga*yr
304     #
305     # mass
306     #
307 gross 2403 kg=Unit("kg","kg",0.,1.)
308 gross 2398 gram=Milli*kg
309 gross 2403 lb=Unit("lb","pound",0.,0.45359237)
310 gross 2398 ton=Kilo*kg
311     #
312     # electric current
313     #
314 gross 2403 A=Unit("A","Ampere",0.,1.)
315 gross 2398 #
316 gross 2403 # Temperature
317     #
318     K=Unit("K","Kelvin",0.,1.)
319     Celsius=Unit("C","Celsius",273.15,1.)
320     Fahrenheit=Unit("F","Fahrenheit",459.67*5./9.,5./9.)
321     #
322 gross 2398 # others
323     #
324 gross 2403 Hz=one/sec
325     N = Unit("N","Newton",0.,1.)
326     Pa = Unit("Pa","Pascal",0.,1.)
327     atm= Unit("atm","atmosphere",0.,101325.024)
328     J = Unit("J","Joule",0.,1.)
329     W= Unit("W","Watt",0.,1.)
330     C=Unit("C","Coulomb",0.,1.)
331     V = Unit("V","Volt",0.,1.)
332     F = Unit("F","Farad",0.,1.)
333     Ohm=Unit("Ohm","Ohm",0.,1.)
334 gross 2398 #
335     # some constants
336     #
337     R_Earth_equator=6378.1370*km
338     R_Earth_poles=6356.7523*km
339     R_Earth=(R_Earth_equator+R_Earth_poles)/2
340     v_light=299792458.*m/sec

  ViewVC Help
Powered by ViewVC 1.1.26