rbmatlab  1.13.10
 All Classes Namespaces Files Functions Variables Groups Pages
adaptive_basisgen_mcmds_changed.m
1 function adaptive_basisgen_mcmds(step, loadfilename)
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 % Options for the experiments (like 2-D, 3-D case , etc) can be fixed
8 % below.
9 %
10 % mu_names = {'cone_weight','vx_weight','vy_weight'};
11 % mu_ranges = [0,1]^3
12 %
13 % steps:
14 %
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
19 % options.
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
24 %
25 % Experiments for the MCMDS paper:
26 % step 4: Basisgeneration using NO p-partition and a fixed-greedy
27 % algorithm.
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
30 %
31 % step 41: mat file from previous step is loaded.
32 % plot of grid?
33 %
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????
40 %
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
45 %
46 % step 61: plot of the p-grid... other plots???
47 %
48 %
49 %
50 % Bernard Haasdonk 4.2.2010
51 % Markus Dihlmann 15.02.2010
52 
53 if nargin < 1
54  step = 1;
55 end;
56 
57 
58 % Options:
59 % ---------------------------
60 
61 params = [];
62 model = [];
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;
66 %verbose(1);
67 trajectory_fnbase = 'advection_trajectory';
68 detailed_data_fname = 'advection_detailed_data';
69 rb_fname = 'advection_rb';
70 
71 % Set Parameter-space dimensions:
72 mu_dimensions ='2D-case';
73 %mu_dimensions ='3D-case';
74 params.mu_dimensions=mu_dimensions;
75 random_seed = 1234;
76 
77 
78 
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';
88 
89 % Set, if plot should be colored or not:
90 options=[];
91  options.simple_grid=1; %if =1 plot is not colored
92 
93 
94 
95 switch step
96 
97  case 1
98  disp('initialization of lin_ds model from lin_evol model');
99  % verbose(1,'initialization of lin_ds model from lin_evol model and ...
100  % plot');
101 
102  ds_model = fast_model(params);
103  ds_model.save_time_indices = 0:ds_model.nt;
104 
105  %ds_model.u_function_ptr = @(params) 0.1; % define new
106 % ds_model.theta = 1;
107  ds_model.theta = 0;
108  ds_model_data = gen_model_data(ds_model);
109  switch mu_dimensions
110  case '2D-case'
111  mu=[0.5,1];
112  case '3D-case'
113  mu = [0.5,1,1];
114  end
115 
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);
119 
120  % save trajectory
121  save(fullfile(rbmatlabtemp,[trajectory_fnbase,num2str(1)]),...
122  'ds_model','ds_model_data','ds_sim_data');
123 
124  % plot trajectory
125  params.no_lines = 1;
126  params.clim = [0,1];
127  params.plot_title='detailed implicit lin_ds';
128  plot_sim_data(ds_model,ds_model_data,ds_sim_data,params);
129 
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');
134  disp('...done.');
135  % smaller epsilon does result in more than 1 vector for mu=0,0,0
136  epsilon = 1e-6;
137  disp('Calling PCA_Fixspace...');
138  RB = PCA_fixspace(ds_sim_data.X,[],ds_model_data.G,150,[],epsilon);
139  disp('...done.');
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);
144  i = min(i);
145  disp(['found ',num2str(size(RB,2)),' basis vectors after traj. 1']);
146  if isempty(i)
147  i = size(RB,2)+1;
148  end;
149 % keyboard;
150  RB = RB(:,1:(i-1));
151  disp(['reduced to ',num2str(size(RB,2)),' basis vectors.']);
152 % keyboard;
153 
154  V = RB;
155  W = ds_model_data.G * V;
156  save(fullfile(rbmatlabtemp,rb_fname),'RB');
157 
158  ds_model.RB_basis_filename = fullfile(rbmatlabtemp,rb_fname);
159  ds_model.RB_generation_mode = 'file_load';
160 
161  detailed_data = gen_detailed_data(ds_model,ds_model_data);
162 
163  model = ds_model;
164  save(fullfile(rbmatlabtemp,detailed_data_fname),...
165  'model','detailed_data')
166 
167  % compare detailed and reduced simulation
168  load(fullfile(rbmatlabtemp,detailed_data_fname),...
169  'model','detailed_data')
170  load(fullfile(rbmatlabtemp,[trajectory_fnbase,'1']),...
171  'ds_sim_data');
172 
173  ds_model = model;
174 
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;
178  tic,
179 % ds_model.nt = ds_model.nt/4;
180 % disp('set nt to quarter!!');
181  rb_sim_data = rb_simulation(ds_model,online_data);
182  t_red = toc;
183  rb_sim_data = rb_reconstruction(ds_model,detailed_data,rb_sim_data)
184  disp(['time for reduced simulation: ',num2str(t_red)]);
185 
186  % plot of reduced trajectory
187  lin_evol_model = ds_model.base_model;
188  lin_evol_model_data = gen_model_data(lin_evol_model);
189 
190  params.no_lines = 1;
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, ...
196  % params);
197 
198  % plot sequence:
199  params.title = 'reduced solution';
200  plot_sequence(rb_sim_data.X,lin_evol_model_data.grid,params)
201 
202  % plot sequence:
203  params.title = 'error';
204  plot_sequence((rb_sim_data.X-ds_sim_data.X),...
205  lin_evol_model_data.grid,params)
206 
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');
210 
211  case 2
212 
213  % generate more intelligent basis
214 
215  disp('initialization of lin_ds model from lin_evol model');
216  % verbose(1,'initialization of lin_ds model from lin_evol model and ...
217  % plot');
218 
219  model = fast_model(params);
220  model.save_time_indices = 0:model.nt;
221  model.theta = 0;
222  model.get_rb_from_detailed_data = @(detailed_data) detailed_data.V; %only improvised help-method for method rb_test_indicator.m
223 
224  switch mu_dimensions
225  case '2D-case'
226  mu = [0.5,1];
227  model.RB_numintervals = [1,1];
228  case '3D-case'
229  mu = [0.5,1,1];
230  model.RB_numintervals = [1,1,1];
231  end
232 
233  % set mu in model and base_model!!!
234  model = model.set_mu(model,mu);
235  model.RB_stop_Nmax = 150;
236 
237 
238  switch RB_method
239 
240  case 'POD_of_single_trajectory'
241  model.RB_generation_mode = 'PCA_trajectory';
242 
243  case 'POD_of_several_trajectories'
244  %%% basis generation. POD of several trajectories
245  model.RB_generation_mode = 'PCA_trajectories';
246  switch mu_dimensions
247  case '2D-case'
248  model.RB_mu_list = {[0,0],[1,1]};
249  case '3D-case'
250  model.RB_mu_list = {[0,0,0],[1,1,1]};
251  end
252 
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;
262  model.RB_extension_algorithm = @RB_extension_PCA_fixspace;
263 
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';
268  model.RB_extension_algorithm = @RB_extension_PCA_fixspace;
269  model.RB_stop_epsilon = 1e-2;
270  model.RB_stop_timeout = 1*60; % 1 minute
271  model.RB_stop_Nmax = 5;
272 
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';
282  model.RB_extension_algorithm = @RB_extension_PCA_fixspace;
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]};
287 
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';
301  model.RB_extension_algorithm = @RB_extension_PCA_fixspace;
302  model.RB_stop_epsilon = 1e-2;
303  model.RB_stop_timeout = 45*60; % ... minutes
304  model.RB_stop_Nmax = 40;
305 
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';
316  model.RB_extension_algorithm = @RB_extension_PCA_fixspace;
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';
321  switch mu_dimensions
322  case '2D-case'
323  params.p_part_numintervals = [2,2];
324  model = p_part_model(model,params);
325  case '3D-case'
326  params.p_part_numintervals = [2,2,2];
327  model = p_part_model(model,params);
328  end
329 
330  case 'p_part_model_adaptive'
331  model.verbose=8;
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';
342  model.RB_extension_algorithm = @RB_extension_PCA_fixspace;
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';
349  switch mu_dimensions
350  case '2D-case'
351  params.p_part_numintervals =[1,1];
352  case '3D-case'
353  params.p_part_numintervals = [1,1,1];
354  end
355  model = p_part_model(model,params);
356 
357 
358 
359  end
360 
361 
362 
363  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
364 
365 
366  %model.verbose=8;
367  model_data = gen_model_data(model);
368  detailed_data = gen_detailed_data(model,model_data);
369 
370  save(fullfile(rbmatlabtemp,'p_part_basisgen_data'),'model','detailed_data');
371 
372  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
373 
374 
375 
376 
377 
378 case 3 %load detailed data generated in previous step and perform animation and reduced simulations
379 
380  load(fullfile(rbmatlabtemp,'p_part_basisgen_data'));
381 
382  % plot an animation of mesh refinement during basis generation
383 
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);
386  end
387 
388  %plot partitions of parameter space
389  if (strcmp(RB_method,'p_part_model_uniform')||strcmp(RB_method,'p_part_model_adaptive'))
390  plot_options=[];
391  %nleaf_elements=get(detailed_data.pgrid,'nleafelements');
392  %RB_N_str=[];
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));
396  %end
397 
398  figure(1)
399  % plot_options.text=RB_N_str;
400  plot_grid(detailed_data.pgrid,plot_options);
401 
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')))
405  options.figureNr=1;
406  options.color='r';
407  options.text=plot_options.text;
408  animate_basisgen(detailed_data,model,options);
409  end
410  end
411 
412  end
413 
414  save(fullfile(rbmatlabtemp,detailed_data_fname),...
415  'model','detailed_data')
416 
417  %model = model;
418 
419  % compare detailed and reduced simulation
420  load(fullfile(rbmatlabtemp,detailed_data_fname),...
421  'model','detailed_data')
422  load(fullfile(rbmatlabtemp,[trajectory_fnbase,'1']),...
423  'ds_sim_data');
424  %model = model;
425 
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;
430  end
431  tic,
432 % model.nt = model.nt/4;
433 % disp('set nt to quarter!!');
434  rb_sim_data = rb_simulation(model,online_data);
435  t_red = toc;
436  rb_sim_data = rb_reconstruction(model,detailed_data,rb_sim_data)
437  disp(['time for reduced simulation: ',num2str(t_red)]);
438 
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;
442  else
443  lin_evol_model = model.base_model.base_model;
444  end
445 
446 
447  lin_evol_model_data = gen_model_data(lin_evol_model);
448 
449  params.no_lines = 1;
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, ...
455  % params);
456 
457  % plot sequence:
458  params.title = 'reduced solution';
459  plot_sequence(rb_sim_data.X,lin_evol_model_data.grid,params)
460 
461  % plot sequence:
462  params.title = 'error';
463  plot_sequence((rb_sim_data.X-ds_sim_data.X),...
464  lin_evol_model_data.grid,params)
465 
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');
469 
470 
471 
472 
473 
474 
475  case 31 %load detailed data generated in previous step and perform animation and reduced simulations
476 
477  detailed_data_fname = 'save_case_4'
478 
479  load(fullfile(rbmatlabtemp,detailed_data_fname));
480 
481  % plot an animation of mesh refinement during basis generation
482 
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);
485 % end
486 
487 % %plot partitions of parameter space
488 % if (strcmp(RB_method,'p_part_model_uniform')||strcmp(RB_method,'p_part_model_adaptive'))
489 % plot_options=[];
490 % %nleaf_elements=get(detailed_data.pgrid,'nleafelements');
491 % %RB_N_str=[];
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));
495 % %end
496 
497 % figure(1)
498  % plot_options.text=RB_N_str;
499 % plot_grid(detailed_data.pgrid,plot_options);
500 
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;
505 % options.color='r';
506 % options.text=plot_options.text;
507 % animate_basisgen(detailed_data,model,options);
508 % end
509 % end
510 
511 % end
512 
513 % save(fullfile(rbmatlabtemp,detailed_data_fname),...
514 % 'model','detailed_data')
515 
516  %model = model;
517 
518  % compare detailed and reduced simulation
519  load(fullfile(rbmatlabtemp,detailed_data_fname),...
520  'model','detailed_data')
521 
522  tr_fn = fullfile(rbmatlabtemp,[trajectory_fnbase,'1']);
523  if exist(tr_fn)
524  load(tr_fn,...
525  'ds_sim_data');
526  else
527  model_data = gen_model_data(model);
528  ds_sim_data = detailed_simulation(model,model_data);
529  end;
530  %model = model;
531  model.N = model.get_rb_size(model,detailed_data);
532 
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;
538  end
539  tic,
540  % model.nt = model.nt/4;
541  % disp('set nt to quarter!!');
542  rb_sim_data = rb_simulation(model,online_data);
543  t_red = toc;
544  rb_sim_data = rb_reconstruction(model,detailed_data,rb_sim_data)
545  disp(['time for reduced simulation: ',num2str(t_red)]);
546 
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;
550  else
551  lin_evol_model = model.base_model.base_model;
552  end
553 
554  lin_evol_model_data = gen_model_data(lin_evol_model);
555 
556  params.no_lines = 1;
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, ...
562  % params);
563 
564  % plot sequence:
565  params.title = 'reduced solution';
566  plot_sequence(rb_sim_data.X,lin_evol_model_data.grid,params)
567 
568  % plot sequence:
569  params.title = 'error';
570  plot_sequence((rb_sim_data.X-ds_sim_data.X),...
571  lin_evol_model_data.grid,params)
572 
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');
576 
577  keyboard;
578 
579 
580 
581  case 4
582 % step 4: Basisgeneration using NO p-partition and a fixed-greedy
583 % algorithm.
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
586 %
587 
588  model = fast_model(params);
589  model.save_time_indices = 0:model.nt;
590  model.theta = 0;
591  model.get_rb_from_detailed_data = @(detailed_data) detailed_data.V; %only improvised help-method for method rb_test_indicator.m
592 
593  switch mu_dimensions
594  case '2D-case'
595  mu = [0.5,1];
596  model.RB_numintervals = [3,3];
597  case '3D-case'
598  mu = [0.5,1,1];
599  model.RB_numintervals = [2,2,2];
600  end
601 
602  % set mu in model and base_model!!!
603  model = model.set_mu(model,mu);
604 
605  model.RB_generation_mode = 'greedy_uniform_fixed';
606  model.RB_error_indicator = 'estimator';
607  model.RB_extension_algorithm = @RB_extension_PCA_fixspace;
608  model.RB_stop_epsilon = 1e-2;
609  model.RB_stop_timeout = 12*60*60; %
610  model.RB_stop_Nmax = 1000;
611  model.verbose=8;
612 
613 
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
615 
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
620 
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');
625 
626 
627 
628  case 41
629 
630 % step 41: mat file from previous step is loaded.
631 % plot of grid?
632 %
633 
634  load(fullfile(rbmatlabtemp,loadfilename));
635  options.simple_grid=1; %if =1 plot is not colored
636  model.random_seed=random_seed;
637 
638 
639 
640  online_data = gen_online_data(model,detailed_data);
641  model.N = online_data.N; %Unschoen aber nötig...
642 
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);
649 
650  model.random_seed=1234;%debug ...raus machen!!!
651  [test_error,test_error_max,av_rb_sim_time] = calculate_test_error(model, online_data);
652 
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');
665  end
666  if(detailed_data.RB_info.stopped_on_timeout)
667  disp('Stopped basis generation due to reached time limit');
668  end
669  if(detailed_data.RB_info.stopped_on_Nmax)
670  disp('Stopped basis generation due to reached maximum number of basis vectors ');
671  end
672 
673  if strcmp(mu_dimensions,'2D-case')
674  disp(['vy_weight was: ',num2str(model.base_model.vy_weight)]);
675  end
676 
677  %Write results to a protocol file
678  settings=[];
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;
686  end
687  results = [];
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;
693 
694 
695  % plot basisgeneration plot
696  animate_basisgen(detailed_data,model,options);
697 
698  save_result_protocol('p_part_test protokoll',settings, results);
699 
700  case 5
701 
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
706 
707  model = fast_model(params);
708  model.save_time_indices = 0:model.nt;
709  model.theta = 0;
710  model.get_rb_from_detailed_data = @(detailed_data) detailed_data.V; %only improvised help-method for method rb_test_indicator.m
711 
712  switch mu_dimensions
713  case '2D-case'
714  mu = [0.5,1];
715  model.RB_numintervals = [2,2];
716  case '3D-case'
717  mu = [0.5,1,1];
718  model.RB_numintervals = [2,2,2];
719  end
720 
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
725  model.verbose=8;
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';
734  switch mu_dimensions
735  case '2D-case'
736  params.p_part_numintervals = [2,2];
737  model = p_part_model(model,params);
738  case '3D-case'
739  params.p_part_numintervals = [2,2,2];
740  model = p_part_model(model,params);
741  end
742 
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
747 
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');
750 
751 
752 
753  case 51
754 % step 51: plot of p-grid of previous step
755 % ???other plots useful????
756 %
757  load(fullfile(rbmatlabtemp,loadfilename));
758  options.simple_grid=1; %if =1 plot is not colored
759  model.random_seed=random_seed;
760 
761 
762 
763  online_data = gen_online_data(model,detailed_data);
764  %model.N = online_data.N; %Unschoen aber nötig...
765 
766  nleafs = get(detailed_data.pgrid, 'nleafelements');
767  leafcogs = get(detailed_data.pgrid, 'leafcogs');
768  %reduced_timer = tic;
769  %for nl=1:nleafs
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);
773  %end
774  %t_red=toc(reduced_timer)/nleafs;
775 
776  %debug
777  t_gen =0;
778  %end debug ...entfernen sobald aktuelle simulationen vorhanden
779 
780  [test_error,test_error_max,av_rb_sim_time] = calculate_test_error(model, online_data);
781 
782  disp(['average time for reduced simulation: ',num2str(av_rb_sim_time)]);
783 
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');
796  end
797  if(detailed_data.parts_detailed_data{p}.RB_info.stopped_on_timeout)
798  disp('Stopped basis generation due to reached time limit');
799  end
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 ');
802  end
803  end
804 
805  if strcmp(mu_dimensions,'2D-case')
806  disp(['vy_weight was: ',num2str(model.base_model.base_model.vy_weight)]);
807  end
808 
809  %Write results to a protocol file
810  settings=[];
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;
818  end
819  results = [];
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);
826  end
827 
828  animate_basisgen(detailed_data,model,options);
829 
830  save_result_protocol('p_part_test protokoll',settings, results);
831 
832  case 6
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
837 %
838 
839  model = fast_model(params);
840  model.save_time_indices = 0:model.nt;
841  model.theta = 0;
842  model.get_rb_from_detailed_data = @(detailed_data) detailed_data.V; %only improvised help-method for method rb_test_indicator.m
843 
844  switch mu_dimensions
845  case '2D-case'
846  mu = [0.5,1];
847  model.RB_numintervals = [2,2];
848  case '3D-case'
849  mu = [0.5,1,1];
850  model.RB_numintervals = [2,2,2];
851  end
852 
853  % set mu in model and base_model!!!
854  model = model.set_mu(model,mu);
855  model.verbose=8;
856 
857  model.RB_generation_mode = 'greedy_uniform_fixed';
858  model.RB_error_indicator = 'estimator';
859  model.RB_extension_algorithm = @RB_extension_PCA_fixspace;
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';
867  switch mu_dimensions
868  case '2D-case'
869  params.p_part_numintervals =[1,1];
870  case '3D-case'
871  params.p_part_numintervals = [1,1,1];
872  end
873  model = p_part_model(model,params);
874 
875  model_data = gen_model_data(model);
876  overall_generation_time = tic;
877  detailed_data = gen_detailed_data(model,model_data);
878  detailes_data = clean_up_part_detailed_data(detailed_data);
879  t_gen = toc(overall_generation_time); %time necessary for generating the basis
880 
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');
883 
884  case 61
885 % step 61: plot of the p-grid... other plots???
886 %
887 
888  load(fullfile(rbmatlabtemp,loadfilename));
889  options.simple_grid=1; %if =1 plot is not colored
890  model.random_seed = random_seed;
891 
892 
893 
894 
895  online_data = gen_online_data(model,detailed_data);
896  %model.N = online_data.N; %Unschoen aber nötig...
897 
898  nleafs = get(detailed_data.pgrid, 'nleafelements');
899  leafcogs = get(detailed_data.pgrid, 'leafcogs');
900  %reduced_timer=tic;
901  %for nl=1:nleafs
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);
905  %end
906  %t_red=toc(reduced_timer)/nleafs;
907 
908  [test_error,test_error_max,av_rb_sim_time] = calculate_test_error(model, online_data);
909 
910  disp(['average time for reduced simulation: ',num2str(av_rb_sim_time)]);
911 
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');
924  end
925  if(detailed_data.parts_detailed_data{p}.RB_info.stopped_on_timeout)
926  disp('Stopped basis generation due to reached time limit');
927  end
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 ');
930  end
931  end
932 
933  if strcmp(mu_dimensions,'2D-case')
934  disp(['vy_weight was: ',num2str(model.base_model.base_model.vy_weight)]);
935  end
936 
937  %Write results to a protocol file
938  settings=[];
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;
946  end
947  results = [];
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);
955  end
956 
957  animate_basisgen(detailed_data,model,options);
958 
959  save_result_protocol('p_part_test protokoll',settings, results);
960 
961 
962 
963 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
964 % step = 7 % time measurements
965 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
966  case 7
967 
968 % clear all;
969  nreps = 10;
970  t_detailed = zeros(1,nreps);
971 
972  % initialize detailed model and simulate
973 
974  load(fullfile(rbmatlabtemp,detailed_data_fname),...
975  'model','detailed_data')
976  model = model;
977 
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);
981 
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;
990 % else
991 % %%% otherwise:
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;
996 % end;
997 %
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);
1004 %
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);
1011 
1012  disp('skipping detailed simulations...')
1013 % if 0
1014 
1015  for r = 1:nreps
1016  disp(['repetition = ',num2str(r)])
1017  mu = [1,1,0.5]-rand(1,3)*0.01;
1018  model = model.set_mu(model,mu);
1019  tic,
1020  ds_sim_data = detailed_simulation(model,model_data);
1021  t_detailed(1,r) = toc;
1022  end;
1023 
1024 % end;
1025 
1026  disp('--------------------------------------------------')
1027  disp('detailed simulation')
1028  disp(t_detailed);
1029  disp(['mean t_detailed=',num2str(mean(t_detailed))]);
1030  disp('--------------------------------------------------')
1031 
1032  % reduced simulation with and without error estimation
1033 
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);
1040 
1041  Ns = [150:-10:10]; %,30,20,10];
1042  Ns = Ns(find(online_data.N>=Ns));
1043 
1044  t_reduced_with_err_est = zeros(length(Ns),nreps);
1045  t_reduced_without_err_est = zeros(length(Ns),nreps);
1046 
1047  % reduced simulation with and without error estimation
1048 
1049  for err_est = 0:1;
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);
1055  for r = 1:nreps
1056  disp(['repetition = ',num2str(r)])
1057  mu = [1,1,1]-rand(1,3)*0.01;
1058  model = model.set_mu(model,mu);
1059  tic;
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!');
1063  keyboard;
1064  end;
1065  if err_est == 1
1066  t_reduced_with_err_est(n_ind,r) = toc;
1067  else
1068  t_reduced_without_err_est(n_ind,r) = toc;
1069  end;
1070  end;
1071  end;
1072  end;
1073 
1074  % output measurements
1075 
1076  disp('--------------------------------------------------')
1077  disp('detailed simulation')
1078  disp(t_detailed);
1079  disp(['mean t_detailed=',num2str(mean(t_detailed))]);
1080 
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)]);
1087 
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('--------------------------------------------------')
1095 
1096  save(fullfile(rbmatlabtemp,'time_measurements'));
1097 
1098 % dim_x = 65536, nt = 2048
1099 %
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%%%
1109 %
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
1120 %
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 %--------------------------------------------------
1124 
1125  case 8 % compare original and accelerated detailed model
1126 
1127  disp('compare original and accelerated lin_ds models');
1128 
1129  % clear all;
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;
1138  else
1139  %%% otherwise:
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;
1144  end;
1145 
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);
1149 
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,...
1154  @A_coefficients,...
1155  model,model_data);
1156 
1157  model_fast.B_function_ptr = @(model,model_data) ...
1158  eval_affine_decomp(@B_components,...
1159  @B_coefficients,...
1160  model,model_data);
1161 
1162  % model_fast.C_function_ptr = @(model,model_data) ...
1163  % eval_affine_decomp(@C_components,...
1164  % @C_coefficients,...
1165  % model,model_data);
1166 
1167  % disp('skipping detailed simulations...')
1168  % if 0
1169 
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);
1173  tic,
1174  ds_sim_data = detailed_simulation(model,model_data);
1175  t_detailed = toc;
1176  tic,
1177  ds_sim_data_fast = detailed_simulation(model_fast,model_data);
1178  t_detailed_fast = toc;
1179 
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!')
1184  else
1185  disp('simulation results of original and accelerated model equal!')
1186  end;
1187 
1188  keyboard;
1189 
1190  case 9 % generate basis from POD-Greedy
1191 
1192  filecache_clear;
1193  model = fast_model(params);
1194  model.cone_range = [0.4,0.6];
1195  model.base_model.cone_range = model.cone_range;
1196  % model.
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;
1204 
1205  % model.RB_generation_mode = 'uniform_fixed';
1206  % model.RB_num_intervals = [2,2,2];
1207 
1208  model_data = gen_model_data(model);
1209 
1210  disp('generating basis from POD-Greedy');
1211 
1212  % for basis generation, set further fields:
1213 
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;
1223  model.RB_extension_algorithm = @RB_extension_PCA_fixspace;
1224  model.RB_error_indicator = 'estimator';
1225  % default:
1226  % model.get_estimator_from_sim_data = @(sim_data) sim_data.DeltaX(end);
1227 
1228  detailed_data = gen_detailed_data(model,model_data);
1229 
1230  model = model;
1231  dfname = fullfile(rbmatlabtemp,[detailed_data_fname,'_greedy']);
1232  save(dfname,'model','detailed_data');
1233 
1234  disp('successfully generated and saved detailed_data');
1235 
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)
1240 
1241  keyboard;
1242 
1243  case 10 % reduced simulations and error plots
1244 
1245  %basis_from_step = 9; % step 9
1246  basis_from_step = 4; % step 4
1247 
1248  if basis_from_step == 9
1249  dfname = fullfile(rbmatlabtemp,[detailed_data_fname,'_greedy']);
1250  load(dfname);
1251  disp('loaded basis from step 9')
1252  else
1253  load(fullfile(rbmatlabtemp,detailed_data_fname),...
1254  'model','detailed_data')
1255 
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');
1264  % load(dfname);
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);
1270  end;
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);
1280  %model.N = 150;
1281  online_data = gen_online_data(model,detailed_data);
1282  rb_sim_data = rb_simulation(model,online_data);
1283 
1284  params.no_lines = 1;
1285  params.show_colorbar = 1;
1286  params.axis_equal = 1;
1287 
1288  % plot of reduced output
1289  figure;
1290  p = plot(rb_sim_data.time,...
1291  [rb_sim_data.Y;...
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 = {'-','-.','-.','-'};
1296  widths = [2,2,2,2];
1297  for i = 1:length(p)
1298  set(p(i),'Color',colors{i},'Linestyle',linestyles{i},...
1299  'Linewidth',widths(i));
1300  end;
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');
1309 
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);
1314 
1315  % table of error, error estimator and effectivity
1316 
1317  online_data = gen_online_data(model,detailed_data);
1318 
1319  Ns = 150:-10:10;
1320  Ns = Ns(find(online_data.N>=Ns));
1321 
1322  err = zeros(1,length(Ns));
1323  err_estim = zeros(1,length(Ns));
1324  effectivity = zeros(1,length(Ns));
1325 
1326  for ni = 1:length(Ns)
1327  model.N = Ns(ni);
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);
1332  end;
1333  Ns
1334  err
1335  err_estim
1336  effectivity
1337  disp('finished computing effectivities')
1338  keyboard;
1339 
1340  case 11 % test of matlab ode solver for resulting sparse system
1341 
1342  params.coarse_factor = 8; % nake model smaller
1343  model = fast_model(params);
1344  model.RB_generation_mode = 'from_detailed_data';
1345 
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);
1350 
1351  model.decomp_mode = 0; % complete
1352  model = model.set_time(model,0)
1353  model = model.set_mu(model,[0.5,1,1]);
1354 
1355  A = @(model,model_data,t) ...
1356  model.A_function_ptr(model.set_time(model,t),model_data);
1357 
1358  B = @(model,model_data,t) ...
1359  model.B_function_ptr(model.set_time(model,t),model_data);
1360 
1361  x0 = model.x0_function_ptr(model,model_data);
1362  tic;
1363  [t,x] = ode15s(@(t,x) A(model,model_data,t)*x + B(model,model_data,t), ...
1364  [0 1], x0);
1365  time_ode15 = toc;
1366  disp(['ode15 takes ',num2str(time_ode15),' sec']);
1367 
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);
1372  plot_sequence((x'),lin_evol_model_data.grid,params)
1373 
1374  tic;
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);
1382  plot_sequence(sim_data.X,lin_evol_model_data.grid,params)
1383 
1384  % further plots for MCMCS paper
1385  case 12
1386 
1387  load(fullfile(rbmatlabtemp,detailed_data_fname),...
1388  'model','detailed_data')
1389 
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')
1401 % keyboard;
1402  % dfname = fullfile(rbmatlabtemp,'advection_fv_output_detailed');
1403  % load(dfname);
1404  tmp = load(fullfile(rbmatlabtemp,[trajectory_fnbase,'2']));
1405  ds_sim_data = tmp.ds_sim_data;
1406  disp('loaded basis from step 4')
1407 
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));
1412  factors = 0:0.05:1;
1413  err_estim = zeros(length(factors),length(Ns));
1414  for ni = 1:length(Ns)
1415  model.N = Ns(ni);
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);
1422  end;
1423  end;
1424 % keyboard;
1425  p = plot(factors,err_estim');
1426  colors = {[1,0,0],[0,0.6,0],[0,0,1],[0,0,0]};
1427  linestyles = {'-','-.','-','-.'};
1428  for i = 1:4;
1429  set(p(i),'Linewidth',2,'Color',colors{i},'Linestyle',linestyles{i});
1430  end;
1431 
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');
1437  keyboard;
1438  otherwise
1439  disp('step number unknown');
1440 end;
1441 
1442 end
1443 
1444 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1445 
1446 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1447 % generation of model from lin_evol_model and replace
1448 % by fast vectors
1449 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1450 
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
1455  case '2D-case'
1456  lin_evol_model.mu_names = {'cone_amplitude','vx_weight'};
1457  lin_evol_model.mu_ranges = {[0,1],[0,1]};
1458  case '3D-case'
1459  lin_evol_model.mu_names = {'cone_amplitude','vx_weight','vy_weight'};
1460  lin_evol_model.mu_ranges = {[0,1],[0,1],[0,1]};
1461 end
1462 %lin_evol_model.debug = 1;
1463 %lin_evol_model_data = gen_model_data(lin_evol_model);
1464 
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;
1473 else
1474  %%% otherwise:
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;
1479 end;
1480 
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,...
1485  @A_coefficients,...
1486  model,model_data);
1487 
1488 model.B_function_ptr = @(model,model_data) ...
1489  eval_affine_decomp(@B_components,...
1490  @B_coefficients,...
1491  model,model_data);
1492 %model.u_function_ptr = @(params) 0.1; % define new
1493 
1494 %model .theta = 1;
1495 %disp('set theta to 1!!!');
1496 %keyboard;
1497 
1498 
1499 end
1500 
1501 
1502 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1503 % auxialiary coefficient functions for acceleration of
1504 % advection model: explicit implementation of coefficients
1505 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1506 
1507 function Acomp = A_components(model,model_data)
1508 Acomp = model_data.A_components;
1509 end
1510 
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;
1520 %keyboard;
1521 %Acoeff = -model.base_model.dt * ...
1522 % model.base_model.velocity_coefficients_ptr(model.base_model);
1523 %if model.t>0
1524  Acoeff = - [model.vx_weight, model.base_model.vy_weight]*0.5; %*(1-model.t);
1525 % keyboard;
1526 %end;
1527 end
1528 
1529 function Bcomp = B_components(model,model_data)
1530 Bcomp = model_data.B_components;
1531 end
1532 
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;
1548 t = model.t;
1549 for q = 1:Q_0
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));
1552 end;
1553 v = -(vcoeff(:)*(dcoeff(:)'));
1554 Bcoeff = v(:)*model.cone_amplitude;
1555 %if model.t>0
1556 %keyboard;
1557 %end;
1558 
1559 %function Ccomp = C_components(model,model_data)
1560 %Ccomp = model_data.C_components;
1561 %
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);
1567 %Ccoeff = ...
1568 % model.base_model.operators_output(model.base_model);
1569 end
1570 
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);
1575 
1576  random_mu = rand_uniform(N_test, model.mu_ranges);
1577 
1578  error_max=0;
1579  rb_sim_time=0;
1580  est_error=zeros(1,N_test);
1581  for i=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
1586  %time step
1587  rb_sim_timer = tic;
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);
1592 
1593  if~(isfield(model,'verbose'))
1594  model.verbose = 20;
1595  end
1596 
1597 
1598  if(model.verbose>5)
1599  if (mod(i,10)==0)
1600  disp('...')
1601  end
1602  end
1603  end
1604 
1605 
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;
1610 
1611  %Visualisation of error map on parameter domain
1612  figure;
1613  scatter(random_mu(1,:),random_mu(2,:),40,est_error,'filled');
1614  colorbar;
1615 end
1616 
1617 function save_result_protocol(filename, settings, results)
1618 
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');
1625 
1626  settingnames=fieldnames(settings);
1627  resultnames=fieldnames(results);
1628 
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}));
1633  else
1634  text = num2str(getfield(settings,settingnames{i}));
1635  end
1636  fprintf(fid,'%s:\t %s\n',settingnames{i},text);
1637  end
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}));
1642  else
1643  text = num2str(getfield(results,resultnames{i}));
1644  end
1645  fprintf(fid,'%s:\t %s\n',resultnames{i},text);
1646  end
1647 
1648  %save figures
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');
1652  clf;
1653  close(figure(1));
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');
1657  clf;
1658  close(figure(2));
1659  fclose(fid);
1660 end