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

  ViewVC Help
Powered by ViewVC 1.1.26