1 function res = rb_tutorial(step)
3 % script
for RB Summerschool in Munich, 16.9.2013-19.9.2013
4 % script used for generating pictures for course
6 % B. Haasdonk, 7.9.2013
13 case 1 % thermal block
for different configurations and
18 params.mu_range = [0.1;10];
19 params.numintervals_per_block = 5;
21 model_data = gen_model_data(model);
23 plot_params.axis_equal = 1;
24 plot_params.axis_tight = 1;
26 % plot 1: four identical values
28 model = set_mu(model,[1,1,1,1]);
29 sim_data = detailed_simulation(model,model_data);
32 % plot 2: lower and upper values identical
34 model = set_mu(model,[0.1,0.1,10,10]);
35 sim_data = detailed_simulation(model,model_data);
38 % plot 3: four different values
40 model = set_mu(model,[0.3,0.2,0.2,0.7]);
41 sim_data = detailed_simulation(model,model_data);
44 % plot 4: larger division
45 % mu = rand(params.B1*params.B2,1)+0.1;
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]
';
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);
63 case 2 % thermal block detailed gui
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);
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]);
79 case 3 % equidistant samples for basis generation, offline versus online time
84 params.numintervals_per_block = 20;
85 params.mu_range = [0.1;10];
86 disp('initializing model
')
87 model = thermalblock_model_struct(params);
89 disp('-----------------------------------------------------')
90 disp('OFFLINE PHASE:');
91 disp(
'generating model data: grid, fem inner product matrix, etc.');
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);
101 disp(
'Offline time: ')
103 save('thermalblock_2x2_data');
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]);
111 sim_data = rb_simulation(model,reduced_data);
114 disp('Online time: ')
117 disp('starting reduced basis gui:')
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);
124 case 4 % 1-parameter example: error, estimator, effectivity bound plot.
126 % generate reduced model
127 disp('generating reduced model')
131 params.numintervals_per_block = 20;
132 params.mu_range = [0.1;10];
134 model_data = gen_model_data(model);
135 model.RB_generation_mode = 'lagrangian';
137 mu_list = {[0.1,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);
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)
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;
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)');
165 % comparison with expensive true error
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)
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);
179 err.dofs = err.dofs - rb_sim_data.uh.dofs;
180 errs(n) = fem_h10_norm(err);
184 plot(mu1s,errs,'r*','Markersize',10);
185 legend({
'estimator \Delta_u(\mu)',
'true error |u-u_N|'},
'Fontsize',15);
187 %
set(gcf,
'Position', [379 492 434 328]);
188 %saveas(gcf,
'error_parameter_sweep.png');
189 %saveas(gcf,
'error_parameter_sweep.eps',
'epsc');
191 save(
'rb_tutorial_step4');
193 case 5 % Effectivity plot
194 load(
'rb_tutorial_step4');
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)
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;
209 etas = Deltas.*(errs.^(-1));
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');
221 effectivities = gammas/alpha;
222 plot(mu1s,effectivities,'r-','Linewidth',2);
224 legend({
'effectivity \Delta_u/|u-u_N|',...
225 'upper bound \gamma(\mu)/\alpha(\mu)'},...
227 'Location',
'NorthWest');
229 %
set(gcf,
'Position', [379 492 434 328]);
230 % saveas(gcf,
'effectivity_parameter_sweep.png');
231 % saveas(gcf,
'effectivity_parameter_sweep.eps',
'epsc');
233 case 6 % Convergence plot
for test error on equidistant samples
238 params.numintervals_per_block = 20;
239 params.mu_range = [0.5;2];
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...
246 maxDeltas = zeros(length(Ns),1);
247 maxerrs = zeros(length(Ns),1);
249 disp(
'Precomputing test snapshots');
252 params,model,model_data,ntest);
254 disp(
'Generating basis and determining max error, estimators');
256 for Ni = 1:length(Ns)
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);
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
273 if maxDeltas(Ni)<rb_sim_data.Delta
274 maxDeltas(Ni)=rb_sim_data.Delta;
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);
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);
292 %
set(gcf,
'Position', [379 492 434 328]);
293 % saveas(gcf,
'lagrangian_error_convergence.png');
294 % saveas(gcf,
'lagrangian_error_convergence.eps',
'epsc');
297 load(
'rb_tutorial_step6');
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';
304 plot_sequence(detailed_data.RB,detailed_data.grid,plot_params);
307 plot_params.show_colorbar = 0;
308 plot_params.no_lines = 1;
312 plot_vertex_data(detailed_data.grid,detailed_data.RB(:,n), ...
314 title([
'n=',num2str(n)],
'Fontsize',15);
318 set(gcf,
'Position',[32 49 1569 906]);
319 % saveas(gcf,
'orthonormal_basis.png');
320 % saveas(gcf,
'orthonormal_basis.eps',
'epsc');
322 case 8 % Greedy error convergence
327 params.numintervals_per_block = 20;
328 params.mu_range = [0.5;2];
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;
335 disp(
'Generating basis and determining max error, estimators');
337 detailed_data = gen_detailed_data(model,model_data);
338 reduced_data = gen_reduced_data(model,detailed_data);
340 disp(
'Precomputing test snapshots');
343 params,model,model_data,ntest);
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)
351 reduced_data_subset = model.reduced_data_subset(model,reduced_data);
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
362 if maxDeltas(Ni)<rb_sim_data.Delta
363 maxDeltas(Ni)=rb_sim_data.Delta;
368 save('rb_tutorial_step8');
370 case 8.1 % plot of greedy errors
371 load('rb_tutorial_step8');
373 plot(Ns,maxDeltas,'b-','Linewidth',2);
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);
386 %
set(gcf,
'Position', [379 492 434 328]);
387 % saveas(gcf,
'greedy_test_error.png');
388 % saveas(gcf,
'greedy_test_error.eps',
'epsc');
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,:),
'*');
397 case 9 % experiment with basis from 4
399 load(
'rb_tutorial_step4');
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)]);
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)]);
420 case 10 % convergence curves, training error
for different block numbers
425 max_err_est_sequences = {};
427 for bi = 1:length(B1S)
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];
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}]
446 % save(
'rb_tutorial_step10');
447 %
case 10.1 % plot training error curves
448 % load(
'rb_tutorial_step10');
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)),')']}]
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');
467 disp(
'step number unknown');
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
476 p.range = model.mu_ranges;
477 p.numintervals = numintervals;
479 mu_list = mat2cell(c.vertex,ones(size(c.vertex,1),1),...
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);
489 model = set_mu(model,test_mus(i,:));
490 sim_data = detailed_simulation(model, model_data);
491 U = [U,sim_data.uh.dofs(:)];
495 function RB = lagrangian_orth_basisgen(model, ...
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);
502 Utot = model.get_dofs_from_sim_data(sim_data);
504 U = model.get_dofs_from_sim_data(sim_data);
508 % Gram-Schmidt orthonormalization
509 KN = Utot'*model.get_inner_product_matrix(detailed_data)*Utot;