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