2 % small script implementing a simple advection example
3 %
for producing matrices to be used in the RB-DS framework
4 % Discretization with FV Functions. Used for the mcmds lin-ds paper.
6 % mu_names = {
'cone_weight',
'vx_weight',
'vy_weight'};
11 % step = 1; % initialization of model and plot of model data
12 % step = 2; % detailed simulation of lin_evol
13 % step = 2.5; % conversion to DS primal model, lin_ds detailed simulation
14 % step = 3; % conversion to DS primal model, DS detailed simulation
15 % and comparison with lin_evol
16 % step = 4 % compute several trajectories.
17 % step = 4.5 % compute reduced basis based on 2 trajecories.
18 % step = 4.75 % comparison of detailed and reduced simulation
19 % step = 5 % figures of detailed simulations based on step 4
20 % step = 6 % figures of reduced simulations based on step 4
21 % step = 7 % time measurements based on step 4
22 % step = 8; % compare original and accelerated ds_model
23 % step = 9; % generate greedy basis on subset of parameter domain
24 % step = 10; % reduced simulations and error plots
25 % step = 11; % test of matlab ode solver
for resulting sparse system
26 % step = 12; % further plots
for MCMDS paper
28 % Bernard Haasdonk 25.8.2009
38 %params.coarse_factor = 2; % fact 2 => ndofs = 16384, nt = 1024, 15s detailed
39 %params.coarse_factor = 1; % fact 1 => ndofs = 32768, nt = 2048, 60s detailed
40 params.coarse_factor = 1;
45 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
46 %step = 1; % initialization of model and plot of model data
48 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
50 %
verbose(1,
'initializaton of lin_evol_model and plot model_data');
51 disp(
'initializaton of lin_evol_model and plot model_data');
52 model = advection_fv_output_model(params);
53 model_data = gen_model_data(model);
54 params.axis_equal = 1;
55 params.axis_tight = 1;
56 plot(model_data.grid,params);
57 % inspect(model_data.grid)
58 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
59 %step = 2; % detailed simulation of lin_evol
60 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
62 %
verbose(1,
'detailed simulation of lin_evol_model and plot');
63 disp(
'detailed simulation of lin_evol_model and plot');
64 model = advection_fv_output_model(params);
65 model_data = gen_model_data(model);
67 % disp(
'model debugging turned on.')
68 model.cone_weight = 0.0;
69 model.vx_weight = 0.0;
70 model.vy_weight = 0.0;
73 model.cone_weight = 1.0;
74 % model.vx_weight = 1;
75 % model.vy_weight = 0;
76 sim_data = detailed_simulation(model,model_data);
80 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81 %step = 2.5; % conversion to DS primal model, DS detailed simulation
82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
84 disp('initialization of lin_ds model from lin_evol model');
85 %
verbose(1,'initialization of lin_ds model from lin_evol model and ...
88 lin_evol_model = advection_fv_output_model(params);
89 %lin_evol_model.debug = 1;
90 lin_evol_model_data = gen_model_data(lin_evol_model);
92 % set some additional fields in model which will be copied into
93 % ds model depending on whether constants are known or not:
94 estimate_constants = 0;
96 %%% if constant estimaton has to be performed:
97 lin_evol_model.estimate_lin_ds_nmu = 4;
98 lin_evol_model.estimate_lin_ds_nX = 10;
99 lin_evol_model.estimate_lin_ds_nt = 4;
102 % the following values are rough bounds, so could be
103 % non-rigorous, then larger search must be performed.
104 lin_evol_model.state_bound_constant_C1 = 1;
105 lin_evol_model.output_bound_constant_C2 = 1.2916;
108 ds_model = lin_ds_from_lin_evol_model(lin_evol_model);
109 %ds_model.u_function_ptr = @(params) 0.1; % define new
110 % ds_model.theta = 1;
112 ds_model_data = gen_model_data(ds_model);
114 % set mu in model and base_model!!!
115 ds_model = ds_model.set_mu(ds_model,mu);
117 ds_sim_data = detailed_simulation(ds_model,ds_model_data);
121 params.plot_title='detailed implicit lin_ds';
122 lin_evol_from_ds_sim_data.U = ds_sim_data.X;
123 lin_evol_from_ds_sim_data.y = ds_sim_data.Y;
124 plot_sim_data(lin_evol_model,lin_evol_model_data,lin_evol_from_ds_sim_data,params);
126 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
127 %step = 3; % conversion to DS primal model, DS detailed simulation
128 % and comparison with lin_evol
129 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
131 case 3 % initialization of DS model and lin_ds detailed simulation
132 disp('initialization of lin_ds model from lin_evol model and plot');
133 %
verbose(1,'initialization of lin_ds model from lin_evol model and ...
136 lin_evol_model = advection_fv_output_model(params);
137 %lin_evol_model.debug = 1;
138 lin_evol_model_data = gen_model_data(lin_evol_model);
140 % set some additional fields in model which will be copied into
141 % ds model depending on whether constants are known or not:
142 estimate_constants = 0;
143 if estimate_constants
144 %%% if constant estimaton has to be performed:
145 lin_evol_model.estimate_lin_ds_nmu = 4;
146 lin_evol_model.estimate_lin_ds_nX = 10;
147 lin_evol_model.estimate_lin_ds_nt = 4;
150 % the following values are rough bounds, so could be
151 % non-rigorous, then larger search must be performed.
152 lin_evol_model.state_bound_constant_C1 = 1;
153 lin_evol_model.output_bound_constant_C2 = 1.2916;
156 ds_model = lin_ds_from_lin_evol_model(lin_evol_model);
157 % ds_model.theta = 0.0;
158 %ds_model.u_function_ptr = @(params) 0.1; % define new
160 ds_model_data = gen_model_data(ds_model);
162 % set mu in model and base_model!!!
163 ds_model = ds_model.set_mu(ds_model,mu);
165 ds_sim_data = detailed_simulation(ds_model,ds_model_data);
166 lin_evol_model = lin_evol_model.set_mu(lin_evol_model,mu);
167 lin_evol_sim_data = detailed_simulation(lin_evol_model,...
168 lin_evol_model_data);
169 err = lin_evol_sim_data.U - ds_sim_data.X;
170 disp(['max error in DOFs of lin_evol and ds simulation: ',...
171 num2str(max(abs(err(:))))]);
173 params.plot_title='detailed lin_evol';
175 plot_sim_data(lin_evol_model,lin_evol_model_data,lin_evol_sim_data,params);
176 params.plot_title='detailed lin_ds';
177 lin_evol_from_ds_sim_data.U = ds_sim_data.X;
178 lin_evol_from_ds_sim_data.y = ds_sim_data.Y;
179 plot_sim_data(lin_evol_model,lin_evol_model_data,lin_evol_from_ds_sim_data,params);
180 params.plot_title='error explicit lin_ds and lin_evol';
181 error_sim_data = lin_evol_sim_data;
182 error_sim_data.U = error_sim_data.U-ds_sim_data.X;
183 plot_sim_data(lin_evol_model,lin_evol_model_data,error_sim_data,params);
186 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
187 %step = 4 % reduced Basis generation by single trajectories and
188 %comparison of detailed and reduced simulation
189 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
191 case 4 % generate some trajectories
193 ds_model = fast_model(params);
194 % lin_evol_model = advection_fv_output_model(params);
195 %lin_evol_model.debug = 1;
196 % lin_evol_model_data = gen_model_data(lin_evol_model);
198 % set some additional fields in model which will be copied into
199 % ds model depending on whether constants are known or not:
200 % estimate_constants = 0;
201 % if estimate_constants
202 % %%% if constant estimaton has to be performed:
203 % lin_evol_model.estimate_lin_ds_nmu = 4;
204 % lin_evol_model.estimate_lin_ds_nX = 10;
205 % lin_evol_model.estimate_lin_ds_nt = 4;
208 % % the following values are rough bounds, so could be
209 % % non-rigorous, then larger search must be performed.
210 % lin_evol_model.state_bound_constant_C1 = 1;
211 % lin_evol_model.output_bound_constant_C2 = 1.2916;
214 % ds_model = lin_ds_from_lin_evol_model(lin_evol_model);
215 %ds_model.u_function_ptr = @(params) 0.1; % define new
216 ds_model_data = gen_model_data(ds_model);
218 % mu = [0.5,0.5,0.5];
220 mus = {[0,0,0],[1,1,1],[1,1,0.5],[0,0.5,1]};
221 for m = 1:length(mus)
222 disp(['computing trajectory number ',num2str(m)]);
224 ds_model = ds_model.set_mu(ds_model,mu);
227 %disp(
'set theta to 1!!!');
229 ds_sim_data = detailed_simulation(ds_model,ds_model_data);
231 disp([
'time for full simulation: ',num2str(t_det)]);
232 save(fullfile(rbmatlabresult,[
'trajectory',num2str(m)]),...
233 'ds_model',
'ds_model_data',
'ds_sim_data');
236 case 4.5 % compute reduced basis.
237 % make sure that the previous trajectories are computed!!
239 disp(
'PCA of snapshots takes a while...');
240 load(fullfile(rbmatlabresult,
'trajectory1'),...
241 'ds_model',
'ds_model_data',
'ds_sim_data');
243 RB1 = PCA_fixspace(ds_sim_data.X,[],ds_model_data.G,1,[], ...
245 %RB = orthonormalize_qr(ds_sim_data.X,ds_model_data.G);
246 % select correct orthogonal
set
247 % K = RB
' * ds_model_data.G * RB;
248 % i = find(abs(diag(K)-1)>1e-2);
250 % disp(['found
',num2str(size(RB,2)),' basis vectors after traj. 1
']);
255 % select first vector only (stationary solution)
257 disp(['reduced to
',num2str(size(RB,2)),' basis vectors.
']);
260 load(fullfile(rbmatlabresult,'trajectory2
'),...
261 'ds_model
','ds_model_data
','ds_sim_data
');
264 % ds_sim_data.X = ds_sim_data.X-...
265 % RB*(RB'*(ds_model_data.G*ds_sim_data.X));
266 RB2 = PCA_fixspace(ds_sim_data.X,RB,ds_model_data.G,[],
'gram-schmidt',epsilon);
267 %RB = orthonormalize_qr(ds_sim_data.X,ds_model_data.G);
268 % select correct orthogonal
set
269 K = RB2
' * ds_model_data.G * RB2;
270 i = find(abs(diag(K)-1)>1e-2);
275 disp(['found
',num2str(size(RB2,2)),' basis vectors after traj. 2
']);
276 RB2 = RB2(:,1:(i-1));
277 disp(['reduced to
',num2str(size(RB2,2)),' basis vectors after traj. 2
']);
279 % RB = orthonormalize(RB);
282 % check orthonormality
283 K = RB' * ds_model_data.G * RB;
284 i = find(abs(diag(K)-1)>1e-2);
289 % make consistent with preprint: 127 vectors overall
295 disp(['reduced to ',num2str(size(RB,2)),' basis vectors overall']);
299 W = ds_model_data.G * V;
300 rbfname = fullfile(rbmatlabresult,'advection_fv_output_basis');
303 ds_model.RB_basis_filename = rbfname;
304 ds_model.RB_generation_mode = 'file_load';
306 detailed_data = gen_detailed_data(ds_model,ds_model_data);
309 save(fullfile(rbmatlabresult,'advection_fv_output_detailed_data_2traj'),...
310 'model','detailed_data')
312 case 4.75 % compare reduced and detailed simulation
314 load(fullfile(rbmatlabresult,'advection_fv_output_detailed_data_2traj'),...
315 'model','detailed_data')
316 load(fullfile(rbmatlabresult,'trajectory2'),...
321 %perform single reduced simulation for given parameter
322 reduced_data = gen_reduced_data(ds_model,detailed_data);
323 ds_model.N = reduced_data.N;
325 % ds_model.nt = ds_model.nt/4;
326 % disp('set nt to quarter!!');
327 rb_sim_data = rb_simulation(ds_model,reduced_data);
329 rb_sim_data = rb_reconstruction(ds_model,detailed_data,rb_sim_data)
330 disp(['time for reduced simulation: ',num2str(t_red)]);
332 % plot of reduced trajectory
333 lin_evol_model = ds_model.base_model;
334 lin_evol_model_data = gen_model_data(lin_evol_model);
337 params.show_colorbar = 1;
338 params.axis_equal = 1;
339 params.plot = lin_evol_model.plot;
340 % plot single snapshot
341 %lin_evol_model.plot(rb_sim_data.X(:,1),lin_evol_model_data.grid, ...
345 params.title = 'reduced solution';
349 params.title = 'error';
351 lin_evol_model_data.grid,params)
353 % plot of reduced output
354 figure,plot(rb_sim_data.time,[rb_sim_data.Y;ds_sim_data.Y]);
355 legend('reduced output','detailed output');
359 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
360 case 5 % generate plots of detailed data for paper:
361 % step = 5 % figures of detailed simuations based on step 4
362 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
364 disp('generate plots of detailed_data for morepas poster');
366 load(fullfile(rbmatlabresult,'advection_fv_output_detailed_data_2traj'),...
367 'model','detailed_data')
369 % model = fast_model(params);
370 % rbfname = 'advection_fv_output_basis';
371 % load(fullfile(rbmatlabresult,rbfname),'ds_model','ds_model_data');
373 % make sure, that these trajectories exist.
374 trajectories = [3,4];
376 for tr = 1:length(trajectories);
377 trname = ['trajectory',num2str(trajectories(tr))];
378 % plots of trajectory
379 load(fullfile(rbmatlabresult,trname));
380 lin_evol_model = ds_model.base_model;
381 lin_evol_model_data = gen_model_data(lin_evol_model);
384 params.show_colorbar = 0;
385 params.axis_equal = 1;
386 params.plot = lin_evol_model.plot;
387 lin_evol_model.plot(ds_sim_data.X(:,1),lin_evol_model_data.grid, ...
391 saveas(gcf,fullfile(rbmatlabresult,[trname,'_t1.eps']),'epsc');
394 lin_evol_model.plot(ds_sim_data.X(:,64),lin_evol_model_data.grid, ...
398 saveas(gcf,fullfile(rbmatlabresult,[trname,'_tmiddle.eps']),'epsc');
401 lin_evol_model.plot(ds_sim_data.X(:,end),lin_evol_model_data.grid, ...
405 saveas(gcf,fullfile(rbmatlabresult,[trname,'_tend.eps']),'epsc');
408 p = ds_model.
plot_sim_data(ds_model, ds_model_data, ds_sim_data,params);
409 % Ylim = get(gca,'Ylim');
410 % Ylim = [Ylim(1),Ylim(2)*1.1];
411 set(gca,'Ylim',[0,0.09]);
412 set(p(2),'Color',[0,0.8,0]);
413 saveas(gcf,fullfile(rbmatlabresult,[trname,'_output.eps']),'epsc');
418 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
419 % step = 6 % figures of reduced simuations based on step 4
420 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
421 case 6 % generate plots of reduced model for poster/paper
423 disp('generate plots of reduced simulations output estimation');
425 load(fullfile(rbmatlabresult,'advection_fv_output_detailed_data_2traj'),...
426 'model','detailed_data')
430 % load(fullfile(rbmatlabresult,'trajectory1'));
431 load(fullfile(rbmatlabresult,'trajectory2'));
432 ds_model.enable_error_estimator = 1;
434 % ds_model.error_estimation = 1;
435 % ds_model.RB_generation_mode = 'file_load'
436 % ds_model.RB_basis_filename = fullfile(...
437 % rbmatlabresult,'advection_fv_output_basis');
438 % detailed_data = gen_detailed_data(ds_model,ds_model_data);
439 reduced_data = gen_reduced_data(ds_model,detailed_data);
443 Ns = Ns(find(reduced_data.N>=Ns));
445 for nind = 1:length(Ns)
446 % perform reduced simulation
447 ds_model.N = Ns(nind);
448 rb_sim_data = rb_simulation(ds_model,reduced_data);
449 rb_sim_data = rb_reconstruction(ds_model,detailed_data,rb_sim_data)
452 params.show_colorbar = 1;
453 params.axis_equal = 1;
455 % plot of reduced output
457 p = plot(rb_sim_data.time,...
459 rb_sim_data.Y+rb_sim_data.DeltaY; ...
460 rb_sim_data.Y-rb_sim_data.DeltaY; ...
462 colors = {[1,0,0],[1,0,0],[1,0,0],[0,1,0]};
463 linestyles = {
'-',
'-.',
'-.',
'-'};
466 set(p(i),'Color',colors{i},
'Linestyle',linestyles{i},...
467 'Linewidth',widths(i));
469 %
set marker on rb_sim_data:
470 Xdata =
get(p(1),
'XData');
471 Ydata =
get(p(1),
'YData');
472 Zdata =
get(p(1),
'ZData');
473 set(p(1),
'XData',Xdata(1:4:end));
474 set(p(1),
'YData',Ydata(1:4:end));
475 set(p(1),
'ZData',Zdata(1:4:end));
476 set(p(1),
'Marker',
'o');
478 l = legend([
'y\^(t): reduced output'],...
479 'y\^(t)+\Delta_y(t)',...
480 'y\^(t)-\Delta_y(t)',...
481 [
'y(t): detailed output'],
'Location',
'SouthEast');
482 set(l,
'Fontsize',15);
483 title([
'k = ',num2str(ds_model.N)],
'Fontsize',15);
485 fullfile(rbmatlabresult,...
486 [
'output_estimation',num2str(ds_model.N),
'.eps']),
'epsc');
490 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
491 % step = 7 % time measurements
492 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
494 disp(
'time measurements for morepas poster');
498 t_detailed = zeros(1,nreps);
500 % initialize detailed model and simulate
502 load(fullfile(rbmatlabresult,
'advection_fv_output_detailed_data_2traj'),...
503 'model',
'detailed_data')
506 % lin_evol_model = advection_fv_output_model(params);
507 %lin_evol_model.debug = 1;
508 %lin_evol_model_data = gen_model_data(lin_evol_model);
510 % set some additional fields in model which will be copied into
511 % ds model depending on whether constants are known or not:
512 % estimate_constants = 0;
513 % if estimate_constants
514 % %%% if constant estimaton has to be performed:
515 % lin_evol_model.estimate_lin_ds_nmu = 4;
516 % lin_evol_model.estimate_lin_ds_nX = 10;
517 % lin_evol_model.estimate_lin_ds_nt = 4;
520 % % the following values are rough bounds, so could be
521 % % non-rigorous, then larger search must be performed.
522 % lin_evol_model.state_bound_constant_C1 = 1;
523 % lin_evol_model.output_bound_constant_C2 = 1.2916;
526 % ds_model = lin_ds_from_lin_evol_model(lin_evol_model);
527 % % set accelerated data functions from below
528 % ds_model.A_function_ptr = @(model,model_data) ...
529 % eval_affine_decomp(@A_components,...
530 % @A_coefficients,...
533 % ds_model.B_function_ptr = @(model,model_data) ...
534 % eval_affine_decomp(@B_components,...
535 % @B_coefficients,...
537 % %ds_model.u_function_ptr = @(params) 0.1; % define new
538 ds_model_data = gen_model_data(ds_model);
540 disp('skipping detailed simulations...')
544 disp(['repetition = ',num2str(r)])
545 mu = [1,1,0.5]-rand(1,3)*0.01;
546 ds_model = ds_model.set_mu(ds_model,mu);
548 ds_sim_data = detailed_simulation(ds_model,ds_model_data);
549 t_detailed(1,r) = toc;
554 disp('--------------------------------------------------')
555 disp('detailed simulation')
557 disp(['mean t_detailed=',num2str(mean(t_detailed))]);
558 disp('--------------------------------------------------')
560 % reduced simulation with and without error estimation
562 clear('ds_sim_data');
563 ds_model.RB_generation_mode = 'file_load';
564 ds_model.RB_basis_filename = ...
565 fullfile(rbmatlabresult,'advection_fv_output_basis');
566 detailed_data = gen_detailed_data(ds_model,ds_model_data);
567 ds_model.enable_error_estimator = 1;
568 reduced_data = gen_reduced_data(ds_model,detailed_data);
570 Ns = [150:-10:10]; %,30,20,10];
571 Ns = Ns(find(reduced_data.N>=Ns));
573 t_reduced_with_err_est = zeros(length(Ns),nreps);
574 t_reduced_without_err_est = zeros(length(Ns),nreps);
576 % reduced simulation with and without error estimation
579 % ds_model.error_estimation = err_est;
580 ds_model.enable_error_estimator = err_est;
581 disp(['err_est= ',num2str(err_est)]);
582 for n_ind = 1:length(Ns)
583 disp(['M = ',num2str(Ns(n_ind))]);
584 ds_model.N = Ns(n_ind);
586 disp(['repetition = ',num2str(r)])
587 mu = [1,1,1]-rand(1,3)*0.01;
588 ds_model = ds_model.set_mu(ds_model,mu);
590 rb_sim_data = rb_simulation(ds_model,reduced_data);
591 if size(rb_sim_data.Xr,1)~=Ns(n_ind)
592 disp('N dimension not set correctly!');
596 t_reduced_with_err_est(n_ind,r) = toc;
598 t_reduced_without_err_est(n_ind,r) = toc;
604 % output measurements
606 disp('--------------------------------------------------')
607 disp('detailed simulation')
609 disp(['mean t_detailed=',num2str(mean(t_detailed))]);
611 disp('--------------------------------------------------')
612 disp('reduced simulation with error estimation')
613 disp(t_reduced_with_err_est);
614 disp(['mean t_reduced_with_err_est=',...
615 num2str(mean(t_reduced_with_err_est,2)')]);
616 disp(['Ns =',num2str(Ns)]);
618 disp('--------------------------------------------------')
619 disp('reduced simulation without error estimation')
620 disp(t_reduced_without_err_est);
621 disp(['mean t_reduced_without_err_est=',...
622 num2str(mean(t_reduced_without_err_est,2)')]);
623 disp(['Ns =',num2str(Ns)]);
624 disp('--------------------------------------------------')
626 save(fullfile(rbmatlabresult,'time_measurements'));
628 % dim_x = 65536, nt = 2048
630 %mean t_detailed=64.3806
631 %--------------------------------------------------
632 %reduced simulation with error estimation
633 % 3.4542 3.5969 3.5659 3.4216 3.6170
634 % 3.3648 3.3816 3.4218 3.4391 3.4552
635 % 3.1643 3.1312 3.1244 3.1433 3.1720
636 % 3.0594 3.0496 3.0628 3.0486 3.0387
637 % 2.9993 2.9953 3.0078 2.9413 2.9603
638 % 2.9555 2.9093 2.9091 2.9510 2.9256%%%
640 %mean t_reduced_with_err_est=3.5311 3.4125 3.147 3.0518 2.9808 2.9301
641 %Ns =60 50 40 30 20 10
642 %--------------------------------------------------
643 %reduced simulation without error estimation
644 % 2.3975 2.4220 2.2924 2.4490 2.3302
645 % 2.3322 2.3198 2.3425 2.3056 2.3512
646 % 2.2227 2.2734 2.2210 2.2582 2.2504
647 % 2.2531 2.2180 2.2429 2.2212 2.2612
648 % 2.2283 2.1979 2.2581 2.2241 2.1957
649 % 2.2274 2.2014 2.2577 2.1856 2.2268
651 %mean t_reduced_without_err_est=2.3782 2.3303 2.2451 2.2393 2.2208 2.2198
652 %Ns =60 50 40 30 20 10
653 %--------------------------------------------------
655 case 8 % compare original and accelerated detailed model
657 disp('compare original and accelerated lin_ds models');
660 % initialize detailed model and simulate
661 lin_evol_model = advection_fv_output_model(params);
662 estimate_constants = 0;
663 if estimate_constants
664 %%% if constant estimaton has to be performed:
665 lin_evol_model.estimate_lin_ds_nmu = 4;
666 lin_evol_model.estimate_lin_ds_nX = 10;
667 lin_evol_model.estimate_lin_ds_nt = 4;
670 % the following values are rough bounds, so could be
671 % non-rigorous, then larger search must be performed.
672 lin_evol_model.state_bound_constant_C1 = 1;
673 lin_evol_model.output_bound_constant_C2 = 1.2916;
676 ds_model = lin_ds_from_lin_evol_model(lin_evol_model);
677 %ds_model.u_function_ptr = @(params) 0.1; % define new
678 ds_model_data = gen_model_data(ds_model);
680 % set accelerated data functions
681 ds_model_fast = lin_ds_from_lin_evol_model(lin_evol_model);
682 ds_model_fast.A_function_ptr = @(model,model_data) ...
683 eval_affine_decomp(@A_components,...
687 ds_model_fast.B_function_ptr = @(model,model_data) ...
688 eval_affine_decomp(@B_components,...
692 % ds_model_fast.C_function_ptr = @(model,model_data) ...
693 % eval_affine_decomp(@C_components,...
694 % @C_coefficients,...
697 % disp('skipping detailed simulations...')
700 mu = [1,1,0.5]-rand(1,3)*0.01;
701 ds_model = ds_model.set_mu(ds_model,mu);
702 ds_model_fast = ds_model_fast.set_mu(ds_model_fast,mu);
704 ds_sim_data = detailed_simulation(ds_model,ds_model_data);
707 ds_sim_data_fast = detailed_simulation(ds_model_fast,ds_model_data);
708 t_detailed_fast = toc;
710 disp(['original t = ',num2str(t_detailed),...
711 ', accelerated t = ',num2str(t_detailed_fast)]);
712 if ~isequal(ds_sim_data,ds_sim_data_fast)
713 disp('simulation results of original and accelerated model differ!')
715 disp('simulation results of original and accelerated model equal!')
720 case 9 % generate basis from POD-Greedy
723 ds_model = fast_model(params);
724 ds_model.cone_range = [0.4,0.6];
725 ds_model.base_model.cone_range = ds_model.cone_range;
727 % shrink range to 'single point'
728 % r = [0.4,0.6]; % => 2 -> 0.8 error nach 10 basisvektoren
729 % r = [0.0,1.0]; % => 4.6 -> 1.9 error nach 10 basisvektoren
730 % r = [0.45,0.55]; % => 2.38 -> 0.51 error nach 10 basisvektoren
731 ds_model.mu_ranges = {[0,1],[0.4,0.6],[0.4,0.6]};
732 % ds_model.rb_basis_generation_ptr = ...
733 % @basisgen_POD_greedy_uniform_fixed;
735 % ds_model.RB_generation_mode =
'uniform_fixed';
736 % ds_model.RB_num_intervals = [2,2,2];
738 ds_model_data = gen_model_data(ds_model);
740 disp(
'generating basis from POD-Greedy');
742 %
for basis generation,
set further fields:
744 ds_model.verbose = 5; % => only dots in greedy loop
745 ds_model.RB_generation_mode =
'greedy_uniform_fixed';
746 ds_model.RB_numintervals = [2,1,1]; % => 0.5 in cone_pos obtained
747 % ds_model.RB_numintervals = [4,4,4];
748 ds_model.RB_stop_epsilon = 1e-5;
749 % ds_model.RB_stop_timeout = 3600*6;
750 % ds_model.RB_stop_Nmax =150;
751 ds_model.RB_stop_timeout = 600;
752 ds_model.RB_stop_Nmax =10;
754 ds_model.RB_error_indicator =
'estimator';
756 % ds_model.get_estimator_from_sim_data = @(sim_data) sim_data.DeltaX(end);
758 detailed_data = gen_detailed_data(ds_model,ds_model_data);
761 dfname = fullfile(rbmatlabresult,
'advection_fv_output_lin_ds_detailed');
762 save(dfname,
'model',
'detailed_data');
764 disp(
'successfully generated and saved detailed_data');
766 params.plot_title =
'reduced basis';
767 params.plot = model.base_model.plot;
768 lin_evol_model_data = gen_model_data(model.base_model);
769 plot_sequence(detailed_data.V,lin_evol_model_data.grid,params)
773 case 10 % reduced simulations and error plots
775 %basis_from_step = 9; % step 9
776 basis_from_step = 4; % step 4
778 if basis_from_step == 9
779 dfname = fullfile(rbmatlabresult,'advection_fv_output_lin_ds_detailed');
781 disp('loaded basis from step 9')
783 load(fullfile(rbmatlabresult,'advection_fv_output_detailed_data_2traj'),...
784 'model','detailed_data')
786 % model = fast_model(params);
787 model_data = gen_model_data(model);
788 % dfname = fullfile(rbmatlabresult,'advection_fv_output_detailed_data_146');
789 % model.RB_basis_filename = ...
790 % fullfile(rbmatlabresult,'advection_fv_output_basis');
791 % model.RB_generation_mode = 'file_load';
792 % detailed_data = gen_detailed_data(model,model_data);
793 % dfname = fullfile(rbmatlabresult,'advection_fv_output_detailed');
795 tmp = load(fullfile(rbmatlabresult,'trajectory2'));
796 ds_sim_data = tmp.ds_sim_data;
797 mu_from_file = get_mu(tmp.ds_model);
798 disp('loaded basis from step 4')
799 model = model.set_mu(model,mu_from_file);
801 % trajectory2 above is made with this
802 % model = model.set_mu(model,[1,1,1]);
803 % model = model.set_mu(model,[1,1,1]);
804 % trajectory3 above is made with this:
805 %model = model.set_mu(model,[1,1,0.5]);
806 %model = model.set_mu(model,[0.5,0.6,0.6]);
807 %model = model.set_mu(model,[0.5,0.4,0.4]);
808 %model = model.set_mu(model,[0.5,0.5,0.5]);
809 model.N = size(detailed_data.V,2);
811 reduced_data = gen_reduced_data(model,detailed_data);
812 rb_sim_data = rb_simulation(model,reduced_data);
815 params.show_colorbar = 1;
816 params.axis_equal = 1;
818 % plot of reduced output
820 p = plot(rb_sim_data.time,...
822 rb_sim_data.Y+rb_sim_data.DeltaY; ...
823 rb_sim_data.Y-rb_sim_data.DeltaY]);
824 colors = {[1,0,0],[1,0,0],[1,0,0],[0,1,0]};
825 linestyles = {
'-',
'-.',
'-.',
'-'};
828 set(p(i),'Color',colors{i},
'Linestyle',linestyles{i},...
829 'Linewidth',widths(i));
831 %
set marker on rb_sim_data:
832 Xdata =
get(p(1),
'XData');
833 Ydata =
get(p(1),
'YData');
834 Zdata =
get(p(1),
'ZData');
835 set(p(1),
'XData',Xdata(1:4:end));
836 set(p(1),
'YData',Ydata(1:4:end));
837 set(p(1),
'ZData',Zdata(1:4:end));
838 set(p(1),
'Marker',
'o');
840 l = legend([
'y\^(t): reduced output'],...
841 'y\^(t)+\Delta_y(t)',...
842 'y\^(t)-\Delta_y(t)',
'Location',
'NorthWest');
843 set(l,
'Fontsize',15);
844 title([
'k = ',num2str(model.N)],
'Fontsize',15);
846 % table of error, error estimator and effectivity
848 reduced_data = gen_reduced_data(model,detailed_data);
851 Ns = Ns(find(reduced_data.N>=Ns));
853 err = zeros(1,length(Ns));
854 err_estim = zeros(1,length(Ns));
855 effectivity = zeros(1,length(Ns));
857 for ni = 1:length(Ns)
859 rb_sim_data = rb_simulation(model, reduced_data);
860 err(ni) = abs(ds_sim_data.Y(end) - rb_sim_data.Y(end));
861 err_estim(ni) = rb_sim_data.DeltaY(end);
862 effectivity(ni) = err_estim(ni)/err(ni);
868 disp('finished computing effectivities')
871 case 11 % test of matlab ode solver for resulting sparse system
873 params.coarse_factor = 8; % make model smaller
874 model = fast_model(params);
875 model.RB_generation_mode = 'from_detailed_data';
877 % detailed simulation:
878 model_data = gen_model_data(model);
879 model_data.RB = zeros(size(model_data.G,1),0);
880 detailed_data = gen_detailed_data(model,model_data);
882 model.decomp_mode = 0; % complete
883 model = model.set_time(model,0)
884 model = model.set_mu(model,[0.5,1,1]);
886 A = @(model,model_data,t) ...
887 model.A_function_ptr(model.set_time(model,t),model_data);
889 B = @(model,model_data,t) ...
890 model.B_function_ptr(model.set_time(model,t),model_data);
892 x0 = model.x0_function_ptr(model,model_data);
894 [t,x] = ode15s(@(t,x) A(model,model_data,t)*x + B(model,model_data,t), ...
897 disp(['ode15 takes ',num2str(time_ode15),' sec']);
899 params.plot_title = 'ode15 solution';
900 params.plot = model.base_model.plot;
901 params.axis_equal = 1;
902 lin_evol_model_data = gen_model_data(model.base_model);
906 sim_data = detailed_simulation(model,model_data);
908 disp(['rbmatlab takes ',num2str(time_rbmatlab),' sec']);
909 params.plot_title = 'rbmatlab solution';
910 params.plot = model.base_model.plot;
911 params.axis_equal = 1;
912 lin_evol_model_data = gen_model_data(model.base_model);
915 % further plots for MCMCS paper
918 load(fullfile(rbmatlabresult,'advection_fv_output_detailed_data_2traj'),...
919 'model','detailed_data')
920 model.enable_error_estimator = 1;
921 % plot of error estimator over parameter variation
922 % model = fast_model(params);
923 % model_data = gen_model_data(model);
924 % dfname = fullfile(rbmatlabresult,'advection_fv_output_detailed_data_146');
925 % model.RB_basis_filename = ...
926 % fullfile(rbmatlabresult,'advection_fv_output_basis');
927 % model.RB_generation_mode = 'file_load';
928 % detailed_data = gen_detailed_data(model,model_data);
929 % save(fullfile(rbmatlabresult,...
930 % 'advection_fv_output_detailed_2trajectories.mat'),...
931 % 'model','detailed_data')
933 % dfname = fullfile(rbmatlabresult,'advection_fv_output_detailed');
935 tmp = load(fullfile(rbmatlabresult,'trajectory2'));
936 ds_sim_data = tmp.ds_sim_data;
937 disp('loaded basis from step 4')
939 % factors = 0:0.05:1;
940 reduced_data = gen_reduced_data(model,detailed_data);
942 Ns = Ns(find(reduced_data.N>=Ns));
944 err_estim = zeros(length(factors),length(Ns));
947 for ni = 1:length(Ns)
949 for i =1:length(factors)
950 model = model.set_mu(model, factors(i)*[1,1,1]);
951 rb_sim_data = rb_simulation(model, reduced_data);
952 % err(ni) = abs(ds_sim_data.Y(end) - rb_sim_data.Y(end));
953 err_estim(i,ni) = rb_sim_data.DeltaY(end);
954 % effectivity(ni) = err_estim(ni)/err(ni);
962 p = plot(factors,err_estim');
963 colors = {[1,0,0],[0,0.6,0],[0,0,1],[0,0,0]};
964 linestyles = {
'-',
'-.',
'-',
'-.'};
966 set(p(i),
'Linewidth',2,
'Color',colors{i},
'Linestyle',linestyles{i});
969 xlabel(
'parameter \mu_1=\mu_2=\mu_3',
'Fontsize',15);
970 ylabel(
'\Delta_y(\mu)',
'Fontsize',15);
971 title(
'error estimator from parameter sweep',
'Fontsize',15)
972 l = legend({
'k=1',
'k=4',
'k=8',
'k=16'},
'Location',
'NorthWest');
973 set(l,
'Fontsize',15);
974 saveas(gcf,fullfile(rbmatlabresult,
'parameter_sweep.eps'),
'epsc');
975 % plot of
true errors
977 model_data = gen_model_data(model);
980 % factors = [0,0.5,1];
981 errs = zeros(length(factors),length(Ns));
982 for i =1:length(factors)
983 model = model.set_mu(model, factors(i)*[1,1,1]);
985 for ni = 1:length(Ns)
987 rb_sim_data = rb_simulation(model, reduced_data);
988 % err(ni) = abs(ds_sim_data.Y(end) - rb_sim_data.Y(end));
989 e = rb_sim_data.Y-ds_sim_data.Y;
990 e = max(sqrt(max(sum(e.*e),0)));
992 % effectivity(ni) = err_estim(ni)/err(ni);
996 p = plot(factors,errs');
997 colors = {[1,0,0],[0,0.6,0],[0,0,1],[0,0,0]};
998 linestyles = {
'-',
'-.',
'-',
'-.'};
1000 set(p(i),
'Linewidth',2,
'Color',colors{i},
'Linestyle',linestyles{i});
1002 % same range as first plot
1003 set(gca,
'Ylim',[0,10]);
1005 xlabel(
'parameter \mu_1=\mu_2=\mu_3',
'Fontsize',15);
1006 ylabel(
'|y(\mu)-y\^(\mu)|',
'Fontsize',15);
1007 title(
'output errors from parameter sweep',
'Fontsize',15)
1008 l = legend({
'k=1',
'k=4',
'k=8',
'k=16'},
'Location',
'NorthWest');
1009 set(l,
'Fontsize',15);
1010 saveas(gcf,fullfile(rbmatlabresult,
'parameter_sweep_errs.eps'),
'epsc');
1012 % plot of
true errors
1015 disp(
'step number unknown');
1018 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1019 % generation of ds_model from lin_evol_model and replace
1021 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1023 function ds_model = fast_model(params);
1025 lin_evol_model = advection_fv_output_model(params);
1026 %lin_evol_model.debug = 1;
1027 %lin_evol_model_data = gen_model_data(lin_evol_model);
1029 %
set some additional fields in model which will be copied into
1030 % ds model depending on whether constants are known or not:
1031 estimate_constants = 0;
1032 if estimate_constants
1033 %%%
if constant estimaton has to be performed:
1034 lin_evol_model.estimate_lin_ds_nmu = 4;
1035 lin_evol_model.estimate_lin_ds_nX = 10;
1036 lin_evol_model.estimate_lin_ds_nt = 4;
1039 % the following values are rough bounds, so could be
1040 % non-rigorous, then larger search must be performed.
1041 lin_evol_model.state_bound_constant_C1 = 1;
1042 lin_evol_model.output_bound_constant_C2 = 1.2916;
1045 ds_model = lin_ds_from_lin_evol_model(lin_evol_model);
1046 %
set accelerated data functions from below
1047 ds_model.A_function_ptr = @(model,model_data) ...
1048 eval_affine_decomp(@A_components,...
1052 ds_model.B_function_ptr = @(model,model_data) ...
1053 eval_affine_decomp(@B_components,...
1056 %ds_model.u_function_ptr = @(params) 0.1; % define
new
1058 %ds_model.theta = 1;
1059 %disp(
'set theta to 1!!!');
1065 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1066 % auxialiary coefficient functions
for acceleration of
1067 % advection model:
explicit implementation of coefficients
1068 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1070 function Acomp = A_components(model,model_data)
1072 Acomp = model_data.A_components;
1074 function Acoeff = A_coefficients(model);
1077 %model.base_model.decomp_mode = 2;
1078 %model.base_model.t = model.t;
1079 %mu = get_mu(model);
1080 %model.base_model = model.set_mu(model.base_model,mu);
1081 %[L_I_coeff, L_E_coeff, b_coeff] = ...
1082 % model.base_model.operators_ptr(model.base_model, );
1083 %% L_E = Id + Delta t * A
1084 %Acoeff = L_E_coeff(2:end)/model.base_model.dt;
1086 %Acoeff = -model.base_model.dt * ...
1087 % model.base_model.velocity_coefficients_ptr(model.base_model);
1089 Acoeff = - [model.vx_weight, model.vy_weight]*(1-model.t);
1093 function Bcomp = B_components(model,model_data)
1095 Bcomp = model_data.B_components;
1097 function Bcoeff = B_coefficients(model);
1100 % hmmm the coefficients are now called twice in A and B
1101 % should somehow be cached later...
1102 %model.base_model.decomp_mode = 2;
1103 %model.base_model.t = model.t;
1104 %mu = get_mu(model);
1105 %model.base_model = model.set_mu(model.base_model,mu);
1106 %[L_I_coeff, L_E_coeff, b_coeff] = ...
1107 % model.base_model.operators_ptr(model.base_model, );
1108 %% L_E = Id + Delta t * A
1109 %Bcoeff2 = b_coeff/model.base_model.dt;
1110 vcoeff = - [model.vx_weight, model.vy_weight]*(1-model.t);
1111 Q_0 = model.base_model.cone_number;
1112 dcoeff = zeros(1,Q_0);
1113 max_pos = model.cone_weight * (Q_0-1)+1;
1116 dcoeff(q) = (1-(max_pos-q))*(1-t) * ((max_pos>=q) && (max_pos < q+1)) ...
1117 + (1+(max_pos-q))*(1-t) * ((max_pos>=q-1) && (max_pos < q));
1119 v = -(vcoeff(:)*(dcoeff(:)'));
1125 %
function Ccomp = C_components(model,model_data)
1126 %Ccomp = model_data.C_components;
1128 %
function Ccoeff = C_coefficients(model);
1129 %model.base_model.decomp_mode = 2;
1130 %model.base_model.t = model.t;
1131 %mu = get_mu(model);
1132 %model.base_model = model.set_mu(model.base_model,mu);
1134 % model.base_model.operators_output(model.base_model);