4 % step 1: detailed simulation with different parameters
5 % step 2: reduced basis simulation
7 % step 4: time measurements comparing OO model with old model.
9 % B. Haasdonk 27.2.2011
12 params.numintervals_per_block = 5;
20 case 1 % different detailed simulations
22 disp(
'detailed simulation:');
27 model_data = gen_model_data(dmodel);
28 dmodel = set_mu(dmodel,[1,1,1,1,1,1,1,1,1]);
30 sim_data = detailed_simulation(dmodel,model_data);
31 plot_params.title = [
'detailed solution , \mu_i=1, output s=',...
33 figure,
plot_sim_data(dmodel,model_data,sim_data,plot_params);
34 dmodel = set_mu(dmodel,[0.1,0.1,1,1,0.1,0.1,1,0.1,1]);
36 sim_data = detailed_simulation(dmodel,model_data);
37 plot_params.title = [
'detailed solution , \mu_i=0.1/1, output s=',...
39 figure,
plot_sim_data(dmodel,model_data,sim_data,plot_params);
41 case 2 % rb treatment and comparison to detailed
43 %
for non-snapshot parameter nice error behaviour is observed
44 %
for snapshot parameter error is numerically zero, but
45 % estimators due to numerical noise not zero.
47 disp(
'detailed simulation:');
52 model_data = gen_model_data(dmodel);
53 % non-snapshot parameter, error <= Delta
54 dmodel = set_mu(dmodel,[0.5,1,1,1,1,1,1,1,1]);
55 % non-snapshot parameter with excellent Delta==error!!!
56 %model = model.set_mu(model,[1,1,1,1,1,1,1,1,1]);
57 % snapshot parameter with error, Delta approximately 0.
58 %model = model.set_mu(model,[0.1,1,1,1,1,1,1,1,1]);
59 sim_data = detailed_simulation(dmodel,model_data);
60 sim_data.mu = get_mu(dmodel);
61 plot_params.title =
'detailed solution';
62 disp([
'output s(mu) = ',num2str(sim_data.s)]);
63 figure,
plot_sim_data(dmodel,model_data,sim_data,plot_params);
64 disp(
'offline computations I (gen_detailed_data: basis):');
65 detailed_data = gen_detailed_data(rmodel,model_data);
66 disp(
'offline computations II (gen_reduced_data: operator components):');
67 reduced_data = gen_reduced_data(rmodel,detailed_data);
69 % reduced_data = model.reduced_data_subset(model,reduced_data);
71 disp(
'online reduced simulation:');
72 rmodel = set_mu(rmodel, sim_data.mu);
73 rb_sim_data = rb_simulation(rmodel,reduced_data);
74 rb_sim_data = rb_reconstruction(rmodel, detailed_data, rb_sim_data);
75 plot_params.title =
'reduced solution';
76 figure,
plot_sim_data(rmodel,model_data,rb_sim_data,plot_params);
77 disp([
'output sN(mu) = ',num2str(rb_sim_data.s)]);
79 disp(
'-------------------------------------------');
80 eh = sim_data.uh - rb_sim_data.uh;
81 l2_err = fem_l2_norm(eh);
82 h10_err = fem_h10_norm(eh);
83 disp([
'L2-error: ',num2str(l2_err)]);
84 disp([
'H10-error: ',num2str(h10_err)]);
85 disp([
'error s(mu)-sN(mu) = ',num2str(sim_data.s-rb_sim_data.s)]);
86 disp([
'error estimator Delta_u(mu) = ',num2str(rb_sim_data.Delta)]);
87 disp([
'error estimator Delta_s(mu) = ',num2str(rb_sim_data.Delta_s)]);
90 save(fullfile(rbmatlabresult,
'thermal_block_offline_data.mat'),...
91 'dmodel',
'detailed_data');
95 fn = fullfile(rbmatlabresult,
'thermal_block_offline_data.mat');
96 if ~exist(fn) % call step 2 first
97 disp(
'calling thermalblock(2) for basis generation')
105 %dmodel = thermalblock_model;
107 rmodel.N = size(detailed_data.RB,2);
108 %model_data = gen_model_data(dmodel);
109 %detailed_data = gen_detailed_data(dmodel,model_data);
110 plot_params.axis_tight = 1;
111 plot_params.yscale_uicontrols = 0.5;
115 fn=fullfile(rbmatlabresult, 'thermal_block_offline_data.mat');
117 detailed_data = tmp.detailed_data;
118 descr = thermalblock_model(params);
120 reduced_data = gen_reduced_data(rmodel, detailed_data);
122 pr.init_sample(rmodel);
126 rtimes = zeros(nmus, 1);
127 dtimes = zeros(nmus, 1);
128 errs = zeros(nmus, 1);
130 rmodel = set_mu(rmodel, M(i,:));
135 rbs = rb_simulation(rmodel, reduced_data);
137 rrbs = rb_reconstruction(rmodel, detailed_data, rbs);
138 rntelapsed = rntelapsed + ttemp;
140 dbs = detailed_simulation(dmodel, detailed_data.model_data);
142 dntelapsed = dntelapsed + ttemp;
144 rtimes(i) = rntelapsed/10;
145 dtimes(i) = dntelapsed/10;
146 errs(i) = fem_l2_norm(rrbs.uh - dbs.uh);
149 save(fullfile(rbmatlabresult, 'thermalblock_timings'), 'errs', 'rtimes', 'dtimes');
152 case 4 % compare with old version
154 disp(['This step will only work after providing nice descr for' ...
158 omodel = thermalblock_model_old;
159 omodel_data = gen_model_data(omodel);
160 odetailed_data = gen_detailed_data(omodel, omodel_data);
161 oreduced_data = gen_reduced_data(omodel, odetailed_data);
163 pr.init_sample(omodel);
168 omodel = omodel.set_mu(omodel, M(i,:));
171 rb_simulation(omodel, oreduced_data);
173 otelapsed = otelapsed + ttemp;
176 disp(num2str(otelapsed));
178 descr = thermalblock_model;
180 model_data = gen_model_data(dmodel);
181 detailed_data = gen_detailed_data(rmodel, model_data);
182 reduced_data = gen_reduced_data(rmodel, detailed_data);
186 descr = omodel.set_mu(descr, M(i,:));
187 rmodel = set_mu(rmodel, M(i,:));
190 rb_simulation(rmodel, reduced_data);
192 ntelapsed = ntelapsed + ttemp;
195 disp(num2str(ntelapsed));
199 descr = omodel.set_mu(descr, M(i,:));
200 rmodel = set_mu(rmodel, M(i,:));
201 descr = rmodel.descr;
204 omodel.rb_simulation(descr, reduced_data);
206 ntelapsed2 = ntelapsed2 + ttemp;
209 disp(num2str(ntelapsed2));
211 rd = struct(reduced_data);
213 descr = omodel.set_mu(descr, M(i,:));
214 rmodel = set_mu(rmodel, M(i,:));
217 rb_simulation(rmodel, rd);
219 ntelapsed3 = ntelapsed3 + ttemp;
222 disp(num2str(ntelapsed3));
226 % compute a bunch of error estimates and true errors...
227 fn = fullfile(rbmatlabresult,'thermal_block_offline_data.mat');
228 if ~exist(fn, 'file') % call step 2 first
229 disp('calling thermalblock(2) for basis generation')
233 descr = thermalblock_model;
235 model_data = gen_model_data(dmodel);
238 reduced_data = gen_reduced_data(rmodel, detailed_data);
244 ps.init_sample(rmodel);
246 deltas = zeros(1, nr);
247 residuals = zeros(1, nr);
249 h=waitbar(0,'Please wait..');
251 set_mu(rmodel, ps.sample(i, :));
252 set_mu(dmodel, ps.sample(i, :));
253 sim_data = detailed_simulation(dmodel, model_data);
254 rb_sim_data = rb_simulation(rmodel, reduced_data);
255 rb_sim_data = rb_reconstruction(rmodel, detailed_data, rb_sim_data);
259 deltas(i) = rb_sim_data.Delta;
260 residuals(i) = rb_sim_data.res_norm;
261 df = sim_data.uh - rb_sim_data.uh;
262 errs(i) = fem_l2_norm(df);
266 save(fullfile(rbmatlabresult, 'deltas_and_errs'), 'deltas', 'errs', 'residuals');
269 dfn = fullfile(rbmatlabresult,'thermal_block_offline_data.mat');
270 fn = fullfile(rbmatlabresult, 'deltas_and_errs.mat');
271 if ~exist(fn, 'file') % call step 5 first
272 disp('calling thermalblock(5) for estimator generation')
276 descr = thermalblock_model;
278 model_data = gen_model_data(dmodel);
280 detailed_data = tmp.detailed_data;
285 residuals = tmp.residuals;
288 % plot estimates and residuals against "true" errors
290 plot(deltas, errs, 'g*', residuals, errs, 'b+'); set(gca, 'XScale', 'log');
291 legend('estimates', 'residuals');
294 plot3(deltas, residuals, errs, 'g*');
295 xlabel('estimates'); ylabel('residuals'); zlabel('errors');
297 % register UQ classes
298 cd ~/workspace/uq_rb_test/
302 gInvFunc = @(X) 10.^X;
303 hFunc = @(X) log10(X);
304 hInvFunc = @(X) 10.^(X);
305 gPlotFunc = gInvFunc;
306 hPlotFunc = hInvFunc;
308 % models for Gaussian processes
309 deltaModel = uqrb.base.ErrorModel(gFunc(deltas'), hFunc(errs'));
310 resModel = uqrb.base.ErrorModel(gFunc(residuals'), hFunc(errs'));
311 combiModel = uqrb.base.ErrorModel([gFunc(residuals'),gFunc(deltas')], hFunc(errs'));
312 N(1) = 100; N(2) = 400;
314 deltaXns = cell(2,1);
316 % dbasis = cell(2,1);
317 % rbasis = cell(2,1);
322 deltaXns{i} = deltaModel.samplePoints(N(i));
323 resXns{i} = resModel.samplePoints(N(i));
324 %dbasis{i} = uqrb.rvm.basisFunction.Radial(deltaXns{i}, [], deltaModel.domain);
325 %rbasis{i} = uqrb.rvm.basisFunction.Radial(resXns{i}, [], resModel.domain);
326 dpb{i} = uqrb.gp.kernel.Gaussian();
327 % dpb{i} = uqrb.rvm.basisFunction.Polynomial(M, deltaModel.domain);
328 rpb{i} = uqrb.rvm.basisFunction.Polynomial(M, resModel.domain);
329 cpb{i} = uqrb.rvm.basisFunction.Mixed(4, combiModel.domain);
333 % GPs = [ GPs, {uqrb.rvm.Inference(cpb{1}, combiModel)} ];
334 GPs = [ GPs, {uqrb.gp.Inference(dpb{1}, deltaModel)} ];
335 GPs = [ GPs, {uqrb.rvm.Inference(rpb{1}, deltaModel)} ];
336 %GPs{1,1} = uqrb.rvm.Inference(dpb{1}, deltaModel);
337 %GPs = [ GPs, {uqrb.rvm.Inference(rpb{1}, deltaModel)} ];
338 %GPs = [ GPs, {uqrb.rvm.Inference(rpb{2}, deltaModel)} ];
339 %GPs{1,4} = uqrb.rvm.Inference(dpb{2}, deltaModel);
340 GPs = [ GPs, {uqrb.gp.Inference(dpb{2}, deltaModel)} ];
341 GPs = [ GPs, {uqrb.rvm.Inference(rpb{2}, resModel)} ];
342 % GPs = [ GPs, {uqrb.rvm.Inference(cpb{2}, combiModel)} ];
343 %GPs = [ GPs, {uqrb.rvm.Inference(rpb{1}, resModel)} ];
344 %GPs = [ GPs, {uqrb.rvm.Inference(rpb{2}, resModel)} ];
345 %GPs = [ GPs, {uqrb.rvm.Inference(rpb{2}, resModel)} ];
347 % declare variables
for tikz plots
348 overviewTitle = {
'filename',
'abscissa',
'Mstart',
'M',
'sigma',
'basistype'};
349 overviewData = cell(6, numel(GPs));
350 overviewFn = fullfile(rbmatlabresult,
'overview.txt');
351 validationTitle = {
'vxns',
'muCurve',
'Sigma1',
'Sigma2',
'Sigma3',
'Sigma4',
'vns'};
359 dim = gp.model.dimension;
360 if gp.model == deltaModel
361 abscissa =
'estimates';
362 elseif gp.model == resModel
363 abscissa =
'residual';
364 elseif gp.model == combiModel
365 abscissa =
'combined';
368 stemFn = fullfile(rbmatlabresult, [
'plots_', num2str(i)]);
369 trainingFn = [stemFn,
'_training.dat'];
371 gp.hyperMu.sigma2 = 1e-1;
372 % disp(num2str(gp.basis.M));
374 [xns, tns] = gp.model.evaluateAtSamplePoints(1000);
375 gp.model.domain = [ min(xns)-0.5; max(xns)+0.5 ]
';
376 % dwidth = gp.model.domain(2) - gp.model.domain(1);
377 % vxns = xns(n+1:1000);
378 % [vxns, si] = sort(vxns);
380 vxns = gp.model.uniformSamplePoints(NSP(dim));
381 % vxns = ((gp.model.domain(1) + 0.5):dwidth/200:(gp.model.domain(2) - 0.5))';
383 % vns = tns(n+1:1000);
384 vns = zeros(size(vxns,1),1);
388 trainingTitle = [arrayfun(@(X) sprintf(
'xns_%i', X), 1:dim,
'UniformOutput',
false), {
'tns'}];
389 trainingData = zeros(dim+1, size(xns,1));
390 trainingData(1:dim,:) = xns
';
391 trainingData(2,:) = tns;
393 print_datatable(trainingFn, trainingTitle, trainingData);
395 validationData = zeros(5, size(vxns,1));
397 validationFn = [stemFn, '_rvm_validation.dat
'];
402 if isa(gp, 'uqrb.rvm.Inference
')
404 if isa(gp.basis, 'uqrb.rvm.basisFunction.Polynomial
')
405 disp(num2str(gp.basis.degrees));
406 gp.alphaDiscardThreshold = 1e4;
407 gp.stop_inferenceConvergenceRate = 1e-3;
410 % [alpGp{i,j}, s2s{i,j}] = gp.inferenceProcedure(tns);
411 gp.inferHyperparameters(xns, tns);
412 if isa(gp, 'uqrb.rvm.Inference
') && isa(gp.basis, 'uqrb.rvm.baseFunction.Polynomial
')
413 disp(num2str(gp.basis.degrees));
415 if isa(gp, 'uqrb.gp.Inference
')
416 disp(num2str(gp.hyperMu.sigma2));
417 disp(num2str(gp.hyperMu.radius));
418 % gp.hyperMu.radius = 0.1;
420 %gp.alpha = alpGp{i,j};
422 % gp.sigma2estimate = 20;
423 % gp.sigma2estimate = s2s{i,j};
424 % if isa(gp.basis, 'uqrb.rvm.basisFunction.Radial
')
425 % gp.basis.radius = 0.9;
426 % gp.basis.xms = xns;
428 p1 = gp.inferPosterior(xns, tns);
429 g1 = p1.predict(vxns);
431 % muCurve = gp.predictForW(vxns,mu);
432 % bfs = gp.regressionBasis.baseFunctionEval(vxns);
435 [confX, confY, stddev] = g1.confidencePatch(0.95);
437 g2.Sigma = g1.Sigma + gp.hyperMu.sigma2*speye(size(g2.Sigma));
438 [confX2, confY2, stddev2] = g2.confidencePatch(0.95);
440 methodname = class(gp);
441 if isa(gp, 'uqrb.rvm.Inference
')
442 methodname = class(gp.basis);
445 plot(xns, tns, ['*
', 'g
']);
447 plot(vxns, g1.mu, ['-
', 'k
']);
449 h=patch(vxns(confX2), confY2, ...
450 [0.9,0.9,0.9], 'EdgeColor
', [0.7,0.7,0.7]);
453 h=patch(vxns(confX), confY, ...
454 [0.7,0.7,0.7], 'EdgeColor
', [0.6,0.6,0.6]);
456 % set(gca, 'XScale
', 'log
');
457 title(['k =
', num2str(n), ' abscissa:
', abscissa, ' bf:
', methodname]);
459 % [stdCurve1, stdCurve2] = gp.plot(xns, tns, vxns, [], p1.mu, p1.Sigma, gPlotFunc, hPlotFunc);
460 plot(gPlotFunc(xns), hPlotFunc(tns), ['*
', 'g
']);
462 plot(gPlotFunc(vxns), hPlotFunc(g1.mu), ['-
', 'k
']);
464 h=patch(gPlotFunc(vxns(confX2)), hPlotFunc(confY2), ...
465 [0.9,0.9,0.9], 'EdgeColor
', [0.7,0.7,0.7]);
467 h=patch(gPlotFunc(vxns(confX)), hPlotFunc(confY), ...
468 [0.7,0.7,0.7], 'EdgeColor
', [0.6,0.6,0.6]);
470 % set(gca, 'YScale
', 'log
');
471 % set(gca, 'XScale
', 'log
');
472 title(['k =
', num2str(n), ' abscissa:
', abscissa, ' bf:
', methodname]);
475 % stddev = sqrt(diag(bfs*(Sigma)*bfs'));
476 stdCurve1 = g1.mu + stddev;
477 stdCurve2 = g1.mu - stddev;
478 stdCurve3 = g1.mu + stddev2;
479 stdCurve4 = g1.mu - stddev2;
481 validationData(1,:) = vxns;
482 validationData(2,:) = g1.mu;
483 validationData(3,:) = stdCurve1;
484 validationData(4,:) = stdCurve2;
485 validationData(5,:) = stdCurve3;
486 validationData(6,:) = stdCurve4;
488 validationData(7,:) = vns;
489 print_datatable(validationFn, validationTitle, validationData);
490 overviewData{1,counter} = validationFn;
491 overviewData{2,counter} = abscissa;
492 overviewData{3,counter} = Mstart;
493 overviewData{4,counter} = p1.getDimension();
494 overviewData{5,counter} = gp.hyperMu.sigma2;
495 overviewData{6,counter} = methodname;
496 counter = counter + 1;
500 print_datatable(overviewFn, overviewTitle, overviewData);
function [ dmodel , rmodel ] = gen_models(ModelDescr descr,BasisGenDescr bg_descr)
generates an IDetailedModel and an IReducedModel instance from description structures.
function demo_rb_gui(varargin)
reduced basis demo with sliders
function dmodel = gen_detailed_model(ModelDescr descr)
generates an IDetailedModel instance from a description structure.
function p = plot_sim_data(model, model_data, sim_data, plot_params)
function performing the plot of the simulation results as specified in model.
function model = thermalblock_model(params)
Thermal Block example similar as described in the book of A.T. patera and G. Rozza (just one paramete...
function res = thermalblock(step, params)
thermalblock example