rbmatlab  1.13.10
 All Classes Namespaces Files Functions Variables Groups Pages
nonlin_symmetry.m
Go to the documentation of this file.
1 function [ddescr, rdescr, dmodel, rmodel] = nonlin_symmetry(steps, combined, M_by_N_ratio, noof_ei_extensions, random, explicit, num_cpus, params)
2 % function [descr, rdescr, dmodel, rmodel] = nonlin_symmetry(steps, combined, M_by_N_ratio, noof_ei_extensions, random, explicit, num_cpus, params)
3 % small script demonstrating the burgers equation with explicit fv
4 % discretization and RB model reduction
5 % example is meant to demonstrate automatic symmetry detection by
6 % the algorithm.
7 % These results are presented at HYP2008
8 
9 % Bernard Haasdonk 3.6.2008
10 
11 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
12 %%%%%% Select here, what is to be performed with the burgers data %%%%%%%%%%
13 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
14 %step = 1 % single detailed simulation with given data and plot. Run
15  % this with varying parameters mu until sure that scheme
16  % is stable. Modify dt or the data-functions accordingly,
17  % until a nice parameter-domain with uniformly stable
18  % detailed scheme is obtained.
19 %step = 2 % generate colateral reduced basis of L_E operator
20 %step = 3 % compare single detailed simulation with and without
21  % interpolated space operator
22 %step = 4 % generate dummy reduced basis from single trajectory and check, if
23  % ei_interpolation with projection on this space maintains
24  % result. A simple reduced simulation can also be
25  % performed. All results should be visually identical.
26 %step = 5 % generate reduced basis
27 %step = 6 % time measurement of reduced simulation and demo rb gui
28 %step = 7 % generate error-landscape over varying N and M.
29  % This can take several hours!!!
30 %step = 8 % generate estimator-landscape over varying N and M and
31 % Mstrich. This can take several hours!!!
32 %step = 9 % generate different Figures for presentation and time measurement
33 %step = 10 % start demo_rb_gui
34 %step = 11 % comparison of error estimator and error
35 
36 if nargin == 0
37  steps = [0,5];
38 end
39 
40 %steps = [1,2,3,4,5,7,8];
41 
42 if nargin <= 1
43  combined = true;
44  M_by_N_ratio = 0;
45  noof_ei_extensions = 1;
46  random = false;
47  explicit = true;
48  num_cpus = 4;
49 end
50 if nargin <= 7
51  params = [];
52 end
53 
54 rmodel = [];
55 
56 for si=1:length(steps)
57 
58 step = steps(si);
59 
60 % output-filenames in rbmatlabresult
61 detailedfname = 'nonlin_symmetry_detailed.mat';
62 testdir = 'nonlin_symmetry_test_10';
63 results_fname_base = 'nonlin_symmetry_step7_result';
64 
65 plot_params.axis_equal = 1;
66 plot_params.clim = [0,1];
67 plot_params.plot = @fv_plot;
68 
69 params.explicit = explicit;
70 params.model_type = 'nonlin_symmetry';
71 params.step7_outputfile = fullfile(rbmatlabresult, [params.model_type, '_step7_output']);
72 params.step8_outputfile = fullfile(rbmatlabresult, [params.model_type, '_step8_output']);
73 
74 extrapar.Mstrich = 100;
75 
76 % grid parameters
77 switch step
78  case 0
79  if ~isempty(getenv('MACHINEFILE'))
80  machinefile = getenv('MACHINEFILE');
81  FakeMPI.MPI_Init(machinefile, fullfile(rbmatlabhome, 'fakeMPI', 'run_parCompErrs.sh'));
82  FakeMPI.MPI_Run;
83  end
84 
85  ddescr = nonlin_symmetry_descr(params);
86  rdescr.rb_problem_type = ddescr.rb_problem_type;
87 
88  if ~combined
89  rdescr.Mmax_small = 0;
90  else
91  rdescr.Mmax_small = 20;
92  end
93 
94  rdescr.extension_M_by_N_r = M_by_N_ratio; % 2.5;
95  rdescr.refinement_theta = 0.15;
96  rdescr.noof_ei_extensions = noof_ei_extensions;
97  rdescr.train_num_intervals = 25;
98  rdescr.val_size = 4;
99  rdescr.stop_Mmax = 600;
100  rdescr.stop_Nmax = 300;
101  rdescr.stop_timeout = 12*60*60;
102  rdescr.stop_epsilon = 1e-9;
103  rdescr.epsilon_M_by_N_r = 1e-1;
104  rdescr.stop_max_val_train_ratio = 1.15;
105 
106  if random
107  rdescr.train_sample_mode = 'random';
108  rdescr.train_num_intervals = 400;
109  rdescr.stop_max_val_train_ratio = inf;
110  end
111 
112  [rmodel, dmodel] = gen_models(ddescr, rdescr);
113 
114  params.mu_ranges = rmodel.mu_ranges;
115  params.mu_names = rmodel.mu_names;
116 
117  dmodel.num_cpus = num_cpus;
118 
119  model_data = gen_model_data(dmodel);
120  case 1 % single detailed simulation and plot
122  case 2 % empirical interpolation of space operator
124  case 3 % compare detailed simulation with and without interpolation
126  case 4 % construct dummy reduced basis by single trajectory and simulate
128  case 5 % reduced basis
129  detailed_data = gen_detailed_data(rmodel, model_data);
130  save(fullfile(rbmatlabresult, detailedfname), 'detailed_data', 'rmodel');
131  FakeMPI.MPI_Finalize;
132  case 6
134  case 7
135  load(fullfile(rbmatlabresult,detailedfname));
136  model.verbose = 1;
137 
138  model.Mstrich = 0;
139  % maximum number of reduced basis functions
140  model.Nmax = 135;
141  % maximum number of collateral reduced basis functions
142  model.Mmax = size(detailed_data.BM{1},2);
143  % fixed ratio between M and N in couples plots.
144  model.M_by_N_ratio = 1.5;
145  % number of reduced basis sizes to be tested.
146  Nsize = 20;
147  % number of collateral reduced basis sizes to be tested.
148  Msize = 25;
149  % sample of values for the coupling variable 'c' for which errors are
150  % computed.
151  cmax = min(1,model.Mmax/(model.M_by_N_ratio*model.Nmax));
152  csample = cmax/20:cmax/20:cmax;
153  % axis description for plots with coupling variable 'c'
154  cdescr = '(N,M) = c * (Nmax, Mmax)';
155  % size of mu vector test set.
156  mu_set_size = 20;
157 
159  case 7.5
160  load(params.step65_output);
161  for i = 1:length(output)
162  plot_error_landscape(output{i});
163  end
164  case 8
165  disp('warning: takes a few hours!');
166  load(fullfile(rbmatlabresult,detailedfname));
167 
168  % maximum number of reduced basis vectors
169  model.Nmax = size( detailed_data.RB, 2 );
170  % maximum number of collateral reduced basis vectors used for
171  model.Mmax = size( detailed_data.BM{1}, 2 ) - extrapar.Mstrich;
172  % fixed ratio between M and N in couples plots.
173  model.M_by_N_ratio = 1.5;
174 
175  % number of reduced basis sizes to be tested.
176  Nsize = 20;
177  % number of collateral reduced basis sizes to be tested.
178  Msize = 25;
179  % Mstrich values to be tested
180  Mstrich_samples = 1:2:extrapar.Mstrich;
181  % sample of values for the coupling variable 'c' for which
182  % estimators are computed.
183  cmax = min(1,model.Mmax/(model.M_by_N_ratio*model.Nmax));
184  csample = cmax/20:cmax/20:cmax;
185  % size of mu vector test set
186  mu_set_size = 20;
187  % axis description for plots with coupling variable 'c'
188  cdescr = '(N,M) = c * (Nmax, Mmax)';
189 
191  case 8.5
192  load(params.step75_output);
193  for i = 1:length(output)
194  plot_error_landscape(output{i});
195  end
196  case 9 % generate figures and runtime table
197  load(fullfile(rbmatlabresult,detailedfname));
198 
199  % anfangsdaten, Enddaten p=1, Enddaten p=2
200 
201  model.flux_pdeg = 1;
202  U1 = detailed_simulation(detailed_data.grid,model);
203  model.flux_pdeg = 2;
204  U2 = detailed_simulation(detailed_data.grid,model);
205 
206  model.no_lines = 1;
207  figure;
208  plot_element_data(detailed_data.grid,U1(:,1),model);
209  title('Initial Data');
210  set(gcf,'Position',[420 663 565 285]);
211  saveas(gcf,'smooth_init_data.eps','epsc');
212 
213  figure;
214  plot_element_data(detailed_data.grid,U1(:,end),model);
215  title('End Data p=1');
216  set(gcf,'Position',[420 663 565 285]);
217  saveas(gcf,'smooth_end_data_p1.eps','epsc');
218 
219  figure;
220  plot_element_data(detailed_data.grid,U2(:,end),model);
221  title('End Data p=2');
222  set(gcf,'Position',[420 663 565 285]);
223  saveas(gcf,'smooth_end_data_p2.eps','epsc');
224 
225  model.no_lines = 1;
226  model.M = length(detailed_data.TM{1});
227  rb_plot_interpolation_points(detailed_data,model);
228  set(gcf,'Position',[420 663 565 285]);
229  cmap = gray(256);
230  cmap = cmap(1:150,:); % take dark shades
231  cmap(1,:) = [1.0,1.0,1.0]; % light yellow
232  colormap(cmap);
233  caxis([0,model.M]);
234  box on;
235  keyboard;
236  saveas(gcf,'smooth_interpolation_points.eps','epsc');
237 
238  % nicely arranged error landscape figures:
239 
240 % load(fullfile(rbmatlabresult,...
241 % [results_fname_base,'_train.mat']));
242  load(fullfile(rbmatlabresult,...
243  [results_fname_base,'_test.mat']));
244 
245  f1 = figure;
246  surf(Ms, Ns,errs');
247 % title(['test L^\infty([0,T],L^2) error'],'Fontsize',15);
248  title('test error','Fontsize',15); %L^\infty([0,T],L^2) error'],'Fontsize',15);
249  set(gca,'Zscale','log');
250  xlabel('M','Fontsize',15);
251  ylabel('N','Fontsize',15);
252  zlabel('error','Fontsize',15);
253  view(120,30)
254  tmp = load('redgreencolmap.mat');
255 % cmap = jet(1000);
256 % cmap = cmap(end:-1:1,:);
257  colormap(tmp.c);
258 % disp('adjust colorbar!');
259  set(gcf,'Color',[1.0,1.0,1.0]);
260 
261  keyboard;
262 
263  % generate runtime table
264  nreps = 10;
265 
266  Ns = [10,20,30,40,50,60, 70, 80, 90, 100];
267  Ms = [15,30,45,60,75,90,105,120, 135,150];
268  t_detailed = zeros(nreps,1);
269  for rep = 1:nreps
270  model.flux_pdeg = rand(1)+1;
271 % offline_data = rb_offline_prep(detailed_data,model);
272  tic;
273  u = detailed_simulation(model);
274  t_detailed(rep) = toc;
275  disp(['runtime for detailed: ',num2str(t_detailed(rep)),' sec.']);
276  end;
277 
278  t_reduced = zeros(nreps,length(Ns));
279  offline_data = rb_offline_prep(detailed_data,model);
280  for rn = 1:length(Ns)
281  model.N = Ns(rn);
282  model.M = Ms(rn);
283  reduced_data = rb_online_prep(offline_data,model);
284  for rep = 1:nreps
285  model.flux_pdeg = rand(1)+1;
286  tic;
287  simulation_data = rb_simulation(reduced_data,model);
288  t_reduced(rep,rn) = toc;
289  disp(['N= ',num2str(model.N),', M= ',num2str(model.M)]);
290  disp(['runtime for reduced: ',num2str(t_reduced(rep,rn)),' sec.']);
291  end;
292  end;
293 
294  disp('detailed :');
295  disp(' t_mean t_std ');
296  disp([num2str(mean(t_detailed)),' ',num2str(std(t_detailed))]);
297 
298  disp('reduced :');
299  disp('N M t_mean t_std');
300  o = [Ns(:), Ms(:), mean(t_reduced,1)', std(t_reduced,1)'];
301  s = sprintf('%2.0f %2.0f %2.2f %2.2f \n ',o');
302  disp(s);
303 
304  offline_data = rb_offline_prep(detailed_data,model);
305  plot(offline_data.grid_local_ext);
306  axis off
307  axis equal
308  axis tight
309  set(gcf,'Color',[1,1,1]);
310 
311  case 10 % start demo_rb_gui
312  load(fullfile(rbmatlabresult,detailedfname));
313  % Mmax = 150; Nmax = 100
314  model.N = 100;
315  model.M = 150;
316  model.Mstrich = 10;
317  model.enable_error_estimator = 1;
318 % model.RB_error_indicator = 'estimator';
319  demo_rb_gui(model,detailed_data,[],plot_params);
320 
321  case 11 % compare error and error estimator
322  load(fullfile(rbmatlabresult,detailedfname));
323  model.N = 135;
324  model.M = 290;
325 % model.M = 150;
326 % model.Mstrich = 10; % set below in loop
327  model.enable_error_estimator = 1;
328 
329  model_data = gen_model_data(model);
330 % detailed_data = gen_detailed_data(model,model_data);
331  reduced_data = gen_reduced_data(model, detailed_data);
332 
333  model = model.set_mu(model,[1.5]);
334 % model = model.set_mu(model,2);
335 % model = model.set_mu(model,1);
336  sim_data = detailed_simulation(model,model_data);
337 % Mstrich = [1,10,150];
338  Mstrich = [1,5,10];
339  for Mi = 1:length(Mstrich);
340  model.Mstrich = Mstrich(Mi);
341  rb_sim_data = rb_simulation(model, reduced_data);
342  rb_sim_data = rb_reconstruction(model, detailed_data, rb_sim_data);
343 
344  De{Mi} = rb_sim_data.Delta;
345  end;
346  err_data = sim_data;
347  err_data.U = sim_data.U - rb_sim_data.U;
348  err = sqrt(max(0,sum(err_data.U' * detailed_data.W * err_data.U)))';
349  max_t_err = err;
350 
351  % somehow replace by matrix version!!!
352  for i = 2:length(max_t_err)
353  if max_t_err(i) < max_t_err(i-1)
354  max_t_err(i) = max_t_err(i-1);
355  end;
356  end;
357  figure;
358  plot([De{1}(:),De{2}(:),De{3}(:),max_t_err(:),err(:)]);
359  Ylim = get(gca,'Ylim');
360  set(gca,'Ylim',[0,max(Ylim)]);
361  title('rb state error and estimator');
362  legend({'M''= 1, estimator \Delta(t)',...
363  'M''= 5, estimator \Delta(t)',...
364  'M''= 10, estimator \Delta(t)',...
365  '||e||_{L\infty([0,t],L2)}',...
366  '||e(t)||_{L2}'},'Location','NorthWest');
367 
368  otherwise
369  error('step-number is unknown!');
370 end
371 
372 end
373 
374 %| \docupdate