[12038] | 1 | #module imports
|
---|
[13023] | 2 | import numpy
|
---|
[13740] | 3 | import copy
|
---|
[12038] | 4 | from fielddisplay import fielddisplay
|
---|
[13023] | 5 | from EnumDefinitions import *
|
---|
[13043] | 6 | from StringToEnum import StringToEnum
|
---|
[13023] | 7 | from checkfield import *
|
---|
| 8 | from WriteData import *
|
---|
[12038] | 9 |
|
---|
[12958] | 10 | class inversion(object):
|
---|
[13023] | 11 | """
|
---|
| 12 | INVERSION class definition
|
---|
| 13 |
|
---|
| 14 | Usage:
|
---|
| 15 | inversion=inversion();
|
---|
| 16 | """
|
---|
| 17 |
|
---|
[12038] | 18 | #properties
|
---|
| 19 | def __init__(self):
|
---|
| 20 | # {{{ Properties
|
---|
| 21 | self.iscontrol = 0
|
---|
| 22 | self.tao = 0
|
---|
| 23 | self.incomplete_adjoint = 0
|
---|
| 24 | self.control_parameters = float('NaN')
|
---|
| 25 | self.nsteps = 0
|
---|
| 26 | self.maxiter_per_step = float('NaN')
|
---|
| 27 | self.cost_functions = float('NaN')
|
---|
| 28 | self.cost_functions_coefficients = float('NaN')
|
---|
| 29 | self.gradient_scaling = float('NaN')
|
---|
| 30 | self.cost_function_threshold = 0
|
---|
| 31 | self.min_parameters = float('NaN')
|
---|
| 32 | self.max_parameters = float('NaN')
|
---|
| 33 | self.step_threshold = float('NaN')
|
---|
| 34 | self.gradient_only = 0
|
---|
| 35 | self.vx_obs = float('NaN')
|
---|
| 36 | self.vy_obs = float('NaN')
|
---|
| 37 | self.vz_obs = float('NaN')
|
---|
| 38 | self.vel_obs = float('NaN')
|
---|
| 39 | self.thickness_obs = float('NaN')
|
---|
[13093] | 40 |
|
---|
| 41 | #set defaults
|
---|
| 42 | self.setdefaultparameters()
|
---|
| 43 |
|
---|
[12038] | 44 | #}}}
|
---|
[13023] | 45 | def __repr__(self):
|
---|
[12038] | 46 | # {{{ Display
|
---|
| 47 | string='\n Inversion parameters:'
|
---|
[13023] | 48 | string="%s\n%s"%(string,fielddisplay(self,'iscontrol','is inversion activated?'))
|
---|
| 49 | string="%s\n%s"%(string,fielddisplay(self,'incomplete_adjoint','do we assume linear viscosity?'))
|
---|
| 50 | string="%s\n%s"%(string,fielddisplay(self,'control_parameters','parameter where inverse control is carried out; ex: {''FrictionCoefficient''}, or {''MaterialsRheologyBbar''}'))
|
---|
| 51 | string="%s\n%s"%(string,fielddisplay(self,'nsteps','number of optimization searches'))
|
---|
| 52 | string="%s\n%s"%(string,fielddisplay(self,'cost_functions','indicate the type of response for each optimization step'))
|
---|
| 53 | string="%s\n%s"%(string,fielddisplay(self,'cost_functions_coefficients','cost_functions_coefficients applied to the misfit of each vertex and for each control_parameter'))
|
---|
| 54 | string="%s\n%s"%(string,fielddisplay(self,'cost_function_threshold','misfit convergence criterion. Default is 1%, NaN if not applied'))
|
---|
| 55 | string="%s\n%s"%(string,fielddisplay(self,'maxiter_per_step','maximum iterations during each optimization step'))
|
---|
| 56 | string="%s\n%s"%(string,fielddisplay(self,'gradient_scaling','scaling factor on gradient direction during optimization, for each optimization step'))
|
---|
| 57 | string="%s\n%s"%(string,fielddisplay(self,'step_threshold','decrease threshold for misfit, default is 30%'))
|
---|
| 58 | string="%s\n%s"%(string,fielddisplay(self,'min_parameters','absolute minimum acceptable value of the inversed parameter on each vertex'))
|
---|
| 59 | string="%s\n%s"%(string,fielddisplay(self,'max_parameters','absolute maximum acceptable value of the inversed parameter on each vertex'))
|
---|
| 60 | string="%s\n%s"%(string,fielddisplay(self,'gradient_only','stop control method solution at gradient'))
|
---|
| 61 | string="%s\n%s"%(string,fielddisplay(self,'vx_obs','observed velocity x component [m/a]'))
|
---|
| 62 | string="%s\n%s"%(string,fielddisplay(self,'vy_obs','observed velocity y component [m/a]'))
|
---|
| 63 | string="%s\n%s"%(string,fielddisplay(self,'vel_obs','observed velocity magnitude [m/a]'))
|
---|
| 64 | string="%s\n%s"%(string,fielddisplay(self,'thickness_obs','observed thickness [m]'))
|
---|
[12038] | 65 | string="%s\n%s"%(string,'Available cost functions:')
|
---|
| 66 | string="%s\n%s"%(string,' 101: SurfaceAbsVelMisfit')
|
---|
| 67 | string="%s\n%s"%(string,' 102: SurfaceRelVelMisfit')
|
---|
| 68 | string="%s\n%s"%(string,' 103: SurfaceLogVelMisfit')
|
---|
| 69 | string="%s\n%s"%(string,' 104: SurfaceLogVxVyMisfit')
|
---|
| 70 | string="%s\n%s"%(string,' 105: SurfaceAverageVelMisfit')
|
---|
| 71 | string="%s\n%s"%(string,' 201: ThicknessAbsMisfit')
|
---|
| 72 | string="%s\n%s"%(string,' 501: DragCoefficientAbsGradient')
|
---|
| 73 | string="%s\n%s"%(string,' 502: RheologyBbarAbsGradient')
|
---|
| 74 | string="%s\n%s"%(string,' 503: ThicknessAbsGradient')
|
---|
| 75 | return string
|
---|
| 76 | #}}}
|
---|
[12123] | 77 |
|
---|
[13029] | 78 | def setdefaultparameters(self): # {{{
|
---|
[12123] | 79 |
|
---|
| 80 | #default is incomplete adjoint for now
|
---|
[13023] | 81 | self.incomplete_adjoint=1
|
---|
[12123] | 82 |
|
---|
| 83 | #parameter to be inferred by control methods (only
|
---|
| 84 | #drag and B are supported yet)
|
---|
[13093] | 85 | self.control_parameters='FrictionCoefficient'
|
---|
[12123] | 86 |
|
---|
| 87 | #number of steps in the control methods
|
---|
[13023] | 88 | self.nsteps=20
|
---|
[12123] | 89 |
|
---|
| 90 | #maximum number of iteration in the optimization algorithm for
|
---|
| 91 | #each step
|
---|
[13093] | 92 | self.maxiter_per_step=20*numpy.ones(self.nsteps)
|
---|
[12123] | 93 |
|
---|
| 94 | #the inversed parameter is updated as follows:
|
---|
| 95 | #new_par=old_par + gradient_scaling(n)*C*gradient with C in [0 1];
|
---|
| 96 | #usually the gradient_scaling must be of the order of magnitude of the
|
---|
| 97 | #inversed parameter (10^8 for B, 50 for drag) and can be decreased
|
---|
| 98 | #after the first iterations
|
---|
[13642] | 99 | self.gradient_scaling=50*numpy.ones((self.nsteps,1))
|
---|
[12123] | 100 |
|
---|
| 101 | #several responses can be used:
|
---|
[13642] | 102 | self.cost_functions=101*numpy.ones((self.nsteps,1))
|
---|
[12123] | 103 |
|
---|
| 104 | #step_threshold is used to speed up control method. When
|
---|
[13023] | 105 | #misfit(1)/misfit(0) < self.step_threshold, we go directly to
|
---|
[12123] | 106 | #the next step
|
---|
[13093] | 107 | self.step_threshold=.7*numpy.ones(self.nsteps) #30 per cent decrement
|
---|
[12123] | 108 |
|
---|
| 109 | #stop control solution at the gradient computation and return it?
|
---|
[13023] | 110 | self.gradient_only=0
|
---|
[12123] | 111 |
|
---|
| 112 | #cost_function_threshold is a criteria to stop the control methods.
|
---|
| 113 | #if J[n]-J[n-1]/J[n] < criteria, the control run stops
|
---|
| 114 | #NaN if not applied
|
---|
[13093] | 115 | self.cost_function_threshold=float('NaN') #not activated
|
---|
[12123] | 116 |
|
---|
[13023] | 117 | return self
|
---|
| 118 | #}}}
|
---|
[13030] | 119 |
|
---|
[13023] | 120 | def checkconsistency(self,md,solution,analyses): # {{{
|
---|
[12123] | 121 |
|
---|
[13023] | 122 | #Early return
|
---|
| 123 | if not self.iscontrol:
|
---|
| 124 | return md
|
---|
| 125 |
|
---|
| 126 | num_controls=numpy.size(md.inversion.control_parameters)
|
---|
[13642] | 127 | num_costfunc=numpy.size(md.inversion.cost_functions,axis=1)
|
---|
[13023] | 128 |
|
---|
| 129 | md = checkfield(md,'inversion.iscontrol','values',[0,1])
|
---|
| 130 | md = checkfield(md,'inversion.tao','values',[0,1])
|
---|
| 131 | md = checkfield(md,'inversion.incomplete_adjoint','values',[0,1])
|
---|
[13624] | 132 | md = checkfield(md,'inversion.control_parameters','cell',1,'values',['BalancethicknessThickeningRate','FrictionCoefficient','MaterialsRheologyBbar','MaterialsRheologyZbar','Vx','Vy'])
|
---|
[13040] | 133 | md = checkfield(md,'inversion.nsteps','numel',[1],'>=',1)
|
---|
[13023] | 134 | md = checkfield(md,'inversion.maxiter_per_step','size',[md.inversion.nsteps],'>=',0)
|
---|
| 135 | md = checkfield(md,'inversion.step_threshold','size',[md.inversion.nsteps])
|
---|
[13059] | 136 | md = checkfield(md,'inversion.cost_functions','size',[md.inversion.nsteps,num_costfunc],'values',[101,102,103,104,105,201,501,502,503,504,505])
|
---|
[13023] | 137 | md = checkfield(md,'inversion.cost_functions_coefficients','size',[md.mesh.numberofvertices,num_costfunc],'>=',0)
|
---|
| 138 | md = checkfield(md,'inversion.gradient_only','values',[0,1])
|
---|
| 139 | md = checkfield(md,'inversion.gradient_scaling','size',[md.inversion.nsteps,num_controls])
|
---|
| 140 | md = checkfield(md,'inversion.min_parameters','size',[md.mesh.numberofvertices,num_controls])
|
---|
| 141 | md = checkfield(md,'inversion.max_parameters','size',[md.mesh.numberofvertices,num_controls])
|
---|
| 142 |
|
---|
| 143 | if solution==BalancethicknessSolutionEnum():
|
---|
| 144 | md = checkfield(md,'inversion.thickness_obs','size',[md.mesh.numberofvertices],'NaN',1)
|
---|
| 145 | else:
|
---|
| 146 | md = checkfield(md,'inversion.vx_obs','size',[md.mesh.numberofvertices],'NaN',1)
|
---|
| 147 | md = checkfield(md,'inversion.vy_obs','size',[md.mesh.numberofvertices],'NaN',1)
|
---|
| 148 |
|
---|
| 149 | return md
|
---|
| 150 | # }}}
|
---|
[13030] | 151 |
|
---|
[13023] | 152 | def marshall(self,fid): # {{{
|
---|
| 153 |
|
---|
| 154 | WriteData(fid,'object',self,'fieldname','iscontrol','format','Boolean')
|
---|
| 155 | WriteData(fid,'object',self,'fieldname','tao','format','Boolean')
|
---|
| 156 | WriteData(fid,'object',self,'fieldname','incomplete_adjoint','format','Boolean')
|
---|
| 157 | if not self.iscontrol:
|
---|
| 158 | return
|
---|
| 159 | WriteData(fid,'object',self,'fieldname','nsteps','format','Integer')
|
---|
| 160 | WriteData(fid,'object',self,'fieldname','maxiter_per_step','format','DoubleMat','mattype',3)
|
---|
| 161 | WriteData(fid,'object',self,'fieldname','cost_functions_coefficients','format','DoubleMat','mattype',1)
|
---|
| 162 | WriteData(fid,'object',self,'fieldname','gradient_scaling','format','DoubleMat','mattype',3)
|
---|
| 163 | WriteData(fid,'object',self,'fieldname','cost_function_threshold','format','Double')
|
---|
| 164 | WriteData(fid,'object',self,'fieldname','min_parameters','format','DoubleMat','mattype',3)
|
---|
| 165 | WriteData(fid,'object',self,'fieldname','max_parameters','format','DoubleMat','mattype',3)
|
---|
| 166 | WriteData(fid,'object',self,'fieldname','step_threshold','format','DoubleMat','mattype',3)
|
---|
| 167 | WriteData(fid,'object',self,'fieldname','gradient_only','format','Boolean')
|
---|
| 168 | WriteData(fid,'object',self,'fieldname','vx_obs','format','DoubleMat','mattype',1)
|
---|
| 169 | WriteData(fid,'object',self,'fieldname','vy_obs','format','DoubleMat','mattype',1)
|
---|
| 170 | WriteData(fid,'object',self,'fieldname','vz_obs','format','DoubleMat','mattype',1)
|
---|
| 171 | WriteData(fid,'object',self,'fieldname','thickness_obs','format','DoubleMat','mattype',1)
|
---|
| 172 |
|
---|
| 173 | #process control parameters
|
---|
[13517] | 174 | num_control_parameters=len(self.control_parameters)
|
---|
[13856] | 175 | data=numpy.array([StringToEnum(control_parameter)[0] for control_parameter in self.control_parameters]).reshape(1,-1)
|
---|
[13023] | 176 | WriteData(fid,'data',data,'enum',InversionControlParametersEnum(),'format','DoubleMat','mattype',3)
|
---|
| 177 | WriteData(fid,'data',num_control_parameters,'enum',InversionNumControlParametersEnum(),'format','Integer')
|
---|
| 178 |
|
---|
| 179 | #process cost functions
|
---|
[13642] | 180 | num_cost_functions=numpy.size(self.cost_functions,axis=1)
|
---|
[13740] | 181 | data=copy.deepcopy(self.cost_functions)
|
---|
[13171] | 182 | data[numpy.nonzero(data==101)]=SurfaceAbsVelMisfitEnum()
|
---|
| 183 | data[numpy.nonzero(data==102)]=SurfaceRelVelMisfitEnum()
|
---|
| 184 | data[numpy.nonzero(data==103)]=SurfaceLogVelMisfitEnum()
|
---|
| 185 | data[numpy.nonzero(data==104)]=SurfaceLogVxVyMisfitEnum()
|
---|
| 186 | data[numpy.nonzero(data==105)]=SurfaceAverageVelMisfitEnum()
|
---|
| 187 | data[numpy.nonzero(data==201)]=ThicknessAbsMisfitEnum()
|
---|
| 188 | data[numpy.nonzero(data==501)]=DragCoefficientAbsGradientEnum()
|
---|
| 189 | data[numpy.nonzero(data==502)]=RheologyBbarAbsGradientEnum()
|
---|
| 190 | data[numpy.nonzero(data==503)]=ThicknessAbsGradientEnum()
|
---|
| 191 | data[numpy.nonzero(data==504)]=ThicknessAlongGradientEnum()
|
---|
| 192 | data[numpy.nonzero(data==505)]=ThicknessAcrossGradientEnum()
|
---|
[13023] | 193 | WriteData(fid,'data',data,'enum',InversionCostFunctionsEnum(),'format','DoubleMat','mattype',3)
|
---|
| 194 | WriteData(fid,'data',num_cost_functions,'enum',InversionNumCostFunctionsEnum(),'format','Integer')
|
---|
| 195 | # }}}
|
---|
[13030] | 196 |
|
---|