Changeset 27458 for issm/trunk-jpl/src/m/classes/toolkits.py
- Timestamp:
- 12/08/22 00:23:36 (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified issm/trunk-jpl/src/m/classes/toolkits.py ¶
r27427 r27458 1 from fielddisplay import fielddisplay 2 from iluasmoptions import iluasmoptions 1 3 from IssmConfig import IssmConfig 2 from mumpsoptions import mumpsoptions3 from iluasmoptions import iluasmoptions4 from fielddisplay import fielddisplay5 4 from issmgslsolver import issmgslsolver 6 5 from issmmumpssolver import issmmumpssolver 6 from mumpsoptions import mumpsoptions 7 7 8 8 9 9 class toolkits(object): 10 """ TOOLKITSclass definition10 """toolkits class definition 11 11 12 12 Usage: … … 14 14 """ 15 15 16 def __init__(self): #{{{ 16 def __init__(self, *args): # {{{ 17 self.DefaultAnalysis = None 18 self.RecoveryAnalysis = None 19 20 nargs = len(args) 21 if nargs == 0: 22 self.setdefaultparameters() 23 elif nargs == 1: 24 # TODO: Replace the following with constructor 25 self.setdefaultparameters() 26 else: 27 raise Exception('constructor not supported') 28 # }}} 29 30 def __repr__(self): # {{{ 31 s = "List of toolkits options per analysis:\n\n" 32 for analysis in list(vars(self).keys()): 33 s += "{}\n".format(fielddisplay(self, analysis, '')) 34 35 return s 36 # }}} 37 38 def addoptions(self, analysis, *args): # {{{ 39 """addoptions - add analysis to md.toolkits.analysis 40 41 Optional third parameter adds toolkits options to analysis. 42 43 Usage: 44 md.toolkits = addoptions(md.toolkits, 'StressbalanceAnalysis', FSoptions()) 45 md.toolkits = addoptions(md.toolkits, 'StressbalanceAnalysis') 46 """ 47 48 # Create dynamic property if property does not exist yet 49 if not hasattr(self, analysis): 50 setattr(self, analysis, None) 51 52 # Add toolkits options to analysis 53 if len(args) == 1: 54 setattr(self, analysis, args[0]) 55 56 return self 57 # }}} 58 59 def setdefaultparameters(self): # {{{ 17 60 # Default toolkits 18 61 if IssmConfig('_HAVE_PETSC_')[0]: … … 28 71 self.DefaultAnalysis = issmgslsolver() 29 72 else: 30 raise IOError( "ToolkitsFile error: need at least Mumps or Gsl to define issm solver type")73 raise IOError('ToolkitsFile error: need at least MUMPS or GSL to define ISSM solver type, no default solver assigned') 31 74 32 75 # Use same solver for Recovery mode 33 76 self.RecoveryAnalysis = self.DefaultAnalysis 34 77 35 # The other properties are dynamic36 # }}}78 return self 79 # }}} 37 80 38 def __repr__(self): #{{{ 39 s = "List of toolkits options per analysis:\n\n" 40 for analysis in list(vars(self).keys()): 41 s += "{}\n".format(fielddisplay(self, analysis, '')) 81 def checkconsistency(self, md, solution, analyses): # {{{ 82 supported_analyses = [ 83 'DefaultAnalysis', 84 'RecoveryAnalysis', 85 'StressbalanceAnalysis', 86 'GLheightadvectionAnalysis', 87 'MasstransportAnalysis', 88 'ThermalAnalysis', 89 'EnthalpyAnalysis', 90 'AdjointBalancethicknessAnalysis', 91 'BalancethicknessAnalysis', 92 'Balancethickness2Analysis', 93 'BalancethicknessSoftAnalysis', 94 'BalancevelocityAnalysis', 95 'DamageEvolutionAnalysis', 96 'LoveAnalysis', 97 'EsaAnalysis', 98 'SealevelchangeAnalysis', 99 'FreeSurfaceBaseAnalysis', 100 'FreeSurfaceTopAnalysis', 101 'LevelsetAnalysis', 102 'DebrisAnalysis', 103 'L2ProjectionBaseAnalysis', 104 'ExtrudeFromBaseAnalysis', 105 'ExtrudeFromTopAnalysis' 106 ] 107 analyses = list(vars(self).keys()) 108 for analysis in analyses: 109 if analysis not in supported_analyses: 110 md.checkmessage('md.toolkits.{} not supported yet'.format(analysis)) 42 111 43 return s44 #}}}45 46 def addoptions(self, analysis, *args): #{{{47 # Usage example:48 # md.toolkits = addoptions(md.toolkits, 'StressbalanceAnalysis', FSoptions())49 # md.toolkits = addoptions(md.toolkits, 'StressbalanceAnalysis')50 51 # Create dynamic property if property does not exist yet52 if not hasattr(self, analysis):53 setattr(self, analysis, None)54 55 # Add toolkits options to analysis56 if len(args) == 1:57 setattr(self, analysis, args[0])58 59 return self60 #}}}61 62 def checkconsistency(self, md, solution, analyses): #{{{63 # TODO:64 # - Implement something closer to a switch as in65 # src/m/classes/toolkits.m?66 #67 for analysis in list(vars(self).keys()):68 112 if not getattr(self, analysis): 69 md.checkmessage( "md.toolkits.{} is empty".format(analysis))113 md.checkmessage('md.toolkits.{} is empty'.format(analysis)) 70 114 71 115 return md 72 # }}}116 # }}} 73 117 74 def ToolkitsFile(self, filename): #{{{75 """T OOLKITSFILE- build toolkits file118 def ToolkitsFile(self, filename): # {{{ 119 """ToolkitsFile - build toolkits file 76 120 77 Build a PETSc compatible options file, from the toolkits model field +return options string.121 Build a PETSc compatible options file, from the toolkits model field and return options string. 78 122 This file will also be used when the toolkit used is 'issm' instead of 'petsc'. 79 123 … … 86 130 fid = open(filename, 'w') 87 131 except IOError as e: 88 raise IOError( "ToolkitsFile error: could not open {}' for writing due to".format(filename), e)132 raise IOError('ToolkitsFile error: could not open {} for writing due to {}'.format(filename), e) 89 133 90 134 # Write header 91 fid.write( "%s%s%s\n" %('%Toolkits options file: ', filename, ' written from Python toolkits array'))135 fid.write('{}{}{}\n'.format('%Toolkits options file: ', filename, ' written from Python toolkits array')) 92 136 93 137 # Start writing options … … 96 140 97 141 # First write analysis 98 fid.write("\n+{}\n".format(analysis)) # Append a + to recognize it's an analysis enum 142 fid.write('\n+{}\n'.format(analysis)) # Append a + to recognize it's an analysis enum 143 99 144 # Now, write options 100 145 for optionname, optionvalue in list(options.items()): … … 102 147 if not optionvalue: 103 148 # This option has only one argument 104 fid.write( "-{}\n".format(optionname))149 fid.write('-{}\n'.format(optionname)) 105 150 else: 106 151 # Option with value. Value can be string or scalar. 107 152 if isinstance(optionvalue, (bool, int, float)): 108 fid.write( "-{} {}\n".format(optionname, optionvalue))153 fid.write('-{} {}\n'.format(optionname, optionvalue)) 109 154 elif isinstance(optionvalue, str): 110 fid.write( "-{} {}\n".format(optionname, optionvalue))155 fid.write('-{} {}\n'.format(optionname, optionvalue)) 111 156 else: 112 raise TypeError( "ToolkitsFile error: option '{}' is not well formatted.".format(optionname))157 raise TypeError('ToolkitsFile error: option {} is not well formatted'.format(optionname)) 113 158 114 159 fid.close() 115 # }}}160 # }}}
Note:
See TracChangeset
for help on using the changeset viewer.