rbmatlab  1.13.10
 All Classes Namespaces Files Functions Variables Groups Pages
chemnitz_gdl_experiments.m
Go to the documentation of this file.
1 % script collecting the commands for running the chemnitz-experiments
2 % i.e. for the paper
3 %
4 %B. Haasdonk, M. Ohlberger, M., G. Rozza: A Reduced Basis Method for Evolution
5 %Schemes with Parameter-Dependent Explicit Operators. Preprint 09/07 - N,
6 %FB 10 , University of Muenster, Preprint Nr. 17/2007,
7 %Institute of Mathematics, University of Freiburg, 2007, Accepted by ETNA.
8 
9 % Bernard Haasdonk 3.9.2007
10 
11 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
12 %%%%%% Select here, what is to be performed with the 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 collateral reduced basis of L_E operator
20 step = 3 % compare single detailed simulation with and without
21  % interpolated space operator vary the parameters to ensure that
22  % the empirical interpolation is stable
23 %step = 4 % generate dummy reduced basis from single trajectory and check, if
24  % ei_interpolation with projection on this space maintains
25  % result. A simple reduced simulation can also be
26  % performed. All results should be visually identical
27 %step = 5 % generate reduced basis
28 %step = 6 % time measurement of reduced simulation and
29  % use of reduced basis in rb_demo_gui
30 %step = 7 % generate train and/or test-error-landscape by doing
31  % reduced simulations for these
32 
33 % load linear gdl example, but turn it into nonlinear in the following
34 load demo_lin_evol_params
35 
36 %CRBfname = 'chemnitz_CRB_new_interpol';
37 %detailed_data_fname = 'chemnitz_detailed_data_new_interpol';
38 %CRBfname = 'chemnitz_CRB_new';
39 %detailed_data_fname = 'chemnitz_detailed_data_new';
40 CRBfname = 'chemnitz_CRB_new_interpol';
41 detailed_data_fname = 'chemnitz_detailed_data_new_interpol';
42 
43 % finite volume settings
44 %params.operators_algorithm = 'fv_operators_implicit_explicit';
45 %params.init_values_algorithm = 'fv_init_values';
46 params.implicit_operators_algorithm = 'fv_operators_implicit';
47 params.inner_product_matrix_algorithm = 'fv_inner_product_matrix';
48 params.data_const_in_time = 1;
49 params.L_E_local_name = 'fv_conv_explicit_space';
50 params.dt = params.T/params.nt;
51 
52 % change to nonlin problem type
53 params.mu_names = {'beta','c_init'};
54 %params.mu_ranges = {[-1,1],[0, 0.4]};
55 params.mu_ranges = {[0,1],[0, 1]};
56 params.rb_problem_type = 'nonlin_evol';
57 params.k = 0;
58 params.beta = 0;
59 params.c_init = 1;
60 
61 % settings for empirical interpolation
62 par.range = params.mu_ranges;
63 params.ei_numintervals = [4,4]; % 5x5 parameter grid
64 %params.Mmax = 300;
65 %params.Mmax = 500;
66 %params.Mmax = 150;
67 params.Mmax = 100;
68 params.ei_detailed_savepath = 'chemnitz_5x5_detailed';
69 params.ei_operator_savepath = 'chemnitz_5x5_ei_operator_new';
70 params.ei_time_indices = 1:params.nt;
71 % spaeter einbauen: params.ei_stop_epsilon = 1e-12;
72 params.CRB_generation_mode = 'param-time-space-grid';
73 %params.ei_target_error = 'approx';
74 %params.ei_target_error = 'interpol';
75 %disp('taking interpolation error as target in empirical interpolation!!');
76 
77 % settings for reduced basis generation
78 params.detailed_simulation_algorithm = 'detailed_simulation';
79 params.error_algorithm = 'fv_error';
80 %params.lxf_lambda = 1.0194e+003;
81 %params.data_const_in_time = 1;
82 params.error_norm = 'l2';
83 params.RB_extension_algorithm = 'RB_extension_PCA_fixspace';
84 % 'RB_extension_max_error_snapshot'};
85 params.RB_stop_timeout = 3*60*60; % 3 hours
86 params.RB_stop_epsilon = 1e-9;
87 params.RB_error_indicator = 'error'; % true error
88 %params.RB_error_indicator = 'estimator'; % Delta from rb_simulation
89 params.RB_stop_Nmax = 150;
90 params.RB_generation_mode = 'uniform_fixed';
91 params.RB_numintervals = params.ei_numintervals;
92 params.RB_detailed_train_savepath = params.ei_detailed_savepath;
93 
94 % caching of velocity field for acceleration of GDL-example
95 params.filecache_velocity_matrixfile_extract = 2;
96 
97 switch step
98  case 1 % single detailed simulation and plot
99  disp('performing single detailed simulation')
100  grid = construct_grid(params);
101  U = detailed_simulation(grid, params);
102  plot_element_data_sequence(grid,U,params);
103  case 2 % empirical interpolation of space operator
104  disp('constructing colateral reduced basis')
105  params.RB_generation_mode = 'none';
106  detailed_data = rb_detailed_prep(params);
107  save(fullfile(rbmatlabresult,CRBfname),...
108  'detailed_data','params');
109  detailed_data.RB = zeros(detailed_data.grid.nelements,1);
110  rb_plot_detailed_data(detailed_data,params);
111  close(gcf-2);
112  case 3 % compare detailed simulation with and without interpolation
113  disp(['comparison between detailed simulation with and without', ...
114  ' interpolation']);
115  % tmp = load(fullfile(rbmatlabresult,'chemnitz_CRB.mat'));
116  tmp = load(fullfile(rbmatlabresult,CRBfname));
117  detailed_data = tmp.detailed_data;
118  params = tmp.params;
119  params.k = 0;
120  params.beta = 0.4;
121  params.c_init = 1;
122  U = detailed_simulation(detailed_data.grid, params);
123  params.title = 'detailed simulation without interpolation';
124  plot_element_data_sequence(detailed_data.grid,U,params);
125  M = 100;
126  %M = round(size(detailed_data.QM,2)/2)
127  % M = round(size(detailed_data.QM,2)/2)
128  params.M = M;
129 % params.M = 400
130  Ui = detailed_ei_nonlin_evol_simulation(detailed_data,params);
131  params.title = 'detailed simulation with empirical interpolation';
132  plot_element_data_sequence(detailed_data.grid,Ui,params);
133  params.title = 'error';
134  plot_element_data_sequence(detailed_data.grid,abs(Ui-U),params);
135  disp(['maximum l-infty error:',num2str(max(max(abs(Ui-U))))]);
136  case 4 % construct dummy reduced basis by single trajectory and simulate
137  load(fullfile(rbmatlabresult,CRBfname));
138  disp('detailed interpolated simulation for basis construction:')
139 % params.M = round(size(detailed_data.QM{1},2)/2);
140  params.M = size(detailed_data.QM{1},2);
141 % params.nt = params.nt /2;
142  Ui = detailed_ei_nonlin_evol_simulation(detailed_data,params);
143  A = feval(params.inner_product_matrix_algorithm,detailed_data.grid,params);
144  UON = orthonormalize(Ui,A);
145  detailed_data.RB = UON;
146  disp('reduced simulation:')
147  offline_data = rb_offline_prep(detailed_data,params);
148  params.N = size(detailed_data.RB,2);
149 % params.M = size(detailed_data.QM{1},2);
150  reduced_data = rb_online_prep(offline_data, params);
151  simulation_data = rb_simulation(reduced_data,params);
152  Uappr = rb_reconstruction(detailed_data,simulation_data);
153 
154  disp('detailed interpolated and rb-projected simulation:')
155  params.M = size(detailed_data.QM{1},2);
156  params.N = size(detailed_data.RB,2);
157  Uirb = detailed_ei_rb_proj_nonlin_evol_simulation(detailed_data,params);
158 
159  params.title = 'reduced simulation result';
160  plot_element_data_sequence(detailed_data.grid,Uappr,params);
161  params.title = 'detailed ei_interpol simulation result';
162  plot_element_data_sequence(detailed_data.grid,Ui,params);
163  params.title = 'detailed ei_interpol and rb_projected simulation result';
164  plot_element_data_sequence(detailed_data.grid,Uirb,params);
165 
166  case 5 % reduced basis
167  disp('constructing reduced basis')
168  tmp = load(fullfile(rbmatlabresult,CRBfname));
169  detailed_data = tmp.detailed_data;
170 % params = tmp.params;
171  detailed_data = rb_basis_generation(detailed_data, ...
172  params);
173  save(fullfile(rbmatlabresult,detailed_data_fname),...
174  'detailed_data','params');
175  case 6
176  load(fullfile(rbmatlabresult,detailed_data_fname));
177  disp('reduced simulation:')
178  offline_data = rb_offline_prep(detailed_data,params);
179  params.N = size(detailed_data.RB,2);
180  params.M = size(detailed_data.QM{1},2);
181  reduced_data = rb_online_prep(offline_data, params);
182  tic;
183  simulation_data = rb_simulation(reduced_data,params);
184  t = toc;
185  disp(['time for online phase: t = ',num2str(t)]);
186 
187  disp('full simulation:')
188  tic;
189  U = detailed_simulation(detailed_data.grid, params);
190  t = toc;
191  disp(['time for detailed simulation: t = ',num2str(t)]);
192 
193  demo_rb_gui(detailed_data,[],params);
194 
195  case 7 % train-error landscape and test-error landscape
196  disp('warning: takes a few hours!');
197  load(fullfile(rbmatlabresult,detailed_data_fname));
198 
199  % replace RB with dummy basis from linear case!!
200  %disp('replacing RB with linear basis!! remove this afterwards!!');
201  %tmp = load('demo_lin_evol_detailed_data');
202  %detailed_data.RB = tmp.detailed_data.RB;
203 
204  % runs = {'train_linRB','test_linRB'};
205 % dirnames = {testdir};
206 % testdir = 'chemnitz_test_100';
207 
208 % runs = {'train','test'};
209 % dirnames = {params.RB_detailed_train_savepath, testdir};
210 
211 %runs = {'train'};
212 %dirnames = {params.RB_detailed_train_savepath};
213 
214 runs = {'test'};
215 dirnames = {testdir};
216 
217  % generate test data if not existent
218  if ismember('test',runs)
219  % generate test data if not existent
220  rand('state',123456);
221  M = rand_uniform(100,params.mu_ranges);
222  save_detailed_simulations(M,testdir,detailed_data.grid,params)
223  end;
224 
225  % load demo_nonlin_evol_detailed_data_LE_on_RB;
226  offline_data = rb_offline_prep(detailed_data,params);
227 % Nsamples = 3;
228  Nsamples = 11;
229  % Nmax = size(detailed_data.RB,2);
230  Nmax = 50;
231  Ns = round((0:(Nsamples-1)) * ...
232  (Nmax-1)/(Nsamples-1)+1);
233 
234  %Msamples = 3;
235  Msamples = 11;
236  % Mmax = size(detailed_data.BM,2);
237  Mmax = 100; % size(detailed_data.BM,2);
238  Ms = round((0:(Msamples-1)) * ...
239  (Mmax-1)/(Msamples-1)+1);
240 
241  for ru = 1:length(runs);
242  disp(['starting run ',runs{ru}]);
243  % storage for maximum l2-errors (i.e. linfty([0,T],l2)-norm)
244  errs = zeros(Msamples,Nsamples);
245  inds = zeros(Msamples,Nsamples);
246  mu_inds = zeros(Msamples,Nsamples);
247  % assuming, that training data is in the following directory
248  settings = load(fullfile(rbmatlabtemp,...
249  dirnames{ru},'settings.mat'));
250 
251  for mu_ind = 1:size(settings.M,2);
252  disp(['processing parameter vector ',num2str(mu_ind),...
253  '/',num2str(size(settings.M,2))]);
254  params = set_mu(settings.M(:,mu_ind),params);
255  sim_data = load_detailed_simulation(mu_ind,settings.savepath,params);
256  U_H = sim_data.U;
257  for Nind = 1:Nsamples
258  N = Ns(Nind);
259  for Mind = 1:Msamples
260  fprintf('.');
261  M = Ms(Mind);
262 
263  % nonlinear simulation
264  params.M = M;
265  params.N = N;
266  reduced_data = rb_online_prep(offline_data,params);
267  simulation_data = rb_simulation(reduced_data,params);
268  U = rb_reconstruction(detailed_data,simulation_data);
269 
270  l2_errors = fv_l2_error(U_H,U,detailed_data.W);
271  [err,ind] = max(l2_errors);
272  if err(1)>errs(Mind,Nind)
273  errs(Mind,Nind) = err(1);
274  inds(Mind,Nind) = ind(1);
275  mu_inds(Mind,Nind) = mu_ind;
276  end;
277  end;
278  end;
279  fprintf('.');
280  end;
281  % define stability-region as error being smaller than
282  % sqrt(diffmax * area), e.g. diffmax = 4, area = 2e-7
283 
284  stab_limit = 1e-3;
285  C = ones(size(errs));
286  i = find(errs<stab_limit);
287  C(i) = 2;
288 
289  if isempty(find(C==1)); % i.e. all stable
290  f1 = figure, surf(Ms, Ns,errs');
291  else % some not stable
292  f1 = figure, surf(Ms, Ns,errs',C');
293  shading interp;
294  end;
295  % figure, pcolor(Ms, Ns,C);
296  title([runs{ru},' L-infty([0,T],L2) error']);
297  set(gca,'Zscale','log');
298  xlabel('M');
299  ylabel('N');
300  zlabel('error');
301 
302  f2 = figure, surf(Ms, Ns,inds');
303  title([runs{ru},' time index of maximum l2-error']);
304  %set(gca,'Zscale','log');
305  xlabel('M');
306  ylabel('N');
307  zlabel('time index');
308 
309  save(fullfile(rbmatlabresult,...
310  ['chemnitz_gdl_experiment_step7_result_',...
311  runs{ru},'.mat']));
312  end;
313 
314  otherwise
315  error('step-number is unknown!');
316 end;
317 
318 % TO BE ADJUSTED TO NEW SYNTAX
319 %| \docupdate