rbmatlab  1.13.10
 All Classes Namespaces Files Functions Variables Groups Pages
gdl_Nadapt.m
Go to the documentation of this file.
1 % dgl_adaptivity
2 %
3 % script performing the experiments for the N-adaptivity with the
4 % extended error estimator
5 
6 % Bernard Haasdonk 7.11.2008
7 
8 %step = 1 % generate figures
9 %step = 2 % perform time measurements and plot time over error/estimator/Nav
10 %step = 3 % profiling non-adaptive
11 %step = 4 % profiling adaptive
12 %step = 5 % simulation with longer time
13 step = 6 % new figures ignoring error
14 
15 disp('preparing simulations:');
16 load demo_lin_evol_detailed_data;
17 offline_data = rb_offline_prep(detailed_data,params);
18 
19 %params.N_adaptivity = 'off';
20 %params.N = 10 % set max!
21 %params.error_norm = 'l1l2';
22 
23 params.N_mode = 'adapt';
24 params.N = 100; % set max!
25 %params.N = size(detailed_data.RB,2); % set max!
26 %params.error_norm = 'l1l2';
27 params.error_norm = 'l2';
28 
29 % the following illustrates non-rigorous error-estimator for adaptivity!!!!
30 params.N_adapt_epsilon = 1e-4;
31 %params.N_adapt_epsilon = 2e-5;
32 %params.N_adapt_epsilon = 1e-8;
33 params.N_adapt_look_ahead_nt = 20;
34 params.N_adapt_slope = 10;
35 % params.N_adapt_epsilon = 1e-3; => everything over 50% N
36 %params = set_mu([1,1,0],params); % convection
37 params = set_mu([1,0,1e-9],params); % convection
38 %params = set_mu([1,0,0],params); % convection => super "prediction"
39 %params = set_mu([1,1,5e-8],params); % diffusion
40 %params = set_mu([0,1,5e-8],params); % diffusion
41 reduced_data = rb_online_prep(offline_data, params);
42 
43 switch step
44  case 1
45 %1. N adaptivity plot
46 disp('detailed_simulation:');
47 %params = set_mu([1,0,1e-9],params); % convection
48 params = set_mu([1,0,0],params); % convection
49 params.N_adapt_epsilon = 5e-5;
50 params.N_adapt_look_ahead_nt = 10;
51 params.N_adapt_slope = 5;
52 U = detailed_simulation(detailed_data.grid,params);
53 disp('reduced_simulation:');
54 simulation_data = rb_simulation(reduced_data,params);
55 Uappr = rb_reconstruction(detailed_data,simulation_data);
56 err = fv_error(Uappr,U,detailed_data.W,params);
57 % params.title = 'reduced simulation result';
58 % plot_element_data_sequence(detailed_data.grid,Uappr,params);
59 if isfield(simulation_data,'N');
60  Ns = simulation_data.N;
61 else
62  Ns = params.N * ones(1,params.nt+1);
63 end;
64 figure; % subplot(2,1,2);
65 plot([0:params.nt],Ns);
66 title('model dimension N');
67 set(gca,'XLim',[0,params.nt]);
68 set(gca,'YLim',[0,100]);
69 xlabel('time index k');
70 set(gcf,'Position',[472 404 564 238]);
71 saveas(gcf,'Nadaptive_error','epsc');
72 %subplot(2,1,1),
73 figure, plot([0:params.nt],[simulation_data.Delta;...
74  0:(params.N_adapt_epsilon)/params.nt:params.N_adapt_epsilon;err]');
75 legend({'estimator','target','error'},'location','west');
76 title('error estimator evolution');
77 set(gca,'XLim',[0,params.nt]);
78 xlabel('time index k');
79 set(gcf,'Position',[472 404 564 238]);
80 disp('please save figure as eps (automatic saving does not get aspectratio)')
81 %saveas(gcf,'Nadaptive_Ns','epsc');
82 
83 %plot_element_data_sequence(detailed_data.grid,Uappr,params);
84 %plot_element_data_sequence(detailed_data.grid,Uappr-U,params);
85 
86 params.N_mode = 'fixed';
87 params.N = 40;
88 reduced_data = rb_online_prep(offline_data, params);
89 disp('reduced_simulation:');
90 simulation_data = rb_simulation(reduced_data,params);
91 Uappr = rb_reconstruction(detailed_data,simulation_data);
92 err = fv_error(Uappr,U,detailed_data.W,params);
93 % params.title = 'reduced simulation result';
94 % plot_element_data_sequence(detailed_data.grid,Uappr,params);
95 if isfield(simulation_data,'N');
96  Ns = simulation_data.N;
97 else
98  Ns = params.N * ones(1,params.nt+1);
99 end;
100 figure, subplot(2,1,2); plot([0:params.nt],Ns);
101 title('model dimension N');
102 set(gca,'XLim',[0,params.nt]);
103 set(gca,'YLim',[0,100]);
104 xlabel('time index k');
105 subplot(2,1,1), plot([0:params.nt],[simulation_data.Delta;...
106  0:(params.N_adapt_epsilon)/params.nt:params.N_adapt_epsilon;err]');
107 legend({'estimator','target','error'},'location','northwest');
108 title('error estimator evolution');
109 set(gca,'XLim',[0,params.nt]);
110 xlabel('time index k');
111 disp('please save figure as eps (automatic saving does not get aspectratio)')
112 
113 
114 case 2
115 % time measurement:
116 
117 params.N = 100;
118 
119 nrep = 2;
120 t_exact = zeros(1,nrep);
121 disp('detailed_simulation:');
122 for r = 1:nrep
123  tic, U = detailed_simulation(detailed_data.grid,params);
124  t_exact(r) = toc;
125 end;
126 disp(' time av(N) Delta err ');
127 disp(['Exact simulation: ', ...
128  num2str(mean(t_exact)), '+-',num2str(std(t_exact)),...
129  ' ', num2str(detailed_data.grid.nelements),' 0 0 ']);
130 
131 disp('reduced_simulations, fixed Ns:');
132 Ns = [100,85,70,55,40,25,10];
133 t_Nfix = zeros(length(Ns),nrep);
134 Delta_Nfix = zeros(size(Ns));
135 % prevent matrix inversion for fair comparison
136 params.skip_matrix_inversion = 1;
137 params.N_mode = 'fixed';
138 
139 %dummy simulation
140 dummy_reduced_data = rb_online_prep(offline_data, params);
141 dummy = rb_simulation(dummy_reduced_data,params);
142 
143 for r = 1:nrep
144 for n = 1:length(Ns)
145  simulation_data = [];
146  params.N = Ns(n);
147  reduced_data = rb_online_prep(offline_data, params);
148  tic;
149  simulation_data = rb_simulation(reduced_data,params);
150  t_Nfix(n,r)= toc;
151  Uappr = rb_reconstruction(detailed_data,simulation_data);
152  err = fv_error(Uappr,U,detailed_data.W,params);
153  err_Nfix(n) = max(err);
154  Delta_Nfix(n) = simulation_data.Delta(end);
155 % disp(['N = ',num2str(Ns(n)),' ',num2str(t_Nfix(n,r)),' ',...
156 % num2str(Ns(n)),' ',...
157 % num2str(Delta_Nfix(n)),' ',num2str(err_Nfix(n))]);
158 end;
159 end;
160 disp('overall:')
161 for n = 1:length(Ns)
162  disp(['N = ',num2str(Ns(n)),' ',num2str(mean(t_Nfix(n,:))),...
163  '+-',num2str(std(t_Nfix(n,:))),' ',...
164  num2str(Ns(n)),' ',...
165  num2str(Delta_Nfix(n)),' ',num2str(err_Nfix(n))]);
166 end;
167 
168 disp('reduced_simulations, adaptive Ns:');
169 epsilons = [5e-6,1e-5,5e-5,1e-4,5e-4,1e-3];
170 Delta_Nadaptive = zeros(size(epsilons));
171 t_Nadaptive = zeros(length(epsilons),nrep);
172 Nav_Nadaptive = zeros(size(epsilons));
173 params.N = 100; %size(detailed_data.RB,2);
174 reduced_data = rb_online_prep(offline_data, params);
175 params.N_mode = 'adapt';
176 
177 for r = 1:nrep
178 for e = 1:length(epsilons)
179  simulation_data =[];
180  params.N_adapt_epsilon = epsilons(e);
181  tic;
182  simulation_data = rb_simulation(reduced_data,params);
183  t_Nadaptive(e,r)= toc;
184  Uappr = rb_reconstruction(detailed_data,simulation_data);
185  err = fv_error(Uappr,U,detailed_data.W,params);
186  err_Nadaptive(e) = max(err);
187  Delta_Nadaptive(e) = simulation_data.Delta(end);
188  Nav_Nadaptive(e) = mean(simulation_data.N);
189 % disp(['epsilon = ',num2str(epsilons(e)),...
190 % ' ',num2str(t_Nadaptive(e,r)),' ',num2str(Nav_Nadaptive(e)),...
191 % ' ',...
192 % num2str(Delta_Nadaptive(e)),' ',num2str(err_Nadaptive(e))]);
193 end;
194 end;
195 disp('overall:')
196 for e = 1:length(epsilons)
197  disp(['epsilon = ',num2str(epsilons(e)),...
198  ' ',num2str(mean(t_Nadaptive(e,:))),'+-',...
199  num2str(std(t_Nadaptive(e,:))),' ',num2str(Nav_Nadaptive(e)),...
200  ' ',...
201  num2str(Delta_Nadaptive(e)),' ',num2str(err_Nadaptive(e))]);
202 end;
203 
204  figure
205  plot(mean(t_Nadaptive,2),Delta_Nadaptive','b-');
206  hold on;
207  plot(mean(t_Nfix,2),Delta_Nfix','g-');
208  hold on;
209 % plot(mean(t_Nswitch,2),Delta_Nswitch','r-');
210 % hold on;
211  xlabel('runtime')
212  ylabel('error estimator')
213  set(gca,'Yscale','log')
214  title('error estimator over runtime')
215 % legend('N-adaptive','N-fixed','N-switch')
216  legend('N-adaptive','N-fixed')
217 
218  figure
219  plot(mean(t_Nadaptive,2),err_Nadaptive','b-');
220  hold on;
221  plot(mean(t_Nfix,2),err_Nfix','g-');
222 % hold on;
223 % plot(mean(t_Nswitch,2),err_Nswitch','r-');
224  xlabel('runtime')
225  ylabel('error')
226  set(gca,'Yscale','log')
227  title('error over runtime')
228 % legend('N-adaptive','N-fixed','N-switch')
229  legend('N-adaptive','N-fixed')
230 
231  figure
232  plot(Nav_Nadaptive,err_Nadaptive','b-');
233  hold on;
234  plot(Ns,err_Nfix','g-');
235 % hold on;
236 % plot(Nav_Nswitch,err_Nswitch','r-');
237  xlabel('average N')
238  ylabel('error')
239  set(gca,'Yscale','log')
240  title('error over average N')
241 % legend('N-adaptive','N-fixed','N-switch')
242  legend('N-adaptive','N-fixed')
243 
244  case 3 % profiling non-adaptivity
245  % => 56.80 fuer bicgstab, 4.17 fuer res_norm_sqr
246 
247  profile clear;
248  profile on
249  disp('without adaptivity:');
250  params.N_mode = 'fixed';
251  params.skip_matrix_inversion = 1;
252  tic
253  for i = 1:100
254  disp(i)
255  simulation_data = rb_simulation(reduced_data,params);
256  end;
257  toc
258  profview
259  profsave(profile('info'),'profile_results_non_adaptive')
260 
261 
262  case 4 % profiling non-adaptivity
263  % => 39.19 sec fuer bicgstab, 3.57 fuer res_norm_sqr
264  profile clear;
265  profile on
266  disp('with adaptivity:');
267  params.N_mode = 'adapt';
268  tic
269  for i = 1:100
270  disp(i)
271  simulation_data = rb_simulation(reduced_data,params);
272  end;
273  toc
274  profview
275  profsave(profile('info'),'profile_results_adaptive')
276 
277  case 5 % test: adaptive and fixed N should give identical result!
278 
279 params.N = 100;
280 disp('detailed_simulation:');
281 U = detailed_simulation(detailed_data.grid,params);
282 % prevent matrix inversion for fair comparison
283 params.N_mode = 'fixed';
284 params.skip_matrix_inversion = 1;
285 %dummy simulation
286 simulation_data_fix = [];
287 reduced_data = rb_online_prep(offline_data, params);
288 simulation_data_fix = rb_simulation(reduced_data,params);
289 Uappr_fix = rb_reconstruction(detailed_data,simulation_data_fix);
290 err_fix = fv_error(Uappr_fix,U,detailed_data.W,params);
291 disp(['error fixed N=100: ',num2str(err_fix(end))]);
292 
293 disp('reduced_simulations, adaptive Ns:');
294 params.N = 100; %size(detailed_data.RB,2);
295 params.N_mode = 'adapt';
296 reduced_data = rb_online_prep(offline_data, params);
297 simulation_data_adapt =[];
298 params.N_adapt_epsilon = 1e-6;
299 simulation_data_adapt = rb_simulation(reduced_data,params);
300 Uappr_adapt = rb_reconstruction(detailed_data,simulation_data_adapt);
301 err_adapt = fv_error(Uappr_adapt,U,detailed_data.W,params);
302 disp(['error adapt N=100: ',num2str(err_adapt(end))]);
303 
304  case 6 % new figures: no error....
305 
306 linestyles = {'-.','-','-',':'};
307 linecolors = {[0,0,1],[0,0.5,0],[1,0,0],[0,0,0]};
308 linewidths = [1,1,2,2];
309 
310 params = set_mu([1,0,0],params); % convection
311 params.N_adapt_epsilon = 5e-5;
312 params.N_adapt_look_ahead_nt = 20;
313 params.N_adapt_slope = 10;
314 disp('reduced_simulation:');
315 simulation_data = rb_simulation(reduced_data,params);
316 
317 params.N_mode = 'fixed';
318 params.N = 20;
319 reduced_data = rb_online_prep(offline_data, params);
320 disp('reduced_simulation:');
321 simulation_data_N1 = rb_simulation(reduced_data,params);
322 params.N = 60;
323 reduced_data = rb_online_prep(offline_data, params);
324 disp('reduced_simulation:');
325 simulation_data_N2 = rb_simulation(reduced_data,params);
326 %Uappr = rb_reconstruction(detailed_data,simulation_data);
327 if isfield(simulation_data,'N');
328  Ns = simulation_data.N;
329 else
330  Ns = params.N * ones(1,params.nt+1);
331 end;
332 figure; % subplot(2,1,2);
333 p = plot([0:params.nt],[ones(size(Ns))*20;ones(size(Ns))*60;Ns]');
334 for i=1:length(p)
335  set(p(i),'Color',linecolors{i},...
336  'LineWidth',linewidths(i),...
337  'LineStyle',linestyles{i})
338 end;
339 %set(p(3),'LineWidth',2);
340 %set(p(1),'LineStyle','-.');
341 title('model dimension N');
342 legend('N-fixed N=20','N-fixed N=60','N-adaptive')
343 set(gca,'XLim',[0,params.nt]);
344 set(gca,'YLim',[0,100]);
345 xlabel('time index k');
346 saveas(gcf,'Nadaptive_error_large','epsc');
347 set(gcf,'Position',[472 404 564 238]);
348 %subplot(2,1,1),
349 figure, p = plot([0:params.nt],[simulation_data_N1.Delta; ...
350  simulation_data_N2.Delta;...
351  simulation_data.Delta;...
352  0:(params.N_adapt_epsilon)/params.nt:params.N_adapt_epsilon
353  ]');
354 %keyboard;
355 for i=1:length(p)
356  set(p(i),'Color',linecolors{i},...
357  'LineWidth',linewidths(i),...
358  'LineStyle',linestyles{i})
359 end;
360 %keyboard;
361 %set(p(1),'LineStyle','-.');
362 %set(p(4),'LineStyle',':');
363 %set(p(3),'LineWidth',2);
364 title('error estimator evolution');
365 l = legend({'N-fixed, N=20', 'N-fixed, N=60', ...
366  'N-adaptive', ...
367  'N-adaptive-target'},'location','northwest');
368 set(gca,'XLim',[0,params.nt]);
369 xlabel('time index k');
370 saveas(gcf,'Nadaptive_Ns_large','epsc');
371 set(gcf,'Position',[472 404 564 238]);
372 po = get(l,'Position');
373 set(l,'Position',po);
374 disp('please save figures as eps (automatic saving does not get aspectratio)')
375 
376  otherwise
377  error('step unknown');
378 end;
379 %| \docupdate