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

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

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

revision 121 by jgs, Fri May 6 04:26:16 2005 UTC revision 148 by jgs, Tue Aug 23 01:24:31 2005 UTC
# Line 8  provides a some tools related to PDEs cu Line 8  provides a some tools related to PDEs cu
8    
9  """  """
10    
11  from esys.escript import *  from escript import *
12  from esys.linearPDEs import LinearPDE  from linearPDEs import LinearPDE
13  import numarray  import numarray
14    
15  class Projector:  class Projector:
# Line 23  class Projector: Line 23  class Projector:
23      @param fast Flag to use a fast method based on matrix lumping (default is true)      @param fast Flag to use a fast method based on matrix lumping (default is true)
24      """      """
25      self.__pde = LinearPDE(domain)      self.__pde = LinearPDE(domain)
26      self.__pde.setLumping(fast)      if fast:
27          self.__pde.setSolverMethod(LinearPDE.LUMPING)
28      self.__pde.setSymmetryOn()      self.__pde.setSymmetryOn()
29      self.__pde.setReducedOrderTo(reduce)      self.__pde.setReducedOrderTo(reduce)
30      self.__pde.setValue(D = 1.)      self.__pde.setValue(D = 1.)
# Line 67  class Projector: Line 68  class Projector:
68      return out      return out
69    
70    
71  class Location:  class Locator:
72       """Location provides a factory to access the values of data objects at a given spatial coordinate x.       """
         In fact, a Location object finds the sample in the set of samples of a given function space or domain which is closest  
         to the given point x. """  
73    
74       def __init__(self,x,where):          Locator provides a  access the values of data objects at a given spatial coordinate x.
75         """initializes a Location to access values in Data objects on the Doamin or FunctionSpace where for the sample point which          In fact, a Locator object finds the sample in the set of samples of a given function space or domain where which is closest
76            to the given point x.
77    
78         """
79    
80         def __init__(self,where,x=numarray.zeros((3,))):
81           """initializes a Locator to access values in Data objects on the Doamin or FunctionSpace where for the sample point which
82            closest to the given point x"""            closest to the given point x"""
83         if isinstance(where,FunctionSpace):         if isinstance(where,FunctionSpace):
84            self.__where=where            self.__function_space=where
85         else:         else:
86            self.__where=ContinuousFunction(where)            self.__function_space=ContinuousFunction(where)
87         self.__id=length(x-self.__where.getX()).minarg()         self.__id=length(x[:self.__function_space.getDim()]-self.__function_space.getX()).mindp()
88    
89       def getValue(self,data):       def __str__(self):
90          """returns the value of data at the Location at a numarray object"""         """returns the coordinates of the Locator as a string"""
91          if isinstance(data,Data):         return "<Locator %s>"%str(self.getX())
            return data  
         else:  
            if not data.getFunctionSpace()==self.getFunctionSpace():  
              raise ValueError,"function space of data obejct does not match function space of Location"  
            else:  
              return data.getValue(self.getId())  
      def getX(self):  
         """returns the exact coordinates of the Location"""  
         return self.getValue(self.getFunctionSpace().getX())  
   
      def getId(self):  
         """returns the identifier of the location"""  
         return self.__id  
92    
93       def getFunctionSpace(self):       def getFunctionSpace(self):
94          """returns the function space of the Location"""          """returns the function space of the Locator"""
95          return self.__function_space          return self.__function_space
96    
97       def __str__(self):       def getId(self):
98         """returns the coordinates of the Location as a string"""          """returns the identifier of the location"""
99         return str(self.getX())          return self.__id
   
 def testProjector(domain):  
       """runs a few test of the Projector factory and returns the largest error plus a description of the test this error occured"""  
       error_max=0.  
       error_text=""  
       x=ContinuousFunction(domain).getX()  
       for f in [True,False]:  
          p=Projector(domain,reduce=False,fast=f)  
          for r in range(5):  
             text="range %s , fast=%s"%(r,f)  
             if r==0:  
                td_ref=x[0]  
             elif r==1:  
                td_ref=x  
             elif r==2:  
                td_ref=[[11.,12.],[21,22.]]*(x[0]+x[1])  
             elif r==3:  
                td_ref=[[[111.,112.],[121,122.]],[[211.,212.],[221,222.]]]*(x[0]+x[1])  
             elif r==3:  
                td_ref=[[[[1111.,1112.],[1121,1122.]],[[1211.,1212.],[1221,1222.]]],[[[2111.,2112.],[2121,2122.]],[[2211.,2212.],[2221,2222.]]]]*(x[0]+x[1])  
             td=p(td_ref.interpolate(Function(domain)))  
             er=Lsup(td-td_ref)/Lsup(td_ref)  
             print text," error = ",er  
             if er>error_max:  
                  error_max=er  
                  error_text=text  
       return error_max,error_text  
   
   
 # this should be removed later  
 if __name__=="__main__":  
     from esys.finley import Rectangle  
     txt=testProjector(Rectangle(56,61))  
     print "test Projector: ",txt  
100    
101         def getX(self):
102            """returns the exact coordinates of the Locator"""
103            return self(self.getFunctionSpace().getX())
104    
105         def __call__(self,data):
106            """returns the value of data at the Locator of a Data object otherwise the object is returned."""
107            return self.getValue(data)
108    
109         def getValue(self,data):
110            """returns the value of data at the Locator if data is a Data object otherwise the object is returned."""
111            if isinstance(data,Data):
112               if data.getFunctionSpace()==self.getFunctionSpace():
113                 out=data.convertToNumArrayFromDPNo(self.getId()[0],self.getId()[1])
114               else:
115                 out=data.interpolate(self.getFunctionSpace()).convertToNumArrayFromDPNo(self.getId()[0],self.getId()[1])
116               if data.getRank()==0:
117                  return out[0]
118               else:
119                  return out
120            else:
121               return data

Legend:
Removed from v.121  
changed lines
  Added in v.148

  ViewVC Help
Powered by ViewVC 1.1.26