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
5 % mu_names = {
'cone_weight',
'vx_weight',
'vy_weight'};
8 % Bernard Haasdonk 25.8.2009
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
20 %step = 6 % figures of reduced simuations based on step
22 %step = 7 % time measurements
26 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
27 %step = 1; % initialization of model and plot of model data
29 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
39 model = advection_fv_output_model([]);
40 model_data = gen_model_data(model,[]);
42 % disp(
'model debugging turned on.')
43 model.cone_weight = 0.5;
46 sim_data = detailed_simulation(model,model_data,[]);
49 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
50 %step = 3; % conversion to DS primal model, DS detailed simulation
51 % and comparison with lin_evol
52 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
54 case 3 % initialization of DS model and lin_ds detailed simulation
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,[]);
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;
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;
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;
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,[]);
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(:))))]);
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);
100 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
101 %step = 4 % reduced Basis generation by single trajectory and
102 %comparison of detailed and reduced simulation
103 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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,[]);
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;
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;
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,[]);
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');
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');
143 % % single trajectory for basis, POD
145 load(fullfile(rbmatlabresult,'trajectory1'),...
146 'ds_model','ds_model_data','ds_sim_data');
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);
156 W = ds_model_data.G * RB;
157 save(fullfile(rbmatlabresult,rbfname),'V','W','ds_model','ds_model_data');
159 ds_model.RB_filename = 'advection_fv_output_basis';
160 detailed_data = gen_detailed_data(ds_model,ds_model_data,[]);
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)
167 % plot of reduced trajectory
168 lin_evol_model = ds_model.base_model;
169 lin_evol_model_data = gen_model_data(lin_evol_model,[]);
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, ...
180 params.title = 'reduced solution';
184 params.title = 'error';
186 lin_evol_model_data.grid,params)
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');
194 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
195 case 5 % generate plots of detailed data for poster:
196 % step = 5 % figures of detailed simuations based on step 4
197 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
201 rbfname = 'advection_fv_output_basis';
202 load(fullfile(rbmatlabresult,rbfname),'ds_model','ds_model_data');
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,[]);
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, ...
217 saveas(gcf,'trajectory1_t1.eps','epsc');
220 lin_evol_model.plot(ds_sim_data.X(:,end),lin_evol_model_data.grid, ...
224 saveas(gcf,'trajectory1_tend.eps','epsc');
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');
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,[]);
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, ...
248 saveas(gcf,'trajectory2_t1.eps','epsc');
251 lin_evol_model.plot(ds_sim_data.X(:,end),lin_evol_model_data.grid, ...
255 saveas(gcf,'trajectory2_tend.eps','epsc');
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');
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
275 load(fullfile(rbmatlabresult,'trajectory1'));
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,[]);
282 Ns = [58,50,40,30,20,10];
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)
291 params.show_colorbar = 1;
292 params.axis_equal = 1;
294 % plot of reduced output
296 p = plot(rb_sim_data.time,...
298 rb_sim_data.Y+rb_sim_data.DeltaY; ...
299 rb_sim_data.Y-rb_sim_data.DeltaY; ...
301 colors = {[1,0,0],[1,0,0],[1,0,0],[0,1,0]};
302 linestyles = {
'-',
'-.',
'-.',
'-'};
305 set(p(i),'Color',colors{i},
'Linestyle',linestyles{i},...
306 'Linewidth',widths(i));
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');
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');
326 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
327 % step = 7 % time measurements
328 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
332 Ns = [50,40,30,20,10]; %,30,20,10];
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);
338 % initialize detailed model and simulate
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,[]);
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;
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;
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,[]);
364 % disp(
'skipping detailed simulations...')
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);
372 ds_sim_data = detailed_simulation(ds_model,ds_model_data,[]);
373 t_detailed(1,r) = toc;
378 disp('--------------------------------------------------')
379 disp('detailed simulation')
381 disp(['mean t_detailed=',num2str(mean(t_detailed))]);
382 disp('--------------------------------------------------')
384 % reduced simulation with and without error estimation
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,[]);
391 % reduced simulation with and without error estimation
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);
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);
404 rb_sim_data = rb_simulation(ds_model,reduced_data,[]);
406 t_reduced_with_err_est(n_ind,r) = toc;
408 t_reduced_without_err_est(n_ind,r) = toc;
414 % output measurements
416 disp('--------------------------------------------------')
417 disp('detailed simulation')
419 disp(['mean t_detailed=',num2str(mean(t_detailed))]);
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)]);
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('--------------------------------------------------')
436 save('time_measurements')
438 disp('step number unknown');