rbmatlab  1.16.09
 All Classes Namespaces Files Functions Variables Modules Pages
RB_greedy_extension.m
Go to the documentation of this file.
1 function [detailed_data, reduced_data] = RB_greedy_extension(model,detailed_data)
2 %function [detailed_data,reduced_data] = ...
3 % RB_greedy_extension(model,detailed_data);
4 % function performing a greedy search loop for reduced basis
5 % generation.
6 %
7 % In each iteration the parameter vector with maximum posterior estimator or
8 % true error is selected for "refinement". An orthonormal basis is generated.
9 %
10 % The detailed_data is expected to be reasonably filled, that rb_simulations
11 % are possible with the given arguments, i.e. gen_reduced_data(model,
12 % detailed_data). That means, that it also can contain an existing basis,
13 % which is then extended in this loop. the offline_data for after the last
14 % extension is optionally returned.
15 %
16 % The stopping of the extention is performed, if
17 %
18 % - 'model.RB_stop_epsilon' is reached: basis vectors are added until
19 % the maximum posterior estimator is smaller than this
20 % value. M_val can be ommited in this case.
21 % - 'model.RB_stop_max_val_train_ratio': basis vectors are added until the
22 % ratio r of the max estimator on a validation set M_val with the
23 % estimator on the training set M is larger than r_max. M_val is
24 % required in this case
25 % - 'model.RB_stop_timeout': basis vectors are added until toc gives a
26 % time exceeding a maximum level given
27 % - 'model.RB_stop_Nmax': If given number of basis vectors in full
28 % basis is obtained.
29 % - 'model.p_part_early_refinement' is set to one and the estimated
30 % number of basis vectors necessary to reach etol is higher
31 % than 'RB_stop_Nmax'.
32 %
33 % The following fields are created/extended in 'detailed_data.RB_info':
34 % - 'M_first_errs' : a vector with the initial error-estimator for each
35 % M_train. Is determined to check, whether any
36 % extension has to be performed or not.
37 % - 'stopped_on_epsilon' : flag indicating whether epsilon-stop-crit is reached
38 % - 'stopped_on_timeout': flag indicating whether timeout is reached
39 % - 'stopped_on_Nmax': flag indicating whether Nmax is reached
40 % - 'stopped_on_max_val_train_ratio': flag indicating whether
41 % epsilon-stop-crit is reached
42 % - 'stopped_on_empty_extension' : flag indicating, whether the last
43 % extension failed due to accuracy problems, i.e. new
44 % basis-vector already in span of existing.
45 % - 'stopped_on_Nlimit_estimation': flag indicating ifthe extension
46 % process was stopped, because it was estimated that more
47 % than Nmax basis vectoras are needed to reach etol.
48 % Partition of the parameter domain can be initiated.
49 % - 'max_err_sequence' : the decreasing maximum posterior estimators or true
50 % errors in each iteration, which are present AFTER the
51 % respective basis extension. So in case of 'stopped_on_epsilon',
52 % the last entry of this field should be smaller than the desired
53 % epsilon. The first entry is the maximal initial error/estimator.
54 % - 'mu_sequence' : sequence of mu_values chosen for
55 % basis extension i.e. list of worst-estimator mu_values as the
56 % columns of the matrix mu_values. The last entry corresponds
57 % to the errors/estimator values AFTER the last basis
58 % extension.
59 % - 'N_sequence' : sequence of basis sizes AFTER each extension. First
60 % entry is basis size before first extension.
61 % - 'r_value_sequence' : ratios of val/train error-estimator in case of
62 % validation set providing, AFTER the respective basis-extension.
63 % - 'toc_value_sequence' : toc_values after each basis extension
64 % - 'M_last_errs' : the posterior estimators for all mu in M_train at final
65 % time AFTER the last respective basis extension
66 %
67 % Required fields in 'detailed_data.RB_info':
68 % - 'M_train' : matrix with parameter-vectors as columns, over which greedy
69 % search is to be performed.
70 % - 'M_val' : matrix with parameter-vectors as columns, which are used
71 % for validation. Only required, if the field
72 % 'model.stop_on_max_val_train_ratio' is set)
73 %
74 % The following fields of detailed_data are created/extended:
75 % - RB : is extended by the number of basis-vectors, overall being the
76 % orthonormal basis
77 % - RB_info : the fields of this info-structure are extended, as described
78 % above
79 %
80 % required fields of model:
81 % RB_extension_algorithm: function pointer to either
82 % RB_extension_max_error_snapshot() or
84 % RB_error_indicator: either 'estimator' or 'error' indicating,
85 % whether an aposteriori indicator or true error
86 % is to be used
87 % RB_detailed_val_savepath : path to the detailed data of the
88 % validation set in validation mode. Is generated
89 % if not available.
90 % RB_detailed_train_savepath : path to the detailed data of the
91 % training set. Is generated if not available. Is
92 % only required in case of 'error' mode instead of
93 % 'estimator'
94 %
95 % additional fields as required by the selected
96 % extension algorithm and the reduced-basis-simulation must be set in model.
97 
98 % Bernard Haasdonk 26.5.2007
99 
100 if ~isfield(model,'RB_stop_timeout')
101  model.RB_stop_timeout = inf;
102 end;
103 
104 if ~isfield(model,'RB_stop_Nmax')
105  model.RB_stop_Nmax = inf;
106 end;
107 
108 if ~isfield(model,'RB_stop_epsilon')
109  model.RB_stop_epsilon = 0;
110 end;
111 
112 if ~isfield(model,'RB_stop_max_val_train_ratio')
113  model.RB_stop_max_val_train_ratio = inf;
114 end;
115 
116 if ~isfield(model,'RB_save_temp_detailed_data')
117  model.RB_save_temp_detailed_data = 0;
118 end;
119 
120 % check, whether val-error is wanted
121 check_val_error = 0;
122 if model.RB_stop_max_val_train_ratio < inf
123  check_val_error = 1;
124 end;
125 
126 if isfield(model,'p_part_early_refinement')
127  enable_p_partition = 1;
128 else
129  enable_p_partition = 0;
130 end
131 
132 %hp-greedy:
133 if isfield(model,'h_part')
134  enable_h_partition = 1;
135 else
136  enable_h_partition = 0;
137 end
138 
139 
140 
141 % make sure, that detailed-simulations for validation set are available
142 % in case of active validation criterion and wanted true error
143 if check_val_error
144  if isempty(detailed_data.RB_info.M_val)
145  error ('M_val must be provided in case of max_val_train_ratio mode!');
146  end;
147  if isequal(model.RB_error_indicator,'error')
148  detailed_val_savepath = model.RB_detailed_val_savepath;
149  save_detailed_simulations_ptr = model.save_detailed_simulations;
150  save_detailed_simulations_ptr(model,detailed_data, ...
151  detailed_data.RB_info.M_val, ...
152  detailed_val_savepath);
153  else
154  detailed_val_savepath = [];
155  end;
156 end;
157 
158 % make sure, that detailed-simulations for the training set are available in
159 % case of wanted true error
160 if isequal(model.RB_error_indicator,'error') | isequal(model.RB_error_indicator,'projection-error')
161  save_detailed_simulations_ptr = model.save_detailed_simulations;
162  save_detailed_simulations_ptr(model,detailed_data,...
163  detailed_data.RB_info.M_train, ...
164  model.RB_detailed_train_savepath);
165  detailed_train_savepath = model.RB_detailed_train_savepath;
166 else
167  detailed_train_savepath = 'dummy-string';
168 end;
169 
170 % initialize detailed_data.RB_info
171 if ~isfield(detailed_data,'RB_info')
172  detailed_data.RB_info = [];
173 end;
174 
175 N = model.get_rb_size(model,detailed_data);
176 
177 if ~isfield(detailed_data.RB_info,'max_err_sequence')
178  detailed_data.RB_info.max_err_sequence = nan(1,N);
179 end
180 if ~isfield(detailed_data.RB_info,'mu_sequence')
181  detailed_data.RB_info.mu_sequence = ...
182  nan(length(model.mu_names), N);
183 end
184 if ~isfield(detailed_data.RB_info,'mu_ind_seq')
185  detailed_data.RB_info.mu_ind_seq = ...
186  nan(1, N);
187 end
188 
189 if check_val_error && ~isfield(detailed_data.RB_info,'r_value_sequence')
190  detailed_data.RB_info.r_value_sequence = nan(1,N);
191 end
192 
193 if ~isfield(detailed_data.RB_info,'toc_value_sequence')
194  detailed_data.RB_info.toc_value_sequence = nan(1,N);
195 end
196 
197 if ~isfield(detailed_data.RB_info, 'N_sequence')
198  detailed_data.RB_info.N_sequence = [];
199 end
200 
201 % (partially) retain info of previous runs
202 RB_info = detailed_data.RB_info;
203 
204 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
205 % initialize loop-break conditions %
206 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
207 RB_info.stopped_on_epsilon = 0;
208 RB_info.stopped_on_timeout = 0;
209 RB_info.stopped_on_Nmax = 0;
210 RB_info.stopped_on_max_val_train_ratio = 0;
211 RB_info.stopped_on_empty_extension = 0;
212 RB_info.stopped_on_Nlimit_estimation = 0;
213 N_limit_est_av=[]; % average over all estimations (pessimistic)
214 continue_loop = 1;
215 
216 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
217 % initialize output quantities %
218 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
219 max_pe = [];
220 mu_values = [];
221 mu_ind_seq = [];
222 toc_values = [];
223 if check_val_error
224  r_values = [];
225 end;
226 N_limit_est = [];% estimated amount of N needed
227 post_errs_sequence = [];
228 post_est_sequence = [];
229 
230 % perform extension loop:
231 while continue_loop
232 
233  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
234  % determine mu-worst error-indicator for current training set %
235  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
236 
237  reduced_data = gen_reduced_data(model,detailed_data);
238 
239  model.N = reduced_data.N;
240  post_errs = rb_test_indicator(model, ...
241  detailed_data, reduced_data,...
242  detailed_data.RB_info.M_train,...
243  detailed_train_savepath);
244  % store real error as well, if estimator and error are computed for
245  % 'ei_estimator_test'
246  if isequal(model.RB_error_indicator,'ei_estimator_test')
247  post_errs_sequence = [post_errs_sequence, post_errs.error];
248  post_est_sequence = [post_est_sequence, post_errs.estimator];
249  post_errs = post_errs.estimator;
250  end
251 
252  [max_errs, mu_inds] = max(post_errs);
253  max_err = max_errs(1); % maximum error (estimation)
254  mu_ind = mu_inds(1); % parameter index in RB_info.M_train
255 
256  if isnan(max_err)
257  keyboard
258  end;
259 
260  % hp-greedy: first chosen mu is the first training parameter
261  if enable_h_partition && model.N == 0
262  mu_ind = 1;
263  end;
264 
265  mu = detailed_data.RB_info.M_train(:,mu_ind);
266 
267  max_pe = [max_pe, max_err];
268  mu_ind_seq = [mu_ind_seq, mu_ind];
269  mu_values = [mu_values, mu];
270 
271  % update mu-vector is stored in variable mu
272  disp(['Detected maximum error prediction ',num2str(max_err),...
273  ' for mu=[',num2str(mu'),']']);
274 
275  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
276  % check loop-break conditions %
277  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
278 
279  if max_err < model.RB_stop_epsilon
280  continue_loop = 0;
281  RB_info.stopped_on_epsilon = 1;
282  end;
283 
284  if model.get_rb_size(model,detailed_data) >= model.RB_stop_Nmax
285  continue_loop = 0;
286  RB_info.stopped_on_Nmax = 1;
287  end;
288 
289  if toc > model.RB_stop_timeout
290  RB_info.stopped_on_timeout = 1;
291  continue_loop = 0;
292  end;
293 
294  if check_val_error
295  val_est = rb_test_indicator(model,...
296  detailed_data,reduced_data,...
297  detailed_data.RB_info.M_val, ...
298  detailed_val_savepath);
299 
300  r_values = [r_values, max(val_est) / max_err];
301  %debug
302  disp(['val_train ratio: ',num2str(r_values(end))]);
303  %end debug
304  if (r_values(end) > model.RB_stop_max_val_train_ratio)
305  RB_info.stopped_on_max_val_train_ratio = 1;
306  continue_loop = 0;
307  end;
308  end;
309 
310  %in models using adaptive p-partition early stopping:
311  %an estimater extrapolates the curve N->epsilon and estimates if the
312  %epsilon_tol can be reached with a basis size smaller than Nmax. If no,
313  %the field RB_stopped_on_Nlimit_estimation is set to 1
314 
315 
316  if enable_p_partition && model.p_part_early_refinement
317  %estimate N total needed to reach etol:
318  err_seq=max_pe;
319  etol = model.RB_stop_epsilon;
320  Nmax = model.RB_stop_Nmax;
321 
322  s=20; %stepsize backwards
323 
324 
325  if(length(err_seq)>(s))
326 
327  %for i=(s+1):length(err_seq);
328  i=length(err_seq);
329  a=(log(err_seq(i-s)/err_seq(i)))/(s);
330  N_limit_est=[N_limit_est,i-1/a*log(etol/err_seq(i))];
331  %end;
332 
333 
334 
335  %for i=1:length(N_limit_est);
336  %summe = summe+N_limit_est(i);
337  if(length(N_limit_est_av>0))
338  N_1=length(N_limit_est_av);
339  newAv=N_1/(N_1+1)*N_limit_est_av(end)+(1/(N_1+1))*N_limit_est(end);
340  N_limit_est_av=[N_limit_est_av,newAv];
341 
342  else
343  N_limit_est_av=N_limit_est(end);
344  end
345  %end;
346  disp(['actual estimation of N needed to reach etol: ',num2str(N_limit_est_av(end))]);
347  if(N_limit_est_av(end)>Nmax)
348  RB_info.stopped_on_Nlimit_estimation = 1;
349  continue_loop = 0;
350  end
351  end
352 
353 
354  end %of P-Part_early_refinement
355 
356  % always store computational time
357  toc_values = [toc_values, toc];
358 
359  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
360  % update information structure and possibly save intermediate state to file %
361  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
362 
363  RB_info.max_err_sequence = [RB_info.max_err_sequence, max_pe(end)];
364  RB_info.mu_ind_seq = [RB_info.mu_ind_seq, mu_ind_seq(end)];
365  RB_info.mu_sequence = [RB_info.mu_sequence, mu_values(:, end)];
366  RB_info.N_sequence = [RB_info.N_sequence, model.N];
367  if isequal(model.RB_error_indicator,'ei_estimator_test')
368  RB_info.post_errs_sequence = post_errs_sequence;
369  RB_info.post_est_sequence = post_est_sequence;
370  end
371  if check_val_error
372  RB_info.r_value_sequence = [RB_info.r_value_sequence, r_values(end)];
373  end;
374  RB_info.toc_value_sequence = [RB_info.toc_value_sequence, toc_values(end)];
375 
376  if enable_p_partition && model.p_part_early_refinement
377  RB_info.N_limit_est = N_limit_est;
378  RB_info.N_limit_est_av = N_limit_est_av;
379  end
380 
381  if numel(max_pe) == 1
382  RB_info.M_first_errs = post_errs;
383  end
384 
385  detailed_data.RB_info = RB_info;
386 
387  if model.RB_save_temp_detailed_data
388  save(fullfile(rbmatlabtemp,'temp_detailed_data.mat'),'detailed_data', ...
389  'model')
390  end;
391 
392  if continue_loop
393 
394  %%%%%%%%%%%%%%%%
395  % extend basis %
396  %%%%%%%%%%%%%%%%
397 
398  % Use original params with new mu such that filecaching works in the
399  % extension routine!
400 
401  %oldmu = model.get_mu(model);
402  %model = model.set_mu(model, mu);
403  %[RBext, par] = model.RB_extension_algorithm(model, ...
404  % detailed_data);
405  %model = model.set_mu(model, oldmu);
406  oldmu = get_mu(model);
407  model = set_mu(model, mu);
408  RBext = model.RB_extension_algorithm(model, detailed_data);
409  model = set_mu(model, oldmu);
410 
411  % if empty: no further processing has to be performed
412  if ~isempty(RBext)
413 
414  if model.verbose > 29
415  disp(par);
416  end;
417  RB = [model.get_rb_from_detailed_data(detailed_data), RBext];
418  % perform permutation that maintains original order if possible:
419  %RBo = model_orthonormalize_qr(model,detailed_data,RB,eps);
420  %K = model.inner_product(model,detailed_data,RBo,RB);
421  %[m,i] = max(abs(K));
422  %RB = RBo(:,i);
423  % if model.debug
424  % K = model.inner_product(model,detailed_data,RB,RB);
425  % if max(max(abs(K-eye(size(K)))))>1e-10
426  % %error('error in orthonormal basis extension!');
427  % end;
428  % % end;
429  detailed_data = model.set_rb_in_detailed_data(...
430  detailed_data,...
431  RB);
432  %detailed_data.RB = [detailed_data.RB, RBext];
433  N = model.get_rb_size(model,detailed_data);
434  detailed_data.N = N;
435  disp(['Extended RB to length ', num2str(N)]);
436 
437  %orthogonality check
438 
439  RB = model.get_rb_from_detailed_data(detailed_data);
440  W = model.get_inner_product_matrix(detailed_data);
441  disp(['checking orthogonality with tolerance ',num2str(1e-12),...
442  '...',...
443  num2str(max(max(RB'*W*RB-eye(size(RB,2)))))]);
444  if(max(max(RB'*W*RB-eye(size(RB,2))))>1e-12)
445  disp('orthogonalising again...')
446  detailed_data = model.set_rb_in_detailed_data(detailed_data,...
447  model.orthonormalize(RB, W));
448  RB = model.get_rb_from_detailed_data(detailed_data);
449  disp(['new orthogonality value: ',num2str(max(max(RB'*W*RB-eye(size(RB,2)))))]);
450  end
451 
452  end % if ~isempty(RB)
453 
454  % model.N is not set yet: quick-fix of problem with "empty extension" after
455  % orthogonalizing
456  if model.N == model.get_rb_size(model, detailed_data)
457  RB_info.stopped_on_empty_extension = 1;
458  continue_loop = 0;
459  end
460 
461  % keyboard;
462 
463  % check on accuracy problem (non-decreasing error)
464  % if length(max_pe)>1 & max_pe(end)>max_pe(end-1)
465  % stopped_on_error_increase = 1;
466  % continue_loop = 0;
467  % disp('stopped on error-increase! Please check!');
468  % keyboard;
469  % else
470  % stopped_on_error_increase = 0;
471  % end;
472 
473  % model.Nmax = size(detailed_data.RB,2);
474  % model.N = model.Nmax;
475  % [mu_ind, post_errs] = detect_mu_worst(detailed_data,model);
476  % max_err = max(post_errs);
477 
478 
479  end; % if continue_loop
480 
481 end; % while continue_loop
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 [ RBext , dummy ] = RB_extension_PCA_fixspace(model, detailed_data)
function computing a RB basis extension for given parameters by the POD-Greedy algorithm.
function [ detailed_data , reduced_data ] = RB_greedy_extension(model, detailed_data)
function performing a greedy search loop for reduced basis generation.
function save_detailed_simulations(model, model_data, M, savepath)
perform loop over detailed simulations and save results or check consistency with existing saved resu...
function test_err = rb_test_indicator(model, detailed_data, reduced_data, M_test, savepath)
M_test,[savepath])