1 function detailed_data = riccati_gen_detailed_data(model, model_data)
2 %
function detailed_data = riccati_gen_detailed_data(model, model_data)
4 %
function generates the reduced basis
5 % the result is stored in the detailed_data
7 % This
function generates the following data structure:
10 % RB_info Information about the basis
12 % Andreas Schmidt, 2015
18 if ~isfield(model,
'RB_generation_mode')
19 error(['Please specify model.RB_generation_mode '])
22 mode = model.RB_generation_mode;
23 dsim_mode = 'riccati';
26 case 'greedy_uniform_fixed'
27 if ~isfield(model, 'RB_numintervals')
28 error('Please specify model.RB_numintervals')
30 mu_ranges = model.mu_ranges;
31 par.numintervals = model.RB_numintervals*ones(length(mu_ranges),1);
32 par.range = model.mu_ranges;
34 training_mus = get(MMesh0, 'vertex');
37 if ~isfield(model, 'training')
38 error('Please specify model.training')
40 training_mus = model.training;
43 training_mus = rand_uniform(model.RB_samplepoints, model.mu_ranges)';
45 case 'greedy_rand_log'
46 training_mus = rand_log(model.RB_samplepoints, model.mu_ranges)';
51 training_mus = rand_uniform(model.RB_samplepoints, model.mu_ranges)';
54 error('Basis generation mode unknown')
56 detailed_data = algorithm(model, model_data, training_mus, dsim_mode);
62 %% First version:
Greedy for detailed system
63 function detailed_data = greedy(model, model_data, training_mus, varargin)
64 % Implementation of the greedy algorithm
65 RB_info.M_train = training_mus;
70 if isfield(model, 'RB_o_tol')
71 o_tol = model.RB_o_tol;
73 RB_info.o_tol = o_tol;
75 % Errors (dummy values set for initial calculation)
76 errors = ones(size(training_mus, 1), 1);
78 % The reduced basis will be stored here:
81 % History of the error decay over the whole parameter domain:
85 % Maximum error decay:
88 % The sizes of the low rank factors
91 % Number of inner iterations
92 inner_elements_added = [];
96 Z = cell(size(training_mus,1));
98 % For the detailed simulation time
106 % Adaptive algorithm or fixed-1 ?
107 basis_extension_mode = model.RB_extension_mode;
109 if strcmp(basis_extension_mode,
'addmultiple')
112 if isfield(model, 'RB_i_tol')
113 i_tol = model.RB_i_tol;
115 maxperstep = model.RB_extension_max_per_step;
116 RB_info.i_tol = i_tol;
118 RB_info.addmultiple = addmultiple;
121 while max(errors) > o_tol
122 display(['LRFG, Iteration ', num2str(i)])
125 % Use the first element in the training set for the
126 % initalization of the basis
130 % Select the worst approximated element
131 [maxErr, theta] = max(errors);
134 display([' - Element ', num2str(theta), ' with error ', num2str(maxErr)])
136 % Save the error decay
137 chosen_mu = [chosen_mu theta];
139 % Copy the model temporarily and perform a detailed simulation to
140 % get the new Low Rank factor Z and cache it in the cell Z
141 display(' - Getting detailed simulation')
143 mu = training_mus(theta,:);
144 tmpModel = set_mu(model, mu);
147 if strcmp(mode,
'riccati')
148 dsim = detailed_simulation(tmpModel, model_data);
149 elseif strcmp(mode, 'pod')
152 parfor l = 1:size(C,1)
154 options = odeset('Jacobian', A, 'Mass', E');
155 [T,Y] = ode23s( @(t,x) (A'*x), [0,5], E\c, options);
156 snapshots = [snapshots Y'];
160 error('Detailed data generation mode not known')
162 dsim_times = [dsim_times toc(tStart)];
168 Z_sizes = [Z_sizes size(thisZ,2)];
169 display([
' - Low rank factor has ', num2str(size(thisZ,2)),
' elements'])
171 z0 = norm(thisZ(:,1));
174 % Extend the basis, i.e. run the inner greedy loop
175 %[RB,c,ierror,newZ] = riccati_greedy_extend_basis(RB,thisZ,itol,z0);
177 thisZ = thisZ - RB*(RB'*thisZ);
181 [Vextend,S,~] = svds(thisZ,1);
183 [Vextend,S,~] = svd(thisZ,'econ');
187 for i = 1:length(diag(S))
188 E = 1 - sum(S(1:i))/sumS;
189 if E < i_tol || i == maxperstep
193 Vextend = Vextend(:, 1:i);
196 c = size(Vextend, 2);
197 ierror = max(diag(S));
199 % Just make sure the basis is orthogonal in cases where numerical
201 RB = orthonormalize_gram_schmidt([RB Vextend]);
203 inner_elements_added = [inner_elements_added c];
205 display([' - Added ', num2str(c), ' new basis vectors']);
206 display([' - New basis size: ', num2str(size(RB,2))]);
208 % Only recalculate if elements have been added
209 % If no elements were added the same error bound can be used again
211 display(' - Recalculating errors')
212 errors = greedy_calc_errors(model, RB, training_mus);
215 last_error = error_decay(end);
216 error_decay = [error_decay max(errors)];
217 error_history{i} = errors;
221 RB_info.basisgen_runtime = toc(bgenStart);
223 detailed_data.RB = RB;
225 % Start gamma procedure:
226 gamma_mode = model.RB_gamma_mode;
227 gamma.mode = gamma_mode;
229 % Check the mode
for gamma:
230 if strcmp(gamma_mode,
'max')
231 error('To implement')
232 elseif strcmp(gamma_mode, 'kernel_interpolation')
233 greedy_interpol_count = 100;
234 if isfield(model, 'RB_gamma_samplesize')
235 greedy_interpol_count = model.RB_gamma_samplesize;
237 mus = rand_uniform(greedy_interpol_count, model.mu_ranges)';
238 greedy_gammas = zeros(greedy_interpol_count, 1);
239 runtimes = zeros(greedy_interpol_count,1);
240 parfor_progress(size(mus,1));
241 parfor i = 1:size(mus, 1)
243 tmpModel = set_mu(model, mu);
244 dsim = detailed_simulation(tmpModel, model_data);
246 greedy_gammas(i) =
riccati_gamma(tmpModel, model_data, dsim);
247 runtimes(i) = toc(gamma_timing);
251 gamma.interpol_mus = mus;
252 gamma.interpol_gammas = greedy_gammas;
253 gamma.kernel = 'gauss';
254 gamma.runtimes = runtimes;
255 elseif strcmp(gamma_mode, 'interpol')
256 detailed_data.gamma_mode = 'interpol';
257 display('Starting calculation of gamma values')
261 ranges = model.mu_ranges;
263 mug = cell(1,length(ranges));
264 for j = 1:length(ranges)
265 mug{j} = linspace(ranges{j}(1), ranges{j}(2), Nmu);
267 mu_grid = cell(1,numel(mug));
269 [mu_grid{:}] = ndgrid(mug{:});
270 gamma_values = zeros(size(mu_grid{1}));
272 reduced_data = gen_reduced_data(model, detailed_data);
274 parfor k = 1:numel(mu_grid{1})
275 mu = zeros(length(ranges),1);
276 for j = 1:length(ranges)
277 mu(j) = mu_grid{j}(k);
280 tmpModel = set_mu(model, mu);
281 sim = rb_reconstruction(tmpModel, detailed_data, rb_simulation(tmpModel, reduced_data));
288 gamma.values = gamma_values;
289 gamma.mu_grid = mu_grid;
290 gamma.interpolant = griddedInterpolant( mu_grid{:}, gamma_values,
'linear' );
292 detailed_data.gamma = gamma;
293 RB_info.gamma_runtime = toc(gamma_timer);
295 % Fill the structure with all information from the basis building
297 inner.Z_sizes = Z_sizes;
298 inner.iterations = inner_elements_added;
299 RB_info.inner = inner;
301 outer.error_decay = error_decay;
302 outer.chosen_mu = chosen_mu;
304 outer.dsim_timings = dsim_times;
305 RB_info.outer = outer;
307 detailed_data.RB_info = RB_info;
310 % Calculate the errors
for the basis stored in the matrix RB
311 function [errs, avg_time] = greedy_calc_errors(model, RB, training_mus)
312 errs = zeros(size(training_mus,1),1);
315 rd = gen_reduced_data(model, dd);
317 parfor i = 1:size(training_mus,1)
319 tmpModel = set_mu(tmpModel, training_mus(i, :));
321 rsim = rb_simulation(tmpModel, rd);
322 errs(i) = rsim.P_residual_normalized;
A hierarchical cubegrid of arbitrary dimension.
function [ E , A , B , C , x0 ] = riccati_assemble_system_matrices(model, model_data)
model_data)
function percent = parfor_progress(N)
PARFOR_PROGRESS Progress monitor (progress bar) that works with parfor. PARFOR_PROGRESS works by crea...
function gamma = riccati_gamma(model, model_data, sim)
This function calculates gamma. Simdata must be a high dimensional solution.
Customizable implementation of an abstract greedy algorithm.