/[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 2414 by jfenwick, Mon Mar 30 02:13:58 2009 UTC revision 2415 by gross, Wed May 13 02:48:39 2009 UTC
# Line 40  import numarray Line 40  import numarray
40  import escript  import escript
41  import os  import os
42  from esys.escript import C_GeneralTensorProduct  from esys.escript import C_GeneralTensorProduct
43  from esys.escript import getVersion  from esys.escript import getVersion, getMPIRankWorld, getMPIWorldMax
44  from esys.escript import printParallelThreadCounts  from esys.escript import printParallelThreadCounts
45  from esys.escript import listEscriptParams  from esys.escript import listEscriptParams
46    
# Line 5861  def deviatoric(arg): Line 5861  def deviatoric(arg):
5861      """      """
5862      return arg-(trace(arg)/trace(kronecker(arg.getDomain())))*kronecker(arg.getDomain())      return arg-(trace(arg)/trace(kronecker(arg.getDomain())))*kronecker(arg.getDomain())
5863    
5864    def vol(arg):
5865        """
5866        Returns the volume or area of the oject C{arg}
5867    
5868        @param arg: a geometrical object
5869        @type arg: L{escript.FunctionSpace} or L{escript.Domain}
5870        @rtype: C{float}
5871        """
5872        if isinstance(arg,escript.Domain): arg=escript.Function(arg)
5873        return integrate(escript.Scalar(1.,arg))
5874    
5875  def diameter(domain):  def diameter(domain):
5876      """      """
5877      Returns the diameter of a domain.      Returns the diameter of a domain.
# Line 5900  def longestEdge(domain): Line 5911  def longestEdge(domain):
5911      """      """
5912      return max([v[1]-v[0] for v in boundingBox(domain) ])      return max([v[1]-v[0] for v in boundingBox(domain) ])
5913    
5914  #=============================  class FileWriter(object):
5915  #      """
5916        Interface to write data to a file. In essence this class wrappes the standart C{file} object to write data that are global in MPI
5917        to a file. In fact, data are writen on the processor with MPI rank 0 only. It is recommended to use C{FileWriter} rather than C{open} in order to write
5918        code that is running with as well as with MPI. It is save to use C{open} onder MPI to read data which are global under MPI.
5919        @var name: name of file
5920        @var mode: access mode (='w' or ='a')
5921        @var closed: True to indicate closed file
5922        @var newlines: line seperator
5923        """
5924        def __init__(self,fn,append=False,createLocalFiles=False):
5925             """
5926             Opens a file of name C{fn} for writing. If running under MPI only the first processor with rank==0
5927             will open the file and write to it. If C{createLocalFiles} each individual processor will create a file
5928             where for any processor with rank>0 the file name is extended by its rank. This option is normally only used for
5929             debug purposes.
5930    
5931             @param fn: filename.
5932             @type fn: C{str}
5933             @param append: switches on the creation of local files.
5934             @type append: C{bool}
5935             @param createLocalFiles: switches on the creation of local files.
5936             @type createLocalFiles: C{bool}
5937             """
5938             errno=0
5939             self.name=fn
5940             if append:
5941                 self.mode='a'
5942             else:
5943                 self.mode='w'
5944             self.__file=None
5945             self.closed=False
5946             self.newlines=os.linesep
5947             e=None
5948             # if not the master:
5949             if getMPIRankWorld()>0:
5950                  if createLocalFiles:
5951                      fn2=fn+".%s"%getMPIRankWorld()
5952                      try:
5953                         self.__file=open(fn2,self.mode)
5954                      except Exception, e:
5955                         errno=1
5956             else:
5957                  try:
5958                      self.__file=open(fn,self.mode)
5959                  except Exception, e:
5960                      print e.message
5961                      errno=1
5962             self.__handelerror(errno,e,"opening")
5963    
5964        def __handelerror(self,errno,e,operation):
5965             errno=getMPIWorldMax(errno)
5966             if errno>0:
5967                if e==None:
5968                   raise IOError,"Unable to access file %s in mode %s for %s."%(self.name,self.mode,operation)
5969                else:
5970                   raise IOError,e.message
5971            
5972        def close(self):
5973            """
5974            Closes the file
5975            """
5976            errno=0
5977            e=None
5978            try:
5979               if not self.__file == None:
5980                   self.__file.close()
5981            except Exception, e:
5982               errno=1
5983            self.__handelerror(errno,e,"closing")
5984            self.closed=True
5985    
5986        def flush(self):
5987            """
5988            Flush the internal I/O buffer.
5989            """
5990            errno=0
5991            e=None
5992            try:
5993               if not self.__file == None:
5994                   self.__file.flush()
5995            except Exception, e:
5996               errno=1
5997            self.__handelerror(errno,e,"flushing")
5998    
5999        def write(self,txt):
6000            """
6001            Write string C{txt} to file.
6002    
6003            @param txt: string C{txt} to be written to file
6004            @type txt: C{str}
6005            """
6006            errno=0
6007            e=None
6008            try:
6009               if not self.__file == None:
6010                   self.__file.write(txt)
6011            except Exception, e:
6012               errno=1
6013            self.__handelerror(errno,e,"writing")
6014    
6015        def writelines(self, txts):
6016            """
6017            Write the list C{txt} of strings to the file.
6018        
6019            @param txts: sequense of strings to be written to file
6020            @type txts: any iterable object producing strings
6021            @note: Note that newlines are not added. This method is equivalent to call write() for each string.
6022            """
6023            errno=0
6024            e=None
6025            try:
6026               if not self.__file == None:
6027                   self.__file.writelines(txts)
6028            except Exception, e:
6029               errno=1
6030            self.__handelerror(errno,e,"writing strings")
6031    
6032    
6033  def reorderComponents(arg,index):  def reorderComponents(arg,index):
6034      """      """

Legend:
Removed from v.2414  
changed lines
  Added in v.2415

  ViewVC Help
Powered by ViewVC 1.1.26