2 %
function [detailed_data,reduced_data] = ...
4 %
function performing a greedy search loop
for reduced basis
7 % In each iteration the parameter vector with maximum posterior estimator or
8 %
true error is selected
for "refinement". An orthonormal basis is generated.
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.
16 % The stopping of the extention is performed,
if
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
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'.
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
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
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)
74 % The following fields of detailed_data are created/extended:
75 % - RB : is extended by the number of basis-vectors, overall being the
77 % - RB_info : the fields of
this info-structure are extended, as described
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
87 % RB_detailed_val_savepath : path to the detailed data of the
88 % validation set in validation mode. Is generated
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
95 % additional fields as required by the selected
96 % extension algorithm and the reduced-basis-simulation must be set in model.
98 % Bernard Haasdonk 26.5.2007
100 if ~isfield(model,
'RB_stop_timeout')
101 model.RB_stop_timeout = inf;
104 if ~isfield(model,'RB_stop_Nmax')
105 model.RB_stop_Nmax = inf;
108 if ~isfield(model,'RB_stop_epsilon')
109 model.RB_stop_epsilon = 0;
112 if ~isfield(model,'RB_stop_max_val_train_ratio')
113 model.RB_stop_max_val_train_ratio = inf;
116 if ~isfield(model,'RB_save_temp_detailed_data')
117 model.RB_save_temp_detailed_data = 0;
120 % check, whether val-error is wanted
122 if model.RB_stop_max_val_train_ratio < inf
126 if isfield(model,'p_part_early_refinement')
127 enable_p_partition = 1;
129 enable_p_partition = 0;
133 if isfield(model,'h_part')
134 enable_h_partition = 1;
136 enable_h_partition = 0;
141 % make sure, that detailed-simulations for validation set are available
142 % in case of active validation criterion and wanted true 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!');
147 if isequal(model.RB_error_indicator,'error')
148 detailed_val_savepath = model.RB_detailed_val_savepath;
150 save_detailed_simulations_ptr(model,detailed_data, ...
151 detailed_data.RB_info.M_val, ...
152 detailed_val_savepath);
154 detailed_val_savepath = [];
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;
167 detailed_train_savepath = 'dummy-
string';
170 % initialize detailed_data.RB_info
171 if ~isfield(detailed_data,'RB_info')
172 detailed_data.RB_info = [];
175 N = model.get_rb_size(model,detailed_data);
177 if ~isfield(detailed_data.RB_info,'max_err_sequence')
178 detailed_data.RB_info.max_err_sequence = nan(1,N);
180 if ~isfield(detailed_data.RB_info,'mu_sequence')
181 detailed_data.RB_info.mu_sequence = ...
182 nan(length(model.mu_names), N);
184 if ~isfield(detailed_data.RB_info,'mu_ind_seq')
185 detailed_data.RB_info.mu_ind_seq = ...
189 if check_val_error && ~isfield(detailed_data.RB_info,'r_value_sequence')
190 detailed_data.RB_info.r_value_sequence = nan(1,N);
193 if ~isfield(detailed_data.RB_info,'toc_value_sequence')
194 detailed_data.RB_info.toc_value_sequence = nan(1,N);
197 if ~isfield(detailed_data.RB_info, 'N_sequence')
198 detailed_data.RB_info.N_sequence = [];
201 % (partially) retain info of previous runs
202 RB_info = detailed_data.RB_info;
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)
216 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
217 % initialize output quantities %
218 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
226 N_limit_est = [];% estimated amount of N needed
227 post_errs_sequence = [];
228 post_est_sequence = [];
230 % perform extension loop:
233 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
234 % determine mu-worst error-indicator for current training set %
235 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
237 reduced_data = gen_reduced_data(model,detailed_data);
239 model.N = reduced_data.N;
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;
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
260 % hp-greedy: first chosen mu is the first training parameter
261 if enable_h_partition && model.N == 0
265 mu = detailed_data.RB_info.M_train(:,mu_ind);
267 max_pe = [max_pe, max_err];
268 mu_ind_seq = [mu_ind_seq, mu_ind];
269 mu_values = [mu_values, mu];
271 % update mu-vector is stored in variable mu
272 disp(['Detected maximum error prediction ',num2str(max_err),...
273 ' for mu=[',num2str(mu'),']']);
275 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
276 % check loop-break conditions %
277 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
279 if max_err < model.RB_stop_epsilon
281 RB_info.stopped_on_epsilon = 1;
284 if model.get_rb_size(model,detailed_data) >= model.RB_stop_Nmax
286 RB_info.stopped_on_Nmax = 1;
289 if toc > model.RB_stop_timeout
290 RB_info.stopped_on_timeout = 1;
296 detailed_data,reduced_data,...
297 detailed_data.RB_info.M_val, ...
298 detailed_val_savepath);
300 r_values = [r_values, max(val_est) / max_err];
302 disp(['val_train ratio: ',num2str(r_values(end))]);
304 if (r_values(end) > model.RB_stop_max_val_train_ratio)
305 RB_info.stopped_on_max_val_train_ratio = 1;
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
316 if enable_p_partition && model.p_part_early_refinement
317 %estimate N total needed to reach etol:
319 etol = model.RB_stop_epsilon;
320 Nmax = model.RB_stop_Nmax;
322 s=20; %stepsize backwards
325 if(length(err_seq)>(s))
327 %for i=(s+1):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))];
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];
343 N_limit_est_av=N_limit_est(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;
354 end %of P-Part_early_refinement
356 % always store computational time
357 toc_values = [toc_values, toc];
359 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
360 % update information structure and possibly save intermediate state to file %
361 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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;
372 RB_info.r_value_sequence = [RB_info.r_value_sequence, r_values(end)];
374 RB_info.toc_value_sequence = [RB_info.toc_value_sequence, toc_values(end)];
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;
381 if numel(max_pe) == 1
382 RB_info.M_first_errs = post_errs;
385 detailed_data.RB_info = RB_info;
387 if model.RB_save_temp_detailed_data
388 save(fullfile(rbmatlabtemp,'temp_detailed_data.mat'),'detailed_data', ...
398 % Use original params with new mu such that filecaching works in the
401 %oldmu = model.get_mu(model);
402 %model = model.set_mu(model, mu);
403 %[RBext, par] = model.RB_extension_algorithm(model, ...
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);
411 % if empty: no further processing has to be performed
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));
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!');
429 detailed_data = model.set_rb_in_detailed_data(...
432 %detailed_data.RB = [detailed_data.RB, RBext];
433 N = model.get_rb_size(model,detailed_data);
435 disp(['Extended RB to length ', num2str(N)]);
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),...
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)))))]);
452 end % if ~isempty(RB)
454 % model.N is not set yet: quick-fix of problem with "empty extension" after
456 if model.N == model.get_rb_size(model, detailed_data)
457 RB_info.stopped_on_empty_extension = 1;
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;
467 % disp('stopped on error-increase! Please check!');
470 % stopped_on_error_increase = 0;
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);
479 end; % if continue_loop
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...
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])