2 %
function demo_rb_gui(model,detailed_data,[reduced_data],plot_params[,title, callbackfigure, cbhandle])
3 % reduced basis demo with sliders
8 %
function demo_rb_gui(model,detailed_data,[reduced_data],plot_params[,title])
@endcode
10 % Creates a gui
for RB-demo on a prescribed grid specified in 'detailed_data'.
11 % if a title is specified, this is set as the figure name. If further
12 % parameters are set, the call is assumed to stem from a callback-function if
13 % detailed_data, offline_data and params are empty, a default basis is chosen
14 % from matlab/datafiles/ if only detailed_data and params is specified, a
15 % offline_precomputation phase is performed.
17 % Bernard Haasdonk 20.7.2006
19 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
21 if nargin < 6 % no callback,open new figure
26 % disp([
'no arguments passed, so loading dummy reduced basis +' ...
28 % disp(
'the basis is constructed from the upper range-parameters');
29 % disp(
'so this is approximated particularly well, other parameters bad.');
30 % fn = fullfile(rbmatlabhome,
'tmpdata',
'dummy_reduced_basis.mat');
32 % tmp.params.init_values_algorithm =
'init_values_cog';
33 % tmp.params.operators_algorithm =
'operators_conv_diff';
34 % tmp.params.lxf_lambda = 1.0194e+003;
35 % tmp.params.data_const_in_time = 1;
36 % tmp.params.T = 0.05;
38 % list{4}= ['RB suited for upper bounds' ...
40 % fn = fullfile(rbmatlabhome,
'tmpdata',
'dummy_offline_data_const.mat');
42 % list{2} = tmp.offline_data;
43 disp([
'no arguments passed, so loading default reduced basis +' ...
45 disp(
'the basis contains vectors generated by PCAfixspace ');
46 disp(
'on an equidistant 5x5x5 grid in the mu-range');
47 % disp(
'guaranteeing the error estimator < 1e-7 on this grid ');
48 fn = fullfile(rbmatlabhome,
'demos',
'datafiles',...
49 'test_rb_lin_evol_data');
50 %
'demo_lin_evol_detailed_data.mat');
51 %
'RB_extension_PCA_fixspace_extended.mat');
53 list{2} = tmp.detailed_data;
54 %tmp.model.axis_tight = 1;
55 %tmp.model.descr = [];
56 %tmp.model.get_rb_size = @get_rb_size_default;
57 %tmp.model.N = get_rb_size(tmp.model,tmp.detailed_data);
58 %%% the following will update the model,
if fields have been missing:
59 % update_model_and_save_to_file = 1;
60 update_model_and_save_to_file = 0;
61 if update_model_and_save_to_file
62 model = lin_evol_model_default;
64 detailed_data = tmp.detailed_data;
65 save(fn,
'model',
'detailed_data');
66 disp(
'transformed precomputed datafile successful');
68 clear(
'detailed_data',
'model')
70 if ~isfield(tmp.model,'descr')
73 tmp.model.get_rb_size = @get_rb_size_default;
76 list{5}= ['RB of ',num2str(size(tmp.detailed_data.RB,2)), ...
77 ' vectors obtained by PCAfixspace'];
87 list{5} =
'RB Demo GUI';
91 if ~isfield(ud.model,
'descr') && ~isa(ud.model,
'IReducedModel')
94 ud.descr = ud.model.descr;
95 if isempty(ud.descr) % copy model to descr for models with empty descr
98 if isstruct(list{2}) || isa(list{2},
'IDetailedData')
99 ud.detailed_data = list{2};
102 if ~isfield(ud.model,
'crb_enabled')
103 ud.model.crb_enabled = 0;
106 if ud.model.crb_enabled
107 ud.model.set_Mratio(ud.detailed_data, 0.5);
110 if ~isfield(ud.descr,'is_stationary')
111 ud.descr.is_stationary = 0; % for evolution problems
114 ud.plot_params = list{4};
115 if isempty(ud.plot_params)
116 % copy some fields from model
117 if isfield(ud.descr,
'yscale_uicontrols')
118 ud.plot_params.yscale_uicontrols = ud.descr.yscale_uicontrols;
120 if isfield(ud.descr,'xscale_gui');
121 ud.plot_params.xscale_gui = ud.descr.xscale_gui;
123 if isfield(ud.descr,'clim');
124 ud.plot_params.clim = ud.descr.clim;
126 if isfield(ud.descr,'show_colorbar');
127 ud.plot_params.show_colorbar = ud.descr.show_colorbar;
129 if isfield(ud.descr,'geometry_transformation');
130 ud.plot_params.geometry_transformation = ud.descr.geometry_transformation;
132 if isfield(ud.model,'axis_tight')
133 ud.plot_params.axis_tight = ud.model.axis_tight;
135 if isfield(ud.model,'axis_equal')
136 ud.plot_params.axis_tight = ud.model.axis_equal;
138 if isfield(ud.model,'yscale_uicontrols')
139 ud.plot_params.yscale_uicontrols = ud.model.yscale_uicontrols;
141 if isfield(ud.model,'xscale_gui');
142 ud.plot_params.xscale_gui = ud.model.xscale_gui;
144 if isfield(ud.model,'clim');
145 ud.plot_params.clim = ud.descr.clim;
147 if isfield(ud.model,'show_colorbar');
148 ud.plot_params.show_colorbar = ud.model.show_colorbar;
150 if isfield(ud.model,'geometry_transformation');
151 ud.plot_params.geometry_transformation = ud.model.geometry_transformation;
156 % ud.grid = ud.detailed_data.grid;
158 disp(
'Preparing offline-quantities...')
160 ud.reduced_data = gen_reduced_data(ud.model, ud.detailed_data);
162 ud.reduced_data = list{3};
165 set(f,
'Name',list{5});
168 set(f,
'Userdata',ud);
169 % disp([
'set userdata to fig ',num2str(f)]);
171 % disp([
'userdata is not set to fig ',num2str(f)]);
174 %
if ~isfield(ud.model,
'geometry_transformation')
175 % ud.model.geometry_transformation =
'none';
178 di = 0.05; % relative distance between gui_objects
181 yscale_uicontrols = 1.0;
185 if isfield(ud.plot_params,
'yscale_uicontrols')
186 yscale_uicontrols = ud.plot_params.yscale_uicontrols;
188 if isfield(ud.plot_params,'xscale_gui')
189 xscale_gui = ud.plot_params.xscale_gui;
192 if isfield(ud.plot_params,'hide_N_ruler')
197 p = get(gcf,'Position');
198 p(3) = p(3) * xscale_gui;
199 set(gcf,'Position',p);
201 % create one txt and slider for each mu_name
202 for i=1:length(ud.model.mu_names)
203 % set mu-param-values to minimum of ranges as the position of a
204 % slider does not seem to be adjustable
205 tmprange = cell2mat(ud.model.mu_ranges);
206 ud.model = set_mu(ud.model,tmprange(1:2:end));
207 mu = get_mu(ud.model);
209 %val = ud.model.(ud.model.mu_names{i});
210 ra = ud.model.mu_ranges{i};
211 norm_val = (val-ra(1))/(ra(2)-ra(1));
212 txt(i) = uicontrol(f,
'Style',
'text',
'Units',
'normalized',...
214 [di,(i*di+(i-1)*textheight)*yscale_uicontrols,...
215 textwidth,textheight*yscale_uicontrols],...
216 'String',[ud.model.mu_names{i},
'=',num2str(val)],...
217 'Tag',[
'text_',ud.model.mu_names{i}]);
218 slider(i) = uicontrol(f,
'Style',
'slider',
'Units',
'normalized',...
220 [ 2*di+textwidth, ...
221 (i*di+(i-1)*textheight)*yscale_uicontrols, ...
223 textheight*yscale_uicontrols],...
224 'Tag',[
'slider_',ud.model.mu_names{i}],...
227 'value',norm_val, ...
228 'Callback',
'demo_rb_gui([],[],[],[],[], gcbf,gcbo)');
229 %
'Min',ud.model.mu_ranges{i}(1), ...
230 %
'Max',ud.model.mu_ranges{i}(2), ...
233 set(f,
'Userdata',ud);
236 % N = number of RB slider
237 i = length(ud.model.mu_names)+1;
238 if isstruct(ud.model) && ~isfield(ud.model,
'N')
239 ud.model.N = get_rb_size(ud.model,ud.detailed_data);
241 Nmax = get_rb_size(ud.model, ud.detailed_data);
245 set(f,'Userdata',ud);
246 txt(i) = uicontrol(f,'Style','text','Units','normalized',...
248 [di,(i*di+(i-1)*textheight)*yscale_uicontrols,...
249 textwidth,textheight*yscale_uicontrols],...
250 'String',['RB-size N = ',num2str(ud.model.N)],...
252 N = get_rb_size(ud.model,ud.detailed_data);
254 slider_increment = [1/((get_rb_size(ud.model, ...
255 ud.detailed_data))-1), ...
258 slider_increment = [0,0];
260 slider(i) = uicontrol(f,'Style','slider','Units','normalized',...
263 (i*di + (i-1)*textheight)*yscale_uicontrols, ...
264 1-3*di-textwidth,textheight*yscale_uicontrols],...
268 'value',ud.model.N, ...
269 'Callback','
demo_rb_gui([],[],[],[],[],gcbf,gcbo)',...
272 end; % of hide_N_slider
273 % M = number of CRB slider
274 % if isequal(ud.model.rb_problem_type,'nonlin_evol')...
275 % || isequal(ud.model.rb_problem_type,'richards')
276 if ud.model.crb_enabled
277 i = length(ud.model.mu_names)+2;
278 txt(i) = uicontrol(f,'Style','text','Units','normalized',...
280 [di,(i*di+(i-1)*textheight)*yscale_uicontrols,...
281 textwidth,textheight*yscale_uicontrols],...
282 'String',['CRB-size M = ',num2str(get_Mratio(ud.model,ud.detailed_data))],...
284 slider(i) = uicontrol(f,'Style','slider','Units','normalized',...
287 (i*di + (i-1)*textheight)*yscale_uicontrols, ...
288 1-3*di-textwidth,textheight*yscale_uicontrols],...
290 'min',0,'max',1,'value',...
291 get_Mratio(ud.model,ud.detailed_data), ...
292 'Callback','demo_rb_gui([],[],[],[],[],gcbf,gcbo)',...
293 'sliderstep',[1/100,0.1]);
294 end; % if nonlin-evol
297 if ~ud.descr.is_stationary
298 i = i+1; %length(ud.params.mu_names)+2; % or +3 if M-slider available!
299 txt(i) = uicontrol(f,'Style','text','Units','normalized',...
301 [di,(i*di+(i-1)*textheight)*yscale_uicontrols,...
302 textwidth,textheight*yscale_uicontrols],...
303 'String','timestep 0','Tag','text_time');
304 slider(i) = uicontrol(f,'Style','slider','Units','normalized',...
307 (i*di + (i-1)*textheight)*yscale_uicontrols, ...
308 1-3*di-textwidth,textheight*yscale_uicontrols],...
309 'Tag','slider_time',...
310 'min',0,'max',ud.descr.nt,'value',0, ...
311 'Callback','demo_rb_gui([],[],[],[],[],gcbf,gcbo)',...
312 'sliderstep',[1/ud.descr.nt,0.1]);
315 ax = axes('Units','normalized',...
316 'Position',[di,((i+2)*di+i*textheight)*yscale_uicontrols,...
317 1-2*di,1-((i+3)*di + i*textheight)*yscale_uicontrols],...
322 % sl = findobj(f,'Tag','slider1');
324 %set(sl,'max',ud.params.nt);
325 %set(sl,'sliderstep',[1/ud.params.nt,1]);
328 % set(sl,'max',size(ud.U,2));
329 % set(sl,'sliderstep',[1/(size(ud.U,2)-1),1]);
331 % set(sl,'max',size(ud.U,2)+eps);
332 % set(sl,'visible','off');
333 % % set(sl,'sliderstep',[0,0]);
336 % th = findobj(f,'Tag','text1');
337 % set(th,'String',['data slice 1']);
339 % set(f,'Resize','on');
342 if nargin >=6 % callback-function
345 ud =
get(cbf,
'Userdata');
346 % disp(
'performing callback call of plot_fv_data');
347 % set
new parameters in ud.params
348 tag =
get(cbo,
'Tag');
349 if isequal(tag,
'slider_time')
350 val = round(get(cbo,'value'));
351 set(cbo,'value',val);
352 % disp('check value of timestep!');
355 txt = findobj(cbf,'Tag','text_time');
356 set(txt,'String',['timestep = ',num2str(val)]);
358 elseif isequal(tag,'slider_N')
359 val = round(get(cbo,'value'));
360 set(cbo,'value',val);
362 txt = findobj(cbf,'Tag','text_N');
363 set(txt,'String',['RB-size N = ',num2str(val)]);
365 elseif isequal(tag,'slider_M')
366 val = get(cbo,'value');
367 set(cbo,'value',val);
368 ud.model.M = ceil(val * get_ei_size(ud.detailed_data.datatree));
369 txt = findobj(cbf,'Tag','text_M');
370 set(txt,'String',['CRB-size M = ',num2str(val)]);
374 for i=1:length(ud.model.mu_names)
375 if isequal(tag,['slider_',ud.model.mu_names{i}])
378 val =
get(cbo,
'value')*...
379 (ud.model.mu_ranges{i}(2)-...
380 ud.model.mu_ranges{i}(1)) + ...
381 ud.model.mu_ranges{i}(1);
383 mu = get_mu(ud.model);
385 ud.model = set_mu(ud.model,mu);
386 % ud.model.(char(ud.model.mu_names(i))) = val;
388 txt = findobj(cbf,
'Tag',[
'text_',ud.model.mu_names{i}]);
389 set(txt,
'String',[ud.model.mu_names{i},
' = ',num2str(val)]);
394 error(
'callback object unknown!!');
397 set(cbf,
'Userdata',ud);
398 % th = findobj(gcbf,
'Tag',
'text1');
399 % v = round(
get(gcbo,
'value'));
400 % set(gcbo,
'value',v)
401 % set(th,
'String',[
'data slice ',num2str(v)]);
402 replot(cbf,sourcestr);
405 function replot(f,tag)
406 ud = get(f,'Userdata');
408 enable_error_estimator = ud.model.enable_error_estimator;
410 if isfield(ud.plot_params,'hide_N_ruler')
411 hide_N_ruler = ud.plot_params.hide_N_ruler;
416 % if required perform new RB simulation
417 if ismember(tag,{
'new',
'mu',
'N',
'M'})
418 % in our problem the two bounds can be chosen 1
419 % ud.model.L_I_inv_norm_bound = 1;
420 % ud.model.L_E_norm_bound = 1;
421 % ud.model.descr.error_norm =
'l2';
423 % no affine decomposition: complete mu-dependent matrices are computed
424 %ud.params.decomp_mode = 0;
425 % in
case of 0, the mu must be set reasonably in advance, as the
426 % current parameters are used.
428 % compute offline-data (mu-independent!)
429 %offline_data = rb_offline_preparation(ud.RB,ud.grid,ud.params);
432 %
for i=1:length(ud.model.mu_names)
433 % mu = [mu, ud.model.(ud.model.mu_names{i})];
435 mu = get_mu(ud.model);
436 % prepare mu-independent but N-dependent online-data
437 %ud.params.N = size(ud.detailed_data.RB,2);
438 if ud.model.crb_enabled
439 disp([
'performing RB simulation with N = ',num2str(ud.model.N),...
440 ', M = ', num2str(ud.model.M),...
441 ' for mu = [',num2str(mu(:)
'),']
']);
444 disp(['performing RB simulation with N =
',num2str(ud.model.N),...
445 ' for mu = [
',num2str(mu(:)'),
']']);
447 disp([
'performing simulation for mu = [',num2str(mu(:)
'),']
']);
450 reduced_data = extract_reduced_data_subset(ud.model,ud.reduced_data);
452 % perform RB-simulation
453 ud.model = set_mu(ud.model,mu);
455 simulation_data = rb_simulation(ud.model,reduced_data);
457 rb_sim_data = rb_reconstruction(ud.model,ud.detailed_data,simulation_data);
458 % please remove later this U explicitly, instead use ud.sim_data;
459 %U = ud.model.get_dofs_from_sim_data(rb_sim_data);
460 ud.sim_data = rb_sim_data;
461 if enable_error_estimator
462 ud.Delta = simulation_data.Delta;
463 if isfield(simulation_data,'reslog
')
464 ud.reslog = simulation_data.reslog;
465 ud.eilog = simulation_data.eilog;
468 if isfield(ud.descr,'name_output_functional
')
469 ud.s = simulation_data.s;
470 if enable_error_estimator
471 ud.Delta_s = simulation_data.Delta_s;
474 if ~isfield(ud.plot_params,'clim
')
475 U = ud.model.get_dofs_from_sim_data(rb_sim_data);
476 ud.cmin = min(min(U));
477 ud.cmax = max(max(U));
478 if (ud.cmin==ud.cmax)
479 ud.cmin = ud.cmin-eps;
480 ud.cmax = ud.cmax+eps;
484 ud.cmin = ud.plot_params.clim(1);
485 ud.cmax = ud.plot_params.clim(2);
487 set(f,'Userdata
',ud);
491 % plot current slice;
493 if enable_error_estimator
494 disp(['error_estimator Delta(
',num2str(ud.timestep),') =
',...
495 num2str(ud.Delta(ud.timestep+1))]);
496 if isfield(ud.descr,'name_output_functional
')
497 disp(['output-estimator s(U(
',num2str(ud.timestep),')) =
',...
498 num2str(ud.s(ud.timestep+1))]);
499 disp(['output-error-estimator Delta_s(
',num2str(ud.timestep),...
500 ') =
', num2str(ud.Delta_s(ud.timestep+1))]);
502 if isfield(ud,'reslog
')
503 disp(['error_estimator reslog(
',num2str(ud.timestep),') =
',...
504 num2str(ud.reslog(ud.timestep+1))]);
505 disp(['error_estimator eilog(
',num2str(ud.timestep),') =
',...
506 num2str(ud.eilog(ud.timestep+1))]);
509 fprintf('plotting...
');
510 % in the following we use pcolor as builtin, as it is faster...
511 % for non-cartesian grids, the plot_element_data function must be used...
512 ax = findobj(f,'Tag
','axes1
');
514 % ud.plot_params is an overlay to the default plot data of the model
515 % not only in case of "bind to model", but always:
516 %ud.plot_params = merge_model_plot_params(ud.model,ud.plot_params);
517 if isfield(ud.plot_params, 'bind_to_model
') && ud.plot_params.bind_to_model
518 ud.plot_params = copy_model_descr_to_plot_params(ud.model, ...
521 if ~isfield(ud.plot_params,'axis_equal
')
522 ud.plot_params.axis_equal = 1;
524 if isequal(ud.descr.gridtype,'rectgrid') ...
525 && ( ~isfield(ud.plot_params, 'geometry_transformation
') || ...
526 isequal(ud.plot_params.geometry_transformation, 'none
') )...
527 && ( ~isfield(ud.plot_params, 'plot_type
') || ...
528 isequal(ud.plot_params.plot_type, 'none
') )
529 U = ud.model.get_dofs_from_sim_data(ud.sim_data);
530 d = reshape(U(:,ud.timestep+1),...
531 ud.descr.xnumintervals,ud.descr.ynumintervals)';
532 xr = ud.descr.xrange;
533 x = xr(1):(xr(2)-xr(1))/(ud.descr.xnumintervals):xr(2);
534 yr = ud.descr.yrange;
535 y = yr(1):(yr(2)-yr(1))/(ud.descr.ynumintervals):yr(2);
536 p=pcolor(x,y,[d,zeros(size(d,1),1);zeros(1,size(d,2)+1)]);
538 if (~isfield(ud.descr,
'geometry_transformation') || ...
539 isequal(ud.descr.geometry_transformation,
'none')) && ...
540 ~isfield(ud.plot_params,
'no_lines')
541 ud.plot_params.no_lines = 1;
543 % must be set externally,
if wanted:
544 % ud.params.show_colorbar = 1;
546 if ud.descr.is_stationary
548 % reduced model itself must allow plotting:
550 ud.detailed_data, ...
553 else % evolution problem
554 if isfield(ud.plot_params,
'plot_slice')
555 % timestep in range 0... nt!!
556 ud.plot_params.timestep = ud.timestep;
557 p = ud.plot_params.plot_slice(ud.descr,...
558 ud.detailed_data, ...
563 p = ud.plot_params.plot(ud.detailed_data.grid, ...
564 U(:,ud.timestep+1), ...
571 if ud.plot_params.axis_equal;
574 if isfield(ud.plot_params,
'axis_tight') && ud.plot_params.axis_tight
577 if isfield(ud.plot_params,
'clim')
578 set(ax,'Clim',ud.plot_params.clim);
580 set(ax,'Clim',[ud.cmin, ud.cmax]);
583 if isfield(ud.plot_params,'show_colorbar') && (ud.plot_params.show_colorbar)
584 if isfield(ud.plot_params,'colorbar_mode')
585 colorbar(ud.plot_params.colorbar_mode);
591 if isfield(ud.plot_params,'no_lines') && (ud.plot_params.no_lines)
592 set(p,'LineStyle','None');
function demo_rb_gui(varargin)
reduced basis demo with sliders
A cartesian rectangular grid in two dimensions with axis parallel elements.
function s1 = structcpy(s1, s2)
copies the fields of structure s2 into structure s1. If the field to be copied does not exist in s1 y...
function p = plot_sim_data(model, model_data, sim_data, plot_params)
function performing the plot of the simulation results as specified in model.