Ignore:
Timestamp:
07/30/19 07:52:21 (6 years ago)
Author:
bdef
Message:

BUG:fixing typos for py3 compatibility

File:
1 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/src/m/classes/toolkits.py

    r24082 r24115  
    33from iluasmoptions import iluasmoptions
    44from fielddisplay import fielddisplay
    5 from checkfield import checkfield
    65from issmgslsolver import issmgslsolver
    76from issmmumpssolver import issmmumpssolver
    87
     8
    99class toolkits(object):
    10         """
    11         TOOLKITS class definition
     10    """
     11    TOOLKITS class definition
    1212
    13            Usage:
    14               self=toolkits();
    15         """
     13       Usage:
     14          self=toolkits();
     15    """
    1616
    17         def __init__(self):    # {{{
    18                 #default toolkits
    19                 if IssmConfig('_HAVE_PETSC_')[0]:
    20                         #MUMPS is the default toolkits
    21                         if IssmConfig('_HAVE_MUMPS_')[0]:
    22                                 self.DefaultAnalysis          = mumpsoptions()
    23                         else:
    24                                 self.DefaultAnalysis          = iluasmoptions()
    25                 else:
    26                         if IssmConfig('_HAVE_MUMPS_')[0]:
    27                                 self.DefaultAnalysis          = issmmumpssolver()
    28                         elif IssmConfig('_HAVE_GSL_')[0]:
    29                                 self.DefaultAnalysis          = issmgslsolver()
    30                         else:
    31                                 raise IOError("ToolkitsFile error: need at least Mumps or Gsl to define issm solver type")
     17    def __init__(self):    # {{{
     18        #default toolkits
     19        if IssmConfig('_HAVE_PETSC_')[0]:
     20            #MUMPS is the default toolkits
     21            if IssmConfig('_HAVE_MUMPS_')[0]:
     22                self.DefaultAnalysis = mumpsoptions()
     23            else:
     24                self.DefaultAnalysis = iluasmoptions()
     25        else:
     26            if IssmConfig('_HAVE_MUMPS_')[0]:
     27                self.DefaultAnalysis = issmmumpssolver()
     28            elif IssmConfig('_HAVE_GSL_')[0]:
     29                self.DefaultAnalysis = issmgslsolver()
     30            else:
     31                raise IOError("ToolkitsFile error: need at least Mumps or Gsl to define issm solver type")
    3232
    33                 #Use same solver for Recovery mode
    34                 self.RecoveryAnalysis = self.DefaultAnalysis
     33        #Use same solver for Recovery mode
     34        self.RecoveryAnalysis = self.DefaultAnalysis
    3535
    36                 #The other properties are dynamic
    37         # }}}
    38         def __repr__(self):    # {{{
    39                 s ="List of toolkits options per analysis:\n\n"
    40                 for analysis in list(vars(self).keys()):
    41                         s+="%s\n" % fielddisplay(self,analysis,'')
     36        #The other properties are dynamic
     37    # }}}
     38    def __repr__(self):    # {{{
     39        s = "List of toolkits options per analysis:\n\n"
     40        for analysis in list(vars(self).keys()):
     41            s += "%s\n" % fielddisplay(self, analysis, '')
    4242
    43                 return s
    44         # }}}
    45         def addoptions(self,analysis,*args):    # {{{
    46                 # Usage example:
    47                 #    md.toolkits=addoptions(md.toolkits,'StressbalanceAnalysis',FSoptions());
    48                 #    md.toolkits=addoptions(md.toolkits,'StressbalanceAnalysis');
     43            return s
     44    # }}}
    4945
    50                 #Create dynamic property if property does not exist yet
    51                 if not hasattr(self,analysis):
    52                         setattr(self,analysis,None)
     46    def addoptions(self, analysis, *args):    # {{{
     47        # Usage example:
     48        #    md.toolkits=addoptions(md.toolkits,'StressbalanceAnalysis',FSoptions());
     49        #    md.toolkits=addoptions(md.toolkits,'StressbalanceAnalysis');
    5350
    54                 #Add toolkits options to analysis
    55                 if len(args)==1:
    56                         setattr(self,analysis,args[0])
     51        #Create dynamic property if property does not exist yet
     52        if not hasattr(self, analysis):
     53            setattr(self, analysis, None)
    5754
    58                 return self
    59         # }}}
    60         def checkconsistency(self,md,solution,analyses):    # {{{
    61                 for analysis in list(vars(self).keys()):
    62                         if not getattr(self,analysis):
    63                                 md.checkmessage("md.toolkits.%s is empty" % analysis)
     55        #Add toolkits options to analysis
     56        if len(args) == 1:
     57            setattr(self, analysis, args[0])
    6458
    65                 return md
    66         # }}}
    67         def ToolkitsFile(self,filename):    # {{{
    68                 """
    69                 TOOLKITSFILE- build toolkits file
     59        return self
     60    # }}}
    7061
    71                    Build a Petsc compatible options file, from the toolkits model field  + return options string
    72                    This file will also be used when the toolkit used is 'issm' instead of 'petsc'
     62    def checkconsistency(self, md, solution, analyses):    # {{{
     63        for analysis in list(vars(self).keys()):
     64            if not getattr(self, analysis):
     65                md.checkmessage("md.toolkits.%s is empty" % analysis)
     66
     67        return md
     68    # }}}
     69
     70    def ToolkitsFile(self, filename):    # {{{
     71        """
     72        TOOLKITSFILE- build toolkits file
     73
     74           Build a Petsc compatible options file, from the toolkits model field  + return options string
     75           This file will also be used when the toolkit used is 'issm' instead of 'petsc'
    7376
    7477
    75                    Usage:     ToolkitsFile(toolkits,filename);
    76                 """
     78           Usage:     ToolkitsFile(toolkits,filename);
     79        """
    7780
    78                 #open file for writing
    79                 try:
    80                         fid=open(filename,'w')
    81                 except IOError as e:
    82                         raise IOError("ToolkitsFile error: could not open '%s' for writing." % filename)
     81        #open file for writing
     82        try:
     83            fid = open(filename, 'w')
     84        except IOError as e:
     85            raise IOError("ToolkitsFile error: could not open '%s' for writing." % filename)
    8386
    84                 #write header
    85                 fid.write("%s%s%s\n" % ('%Toolkits options file: ',filename,' written from Python toolkits array'))
     87        #write header
     88        fid.write("%s%s%s\n" % ('%Toolkits options file: ', filename, ' written from Python toolkits array'))
    8689
    87                 #start writing options
    88                 for analysis in list(vars(self).keys()):
    89                         options=getattr(self,analysis)
     90        #start writing options
     91        for analysis in list(vars(self).keys()):
     92            options = getattr(self, analysis)
    9093
    91                         #first write analysis:
    92                         fid.write("\n+%s\n" % analysis)    #append a + to recognize it's an analysis enum
    93                         #now, write options
    94                         for optionname,optionvalue in list(options.items()):
     94            #first write analysis:
     95            fid.write("\n+%s\n" % analysis)    #append a + to recognize it's an analysis enum
     96            #now, write options
     97            for optionname, optionvalue in list(options.items()):
    9598
    96                                 if not optionvalue:
    97                                         #this option has only one argument
    98                                         fid.write("-%s\n" % optionname)
    99                                 else:
    100                                         #option with value. value can be string or scalar
    101                                         if   isinstance(optionvalue,(bool,int,float)):
    102                                                 fid.write("-%s %g\n" % (optionname,optionvalue))
    103                                         elif isinstance(optionvalue,str):
    104                                                 fid.write("-%s %s\n" % (optionname,optionvalue))
    105                                         else:
    106                                                 raise TypeError("ToolkitsFile error: option '%s' is not well formatted." % optionname)
     99                if not optionvalue:
     100                    #this option has only one argument
     101                    fid.write("-%s\n" % optionname)
     102                else:
     103                    #option with value. value can be string or scalar
     104                    if isinstance(optionvalue, (bool, int, float)):
     105                        fid.write("-%s %g\n" % (optionname, optionvalue))
     106                    elif isinstance(optionvalue, str):
     107                        fid.write("-%s %s\n" % (optionname, optionvalue))
     108                    else:
     109                        raise TypeError("ToolkitsFile error: option '%s' is not well formatted." % optionname)
    107110
    108                 fid.close()
    109         # }}}
     111        fid.close()
     112    # }}}
Note: See TracChangeset for help on using the changeset viewer.