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

Diff of /trunk/escript/py_src/unitsSI.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2399 by gross, Tue Apr 28 12:04:34 2009 UTC revision 2403 by gross, Wed Apr 29 06:08:18 2009 UTC
# Line 23  __author__="Lutz Gross, l.gross@uq.edu.a Line 23  __author__="Lutz Gross, l.gross@uq.edu.a
23  ## @file unitsSI.py  ## @file unitsSI.py
24    
25  """  """
26  some tools supporting the usage of symbols.  some tools supporting physical units and conversion
27    
28  @var __author__: name of author  @var __author__: name of author
29  @var __copyright__: copyrights  @var __copyright__: copyrights
# Line 69  some tools supporting the usage of symbo Line 69  some tools supporting the usage of symbo
69  @var lb : unit of pound  @var lb : unit of pound
70  @var ton : metric ton  @var ton : metric ton
71  @var A : unit of Ampere  @var A : unit of Ampere
72  @var Hz: unit of Hertz (frequency)  @var Hz: unit of Hertz (frequenacy)
73  @var N: unit of Newton (force)  @var N: unit of Newton (force)
74  @var Pa: unit of Pascal (pressure, stress)  @var Pa: unit of Pascal (pressure, stress)
75  @var atm: unit of atmosphere (pressure)  @var atm: unit of atmosphere (pressure)
# Line 77  some tools supporting the usage of symbo Line 77  some tools supporting the usage of symbo
77  @var W: unit of Watt (power)  @var W: unit of Watt (power)
78  @var C: unit of Coulomb (electric charge)  @var C: unit of Coulomb (electric charge)
79  @var V: unit of Volt (electric potential)  @var V: unit of Volt (electric potential)
80  @var F: unit of Farad (Capacitance)  @var F: unit of Farad (capacitance)
81  @var Ohm: unit of Ohm (electric resistance)  @var Ohm: unit of Ohm (electric resistance)
82    @var K : unit of Kelvin (temperature)
83    @var Celsius: unit of Celsius (temperature)
84    @var Fahrenheit : unit of Fahrenheit (temperature)
85  @var R_Earth_equator: Earth's equatorial radius  @var R_Earth_equator: Earth's equatorial radius
86  @var R_Earth_poles: Earth's polar radius  @var R_Earth_poles: Earth's polar radius
87  @var R_Earth: Earth's radius  @var R_Earth: Earth's radius
# Line 86  some tools supporting the usage of symbo Line 89  some tools supporting the usage of symbo
89  """  """
90  class Unit(object):  class Unit(object):
91     """     """
92     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)     a general class to define a physical unit and convert from this unit to an appropriate SI unit.
93    
94       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
95       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
96       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
97       converted back to the given unit, eg. to express 0.01m in physical units of mm use 0.01/mm which will return 10.
98     """     """
99     def __init__(self, name, longname,a ,b ):     def __init__(self, name, longname,a ,b ):
100         """         """
101         initializes the unit         initializes the physical unit
102                
103         @param name: short name of the unit or prefix         @param name: short name of the physical unit or prefix
104         @type name: C{str}         @type name: C{str}
105         @param longname: long name of the unit or prefix         @param longname: long name of the physical unit or prefix
106         @type longname: C{str}         @type longname: C{str}
107         @param a: absolute value in transformation         @param a: absolute value in transformation
108         @type a: C{float}         @type a: C{float}
# Line 111  class Unit(object): Line 119  class Unit(object):
119    
120     def getName(self):     def getName(self):
121         """         """
122         Returns the name of the unit         Returns the name of the physical unit
123    
124         @return: name of the unit         @return: name of the physical unit
125         @rtype: C{str}         @rtype: C{str}
126         """         """
127         return self.__name         return self.__name
128    
129     def setName(self, name):     def setName(self, name):
130         """         """
131         Sets the name of the unit         Sets the name of the physical unit
132    
133         @param name: new name of the unit         @param name: new name of the physical unit
134         @type name: C{str}         @type name: C{str}
135         """         """
136         self.__name=name         self.__name=name
137    
138     def getLongName(self):     def getLongName(self):
139         """         """
140         Returns the long name of the unit         Returns the long name of the physical unit
141    
142         @return: name of the unit         @return: name of the physical unit
143         @rtype: C{str}         @rtype: C{str}
144         """         """
145         return self.__longname         return self.__longname
146    
147     def setLongName(self, name):     def setLongName(self, name):
148         """         """
149         Sets the long name of the unit         Sets the long name of the physical unit
150    
151         @param name: new long name of the unit         @param name: new long name of the physical unit
152         @type name: C{str}         @type name: C{str}
153         """         """
154         self.__longname=name         self.__longname=name
155    
156     def __call__(self,x):     def __call__(self,x):
157         """         """
158         Converts a value x in the unit self to SI         Converts a value x in the physical unit self to SI
159    
160         @param x: value to convert         @param x: value to convert
161         @type x: an arithmetic object         @type x: an arithmetic object
# Line 158  class Unit(object): Line 166  class Unit(object):
166         """         """
167         Performs self*other operation for two L{Unit} objects         Performs self*other operation for two L{Unit} objects
168    
169         @param other: an other unit         @param other: an other physical unit
170         @type other: L{Unit}         @type other: L{Unit}
171         @rtype: L{Unit} or C{NotImplemented}         @rtype: L{Unit} or C{NotImplemented}
172         """         """
173         if isinstance(other, Unit):         if isinstance(other, Unit):
174            a=self(other(0.))            a=self(other(0.))
175            b=(self(other(1.))-a)            b=(self(other(1.))-a)
176            if isinstance(other, PowUnit) or isinstance(self,DivUnit) or  isinstance(self, PowUnit):            if isinstance(other, _PowUnit) or isinstance(self,_DivUnit) or  isinstance(self, _PowUnit):
177              return ProdUnit(self.getName()+" "+other.getName(),self.getLongName()+"*"+other.getLongName(),a ,b)              return _ProdUnit(self.getName()+" "+other.getName(),self.getLongName()+"*"+other.getLongName(),a ,b)
178            else:            else:
179              return ProdUnit(self.getName()+other.getName(),self.getLongName()+"*"+other.getLongName(),a ,b)              return _ProdUnit(self.getName()+other.getName(),self.getLongName()+"*"+other.getLongName(),a ,b)
180         else:         else:
181            return NotImplemented            return NotImplemented
182    
# Line 176  class Unit(object): Line 184  class Unit(object):
184         """         """
185         Performs other*self operation         Performs other*self operation
186    
187         @param other: an other L{Unit} or an arithmetic object         @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
188         @type other: L{Unit}         physical unit C{self} and is converted into the corresponding SI unit.
189         @rtype: L{Unit} or an arithmetic object         @type other: L{Unit} or
190           @rtype: L{Unit} of or an arithmetic object
191         """         """
192         if isinstance(other, Unit):         if isinstance(other, Unit):
193            a=other(self(0.))            a=other(self(0.))
194            b=(other(self(1.))-a)            b=(other(self(1.))-a)
195            if isinstance(other, PowUnit) or  isinstance(self, PowUnit) or isinstance(other, DivUnit):            if isinstance(other, _PowUnit) or  isinstance(self, _PowUnit) or isinstance(other, _DivUnit):
196               return ProdUnit(other.getName()+" "+self.getName(),other.getLongName()+"*"+self.getLongName(),a ,b)               return _ProdUnit(other.getName()+" "+self.getName(),other.getLongName()+"*"+self.getLongName(),a ,b)
197            else:            else:
198               return ProdUnit(other.getName()+self.getName(),other.getLongName()+"*"+self.getLongName(),a ,b)               return _ProdUnit(other.getName()+self.getName(),other.getLongName()+"*"+self.getLongName(),a ,b)
199         else:         else:
200            return self(other)            return self(other)
201    
# Line 194  class Unit(object): Line 203  class Unit(object):
203         """         """
204         Performs self*other operation for two L{Unit} objects         Performs self*other operation for two L{Unit} objects
205    
206         @param other: an other unit         @param other: an other physical unit
207         @type other: L{Unit}         @type other: L{Unit}
208         @rtype: L{Unit} or C{NotImplemented}         @rtype: L{Unit} or C{NotImplemented}
209         """         """
210         if isinstance(other, Unit):         if isinstance(other, Unit):
211            if abs(self(0.))+abs(other(0.))>0:            if abs(self(0.))+abs(other(0.))>0:
212                raise ValueError,"Division of units requires 0 absolute values"                raise ValueError,"Division of physical units requires 0 absolute values"
213            if  isinstance(other, (ProdUnit, DivUnit)):            if  isinstance(other, (_ProdUnit, _DivUnit)):
214                # X/(c*d) or X/(c/d) requires brackets:                # X/(c*d) or X/(c/d) requires brackets:
215                return DivUnit(self.getName()+"/("+other.getName()+")",self.getLongName()+"/("+other.getLongName()+")",0 , self(1.)/other(1.))                return _DivUnit(self.getName()+"/("+other.getName()+")",self.getLongName()+"/("+other.getLongName()+")",0 , self(1.)/other(1.))
216            else:            else:
217                return DivUnit(self.getName()+"/"+other.getName(),self.getLongName()+"/"+other.getLongName(),0 , self(1.)/other(1.))                return _DivUnit(self.getName()+"/"+other.getName(),self.getLongName()+"/"+other.getLongName(),0 , self(1.)/other(1.))
218         else:         else:
219            return NotImplemented            return NotImplemented
220     def __rdiv__(self,other):     def __rdiv__(self,other):
# Line 218  class Unit(object): Line 227  class Unit(object):
227         """         """
228         if isinstance(other, Unit):         if isinstance(other, Unit):
229            if abs(self(0.))+abs(other(0.))>0:            if abs(self(0.))+abs(other(0.))>0:
230                raise ValueError,"Division of units requires 0 absolute values"                raise ValueError,"Division of physical units requires 0 absolute values"
231            if  isinstance(self, (ProdUnit, DivUnit)):            if  isinstance(self, (_ProdUnit, _DivUnit)):
232                # X/(a*b) or X/(a/b) requires brackets:                # X/(a*b) or X/(a/b) requires brackets:
233                return DivUnit(other.getName()+"/("+self.getName()+")",other.getLongName()+"/("+self.getLongName()+")",0 , other(1.)/self(1.))                return _DivUnit(other.getName()+"/("+self.getName()+")",other.getLongName()+"/("+self.getLongName()+")",0 , other(1.)/self(1.))
234            else:            else:
235                return DivUnit(other.getName()+"/"+self.getName(),other.getLongName()+"/"+self.getLongName(),0 , other(1.)/self(1.))                return _DivUnit(other.getName()+"/"+self.getName(),other.getLongName()+"/"+self.getLongName(),0 , other(1.)/self(1.))
236         else:         else:
237            return (other-self(0.))/(self(1.)-self(0.))            return (other-self(0.))/(self(1.)-self(0.))
238     def __pow__(self,other):     def __pow__(self,other):
# Line 236  class Unit(object): Line 245  class Unit(object):
245         """         """
246         if isinstance(other, float) or isinstance(other, int):         if isinstance(other, float) or isinstance(other, int):
247            if abs(self(0.))>0:            if abs(self(0.))>0:
248                raise ValueError,"Power of unit requires 0 absolute values"                raise ValueError,"Power of physical unit requires 0 absolute values"
249            if  isinstance(self, (ProdUnit, DivUnit, PowUnit)):            if  isinstance(self, (_ProdUnit, _DivUnit, _PowUnit)):
250                return PowUnit("("+self.getName()+")^%s"%other, "("+self.getLongName()+")^%s"%other, 0., self(1.)**other)                return _PowUnit("("+self.getName()+")^%s"%other, "("+self.getLongName()+")^%s"%other, 0., self(1.)**other)
251            else:            else:
252                return PowUnit(self.getName()+"^%s"%other, self.getLongName()+"^%s"%other, 0., self(1.)**other)                return _PowUnit(self.getName()+"^%s"%other, self.getLongName()+"^%s"%other, 0., self(1.)**other)
253         else:         else:
254            return NotImplemented            return NotImplemented
255    
256  class ProdUnit(Unit):  class _ProdUnit(Unit):
257      pass      pass
258  class DivUnit(Unit):  class _DivUnit(Unit):
259      pass      pass
260  class PowUnit(Unit):  class _PowUnit(Unit):
261      pass      pass
262    
263  #  #
264  #  prefixes:  #  prefixes:
265  #  #
266  Yotta=1.e24  Yotta=Unit("Y","Yotta",0.,1.e24)
267  Zetta=1.e21  Zetta=Unit("Z","Zetta",0.,1.e21)
268  Exa=1.e18  Exa=Unit("E","Exa",0.,1.e18)
269  Peta=1.e15  Peta=Unit("P","Peta",0.,1.e15)
270  Tera=1.e12  Tera=Unit("T","Tera",0.,1.e12)
271  Giga=1.e9  Giga=Unit("G","Giga",0.,1.e9)
272  Mega=1.e6  Mega=Unit("M","Mega",0.,1.e6)
273  Kilo=1.e3  Kilo=Unit("k","Kilo",0.,1.e3)
274  Hecto=1.e2  Hecto=Unit("h","Hecto",0.,1.e2)
275  Deca=1.e1  Deca=Unit("da","Deca",0.,1.e1)
276  Deci=1.e-1  one=Unit("1","1",0.,1.)
277  Centi=1.e-2  Deci=Unit("d","Deci",0.,1.e-1)
278  Milli=1.e-3  Centi=Unit("c","Centi",0.,1.e-2)
279  Micro=1.e-6  Milli=Unit("m","Milli",0.,1.e-3)
280  Nano=1.e-9  Micro=Unit("mu","Micro",0.,1.e-6)
281  Pico=1.e-12  Nano=Unit("n","Nano",0.,1.e-9)
282  Femto=1.e-15  Pico=Unit("p","Pico",0.,1.e-12)
283  Atto=1.e-18  Femto=Unit("f","Femto",0.,1.e-15)
284  Zepto=1.e-21  Atto=Unit("a","Atto",0.,1.e-18)
285  Yocto=1.e-24  Zepto=Unit("z","Zepto",0.,1.e-21)
286    Yocto=Unit("y","Yocto",0.,1.e-24)
287  #  #
288  #   length  #   length
289  #  #
290  m=1.  m=Unit("m","meter",0.,1.)
291  km=Kilo*m  km=Kilo*m
292  cm=Centi*m  cm=Centi*m
293  mm=Milli*m  mm=Milli*m
294  #  #
295  #  time  #  time
296  #  #
297  sec=1.  sec=Unit("sec","second",0.,1.)
298  minute=60.*sec  minute=Unit("min","minute",0.,60.)
299  h=60.*minute  h=Unit("h","hour",0.,60.*60.)
300  day=h*24.  day=Unit("d","day",0.,60.*60.*24.)
301  yr=day*365.2425  yr=Unit("yr","year",0.,60.*60.*24.*365.2425)
302  Myr=Mega*yr  Myr=Mega*yr
303  Gyr=Giga*yr  Gyr=Giga*yr
304  #  #
305  #  mass  #  mass
306  #  #
307  kg=1.  kg=Unit("kg","kg",0.,1.)
308  gram=Milli*kg  gram=Milli*kg
309  lb=453.59237*gram  lb=Unit("lb","pound",0.,0.45359237)
310  ton=Kilo*kg  ton=Kilo*kg
311  #  #
312  #   electric current  #   electric current
313  #  #
314  A=1.  A=Unit("A","Ampere",0.,1.)
315    #
316    #   Temperature
317    #
318    K=Unit("K","Kelvin",0.,1.)
319    Celsius=Unit("C","Celsius",273.15,1.)
320    Fahrenheit=Unit("F","Fahrenheit",459.67*5./9.,5./9.)
321  #  #
322  #  others  #  others
323  #  #
324  Hz=1./sec  Hz=one/sec
325  N = m*kg/sec**2  N = Unit("N","Newton",0.,1.)
326    Pa = Unit("Pa","Pascal",0.,1.)
327  Pa = N/m**2  atm= Unit("atm","atmosphere",0.,101325.024)
328  atm=101325.024*Pa  J = Unit("J","Joule",0.,1.)
329    W= Unit("W","Watt",0.,1.)
330  J = N*m  C=Unit("C","Coulomb",0.,1.)
331  W= J/sec  V = Unit("V","Volt",0.,1.)
332  C=sec*A  F = Unit("F","Farad",0.,1.)
333  V = W/A  Ohm=Unit("Ohm","Ohm",0.,1.)
 F = C/V  
 Ohm=V/A  
334  #  #
335  #  some constants  #  some constants
336  #  #

Legend:
Removed from v.2399  
changed lines
  Added in v.2403

  ViewVC Help
Powered by ViewVC 1.1.26