1 function adaptive_basisgen_mcmds(step, loadfilename)
2 %
function adaptive_basisgen_mcmds(step)
4 % small script performing the experiments
for the (second half) of
5 % the MCMDS paper. Experiments are based on a simple advection example
7 % Options
for the experiments (like 2-D, 3-D
case , etc) can be fixed
10 % mu_names = {
'cone_weight',
'vx_weight',
'vy_weight'};
15 % step 1: detailed simulation of single trajectory, plot
16 % POD and save reduced basis
17 % reduced simulation and error
18 % step 2: conduct a basis generation
using the RB-method defined in the
20 % detailed_data and model are saved in a mat-file
21 % step 3: the previously saved mat-file is loaded and an animation/a plot
22 % of the basisgeneration and p-partition is made
23 % step 31: detailed and reduced simulation of previously computed basis
25 % Experiments for the MCMDS paper:
26 % step 4: Basisgeneration using NO p-partition and a fixed-greedy
28 % M-Train for greedy can be fixed, but will probably be a 3^p grid
29 % detailed_data and model saved in mat file
31 % step 41: mat file from previous step is loaded.
34 % step 5: Basisgeneration using a fixed p-partition and a fixed-greedy
35 % algorithm using the same M-Train grid as the previous step
36 % No adaption of p-grid, so no N_max is given
37 % detailed_data and model are saved
38 % step 51: plot of p-grid of previous step
39 % ???other plots useful????
41 % step 6: Basisgeneration using an adaptive p-partition given an N_max of
42 % basis vectors per partition and a fixed-greedy algorithm using
43 % the same M-Train grid as in the previous two steps.
44 % detailed_data and the model are saved
46 % step 61: plot of the p-grid... other plots???
50 % Bernard Haasdonk 4.2.2010
51 % Markus Dihlmann 15.02.2010
59 % ---------------------------
63 %params.coarse_factor = 2; % fact 2 => ndofs = 16384, nt = 1024, 15s detailed
64 %params.coarse_factor = 1; % fact 1 => ndofs = 32768, nt = 2048, 60s detailed
65 params.coarse_factor = 2;
67 trajectory_fnbase =
'advection_trajectory';
68 detailed_data_fname =
'advection_detailed_data';
69 rb_fname =
'advection_rb';
71 % Set Parameter-space dimensions:
72 mu_dimensions ='2D-case';
73 %mu_dimensions =
'3D-case';
74 params.mu_dimensions=mu_dimensions;
79 % Set RB-construction method:
80 %RB_method = 'POD_of_single_trajectory';
81 %RB_method =
'POD_of_several_trajectories';
82 %RB_method =
'greedy_random_fixed';
83 %RB_method =
'greedy_uniform_fixed';
84 %RB_method =
'greedy_refined_uniform';
85 %RB_method =
'greedy_refined_adaptive';
86 %RB_method =
'p_part_model_uniform';
87 RB_method =
'p_part_model_adaptive';
89 % Set,
if plot should be colored or not:
91 options.simple_grid=1; %
if =1 plot is not colored
98 disp(
'initialization of lin_ds model from lin_evol model');
99 %
verbose(1,
'initialization of lin_ds model from lin_evol model and ...
102 ds_model = fast_model(params);
103 ds_model.save_time_indices = 0:ds_model.nt;
105 %ds_model.u_function_ptr = @(params) 0.1; % define
new
106 % ds_model.theta = 1;
108 ds_model_data = gen_model_data(ds_model);
116 %
set mu in model and base_model!!!
117 ds_model = ds_model.set_mu(ds_model,mu);
118 ds_sim_data = detailed_simulation(ds_model,ds_model_data);
121 save(fullfile(rbmatlabtemp,[trajectory_fnbase,num2str(1)]),...
122 'ds_model',
'ds_model_data',
'ds_sim_data');
127 params.plot_title=
'detailed implicit lin_ds';
130 disp(
'PCA of snapshots takes a while...');
131 disp(
'Loading of trajectory...');
132 load(fullfile(rbmatlabtemp,[trajectory_fnbase,
'1']),...
133 'ds_model',
'ds_model_data',
'ds_sim_data');
135 % smaller epsilon does result in more than 1 vector
for mu=0,0,0
137 disp(
'Calling PCA_Fixspace...');
138 RB = PCA_fixspace(ds_sim_data.X,[],ds_model_data.G,150,[],epsilon);
140 %RB = orthonormalize_qr(ds_sim_data.X,ds_model_data.G);
141 % select correct orthogonal
set
142 K = RB
' * ds_model_data.G * RB;
143 i = find(abs(diag(K)-1)>1e-2);
145 disp(['found
',num2str(size(RB,2)),' basis vectors after traj. 1
']);
151 disp(['reduced to
',num2str(size(RB,2)),' basis vectors.
']);
155 W = ds_model_data.G * V;
156 save(fullfile(rbmatlabtemp,rb_fname),'RB
');
158 ds_model.RB_basis_filename = fullfile(rbmatlabtemp,rb_fname);
159 ds_model.RB_generation_mode = 'file_load
';
161 detailed_data = gen_detailed_data(ds_model,ds_model_data);
164 save(fullfile(rbmatlabtemp,detailed_data_fname),...
165 'model
','detailed_data
')
167 % compare detailed and reduced simulation
168 load(fullfile(rbmatlabtemp,detailed_data_fname),...
169 'model
','detailed_data
')
170 load(fullfile(rbmatlabtemp,[trajectory_fnbase,'1
']),...
175 %perform single reduced simulation for given parameter
176 online_data = gen_online_data(ds_model,detailed_data);
177 ds_model.N = online_data.N;
179 % ds_model.nt = ds_model.nt/4;
180 % disp('set nt to quarter!!
');
181 rb_sim_data = rb_simulation(ds_model,online_data);
183 rb_sim_data = rb_reconstruction(ds_model,detailed_data,rb_sim_data)
184 disp(['time
for reduced simulation:
',num2str(t_red)]);
186 % plot of reduced trajectory
187 lin_evol_model = ds_model.base_model;
188 lin_evol_model_data = gen_model_data(lin_evol_model);
191 params.show_colorbar = 1;
192 params.axis_equal = 1;
193 params.plot = lin_evol_model.plot;
194 % plot single snapshot
195 %lin_evol_model.plot(rb_sim_data.X(:,1),lin_evol_model_data.grid, ...
199 params.title = 'reduced solution
';
200 plot_sequence(rb_sim_data.X,lin_evol_model_data.grid,params)
203 params.title = 'error
';
204 plot_sequence((rb_sim_data.X-ds_sim_data.X),...
205 lin_evol_model_data.grid,params)
207 % plot of reduced output
208 figure,plot(rb_sim_data.time,[rb_sim_data.Y;ds_sim_data.Y]);
209 legend('reduced output
','detailed output
');
213 % generate more intelligent basis
215 disp('initialization of lin_ds model from lin_evol model
');
216 % verbose(1,'initialization of lin_ds model from lin_evol model and ...
219 model = fast_model(params);
220 model.save_time_indices = 0:model.nt;
222 model.get_rb_from_detailed_data = @(detailed_data) detailed_data.V; %only improvised help-method for method rb_test_indicator.m
227 model.RB_numintervals = [1,1];
230 model.RB_numintervals = [1,1,1];
233 %
set mu in model and base_model!!!
234 model = model.set_mu(model,mu);
235 model.RB_stop_Nmax = 150;
240 case 'POD_of_single_trajectory'
241 model.RB_generation_mode =
'PCA_trajectory';
243 case 'POD_of_several_trajectories'
244 %%% basis generation. POD of several trajectories
245 model.RB_generation_mode =
'PCA_trajectories';
248 model.RB_mu_list = {[0,0],[1,1]};
250 model.RB_mu_list = {[0,0,0],[1,1,1]};
253 case 'greedy_random_fixed'
254 %%% basis generation: greedy_random_fixed
255 model.RB_generation_mode =
'greedy_random_fixed';
256 model.RB_train_rand_seed = 12356;
257 model.RB_train_size = 5;
258 model.RB_error_indicator =
'estimator';
259 model.RB_stop_epsilon = 1e-2;
260 model.RB_stop_timeout = 1*60; % 1 minute
261 model.RB_stop_Nmax = 5;
264 case 'greedy_uniform_fixed'
265 %%% basis generation: greedy_uniform_fixed
266 model.RB_generation_mode =
'greedy_uniform_fixed';
267 model.RB_error_indicator =
'estimator';
269 model.RB_stop_epsilon = 1e-2;
270 model.RB_stop_timeout = 1*60; % 1 minute
271 model.RB_stop_Nmax = 5;
273 case 'greedy_refined_uniform'
274 %%% basis generation: greedy_refined with uniform refinement
275 model.RB_generation_mode =
'greedy_refined';
276 model.RB_stop_max_val_train_ratio = 0.95;
277 model.RB_refinement_mode =
'uniform';
278 model.RB_val_rand_seed = 543;
279 model.RB_M_val_size = 10;
280 model.RB_max_refinement_level = 5;
281 model.RB_error_indicator =
'estimator';
283 model.RB_stop_epsilon = 1e-8;
284 model.RB_stop_timeout = 60*5; % 5 minutes
285 model.RB_stop_Nmax = 4;
286 %model.mu_ranges = {[0,1.0],[0,1.0],[0,1.0]};%,[0.4,0.6]};
288 case 'greedy_refined_adaptive'
289 % basis generation: greedy_refined with local refinement
290 % RB_detailed_val_savepath
291 model.RB_generation_mode =
'greedy_refined';
292 model.RB_refinement_mode =
'adaptive';
293 model.RB_stop_max_val_train_ratio = 0.7;
294 model.RB_refinement_theta = 0.2;
295 model.RB_element_indicator_mode=
'nodes_cogs_skippedrefs';
296 model.RB_element_indicator_s_max = 5;
297 model.RB_val_rand_seed = 543;
298 model.RB_M_val_size = 10;
299 model.RB_max_refinement_level = 10;
300 model.RB_error_indicator =
'estimator';
302 model.RB_stop_epsilon = 1e-2;
303 model.RB_stop_timeout = 45*60; % ... minutes
304 model.RB_stop_Nmax = 40;
306 case 'p_part_model_uniform'
307 % convert model into a p_part model
308 %!!usual model attributes must be fixed
309 model.RB_generation_mode =
'greedy_refined';
310 model.RB_stop_max_val_train_ratio = 0.95;
311 model.RB_refinement_mode =
'uniform';
312 model.RB_val_rand_seed = 543;
313 model.RB_M_val_size = 10;
314 model.RB_max_refinement_level = 5;
315 model.RB_error_indicator =
'estimator';
317 model.RB_stop_epsilon = 1e-8;
318 model.RB_stop_timeout = 60*5; % 5 minutes
319 model.RB_stop_Nmax = 4;
320 params.p_part_generation_mode =
'uniform';
323 params.p_part_numintervals = [2,2];
324 model = p_part_model(model,params);
326 params.p_part_numintervals = [2,2,2];
327 model = p_part_model(model,params);
330 case 'p_part_model_adaptive'
332 model.RB_generation_mode =
'greedy_refined';
333 model.RB_refinement_mode =
'adaptive';
334 model.RB_stop_max_val_train_ratio = 2;
335 model.RB_refinement_theta = 0.2;
336 model.RB_element_indicator_mode=
'nodes_cogs_skippedrefs';
337 model.RB_element_indicator_s_max = 5;
338 model.RB_val_rand_seed = 543;
339 model.RB_M_val_size = 10;
340 model.RB_max_refinement_level = 10;
341 model.RB_error_indicator =
'estimator';
343 model.RB_stop_epsilon = 1e-2;
344 model.RB_stop_timeout = 15*60*60; % .
345 model.RB_stop_Nmax = 2; %maximal number of RB-vectors per p_partition
346 model.p_part_max_refinement_level=1; %%Maximale refinement level
for p_partitions
347 % convert model into a p_part model
348 params.p_part_generation_mode =
'adaptive';
351 params.p_part_numintervals =[1,1];
353 params.p_part_numintervals = [1,1,1];
355 model = p_part_model(model,params);
363 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
367 model_data = gen_model_data(model);
368 detailed_data = gen_detailed_data(model,model_data);
370 save(fullfile(rbmatlabtemp,
'p_part_basisgen_data'),
'model',
'detailed_data');
372 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
378 case 3 %load detailed data generated in previous step and perform animation and reduced simulations
380 load(fullfile(rbmatlabtemp,
'p_part_basisgen_data'));
382 % plot an animation of mesh refinement during basis generation
384 if ~(strcmp(RB_method,
'POD_of_single_trajectory')||strcmp(RB_method,
'POD_of_several_trajectories')||strcmp(RB_method,
'p_part_model_uniform')||strcmp(RB_method,
'p_part_model_adaptive'))
385 animate_basisgen(detailed_data,model,options);
388 %plot partitions of parameter space
389 if (strcmp(RB_method,
'p_part_model_uniform')||strcmp(RB_method,
'p_part_model_adaptive'))
391 %nleaf_elements=
get(detailed_data.pgrid,
'nleafelements');
393 %
for i=1:nleaf_elements
394 % gid = lid2gid(detailed_data.pgrid,i);
395 % RB_N_str{i}=num2str(size(detailed_data.parts_detailed_data{gid}.RB_info.mu_ind_seq,2));
399 % plot_options.text=RB_N_str;
400 plot_grid(detailed_data.pgrid,plot_options);
402 %plot grid from greedy adaptive into the existant p-part grid
403 if isfield(model,'base_model')
404 if (isfield(model.base_model,'RB_refinement_mode')&&(strcmp(model.base_model.RB_refinement_mode,'adaptive')))
407 options.text=plot_options.text;
408 animate_basisgen(detailed_data,model,options);
414 save(fullfile(rbmatlabtemp,detailed_data_fname),...
415 'model',
'detailed_data')
419 % compare detailed and reduced simulation
420 load(fullfile(rbmatlabtemp,detailed_data_fname),...
421 'model',
'detailed_data')
422 load(fullfile(rbmatlabtemp,[trajectory_fnbase,'1']),...
426 %perform single reduced simulation for given parameter
427 online_data = gen_online_data(model,detailed_data);
428 if ~(strcmp(RB_method,'p_part_model_uniform')||strcmp(RB_method,'p_part_model_adaptive'))
429 model.N = online_data.N;
432 % model.nt = model.nt/4;
433 % disp('set nt to quarter!!');
434 rb_sim_data = rb_simulation(model,online_data);
436 rb_sim_data = rb_reconstruction(model,detailed_data,rb_sim_data)
437 disp(['time for reduced simulation: ',num2str(t_red)]);
439 % plot of reduced trajectory
440 if ~(strcmp(RB_method,'p_part_model_uniform')||strcmp(RB_method,'p_part_model_adaptive'))
441 lin_evol_model = model.base_model;
443 lin_evol_model = model.base_model.base_model;
447 lin_evol_model_data = gen_model_data(lin_evol_model);
450 params.show_colorbar = 1;
451 params.axis_equal = 1;
452 params.plot = lin_evol_model.plot;
453 % plot single snapshot
454 %lin_evol_model.plot(rb_sim_data.X(:,1),lin_evol_model_data.grid, ...
458 params.title = 'reduced solution';
462 params.title = 'error';
464 lin_evol_model_data.grid,params)
466 % plot of reduced output
467 figure,plot(rb_sim_data.time,[rb_sim_data.Y;ds_sim_data.Y]);
468 legend('reduced output','detailed output');
475 case 31 %load detailed data generated in previous step and perform animation and reduced simulations
477 detailed_data_fname = 'save_case_4'
479 load(fullfile(rbmatlabtemp,detailed_data_fname));
481 % plot an animation of mesh refinement during basis generation
483 % if ~(strcmp(RB_method,'POD_of_single_trajectory')||strcmp(RB_method,'POD_of_several_trajectories')||strcmp(RB_method,'p_part_model_uniform')||strcmp(RB_method,'p_part_model_adaptive'))
484 % animate_basisgen(detailed_data,model,options);
487 % %plot partitions of parameter space
488 % if (strcmp(RB_method,'p_part_model_uniform')||strcmp(RB_method,'p_part_model_adaptive'))
490 % %nleaf_elements=get(detailed_data.pgrid,'nleafelements');
492 % %for i=1:nleaf_elements
493 % % gid = lid2gid(detailed_data.pgrid,i);
494 % % RB_N_str{i}=num2str(size(detailed_data.parts_detailed_data{gid}.RB_info.mu_ind_seq,2));
498 % plot_options.text=RB_N_str;
499 % plot_grid(detailed_data.pgrid,plot_options);
501 %plot grid from greedy adaptive into the existant p-part grid
502 % if isfield(model,'base_model')
503 % if (isfield(model.base_model,'RB_refinement_mode')&&(strcmp(model.base_model.RB_refinement_mode,'adaptive')))
504 % options.figureNr=1;
506 % options.text=plot_options.text;
507 % animate_basisgen(detailed_data,model,options);
513 % save(fullfile(rbmatlabtemp,detailed_data_fname),...
514 %
'model',
'detailed_data')
518 % compare detailed and reduced simulation
519 load(fullfile(rbmatlabtemp,detailed_data_fname),...
520 'model',
'detailed_data')
522 tr_fn = fullfile(rbmatlabtemp,[trajectory_fnbase,'1']);
527 model_data = gen_model_data(model);
528 ds_sim_data = detailed_simulation(model,model_data);
531 model.N = model.get_rb_size(model,detailed_data);
533 %perform single reduced simulation for given parameter
534 online_data = gen_online_data(model,detailed_data);
535 RB_method = model.RB_generation_mode;
536 if ~(strcmp(RB_method,'p_part_model_uniform')||strcmp(RB_method,'p_part_model_adaptive'))
537 model.N = online_data.N;
540 % model.nt = model.nt/4;
541 % disp('set nt to quarter!!');
542 rb_sim_data = rb_simulation(model,online_data);
544 rb_sim_data = rb_reconstruction(model,detailed_data,rb_sim_data)
545 disp(['time for reduced simulation: ',num2str(t_red)]);
547 % % plot of reduced trajectory
548 if ~(strcmp(RB_method,'p_part_model_uniform')||strcmp(RB_method,'p_part_model_adaptive'))
549 lin_evol_model = model.base_model;
551 lin_evol_model = model.base_model.base_model;
554 lin_evol_model_data = gen_model_data(lin_evol_model);
557 params.show_colorbar = 1;
558 params.axis_equal = 1;
559 params.plot = lin_evol_model.plot;
560 % plot single snapshot
561 %lin_evol_model.plot(rb_sim_data.X(:,1),lin_evol_model_data.grid, ...
565 params.title = 'reduced solution';
569 params.title = 'error';
571 lin_evol_model_data.grid,params)
573 % plot of reduced output
574 figure,plot(rb_sim_data.time,[rb_sim_data.Y;ds_sim_data.Y]);
575 legend('reduced output','detailed output');
582 % step 4: Basisgeneration using NO p-partition and a fixed-greedy
584 % M-Train for greedy can be fixed, but will probably be a 3^p grid
585 % detailed_data and model saved in mat file
588 model = fast_model(params);
589 model.save_time_indices = 0:model.nt;
591 model.get_rb_from_detailed_data = @(detailed_data) detailed_data.V; %only improvised help-method for method
rb_test_indicator.m
596 model.RB_numintervals = [3,3];
599 model.RB_numintervals = [2,2,2];
602 % set mu in model and base_model!!!
603 model = model.set_mu(model,mu);
605 model.RB_generation_mode = 'greedy_uniform_fixed';
606 model.RB_error_indicator = 'estimator';
608 model.RB_stop_epsilon = 1e-2;
609 model.RB_stop_timeout = 12*60*60; %
610 model.RB_stop_Nmax = 1000;
614 model.filecache_ignore_fields_in_model=[model.filecache_ignore_fields_in_model,'mu_ranges']; %for making possible to read data from cache in p-partitions
616 model_data = gen_model_data(model);
617 overall_generation_time = tic;
618 detailed_data = gen_detailed_data(model,model_data);
619 t_gen = toc(overall_generation_time); %time necessary for generating the basis
621 %construct name of file to save:
622 %case_epsilon_N_parametergreedybase_coarsefactor
623 file_name = ['save_case_4_',num2str(model.RB_stop_epsilon),'_',num2str(params.coarse_factor),'_',num2str(model.RB_numintervals(1)),'.mat'];
624 save(fullfile(rbmatlabtemp,file_name),'model','detailed_data','t_gen');
630 % step 41: mat file from previous step is loaded.
634 load(fullfile(rbmatlabtemp,loadfilename));
635 options.simple_grid=1; %if =1 plot is not colored
636 model.random_seed=random_seed;
640 online_data = gen_online_data(model,detailed_data);
641 model.N = online_data.N; %Unschoen aber nötig...
643 %reduced_timer = tic;
644 %model=set_mu(model,[0.5 0.5]);
645 % model.nt = model.nt/4;
646 % disp('set nt to quarter!!');
647 %rb_sim_data = rb_simulation(model,online_data);
648 %t_red = toc(reduced_timer);
650 model.random_seed=1234;%debug ...raus machen!!!
651 [test_error,test_error_max,av_rb_sim_time] = calculate_test_error(model, online_data);
653 %rb_sim_data = rb_reconstruction(model,detailed_data,rb_sim_data);
654 disp(['time for reduced simulation: ',num2str(av_rb_sim_time)]);
655 disp('Results of basis generation without p_partition:');
656 disp(['intervalls:',num2str(model.RB_numintervals(1))]);
657 disp(['Test Error Average(estimated error): ',num2str(test_error)]);
658 disp(['Maximal Test Error (estimated error):',num2str(test_error_max)]);
659 disp(['Time for generation of detailed_data: ',num2str(t_gen)]);
660 disp(['Epsilon criteria to stop was: ',num2str(model.RB_stop_epsilon)]);
661 disp(['Nmax was: ',num2str(model.RB_stop_Nmax)]);
662 disp(['Number of basis vectors: ',num2str(length(detailed_data.RB_info.mu_sequence))]);
663 if(detailed_data.RB_info.stopped_on_epsilon)
664 disp('Stopped basis generation due to reached epsilon criteria');
666 if(detailed_data.RB_info.stopped_on_timeout)
667 disp('Stopped basis generation due to reached time limit');
669 if(detailed_data.RB_info.stopped_on_Nmax)
670 disp('Stopped basis generation due to reached maximum number of basis vectors ');
673 if strcmp(mu_dimensions,'2D-case')
674 disp(['vy_weight was: ',num2str(model.base_model.vy_weight)]);
677 %Write results to a protocol file
679 settings.setting = 'Without p-partitioning';
680 settings.file_name = loadfilename;
681 settings.Greedy_intervals = model.RB_numintervals(1);
682 settings.stop_on_epsilon = model.RB_stop_epsilon;
683 settings.stop_on_N_max = model.RB_stop_Nmax;
684 if strcmp(mu_dimensions,'2D-case')
685 settings.vy_weight = model.base_model.vy_weight;
688 results.Number_of_basisvectors_used = length(detailed_data.RB_info.mu_sequence);
689 results.average_test_error = test_error;
690 results.maximal_test_error = test_error_max;
691 results.time_for_reduced_simulation = av_rb_sim_time;
692 results.time_for_generation = t_gen;
695 % plot basisgeneration plot
696 animate_basisgen(detailed_data,model,options);
698 save_result_protocol('p_part_test protokoll',settings, results);
702 % step 5: Basisgeneration using a fixed p-partition and a fixed-greedy
703 % algorithm using the same M-Train grid as the previous step
704 % No adaption of p-grid, so no N_max for refinement is given
705 % detailed_data and model are saved
707 model = fast_model(params);
708 model.save_time_indices = 0:model.nt;
710 model.get_rb_from_detailed_data = @(detailed_data) detailed_data.V; %only improvised help-method for method
rb_test_indicator.m
715 model.RB_numintervals = [2,2];
718 model.RB_numintervals = [2,2,2];
721 % set mu in model and base_model!!!
722 model = model.set_mu(model,mu);
723 % convert model into a p_part model
724 %!!usual model attributes must be fixed
726 model.RB_generation_mode = 'greedy_uniform_fixed';
727 model.RB_error_indicator = 'estimator';
728 model.RB_extension_algorithm = @RB_extension_PCA_fixspace;
729 model.RB_stop_epsilon = 1e-4;
730 model.RB_stop_timeout = 60*60*12;
731 model.RB_stop_Nmax = 1000;
732 model.filecache_ignore_fields_in_model=[model.filecache_ignore_fields_in_model,'mu_ranges']; %for making possible to read data from cache in p-partitions
733 params.p_part_generation_mode = 'uniform';
736 params.p_part_numintervals = [2,2];
737 model = p_part_model(model,params);
739 params.p_part_numintervals = [2,2,2];
740 model = p_part_model(model,params);
743 model_data = gen_model_data(model);
744 overall_generation_time = tic;
745 detailed_data = gen_detailed_data(model,model_data);
746 t_gen = toc(overall_generation_time); %time necessary for generating the basis
748 file_name = ['save_case_5_',num2str(model.base_model.RB_stop_epsilon),'_',num2str(params.coarse_factor),'_',num2str(model.base_model.RB_numintervals(1)),'.mat'];
749 save(fullfile(rbmatlabtemp,file_name),'model','detailed_data','t_gen');
754 % step 51: plot of p-grid of previous step
755 % ???other plots useful????
757 load(fullfile(rbmatlabtemp,loadfilename));
758 options.simple_grid=1; %if =1 plot is not colored
759 model.random_seed=random_seed;
763 online_data = gen_online_data(model,detailed_data);
764 %model.N = online_data.N; %Unschoen aber nötig...
766 nleafs = get(detailed_data.pgrid, 'nleafelements');
767 leafcogs = get(detailed_data.pgrid, 'leafcogs');
768 %reduced_timer = tic;
770 % model=set_mu(model,leafcogs(nl,:));
771 % rb_sim_data = rb_simulation(model,online_data);
772 %rb_sim_data = rb_reconstruction(model,detailed_data,rb_sim_data);
774 %t_red=toc(reduced_timer)/nleafs;
778 %end debug ...entfernen sobald aktuelle simulationen vorhanden
780 [test_error,test_error_max,av_rb_sim_time] = calculate_test_error(model, online_data);
782 disp(['average time for reduced simulation: ',num2str(av_rb_sim_time)]);
784 disp('Results of basis generation with fixed p_partition:');
785 disp(['Test Error (estimated error): ',num2str(test_error)]);
786 disp(['Maximal Test Error (estimated error):',num2str(test_error_max)]);
787 disp(['Time for generation of detailed_data: ',num2str(t_gen)]);
788 disp(['Epsilon criteria to stop was: ',num2str(model.base_model.RB_stop_epsilon)]);
789 disp(['Nmax was: ',num2str(model.base_model.RB_stop_Nmax)]);
790 disp(['Number of partitions: ',num2str(nleafs)]);
791 for p=1:length(detailed_data.parts_detailed_data)
792 disp(['Partition ',num2str(p),' :']);
793 disp(['Number of basis vectors in part ',num2str(p),' : ',num2str(length(detailed_data.parts_detailed_data{p}.RB_info.mu_sequence))]);
794 if(detailed_data.parts_detailed_data{p}.RB_info.stopped_on_epsilon)
795 disp(
'Stopped basis generation due to reached epsilon criteria');
797 if(detailed_data.parts_detailed_data{p}.RB_info.stopped_on_timeout)
798 disp(
'Stopped basis generation due to reached time limit');
800 if(detailed_data.parts_detailed_data{p}.RB_info.stopped_on_Nmax)
801 disp(
'Stopped basis generation due to reached maximum number of basis vectors ');
805 if strcmp(mu_dimensions,
'2D-case')
806 disp(['vy_weight was: ',num2str(model.base_model.base_model.vy_weight)]);
809 %Write results to a protocol file
811 settings.setting = 'With 2X2 fixed p-partitioning';
812 settings.file_name = loadfilename;
813 settings.Greedy_intervals = model.base_model.RB_numintervals(1);
814 settings.stop_on_epsilon = model.base_model.RB_stop_epsilon;
815 settings.stop_on_N_max = model.base_model.RB_stop_Nmax;
816 if strcmp(mu_dimensions,'2D-case')
817 settings.vy_weight = model.base_model.base_model.vy_weight;
820 results.average_test_error = test_error;
821 results.maximal_test_error = test_error_max;
822 results.time_for_reduced_simulation = av_rb_sim_time;
823 results.time_for_generation = t_gen;
824 for p=1:length(detailed_data.parts_detailed_data)
825 results.(['Number_of_basisvectors_used_in',num2str(p)]) = length(detailed_data.parts_detailed_data{p}.RB_info.mu_sequence);
828 animate_basisgen(detailed_data,model,options);
830 save_result_protocol(
'p_part_test protokoll',settings, results);
833 % step 6: Basisgeneration
using an adaptive p-partition given an N_max of
834 % basis vectors per partition and a fixed-greedy algorithm using
835 % the same M-Train grid as in the previous two steps.
836 % detailed_data and the model are saved
839 model = fast_model(params);
840 model.save_time_indices = 0:model.nt;
842 model.get_rb_from_detailed_data = @(detailed_data) detailed_data.V; %only improvised help-method
for method
rb_test_indicator.m
847 model.RB_numintervals = [2,2];
850 model.RB_numintervals = [2,2,2];
853 %
set mu in model and base_model!!!
854 model = model.set_mu(model,mu);
857 model.RB_generation_mode =
'greedy_uniform_fixed';
858 model.RB_error_indicator =
'estimator';
860 model.RB_stop_epsilon = 1e-4;
861 model.RB_stop_timeout = 24*60*60; % .
862 model.RB_stop_Nmax = 80; %maximal number of RB-vectors per p_partition
863 model.p_part_max_refinement_level=3; %%Maximale refinement level
for p_partitions
864 model.filecache_ignore_fields_in_model=[model.filecache_ignore_fields_in_model,'mu_ranges']; %
for making possible to read data from cache in p-partitions
865 % convert model into a p_part model
866 params.p_part_generation_mode = 'adaptive';
869 params.p_part_numintervals =[1,1];
871 params.p_part_numintervals = [1,1,1];
873 model = p_part_model(model,params);
875 model_data = gen_model_data(model);
876 overall_generation_time = tic;
877 detailed_data = gen_detailed_data(model,model_data);
879 t_gen = toc(overall_generation_time); %time necessary
for generating the basis
881 file_name = ['save_case_6_',num2str(model.base_model.RB_stop_epsilon),'_',num2str(model.base_model.RB_stop_Nmax),'_',num2str(params.coarse_factor),'_',num2str(model.base_model.RB_numintervals(1)),'_',num2str(model.base_model.p_part_max_refinement_level),'.mat'];
882 save(fullfile(rbmatlabtemp,file_name),
'model',
'detailed_data',
't_gen');
885 % step 61: plot of the p-grid... other plots???
888 load(fullfile(rbmatlabtemp,loadfilename));
889 options.simple_grid=1; %
if =1 plot is not colored
890 model.random_seed = random_seed;
895 online_data = gen_online_data(model,detailed_data);
896 %model.N = online_data.N; %Unschoen aber nötig...
898 nleafs =
get(detailed_data.pgrid,
'nleafelements');
899 leafcogs =
get(detailed_data.pgrid,
'leafcogs');
902 % model=set_mu(model,leafcogs(nl,:));
903 % rb_sim_data = rb_simulation(model,online_data);
904 %rb_sim_data = rb_reconstruction(model,detailed_data,rb_sim_data);
906 %t_red=toc(reduced_timer)/nleafs;
908 [test_error,test_error_max,av_rb_sim_time] = calculate_test_error(model, online_data);
910 disp([
'average time for reduced simulation: ',num2str(av_rb_sim_time)]);
912 disp(
'Results of basis generation with adaptive p_partition:');
913 disp([
'Test Error (estimated error): ',num2str(test_error)]);
914 disp([
'Maximal Test Error (estimated error):',num2str(test_error_max)]);
915 disp([
'Time for generation of detailed_data: ',num2str(t_gen)]);
916 disp([
'Epsilon criteria to stop was: ',num2str(model.base_model.RB_stop_epsilon)]);
917 disp([
'Nmax was: ',num2str(model.base_model.RB_stop_Nmax)]);
918 disp([
'Number of partitions: ',num2str(nleafs)]);
919 for p=1:length(detailed_data.parts_detailed_data)
920 disp(['Partition ',num2str(p),' :']);
921 disp(['Number of basis vectors in part ',num2str(p),' : ',num2str(length(detailed_data.parts_detailed_data{p}.RB_info.mu_sequence))]);
922 if(detailed_data.parts_detailed_data{p}.RB_info.stopped_on_epsilon)
923 disp(
'Stopped basis generation due to reached epsilon criteria');
925 if(detailed_data.parts_detailed_data{p}.RB_info.stopped_on_timeout)
926 disp(
'Stopped basis generation due to reached time limit');
928 if(detailed_data.parts_detailed_data{p}.RB_info.stopped_on_Nmax)
929 disp(
'Stopped basis generation due to reached maximum number of basis vectors ');
933 if strcmp(mu_dimensions,
'2D-case')
934 disp(['vy_weight was: ',num2str(model.base_model.base_model.vy_weight)]);
937 %Write results to a protocol file
939 settings.setting = 'With adaptive p-partitioning';
940 settings.file_name = loadfilename;
941 settings.Greedy_intervals = model.base_model.RB_numintervals(1);
942 settings.stop_on_epsilon = model.base_model.RB_stop_epsilon;
943 settings.stop_on_N_max = model.base_model.RB_stop_Nmax;
944 if strcmp(mu_dimensions,'2D-case')
945 settings.vy_weight = model.base_model.base_model.vy_weight;
948 results.average_test_error = test_error;
949 results.maximal_test_error = test_error_max;
950 results.time_for_reduced_simulation = av_rb_sim_time;
951 results.time_for_generation = t_gen;
952 results.Number_of_partitions = nleafs;
953 for p=1:length(detailed_data.parts_detailed_data)
954 results.(['Number_of_basisvectors_used_in',num2str(p)]) = length(detailed_data.parts_detailed_data{p}.RB_info.mu_sequence);
957 animate_basisgen(detailed_data,model,options);
959 save_result_protocol(
'p_part_test protokoll',settings, results);
963 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
964 % step = 7 % time measurements
965 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
970 t_detailed = zeros(1,nreps);
972 % initialize detailed model and simulate
974 load(fullfile(rbmatlabtemp,detailed_data_fname),...
975 'model',
'detailed_data')
978 % lin_evol_model = advection_fv_output_vconst_model(params);
979 %lin_evol_model.debug = 1;
980 %lin_evol_model_data = gen_model_data(lin_evol_model);
982 % set some additional fields in model which will be copied into
983 % ds model depending on whether constants are known or not:
984 % estimate_constants = 0;
985 % if estimate_constants
986 % %%% if constant estimaton has to be performed:
987 % lin_evol_model.estimate_lin_ds_nmu = 4;
988 % lin_evol_model.estimate_lin_ds_nX = 10;
989 % lin_evol_model.estimate_lin_ds_nt = 4;
992 % % the following values are rough bounds, so could be
993 % % non-rigorous, then larger search must be performed.
994 % lin_evol_model.state_bound_constant_C1 = 1;
995 % lin_evol_model.output_bound_constant_C2 = 1.2916;
998 % model = lin_ds_from_lin_evol_model(lin_evol_model);
999 % % set accelerated data functions from below
1000 % model.A_function_ptr = @(model,model_data) ...
1001 % eval_affine_decomp(@A_components,...
1002 % @A_coefficients,...
1003 % model,model_data);
1005 % model.B_function_ptr = @(model,model_data) ...
1006 % eval_affine_decomp(@B_components,...
1007 % @B_coefficients,...
1008 % model,model_data);
1009 % %model.u_function_ptr = @(params) 0.1; % define new
1010 model_data = gen_model_data(model);
1012 disp('skipping detailed simulations...')
1016 disp(['repetition = ',num2str(r)])
1017 mu = [1,1,0.5]-rand(1,3)*0.01;
1018 model = model.set_mu(model,mu);
1020 ds_sim_data = detailed_simulation(model,model_data);
1021 t_detailed(1,r) = toc;
1026 disp('--------------------------------------------------')
1027 disp('detailed simulation')
1029 disp(['mean t_detailed=',num2str(mean(t_detailed))]);
1030 disp('--------------------------------------------------')
1032 % reduced simulation with and without error estimation
1034 clear('ds_sim_data');
1035 model.RB_generation_mode = 'file_load';
1036 model.RB_basis_filename = ...
1037 fullfile(rbmatlabtemp,rb_fname);
1038 detailed_data = gen_detailed_data(model,model_data);
1039 online_data = gen_online_data(model,detailed_data);
1041 Ns = [150:-10:10]; %,30,20,10];
1042 Ns = Ns(find(online_data.N>=Ns));
1044 t_reduced_with_err_est = zeros(length(Ns),nreps);
1045 t_reduced_without_err_est = zeros(length(Ns),nreps);
1047 % reduced simulation with and without error estimation
1050 model.error_estimation = err_est;
1051 disp(['err_est= ',num2str(err_est)]);
1052 for n_ind = 1:length(Ns)
1053 disp(['M = ',num2str(Ns(n_ind))]);
1054 model.N = Ns(n_ind);
1056 disp(['repetition = ',num2str(r)])
1057 mu = [1,1,1]-rand(1,3)*0.01;
1058 model = model.set_mu(model,mu);
1060 rb_sim_data = rb_simulation(model,online_data);
1061 if size(rb_sim_data.Xr,1)~=Ns(n_ind)
1062 disp('N dimension not set correctly!');
1066 t_reduced_with_err_est(n_ind,r) = toc;
1068 t_reduced_without_err_est(n_ind,r) = toc;
1074 % output measurements
1076 disp('--------------------------------------------------')
1077 disp('detailed simulation')
1079 disp(['mean t_detailed=',num2str(mean(t_detailed))]);
1081 disp('--------------------------------------------------')
1082 disp('reduced simulation with error estimation')
1083 disp(t_reduced_with_err_est);
1084 disp(['mean t_reduced_with_err_est=',...
1085 num2str(mean(t_reduced_with_err_est,2)')]);
1086 disp(['Ns =',num2str(Ns)]);
1088 disp('--------------------------------------------------')
1089 disp('reduced simulation without error estimation')
1090 disp(t_reduced_without_err_est);
1091 disp(['mean t_reduced_without_err_est=',...
1092 num2str(mean(t_reduced_without_err_est,2)')]);
1093 disp(['Ns =',num2str(Ns)]);
1094 disp('--------------------------------------------------')
1096 save(fullfile(rbmatlabtemp,'time_measurements'));
1098 % dim_x = 65536, nt = 2048
1100 %mean t_detailed=64.3806
1101 %--------------------------------------------------
1102 %reduced simulation with error estimation
1103 % 3.4542 3.5969 3.5659 3.4216 3.6170
1104 % 3.3648 3.3816 3.4218 3.4391 3.4552
1105 % 3.1643 3.1312 3.1244 3.1433 3.1720
1106 % 3.0594 3.0496 3.0628 3.0486 3.0387
1107 % 2.9993 2.9953 3.0078 2.9413 2.9603
1108 % 2.9555 2.9093 2.9091 2.9510 2.9256%%%
1110 %mean t_reduced_with_err_est=3.5311 3.4125 3.147 3.0518 2.9808 2.9301
1111 %Ns =60 50 40 30 20 10
1112 %--------------------------------------------------
1113 %reduced simulation without error estimation
1114 % 2.3975 2.4220 2.2924 2.4490 2.3302
1115 % 2.3322 2.3198 2.3425 2.3056 2.3512
1116 % 2.2227 2.2734 2.2210 2.2582 2.2504
1117 % 2.2531 2.2180 2.2429 2.2212 2.2612
1118 % 2.2283 2.1979 2.2581 2.2241 2.1957
1119 % 2.2274 2.2014 2.2577 2.1856 2.2268
1121 %mean t_reduced_without_err_est=2.3782 2.3303 2.2451 2.2393 2.2208 2.2198
1122 %Ns =60 50 40 30 20 10
1123 %--------------------------------------------------
1125 case 8 % compare original and accelerated detailed model
1127 disp('compare original and accelerated lin_ds models');
1130 % initialize detailed model and simulate
1131 lin_evol_model = advection_fv_output_vconst_model(params);
1132 estimate_constants = 0;
1133 if estimate_constants
1134 %%% if constant estimaton has to be performed:
1135 lin_evol_model.estimate_lin_ds_nmu = 4;
1136 lin_evol_model.estimate_lin_ds_nX = 10;
1137 lin_evol_model.estimate_lin_ds_nt = 4;
1140 % the following values are rough bounds, so could be
1141 % non-rigorous, then larger search must be performed.
1142 lin_evol_model.state_bound_constant_C1 = 1;
1143 lin_evol_model.output_bound_constant_C2 = 1.2916;
1146 model = lin_ds_from_lin_evol_model(lin_evol_model);
1147 %model.u_function_ptr = @(params) 0.1; % define new
1148 model_data = gen_model_data(model);
1150 % set accelerated data functions
1151 model_fast = lin_ds_from_lin_evol_model(lin_evol_model);
1152 model_fast.A_function_ptr = @(model,model_data) ...
1153 eval_affine_decomp(@A_components,...
1157 model_fast.B_function_ptr = @(model,model_data) ...
1158 eval_affine_decomp(@B_components,...
1162 % model_fast.C_function_ptr = @(model,model_data) ...
1163 % eval_affine_decomp(@C_components,...
1164 % @C_coefficients,...
1165 % model,model_data);
1167 % disp('skipping detailed simulations...')
1170 mu = [1,1,0.5]-rand(1,3)*0.01;
1171 model = model.set_mu(model,mu);
1172 model_fast = model_fast.set_mu(model_fast,mu);
1174 ds_sim_data = detailed_simulation(model,model_data);
1177 ds_sim_data_fast = detailed_simulation(model_fast,model_data);
1178 t_detailed_fast = toc;
1180 disp(['original t = ',num2str(t_detailed),...
1181 ', accelerated t = ',num2str(t_detailed_fast)]);
1182 if ~isequal(ds_sim_data,ds_sim_data_fast)
1183 disp('simulation results of original and accelerated model differ!')
1185 disp('simulation results of original and accelerated model equal!')
1190 case 9 % generate basis from POD-Greedy
1193 model = fast_model(params);
1194 model.cone_range = [0.4,0.6];
1195 model.base_model.cone_range = model.cone_range;
1197 % shrink range to 'single point'
1198 % r = [0.4,0.6]; % => 2 -> 0.8 error nach 10 basisvektoren
1199 % r = [0.0,1.0]; % => 4.6 -> 1.9 error nach 10 basisvektoren
1200 % r = [0.45,0.55]; % => 2.38 -> 0.51 error nach 10 basisvektoren
1201 model.mu_ranges = {[0,1],[0.4,0.6],[0.4,0.6]};
1202 % model.rb_basis_generation_ptr = ...
1203 % @basisgen_POD_greedy_uniform_fixed;
1205 % model.RB_generation_mode =
'uniform_fixed';
1206 % model.RB_num_intervals = [2,2,2];
1208 model_data = gen_model_data(model);
1210 disp(
'generating basis from POD-Greedy');
1212 %
for basis generation,
set further fields:
1214 model.verbose = 5; % => only dots in greedy loop
1215 model.RB_generation_mode =
'greedy_uniform_fixed';
1216 model.RB_numintervals = [1,1,1]; % => 0.5 in cone_pos obtained
1217 % model.RB_numintervals = [4,4,4];
1218 model.RB_stop_epsilon = 1e-5;
1219 model.RB_stop_timeout = 3600;
1220 model.RB_stop_Nmax =150;
1221 % model.RB_stop_timeout = 600;
1222 % model.RB_stop_Nmax =10;
1224 model.RB_error_indicator =
'estimator';
1226 % model.get_estimator_from_sim_data = @(sim_data) sim_data.DeltaX(end);
1228 detailed_data = gen_detailed_data(model,model_data);
1231 dfname = fullfile(rbmatlabtemp,[detailed_data_fname,
'_greedy']);
1232 save(dfname,
'model',
'detailed_data');
1234 disp(
'successfully generated and saved detailed_data');
1236 params.plot_title =
'reduced basis';
1237 params.plot = model.base_model.plot;
1238 lin_evol_model_data = gen_model_data(model.base_model);
1239 plot_sequence(detailed_data.V,lin_evol_model_data.grid,params)
1243 case 10 % reduced simulations and error plots
1245 %basis_from_step = 9; % step 9
1246 basis_from_step = 4; % step 4
1248 if basis_from_step == 9
1249 dfname = fullfile(rbmatlabtemp,[detailed_data_fname,'_greedy']);
1251 disp('loaded basis from step 9')
1253 load(fullfile(rbmatlabtemp,detailed_data_fname),...
1254 'model','detailed_data')
1256 % model = fast_model(params);
1257 model_data = gen_model_data(model);
1258 % dfname = fullfile(rbmatlabtemp,'advection_fv_output_detailed_data_146');
1259 % model.RB_basis_filename = ...
1260 % fullfile(rbmatlabtemp,'advection_fv_output_basis');
1261 % model.RB_generation_mode = 'file_load';
1262 % detailed_data = gen_detailed_data(model,model_data);
1263 % dfname = fullfile(rbmatlabtemp,'advection_fv_output_detailed');
1265 tmp = load(fullfile(rbmatlabtemp,[trajectory_fnbase,'2']));
1266 ds_sim_data = tmp.ds_sim_data;
1267 mu_from_file = get_mu(tmp.model);
1268 disp('loaded basis from step 4')
1269 model = model.set_mu(model,mu_from_file);
1271 % trajectory2 above is made with this
1272 % model = model.set_mu(model,[1,1,1]);
1273 % model = model.set_mu(model,[1,1,1]);
1274 % trajectory3 above is made with this:
1275 %model = model.set_mu(model,[1,1,0.5]);
1276 %model = model.set_mu(model,[0.5,0.6,0.6]);
1277 %model = model.set_mu(model,[0.5,0.4,0.4]);
1278 %model = model.set_mu(model,[0.5,0.5,0.5]);
1279 model.N = size(detailed_data.V,2);
1281 online_data = gen_online_data(model,detailed_data);
1282 rb_sim_data = rb_simulation(model,online_data);
1284 params.no_lines = 1;
1285 params.show_colorbar = 1;
1286 params.axis_equal = 1;
1288 % plot of reduced output
1290 p = plot(rb_sim_data.time,...
1292 rb_sim_data.Y+rb_sim_data.DeltaY; ...
1293 rb_sim_data.Y-rb_sim_data.DeltaY]);
1294 colors = {[1,0,0],[1,0,0],[1,0,0],[0,1,0]};
1295 linestyles = {
'-',
'-.',
'-.',
'-'};
1298 set(p(i),'Color',colors{i},
'Linestyle',linestyles{i},...
1299 'Linewidth',widths(i));
1301 %
set marker on rb_sim_data:
1302 Xdata =
get(p(1),
'XData');
1303 Ydata =
get(p(1),
'YData');
1304 Zdata =
get(p(1),
'ZData');
1305 set(p(1),
'XData',Xdata(1:4:end));
1306 set(p(1),
'YData',Ydata(1:4:end));
1307 set(p(1),
'ZData',Zdata(1:4:end));
1308 set(p(1),
'Marker',
'o');
1310 legend([
'y\^(t): reduced output'],...
1311 'y\^(t)+\Delta_y(t)',...
1312 'y\^(t)-\Delta_y(t)',
'Location',
'NorthWest');
1313 title([
'k = ',num2str(model.N)],
'Fontsize',15);
1315 % table of error, error estimator and effectivity
1317 online_data = gen_online_data(model,detailed_data);
1320 Ns = Ns(find(online_data.N>=Ns));
1322 err = zeros(1,length(Ns));
1323 err_estim = zeros(1,length(Ns));
1324 effectivity = zeros(1,length(Ns));
1326 for ni = 1:length(Ns)
1328 rb_sim_data = rb_simulation(model, online_data);
1329 err(ni) = abs(ds_sim_data.Y(end) - rb_sim_data.Y(end));
1330 err_estim(ni) = rb_sim_data.DeltaY(end);
1331 effectivity(ni) = err_estim(ni)/err(ni);
1337 disp('finished computing effectivities')
1340 case 11 % test of matlab ode solver for resulting sparse system
1342 params.coarse_factor = 8; % nake model smaller
1343 model = fast_model(params);
1344 model.RB_generation_mode = 'from_detailed_data';
1346 % detailed simulation:
1347 model_data = gen_model_data(model);
1348 model_data.RB = zeros(size(model_data.G,1),0);
1349 detailed_data = gen_detailed_data(model,model_data);
1351 model.decomp_mode = 0; % complete
1352 model = model.set_time(model,0)
1353 model = model.set_mu(model,[0.5,1,1]);
1355 A = @(model,model_data,t) ...
1356 model.A_function_ptr(model.set_time(model,t),model_data);
1358 B = @(model,model_data,t) ...
1359 model.B_function_ptr(model.set_time(model,t),model_data);
1361 x0 = model.x0_function_ptr(model,model_data);
1363 [t,x] = ode15s(@(t,x) A(model,model_data,t)*x + B(model,model_data,t), ...
1366 disp(['ode15 takes ',num2str(time_ode15),' sec']);
1368 params.plot_title = 'ode15 solution';
1369 params.plot = model.base_model.plot;
1370 params.axis_equal = 1;
1371 lin_evol_model_data = gen_model_data(model.base_model);
1375 sim_data = detailed_simulation(model,model_data);
1376 time_rbmatlab = toc;
1377 disp(['rbmatlab takes ',num2str(time_rbmatlab),' sec']);
1378 params.plot_title = 'rbmatlab solution';
1379 params.plot = model.base_model.plot;
1380 params.axis_equal = 1;
1381 lin_evol_model_data = gen_model_data(model.base_model);
1384 % further plots for MCMCS paper
1387 load(fullfile(rbmatlabtemp,detailed_data_fname),...
1388 'model','detailed_data')
1390 % plot of error estimator over parameter variation
1391 % model = fast_model(params);
1392 % model_data = gen_model_data(model);
1393 % dfname = fullfile(rbmatlabtemp,'advection_fv_output_detailed_data_146');
1394 % model.RB_basis_filename = ...
1395 % fullfile(rbmatlabtemp,'advection_fv_output_basis');
1396 % model.RB_generation_mode = 'file_load';
1397 % detailed_data = gen_detailed_data(model,model_data);
1398 % save(fullfile(rbmatlabtemp,...
1399 % 'advection_fv_output_detailed_2trajectories.mat'),...
1400 % 'model','detailed_data')
1402 % dfname = fullfile(rbmatlabtemp,'advection_fv_output_detailed');
1404 tmp = load(fullfile(rbmatlabtemp,[trajectory_fnbase,'2']));
1405 ds_sim_data = tmp.ds_sim_data;
1406 disp('loaded basis from step 4')
1408 % factors = 0:0.05:1;
1409 online_data = gen_online_data(model,detailed_data);
1410 Ns = [1,8,16,32,64];
1411 Ns = Ns(find(online_data.N>=Ns));
1413 err_estim = zeros(length(factors),length(Ns));
1414 for ni = 1:length(Ns)
1416 for i =1:length(factors)
1417 model = model.set_mu(model, factors(i)*[1,1,1]);
1418 rb_sim_data = rb_simulation(model, online_data);
1419 % err(ni) = abs(ds_sim_data.Y(end) - rb_sim_data.Y(end));
1420 err_estim(i,ni) = rb_sim_data.DeltaY(end);
1421 % effectivity(ni) = err_estim(ni)/err(ni);
1425 p = plot(factors,err_estim');
1426 colors = {[1,0,0],[0,0.6,0],[0,0,1],[0,0,0]};
1427 linestyles = {
'-',
'-.',
'-',
'-.'};
1429 set(p(i),
'Linewidth',2,
'Color',colors{i},
'Linestyle',linestyles{i});
1432 xlabel(
'parameter \mu_1=\mu_2=\mu_3');
1433 ylabel(
'\Delta_y(\mu)');
1434 title(
'error estimator from parameter sweep')
1435 legend({
'k=1',
'k=8',
'k=16',
'k=32',
'k=64'},
'Location',
'NorthWest');
1436 saveas(gcf,fullfile(rbmatlabtemp,
'parameter_sweep.eps'),
'epsc');
1439 disp(
'step number unknown');
1444 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1446 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1447 % generation of model from lin_evol_model and replace
1449 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1451 function model = fast_model(params);
1452 lin_evol_model = advection_fv_output_vconst_model(params);
1453 lin_evol_model.vy_weight = 0.5;
1454 switch params.mu_dimensions
1456 lin_evol_model.mu_names = {
'cone_amplitude',
'vx_weight'};
1457 lin_evol_model.mu_ranges = {[0,1],[0,1]};
1459 lin_evol_model.mu_names = {
'cone_amplitude',
'vx_weight',
'vy_weight'};
1460 lin_evol_model.mu_ranges = {[0,1],[0,1],[0,1]};
1462 %lin_evol_model.debug = 1;
1463 %lin_evol_model_data = gen_model_data(lin_evol_model);
1465 %
set some additional fields in model which will be copied into
1466 % ds model depending on whether constants are known or not:
1467 estimate_constants = 0;
1468 if estimate_constants
1469 %%%
if constant estimaton has to be performed:
1470 lin_evol_model.estimate_lin_ds_nmu = 4;
1471 lin_evol_model.estimate_lin_ds_nX = 10;
1472 lin_evol_model.estimate_lin_ds_nt = 4;
1475 % the following values are rough bounds, so could be
1476 % non-rigorous, then larger search must be performed.
1477 lin_evol_model.state_bound_constant_C1 = 1;
1478 lin_evol_model.output_bound_constant_C2 = 1.2916;
1481 model = lin_ds_from_lin_evol_model(lin_evol_model);
1482 %
set accelerated data functions from below
1483 model.A_function_ptr = @(model,model_data) ...
1484 eval_affine_decomp(@A_components,...
1488 model.B_function_ptr = @(model,model_data) ...
1489 eval_affine_decomp(@B_components,...
1492 %model.u_function_ptr = @(params) 0.1; % define
new
1495 %disp(
'set theta to 1!!!');
1502 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1503 % auxialiary coefficient functions
for acceleration of
1504 % advection model:
explicit implementation of coefficients
1505 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1507 function Acomp = A_components(model,model_data)
1508 Acomp = model_data.A_components;
1511 function Acoeff = A_coefficients(model);
1512 %model.base_model.decomp_mode = 2;
1513 %model.base_model.t = model.t;
1514 %mu = get_mu(model);
1515 %model.base_model = model.set_mu(model.base_model,mu);
1516 %[L_I_coeff, L_E_coeff, b_coeff] = ...
1517 % model.base_model.operators_ptr(model.base_model, );
1518 %% L_E = Id + Delta t * A
1519 %Acoeff = L_E_coeff(2:end)/model.base_model.dt;
1521 %Acoeff = -model.base_model.dt * ...
1522 % model.base_model.velocity_coefficients_ptr(model.base_model);
1524 Acoeff = - [model.vx_weight, model.base_model.vy_weight]*0.5; %*(1-model.t);
1529 function Bcomp = B_components(model,model_data)
1530 Bcomp = model_data.B_components;
1533 function Bcoeff = B_coefficients(model)
1534 % hmmm the coefficients are now called twice in A and B
1535 % should somehow be cached later...
1536 %model.base_model.decomp_mode = 2;
1537 %model.base_model.t = model.t;
1538 %mu = get_mu(model);
1539 %model.base_model = model.set_mu(model.base_model,mu);
1540 %[L_I_coeff, L_E_coeff, b_coeff] = ...
1541 % model.base_model.operators_ptr(model.base_model, );
1542 %% L_E = Id + Delta t * A
1543 %Bcoeff2 = b_coeff/model.base_model.dt;
1544 vcoeff = - [model.vx_weight, model.base_model.vy_weight]*0.5;%*(1-model.t);
1545 Q_0 = model.base_model.cone_number;
1546 dcoeff = zeros(1,Q_0);
1547 max_pos = model.base_model.cone_weight * (Q_0-1)+1;
1550 dcoeff(q) = (1-(max_pos-q))*(1-t) * ((max_pos>=q) && (max_pos < q+1)) ...
1551 + (1+(max_pos-q))*(1-t) * ((max_pos>=q-1) && (max_pos < q));
1553 v = -(vcoeff(:)*(dcoeff(:)'));
1554 Bcoeff = v(:)*model.cone_amplitude;
1559 %function Ccomp = C_components(model,model_data)
1560 %Ccomp = model_data.C_components;
1562 %function Ccoeff = C_coefficients(model);
1563 %model.base_model.decomp_mode = 2;
1564 %model.base_model.t = model.t;
1565 %mu = get_mu(model);
1566 %model.base_model = model.set_mu(model.base_model,mu);
1568 % model.base_model.operators_output(model.base_model);
1571 function [epsilon_test,epsilon_max,rb_sim_av_time] = calculate_test_error(model, online_data)
1572 display('Calculating estimated test error');
1573 N_test=500;%amount of Tests
1574 rand('state',model.random_seed);
1576 random_mu = rand_uniform(N_test, model.mu_ranges);
1580 est_error=zeros(1,N_test);
1582 %Choose a mu from range by random choice
1583 mu = random_mu(:,i);
1584 model = set_mu(model,mu);
1585 %perform a reduced simulation and take the estiamated error of last
1588 rb_sim_data = rb_simulation(model, online_data);
1589 rb_sim_time = rb_sim_time + toc(rb_sim_timer);
1590 %add error to total error
1591 est_error(i) = rb_sim_data.DeltaX(end);
1606 %calculate average estimated error
1607 epsilon_test = sum(est_error)/N_test;
1608 epsilon_max=max(est_error);
1609 rb_sim_av_time = rb_sim_time/N_test;
1611 %Visualisation of error map on parameter domain
1613 scatter(random_mu(1,:),random_mu(2,:),40,est_error,'filled');
1617 function save_result_protocol(filename, settings, results)
1619 fid = fopen(fullfile(rbmatlabhome,filename),'a');
1620 fprintf(fid,'----------------------------------------------------------\n');
1621 fprintf(fid,'----------------------------------------------------------\n');
1622 fprintf(fid,'%s\n',settings.setting);
1623 fprintf(fid,'%s\n',settings.file_name);
1624 fprintf(fid,'-----------------------------------------------------------\n');
1626 settingnames=fieldnames(settings);
1627 resultnames=fieldnames(results);
1629 fprintf(fid,'Settings:\n');
1630 for i=1:length(settingnames)
1631 if isscalar(getfield(settings,settingnames{i}))
1632 text=num2str(getfield(settings,settingnames{i}));
1634 text = num2str(getfield(settings,settingnames{i}));
1636 fprintf(fid,
'%s:\t %s\n',settingnames{i},text);
1638 fprintf(fid,
'\nResults:\n');
1639 for i=1:length(resultnames)
1640 if isscalar(getfield(results,resultnames{i}))
1641 text=num2str(getfield(results,resultnames{i}));
1643 text = num2str(getfield(results,resultnames{i}));
1645 fprintf(fid,
'%s:\t %s\n',resultnames{i},text);
1649 saveas(figure(1),fullfile(rbmatlabhome,
'protocol_figures',[settings.file_name,
'_error_map.fig']),
'fig');
1650 saveas(figure(1),fullfile(rbmatlabhome,
'protocol_figures',[settings.file_name,
'_error_map.jpg']),
'jpg');
1651 saveas(figure(1),fullfile(rbmatlabhome,
'protocol_figures',[settings.file_name,
'_error_map.eps']),
'eps');
1654 saveas(figure(2),fullfile(rbmatlabhome,
'protocol_figures',[settings.file_name,
'.fig']),
'fig');
1655 saveas(figure(2),fullfile(rbmatlabhome,
'protocol_figures',[settings.file_name,
'.jpg']),
'jpg');
1656 saveas(figure(2),fullfile(rbmatlabhome,
'protocol_figures',[settings.file_name,
'.eps']),
'eps');