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

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

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

trunk/escript/py_src/util.py revision 1042 by gross, Mon Mar 19 03:50:34 2007 UTC temp/escript/py_src/util.py revision 1387 by trankine, Fri Jan 11 07:45:26 2008 UTC
# Line 1  Line 1 
1    #
2  # $Id$  # $Id$
3    #
4    #######################################################
5    #
6    #           Copyright 2003-2007 by ACceSS MNRF
7    #       Copyright 2007 by University of Queensland
8    #
9    #                http://esscc.uq.edu.au
10    #        Primary Business: Queensland, Australia
11    #  Licensed under the Open Software License version 3.0
12    #     http://www.opensource.org/licenses/osl-3.0.php
13    #
14    #######################################################
15    #
16    
17  """  """
18  Utility functions for escript  Utility functions for escript
# Line 9  Utility functions for escript Line 23  Utility functions for escript
23  @var __url__: url entry point on documentation  @var __url__: url entry point on documentation
24  @var __version__: version  @var __version__: version
25  @var __date__: date of the version  @var __date__: date of the version
26    @var EPSILON: smallest positive value with 1.<1.+EPSILON
27  """  """
28                                                                                                                                                                                                                                                                                                                                                                                                            
29  __author__="Lutz Gross, l.gross@uq.edu.au"  __author__="Lutz Gross, l.gross@uq.edu.au"
# Line 27  import numarray Line 42  import numarray
42  import escript  import escript
43  import os  import os
44  from esys.escript import C_GeneralTensorProduct  from esys.escript import C_GeneralTensorProduct
45    from esys.escript import getVersion
46    
47  #=========================================================  #=========================================================
48  #   some helpers:  #   some helpers:
49  #=========================================================  #=========================================================
50    def getEpsilon():
51         #     ------------------------------------------------------------------
52         #     Compute EPSILON, the machine precision.  The call to daxpp is
53         #     inTENded to fool compilers that use extra-length registers.
54         #     31 Map 1999: Hardwire EPSILON so the debugger can step thru easily.
55         #     ------------------------------------------------------------------
56         eps    = 2.**(-12)
57         p=2.
58         while p>1.:
59                eps/=2.
60                p=1.+eps
61         return eps*2.
62    
63    EPSILON=getEpsilon()
64    
65  def getTagNames(domain):  def getTagNames(domain):
66      """      """
67      returns a list of the tag names used by the domain      returns a list of the tag names used by the domain
68    
69            
70      @param domain: a domain object      @param domain: a domain object
71      @type domain: C{escript.Domain}      @type domain: L{escript.Domain}
72      @return: a list of the tag name used by the domain.      @return: a list of the tag name used by the domain.
73      @rtype: C{list} of C{str}      @rtype: C{list} of C{str}
74      """      """
75      return [n.strip() for n in domain.showTagNames().split(",") ]      return [n.strip() for n in domain.showTagNames().split(",") ]
76        
77    def insertTagNames(domain,**kwargs):
78        """
79        inserts tag names into the domain
80    
81        @param domain: a domain object
82        @type domain: C{escript.Domain}
83        @keyword <tag name>: tag key assigned to <tag name>
84        @type <tag name>: C{int}
85        """
86        for  k in kwargs:
87             domain.setTagMap(k,kwargs[k])
88    
89    def insertTaggedValues(target,**kwargs):
90        """
91        inserts tagged values into the tagged using tag names
92    
93        @param target: data to be filled by tagged values
94        @type target: L{escript.Data}
95        @keyword <tag name>: value to be used for <tag name>
96        @type <tag name>: C{float} or {numarray.NumArray}
97        @return: C{target}
98        @rtype: L{escript.Data}
99        """
100        for k in kwargs:
101            target.setTaggedValue(k,kwargs[k])
102        return target
103    
104  def saveVTK(filename,domain=None,**data):  def saveVTK(filename,domain=None,**data):
105      """      """
106      writes a L{Data} objects into a files using the the VTK XML file format.      writes a L{Data} objects into a files using the the VTK XML file format.
# Line 51  def saveVTK(filename,domain=None,**data) Line 109  def saveVTK(filename,domain=None,**data)
109    
110         tmp=Scalar(..)         tmp=Scalar(..)
111         v=Vector(..)         v=Vector(..)
112         saveVTK("solution.xml",temperature=tmp,velovity=v)         saveVTK("solution.xml",temperature=tmp,velocity=v)
113    
114      tmp and v are written into "solution.xml" where tmp is named "temperature" and v is named "velovity"      tmp and v are written into "solution.xml" where tmp is named "temperature" and v is named "velocity"
115    
116      @param filename: file name of the output file      @param filename: file name of the output file
117      @type filename: C{str}      @type filename: C{str}
# Line 63  def saveVTK(filename,domain=None,**data) Line 121  def saveVTK(filename,domain=None,**data)
121      @type <name>: L{Data} object.      @type <name>: L{Data} object.
122      @note: The data objects have to be defined on the same domain. They may not be in the same L{FunctionSpace} but one cannot expect that all L{FunctionSpace} can be mixed. Typically, data on the boundary and data on the interior cannot be mixed.      @note: The data objects have to be defined on the same domain. They may not be in the same L{FunctionSpace} but one cannot expect that all L{FunctionSpace} can be mixed. Typically, data on the boundary and data on the interior cannot be mixed.
123      """      """
124      if domain==None:      new_data={}
125         for i in data.keys():      for n,d in data.items():
126            if not data[i].isEmpty(): domain=data[i].getFunctionSpace().getDomain()            if not d.isEmpty():
127                fs=d.getFunctionSpace()
128                domain2=fs.getDomain()
129                if fs == escript.Solution(domain2):
130                   new_data[n]=interpolate(d,escript.ContinuousFunction(domain2))
131                elif fs == escript.ReducedSolution(domain2):
132                   new_data[n]=interpolate(d,escript.ReducedContinuousFunction(domain2))
133                else:
134                   new_data[n]=d
135                if domain==None: domain=domain2
136      if domain==None:      if domain==None:
137          raise ValueError,"no domain detected."          raise ValueError,"no domain detected."
138      else:      domain.saveVTK(filename,new_data)
         domain.saveVTK(filename,data)  
139    
140  def saveDX(filename,domain=None,**data):  def saveDX(filename,domain=None,**data):
141      """      """
# Line 79  def saveDX(filename,domain=None,**data): Line 145  def saveDX(filename,domain=None,**data):
145    
146         tmp=Scalar(..)         tmp=Scalar(..)
147         v=Vector(..)         v=Vector(..)
148         saveDX("solution.dx",temperature=tmp,velovity=v)         saveDX("solution.dx",temperature=tmp,velocity=v)
149    
150      tmp and v are written into "solution.dx" where tmp is named "temperature" and v is named "velovity".      tmp and v are written into "solution.dx" where tmp is named "temperature" and v is named "velocity".
151    
152      @param filename: file name of the output file      @param filename: file name of the output file
153      @type filename: C{str}      @type filename: C{str}
# Line 91  def saveDX(filename,domain=None,**data): Line 157  def saveDX(filename,domain=None,**data):
157      @type <name>: L{Data} object.      @type <name>: L{Data} object.
158      @note: The data objects have to be defined on the same domain. They may not be in the same L{FunctionSpace} but one cannot expect that all L{FunctionSpace} can be mixed. Typically, data on the boundary and data on the interior cannot be mixed.      @note: The data objects have to be defined on the same domain. They may not be in the same L{FunctionSpace} but one cannot expect that all L{FunctionSpace} can be mixed. Typically, data on the boundary and data on the interior cannot be mixed.
159      """      """
160      if domain==None:      new_data={}
161         for i in data.keys():      for n,d in data.items():
162            if not data[i].isEmpty(): domain=data[i].getFunctionSpace().getDomain()            if not d.isEmpty():
163                fs=d.getFunctionSpace()
164                domain2=fs.getDomain()
165                if fs == escript.Solution(domain2):
166                   new_data[n]=interpolate(d,escript.ReducedContinuousFunction(domain2))
167                elif fs == escript.ReducedSolution(domain2):
168                   new_data[n]=interpolate(d,escript.ReducedContinuousFunction(domain2))
169                elif fs == escript.ContinuousFunction(domain2):
170                   new_data[n]=interpolate(d,escript.ReducedContinuousFunction(domain2))
171                else:
172                   new_data[n]=d
173                if domain==None: domain=domain2
174      if domain==None:      if domain==None:
175          raise ValueError,"no domain detected."          raise ValueError,"no domain detected."
176      else:      domain.saveDX(filename,new_data)
         domain.saveDX(filename,data)  
177    
178  def kronecker(d=3):  def kronecker(d=3):
179     """     """
# Line 3739  class Add_Symbol(DependendSymbol): Line 3815  class Add_Symbol(DependendSymbol):
3815              raise TypeError,"%s: new value is not appropriate."%str(self)              raise TypeError,"%s: new value is not appropriate."%str(self)
3816        else:        else:
3817           args=self.getSubstitutedArguments(argvals)           args=self.getSubstitutedArguments(argvals)
3818           return add(args[0],args[1])           out=add(args[0],args[1])
3819             return out
3820    
3821     def diff(self,arg):     def diff(self,arg):
3822        """        """
# Line 4880  def integrate(arg,where=None): Line 4957  def integrate(arg,where=None):
4957         else:         else:
4958            return arg._integrate()            return arg._integrate()
4959      else:      else:
4960        raise TypeError,"integrate: Unknown argument type."         arg2=escript.Data(arg,where)
4961           if arg2.getRank()==0:
4962              return arg2._integrate()[0]
4963           else:
4964              return arg2._integrate()
4965    
4966  class Integrate_Symbol(DependendSymbol):  class Integrate_Symbol(DependendSymbol):
4967     """     """
# Line 4952  class Integrate_Symbol(DependendSymbol): Line 5033  class Integrate_Symbol(DependendSymbol):
5033    
5034  def interpolate(arg,where):  def interpolate(arg,where):
5035      """      """
5036      interpolates the function into the FunctionSpace where.      interpolates the function into the FunctionSpace where. If the argument C{arg} has the requested function space
5037        C{where} no interpolation is performed and C{arg} is returned.
5038    
5039      @param arg: interpolant      @param arg: interpolant
5040      @type arg: L{escript.Data} or L{Symbol}      @type arg: L{escript.Data} or L{Symbol}
# Line 4964  def interpolate(arg,where): Line 5046  def interpolate(arg,where):
5046      if isinstance(arg,Symbol):      if isinstance(arg,Symbol):
5047         return Interpolate_Symbol(arg,where)         return Interpolate_Symbol(arg,where)
5048      else:      else:
5049         return escript.Data(arg,where)         if where == arg.getFunctionSpace():
5050              return arg
5051           else:
5052              return escript.Data(arg,where)
5053    
5054  class Interpolate_Symbol(DependendSymbol):  class Interpolate_Symbol(DependendSymbol):
5055     """     """

Legend:
Removed from v.1042  
changed lines
  Added in v.1387

  ViewVC Help
Powered by ViewVC 1.1.26