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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4507 - (show annotations)
Wed Jul 24 02:50:22 2013 UTC (6 years, 1 month ago) by jfenwick
File MIME type: text/x-python
File size: 12207 byte(s)
It begins
1
2 ##############################################################################
3 #
4 # Copyright (c) 2003-2013 by University of Queensland
5 # http://www.uq.edu.au
6 #
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 # Development until 2012 by Earth Systems Science Computational Center (ESSCC)
12 # Development since 2012 by School of Earth Sciences
13 #
14 ##############################################################################
15
16 __copyright__="""Copyright (c) 2003-2013 by University of Queensland
17 http://www.uq.edu.au
18 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 ## :file unitsSI.py
25
26 """
27 some tools supporting physical units and conversion
28
29 :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
36 :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
57 :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 :var hour : unit of hour
65 :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 :var bar: unit of bar (pressure)
78 :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 """
96 from math import pi
97 class Unit(object):
98 """
99 a general class to define a physical unit and convert from this unit to an appropriate SI unit.
100
101 `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 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 """
106 def __init__(self, name, longname,a ,b ):
107 """
108 initializes the physical unit
109
110 :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 """
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 Returns the name of the physical unit
130
131 :return: name of the physical unit
132 :rtype: ``str``
133 """
134 return self.__name
135
136 def setName(self, name):
137 """
138 Sets the name of the physical unit
139
140 :param name: new name of the physical unit
141 :type name: ``str``
142 """
143 self.__name=name
144
145 def getLongName(self):
146 """
147 Returns the long name of the physical unit
148
149 :return: name of the physical unit
150 :rtype: ``str``
151 """
152 return self.__longname
153
154 def setLongName(self, name):
155 """
156 Sets the long name of the physical unit
157
158 :param name: new long name of the physical unit
159 :type name: ``str``
160 """
161 self.__longname=name
162
163 def __call__(self,x):
164 """
165 Converts a value x in the physical unit self to SI
166
167 :param x: value to convert
168 :type x: an arithmetic object
169 """
170 return self.__b*x+self.__a
171
172 def __mul__(self,other):
173 """
174 Performs self*other operation for two `Unit` objects
175
176 :param other: an other physical unit
177 :type other: `Unit`
178 :rtype: `Unit` or ``NotImplemented``
179 """
180 if isinstance(other, Unit):
181 a=self(other(0.))
182 b=(self(other(1.))-a)
183 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 else:
186 return _ProdUnit(self.getName()+other.getName(),self.getLongName()+"*"+other.getLongName(),a ,b)
187 else:
188 return NotImplemented
189
190 def __rmul__(self,other):
191 """
192 Performs other*self operation
193
194 :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 """
198 if isinstance(other, Unit):
199 a=other(self(0.))
200 b=(other(self(1.))-a)
201 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 else:
204 return _ProdUnit(other.getName()+self.getName(),other.getLongName()+"*"+self.getLongName(),a ,b)
205 else:
206 return self(other)
207
208 #Need this for python2
209 def __div__(self, other):
210 return self.__truediv__(other)
211
212 def __truediv__(self,other):
213 """
214 Performs self*other operation for two `Unit` objects
215
216 :param other: an other physical unit
217 :type other: `Unit`
218 :rtype: `Unit` or ``NotImplemented``
219 """
220 if isinstance(other, Unit):
221 if abs(self(0.))+abs(other(0.))>0:
222 raise ValueError("Division of physical units requires 0 absolute values")
223 if isinstance(other, (_ProdUnit, _DivUnit)):
224 # X/(c*d) or X/(c/d) requires brackets:
225 return _DivUnit(self.getName()+"/("+other.getName()+")",self.getLongName()+"/("+other.getLongName()+")",0 , self(1.)/other(1.))
226 else:
227 return _DivUnit(self.getName()+"/"+other.getName(),self.getLongName()+"/"+other.getLongName(),0 , self(1.)/other(1.))
228 else:
229 return NotImplemented
230
231 #Need this for python2
232 def __rdiv__(self, other):
233 return self.__rtruediv__(other)
234
235 def __rtruediv__(self,other):
236 """
237 Performs other/self operation
238
239 :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 """
243 if isinstance(other, Unit):
244 if abs(self(0.))+abs(other(0.))>0:
245 raise ValueError("Division of physical units requires 0 absolute values")
246 if isinstance(self, (_ProdUnit, _DivUnit)):
247 # X/(a*b) or X/(a/b) requires brackets:
248 return _DivUnit(other.getName()+"/("+self.getName()+")",other.getLongName()+"/("+self.getLongName()+")",0 , other(1.)/self(1.))
249 else:
250 return _DivUnit(other.getName()+"/"+self.getName(),other.getLongName()+"/"+self.getLongName(),0 , other(1.)/self(1.))
251 else:
252 return (other-self(0.))/(self(1.)-self(0.))
253 def __pow__(self,other):
254 """
255 Performs self**other operation
256
257 :param other: an exponent
258 :type other: ``int`` or ``float``
259 :rtype: `Unit`
260 """
261 if isinstance(other, float) or isinstance(other, int):
262 if abs(self(0.))>0:
263 raise ValueError("Power of physical unit requires 0 absolute values")
264 if isinstance(self, (_ProdUnit, _DivUnit, _PowUnit)):
265 return _PowUnit("("+self.getName()+")^%s"%other, "("+self.getLongName()+")^%s"%other, 0., self(1.)**other)
266 else:
267 return _PowUnit(self.getName()+"^%s"%other, self.getLongName()+"^%s"%other, 0., self(1.)**other)
268 else:
269 return NotImplemented
270
271 class _ProdUnit(Unit):
272 pass
273 class _DivUnit(Unit):
274 pass
275 class _PowUnit(Unit):
276 pass
277
278 #
279 # prefixes:
280 #
281 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 #
303 # length
304 #
305 m=Unit("m","meter",0.,1.)
306 km=Kilo*m
307 cm=Centi*m
308 mm=Milli*m
309 liter=(Deci*m)**3
310 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 Mcf=1000. * ft**3
314 #
315 # time
316 #
317 sec=Unit("sec","second",0.,1.)
318 minute=Unit("min","minute",0.,60.)
319 hour=Unit("h","hour",0.,60.* minute)
320 h=hour
321 day=Unit("d","day",0., 24. * h)
322 yr=Unit("yr","year",0.,365.2425 * day)
323 year=yr
324 Myr=Mega*yr
325 Gyr=Giga*yr
326 #
327 # mass
328 #
329 kg=Unit("kg","kg",0.,1.)
330 gram=Milli*kg
331 lb=Unit("lb","pound",0.,0.45359237 * kg)
332 ton=Kilo*kg
333 #
334 # electric current
335 #
336 A=Unit("A","Ampere",0.,1.)
337 #
338 # 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 # others
345 #
346 Mol=Unit("mole","Mole",0.,1.)
347 Hz=one/sec
348 N = Unit("N","Newton",0.,1.)
349 Pa = Unit("Pa","Pascal",0.,1.)
350 bar=100*Kilo*Pa
351 atm= Unit("atm","atmosphere",0.,101325.024 * Pa)
352 psi= Unit("psi","Psi",0.,6894.75728034 * Pa)
353 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 RAD=Unit("RAD","rad",0.,1.)
360 DEG=Unit("Ohm","Ohm",0.,pi/180.)
361 gal=Unit("gal","galileo",0.,0.01)
362 mgal=gal*Milli
363 Tesla=V*sec/(m**2)
364 T=Tesla
365 #
366 # Derived
367 #
368 Poise= gram/cm/sec
369 cPoise=Poise*Centi
370 mPoise=Poise*Milli
371 Darcy= 9.869233e-13*m**2
372 mDarcy=Darcy*Milli
373 #
374 # some constants
375 #
376 R_Earth_equator=6378.1370*km
377 R_Earth_poles=6356.7523*km
378 R_Earth=(R_Earth_equator+R_Earth_poles)/2
379 v_light=299792458.*m/sec
380 Mu_0 = 4 * pi * 10**(-7)*V*sec/A/m
381 Magnetic_Dipole_Moment_Earth = 8.22 * 10**22*A*m**2
382 Gravitational_Constant=6.6742e-11*m**3/(kg*sec**2)
383
384

  ViewVC Help
Powered by ViewVC 1.1.26