[35] | 1 | function md=control(md)
|
---|
| 2 | %CONTROL - launch a control method using MacAyeal solution
|
---|
| 3 | %
|
---|
| 4 | % the routine is used for a control method. It determines the most adapted viscosity
|
---|
| 5 | % field so that the calculated velocity field is as close as possible to an observed velocity field
|
---|
| 6 | %
|
---|
| 7 | % Usage:
|
---|
| 8 | % md=control(md)
|
---|
| 9 |
|
---|
| 10 | %First check we do have the correct argument number
|
---|
| 11 | if ((nargin~=1) || (nargout~=1)),
|
---|
| 12 | velfinderusage();
|
---|
| 13 | error('macayealcontrol error message: incorrect number of input and output arguments');
|
---|
| 14 | end
|
---|
| 15 |
|
---|
| 16 | if ~isa(md,'model'),
|
---|
| 17 | macayealcontrolusage();
|
---|
| 18 | error('macayealcontrol error message: input argument is not a @model object');
|
---|
| 19 | end
|
---|
| 20 |
|
---|
| 21 | %Check that control is done on flow law, and not drag (not supported yet):
|
---|
[408] | 22 | %if ~strcmp(md.control_type,'B')
|
---|
| 23 | % error('macayealcontrol error message: only ''B'' inversion supported yet');
|
---|
| 24 | %end
|
---|
[35] | 25 |
|
---|
| 26 | %Transfer model fields into matlab variables
|
---|
| 27 | x=md.x;
|
---|
| 28 | y=md.y;
|
---|
| 29 | index=md.elements;
|
---|
| 30 | index=sort(index,2); %necessary
|
---|
| 31 | nods=md.numberofgrids;
|
---|
| 32 | nel=md.numberofelements;
|
---|
| 33 | z_thick=md.thickness;
|
---|
| 34 | z_surf=md.surface;
|
---|
| 35 | z_bed=md.bed;
|
---|
| 36 | z_thick_bar=(z_thick(index(:,1))+z_thick(index(:,2))+z_thick(index(:,3)))/3;
|
---|
| 37 | rho_ice=md.rho_ice;
|
---|
| 38 | rho_water=md.rho_water;
|
---|
| 39 | g=md.g;
|
---|
| 40 | index_icefront=md.segmentonneumann_diag; index_icefront=index_icefront(:,1:2); %we strip the last column, which holds the element number for the boundary segment
|
---|
| 41 | nodes_on_boundary=md.gridonboundary;
|
---|
| 42 | nodes_on_dirichlet=md.gridondirichlet_diag;
|
---|
| 43 | nodes_on_icefront=zeros(nods,1); nodes_on_icefront(index_icefront)=1;
|
---|
| 44 | nodes_on_iceshelf=md.gridoniceshelf;
|
---|
| 45 |
|
---|
| 46 | criterion=md.eps_rel;
|
---|
| 47 | yts=md.yts;
|
---|
| 48 | tolx=md.tolx;
|
---|
| 49 | maxiter=md.maxiter(1);
|
---|
[408] | 50 | if strcmp(md.control_type,'B')
|
---|
| 51 | B_ini=md.B;
|
---|
| 52 | B_to_p=10^-3*yts^(-1/3);
|
---|
| 53 | else
|
---|
| 54 | drag_coeff_ini=md.drag;
|
---|
| 55 | end
|
---|
[35] | 56 | glen_coeff=md.n;
|
---|
| 57 | vx_obs=md.vx_obs/md.yts; %From m/a to m/s
|
---|
| 58 | vy_obs=md.vy_obs/md.yts;
|
---|
| 59 | nsteps=md.nsteps;
|
---|
| 60 |
|
---|
| 61 | %Build length_icefront and normal_icefront:
|
---|
| 62 | [length_icefront,normal_icefront]=buildicefrontnormal(x,y,index_icefront);
|
---|
| 63 |
|
---|
| 64 | %Building shape functions and derivative operators
|
---|
| 65 | [alpha, beta, gamma, area]=shape(index,x,y,nel,nods);
|
---|
| 66 |
|
---|
| 67 | [matrix_bar, matrix_xbar, matrix_ybar]=...
|
---|
| 68 | bar_maker(nel,nods,index,alpha,beta);
|
---|
| 69 |
|
---|
| 70 | %initialize some data
|
---|
| 71 | create_el2nod_matrices
|
---|
| 72 | old_direction=zeros(nods,1);
|
---|
| 73 |
|
---|
| 74 | %setup some fake distributions.
|
---|
| 75 | z_thick_bar=matrix_bar*z_thick;
|
---|
| 76 | z_surf_bar=matrix_bar*z_surf;
|
---|
| 77 | z_surf_xbar=matrix_xbar*z_surf;
|
---|
| 78 | z_surf_ybar=matrix_ybar*z_surf;
|
---|
| 79 | z_surf_x=el2nod\(el2nodRhs*z_surf_xbar);
|
---|
| 80 | z_surf_y=el2nod\(el2nodRhs*z_surf_ybar);
|
---|
| 81 | z_bed_bar=matrix_bar*z_bed;
|
---|
| 82 | z_bed_xbar=matrix_xbar*z_bed;
|
---|
| 83 | z_bed_ybar=matrix_ybar*z_bed;
|
---|
| 84 |
|
---|
| 85 | %data_elements and data_nodes are the elements and nodes where
|
---|
| 86 | %we have observations
|
---|
| 87 | data_elements=[1:nel]';
|
---|
| 88 | data_nodes=ones(nods,1);
|
---|
| 89 |
|
---|
| 90 | %initialize misfit between model velocity and observed velocity
|
---|
| 91 | J=zeros(nsteps,1);
|
---|
| 92 |
|
---|
| 93 | %Weighting: one areas where we don't want the control method to optimize
|
---|
| 94 | %the misfit, we can set weighting to 0.
|
---|
| 95 | weighting=ones(nods,1);
|
---|
| 96 |
|
---|
| 97 | %optimization parameters for the matlab functoin fminbnd
|
---|
| 98 | options=optimset('fminbnd');
|
---|
| 99 | options=optimset(options,'Display','iter');
|
---|
| 100 | options=optimset(options,'MaxFunEvals',maxiter);
|
---|
| 101 | options=optimset(options,'MaxIter',100);
|
---|
| 102 | options=optimset(options,'TolX',tolx);
|
---|
| 103 |
|
---|
| 104 | %build useful matrices.
|
---|
| 105 | setup_control
|
---|
| 106 |
|
---|
| 107 | %setup initial drag paramter distribution to startup the optimization.
|
---|
| 108 | drag_type=md.drag_type;
|
---|
| 109 | qcoeff=md.q;
|
---|
| 110 | pcoeff=md.p;
|
---|
| 111 |
|
---|
[408] | 112 | if strcmp(md.control_type,'B')
|
---|
| 113 | %setup initial flow law paramter distribution to startup the optimization.
|
---|
| 114 | B_bar_ini=(B_ini(index(:,1))+B_ini(index(:,2))+B_ini(index(:,3)))/3;
|
---|
| 115 | B=B_ini; %variables used in optimziation are B and B_bar.
|
---|
| 116 | B_bar=B_bar_ini;
|
---|
| 117 | else
|
---|
| 118 | B_bar=(md.B(index(:,1))+md.B(index(:,2))+md.B(index(:,3)))/3;
|
---|
| 119 | if (drag_type~=2), error('md.drag_type must be 2 for control methods'); end
|
---|
| 120 | drag_coeff_bar_ini=(drag_coeff_ini(index(:,1))+drag_coeff_ini(index(:,2))+drag_coeff_ini(index(:,3)))/3;
|
---|
| 121 | drag_coeff=drag_coeff_ini; %variables used in optimziation are drag_coeff and drag_coeff_bar.
|
---|
| 122 | drag_coeff_bar=drag_coeff_bar_ini;
|
---|
| 123 | end
|
---|
| 124 |
|
---|
[35] | 125 | %check that the model is not a pure ice shelf (no friction on ice shelves)
|
---|
[408] | 126 | if strcmp(md.control_type,'drag') & (length(find(drag_coeff))==0),
|
---|
[35] | 127 | disp(sprintf('\n No drag pure for ice shelves => STOP'))
|
---|
| 128 | return
|
---|
| 129 | end
|
---|
| 130 |
|
---|
| 131 | %nu_bar=10^14*ones(nel,1); %initial element viscosity distribution.
|
---|
[170] | 132 | nu_bar=viscosity(index,nel,alpha,beta,[],[],B_bar,glen_coeff);
|
---|
[35] | 133 |
|
---|
| 134 | %%%AK velfinder; %forward model that determines first velocity field used
|
---|
| 135 | %to start optimization.
|
---|
| 136 | disp('calculating the velocity with the initial parameters')
|
---|
| 137 | c_velfinder;
|
---|
| 138 |
|
---|
[170] | 139 | if strcmp(md.control_type,'B'),
|
---|
[35] | 140 | for niteration=1:nsteps,
|
---|
| 141 |
|
---|
| 142 | disp([' Step #' num2str(niteration) ' on ' num2str(nsteps)]);
|
---|
| 143 |
|
---|
| 144 | % Compute search direction -
|
---|
| 145 | [u,v,adjointu,adjointv,direction]= ...
|
---|
| 146 | grad_J_flow(Rhs,S,F,P,P0,area,specified_velocity,nods,vx_obs,vy_obs,...
|
---|
| 147 | index,x,y,nel,rho_ice,g,weighting,alpha,beta,z_thick_bar,B_bar);
|
---|
| 148 |
|
---|
| 149 | if md.plot==1,
|
---|
| 150 | if niteration==1
|
---|
| 151 | scrsz = get(0,'ScreenSize');
|
---|
| 152 | figure('Position',[scrsz(3)/3 scrsz(4)*1.8/3 scrsz(3)/3 scrsz(4)/3])
|
---|
| 153 | end
|
---|
| 154 | plotmodel(md,'data',sqrt(u.^2+v.^2)*yts,'title','Modeled velocity',...
|
---|
| 155 | 'data',sqrt(adjointu.^2+adjointv.^2)*yts,'title','Adjoint vectors','colorbar#all', 'on',...
|
---|
| 156 | 'data',sqrt(vx_obs.^2+vy_obs.^2)*yts,'title','Observed velocity',...
|
---|
| 157 | 'data',100*abs(sqrt(vx_obs.^2+vy_obs.^2)-sqrt(u.^2+v.^2))./sqrt(vx_obs.^2+vy_obs.^2),'title','Relative misfit','caxis#4',[0 100],'figure',1);drawnow;
|
---|
| 158 | end
|
---|
| 159 |
|
---|
| 160 | %Keep track of u and v to use in objectivefunction_C:
|
---|
| 161 | u_objective=u;
|
---|
| 162 | v_objective=v;
|
---|
| 163 |
|
---|
| 164 | %Orthogonalize direction
|
---|
| 165 | direction=real(direction);
|
---|
| 166 | direction=direction/sqrt(direction'*direction);
|
---|
| 167 |
|
---|
| 168 | % rough orthagonalization
|
---|
| 169 | direction=direction-(direction'*old_direction)*old_direction;
|
---|
| 170 | old_direction=direction;
|
---|
| 171 |
|
---|
| 172 | %visualize direction
|
---|
| 173 | if md.plot==1,
|
---|
| 174 | if niteration==1
|
---|
| 175 | scrsz = get(0,'ScreenSize');
|
---|
| 176 | figure('Position',[10 scrsz(4)*1/3 scrsz(3)/3 scrsz(4)/3])
|
---|
| 177 | end
|
---|
| 178 | plotmodel(md,'data',direction,'title','Orthogonal direction','figure',2);drawnow;
|
---|
| 179 | end
|
---|
| 180 |
|
---|
| 181 |
|
---|
| 182 | % normalize direction to 10^7, so that when variations on B are computed
|
---|
| 183 | %they will be significant.
|
---|
| 184 | if abs(max(direction))>abs(min(direction))
|
---|
| 185 | direction=10^7*direction/abs(max(direction));
|
---|
| 186 | else
|
---|
| 187 | direction=10^7*direction/abs(min(direction));
|
---|
| 188 | end
|
---|
| 189 |
|
---|
| 190 | %during optimization, bounds on B variations can vary. Here, they are less
|
---|
| 191 | %strict in the first iteration.
|
---|
| 192 | if niteration<=2,
|
---|
| 193 | upperbound=20;
|
---|
| 194 | lowerbound=0;
|
---|
| 195 | else
|
---|
| 196 | upperbound=10;
|
---|
| 197 | lowerbound=0;
|
---|
| 198 | end
|
---|
| 199 |
|
---|
| 200 | %search the multiplicative constant to the direction, that will
|
---|
| 201 | %be used to modify B.
|
---|
| 202 |
|
---|
| 203 | search_constant=fminbnd('objectivefunction_C_flow',lowerbound,upperbound, ...
|
---|
| 204 | options,B,glen_coeff,direction,Rhs,S,F,P,specified_velocity,nods,nel,vx_obs, ...
|
---|
| 205 | vy_obs,u_objective, v_objective, index,alpha, beta, area,weighting,rowD,colD,rowDshort,colDshort,valueuu,valuevv,valueuv,matrix_bar,criterion);
|
---|
| 206 |
|
---|
| 207 |
|
---|
| 208 | %update value of B
|
---|
| 209 | B_old=B;
|
---|
| 210 | B_new=B+search_constant*direction;
|
---|
| 211 | B=B_new;
|
---|
| 212 | pos=find(B<0);B(pos)=-B(pos);
|
---|
| 213 |
|
---|
| 214 | %Average B over elements:
|
---|
| 215 | B_bar=(B(index(:,1))+B(index(:,2))+B(index(:,3)))/3;
|
---|
| 216 |
|
---|
| 217 | %visualize new distribution.
|
---|
| 218 | if md.plot==1,
|
---|
| 219 | if niteration==1
|
---|
| 220 | scrsz = get(0,'ScreenSize');
|
---|
| 221 | figure('Position',[scrsz(3)*1.97/3 scrsz(4)*1/3 scrsz(3)/3 scrsz(4)/3])
|
---|
| 222 | end
|
---|
| 223 | plotmodel(md,'data',B*B_to_p,'title',['B at iteration ' num2str(niteration)],'caxis',[200 900],'colorbar','on','figure',3);drawnow;
|
---|
| 224 | end
|
---|
| 225 |
|
---|
| 226 | %evaluate new misfit.
|
---|
| 227 | %@@@AK load F_file %this file was created in objectivefunction_C, and is reloaded
|
---|
| 228 | %here to win some computation time.
|
---|
| 229 | load F_file
|
---|
| 230 |
|
---|
| 231 | J(niteration)=objectivefunction_C_flow(0,B,glen_coeff,direction,Rhs,S,F,P,specified_velocity,nods,nel,vx_obs, ...
|
---|
| 232 | vy_obs,u_objective, v_objective, index,alpha, beta, area,weighting,rowD,colD,rowDshort,colDshort,valueuu,valuevv,valueuv,matrix_bar,criterion);
|
---|
| 233 |
|
---|
| 234 | %do a backup every 5 iterations
|
---|
| 235 | if(mod(niteration,5)==0),
|
---|
| 236 | save temporary_control_results_flow B B_bar u v J direction
|
---|
| 237 | end
|
---|
| 238 | end
|
---|
| 239 |
|
---|
[674] | 240 | %Load results onto md:
|
---|
| 241 | md.results.control.step=1;
|
---|
| 242 | md.results.control.time=0;
|
---|
| 243 | md.results.control.J=J;
|
---|
| 244 | md.results.control.parameter=B;
|
---|
| 245 | md.results.control.vx=u;
|
---|
| 246 | md.results.control.vy=v;
|
---|
| 247 | md.results.control.vel=sqrt(u.^2+v.^2);
|
---|
[35] | 248 |
|
---|
[408] | 249 | elseif strcmp(md.control_type,'drag'),
|
---|
[35] | 250 | for niteration=1:nsteps,
|
---|
| 251 |
|
---|
| 252 | disp([' Step #' num2str(niteration) ' on ' num2str(nsteps)]);
|
---|
| 253 |
|
---|
| 254 | % Compute search direction -
|
---|
| 255 | [u,v,adjointu,adjointv,direction]= ...
|
---|
| 256 | grad_J_drag(Rhs,S,F,P,P0,area,specified_velocity,nods,vx_obs,vx_obs,...
|
---|
| 257 | index,x,y,nel,rho_ice,g,weighting,alpha,beta,z_thick_bar,drag_coeff,drag_coeff_bar);
|
---|
| 258 |
|
---|
| 259 | if md.plot==1,
|
---|
| 260 | if niteration==1
|
---|
| 261 | scrsz = get(0,'ScreenSize');
|
---|
| 262 | figure('Position',[scrsz(3)/3 scrsz(4)*1.8/3 scrsz(3)/3 scrsz(4)/3])
|
---|
| 263 | end
|
---|
| 264 | plotmodel(md,'data',sqrt(u.^2+v.^2),'title','Modeled velocity',...
|
---|
| 265 | 'data',sqrt(adjointu.^2+adjointv.^2),'title','Adjoint vectors','colorbar', 'all',...
|
---|
| 266 | 'data',sqrt(vx_obs.^2+vy_obs.^2),'title','Observed velocity',...
|
---|
| 267 | 'data',100*abs(sqrt(vx_obs.^2+vy_obs.^2)-sqrt(u.^2+v.^2))./sqrt(vx_obs.^2+vy_obs.^2),'title','Relative misfit','caxis#3',[0 100],'figure',1);drawnow;
|
---|
| 268 | end
|
---|
| 269 |
|
---|
| 270 | %Keep track of u and v to use in objectivefunction_C:
|
---|
| 271 | u_objective=u;
|
---|
| 272 | v_objective=v;
|
---|
| 273 |
|
---|
[408] | 274 | %Orthogonalize direction
|
---|
| 275 | direction=real(direction);
|
---|
| 276 | direction=direction/sqrt(direction'*direction);
|
---|
| 277 | pos=find(nodes_on_iceshelf);
|
---|
| 278 | direction(pos)=0;
|
---|
[35] | 279 |
|
---|
| 280 |
|
---|
[408] | 281 | % rough orthagonalization
|
---|
| 282 | direction=direction-(direction'*old_direction)*old_direction;
|
---|
| 283 | old_direction=direction;
|
---|
[35] | 284 |
|
---|
[408] | 285 | %visualize direction
|
---|
| 286 | if md.plot==1,
|
---|
| 287 | if niteration==1
|
---|
| 288 | scrsz = get(0,'ScreenSize');
|
---|
| 289 | figure('Position',[10 scrsz(4)*1/3 scrsz(3)/3 scrsz(4)/3])
|
---|
| 290 | end
|
---|
| 291 | plotmodel(md,'data',direction,'title','Orthogonal direction','figure',2);drawnow;
|
---|
| 292 | end
|
---|
[35] | 293 |
|
---|
| 294 |
|
---|
[408] | 295 | % normalize direction to 50, so that when variations on drag are computed
|
---|
| 296 | %they will be significant.
|
---|
| 297 | direction=50*direction/max(abs(direction));
|
---|
[35] | 298 |
|
---|
[408] | 299 | %during optimization, bounds on drag variations can vary. Here, they are less
|
---|
| 300 | %strict in the first iteration.
|
---|
| 301 | if niteration<=2,
|
---|
| 302 | upperbound=2;
|
---|
| 303 | lowerbound=0;
|
---|
| 304 | else
|
---|
| 305 | upperbound=1;
|
---|
| 306 | lowerbound=0;
|
---|
| 307 | end
|
---|
[35] | 308 |
|
---|
[408] | 309 | %search the multiplicative constant to the direction, that will
|
---|
| 310 | %be used to modify drag.
|
---|
[35] | 311 |
|
---|
[408] | 312 | search_constant=fminbnd('objectivefunction_C_drag',lowerbound,upperbound, ...
|
---|
| 313 | options,B,glen_coeff,drag_coeff,direction,Rhs,S,F,P,specified_velocity,nods,nel,vx_obs, ...
|
---|
| 314 | vy_obs,u_objective, v_objective, index,alpha, beta, area,weighting,rowD,colD,rowDshort,colDshort,valueuu,valuevv,valueuv,matrix_bar,criterion);
|
---|
[35] | 315 |
|
---|
[408] | 316 | %update value of drag
|
---|
| 317 | drag_coeff_old=drag_coeff;
|
---|
| 318 | drag_coeff_new=drag_coeff+search_constant*direction;
|
---|
| 319 | drag_coeff=drag_coeff_new;
|
---|
[35] | 320 |
|
---|
[408] | 321 | if length(find(drag_coeff<0))~=0,
|
---|
| 322 | disp(sprintf('\n Some basal drag coefficient negative => STOP'))
|
---|
| 323 | break
|
---|
| 324 | end
|
---|
[35] | 325 |
|
---|
[408] | 326 | %Average drag over elements:
|
---|
| 327 | if niteration==1
|
---|
| 328 | scrsz = get(0,'ScreenSize');
|
---|
| 329 | figure('Position',[scrsz(3)*1.97/3 scrsz(4)*1/3 scrsz(3)/3 scrsz(4)/3])
|
---|
| 330 | end
|
---|
| 331 | drag_coeff_bar=(drag_coeff(index(:,1))+drag_coeff(index(:,2))+drag_coeff(index(:,3)))/3;
|
---|
[35] | 332 |
|
---|
[408] | 333 | %visualize new distribution.
|
---|
| 334 | if md.plot==1,
|
---|
| 335 | plotmodel(md,'data',drag_coeff,'title',['Drag at iteration ' num2str(niteration)],'caxis',[0 1000],'colorbar','on','figure',3);drawnow;
|
---|
| 336 | end
|
---|
[35] | 337 |
|
---|
[408] | 338 | %evaluate new misfit.
|
---|
| 339 | %@@@AK load F_file %this file was created in objectivefunction_C, and is reloaded
|
---|
[35] | 340 | %here to win some computation time.
|
---|
| 341 | load F_file
|
---|
| 342 |
|
---|
| 343 | J(niteration)=objectivefunction_C_drag(0,B,glen_coeff,drag_coeff,direction,Rhs,S,F,P,specified_velocity,nods,nel,vx_obs, ...
|
---|
| 344 | vy_obs,u_objective, v_objective, index,alpha, beta, area,weighting,rowD,colD,rowDshort,colDshort,valueuu,valuevv,valueuv,matrix_bar,criterion);
|
---|
| 345 | disp(J(niteration));
|
---|
| 346 |
|
---|
| 347 | %do a backup every 5 iterations
|
---|
| 348 | if(mod(niteration,5)==0),
|
---|
| 349 | save temporary_control_results_drag drag_coeff drag_coeff_bar u v J direction
|
---|
| 350 | end
|
---|
| 351 | end
|
---|
| 352 | end
|
---|
| 353 |
|
---|
| 354 | function macayealcontrolusage();
|
---|
| 355 | disp('md=macayealcontrol(md)');
|
---|
| 356 | disp(' where md is a structure of class @model');
|
---|