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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.26