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):
|
---|
22 | %if ~strcmp(md.control_type,'B')
|
---|
23 | % error('macayealcontrol error message: only ''B'' inversion supported yet');
|
---|
24 | %end
|
---|
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);
|
---|
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
|
---|
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 |
|
---|
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 |
|
---|
125 |
|
---|
126 |
|
---|
127 | %check that the model is not a pure ice shelf (no friction on ice shelves)
|
---|
128 | if strcmp(md.control_type,'drag') & (length(find(drag_coeff))==0),
|
---|
129 | disp(sprintf('\n No drag pure for ice shelves => STOP'))
|
---|
130 | return
|
---|
131 | end
|
---|
132 |
|
---|
133 | %nu_bar=10^14*ones(nel,1); %initial element viscosity distribution.
|
---|
134 | nu_bar=viscosity(index,nel,alpha,beta,[],[],B_bar,glen_coeff);
|
---|
135 |
|
---|
136 | %%%AK velfinder; %forward model that determines first velocity field used
|
---|
137 | %to start optimization.
|
---|
138 | disp('calculating the velocity with the initial parameters')
|
---|
139 | c_velfinder;
|
---|
140 |
|
---|
141 | if strcmp(md.control_type,'B'),
|
---|
142 | for niteration=1:nsteps,
|
---|
143 |
|
---|
144 | disp([' Step #' num2str(niteration) ' on ' num2str(nsteps)]);
|
---|
145 |
|
---|
146 | % Compute search direction -
|
---|
147 | [u,v,adjointu,adjointv,direction]= ...
|
---|
148 | grad_J_flow(Rhs,S,F,P,P0,area,specified_velocity,nods,vx_obs,vy_obs,...
|
---|
149 | index,x,y,nel,rho_ice,g,weighting,alpha,beta,z_thick_bar,B_bar);
|
---|
150 |
|
---|
151 | if md.plot==1,
|
---|
152 | if niteration==1
|
---|
153 | scrsz = get(0,'ScreenSize');
|
---|
154 | figure('Position',[scrsz(3)/3 scrsz(4)*1.8/3 scrsz(3)/3 scrsz(4)/3])
|
---|
155 | end
|
---|
156 | plotmodel(md,'data',sqrt(u.^2+v.^2)*yts,'title','Modeled velocity',...
|
---|
157 | 'data',sqrt(adjointu.^2+adjointv.^2)*yts,'title','Adjoint vectors','colorbar#all', 'on',...
|
---|
158 | 'data',sqrt(vx_obs.^2+vy_obs.^2)*yts,'title','Observed velocity',...
|
---|
159 | '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;
|
---|
160 | end
|
---|
161 |
|
---|
162 | %Keep track of u and v to use in objectivefunction_C:
|
---|
163 | u_objective=u;
|
---|
164 | v_objective=v;
|
---|
165 |
|
---|
166 | %Orthogonalize direction
|
---|
167 | direction=real(direction);
|
---|
168 | direction=direction/sqrt(direction'*direction);
|
---|
169 |
|
---|
170 |
|
---|
171 | % rough orthagonalization
|
---|
172 | direction=direction-(direction'*old_direction)*old_direction;
|
---|
173 | old_direction=direction;
|
---|
174 |
|
---|
175 | %visualize direction
|
---|
176 | if md.plot==1,
|
---|
177 | if niteration==1
|
---|
178 | scrsz = get(0,'ScreenSize');
|
---|
179 | figure('Position',[10 scrsz(4)*1/3 scrsz(3)/3 scrsz(4)/3])
|
---|
180 | end
|
---|
181 | plotmodel(md,'data',direction,'title','Orthogonal direction','figure',2);drawnow;
|
---|
182 | end
|
---|
183 |
|
---|
184 |
|
---|
185 | % normalize direction to 10^7, so that when variations on B are computed
|
---|
186 | %they will be significant.
|
---|
187 | if abs(max(direction))>abs(min(direction))
|
---|
188 | direction=10^7*direction/abs(max(direction));
|
---|
189 | else
|
---|
190 | direction=10^7*direction/abs(min(direction));
|
---|
191 | end
|
---|
192 |
|
---|
193 | %during optimization, bounds on B variations can vary. Here, they are less
|
---|
194 | %strict in the first iteration.
|
---|
195 | if niteration<=2,
|
---|
196 | upperbound=20;
|
---|
197 | lowerbound=0;
|
---|
198 | else
|
---|
199 | upperbound=10;
|
---|
200 | lowerbound=0;
|
---|
201 | end
|
---|
202 |
|
---|
203 | %search the multiplicative constant to the direction, that will
|
---|
204 | %be used to modify B.
|
---|
205 |
|
---|
206 | search_constant=fminbnd('objectivefunction_C_flow',lowerbound,upperbound, ...
|
---|
207 | options,B,glen_coeff,direction,Rhs,S,F,P,specified_velocity,nods,nel,vx_obs, ...
|
---|
208 | vy_obs,u_objective, v_objective, index,alpha, beta, area,weighting,rowD,colD,rowDshort,colDshort,valueuu,valuevv,valueuv,matrix_bar,criterion);
|
---|
209 |
|
---|
210 |
|
---|
211 | %update value of B
|
---|
212 | B_old=B;
|
---|
213 | B_new=B+search_constant*direction;
|
---|
214 | B=B_new;
|
---|
215 | pos=find(B<0);B(pos)=-B(pos);
|
---|
216 |
|
---|
217 | %Average B over elements:
|
---|
218 | B_bar=(B(index(:,1))+B(index(:,2))+B(index(:,3)))/3;
|
---|
219 |
|
---|
220 | %visualize new distribution.
|
---|
221 | if md.plot==1,
|
---|
222 | if niteration==1
|
---|
223 | scrsz = get(0,'ScreenSize');
|
---|
224 | figure('Position',[scrsz(3)*1.97/3 scrsz(4)*1/3 scrsz(3)/3 scrsz(4)/3])
|
---|
225 | end
|
---|
226 | plotmodel(md,'data',B*B_to_p,'title',['B at iteration ' num2str(niteration)],'caxis',[200 900],'colorbar','on','figure',3);drawnow;
|
---|
227 | end
|
---|
228 |
|
---|
229 | %evaluate new misfit.
|
---|
230 | %@@@AK load F_file %this file was created in objectivefunction_C, and is reloaded
|
---|
231 | %here to win some computation time.
|
---|
232 | load F_file
|
---|
233 |
|
---|
234 | J(niteration)=objectivefunction_C_flow(0,B,glen_coeff,direction,Rhs,S,F,P,specified_velocity,nods,nel,vx_obs, ...
|
---|
235 | vy_obs,u_objective, v_objective, index,alpha, beta, area,weighting,rowD,colD,rowDshort,colDshort,valueuu,valuevv,valueuv,matrix_bar,criterion);
|
---|
236 | disp(J(niteration));
|
---|
237 |
|
---|
238 | %do a backup every 5 iterations
|
---|
239 | if(mod(niteration,5)==0),
|
---|
240 | save temporary_control_results_flow B B_bar u v J direction
|
---|
241 | end
|
---|
242 | end
|
---|
243 |
|
---|
244 | %Load results onto md:
|
---|
245 | md.cont_J=J;
|
---|
246 | md.cont_parameter=B;
|
---|
247 | md.cont_vx=u;
|
---|
248 | md.cont_vy=sqrt(u.^2+v.^2);
|
---|
249 |
|
---|
250 | elseif strcmp(md.control_type,'drag'),
|
---|
251 | for niteration=1:nsteps,
|
---|
252 |
|
---|
253 | disp([' Step #' num2str(niteration) ' on ' num2str(nsteps)]);
|
---|
254 |
|
---|
255 | % Compute search direction -
|
---|
256 | [u,v,adjointu,adjointv,direction]= ...
|
---|
257 | grad_J_drag(Rhs,S,F,P,P0,area,specified_velocity,nods,vx_obs,vx_obs,...
|
---|
258 | index,x,y,nel,rho_ice,g,weighting,alpha,beta,z_thick_bar,drag_coeff,drag_coeff_bar);
|
---|
259 |
|
---|
260 | if md.plot==1,
|
---|
261 | if niteration==1
|
---|
262 | scrsz = get(0,'ScreenSize');
|
---|
263 | figure('Position',[scrsz(3)/3 scrsz(4)*1.8/3 scrsz(3)/3 scrsz(4)/3])
|
---|
264 | end
|
---|
265 | plotmodel(md,'data',sqrt(u.^2+v.^2),'title','Modeled velocity',...
|
---|
266 | 'data',sqrt(adjointu.^2+adjointv.^2),'title','Adjoint vectors','colorbar', 'all',...
|
---|
267 | 'data',sqrt(vx_obs.^2+vy_obs.^2),'title','Observed velocity',...
|
---|
268 | '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;
|
---|
269 | end
|
---|
270 |
|
---|
271 | %Keep track of u and v to use in objectivefunction_C:
|
---|
272 | u_objective=u;
|
---|
273 | v_objective=v;
|
---|
274 |
|
---|
275 | %Orthogonalize direction
|
---|
276 | direction=real(direction);
|
---|
277 | direction=direction/sqrt(direction'*direction);
|
---|
278 | pos=find(nodes_on_iceshelf);
|
---|
279 | direction(pos)=0;
|
---|
280 |
|
---|
281 |
|
---|
282 | % rough orthagonalization
|
---|
283 | direction=direction-(direction'*old_direction)*old_direction;
|
---|
284 | old_direction=direction;
|
---|
285 |
|
---|
286 | %visualize direction
|
---|
287 | if md.plot==1,
|
---|
288 | if niteration==1
|
---|
289 | scrsz = get(0,'ScreenSize');
|
---|
290 | figure('Position',[10 scrsz(4)*1/3 scrsz(3)/3 scrsz(4)/3])
|
---|
291 | end
|
---|
292 | plotmodel(md,'data',direction,'title','Orthogonal direction','figure',2);drawnow;
|
---|
293 | end
|
---|
294 |
|
---|
295 |
|
---|
296 | % normalize direction to 50, so that when variations on drag are computed
|
---|
297 | %they will be significant.
|
---|
298 | direction=50*direction/max(abs(direction));
|
---|
299 |
|
---|
300 | %during optimization, bounds on drag variations can vary. Here, they are less
|
---|
301 | %strict in the first iteration.
|
---|
302 | if niteration<=2,
|
---|
303 | upperbound=2;
|
---|
304 | lowerbound=0;
|
---|
305 | else
|
---|
306 | upperbound=1;
|
---|
307 | lowerbound=0;
|
---|
308 | end
|
---|
309 |
|
---|
310 | %search the multiplicative constant to the direction, that will
|
---|
311 | %be used to modify drag.
|
---|
312 |
|
---|
313 | search_constant=fminbnd('objectivefunction_C_drag',lowerbound,upperbound, ...
|
---|
314 | options,B,glen_coeff,drag_coeff,direction,Rhs,S,F,P,specified_velocity,nods,nel,vx_obs, ...
|
---|
315 | vy_obs,u_objective, v_objective, index,alpha, beta, area,weighting,rowD,colD,rowDshort,colDshort,valueuu,valuevv,valueuv,matrix_bar,criterion);
|
---|
316 |
|
---|
317 | %update value of drag
|
---|
318 | drag_coeff_old=drag_coeff;
|
---|
319 | drag_coeff_new=drag_coeff+search_constant*direction;
|
---|
320 | drag_coeff=drag_coeff_new;
|
---|
321 |
|
---|
322 | if length(find(drag_coeff<0))~=0,
|
---|
323 | disp(sprintf('\n Some basal drag coefficient negative => STOP'))
|
---|
324 | break
|
---|
325 | end
|
---|
326 |
|
---|
327 | %Average drag over elements:
|
---|
328 | if niteration==1
|
---|
329 | scrsz = get(0,'ScreenSize');
|
---|
330 | figure('Position',[scrsz(3)*1.97/3 scrsz(4)*1/3 scrsz(3)/3 scrsz(4)/3])
|
---|
331 | end
|
---|
332 | drag_coeff_bar=(drag_coeff(index(:,1))+drag_coeff(index(:,2))+drag_coeff(index(:,3)))/3;
|
---|
333 |
|
---|
334 | %visualize new distribution.
|
---|
335 | if md.plot==1,
|
---|
336 | plotmodel(md,'data',drag_coeff,'title',['Drag at iteration ' num2str(niteration)],'caxis',[0 1000],'colorbar','on','figure',3);drawnow;
|
---|
337 | end
|
---|
338 |
|
---|
339 | %evaluate new misfit.
|
---|
340 | %@@@AK load F_file %this file was created in objectivefunction_C, and is reloaded
|
---|
341 | %here to win some computation time.
|
---|
342 | load F_file
|
---|
343 |
|
---|
344 | J(niteration)=objectivefunction_C_drag(0,B,glen_coeff,drag_coeff,direction,Rhs,S,F,P,specified_velocity,nods,nel,vx_obs, ...
|
---|
345 | vy_obs,u_objective, v_objective, index,alpha, beta, area,weighting,rowD,colD,rowDshort,colDshort,valueuu,valuevv,valueuv,matrix_bar,criterion);
|
---|
346 | disp(J(niteration));
|
---|
347 |
|
---|
348 | %do a backup every 5 iterations
|
---|
349 | if(mod(niteration,5)==0),
|
---|
350 | save temporary_control_results_drag drag_coeff drag_coeff_bar u v J direction
|
---|
351 | end
|
---|
352 | end
|
---|
353 | end
|
---|
354 |
|
---|
355 | function macayealcontrolusage();
|
---|
356 | disp('md=macayealcontrol(md)');
|
---|
357 | disp(' where md is a structure of class @model');
|
---|