/[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 3891 by jfenwick, Thu Mar 1 05:34:52 2012 UTC revision 3892 by jfenwick, Tue Apr 10 08:57:23 2012 UTC
# Line 1135  class Symbol(object): Line 1135  class Symbol(object):
1135         """         """
1136         return quotient(self,other)         return quotient(self,other)
1137    
1138       def __truediv__(self,other):
1139           return self.__div__(other)
1140    
1141     def __rdiv__(self,other):     def __rdiv__(self,other):
1142         """         """
1143         Divides another object by this object.         Divides another object by this object.
# Line 1148  class Symbol(object): Line 1151  class Symbol(object):
1151         """         """
1152         return quotient(other,self)         return quotient(other,self)
1153    
1154       def __rtruediv__(self,other):
1155           return self.__rdiv__(other)
1156    
1157     def __pow__(self,other):     def __pow__(self,other):
1158         """         """
1159         Raises this object to the power of ``other``.         Raises this object to the power of ``other``.
# Line 1209  class DependendSymbol(Symbol): Line 1215  class DependendSymbol(Symbol):
1215            `Symbol` arguments. This will allow the optimizer to remove            `Symbol` arguments. This will allow the optimizer to remove
1216            redundant function calls.            redundant function calls.
1217     """     """
1218    
1219       def __hash__(self):
1220           return hash(self.getShape()) ^ hash(self.getDim()) ^ len(self.getArgument())
1221    
1222     def __eq__(self,other):     def __eq__(self,other):
1223        """        """
1224        Checks if ``other`` equals self.        Checks if ``other`` equals self.
# Line 4791  def maximum(*args): Line 4801  def maximum(*args):
4801          #ranked objects mixed. If the scalar was first it will get          #ranked objects mixed. If the scalar was first it will get
4802          #picked as the initial out and we have a problem,          #picked as the initial out and we have a problem,
4803          #so we swap the objects          #so we swap the objects
4804          res=a.copy()    #Deep copy of a                  res=a.copy()    #Deep copy of a
4805          res.copyWithMask(out,wherePositive(out-a))                  res.copyWithMask(out,wherePositive(out-a))
4806          out=res                  out=res
4807           else:               else:
4808                  out.copyWithMask(a,wherePositive(a-out))                  out.copyWithMask(a,wherePositive(a-out))
4809            else:            else:
4810               diff=add(a,-out)               diff=add(a,-out)
4811               temp=add(mult(out, whereNonPositive(diff)), mult(a, wherePositive(diff)))               temp=add(mult(out, whereNonPositive(diff)), mult(a, wherePositive(diff)))
4812               if isinstance(out,numpy.ndarray) and isinstance(a,numpy.ndarray):               if isinstance(out,numpy.ndarray) and isinstance(a,numpy.ndarray):
4813             # we need to convert the result to an array             # we need to convert the result to an array
4814             temp=numpy.array(temp)                             temp=numpy.array(temp)            
4815               out=temp               out=temp
4816      return out      return out
4817    
# Line 4821  def minimum(*args): Line 4831  def minimum(*args):
4831      for a in args:      for a in args:
4832         if out==None:         if out==None:
4833            #out=a*1            #out=a*1
4834        if isinstance(a, numpy.ndarray):  #Coz rank0 array * a scalar = scalar not array            if isinstance(a, numpy.ndarray):  #Coz rank0 array * a scalar = scalar not array
4835            out=a.copy()                out=a.copy()
4836        else:            else:
4837            out=a*1                out=a*1
4838         else:         else:
4839            if isinstance(out,escript.Data) and isinstance(a,escript.Data):            if isinstance(out,escript.Data) and isinstance(a,escript.Data):
4840           if out.getRank()==0 and a.getRank()>0:               if out.getRank()==0 and a.getRank()>0:
4841          #We need to consider the case where we have scalars and higher              #We need to consider the case where we have scalars and higher
4842          #ranked objects mixed. If the scalar was first it will get          #ranked objects mixed. If the scalar was first it will get
4843          #picked as the initial out and we have a problem,          #picked as the initial out and we have a problem,
4844          #so we swap the objects          #so we swap the objects
4845          res=a.copy()    #Deep copy of a                  res=a.copy()    #Deep copy of a
4846          res.copyWithMask(out,whereNegative(out-a))                  res.copyWithMask(out,whereNegative(out-a))
4847          out=res                  out=res
4848           else:               else:
4849          out.copyWithMask(a,whereNegative(a-out))                  out.copyWithMask(a,whereNegative(a-out))
4850            else:            else:
4851               diff=add(a,-out)               diff=add(a,-out)
4852               #out=add(out,mult(whereNegative(diff),diff))               #out=add(out,mult(whereNegative(diff),diff))
4853               temp=add(mult(out, whereNonNegative(diff)), mult(a, whereNegative(diff)))               temp=add(mult(out, whereNonNegative(diff)), mult(a, whereNegative(diff)))
4854               if isinstance(out,numpy.ndarray) and isinstance(a,numpy.ndarray):               if isinstance(out,numpy.ndarray) and isinstance(a,numpy.ndarray):
4855             # we need to convert the result to an array             # we need to convert the result to an array
4856             temp=numpy.array(temp)                 temp=numpy.array(temp)
4857               out=temp               out=temp
4858      return out      return out
4859    
# Line 6070  def mkDir(*pathname): Line 6080  def mkDir(*pathname):
6080      if getMPIRankWorld()==0:      if getMPIRankWorld()==0:
6081        for p in pathname:        for p in pathname:
6082         if os.path.exists(p):         if os.path.exists(p):
6083         if not os.path.isdir(p):            if not os.path.isdir(p):
6084          errno=2                  errno=2
6085                  p_fail=p                  p_fail=p
6086         else:         else:
6087            try:            try:
# Line 6082  def mkDir(*pathname): Line 6092  def mkDir(*pathname):
6092            
6093      errno=getMPIWorldMax(errno)      errno=getMPIWorldMax(errno)
6094      if errno>0:      if errno>0:
6095       if errno==2:           if errno==2:
6096              if p_fail == None:              if p_fail == None:
6097             raise IOError("Unable to create directory.")                 raise IOError("Unable to create directory.")
6098              else:              else:
6099             raise IOError("Unable to create directory %s. It already exists and is not a directory."%p_fail)                 raise IOError("Unable to create directory %s. It already exists and is not a directory."%p_fail)
6100           elif e==None:           elif e==None:
6101              if p_fail == None:              if p_fail == None:
6102             raise IOError("Unable to create directory.")                 raise IOError("Unable to create directory.")
6103              else:              else:
6104             raise IOError("Unable to create directory %s."%p_fail)                 raise IOError("Unable to create directory %s."%p_fail)
6105           else:           else:
6106              if hasattr(e,"message"):              if hasattr(e,"message"):
6107                 raise IOError(e.message)                 raise IOError(e.message)
6108              else:              else:
6109                 if p_fail == None:                 if p_fail == None:
6110                raise IOError("Unable to create directory.")                    raise IOError("Unable to create directory.")
6111                 else:                 else:
6112                raise IOError("Unable to create directory %s."%p_fail)                    raise IOError("Unable to create directory %s."%p_fail)
6113    
6114  class FileWriter(object):  class FileWriter(object):
6115      """      """
# Line 6125  class FileWriter(object): Line 6135  class FileWriter(object):
6135           :param createLocalFiles: switches on the creation of local files.           :param createLocalFiles: switches on the creation of local files.
6136           :type createLocalFiles: ``bool``           :type createLocalFiles: ``bool``
6137           """           """
6138             error=None
6139           errno=0           errno=0
6140           self.name=fn           self.name=fn
6141           if append:           if append:
# Line 6134  class FileWriter(object): Line 6145  class FileWriter(object):
6145           self.__file=None           self.__file=None
6146           self.closed=False           self.closed=False
6147           self.newlines=os.linesep           self.newlines=os.linesep
          e=None  
6148           # if not the master:           # if not the master:
6149           if getMPIRankWorld()>0:           if getMPIRankWorld()>0:
6150                if createLocalFiles:                if createLocalFiles:
# Line 6143  class FileWriter(object): Line 6153  class FileWriter(object):
6153                       self.__file=open(fn2,self.mode)                       self.__file=open(fn2,self.mode)
6154                    except Exception as e:                    except Exception as e:
6155                       errno=1                       errno=1
6156                         error=e
6157           else:           else:
6158                try:                try:
6159                    self.__file=open(fn,self.mode)                    self.__file=open(fn,self.mode)
6160                except Exception as e:                except Exception as e:
6161                    errno=1                    errno=1
6162           self.__handelerror(errno,e,"opening")                    error=e
6163             self.__handelerror(errno, error, "opening")
6164    
6165      def __handelerror(self,errno,e,operation):      def __handelerror(self,errno,e,operation):
6166           errno=getMPIWorldMax(errno)           errno=getMPIWorldMax(errno)
# Line 6233  def showEscriptParams(): Line 6245  def showEscriptParams():
6245      """      """
6246      p=listEscriptParams()      p=listEscriptParams()
6247      for name,value,desc in p:      for name,value,desc in p:
6248      print('%s (=%s): %s'%(name, value, desc))         print('%s (=%s): %s'%(name, value, desc))
6249    
6250  #Lazy related things  #Lazy related things
6251  #These are just wrappers  #These are just wrappers
# Line 6242  def resolve(arg): Line 6254  def resolve(arg):
6254     Returns the value of arg resolved.     Returns the value of arg resolved.
6255     """     """
6256     if not isinstance(arg,Data):     if not isinstance(arg,Data):
6257      raise TypeError("Can only resolve Data.")          raise TypeError("Can only resolve Data.")
6258     if arg.isLazy():     if arg.isLazy():
6259      arg.resolve()          arg.resolve()
6260     return arg     return arg
6261        
6262  def delay(arg):  def delay(arg):
# Line 6252  def delay(arg): Line 6264  def delay(arg):
6264     Returns a lazy version of arg     Returns a lazy version of arg
6265     """     """
6266     if not isinstance(arg,Data):     if not isinstance(arg,Data):
6267      raise TypeError("Can only delay Data.")           raise TypeError("Can only delay Data.")
6268     return arg.delay()     return arg.delay()
6269    
6270  def positive(arg):  def positive(arg):
# Line 6282  def condEval(f, tval, fval): Line 6294  def condEval(f, tval, fval):
6294      Wrapper to allow non-data objects to be used.      Wrapper to allow non-data objects to be used.
6295      """      """
6296      if not isinstance(tval,Data) and not isinstance(fval,Data):      if not isinstance(tval,Data) and not isinstance(fval,Data):
6297      raise TypeError("At least one of the alternatives must be a Data object.")          raise TypeError("At least one of the alternatives must be a Data object.")
6298      if isinstance(tval,Data) and isinstance(fval, Data):      if isinstance(tval,Data) and isinstance(fval, Data):
6299      return _condEval(f,tval,fval)          return _condEval(f,tval,fval)
6300      if not isinstance(fval, Data):      if not isinstance(fval, Data):
6301      return _condEval(f, tval, Data(fval, tval.getShape(), tval.getFunctionSpace()))          return _condEval(f, tval, Data(fval, tval.getShape(), tval.getFunctionSpace()))
6302      return _condEval(f, Data(fval, fval.getShape(), fval.getFunctionSpace()), fval )      return _condEval(f, Data(fval, fval.getShape(), fval.getFunctionSpace()), fval )
6303    

Legend:
Removed from v.3891  
changed lines
  Added in v.3892

  ViewVC Help
Powered by ViewVC 1.1.26