rbmatlab  1.16.09
 All Classes Namespaces Files Functions Variables Modules Pages
dune_demo_rb_gui.m
Go to the documentation of this file.
1 function dune_demo_rb_gui(varargin)
2 % function dune_demo_rb_gui(model,detailed_data,[reduced_data],plot_params[,title, callbackfigure, cbhandle])
3 % reduced basis demo with sliders for a remotely running DUNE-rb server
4 % application
5 %
6 
7 % Martin Drohmann 08.02.2012
8 % Bernard Haasdonk 20.7.2006
9 
10 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
11 % open figure
12 if nargin < 6 % no callback,open new figure
13  f = figure;
14  ud = [];
15  list = varargin;
16  if nargin==0
17  error(['no arguments passed, cannot load a reduced basis']);
18  end;
19 
20  if nargin <3
21  list{3} = [];
22  end;
23  if nargin <4
24  list{4} = [];
25  end;
26  if nargin <5
27  list{5} = 'Dune RB Demo GUI';
28  end;
29 
30  ud.model = list{1};
31  ud.descr = ud.model.descr;
32  if isa(list{2}, 'IDetailedData')
33  ud.detailed_data = list{2};
34  end;
35 
36 % if ud.model.crb_enabled
37 % ud.model.set_Mratio(ud.detailed_data, 0.5);
38 % end
39 
40  if ~isfield(ud.descr,'is_stationary')
41  ud.descr.is_stationary = 0; % for evolution problems
42  end
43 
44  ud.plot_params = list{4};
45  if isempty(ud.plot_params)
46  % copy some fields from model
47  if isfield(ud.descr,'yscale_uicontrols')
48  ud.plot_params.yscale_uicontrols = ud.descr.yscale_uicontrols;
49  end;
50  if isfield(ud.descr,'xscale_gui');
51  ud.plot_params.xscale_gui = ud.descr.xscale_gui;
52  end;
53  if isfield(ud.descr,'clim');
54  ud.plot_params.clim = ud.descr.clim;
55  end;
56  if isfield(ud.descr,'show_colorbar');
57  ud.plot_params.show_colorbar = ud.descr.show_colorbar;
58  end;
59  end;
60 % ud.grid = ud.detailed_data.grid;
61  if isempty(list{3})
62  disp('Preparing offline-quantities...')
63  % keyboard;
64  ud.reduced_data = gen_reduced_data(ud.model, ud.detailed_data);
65  else
66  ud.reduced_data = list{3};
67  end;
68  if length(list) > 4;
69  set(f,'Name',list{5});
70  end;
71  ud.timestep = 0;
72  set(f,'Userdata',ud);
73  di = 0.05; % relative distance between gui_objects
74  textwidth= 0.15;
75  textheight = 0.05;
76  yscale_uicontrols = 1.0;
77  xscale_gui = 1.0;
78 
79  if isfield(ud.plot_params,'yscale_uicontrols')
80  yscale_uicontrols = ud.plot_params.yscale_uicontrols;
81  end;
82  if isfield(ud.plot_params,'xscale_gui')
83  xscale_gui = ud.plot_params.xscale_gui;
84  end;
85 
86  % xscale gui
87  p = get(gcf,'Position');
88  p(3) = p(3) * xscale_gui;
89  set(gcf,'Position',p);
90 
91  % create one txt and slider for each mu_name
92  for i=1:length(ud.model.mu_names)
93  % set mu-param-values to minimum of ranges as the position of a
94  % slider does not seem to be adjustable
95  tmprange = cell2mat(ud.model.mu_ranges);
96  ud.model = set_mu(ud.model,tmprange(1:2:end));
97  mu = get_mu(ud.model);
98  val = mu(i);
99  ra = ud.model.mu_ranges{i};
100  norm_val = (val-ra(1))/(ra(2)-ra(1));
101  txt(i) = uicontrol(f,'Style','text','Units','normalized',...
102  'Position',...
103  [di,(i*di+(i-1)*textheight)*yscale_uicontrols,...
104  textwidth,textheight*yscale_uicontrols],...
105  'String',[ud.model.mu_names{i},'=',num2str(val)],...
106  'Tag',['text_',ud.model.mu_names{i}]);
107  slider(i) = uicontrol(f,'Style','slider','Units','normalized',...
108  'Position',...
109  [ 2*di+textwidth, ...
110  (i*di+(i-1)*textheight)*yscale_uicontrols, ...
111  1-3*di-textwidth,...
112  textheight*yscale_uicontrols],...
113  'Tag',['slider_',ud.model.mu_names{i}],...
114  'Min',0, ...
115  'Max',1, ...
116  'value',norm_val, ...
117  'Callback','dune_demo_rb_gui([],[],[],[],[], gcbf,gcbo)');
118  % 'Min',ud.model.mu_ranges{i}(1), ...
119 % 'Max',ud.model.mu_ranges{i}(2), ...
120 
121  end;
122  set(f,'Userdata',ud);
123 
124  % N = number of RB slider
125  i = length(ud.model.mu_names)+1;
126  txt(i) = uicontrol(f,'Style','text','Units','normalized',...
127  'Position', ...
128  [di,(i*di+(i-1)*textheight)*yscale_uicontrols,...
129  textwidth,textheight*yscale_uicontrols],...
130  'String',['RB-size N = ',num2str(get_rb_size(ud.detailed_data, ud.model))],...
131  'Tag','text_N');
132  slider(i) = uicontrol(f,'Style','slider','Units','normalized',...
133  'Position',...
134  [2*di+textwidth, ...
135  (i*di + (i-1)*textheight)*yscale_uicontrols, ...
136  1-3*di-textwidth,textheight*yscale_uicontrols],...
137  'Tag','slider_N',...
138  'min',1,'max', ...
139  get_rb_size(ud.detailed_data, ud.model),...
140  'value',ud.model.N, ...
141  'Callback','dune_demo_rb_gui([],[],[],[],[],gcbf,gcbo)',...
142  'sliderstep',...
143  [1/((get_rb_size(ud.detailed_data, ud.model))-1),0.1]);
144 
145 
146  % M = number of CRB slider
147  if ud.model.crb_enabled
148  i = length(ud.model.mu_names)+2;
149  txt(i) = uicontrol(f,'Style','text','Units','normalized',...
150  'Position', ...
151  [di,(i*di+(i-1)*textheight)*yscale_uicontrols,...
152  textwidth,textheight*yscale_uicontrols],...
153  'String',['CRB-size M = ',num2str(get_Mratio(ud.model,ud.detailed_data))],...
154  'Tag','text_M');
155  slider(i) = uicontrol(f,'Style','slider','Units','normalized',...
156  'Position',...
157  [2*di+textwidth, ...
158  (i*di + (i-1)*textheight)*yscale_uicontrols, ...
159  1-3*di-textwidth,textheight*yscale_uicontrols],...
160  'Tag','slider_M',...
161  'min',0,'max',1,'value',...
162  get_Mratio(ud.model,ud.detailed_data), ...
163  'Callback','dune_demo_rb_gui([],[],[],[],[],gcbf,gcbo)',...
164  'sliderstep',[1/100,0.1]);
165  end; % if nonlin-evol
166 
167  % time slider
168  if ~ud.descr.is_stationary
169  i = i+1; %length(ud.params.mu_names)+2; % or +3 if M-slider available!
170  txt(i) = uicontrol(f,'Style','text','Units','normalized',...
171  'Position', ...
172  [di,(i*di+(i-1)*textheight)*yscale_uicontrols,...
173  textwidth,textheight*yscale_uicontrols],...
174  'String','timestep 0','Tag','text_time');
175  slider(i) = uicontrol(f,'Style','slider','Units','normalized',...
176  'Position',...
177  [2*di+textwidth, ...
178  (i*di + (i-1)*textheight)*yscale_uicontrols, ...
179  1-3*di-textwidth,textheight*yscale_uicontrols],...
180  'Tag','slider_time',...
181  'min',0,'max',ud.descr.nt,'value',0, ...
182  'Callback','dune_demo_rb_gui([],[],[],[],[],gcbf,gcbo)',...
183  'sliderstep',[1/ud.descr.nt,0.1]);
184  end
185 
186  pos = [10 300 100 10];
187  ht = uicontrol(f,'Style','Text','Position',pos);
188  string = {'This is a string for the left text uicontrol.',...
189  'to be wrapped in Units of Pixels,',...
190  'with a position determined by TEXTWRAP.'};
191  % Wrap string, also returning a new position for ht
192  [outstring,newpos] = textwrap(ht,string)
193 
194  replot(f,'new');
195 % set(f,'Resize','on');
196 end;
197 
198 if nargin >=6 % callback-function
199  cbf = varargin{6};
200  cbo = varargin{7};
201  ud = get(cbf,'Userdata');
202 % disp('performing callback call of plot_fv_data');
203  % set new parameters in ud.params
204  tag = get(cbo,'Tag');
205  if isequal(tag,'slider_time')
206  val = round(get(cbo,'value'));
207  set(cbo,'value',val);
208 % disp('check value of timestep!');
209 % keyboard;
210  ud.timestep = val;
211  txt = findobj(cbf,'Tag','text_time');
212  set(txt,'String',['timestep = ',num2str(val)]);
213  sourcestr = 'time';
214  elseif isequal(tag,'slider_N')
215  val = round(get(cbo,'value'));
216  set(cbo,'value',val);
217  ud.model.N = val;
218  txt = findobj(cbf,'Tag','text_N');
219  set(txt,'String',['RB-size N = ',num2str(val)]);
220  sourcestr = 'N';
221  elseif isequal(tag,'slider_M')
222  val = get(cbo,'value');
223  set(cbo,'value',val);
224  ud.model.M = ceil(val * get_ei_size(ud.detailed_data.datatree));
225  txt = findobj(cbf,'Tag','text_M');
226  set(txt,'String',['CRB-size M = ',num2str(val)]);
227  sourcestr = 'M';
228  else
229  found_cbo = 0;
230  for i=1:length(ud.model.mu_names)
231  if isequal(tag,['slider_',ud.model.mu_names{i}])
232 % keyboard;
233  found_cbo = 1;
234  val = get(cbo,'value')*...
235  (ud.model.mu_ranges{i}(2)-...
236  ud.model.mu_ranges{i}(1)) + ...
237  ud.model.mu_ranges{i}(1);
238 
239  mu = get_mu(ud.model);
240  mu(i) = val;
241  ud.model = set_mu(ud.model,mu);
242 
243  txt = findobj(cbf,'Tag',['text_',ud.model.mu_names{i}]);
244  set(txt,'String',[ud.model.mu_names{i},' = ',num2str(val)]);
245  sourcestr = 'mu';
246  end;
247  end;
248  if ~found_cbo
249  error('callback object unknown!!');
250  end;
251  end;
252  set(cbf,'Userdata',ud);
253  replot(cbf,sourcestr);
254 end;
255 
256 function replot(f,tag)
257 ud = get(f,'Userdata');
258 
259 enable_error_estimator = ud.model.enable_error_estimator;
260 
261 data_reload_required = false;
262 % if required perform new RB simulation
263 if ismember(tag,{'new','mu','N','M'})
264  % in our problem the two bounds can be chosen 1
265  % ud.model.descr.error_norm = 'l2';
266 
267  data_reload_required = true;
268 
269  mu = get_mu(ud.model);
270  % prepare mu-independent but N-dependent online-data
271  %ud.params.N = size(ud.detailed_data.RB,2);
272  if ud.model.crb_enabled
273  disp(['performing RB simulation with N = ',num2str(ud.model.N),...
274  ', M = ', num2str(ud.model.M),...
275  ' for mu = [',num2str(mu(:)'),']']);
276  else
277  disp(['performing RB simulation with N = ',num2str(ud.model.N),...
278  ' for mu = [',num2str(mu(:)'),']']);
279  end;
280  reduced_data = extract_reduced_data_subset(ud.model,ud.reduced_data);
281 
282  % perform RB-simulation
283  ud.model = set_mu(ud.model,mu);
284 
285  simulation_data = rb_simulation(ud.model,reduced_data);
286 
287  rb_sim_data = rb_reconstruction(ud.model,ud.detailed_data,simulation_data);
288  if enable_error_estimator
289  ud.Delta = ud.model.get_estimators_from_sim_data(simulation_data);
290  if isfield(simulation_data,'reslog')
291  ud.reslog = simulation_data.reslog;
292  ud.eilog = simulation_data.eilog;
293  end
294  end
295  ud.sim_data = rb_sim_data;
296  if isfield(ud.descr,'name_output_functional')
297  ud.s = simulation_data.s;
298  if enable_error_estimator
299  ud.Delta_s = simulation_data.Delta_s;
300  end
301  end;
302  if ~isfield(ud.plot_params,'clim')
303  U = ud.model.get_dofs_from_sim_data(rb_sim_data);
304  ud.cmin = min(min(U));
305  ud.cmax = max(max(U));
306  if (ud.cmin==ud.cmax)
307  ud.cmin = ud.cmin-eps;
308  ud.cmax = ud.cmax+eps;
309  end;
310  clear('U');
311  else
312  ud.cmin = ud.plot_params.clim(1);
313  ud.cmax = ud.plot_params.clim(2);
314  end
315  set(f,'Userdata',ud);
316 
317 end
318 
319 % plot current slice;
320 
321 if enable_error_estimator
322  disp(['error_estimator Delta(',num2str(ud.timestep),') = ',...
323  num2str(ud.Delta(ud.timestep+1))]);
324  if isfield(ud.descr,'name_output_functional')
325  disp(['output-estimator s(U(',num2str(ud.timestep),')) = ',...
326  num2str(ud.s(ud.timestep+1))]);
327  disp(['output-error-estimator Delta_s(',num2str(ud.timestep),...
328  ') = ', num2str(ud.Delta_s(ud.timestep+1))]);
329  end;
330  if isfield(ud,'reslog')
331  disp(['error_estimator reslog(',num2str(ud.timestep),') = ',...
332  num2str(ud.reslog(ud.timestep+1))]);
333  disp(['error_estimator eilog(',num2str(ud.timestep),') = ',...
334  num2str(ud.eilog(ud.timestep+1))]);
335  end
336 end
337 fprintf('plotting...');
338 
339 ud.plot_params.timestep = ud.timestep;
340 ud.plot_params.gcf = gcf;
341 ud.plot_params.reload_required = data_reload_required;
342 plot_sim_data(ud.model.detailed_model, ...
343  ud.detailed_data, ...
344  ud.sim_data, ...
345  ud.plot_params);
346 
347 fprintf('done\n');
348 
349 %| \docupdate
function dune_demo_rb_gui(varargin)
reduced basis demo with sliders for a remotely running DUNE-rb server application ...
function p = plot_sim_data(model, model_data, sim_data, plot_params)
function performing the plot of the simulation results as specified in model.
Definition: plot_sim_data.m:17