rbmatlab  1.13.10
 All Classes Namespaces Files Functions Variables Groups Pages
adaptive_basisgen_mcmds.m
1 function adaptive_basisgen_mcmds(step)
2 %function adaptive_basisgen_mcmds(step)
3 
4 % small script performing the experiments for the (second half) of
5 % the MCMDS paper. Experiments are based on a simple advection example
6 %
7 % mu_names = {'cone_weight','vx_weight','vy_weight'};
8 % mu_ranges = [0,1]^3
9 %
10 % possible steps:
11 %
12 % step 1: detailed simulation of single trajectory, plot
13 % POD and save reduced basis
14 % reduced simulation and error
15 
16 % Bernard Haasdonk 4.2.2010
17 
18 if nargin < 1
19  step = 1;
20 end;
21 
22 params = [];
23 model = [];
24 %params.coarse_factor = 2; % fact 2 => ndofs = 16384, nt = 1024, 15s detailed
25 %params.coarse_factor = 1; % fact 1 => ndofs = 32768, nt = 2048, 60s detailed
26 params.coarse_factor = 2;
27 %verbose(1);
28 trajectory_fnbase = 'advection_trajectory';
29 detailed_data_fname = 'advection_detailed_data';
30 rb_fname = 'advection_rb';
31 
32 switch step
33 
34  case 1
35  disp('initialization of lin_ds model from lin_evol model');
36  % verbose(1,'initialization of lin_ds model from lin_evol model and ...
37  % plot');
38 
39  ds_model = fast_model(params);
40  ds_model.save_time_indices = 0:ds_model.nt;
41 
42  %ds_model.u_function_ptr = @(params) 0.1; % define new
43 % ds_model.theta = 1;
44  ds_model.theta = 0;
45  ds_model_data = gen_model_data(ds_model);
46 % mu = [0.5,1,1];
47  mu = [0.5,1];
48  % set mu in model and base_model!!!
49  ds_model = ds_model.set_mu(ds_model,mu);
50  ds_sim_data = detailed_simulation(ds_model,ds_model_data);
51 
52  % save trajectory
53  save(fullfile(rbmatlabresult,[trajectory_fnbase,num2str(1)]),...
54  'ds_model','ds_model_data','ds_sim_data');
55 
56  % plot trajectory
57  params.no_lines = 1;
58  params.clim = [0,1];
59  params.plot_title='detailed implicit lin_ds';
60  plot_sim_data(ds_model,ds_model_data,ds_sim_data,params);
61 
62  disp('PCA of snapshots takes a while...');
63  disp('Loading of trajectory...');
64  load(fullfile(rbmatlabresult,[trajectory_fnbase,'1']),...
65  'ds_model','ds_model_data','ds_sim_data');
66  disp('...done.');
67  % smaller epsilon does result in more than 1 vector for mu=0,0,0
68  epsilon = 1e-6;
69  disp('Calling PCA_Fixspace...');
70  RB = PCA_fixspace(ds_sim_data.X,[],ds_model_data.G,150,[],epsilon);
71  disp('...done.');
72  %RB = orthonormalize_qr(ds_sim_data.X,ds_model_data.G);
73  % select correct orthogonal set
74  K = RB' * ds_model_data.G * RB;
75  i = find(abs(diag(K)-1)>1e-2);
76  i = min(i);
77  disp(['found ',num2str(size(RB,2)),' basis vectors after traj. 1']);
78  if isempty(i)
79  i = size(RB,2)+1;
80  end;
81 % keyboard;
82  RB = RB(:,1:(i-1));
83  disp(['reduced to ',num2str(size(RB,2)),' basis vectors.']);
84 % keyboard;
85 
86  V = RB;
87  W = ds_model_data.G * V;
88  save(fullfile(rbmatlabresult,rb_fname),'RB');
89 
90  ds_model.RB_basis_filename = fullfile(rbmatlabresult,rb_fname);
91  ds_model.RB_generation_mode = 'file_load';
92 
93  detailed_data = gen_detailed_data(ds_model,ds_model_data);
94 
95  model = ds_model;
96  save(fullfile(rbmatlabresult,detailed_data_fname),...
97  'model','detailed_data')
98 
99  % compare detailed and reduced simulation
100  load(fullfile(rbmatlabresult,detailed_data_fname),...
101  'model','detailed_data')
102  load(fullfile(rbmatlabresult,[trajectory_fnbase,'1']),...
103  'ds_sim_data');
104 
105  ds_model = model;
106 
107  %perform single reduced simulation for given parameter
108  reduced_data = gen_reduced_data(ds_model,detailed_data);
109  ds_model.N = reduced_data.N;
110  tic,
111 % ds_model.nt = ds_model.nt/4;
112 % disp('set nt to quarter!!');
113  rb_sim_data = rb_simulation(ds_model,reduced_data);
114  t_red = toc;
115  rb_sim_data = rb_reconstruction(ds_model,detailed_data,rb_sim_data)
116  disp(['time for reduced simulation: ',num2str(t_red)]);
117 
118  % plot of reduced trajectory
119  lin_evol_model = ds_model.base_model;
120  lin_evol_model_data = gen_model_data(lin_evol_model);
121 
122  params.no_lines = 1;
123  params.show_colorbar = 1;
124  params.axis_equal = 1;
125  params.plot = lin_evol_model.plot;
126  % plot single snapshot
127  %lin_evol_model.plot(rb_sim_data.X(:,1),lin_evol_model_data.grid, ...
128  % params);
129 
130  % plot sequence:
131  params.title = 'reduced solution';
132  plot_sequence(rb_sim_data.X,lin_evol_model_data.grid,params)
133 
134  % plot sequence:
135  params.title = 'error';
136  plot_sequence((rb_sim_data.X-ds_sim_data.X),...
137  lin_evol_model_data.grid,params)
138 
139  % plot of reduced output
140  figure,plot(rb_sim_data.time,[rb_sim_data.Y;ds_sim_data.Y]);
141  legend('reduced output','detailed output');
142 
143  case 2
144 
145  % generate more intelligent basis
146 
147  disp('initialization of lin_ds model from lin_evol model');
148  % verbose(1,'initialization of lin_ds model from lin_evol model and ...
149  % plot');
150 
151  model = fast_model(params);
152  model.save_time_indices = 0:model.nt;
153  model.theta = 0;
154 
155 % mu = [0.5,1,1];
156  mu = [0.5,1];
157  % set mu in model and base_model!!!
158  model = model.set_mu(model,mu);
159  model.RB_stop_Nmax = 150;
160 
161  %%%% basis generation: POD of single trajectory:
162  model.RB_generation_mode = 'PCA_trajectory';
163 
164  %%% basis generation. POD of several trajectories
165  %model.RB_generation_mode = 'PCA_trajectories';
166  %model.RB_mu_list = {[0,0,0],[1,1,1]};
167 
168  %%% basis generation: greedy_random_fixed
169  %model.RB_generation_mode = 'greedy_random_fixed';
170  %model.RB_train_rand_seed = 12356;
171  %model.RB_train_size = 5;
172  %model.RB_error_indicator = 'estimator';
173  %model.RB_stop_epsilon = 1e-2;
174  %model.RB_stop_timeout = 1*60; % 1 minute
175  %model.RB_stop_Nmax = 5;
176  %model.RB_extension_algorithm = @RB_extension_PCA_fixspace;
177 
178  %%% basis generation: greedy_uniform_fixed
179  %model.RB_generation_mode = 'greedy_uniform_fixed';
180  %model.RB_numintervals = [1,1,1];
181  %model.RB_error_indicator = 'estimator';
182  %model.RB_extension_algorithm = @RB_extension_PCA_fixspace;
183  %model.RB_stop_epsilon = 1e-2;
184  %model.RB_stop_timeout = 1*60; % 1 minute
185  %model.RB_stop_Nmax = 5;
186 
187  %%% basis generation: greedy_refined with uniform refinement
188  %model.RB_generation_mode = 'greedy_refined';
189  %model.RB_stop_max_val_train_ratio = 5;
190  %model.RB_refinement_mode = 'uniform';
191  %model.RB_val_rand_seed = 543;
192  %model.RB_M_val_size = 10;
193  %model.RB_numintervals = [1,1,1];
194  %model.RB_max_refinement_level = 10;
195  %model.RB_error_indicator = 'estimator';
196  %model.RB_extension_algorithm = @RB_extension_PCA_fixspace;
197  %model.RB_stop_epsilon = 1e-8;
198  %model.RB_stop_timeout = 60*60; % 1 hour
199  %model.RB_stop_max_val_train_ratio = 4;
200  %model.RB_stop_Nmax = 150;
201  %model.mu_ranges = {[0.4,0.6],[0.4,0.6],[0.4,0.6]};
202 
203  %%% basis generation: greedy_refined with local refinement
204  %% RB_detailed_val_savepath
205  %model.RB_generation_mode = 'greedy_refined';
206  %model.RB_refinement_mode = 'adaptive';
207  %model.RB_stop_max_val_train_ratio = 5;
208  %model.RB_refinement_theta = 0.2;
209  %model.RB_val_rand_seed = 543;
210  %model.RB_M_val_size = 10;
211  %model.RB_numintervals = [1,1,1];
212  %model.RB_max_refinement_level = 10;
213  %model.RB_error_indicator = 'estimator';
214  %model.RB_extension_algorithm = @RB_extension_PCA_fixspace;
215  %model.RB_stop_epsilon = 1e-2;
216  %model.RB_stop_timeout = 1*60; % 1 minute
217  %model.RB_stop_Nmax = 5;
218 
219  % convert model into a p_part model
220  %params.p_part_generation_mode = 'uniform';
221  %params.p_part_numintervals = [2,1,1];
222  %model = p_part_model(model,params);
223 
224  % convert model into a p_part model
225  %params.p_part_generation_mode = 'adaptive';
226  %params.p_part_numintervals = [2,1,1];
227  %model = p_part_model(model,params);
228 
229  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
230 
231  model_data = gen_model_data(model);
232  detailed_data = gen_detailed_data(model,model_data);
233 
234 
235  model = model;
236  save(fullfile(rbmatlabresult,detailed_data_fname),...
237  'model','detailed_data')
238 
239  % compare detailed and reduced simulation
240  load(fullfile(rbmatlabresult,detailed_data_fname),...
241  'model','detailed_data')
242  load(fullfile(rbmatlabresult,[trajectory_fnbase,'1']),...
243  'ds_sim_data');
244 
245  model = model;
246 
247  %perform single reduced simulation for given parameter
248  reduced_data = gen_reduced_data(model,detailed_data);
249  model.N = reduced_data.N;
250  tic,
251 % model.nt = model.nt/4;
252 % disp('set nt to quarter!!');
253  rb_sim_data = rb_simulation(model,reduced_data);
254  t_red = toc;
255  rb_sim_data = rb_reconstruction(model,detailed_data,rb_sim_data)
256  disp(['time for reduced simulation: ',num2str(t_red)]);
257 
258  % plot of reduced trajectory
259  lin_evol_model = model.base_model;
260  lin_evol_model_data = gen_model_data(lin_evol_model);
261 
262  params.no_lines = 1;
263  params.show_colorbar = 1;
264  params.axis_equal = 1;
265  params.plot = lin_evol_model.plot;
266  % plot single snapshot
267  %lin_evol_model.plot(rb_sim_data.X(:,1),lin_evol_model_data.grid, ...
268  % params);
269 
270  % plot sequence:
271  params.title = 'reduced solution';
272  plot_sequence(rb_sim_data.X,lin_evol_model_data.grid,params)
273 
274  % plot sequence:
275  params.title = 'error';
276  plot_sequence((rb_sim_data.X-ds_sim_data.X),...
277  lin_evol_model_data.grid,params)
278 
279  % plot of reduced output
280  figure,plot(rb_sim_data.time,[rb_sim_data.Y;ds_sim_data.Y]);
281  legend('reduced output','detailed output');
282 
283 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
284 % step = 7 % time measurements
285 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
286  case 7
287 
288 % clear all;
289  nreps = 10;
290  t_detailed = zeros(1,nreps);
291 
292  % initialize detailed model and simulate
293 
294  load(fullfile(rbmatlabresult,detailed_data_fname),...
295  'model','detailed_data')
296  model = model;
297 
298 % lin_evol_model = advection_fv_output_vconst_model(params);
299  %lin_evol_model.debug = 1;
300  %lin_evol_model_data = gen_model_data(lin_evol_model);
301 
302  % set some additional fields in model which will be copied into
303  % ds model depending on whether constants are known or not:
304 % estimate_constants = 0;
305 % if estimate_constants
306 % %%% if constant estimaton has to be performed:
307 % lin_evol_model.estimate_lin_ds_nmu = 4;
308 % lin_evol_model.estimate_lin_ds_nX = 10;
309 % lin_evol_model.estimate_lin_ds_nt = 4;
310 % else
311 % %%% otherwise:
312 % % the following values are rough bounds, so could be
313 % % non-rigorous, then larger search must be performed.
314 % lin_evol_model.state_bound_constant_C1 = 1;
315 % lin_evol_model.output_bound_constant_C2 = 1.2916;
316 % end;
317 %
318 % model = lin_ds_from_lin_evol_model(lin_evol_model);
319 % % set accelerated data functions from below
320 % model.A_function_ptr = @(model,model_data) ...
321 % eval_affine_decomp(@A_components,...
322 % @A_coefficients,...
323 % model,model_data);
324 %
325 % model.B_function_ptr = @(model,model_data) ...
326 % eval_affine_decomp(@B_components,...
327 % @B_coefficients,...
328 % model,model_data);
329 % %model.u_function_ptr = @(params) 0.1; % define new
330  model_data = gen_model_data(model);
331 
332  disp('skipping detailed simulations...')
333 % if 0
334 
335  for r = 1:nreps
336  disp(['repetition = ',num2str(r)])
337  mu = [1,1,0.5]-rand(1,3)*0.01;
338  model = model.set_mu(model,mu);
339  tic,
340  ds_sim_data = detailed_simulation(model,model_data);
341  t_detailed(1,r) = toc;
342  end;
343 
344 % end;
345 
346  disp('--------------------------------------------------')
347  disp('detailed simulation')
348  disp(t_detailed);
349  disp(['mean t_detailed=',num2str(mean(t_detailed))]);
350  disp('--------------------------------------------------')
351 
352  % reduced simulation with and without error estimation
353 
354  clear('ds_sim_data');
355  model.RB_generation_mode = 'file_load';
356  model.RB_basis_filename = ...
357  fullfile(rbmatlabresult,rb_fname);
358  detailed_data = gen_detailed_data(model,model_data);
359  reduced_data = gen_reduced_data(model,detailed_data);
360 
361  Ns = [150:-10:10]; %,30,20,10];
362  Ns = Ns(find(reduced_data.N>=Ns));
363 
364  t_reduced_with_err_est = zeros(length(Ns),nreps);
365  t_reduced_without_err_est = zeros(length(Ns),nreps);
366 
367  % reduced simulation with and without error estimation
368 
369  for err_est = 0:1;
370  model.error_estimation = err_est;
371  disp(['err_est= ',num2str(err_est)]);
372  for n_ind = 1:length(Ns)
373  disp(['M = ',num2str(Ns(n_ind))]);
374  model.N = Ns(n_ind);
375  for r = 1:nreps
376  disp(['repetition = ',num2str(r)])
377  mu = [1,1,1]-rand(1,3)*0.01;
378  model = model.set_mu(model,mu);
379  tic;
380  rb_sim_data = rb_simulation(model,reduced_data);
381  if size(rb_sim_data.Xr,1)~=Ns(n_ind)
382  disp('N dimension not set correctly!');
383  keyboard;
384  end;
385  if err_est == 1
386  t_reduced_with_err_est(n_ind,r) = toc;
387  else
388  t_reduced_without_err_est(n_ind,r) = toc;
389  end;
390  end;
391  end;
392  end;
393 
394  % output measurements
395 
396  disp('--------------------------------------------------')
397  disp('detailed simulation')
398  disp(t_detailed);
399  disp(['mean t_detailed=',num2str(mean(t_detailed))]);
400 
401  disp('--------------------------------------------------')
402  disp('reduced simulation with error estimation')
403  disp(t_reduced_with_err_est);
404  disp(['mean t_reduced_with_err_est=',...
405  num2str(mean(t_reduced_with_err_est,2)')]);
406  disp(['Ns =',num2str(Ns)]);
407 
408  disp('--------------------------------------------------')
409  disp('reduced simulation without error estimation')
410  disp(t_reduced_without_err_est);
411  disp(['mean t_reduced_without_err_est=',...
412  num2str(mean(t_reduced_without_err_est,2)')]);
413  disp(['Ns =',num2str(Ns)]);
414  disp('--------------------------------------------------')
415 
416  save(fullfile(rbmatlabresult,'time_measurements'));
417 
418 % dim_x = 65536, nt = 2048
419 %
420 %mean t_detailed=64.3806
421 %--------------------------------------------------
422 %reduced simulation with error estimation
423 % 3.4542 3.5969 3.5659 3.4216 3.6170
424 % 3.3648 3.3816 3.4218 3.4391 3.4552
425 % 3.1643 3.1312 3.1244 3.1433 3.1720
426 % 3.0594 3.0496 3.0628 3.0486 3.0387
427 % 2.9993 2.9953 3.0078 2.9413 2.9603
428 % 2.9555 2.9093 2.9091 2.9510 2.9256%%%
429 %
430 %mean t_reduced_with_err_est=3.5311 3.4125 3.147 3.0518 2.9808 2.9301
431 %Ns =60 50 40 30 20 10
432 %--------------------------------------------------
433 %reduced simulation without error estimation
434 % 2.3975 2.4220 2.2924 2.4490 2.3302
435 % 2.3322 2.3198 2.3425 2.3056 2.3512
436 % 2.2227 2.2734 2.2210 2.2582 2.2504
437 % 2.2531 2.2180 2.2429 2.2212 2.2612
438 % 2.2283 2.1979 2.2581 2.2241 2.1957
439 % 2.2274 2.2014 2.2577 2.1856 2.2268
440 %
441 %mean t_reduced_without_err_est=2.3782 2.3303 2.2451 2.2393 2.2208 2.2198
442 %Ns =60 50 40 30 20 10
443 %--------------------------------------------------
444 
445  case 8 % compare original and accelerated detailed model
446 
447  disp('compare original and accelerated lin_ds models');
448 
449  % clear all;
450  % initialize detailed model and simulate
451  lin_evol_model = advection_fv_output_vconst_model(params);
452  estimate_constants = 0;
453  if estimate_constants
454  %%% if constant estimaton has to be performed:
455  lin_evol_model.estimate_lin_ds_nmu = 4;
456  lin_evol_model.estimate_lin_ds_nX = 10;
457  lin_evol_model.estimate_lin_ds_nt = 4;
458  else
459  %%% otherwise:
460  % the following values are rough bounds, so could be
461  % non-rigorous, then larger search must be performed.
462  lin_evol_model.state_bound_constant_C1 = 1;
463  lin_evol_model.output_bound_constant_C2 = 1.2916;
464  end;
465 
466  model = lin_ds_from_lin_evol_model(lin_evol_model);
467  %model.u_function_ptr = @(params) 0.1; % define new
468  model_data = gen_model_data(model);
469 
470  % set accelerated data functions
471  model_fast = lin_ds_from_lin_evol_model(lin_evol_model);
472  model_fast.A_function_ptr = @(model,model_data) ...
473  eval_affine_decomp(@A_components,...
474  @A_coefficients,...
475  model,model_data);
476 
477  model_fast.B_function_ptr = @(model,model_data) ...
478  eval_affine_decomp(@B_components,...
479  @B_coefficients,...
480  model,model_data);
481 
482  % model_fast.C_function_ptr = @(model,model_data) ...
483  % eval_affine_decomp(@C_components,...
484  % @C_coefficients,...
485  % model,model_data);
486 
487  % disp('skipping detailed simulations...')
488  % if 0
489 
490  mu = [1,1,0.5]-rand(1,3)*0.01;
491  model = model.set_mu(model,mu);
492  model_fast = model_fast.set_mu(model_fast,mu);
493  tic,
494  ds_sim_data = detailed_simulation(model,model_data);
495  t_detailed = toc;
496  tic,
497  ds_sim_data_fast = detailed_simulation(model_fast,model_data);
498  t_detailed_fast = toc;
499 
500  disp(['original t = ',num2str(t_detailed),...
501  ', accelerated t = ',num2str(t_detailed_fast)]);
502  if ~isequal(ds_sim_data,ds_sim_data_fast)
503  disp('simulation results of original and accelerated model differ!')
504  else
505  disp('simulation results of original and accelerated model equal!')
506  end;
507 
508  keyboard;
509 
510  case 9 % generate basis from POD-Greedy
511 
512  filecache_clear;
513  model = fast_model(params);
514  model.cone_range = [0.4,0.6];
515  model.base_model.cone_range = model.cone_range;
516  % model.
517  % shrink range to 'single point'
518 % r = [0.4,0.6]; % => 2 -> 0.8 error nach 10 basisvektoren
519 % r = [0.0,1.0]; % => 4.6 -> 1.9 error nach 10 basisvektoren
520 % r = [0.45,0.55]; % => 2.38 -> 0.51 error nach 10 basisvektoren
521  model.mu_ranges = {[0,1],[0.4,0.6],[0.4,0.6]};
522  % model.rb_basis_generation_ptr = ...
523  % @basisgen_POD_greedy_uniform_fixed;
524 
525  % model.RB_generation_mode = 'uniform_fixed';
526  % model.RB_num_intervals = [2,2,2];
527 
528  model_data = gen_model_data(model);
529 
530  disp('generating basis from POD-Greedy');
531 
532  % for basis generation, set further fields:
533 
534  model.verbose = 5; % => only dots in greedy loop
535  model.RB_generation_mode = 'greedy_uniform_fixed';
536  model.RB_numintervals = [1,1,1]; % => 0.5 in cone_pos obtained
537 % model.RB_numintervals = [4,4,4];
538  model.RB_stop_epsilon = 1e-5;
539  model.RB_stop_timeout = 3600;
540  model.RB_stop_Nmax =150;
541 % model.RB_stop_timeout = 600;
542 % model.RB_stop_Nmax =10;
543  model.RB_extension_algorithm = @RB_extension_PCA_fixspace;
544  model.RB_error_indicator = 'estimator';
545  % default:
546  % model.get_estimator_from_sim_data = @(sim_data) sim_data.DeltaX(end);
547 
548  detailed_data = gen_detailed_data(model,model_data);
549 
550  model = model;
551  dfname = fullfile(rbmatlabresult,[detailed_data_fname,'_greedy']);
552  save(dfname,'model','detailed_data');
553 
554  disp('successfully generated and saved detailed_data');
555 
556  params.plot_title = 'reduced basis';
557  params.plot = model.base_model.plot;
558  lin_evol_model_data = gen_model_data(model.base_model);
559  plot_sequence(detailed_data.V,lin_evol_model_data.grid,params)
560 
561  keyboard;
562 
563  case 10 % reduced simulations and error plots
564 
565  %basis_from_step = 9; % step 9
566  basis_from_step = 4; % step 4
567 
568  if basis_from_step == 9
569  dfname = fullfile(rbmatlabresult,[detailed_data_fname,'_greedy']);
570  load(dfname);
571  disp('loaded basis from step 9')
572  else
573  load(fullfile(rbmatlabresult,detailed_data_fname),...
574  'model','detailed_data')
575 
576 % model = fast_model(params);
577  model_data = gen_model_data(model);
578  % dfname = fullfile(rbmatlabresult,'advection_fv_output_detailed_data_146');
579 % model.RB_basis_filename = ...
580 % fullfile(rbmatlabresult,'advection_fv_output_basis');
581 % model.RB_generation_mode = 'file_load';
582 % detailed_data = gen_detailed_data(model,model_data);
583  % dfname = fullfile(rbmatlabresult,'advection_fv_output_detailed');
584  % load(dfname);
585  tmp = load(fullfile(rbmatlabresult,[trajectory_fnbase,'2']));
586  ds_sim_data = tmp.ds_sim_data;
587  mu_from_file = get_mu(tmp.model);
588  disp('loaded basis from step 4')
589  model = model.set_mu(model,mu_from_file);
590  end;
591  % trajectory2 above is made with this
592  % model = model.set_mu(model,[1,1,1]);
593  % model = model.set_mu(model,[1,1,1]);
594  % trajectory3 above is made with this:
595  %model = model.set_mu(model,[1,1,0.5]);
596  %model = model.set_mu(model,[0.5,0.6,0.6]);
597  %model = model.set_mu(model,[0.5,0.4,0.4]);
598  %model = model.set_mu(model,[0.5,0.5,0.5]);
599  model.N = size(detailed_data.V,2);
600  %model.N = 150;
601  reduced_data = gen_reduced_data(model,detailed_data);
602  rb_sim_data = rb_simulation(model,reduced_data);
603 
604  params.no_lines = 1;
605  params.show_colorbar = 1;
606  params.axis_equal = 1;
607 
608  % plot of reduced output
609  figure;
610  p = plot(rb_sim_data.time,...
611  [rb_sim_data.Y;...
612  rb_sim_data.Y+rb_sim_data.DeltaY; ...
613  rb_sim_data.Y-rb_sim_data.DeltaY]);
614  colors = {[1,0,0],[1,0,0],[1,0,0],[0,1,0]};
615  linestyles = {'-','-.','-.','-'};
616  widths = [2,2,2,2];
617  for i = 1:length(p)
618  set(p(i),'Color',colors{i},'Linestyle',linestyles{i},...
619  'Linewidth',widths(i));
620  end;
621  % set marker on rb_sim_data:
622  Xdata = get(p(1),'XData');
623  Ydata = get(p(1),'YData');
624  Zdata = get(p(1),'ZData');
625  set(p(1),'XData',Xdata(1:4:end));
626  set(p(1),'YData',Ydata(1:4:end));
627  set(p(1),'ZData',Zdata(1:4:end));
628  set(p(1),'Marker','o');
629 
630  legend(['y\^(t): reduced output'],...
631  'y\^(t)+\Delta_y(t)',...
632  'y\^(t)-\Delta_y(t)','Location','NorthWest');
633  title(['k = ',num2str(model.N)],'Fontsize',15);
634 
635  % table of error, error estimator and effectivity
636 
637  reduced_data = gen_reduced_data(model,detailed_data);
638 
639  Ns = 150:-10:10;
640  Ns = Ns(find(reduced_data.N>=Ns));
641 
642  err = zeros(1,length(Ns));
643  err_estim = zeros(1,length(Ns));
644  effectivity = zeros(1,length(Ns));
645 
646  for ni = 1:length(Ns)
647  model.N = Ns(ni);
648  rb_sim_data = rb_simulation(model, reduced_data);
649  err(ni) = abs(ds_sim_data.Y(end) - rb_sim_data.Y(end));
650  err_estim(ni) = rb_sim_data.DeltaY(end);
651  effectivity(ni) = err_estim(ni)/err(ni);
652  end;
653  Ns
654  err
655  err_estim
656  effectivity
657  disp('finished computing effectivities')
658  keyboard;
659 
660  case 11 % test of matlab ode solver for resulting sparse system
661 
662  params.coarse_factor = 8; % nake model smaller
663  model = fast_model(params);
664  model.RB_generation_mode = 'from_detailed_data';
665 
666  % detailed simulation:
667  model_data = gen_model_data(model);
668  model_data.RB = zeros(size(model_data.G,1),0);
669  detailed_data = gen_detailed_data(model,model_data);
670 
671  model.decomp_mode = 0; % complete
672  model = model.set_time(model,0)
673  model = model.set_mu(model,[0.5,1,1]);
674 
675  A = @(model,model_data,t) ...
676  model.A_function_ptr(model.set_time(model,t),model_data);
677 
678  B = @(model,model_data,t) ...
679  model.B_function_ptr(model.set_time(model,t),model_data);
680 
681  x0 = model.x0_function_ptr(model,model_data);
682  tic;
683  [t,x] = ode15s(@(t,x) A(model,model_data,t)*x + B(model,model_data,t), ...
684  [0 1], x0);
685  time_ode15 = toc;
686  disp(['ode15 takes ',num2str(time_ode15),' sec']);
687 
688  params.plot_title = 'ode15 solution';
689  params.plot = model.base_model.plot;
690  params.axis_equal = 1;
691  lin_evol_model_data = gen_model_data(model.base_model);
692  plot_sequence((x'),lin_evol_model_data.grid,params)
693 
694  tic;
695  sim_data = detailed_simulation(model,model_data);
696  time_rbmatlab = toc;
697  disp(['rbmatlab takes ',num2str(time_rbmatlab),' sec']);
698  params.plot_title = 'rbmatlab solution';
699  params.plot = model.base_model.plot;
700  params.axis_equal = 1;
701  lin_evol_model_data = gen_model_data(model.base_model);
702  plot_sequence(sim_data.X,lin_evol_model_data.grid,params)
703 
704  % further plots for MCMCS paper
705  case 12
706 
707  load(fullfile(rbmatlabresult,detailed_data_fname),...
708  'model','detailed_data')
709 
710  % plot of error estimator over parameter variation
711 % model = fast_model(params);
712 % model_data = gen_model_data(model);
713  % dfname = fullfile(rbmatlabresult,'advection_fv_output_detailed_data_146');
714 % model.RB_basis_filename = ...
715 % fullfile(rbmatlabresult,'advection_fv_output_basis');
716 % model.RB_generation_mode = 'file_load';
717 % detailed_data = gen_detailed_data(model,model_data);
718 % save(fullfile(rbmatlabresult,...
719 % 'advection_fv_output_detailed_2trajectories.mat'),...
720 % 'model','detailed_data')
721 % keyboard;
722  % dfname = fullfile(rbmatlabresult,'advection_fv_output_detailed');
723  % load(dfname);
724  tmp = load(fullfile(rbmatlabresult,[trajectory_fnbase,'2']));
725  ds_sim_data = tmp.ds_sim_data;
726  disp('loaded basis from step 4')
727 
728  % factors = 0:0.05:1;
729  reduced_data = gen_reduced_data(model,detailed_data);
730  Ns = [1,8,16,32,64];
731  Ns = Ns(find(reduced_data.N>=Ns));
732  factors = 0:0.05:1;
733  err_estim = zeros(length(factors),length(Ns));
734  for ni = 1:length(Ns)
735  model.N = Ns(ni);
736  for i =1:length(factors)
737  model = model.set_mu(model, factors(i)*[1,1,1]);
738  rb_sim_data = rb_simulation(model, reduced_data);
739  % err(ni) = abs(ds_sim_data.Y(end) - rb_sim_data.Y(end));
740  err_estim(i,ni) = rb_sim_data.DeltaY(end);
741  % effectivity(ni) = err_estim(ni)/err(ni);
742  end;
743  end;
744 % keyboard;
745  p = plot(factors,err_estim');
746  colors = {[1,0,0],[0,0.6,0],[0,0,1],[0,0,0]};
747  linestyles = {'-','-.','-','-.'};
748  for i = 1:4;
749  set(p(i),'Linewidth',2,'Color',colors{i},'Linestyle',linestyles{i});
750  end;
751 
752  xlabel('parameter \mu_1=\mu_2=\mu_3');
753  ylabel('\Delta_y(\mu)');
754  title('error estimator from parameter sweep')
755  legend({'k=1','k=8','k=16','k=32','k=64'},'Location','NorthWest');
756  saveas(gcf,fullfile(rbmatlabresult,'parameter_sweep.eps'),'epsc');
757  keyboard;
758  otherwise
759  disp('step number unknown');
760 end;
761 
762 
763 
764 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
765 
766 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
767 % generation of model from lin_evol_model and replace
768 % by fast vectors
769 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
770 
771 function model = fast_model(params);
772 lin_evol_model = advection_fv_output_vconst_model(params);
773 lin_evol_model.vy_weight = 0.5;
774 lin_evol_model.mu_names = {'cone_amplitude','vx_weight'};
775 lin_evol_model.mu_ranges = {[0,1],[0,1]};
776 %lin_evol_model.debug = 1;
777 %lin_evol_model_data = gen_model_data(lin_evol_model);
778 
779 % set some additional fields in model which will be copied into
780 % ds model depending on whether constants are known or not:
781 estimate_constants = 0;
782 if estimate_constants
783  %%% if constant estimaton has to be performed:
784  lin_evol_model.estimate_lin_ds_nmu = 4;
785  lin_evol_model.estimate_lin_ds_nX = 10;
786  lin_evol_model.estimate_lin_ds_nt = 4;
787 else
788  %%% otherwise:
789  % the following values are rough bounds, so could be
790  % non-rigorous, then larger search must be performed.
791  lin_evol_model.state_bound_constant_C1 = 1;
792  lin_evol_model.output_bound_constant_C2 = 1.2916;
793 end;
794 
795 model = lin_ds_from_lin_evol_model(lin_evol_model);
796 % set accelerated data functions from below
797 model.A_function_ptr = @(model,model_data) ...
798  eval_affine_decomp(@A_components,...
799  @A_coefficients,...
800  model,model_data);
801 
802 model.B_function_ptr = @(model,model_data) ...
803  eval_affine_decomp(@B_components,...
804  @B_coefficients,...
805  model,model_data);
806 %model.u_function_ptr = @(params) 0.1; % define new
807 
808 %model .theta = 1;
809 %disp('set theta to 1!!!');
810 %keyboard;
811 
812 
813 
814 
815 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
816 % auxialiary coefficient functions for acceleration of
817 % advection model: explicit implementation of coefficients
818 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
819 
820 function Acomp = A_components(model,model_data)
821 Acomp = model_data.A_components;
822 
823 function Acoeff = A_coefficients(model);
824 %model.base_model.decomp_mode = 2;
825 %model.base_model.t = model.t;
826 %mu = get_mu(model);
827 %model.base_model = model.set_mu(model.base_model,mu);
828 %[L_I_coeff, L_E_coeff, b_coeff] = ...
829 % model.base_model.operators_ptr(model.base_model, );
830 %% L_E = Id + Delta t * A
831 %Acoeff = L_E_coeff(2:end)/model.base_model.dt;
832 %keyboard;
833 %Acoeff = -model.base_model.dt * ...
834 % model.base_model.velocity_coefficients_ptr(model.base_model);
835 %if model.t>0
836  Acoeff = - [model.vx_weight, model.base_model.vy_weight]*0.5; %*(1-model.t);
837 % keyboard;
838 %end;
839 
840 function Bcomp = B_components(model,model_data)
841 Bcomp = model_data.B_components;
842 
843 function Bcoeff = B_coefficients(model);
844 % hmmm the coefficients are now called twice in A and B
845 % should somehow be cached later...
846 %model.base_model.decomp_mode = 2;
847 %model.base_model.t = model.t;
848 %mu = get_mu(model);
849 %model.base_model = model.set_mu(model.base_model,mu);
850 %[L_I_coeff, L_E_coeff, b_coeff] = ...
851 % model.base_model.operators_ptr(model.base_model, );
852 %% L_E = Id + Delta t * A
853 %Bcoeff2 = b_coeff/model.base_model.dt;
854 vcoeff = - [model.vx_weight, model.base_model.vy_weight]*0.5;%*(1-model.t);
855 Q_0 = model.base_model.cone_number;
856 dcoeff = zeros(1,Q_0);
857 max_pos = model.base_model.cone_weight * (Q_0-1)+1;
858 t = model.t;
859 for q = 1:Q_0
860  dcoeff(q) = (1-(max_pos-q))*(1-t) * ((max_pos>=q) && (max_pos < q+1)) ...
861  + (1+(max_pos-q))*(1-t) * ((max_pos>=q-1) && (max_pos < q));
862 end;
863 v = -(vcoeff(:)*(dcoeff(:)'));
864 Bcoeff = v(:)*model.cone_amplitude;
865 %if model.t>0
866 %keyboard;
867 %end;
868 
869 %function Ccomp = C_components(model,model_data)
870 %Ccomp = model_data.C_components;
871 %
872 %function Ccoeff = C_coefficients(model);
873 %model.base_model.decomp_mode = 2;
874 %model.base_model.t = model.t;
875 %mu = get_mu(model);
876 %model.base_model = model.set_mu(model.base_model,mu);
877 %Ccoeff = ...
878 % model.base_model.operators_output(model.base_model);
879 
880 
881 %| \docupdate