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