/[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 6474 - (show annotations)
Fri Jan 20 00:12:46 2017 UTC (2 years, 3 months ago) by gross
File MIME type: text/x-python
File size: 12456 byte(s)
docstring updated.
1 ##############################################################################
2 #
3 # Copyright (c) 2003-2016 by The University of Queensland
4 # http://www.uq.edu.au
5 #
6 # Primary Business: Queensland, Australia
7 # Licensed under the Apache License, version 2.0
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Development until 2012 by Earth Systems Science Computational Center (ESSCC)
11 # Development 2012-2013 by School of Earth Sciences
12 # Development from 2014 by Centre for Geoscience Computing (GeoComp)
13 #
14 ##############################################################################
15
16 from __future__ import print_function, division
17
18 """
19
20 :synopsis: some tools supporting physical units and conversion
21
22 :var Yotta: prefix yotta, symbol: Y
23 :var Zetta: prefix zetta, symbol: Z
24 :var Exa: prefix exa, symbol: E
25 :var Peta: prefix peta, symbol: P
26 :var Tera: prefix tera, symbol: T
27 :var Giga: prefix giga, symbol: G
28 :var Mega: prefix mega, symbol: M
29 :var Kilo: prefix kilo, symbol: k
30 :var Hecto: prefix hecto, symbol: h
31 :var Deca: prefix deca, symbol: da
32 :var Deci: prefix deci, symbol: d
33 :var Centi: prefix centi, symbol: c
34 :var Milli: prefix milli, symbol: m
35 :var Micro: prefix micro, symbol: mu
36 :var Nano: prefix nano, symbol: n
37 :var Pico: prefix pico, symbol: p
38 :var Femto: prefix femto, symbol: f
39 :var Atto: prefix atto, symbol: a
40 :var Zepto: prefix zepto, symbol: z
41 :var Yocto: prefix yocto, symbol: y
42
43 :var km: unit of kilo meter
44 :var m: unit of meter
45 :var cm: unit of centi meter
46 :var mm: unit of milli meter
47 :var sec: unit of second
48 :var msec: unit of milli second
49 :var minute: unit of minute
50 :var h: unit of hour
51 :var hour: unit of hour
52 :var day: unit of day
53 :var yr: unit of year
54 :var Myr: unit of mega year
55 :var Gyr: unit of giga year
56 :var gram: unit of gram
57 :var kg: unit of kilo gram
58 :var lb: unit of pound
59 :var ton: metric ton
60 :var A: unit of Ampere
61 :var Hz: unit of Hertz (frequenacy)
62 :var N: unit of Newton (force)
63 :var Pa: unit of Pascal (pressure, stress)
64 :var bar: unit of bar (pressure)
65 :var atm: unit of atmosphere (pressure)
66 :var J: unit of Joule (energy, work)
67 :var W: unit of Watt (power)
68 :var C: unit of Coulomb (electric charge)
69 :var V: unit of Volt (electric potential)
70 :var F: unit of Farad (capacitance)
71 :var Ohm: unit of Ohm (electric resistance)
72 :var K: unit of Kelvin (temperature)
73 :var Mol: unit of Mole (temperature)
74 :var Celsius: unit of Celsius (temperature)
75 :var Fahrenheit: unit of Fahrenheit (temperature)
76 :var Poise: unit of Poise (dynamic viscosity)
77 :var R_Earth_equator: Earth's equatorial radius
78 :var R_Earth_poles: Earth's polar radius
79 :var R_Earth: Earth's radius
80 :var v_light: speed of light
81 :var pi: value of pi accurate to 10 decimal places
82 :var Gravitational_Constant: gravitational constant
83 """
84
85
86 __copyright__="""Copyright (c) 2003-2016 by The University of Queensland
87 http://www.uq.edu.au
88 Primary Business: Queensland, Australia"""
89 __license__="""Licensed under the Apache License, version 2.0
90 http://www.apache.org/licenses/LICENSE-2.0"""
91 __url__="https://launchpad.net/escript-finley"
92 __author__="Lutz Gross, l.gross@uq.edu.au"
93
94 ## :file unitsSI.py
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 ccm=cm**3
315 #
316 # time
317 #
318 sec=Unit("sec","second",0.,1.)
319 msec=Unit("msec","milli-second",0.,1e-3)
320 minute=Unit("min","minute",0.,60.)
321 hour=Unit("h","hour",0.,60.* minute)
322 h=hour
323 day=Unit("d","day",0., 24. * h)
324 yr=Unit("yr","year",0.,365.2425 * day)
325 year=yr
326 Myr=Mega*yr
327 Gyr=Giga*yr
328 #
329 # mass
330 #
331 kg=Unit("kg","kg",0.,1.)
332 gram=Milli*kg
333 lb=Unit("lb","pound",0.,0.45359237 * kg)
334 ton=Kilo*kg
335 #
336 # electric current
337 #
338 A=Unit("A","Ampere",0.,1.)
339 #
340 # Temperature
341 #
342 K=Unit("K","Kelvin",0.,1.)
343 Celsius=Unit("C","Celsius",273.15,1.)
344 Fahrenheit=Unit("F","Fahrenheit",459.67*5./9.,5./9.)
345 #
346 # others
347 #
348 Mol=Unit("mole","Mole",0.,1.)
349 Hz=one/sec
350 N = Unit("N","Newton",0.,1.)
351 Pa = Unit("Pa","Pascal",0.,1.)
352 bar=100*Kilo*Pa
353 atm= Unit("atm","atmosphere",0.,101325.024 * Pa)
354 psi= Unit("psi","Psi",0.,6894.75728034 * Pa)
355 J = Unit("J","Joule",0.,1.)
356 W= Unit("W","Watt",0.,1.)
357 C=Unit("C","Coulomb",0.,1.)
358 V = Unit("V","Volt",0.,1.)
359 F = Unit("F","Farad",0.,1.)
360 Ohm=Unit("Ohm","Ohm",0.,1.)
361 RAD=Unit("RAD","rad",0.,1.)
362 DEG=Unit("Ohm","Ohm",0.,pi/180.)
363 gal=Unit("gal","galileo",0.,0.01)
364 mgal=gal*Milli
365 Tesla=V*sec/(m**2)
366 T=Tesla
367 #
368 # Derived
369 #
370 Poise= gram/cm/sec
371 cPoise=Poise*Centi
372 mPoise=Poise*Milli
373 Darcy= 9.869233e-13*m**2
374 mDarcy=Darcy*Milli
375 #
376 # some constants
377 #
378 R_Earth_equator=6378.1370*km
379 R_Earth_poles=6356.7523*km
380 R_Earth=(R_Earth_equator+R_Earth_poles)/2
381 v_light=299792458.*m/sec
382 Mu_0 = 4 * pi * 10**(-7)*V*sec/A/m
383 Magnetic_Dipole_Moment_Earth = 8.22 * 10**22*A*m**2
384 Gravitational_Constant=6.6742e-11*m**3/(kg*sec**2)
385
386

  ViewVC Help
Powered by ViewVC 1.1.26