/[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 2399 - (show annotations)
Tue Apr 28 12:04:34 2009 UTC (10 years, 4 months ago) by gross
File MIME type: text/x-python
File size: 9026 byte(s)
some improvements on units
1
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 some tools supporting the usage of symbols.
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 (frequency)
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 R_Earth_equator: Earth's equatorial radius
83 @var R_Earth_poles: Earth's polar radius
84 @var R_Earth: Earth's radius
85 @var v_light: speed of light
86 """
87 class Unit(object):
88 """
89 a general class to define a physical unit and a linear converter a+b*x to get transfert to a referene unit system (typically SI)
90 """
91 def __init__(self, name, longname,a ,b ):
92 """
93 initializes the unit
94
95 @param name: short name of the unit or prefix
96 @type name: C{str}
97 @param longname: long name of the unit or prefix
98 @type longname: C{str}
99 @param a: absolute value in transformation
100 @type a: C{float}
101 @param b: slop in translation
102 @type b: C{float}
103 """
104 self.setName(name)
105 self.setLongName(longname)
106 self.__a=a
107 self.__b=b
108
109 def __str__(self):
110 return self.getName()
111
112 def getName(self):
113 """
114 Returns the name of the unit
115
116 @return: name of the unit
117 @rtype: C{str}
118 """
119 return self.__name
120
121 def setName(self, name):
122 """
123 Sets the name of the unit
124
125 @param name: new name of the unit
126 @type name: C{str}
127 """
128 self.__name=name
129
130 def getLongName(self):
131 """
132 Returns the long name of the unit
133
134 @return: name of the unit
135 @rtype: C{str}
136 """
137 return self.__longname
138
139 def setLongName(self, name):
140 """
141 Sets the long name of the unit
142
143 @param name: new long name of the unit
144 @type name: C{str}
145 """
146 self.__longname=name
147
148 def __call__(self,x):
149 """
150 Converts a value x in the unit self to SI
151
152 @param x: value to convert
153 @type x: an arithmetic object
154 """
155 return self.__b*x+self.__a
156
157 def __mul__(self,other):
158 """
159 Performs self*other operation for two L{Unit} objects
160
161 @param other: an other unit
162 @type other: L{Unit}
163 @rtype: L{Unit} or C{NotImplemented}
164 """
165 if isinstance(other, Unit):
166 a=self(other(0.))
167 b=(self(other(1.))-a)
168 if isinstance(other, PowUnit) or isinstance(self,DivUnit) or isinstance(self, PowUnit):
169 return ProdUnit(self.getName()+" "+other.getName(),self.getLongName()+"*"+other.getLongName(),a ,b)
170 else:
171 return ProdUnit(self.getName()+other.getName(),self.getLongName()+"*"+other.getLongName(),a ,b)
172 else:
173 return NotImplemented
174
175 def __rmul__(self,other):
176 """
177 Performs other*self operation
178
179 @param other: an other L{Unit} or an arithmetic object
180 @type other: L{Unit}
181 @rtype: L{Unit} or an arithmetic object
182 """
183 if isinstance(other, Unit):
184 a=other(self(0.))
185 b=(other(self(1.))-a)
186 if isinstance(other, PowUnit) or isinstance(self, PowUnit) or isinstance(other, DivUnit):
187 return ProdUnit(other.getName()+" "+self.getName(),other.getLongName()+"*"+self.getLongName(),a ,b)
188 else:
189 return ProdUnit(other.getName()+self.getName(),other.getLongName()+"*"+self.getLongName(),a ,b)
190 else:
191 return self(other)
192
193 def __div__(self,other):
194 """
195 Performs self*other operation for two L{Unit} objects
196
197 @param other: an other unit
198 @type other: L{Unit}
199 @rtype: L{Unit} or C{NotImplemented}
200 """
201 if isinstance(other, Unit):
202 if abs(self(0.))+abs(other(0.))>0:
203 raise ValueError,"Division of units requires 0 absolute values"
204 if isinstance(other, (ProdUnit, DivUnit)):
205 # X/(c*d) or X/(c/d) requires brackets:
206 return DivUnit(self.getName()+"/("+other.getName()+")",self.getLongName()+"/("+other.getLongName()+")",0 , self(1.)/other(1.))
207 else:
208 return DivUnit(self.getName()+"/"+other.getName(),self.getLongName()+"/"+other.getLongName(),0 , self(1.)/other(1.))
209 else:
210 return NotImplemented
211 def __rdiv__(self,other):
212 """
213 Performs other/self operation
214
215 @param other: an other L{Unit} or an arithmetic object
216 @type other: L{Unit} or an arithmetic object
217 @rtype: L{Unit} or an arithmetic object
218 """
219 if isinstance(other, Unit):
220 if abs(self(0.))+abs(other(0.))>0:
221 raise ValueError,"Division of units requires 0 absolute values"
222 if isinstance(self, (ProdUnit, DivUnit)):
223 # X/(a*b) or X/(a/b) requires brackets:
224 return DivUnit(other.getName()+"/("+self.getName()+")",other.getLongName()+"/("+self.getLongName()+")",0 , other(1.)/self(1.))
225 else:
226 return DivUnit(other.getName()+"/"+self.getName(),other.getLongName()+"/"+self.getLongName(),0 , other(1.)/self(1.))
227 else:
228 return (other-self(0.))/(self(1.)-self(0.))
229 def __pow__(self,other):
230 """
231 Performs self**other operation
232
233 @param other: an exponent
234 @type other: C{int} or C{float}
235 @rtype: L{Unit}
236 """
237 if isinstance(other, float) or isinstance(other, int):
238 if abs(self(0.))>0:
239 raise ValueError,"Power of unit requires 0 absolute values"
240 if isinstance(self, (ProdUnit, DivUnit, PowUnit)):
241 return PowUnit("("+self.getName()+")^%s"%other, "("+self.getLongName()+")^%s"%other, 0., self(1.)**other)
242 else:
243 return PowUnit(self.getName()+"^%s"%other, self.getLongName()+"^%s"%other, 0., self(1.)**other)
244 else:
245 return NotImplemented
246
247 class ProdUnit(Unit):
248 pass
249 class DivUnit(Unit):
250 pass
251 class PowUnit(Unit):
252 pass
253 #
254 # prefixes:
255 #
256 Yotta=1.e24
257 Zetta=1.e21
258 Exa=1.e18
259 Peta=1.e15
260 Tera=1.e12
261 Giga=1.e9
262 Mega=1.e6
263 Kilo=1.e3
264 Hecto=1.e2
265 Deca=1.e1
266 Deci=1.e-1
267 Centi=1.e-2
268 Milli=1.e-3
269 Micro=1.e-6
270 Nano=1.e-9
271 Pico=1.e-12
272 Femto=1.e-15
273 Atto=1.e-18
274 Zepto=1.e-21
275 Yocto=1.e-24
276 #
277 # length
278 #
279 m=1.
280 km=Kilo*m
281 cm=Centi*m
282 mm=Milli*m
283 #
284 # time
285 #
286 sec=1.
287 minute=60.*sec
288 h=60.*minute
289 day=h*24.
290 yr=day*365.2425
291 Myr=Mega*yr
292 Gyr=Giga*yr
293 #
294 # mass
295 #
296 kg=1.
297 gram=Milli*kg
298 lb=453.59237*gram
299 ton=Kilo*kg
300 #
301 # electric current
302 #
303 A=1.
304 #
305 # others
306 #
307 Hz=1./sec
308 N = m*kg/sec**2
309
310 Pa = N/m**2
311 atm=101325.024*Pa
312
313 J = N*m
314 W= J/sec
315 C=sec*A
316 V = W/A
317 F = C/V
318 Ohm=V/A
319 #
320 # some constants
321 #
322 R_Earth_equator=6378.1370*km
323 R_Earth_poles=6356.7523*km
324 R_Earth=(R_Earth_equator+R_Earth_poles)/2
325 v_light=299792458.*m/sec

  ViewVC Help
Powered by ViewVC 1.1.26