rbmatlab  1.13.10
 All Classes Namespaces Files Functions Variables Groups Pages
advection_fv_output_morepas.m
Go to the documentation of this file.
1 % small script implementing a simple advection example
2 % for producing matrices to be used in the RB-DS framework
3 % Discretization with FV Functions
4 %
5 % mu_names = {'cone_weight','vx_weight','vy_weight'};
6 % mu_ranges = [0,1]^3
7 
8 % Bernard Haasdonk 25.8.2009
9 
10 % cartesian grid
11 
12 step = 1; % initialization of model and plot of model data
13 %step = 2; % detailed simulation of lin_evol
14 %step = 3; % conversion to DS primal model, DS detailed simulation
15  % and comparison with lin_evol
16 %step = 4 % reduced Basis generation by single trajectory and
17  %comparison of detailed and reduced simulation
18 % step = 5 % figures of detailed simuations based on step
19  % 4
20 %step = 6 % figures of reduced simuations based on step
21  %
22 %step = 7 % time measurements
23 
24 switch step
25 
26  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
27  %step = 1; % initialization of model and plot of model data
28  case 1
29  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
30 
31  model = advection_fv_output_model([]);
32  model_data = gen_model_data(model,[]);
33  plot(model_data.grid);
34  % inspect(model_data.grid)
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36  %step = 2; % detailed simulation of lin_evol
37 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
38  case 2
39  model = advection_fv_output_model([]);
40  model_data = gen_model_data(model,[]);
41  % model.debug = 1;
42  % disp('model debugging turned on.')
43  model.cone_weight = 0.5;
44  model.vx_weight = 1;
45  model.vy_weight = 1;
46  sim_data = detailed_simulation(model,model_data,[]);
47  plot_sim_data(model,model_data,sim_data);
48 
49  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
50 %step = 3; % conversion to DS primal model, DS detailed simulation
51  % and comparison with lin_evol
52 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
53 
54  case 3 % initialization of DS model and lin_ds detailed simulation
55 
56  lin_evol_model = advection_fv_output_model([]);
57  %lin_evol_model.debug = 1;
58  lin_evol_model_data = gen_model_data(lin_evol_model,[]);
59 
60  % set some additional fields in model which will be copied into
61  % ds model depending on whether constants are known or not:
62  estimate_constants = 0;
63  if estimate_constants
64  %%% if constant estimaton has to be performed:
65  lin_evol_model.estimate_lin_ds_nmu = 4;
66  lin_evol_model.estimate_lin_ds_nX = 10;
67  lin_evol_model.estimate_lin_ds_nt = 4;
68  else
69  %%% otherwise:
70  % the following values are rough bounds, so could be
71  % non-rigorous, then larger search must be performed.
72  lin_evol_model.state_bound_constant_C1 = 1;
73  lin_evol_model.output_bound_constant_C2 = 1.2916;
74  end;
75 
76  ds_model = lin_ds_from_lin_evol_model(lin_evol_model);
77  %ds_model.u_function_ptr = @(params) 0.1; % define new
78  ds_model_data = gen_model_data(ds_model,[]);
79  mu = [0.5,1,1];
80  ds_model = ds_model.set_mu(ds_model,mu);
81  ds_sim_data = detailed_simulation(ds_model,ds_model_data,[]);
82  lin_evol_model = lin_evol_model.set_mu(lin_evol_model,mu);
83  lin_evol_sim_data = detailed_simulation(lin_evol_model,...
84  lin_evol_model_data,[]);
85  err = lin_evol_sim_data.U - ds_sim_data.X;
86  disp(['max error in DOFs of lin_evol and ds simulation: ',...
87  num2str(max(abs(err(:))))]);
88 
89  lin_evol_model.plot_title='detailed lin_evol';
90  plot_sim_data(lin_evol_model,lin_evol_model_data,lin_evol_sim_data);
91  lin_evol_model.plot_title='detailed lin_ds';
92  lin_evol_from_ds_sim_data.U = ds_sim_data.X;
93  lin_evol_from_ds_sim_data.y = ds_sim_data.Y;
94  plot_sim_data(lin_evol_model,lin_evol_model_data,lin_evol_from_ds_sim_data);
95  lin_evol_model.plot_title='error';
96  error_sim_data = lin_evol_sim_data;
97  error_sim_data.U = error_sim_data.U-ds_sim_data.X;
98  plot_sim_data(lin_evol_model,lin_evol_model_data,error_sim_data);
99 
100  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
101  %step = 4 % reduced Basis generation by single trajectory and
102  %comparison of detailed and reduced simulation
103  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
104 
105  case 4 % generate and store reduced basis and test reduced simulation
106  lin_evol_model = advection_fv_output_model([]);
107  %lin_evol_model.debug = 1;
108  lin_evol_model_data = gen_model_data(lin_evol_model,[]);
109 
110  % set some additional fields in model which will be copied into
111  % ds model depending on whether constants are known or not:
112  estimate_constants = 0;
113  if estimate_constants
114  %%% if constant estimaton has to be performed:
115  lin_evol_model.estimate_lin_ds_nmu = 4;
116  lin_evol_model.estimate_lin_ds_nX = 10;
117  lin_evol_model.estimate_lin_ds_nt = 4;
118  else
119  %%% otherwise:
120  % the following values are rough bounds, so could be
121  % non-rigorous, then larger search must be performed.
122  lin_evol_model.state_bound_constant_C1 = 1;
123  lin_evol_model.output_bound_constant_C2 = 1.2916;
124  end;
125 
126  ds_model = lin_ds_from_lin_evol_model(lin_evol_model);
127  %ds_model.u_function_ptr = @(params) 0.1; % define new
128  ds_model_data = gen_model_data(ds_model,[]);
129  mu = [1,1,0.5];
130  %mu = [0,0.5,1];
131  ds_model = ds_model.set_mu(ds_model,mu);
132  ds_sim_data = detailed_simulation(ds_model,ds_model_data,[]);
133  save(fullfile(rbmatlabresult,'trajectory1'),...
134  'ds_model','ds_model_data','ds_sim_data');
135 
136 % % mu = [1,1,0.5];
137 % mu = [0,0.5,1];
138 % ds_model = ds_model.set_mu(ds_model,mu);
139 % ds_sim_data = detailed_simulation(ds_model,ds_model_data,[]);
140 % save(fullfile(rbmatlabresult,'trajectory2'),...
141 % 'ds_model','ds_model_data','ds_sim_data');
142 %
143 % % single trajectory for basis, POD
144 
145  load(fullfile(rbmatlabresult,'trajectory1'),...
146  'ds_model','ds_model_data','ds_sim_data');
147 
148  rbfname = 'advection_fv_output_basis';
149  RB = PCA_fixspace(ds_sim_data.X,[],ds_model_data.G);
150  % select correct orthogonal set
151  K = RB' * ds_model_data.G * RB;
152  i = find(abs(diag(K)-1)>1e-2);
153  i = min(i);
154  RB = RB(:,1:(i-1));
155  V = RB;
156  W = ds_model_data.G * RB;
157  save(fullfile(rbmatlabresult,rbfname),'V','W','ds_model','ds_model_data');
158 
159  ds_model.RB_filename = 'advection_fv_output_basis';
160  detailed_data = gen_detailed_data(ds_model,ds_model_data,[]);
161 
162  %perform single reduced simulation for given parameter
163  reduced_data = gen_reduced_data(ds_model,detailed_data,[]);
164  rb_sim_data = rb_simulation(ds_model,reduced_data,[]);
165  rb_sim_data = rb_reconstruction(ds_model,detailed_data,rb_sim_data)
166 
167  % plot of reduced trajectory
168  lin_evol_model = ds_model.base_model;
169  lin_evol_model_data = gen_model_data(lin_evol_model,[]);
170 
171  params.no_lines = 1;
172  params.show_colorbar = 1;
173  params.axis_equal = 1;
174  params.plot = lin_evol_model.plot;
175  % plot single snapshot
176  %lin_evol_model.plot(rb_sim_data.X(:,1),lin_evol_model_data.grid, ...
177  % params);
178 
179  % plot sequence:
180  params.title = 'reduced solution';
181  plot_sequence(rb_sim_data.X,lin_evol_model_data.grid,params)
182 
183  % plot sequence:
184  params.title = 'error';
185  plot_sequence((rb_sim_data.X-ds_sim_data.X),...
186  lin_evol_model_data.grid,params)
187 
188  % plot of reduced output
189  figure,plot(rb_sim_data.time,[rb_sim_data.Y;ds_sim_data.Y]);
190  legend('reduced output','detailed output');
191 
192  keyboard;
193 
194 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
195  case 5 % generate plots of detailed data for poster:
196  % step = 5 % figures of detailed simuations based on step 4
197 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
198 
199 
200 
201  rbfname = 'advection_fv_output_basis';
202  load(fullfile(rbmatlabresult,rbfname),'ds_model','ds_model_data');
203 
204  % plots of trajectory 1
205  load(fullfile(rbmatlabresult,'trajectory1'));
206  lin_evol_model = ds_model.base_model;
207  lin_evol_model_data = gen_model_data(lin_evol_model,[]);
208 
209  params.no_lines = 1;
210  params.show_colorbar = 0;
211  params.axis_equal = 1;
212  params.plot = lin_evol_model.plot;
213  lin_evol_model.plot(ds_sim_data.X(:,1),lin_evol_model_data.grid, ...
214  params);
215  set(gca,'Xtick',[])
216  set(gca,'Ytick',[])
217  saveas(gcf,'trajectory1_t1.eps','epsc');
218  close(gcf);
219 
220  lin_evol_model.plot(ds_sim_data.X(:,end),lin_evol_model_data.grid, ...
221  params);
222  set(gca,'Xtick',[])
223  set(gca,'Ytick',[])
224  saveas(gcf,'trajectory1_tend.eps','epsc');
225  close(gcf);
226 
227  p = ds_model.plot_sim_data(ds_model, ds_model_data, ds_sim_data);
228  % Ylim = get(gca,'Ylim');
229  % Ylim = [Ylim(1),Ylim(2)*1.1];
230  set(gca,'Ylim',[0,0.09]);
231  set(p(2),'Color',[0,0.8,0]);
232  saveas(gcf,'trajectory1_output.eps','epsc');
233  close(gcf);
234  close(gcf);
235 
236  % plots of trajectory 2
237  load(fullfile(rbmatlabresult,'trajectory2'));
238  lin_evol_model = ds_model.base_model;
239  lin_evol_model_data = gen_model_data(lin_evol_model,[]);
240 
241  params.no_lines = 1;
242  params.show_colorbar = 0;
243  params.axis_equal = 1;
244  lin_evol_model.plot(ds_sim_data.X(:,1),lin_evol_model_data.grid, ...
245  params);
246  set(gca,'Xtick',[])
247  set(gca,'Ytick',[])
248  saveas(gcf,'trajectory2_t1.eps','epsc');
249  close(gcf);
250 
251  lin_evol_model.plot(ds_sim_data.X(:,end),lin_evol_model_data.grid, ...
252  params);
253  set(gca,'Xtick',[])
254  set(gca,'Ytick',[])
255  saveas(gcf,'trajectory2_tend.eps','epsc');
256  close(gcf);
257 
258  p = ds_model.plot_sim_data(ds_model, ds_model_data, ds_sim_data);
259 % Ylim = get(gca,'Ylim');
260 % Ylim = [Ylim(1),Ylim(2)*1.1];
261  set(gca,'Ylim',[0,0.9]);
262  set(p(2),'Color',[0,0.8,0]);
263  saveas(gcf,'trajectory2_output.eps','epsc');
264  close(gcf);
265  close(gcf);
266 
267 
268  disp('generated 4 pictures of 2 trajectories')
269 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
270 % step = 6 % figures of reduced simuations based on step 4
271 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
272  case 6 % generate plots of reduced model for poster
273 
274  % as reference
275  load(fullfile(rbmatlabresult,'trajectory1'));
276 
277  ds_model.error_estimation = 1;
278  ds_model.RB_filename = 'advection_fv_output_basis';
279  detailed_data = gen_detailed_data(ds_model,ds_model_data,[]);
280  reduced_data = gen_reduced_data(ds_model,detailed_data,[]);
281 
282  Ns = [58,50,40,30,20,10];
283 
284  for nind = 1:length(Ns)
285  % perform reduced simulation
286  ds_model.N = Ns(nind);
287  rb_sim_data = rb_simulation(ds_model,reduced_data,[]);
288  rb_sim_data = rb_reconstruction(ds_model,detailed_data,rb_sim_data)
289 
290  params.no_lines = 1;
291  params.show_colorbar = 1;
292  params.axis_equal = 1;
293 
294  % plot of reduced output
295  figure;
296  p = plot(rb_sim_data.time,...
297  [rb_sim_data.Y;...
298  rb_sim_data.Y+rb_sim_data.DeltaY; ...
299  rb_sim_data.Y-rb_sim_data.DeltaY; ...
300  ds_sim_data.Y]);
301  colors = {[1,0,0],[1,0,0],[1,0,0],[0,1,0]};
302  linestyles = {'-','-.','-.','-'};
303  widths = [2,2,2,2];
304  for i = 1:length(p)
305  set(p(i),'Color',colors{i},'Linestyle',linestyles{i},...
306  'Linewidth',widths(i));
307  end;
308  % set marker on rb_sim_data:
309  Xdata = get(p(1),'XData');
310  Ydata = get(p(1),'YData');
311  Zdata = get(p(1),'ZData');
312  set(p(1),'XData',Xdata(1:4:end));
313  set(p(1),'YData',Ydata(1:4:end));
314  set(p(1),'ZData',Zdata(1:4:end));
315  set(p(1),'Marker','o');
316 
317  legend(['y\^(t): reduced output'],...
318  'y\^(t)+\Delta_y(t)',...
319  'y\^(t)-\Delta_y(t)',...
320  ['y(t): detailed output'],'Location','NorthWest');
321  title(['k = ',num2str(ds_model.N)],'Fontsize',15);
322  saveas(gcf,['output_estimation',num2str(ds_model.N),'.eps'],'epsc');
323  close(gcf);
324  end;
325 
326 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
327 % step = 7 % time measurements
328 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
329  case 7
330 
331  clear all;
332  Ns = [50,40,30,20,10]; %,30,20,10];
333  nreps = 5;
334  t_detailed = zeros(1,nreps);
335  t_reduced_with_err_est = zeros(length(Ns),nreps);
336  t_reduced_without_err_est = zeros(length(Ns),nreps);
337 
338  % initialize detailed model and simulate
339 
340  lin_evol_model = advection_fv_output_model([]);
341  %lin_evol_model.debug = 1;
342  %lin_evol_model_data = gen_model_data(lin_evol_model,[]);
343 
344  % set some additional fields in model which will be copied into
345  % ds model depending on whether constants are known or not:
346  estimate_constants = 0;
347  if estimate_constants
348  %%% if constant estimaton has to be performed:
349  lin_evol_model.estimate_lin_ds_nmu = 4;
350  lin_evol_model.estimate_lin_ds_nX = 10;
351  lin_evol_model.estimate_lin_ds_nt = 4;
352  else
353  %%% otherwise:
354  % the following values are rough bounds, so could be
355  % non-rigorous, then larger search must be performed.
356  lin_evol_model.state_bound_constant_C1 = 1;
357  lin_evol_model.output_bound_constant_C2 = 1.2916;
358  end;
359 
360  ds_model = lin_ds_from_lin_evol_model(lin_evol_model);
361  %ds_model.u_function_ptr = @(params) 0.1; % define new
362  ds_model_data = gen_model_data(ds_model,[]);
363 
364 % disp('skipping detailed simulations...')
365 % if 0
366 
367  for r = 1:nreps
368  disp(['repetition = ',num2str(r)])
369  mu = [1,1,0.5]-rand(1,3)*0.01;
370  ds_model = ds_model.set_mu(ds_model,mu);
371  tic,
372  ds_sim_data = detailed_simulation(ds_model,ds_model_data,[]);
373  t_detailed(1,r) = toc;
374  end;
375 
376 % end;
377 
378  disp('--------------------------------------------------')
379  disp('detailed simulation')
380  disp(t_detailed);
381  disp(['mean t_detailed=',num2str(mean(t_detailed))]);
382  disp('--------------------------------------------------')
383 
384  % reduced simulation with and without error estimation
385 
386  clear('ds_sim_data');
387  ds_model.RB_filename = 'advection_fv_output_basis';
388  detailed_data = gen_detailed_data(ds_model,ds_model_data,[]);
389  reduced_data = gen_reduced_data(ds_model,detailed_data,[]);
390 
391  % reduced simulation with and without error estimation
392 
393  for err_est = 0:1;
394  ds_model.error_estimation = err_est;
395  disp(['err_est= ',num2str(err_est)]);
396  for n_ind = 1:length(Ns)
397  disp(['M = ',num2str(Ns(n_ind))]);
398  ds_model.N = Ns(n_ind);
399  for r = 1:nreps
400  disp(['repetition = ',num2str(r)])
401  mu = [1,1,0.5]-rand(1,3)*0.01;
402  ds_model = ds_model.set_mu(ds_model,mu);
403  tic;
404  rb_sim_data = rb_simulation(ds_model,reduced_data,[]);
405  if err_est == 1
406  t_reduced_with_err_est(n_ind,r) = toc;
407  else
408  t_reduced_without_err_est(n_ind,r) = toc;
409  end;
410  end;
411  end;
412  end;
413 
414  % output measurements
415 
416  disp('--------------------------------------------------')
417  disp('detailed simulation')
418  disp(t_detailed);
419  disp(['mean t_detailed=',num2str(mean(t_detailed))]);
420 
421  disp('--------------------------------------------------')
422  disp('reduced simulation with error estimation')
423  disp(t_reduced_with_err_est);
424  disp(['mean t_reduced_with_err_est=',...
425  num2str(mean(t_reduced_with_err_est,2)')]);
426  disp(['Ns =',num2str(Ns)]);
427 
428  disp('--------------------------------------------------')
429  disp('reduced simulation without error estimation')
430  disp(t_reduced_without_err_est);
431  disp(['mean t_reduced_without_err_est=',...
432  num2str(mean(t_reduced_without_err_est,2)')]);
433  disp(['Ns =',num2str(Ns)]);
434  disp('--------------------------------------------------')
435 
436  save('time_measurements')
437  otherwise
438  disp('step number unknown');
439 end;
440 
441