rbmatlab  1.13.10
 All Classes Namespaces Files Functions Variables Groups Pages
rb_tutorial.m
1 function res = rb_tutorial(step)
2 
3 % script for RB Summerschool in Munich, 16.9.2013-19.9.2013
4 % script used for generating pictures for course
5 
6 % B. Haasdonk, 7.9.2013
7 
8 if nargin<1
9  step = 1
10 end;
11 
12 switch step
13  case 1 % thermal block for different configurations and
14  % corresponding plots
15  params = [];
16  params.B1 = 2;
17  params.B2 = 2;
18  params.mu_range = [0.1;10];
19  params.numintervals_per_block = 5;
20  model = thermalblock_model_struct(params);
21  model_data = gen_model_data(model);
22  plot_params = [];
23  plot_params.axis_equal = 1;
24  plot_params.axis_tight = 1;
25 
26  % plot 1: four identical values
27  figure;
28  model = set_mu(model,[1,1,1,1]);
29  sim_data = detailed_simulation(model,model_data);
30  plot_sim_data(model,model_data,sim_data,plot_params);
31 
32  % plot 2: lower and upper values identical
33  figure;
34  model = set_mu(model,[0.1,0.1,10,10]);
35  sim_data = detailed_simulation(model,model_data);
36  plot_sim_data(model,model_data,sim_data,plot_params);
37 
38  % plot 3: four different values
39  figure;
40  model = set_mu(model,[0.3,0.2,0.2,0.7]);
41  sim_data = detailed_simulation(model,model_data);
42  plot_sim_data(model,model_data,sim_data,plot_params);
43 
44  % plot 4: larger division
45  % mu = rand(params.B1*params.B2,1)+0.1;
46  figure;
47  mu = [ 0.8094 0.8547 0.3760 0.7797 0.7551 0.2626, ...
48  0.2190 0.5984 1.0597 0.4404 0.6853 0.3238, ...
49  0.8513 0.3551 0.6060 0.7991 0.9909 1.0593 ...
50  0.6472 0.2386 0.2493 0.3575 0.9407 0.3543 ...
51  0.9143 0.3435 1.0293 0.4500 0.2966 0.3511 ...
52  0.7160 0.5733 0.4517 0.9308 0.6853 0.6497]';
53  params = [];
54  params.B1 = 6;
55  params.B2 = 6;
56  params.mu_range = [0.1;10];
57  model = thermalblock_model_struct(params);
58  model_data = gen_model_data(model);
59  model = set_mu(model,mu);
60  sim_data = detailed_simulation(model,model_data);
61  plot_sim_data(model,model_data,sim_data,plot_params);
62 
63  case 2 % thermal block detailed gui
64  params = [];
65  params.B1 = 2;
66  params.B2 = 2;
67  params.numintervals_per_block = 50;
68  params.mu_range = [0.1;10];
69  params.numintervals_per_block = 5;
70  model = thermalblock_model_struct(params);
71  model_data = gen_model_data(model);
72  plot_params = [];
73  plot_params.axis_equal = 1;
74  plot_params.axis_tight = 1;
75  plot_params.yscale_uicontrols = 0.7;
76  demo_detailed_gui(model,model_data,[],plot_params);
77  set(gcf,'Position',[584 245 553 691]);
78 
79  case 3 % equidistant samples for basis generation, offline versus online time
80 
81  params = [];
82  params.B1 = 2;
83  params.B2 = 2;
84  params.numintervals_per_block = 20;
85  params.mu_range = [0.1;10];
86  disp('initializing model')
87  model = thermalblock_model_struct(params);
88 
89  disp('-----------------------------------------------------')
90  disp('OFFLINE PHASE:');
91  disp('generating model data: grid, fem inner product matrix, etc.');
92  tic
93  model_data = gen_model_data(model);
94  model.RB_generation_mode = 'lagrangian';
95  model.RB_mu_list = lin_equidist_samples(model,[4,2,2,2]);
96  disp('generating detailed data: A_q,f_q,l_q components, reduced basis.');
97  detailed_data = gen_detailed_data(model,model_data);
98  disp('generating reduced data: A_Nq,f_Nq,l_Nq components');
99  reduced_data = gen_reduced_data(model, detailed_data);
100  t_offline = toc;
101  disp('Offline time: ')
102  t_offline
103  save('thermalblock_2x2_data');
104 
105  disp('-----------------------------------------------------')
106  disp('ONLINE PHASE:');
107  % online phase for single simulation:
108  disp('reduced simulation: assembling system and solve');
109  model = set_mu(model,[1,1,1,1]);
110  tic
111  sim_data = rb_simulation(model,reduced_data);
112  t_online = toc;
113 
114  disp('Online time: ')
115  t_online
116 
117  disp('starting reduced basis gui:')
118  plot_params = [];
119  plot_params.axis_equal = 1;
120  plot_params.axis_tight = 1;
121  plot_params.yscale_uicontrols = 0.7;
122  demo_rb_gui(model,detailed_data,reduced_data,plot_params);
123 
124  case 4 % 1-parameter example: error, estimator, effectivity bound plot.
125 
126  % generate reduced model
127  disp('generating reduced model')
128  params = [];
129  params.B1 = 2;
130  params.B2 = 2;
131  params.numintervals_per_block = 20;
132  params.mu_range = [0.1;10];
133  model = thermalblock_model_struct(params);
134  model_data = gen_model_data(model);
135  model.RB_generation_mode = 'lagrangian';
136  c = 0.1;
137  mu_list = {[0.1,c,c,c],...
138  [0.5,c,c,c],...
139  [0.9,c,c,c],...
140  [1.3,c,c,c],...
141  [1.7,c,c,c]};
142  model.RB_mu_list = mu_list;
143  detailed_data = gen_detailed_data(model,model_data);
144  reduced_data = gen_reduced_data(model,detailed_data);
145 
146  disp('computing parameter sweep')
147  mu1s = linspace(0.1,4,1000);
148  % rapidly computable error landscape:
149  Deltas = zeros(length(mu1s),1);
150  for n = 1:length(mu1s)
151  fprintf('.');
152  mu = [mu1s(n),c,c,c];
153  model = set_mu(model,mu);
154  sim_data = rb_simulation(model,reduced_data);
155  Deltas(n) = sim_data.Delta;
156  end;
157  fprintf('\n');
158  plot(mu1s,Deltas,'Linewidth',2);
159  xlabel('mu1','Fontsize',15);
160  ylabel('error/estimator','Fontsize',15);
161  title('error estimator by parameter sweep','Fontsize',15);
162  set(gca,'Yscale','log');
163  legend('estimator \Delta_u(\mu)');
164 
165  % comparison with expensive true error
166 
167  disp('computing true error for some parameters')
168  mu1s = linspace(0.1,4,40);
169  errs = zeros(length(mu1s),1);
170  for n = 1:length(mu1s)
171  fprintf('.');
172  mu = [mu1s(n),c,c,c];
173  model = set_mu(model,mu);
174  sim_data = detailed_simulation(model,model_data);
175  rb_sim_data = rb_simulation(model,reduced_data);
176  % linear combination of reduced vector with basis:
177  rb_sim_data= rb_reconstruction(model,detailed_data,rb_sim_data);
178  err = sim_data.uh;
179  err.dofs = err.dofs - rb_sim_data.uh.dofs;
180  errs(n) = fem_h10_norm(err);
181  end;
182  fprintf('\n');
183  hold on;
184  plot(mu1s,errs,'r*','Markersize',10);
185  legend({'estimator \Delta_u(\mu)','true error |u-u_N|'},'Fontsize',15);
186 
187  %set(gcf,'Position', [379 492 434 328]);
188  %saveas(gcf,'error_parameter_sweep.png');
189  %saveas(gcf,'error_parameter_sweep.eps','epsc');
190 
191  save('rb_tutorial_step4');
192 
193  case 5 % Effectivity plot
194  load('rb_tutorial_step4');
195 
196  disp('computing parameter sweep')
197  mu1s = linspace(0.1,4,40);
198  % rapidly computable error landscape:
199  Deltas = zeros(length(mu1s),1);
200  for n = 1:length(mu1s)
201  fprintf('.');
202  mu = [mu1s(n),0.1,0.1,0.1];
203  model = set_mu(model,mu);
204  sim_data = rb_simulation(model,reduced_data);
205  Deltas(n) = sim_data.Delta;
206  end;
207  fprintf('\n');
208 
209  etas = Deltas.*(errs.^(-1));
210  i = find(etas>100);
211  etas(i)=NaN;
212  plot(mu1s,etas,'b*','Markersize',10);
213  xlabel('\mu1','Fontsize',15);
214  ylabel('effectivity','Fontsize',15);
215  title('effectivity over parameter','Fontsize',15);
216 % set(gca,'Yscale','log');
217 
218  hold on;
219  gammas = mu1s;
220  alpha = 0.1;
221  effectivities = gammas/alpha;
222  plot(mu1s,effectivities,'r-','Linewidth',2);
223 
224  legend({'effectivity \Delta_u/|u-u_N|',...
225  'upper bound \gamma(\mu)/\alpha(\mu)'},...
226  'Fontsize',15,...
227  'Location','NorthWest');
228 
229 % set(gcf,'Position', [379 492 434 328]);
230 % saveas(gcf,'effectivity_parameter_sweep.png');
231 % saveas(gcf,'effectivity_parameter_sweep.eps','epsc');
232 
233  case 6 % Convergence plot for test error on equidistant samples
234 
235  params = [];
236  params.B1 = 3;
237  params.B2 = 3;
238  params.numintervals_per_block = 20;
239  params.mu_range = [0.5;2];
240  model = thermalblock_model_struct(params);
241  model_data = gen_model_data(model);
242  model.RB_generation_mode = 'model_RB_basisgen';
243  model.RB_basisgen = @lagrangian_orth_basisgen;
244  Ns = 2:8; % linear dependent for higher values...
245 
246  maxDeltas = zeros(length(Ns),1);
247  maxerrs = zeros(length(Ns),1);
248 
249  disp('Precomputing test snapshots');
250  ntest = 100;
251  [U,test_mus] = filecache_function(@precompute_test_snapshots,...
252  params,model,model_data,ntest);
253 
254  disp('Generating basis and determining max error, estimators');
255  tic;
256  for Ni = 1:length(Ns)
257  N = Ns(Ni)
258  train_mu1s = linspace(params.mu_range(1),params.mu_range(2),N);
259  train_mus = [train_mu1s(:), ones(N,params.B1*params.B2-1)*1];
260  model.RB_mu_list = mat2cell(train_mus,ones(N,1),params.B1*params.B2);
261  detailed_data = gen_detailed_data(model,model_data);
262  reduced_data = gen_reduced_data(model,detailed_data);
263  for i = 1:ntest
264  model = set_mu(model,test_mus(i,:));
265  rb_sim_data = rb_simulation(model, reduced_data);
266  rb_sim_data = rb_reconstruction(model,detailed_data,rb_sim_data);
267  err = rb_sim_data.uh;
268  err.dofs = err.dofs - U(:,i);
269  errnorm = fem_h10_norm(err);
270  if maxerrs(Ni)<errnorm
271  maxerrs(Ni)=errnorm;
272  end;
273  if maxDeltas(Ni)<rb_sim_data.Delta
274  maxDeltas(Ni)=rb_sim_data.Delta;
275  end;
276  end; % i
277  end; % Ni
278 
279  time_step_6 = toc
280  % save('rb_tutorial_step6');
281  % case 6.1 % plot of error convergenc
282  % load('rb_tutorial_step6');
283  plot(Ns,maxDeltas,'b-','Linewidth',2);
284  hold on
285  plot(Ns,maxerrs,'r:','Linewidth',2);
286  set(gca,'Yscale','log');
287  legend({'estimator \Delta_u(\mu)','error |u(\mu)-u_N(\mu)|'},'Fontsize',15);
288  title('test error decay for growing sample size','Fontsize',15);
289  xlabel('sample/basis Size N','Fontsize',15);
290  ylabel('error/estimator','Fontsize',15);
291 
292  % set(gcf,'Position', [379 492 434 328]);
293  % saveas(gcf,'lagrangian_error_convergence.png');
294  % saveas(gcf,'lagrangian_error_convergence.eps','epsc');
295 
296  case 7 % plot basis
297  load('rb_tutorial_step6');
298  plot_params = [];
299  plot_params.axis_equal = 1;
300  plot_params.axis_tight = 1;
301  plot_params.plot = @plot_vertex_data;
302  plot_params.title='Orthonormal Reduced Basis \Phi_N';
303  % plot as sequence
304  plot_sequence(detailed_data.RB,detailed_data.grid,plot_params);
305 
306  % or in single plot
307  plot_params.show_colorbar = 0;
308  plot_params.no_lines = 1;
309  figure;
310  for n = 1:8
311  subplot(2,4,n);
312  plot_vertex_data(detailed_data.grid,detailed_data.RB(:,n), ...
313  plot_params);
314  title(['n=',num2str(n)],'Fontsize',15);
315  set(gca,'Xtick',[]);
316  set(gca,'Ytick',[]);
317  end;
318  set(gcf,'Position',[32 49 1569 906]);
319 % saveas(gcf,'orthonormal_basis.png');
320 % saveas(gcf,'orthonormal_basis.eps','epsc');
321 
322  case 8 % Greedy error convergence
323 
324  params = [];
325  params.B1 = 2;
326  params.B2 = 2;
327  params.numintervals_per_block = 20;
328  params.mu_range = [0.5;2];
329  model = thermalblock_model_struct(params);
330  model_data = gen_model_data(model);
331  model.RB_stop_epsilon = 1e-6;
332  model.RB_stop_Nmax = 50;
333  model.RB_train_size = 1000;
334 
335  disp('Generating basis and determining max error, estimators');
336  tic;
337  detailed_data = gen_detailed_data(model,model_data);
338  reduced_data = gen_reduced_data(model,detailed_data);
339 
340  disp('Precomputing test snapshots');
341  ntest = 100;
342  [U,test_mus] = filecache_function(@precompute_test_snapshots,...
343  params,model,model_data,ntest);
344 
345  Ns = 1:size(detailed_data.RB,2);
346  maxDeltas = zeros(length(Ns),1);
347  maxerrs = zeros(length(Ns),1);
348  for Ni = 1:length(Ns)
349  N = Ns(Ni)
350  model.N = N;
351  reduced_data_subset = model.reduced_data_subset(model,reduced_data);
352  for i = 1:ntest
353  model = set_mu(model,test_mus(i,:));
354  rb_sim_data = rb_simulation(model, reduced_data_subset);
355  rb_sim_data = rb_reconstruction(model,detailed_data,rb_sim_data);
356  err = rb_sim_data.uh;
357  err.dofs = err.dofs - U(:,i);
358  errnorm = fem_h10_norm(err);
359  if maxerrs(Ni)<errnorm
360  maxerrs(Ni)=errnorm;
361  end;
362  if maxDeltas(Ni)<rb_sim_data.Delta
363  maxDeltas(Ni)=rb_sim_data.Delta;
364  end;
365  end; % i
366  end; % Ni
367  time_step_6 = toc
368  save('rb_tutorial_step8');
369 
370  case 8.1 % plot of greedy errors
371  load('rb_tutorial_step8');
372 
373  plot(Ns,maxDeltas,'b-','Linewidth',2);
374  hold on
375  plot(Ns,maxerrs,'r:','Linewidth',2);
376  set(gca,'Yscale','log');
377  % if wanted, training error can be plotted
378  % plot(Ns(1:end-2),detailed_data.RB_info.max_err_est_sequence(2:end),...
379 % 'g-.','Linewidth',2);
380  set(gca,'Yscale','log');
381  legend({'test estimator \Delta_u(\mu)','test error |u(\mu)-u_N(\mu)|'},'Fontsize',15);
382  title('Error decay for Greedy basis','Fontsize',15);
383  xlabel('sample/basis Size N','Fontsize',15);
384  ylabel('error/estimator','Fontsize',15);
385 
386 % set(gcf,'Position', [379 492 434 328]);
387 % saveas(gcf,'greedy_test_error.png');
388 % saveas(gcf,'greedy_test_error.eps','epsc');
389 
390  % possible further cases:
391  % plot of sample points: not spectacular...
392  % load('rb_tutorial_step8');
393  % mus = detailed_data.RB_info.max_mu_sequence;
394  % mumat = cell2mat(mus)
395  % plot(mumat(1,:),mumat(2,:),'*');
396 
397  case 9 % experiment with basis from 4
398 
399  load('rb_tutorial_step4');
400 
401  mus= {[1,0.2,0.2,0.2],[1,0.1,1,0.1]};
402  for i = 1:length(mus)
403  disp('-------------------------------------------')
404  disp(['Experiment for mu = (',num2str(mus{i}),')']);
405  model = set_mu(model,mus{i});
406  sim_data = detailed_simulation(model,model_data);
407  rb_sim_data = rb_simulation(model,reduced_data);
408  rb_sim_data = rb_reconstruction(model,detailed_data,rb_sim_data);
409  disp(['output s(mu) = ', num2str(sim_data.s)]);
410  disp(['output s_N(mu) = ', num2str(rb_sim_data.s)]);
411  disp(['output error s(mu)-s_N(mu) = ', ...
412  num2str(sim_data.s-rb_sim_data.s)]);
413  disp(['output error bound Delta_s(\mu) = ', num2str(rb_sim_data.Delta_s)]);
414  err = sim_data.uh;
415  err.dofs = err.dofs - rb_sim_data.uh.dofs;
416  disp(['field error |u-u_N| = ',num2str(fem_h10_norm(err))]);
417  disp(['field error bound Delta_u(\mu) = ', num2str(rb_sim_data.Delta)]);
418  end;
419 
420  case 10 % convergence curves, training error for different block numbers
421 
422  B1S = [2,3,4];
423  B2S = [2,3,4];
424 
425  max_err_est_sequences = {};
426 
427  for bi = 1:length(B1S)
428  params = [];
429  params.B1 = B1S(bi);
430  params.B2 = B2S(bi);
431  disp('--------------------------------------------------');
432  disp(['generating basis for (B1,B2) = (',...
433  num2str(params.B1),',',num2str(params.B2),')']);
434  params.numintervals_per_block = 20;
435  params.mu_range = [0.5;2];
436  model = thermalblock_model_struct(params);
437  model_data = gen_model_data(model);
438  model.RB_stop_epsilon = 1e-6;
439  model.RB_stop_Nmax = 50;
440  model.RB_train_size = 1000;
441  detailed_data = gen_detailed_data(model,model_data);
442  max_err_est_sequences = [max_err_est_sequences,...
443  {detailed_data.RB_info.max_err_est_sequence}]
444  end;
445 
446 % save('rb_tutorial_step10');
447 % case 10.1 % plot training error curves
448 % load('rb_tutorial_step10');
449 
450  figure;
451  hold on;
452  legstr = {};
453  styles = {'b-','r-','g-','k-'}
454  for i=1:length(max_err_est_sequences)
455  seq = max_err_est_sequences{i};
456  plot(0:length(seq)-1,seq,styles{i});
457  legstr = [legstr,{['(B1,B2)= (',...
458  num2str(B1S(i)),',',num2str(B2S(i)),')']}]
459  end;
460  legend(legstr,'Fontsize',15);
461  title('greedy training error decay','Fontsize',15);
462  xlabel('basis size N','Fontsize',15);
463  ylabel('training error','Fontsize',15);
464  set(gca,'Yscale','log');
465 
466  otherwise
467  disp('step number unknown');
468 end;
469 
470 
471 function mu_list = lin_equidist_samples(model,numintervals)
472 % generate cell array of parameter vectors
473 % numintervals a vector of number of equal sized intervals per
474 % parameter direction
475 p = [];
476 p.range = model.mu_ranges;
477 p.numintervals = numintervals;
478 c = cubegrid(p);
479 mu_list = mat2cell(c.vertex,ones(size(c.vertex,1),1),...
480  size(c.vertex,2));
481 
482 function [U,test_mus] = precompute_test_snapshots(params,model,model_data,ntest)
483 % function precomputing test snapshots for step 6
484 test_mu1s = rand_uniform(ntest,{params.mu_range});
485 test_mus = [test_mu1s(:), ones(ntest,params.B1*params.B2-1)*1];
486 U = zeros(model_data.df_info.ndofs,0);
487 for i = 1:ntest
488  fprintf('.');
489  model = set_mu(model,test_mus(i,:));
490  sim_data = detailed_simulation(model, model_data);
491  U = [U,sim_data.uh.dofs(:)];
492 end;
493 fprintf('\n');
494 
495 function RB = lagrangian_orth_basisgen(model, ...
496  detailed_data)
497 Utot = [];
498 for m = 1:length(model.RB_mu_list)
499  model = set_mu(model,model.RB_mu_list{m});
500  sim_data = detailed_simulation(model,detailed_data);
501  if isempty(Utot)
502  Utot = model.get_dofs_from_sim_data(sim_data);
503  else
504  U = model.get_dofs_from_sim_data(sim_data);
505  Utot = [Utot U];
506  end;
507 end;
508 % Gram-Schmidt orthonormalization
509 KN = Utot'*model.get_inner_product_matrix(detailed_data)*Utot;
510 KN = 0.5*(KN + KN');
511 Ltransp = chol(KN);
512 RB = Utot / Ltransp;