Changeset 27744
- Timestamp:
- 05/12/23 13:44:31 (23 months ago)
- Location:
- issm/trunk-jpl/src/m/classes
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified issm/trunk-jpl/src/m/classes/autodiff.py ¶
r27458 r27744 9 9 10 10 class autodiff(object): 11 """ 12 AUTODIFF class definition 13 14 Usage: 15 autodiff = autodiff() 11 """autodiff class definition 12 13 Usage: 14 autodiff = autodiff() 16 15 """ 17 16 def __init__(self, *args): # {{{ … … 20 19 self.independents = [] 21 20 self.driver = 'fos_forward' 22 self.obufsize = float('NaN')23 self.lbufsize = float('NaN')24 self.cbufsize = float('NaN')25 self.tbufsize = float('NaN')26 self.gcTriggerMaxSize = float('NaN')27 self.gcTriggerRatio = float('NaN')28 self.tapeAlloc = float('NaN')21 self.obufsize = np.nan 22 self.lbufsize = np.nan 23 self.cbufsize = np.nan 24 self.tbufsize = np.nan 25 self.gcTriggerMaxSize = np.nan 26 self.gcTriggerRatio = np.nan 27 self.tapeAlloc = np.nan 29 28 if not len(args): 30 29 self.setdefaultparameters() … … 34 33 35 34 def __repr__(self): # {{{ 36 s = " automatic differentiation parameters:\n"37 s += "%s\n" % fielddisplay(self, 'isautodiff', "indicates if the automatic differentiation is activated")38 s += "%s\n" % fielddisplay(self, 'dependents', "list of dependent variables")39 s += "%s\n" % fielddisplay(self, 'independents', "list of independent variables")40 s += "%s\n" % fielddisplay(self, 'driver', "ADOLC driver ('fos_forward' or 'fov_forward')")41 s += "%s\n" % fielddisplay(self, 'obufsize', "Number of operations per buffer (== OBUFSIZE in usrparms.h)")42 s += "%s\n" % fielddisplay(self, 'lbufsize', "Number of locations per buffer (== LBUFSIZE in usrparms.h)")43 s += "%s\n" % fielddisplay(self, 'cbufsize', "Number of values per buffer (== CBUFSIZE in usrparms.h)")44 s += "%s\n" % fielddisplay(self, 'tbufsize', "Number of taylors per buffer (<=TBUFSIZE in usrparms.h)")45 s += "%s\n" % fielddisplay(self, 'gcTriggerRatio', "free location block sorting / consolidation triggered if the ratio between allocated and used locations exceeds gcTriggerRatio")46 s += "%s\n" % fielddisplay(self, 'gcTriggerMaxSize', "free location block sorting / consolidation triggered if the allocated locations exceed gcTriggerMaxSize)")47 s += "%s\n" % fielddisplay(self, 'tapeAlloc', 'Iteration count of a priori memory allocation of the AD tape')35 s = ' automatic differentiation parameters:\n' 36 s += '{}\n'.format(fielddisplay(self, 'isautodiff', "indicates if the automatic differentiation is activated")) 37 s += '{}\n'.format(fielddisplay(self, 'dependents', "list of dependent variables")) 38 s += '{}\n'.format(fielddisplay(self, 'independents', "list of independent variables")) 39 s += '{}\n'.format(fielddisplay(self, 'driver', "ADOLC driver ('fos_forward' or 'fov_forward')")) 40 s += '{}\n'.format(fielddisplay(self, 'obufsize', "Number of operations per buffer (== OBUFSIZE in usrparms.h)")) 41 s += '{}\n'.format(fielddisplay(self, 'lbufsize', "Number of locations per buffer (== LBUFSIZE in usrparms.h)")) 42 s += '{}\n'.format(fielddisplay(self, 'cbufsize', "Number of values per buffer (== CBUFSIZE in usrparms.h)")) 43 s += '{}\n'.format(fielddisplay(self, 'tbufsize', "Number of taylors per buffer (<=TBUFSIZE in usrparms.h)")) 44 s += '{}\n'.format(fielddisplay(self, 'gcTriggerRatio', "free location block sorting / consolidation triggered if the ratio between allocated and used locations exceeds gcTriggerRatio")) 45 s += '{}\n'.format(fielddisplay(self, 'gcTriggerMaxSize', "free location block sorting / consolidation triggered if the allocated locations exceed gcTriggerMaxSize)")) 46 s += '{}\n'.format(fielddisplay(self, 'tapeAlloc', 'Iteration count of a priori memory allocation of the AD tape')) 48 47 49 48 return s … … 62 61 63 62 def checkconsistency(self, md, solution, analyses): # {{{ 64 # Early return63 # Early return 65 64 if not self.isautodiff: 66 65 return md … … 74 73 md = checkfield(md, 'fieldname', 'autodiff.tapeAlloc', '>=', 0) 75 74 76 #Driver value:75 # Driver value 77 76 md = checkfield(md, 'fieldname', 'autodiff.driver', 'values', ['fos_forward', 'fov_forward', 'fov_forward_all', 'fos_reverse', 'fov_reverse', 'fov_reverse_all']) 78 77 79 #go through our dependents and independents and check consistency:78 # Go through our dependents and independents and check consistency 80 79 for dep in self.dependents: 81 80 dep.checkconsistency(md, solution, analyses) … … 90 89 WriteData(fid, prefix, 'object', self, 'fieldname', 'driver', 'format', 'String') 91 90 92 # early return91 # Early return 93 92 if not self.isautodiff: 94 93 WriteData(fid, prefix, 'data', False, 'name', 'md.autodiff.mass_flux_segments_present', 'format', 'Boolean') … … 96 95 return 97 96 98 # buffer sizes {{{97 # Buffer sizes 99 98 WriteData(fid, prefix, 'object', self, 'fieldname', 'obufsize', 'format', 'Double') 100 99 WriteData(fid, prefix, 'object', self, 'fieldname', 'lbufsize', 'format', 'Double') … … 104 103 WriteData(fid, prefix, 'object', self, 'fieldname', 'gcTriggerMaxSize', 'format', 'Double') 105 104 WriteData(fid, prefix, 'object', self, 'fieldname', 'tapeAlloc', 'format', 'Integer') 106 # }}} 107 # process dependent variables {{{105 106 # Process dependent variables 108 107 num_dependent_objects = len(self.dependents) 109 108 WriteData(fid, prefix, 'data', num_dependent_objects, 'name', 'md.autodiff.num_dependent_objects', 'format', 'Integer') … … 111 110 if num_dependent_objects: 112 111 names = [] 113 indices = np.zeros(num_dependent_objects)114 115 112 for i, dep in enumerate(self.dependents): 116 113 names.append(dep.name) 117 indices[i] = dep.index118 114 119 115 WriteData(fid, prefix, 'data', names, 'name', 'md.autodiff.dependent_object_names', 'format', 'StringArray') 120 WriteData(fid, prefix, 'data', indices, 'name', 'md.autodiff.dependent_object_indices', 'format', 'IntMat', 'mattype', 3) 121 # }}} 122 #process independent variables {{{ 116 117 # Process independent variables 123 118 num_independent_objects = len(self.independents) 124 119 WriteData(fid, prefix, 'data', num_independent_objects, 'name', 'md.autodiff.num_independent_objects', 'format', 'Integer') 125 120 126 if num_independent_objects: 127 names = [None] * num_independent_objects 128 types = np.zeros(num_independent_objects) 129 130 for i, indep in enumerate(self.independents): 131 names[i] = indep.name 132 types[i] = indep.typetoscalar() 133 134 WriteData(fid, prefix, 'data', names, 'name', 'md.autodiff.independent_object_names', 'format', 'StringArray') 135 WriteData(fid, prefix, 'data', types, 'name', 'md.autodiff.independent_object_types', 'format', 'IntMat', 'mattype', 3) 136 # }}} 137 #if driver is fos_forward, build index: {{{ 121 for indep in self.independents: 122 WriteData(fid, prefix, 'data', indep.name, 'name', 'md.autodiff.independent_name', 'format', 'String') 123 WriteData(fid, prefix, 'data', indep.typetoscalar(), 'name', 'md.autodiff.independent_type', 'format', 'Integer') 124 WriteData(fid, prefix, 'data', indep.min_parameters, 'name','md.autodiff.independent_min_parameters','format', 'DoubleMat', 'mattype', 3) 125 WriteData(fid, prefix, 'data', indep.max_parameters, 'name', 'md.autodiff.independent_max_parameters', 'format', 'DoubleMat', 'mattype', 3) 126 WriteData(fid, prefix, 'data', indep.control_scaling_factor, 'name', 'md.autodiff.independent_scaling_factor', 'format', 'Double') 127 WriteData(fid, prefix, 'data', indep.control_size, 'name', 'md.autodiff.independent_control_size', 'format', 'Integer') 128 129 # If driver is fos_forward, build index 138 130 if strcmpi(self.driver, 'fos_forward'): 139 131 index = 0 … … 149 141 index += indep.nods 150 142 151 index -= 1 # get c -index numbering going143 index -= 1 # get c-index numbering going 152 144 WriteData(fid, prefix, 'data', index, 'name', 'md.autodiff.fos_forward_index', 'format', 'Integer') 153 # }}} 154 # if driver is fos_reverse, build index: {{{145 146 # If driver is fos_reverse, build index 155 147 if strcmpi(self.driver, 'fos_reverse'): 156 148 index = 0 … … 163 155 index += 1 164 156 165 index -= 1 # get c -index numbering going157 index -= 1 # get c-index numbering going 166 158 WriteData(fid, prefix, 'data', index, 'name', 'md.autodiff.fos_reverse_index', 'format', 'Integer') 167 # }}} 168 # if driver is fov_forward, build indices: {{{159 160 # If driver is fov_forward, build indices 169 161 if strcmpi(self.driver, 'fov_forward'): 170 162 indices = 0 … … 180 172 indices += indep.nods 181 173 182 indices -= 1 # get c -indices numbering going174 indices -= 1 # get c-indices numbering going 183 175 WriteData(fid, prefix, 'data', indices, 'name', 'md.autodiff.fov_forward_indices', 'format', 'IntMat', 'mattype', 3) 184 # }}} 185 # deal with mass fluxes: {{{176 177 # Deal with mass fluxes 186 178 mass_flux_segments = [dep.segments for dep in self.dependents if strcmpi(dep.name, 'MassFlux')] 187 179 … … 192 184 flag = False 193 185 WriteData(fid, prefix, 'data', flag, 'name', 'md.autodiff.mass_flux_segments_present', 'format', 'Boolean') 194 # }}} 195 # deal with trace keep on: {{{186 187 # Deal with trace keep on 196 188 keep = False 197 189 198 #From ADOLC userdoc: 199 # The optional integer argument keep of trace on determines whether the numerical values of all active variables are 200 # recorded in a buffered temporary array or file called the taylor stack. This option takes effect if keep = 1 and 201 # prepares the scene for an immediately following gradient evaluation by a call to a routine implementing the reverse 202 # mode as described in the Section 4 and Section 5. 190 # From ADOLC userdoc: 191 # The optional integer argument keep of trace on determines whether the 192 # numerical values of all active variables are recorded in a buffered 193 # temporary array or file called the taylor stack. This option takes 194 # effect if keep = 1 and prepares the scene for an immediately 195 # following gradient evaluation by a call to a routine implementing the 196 # reverse mode as described in the Section 4 and Section 5. 203 197 # 204 198 if len(self.driver) <= 3: 205 keep = False # there is no "_reverse" string within the driver string:199 keep = False # there is no "_reverse" string within the driver string 206 200 else: 207 201 if strncmpi(self.driver[3:], '_reverse', 8): -
TabularUnified issm/trunk-jpl/src/m/classes/independent.m ¶
r25551 r27744 66 66 fielddisplay(self,'name','variable name (must match corresponding String)'); 67 67 fielddisplay(self,'type','type of variable (''vertex'' or ''scalar'')'); 68 fielddisplay(self,'nods','size of dependent variables');68 fielddisplay(self,'nods','size of independent variables'); 69 69 fielddisplay(self,'control_size','number of timesteps'); 70 70 fielddisplay(self,'min_parameters','absolute minimum acceptable value of the inversed parameter on each vertex'); -
TabularUnified issm/trunk-jpl/src/m/classes/independent.py ¶
r24213 r27744 7 7 8 8 class independent(object): 9 """ 10 INDEPENDENT class definition 9 """independent class definition 11 10 12 13 11 Usage: 12 independent = independent() 14 13 """ 15 14 … … 17 16 self.name = '' 18 17 self.type = '' 19 self.fos_forward_index = float('NaN')18 self.fos_forward_index = np.nan 20 19 self.fov_forward_indices = np.array([]) 21 20 self.nods = 0 21 self.min_parameters = np.nan 22 self.max_parameters = np.nan 23 self.control_scaling_factor = np.nan 24 self.control_size = 0 22 25 23 #set defaults26 # Set defaults 24 27 self.setdefaultparameters() 25 28 26 #use provided options to change fields29 # Use provided options to change fields 27 30 options = pairoptions(*args) 28 31 29 #OK get other fields32 # Get other fields 30 33 self = options.AssignObjectFields(self) 34 35 if self.control_size == 0: 36 self.control_size = 1 31 37 # }}} 32 38 33 39 def __repr__(self): # {{{ 34 s = " independent variable:\n"40 s = ' independent variable:\n' 35 41 36 s += "%s\n" % fielddisplay(self, 'name', "variable name (must match corresponding String)") 37 s += "%s\n" % fielddisplay(self, 'type', "type of variable ('vertex' or 'scalar')") 42 s += '{}\n'.format(fielddisplay(self, 'name', 'variable name (must match corresponding String)')) 43 s += '{}\n'.format(fielddisplay(self, 'type', 'type of variable (\'vertex\' or \'scalar\')')) 44 s += '{}\n'.format(fielddisplay(self, 'nods', 'size of independent variables')) 45 s += '{}\n'.format(fielddisplay(self, 'control_size', 'number of timesteps')) 46 s += '{}\n'.format(fielddisplay(self, 'min_parameters', 'absolute minimum acceptable value of the inversed parameter on each vertex')) 47 s += '{}\n'.format(fielddisplay(self, 'max_parameters', 'absolute maximum acceptable value of the inversed parameter on each vertex')) 48 s += '{}\n'.format(fielddisplay(self, 'control_scaling_factor', 'order of magnitude of each control (useful for multi-parameter optimization)')) 38 49 if not np.isnan(self.fos_forward_index): 39 s += "%s\n" % fielddisplay(self, 'fos_forward_index', "index for fos_foward driver of ADOLC")50 s += '{}\n'.format(fielddisplay(self, 'fos_forward_index', 'index for fos_foward driver of ADOLC')) 40 51 if np.any(np.logical_not(np.isnan(self.fov_forward_indices))): 41 s += "%s\n" % fielddisplay(self, 'fov_forward_indices', "indices for fov_foward driver of ADOLC")52 s += '{}\n'.format(fielddisplay(self, 'fov_forward_indices', 'indices for fov_foward driver of ADOLC')) 42 53 43 54 return s … … 45 56 46 57 def setdefaultparameters(self): # {{{ 47 # do nothing58 # Do nothing 48 59 return self 49 60 # }}} … … 52 63 if not np.isnan(self.fos_forward_index): 53 64 if not strcmpi(driver, 'fos_forward'): 54 raise TypeError( "cannot declare an independent with a fos_forward_index when the driver is not fos_forward!")65 raise TypeError('cannot declare an independent with a fos_forward_index when the driver is not fos_forward!') 55 66 if self.nods == 0: 56 raise TypeError( "independent checkconsistency error: nods should be set to the size of the independent variable")67 raise TypeError('independent checkconsistency error: nods should be set to the size of the independent variable') 57 68 58 69 if len(self.fov_forward_indices) > 0: 59 70 if not strcmpi(driver, 'fov_forward'): 60 raise TypeError( "cannot declare an independent with fov_forward_indices when the driver is not fov_forward!")71 raise TypeError('cannot declare an independent with fov_forward_indices when the driver is not fov_forward!') 61 72 if self.nods == 0: 62 raise TypeError( "independent checkconsistency error: nods should be set to the size of the independent variable")63 md = checkfield(md, 'fieldname', "autodiff.independents[%d].fov_forward_indices"% i, '>=', 1, '<=', self.nods)73 raise TypeError('independent checkconsistency error: nods should be set to the size of the independent variable') 74 md = checkfield(md, 'fieldname', 'autodiff.independents[%d].fov_forward_indices' % i, '>=', 1, '<=', self.nods) 64 75 65 76 return md … … 68 79 def typetoscalar(self): # {{{ 69 80 if strcmpi(self.type, 'scalar'): 70 scalar = 081 scalartype = 0 71 82 elif strcmpi(self.type, 'vertex'): 72 scalar = 1 83 scalartype = 1 84 elif strcmpi(self.type, 'matrix'): 85 scalartype = 1 86 else: 87 raise TypeError('{} not supported yet!'.format(self.type)) 73 88 74 return scalar 89 return scalartype 75 90 # }}}
Note:
See TracChangeset
for help on using the changeset viewer.