/[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

revision 912 by gross, Wed Dec 6 03:29:49 2006 UTC revision 1561 by ksteube, Wed May 21 06:58:52 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    from esys.escript import printParallelThreadCounts
47    
48  #=========================================================  #=========================================================
49  #   some helpers:  #   some helpers:
50  #=========================================================  #=========================================================
51    def getEpsilon():
52         #     ------------------------------------------------------------------
53         #     Compute EPSILON, the machine precision.  The call to daxpp is
54         #     inTENded to fool compilers that use extra-length registers.
55         #     31 Map 1999: Hardwire EPSILON so the debugger can step thru easily.
56         #     ------------------------------------------------------------------
57         eps    = 2.**(-12)
58         p=2.
59         while p>1.:
60                eps/=2.
61                p=1.+eps
62         return eps*2.
63    
64    EPSILON=getEpsilon()
65    
66    def getTagNames(domain):
67        """
68        returns a list of the tag names used by the domain
69    
70        
71        @param domain: a domain object
72        @type domain: L{escript.Domain}
73        @return: a list of the tag name used by the domain.
74        @rtype: C{list} of C{str}
75        """
76        return [n.strip() for n in domain.showTagNames().split(",") ]
77    
78    def insertTagNames(domain,**kwargs):
79        """
80        inserts tag names into the domain
81    
82        @param domain: a domain object
83        @type domain: C{escript.Domain}
84        @keyword <tag name>: tag key assigned to <tag name>
85        @type <tag name>: C{int}
86        """
87        for  k in kwargs:
88             domain.setTagMap(k,kwargs[k])
89    
90    def insertTaggedValues(target,**kwargs):
91        """
92        inserts tagged values into the tagged using tag names
93    
94        @param target: data to be filled by tagged values
95        @type target: L{escript.Data}
96        @keyword <tag name>: value to be used for <tag name>
97        @type <tag name>: C{float} or {numarray.NumArray}
98        @return: C{target}
99        @rtype: L{escript.Data}
100        """
101        for k in kwargs:
102            target.setTaggedValue(k,kwargs[k])
103        return target
104    
105  def saveVTK(filename,domain=None,**data):  def saveVTK(filename,domain=None,**data):
106      """      """
107      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 39  def saveVTK(filename,domain=None,**data) Line 110  def saveVTK(filename,domain=None,**data)
110    
111         tmp=Scalar(..)         tmp=Scalar(..)
112         v=Vector(..)         v=Vector(..)
113         saveVTK("solution.xml",temperature=tmp,velovity=v)         saveVTK("solution.xml",temperature=tmp,velocity=v)
114    
115      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"
116    
117      @param filename: file name of the output file      @param filename: file name of the output file
118      @type filename: C{str}      @type filename: C{str}
# Line 51  def saveVTK(filename,domain=None,**data) Line 122  def saveVTK(filename,domain=None,**data)
122      @type <name>: L{Data} object.      @type <name>: L{Data} object.
123      @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.
124      """      """
125      if domain==None:      new_data={}
126         for i in data.keys():      for n,d in data.items():
127            if not data[i].isEmpty(): domain=data[i].getFunctionSpace().getDomain()            if not d.isEmpty():
128                fs=d.getFunctionSpace()
129                domain2=fs.getDomain()
130                if fs == escript.Solution(domain2):
131                   new_data[n]=interpolate(d,escript.ContinuousFunction(domain2))
132                elif fs == escript.ReducedSolution(domain2):
133                   new_data[n]=interpolate(d,escript.ReducedContinuousFunction(domain2))
134                else:
135                   new_data[n]=d
136                if domain==None: domain=domain2
137      if domain==None:      if domain==None:
138          raise ValueError,"no domain detected."          raise ValueError,"no domain detected."
139      else:      domain.saveVTK(filename,new_data)
         domain.saveVTK(filename,data)  
140    
141  def saveDX(filename,domain=None,**data):  def saveDX(filename,domain=None,**data):
142      """      """
# Line 67  def saveDX(filename,domain=None,**data): Line 146  def saveDX(filename,domain=None,**data):
146    
147         tmp=Scalar(..)         tmp=Scalar(..)
148         v=Vector(..)         v=Vector(..)
149         saveDX("solution.dx",temperature=tmp,velovity=v)         saveDX("solution.dx",temperature=tmp,velocity=v)
150    
151      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".
152    
153      @param filename: file name of the output file      @param filename: file name of the output file
154      @type filename: C{str}      @type filename: C{str}
# Line 79  def saveDX(filename,domain=None,**data): Line 158  def saveDX(filename,domain=None,**data):
158      @type <name>: L{Data} object.      @type <name>: L{Data} object.
159      @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.
160      """      """
161      if domain==None:      new_data={}
162         for i in data.keys():      for n,d in data.items():
163            if not data[i].isEmpty(): domain=data[i].getFunctionSpace().getDomain()            if not d.isEmpty():
164                fs=d.getFunctionSpace()
165                domain2=fs.getDomain()
166                if fs == escript.Solution(domain2):
167                   new_data[n]=interpolate(d,escript.ReducedContinuousFunction(domain2))
168                elif fs == escript.ReducedSolution(domain2):
169                   new_data[n]=interpolate(d,escript.ReducedContinuousFunction(domain2))
170                elif fs == escript.ContinuousFunction(domain2):
171                   new_data[n]=interpolate(d,escript.ReducedContinuousFunction(domain2))
172                else:
173                   new_data[n]=d
174                if domain==None: domain=domain2
175      if domain==None:      if domain==None:
176          raise ValueError,"no domain detected."          raise ValueError,"no domain detected."
177      else:      domain.saveDX(filename,new_data)
         domain.saveDX(filename,data)  
178    
179  def kronecker(d=3):  def kronecker(d=3):
180     """     """
# Line 3727  class Add_Symbol(DependendSymbol): Line 3816  class Add_Symbol(DependendSymbol):
3816              raise TypeError,"%s: new value is not appropriate."%str(self)              raise TypeError,"%s: new value is not appropriate."%str(self)
3817        else:        else:
3818           args=self.getSubstitutedArguments(argvals)           args=self.getSubstitutedArguments(argvals)
3819           return add(args[0],args[1])           out=add(args[0],args[1])
3820             return out
3821    
3822     def diff(self,arg):     def diff(self,arg):
3823        """        """
# Line 4868  def integrate(arg,where=None): Line 4958  def integrate(arg,where=None):
4958         else:         else:
4959            return arg._integrate()            return arg._integrate()
4960      else:      else:
4961        raise TypeError,"integrate: Unknown argument type."         arg2=escript.Data(arg,where)
4962           if arg2.getRank()==0:
4963              return arg2._integrate()[0]
4964           else:
4965              return arg2._integrate()
4966    
4967  class Integrate_Symbol(DependendSymbol):  class Integrate_Symbol(DependendSymbol):
4968     """     """
# Line 4940  class Integrate_Symbol(DependendSymbol): Line 5034  class Integrate_Symbol(DependendSymbol):
5034    
5035  def interpolate(arg,where):  def interpolate(arg,where):
5036      """      """
5037      interpolates the function into the FunctionSpace where.      interpolates the function into the FunctionSpace where. If the argument C{arg} has the requested function space
5038        C{where} no interpolation is performed and C{arg} is returned.
5039    
5040      @param arg: interpolant      @param arg: interpolant
5041      @type arg: L{escript.Data} or L{Symbol}      @type arg: L{escript.Data} or L{Symbol}
# Line 4952  def interpolate(arg,where): Line 5047  def interpolate(arg,where):
5047      if isinstance(arg,Symbol):      if isinstance(arg,Symbol):
5048         return Interpolate_Symbol(arg,where)         return Interpolate_Symbol(arg,where)
5049      else:      else:
5050         return escript.Data(arg,where)         if where == arg.getFunctionSpace():
5051              return arg
5052           else:
5053              return escript.Data(arg,where)
5054    
5055  class Interpolate_Symbol(DependendSymbol):  class Interpolate_Symbol(DependendSymbol):
5056     """     """

Legend:
Removed from v.912  
changed lines
  Added in v.1561

  ViewVC Help
Powered by ViewVC 1.1.26