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

  ViewVC Help
Powered by ViewVC 1.1.26