rbmatlab  1.16.09
 All Classes Namespaces Files Functions Variables Modules Pages
thermalblock.m
Go to the documentation of this file.
1 function res = thermalblock(step, params)
2 % thermalblock example
3 %
4 % step 1: detailed simulation with different parameters
5 % step 2: reduced basis simulation
6 % step 3: demo_rb_gui
7 % step 4: time measurements comparing OO model with old model.
8 
9 % B. Haasdonk 27.2.2011
10 
11 if nargin <= 1
12  params.numintervals_per_block = 5;
13 end
14 
15 if nargin<1
16  step = 1;
17 end;
18 
19 switch step
20  case 1 % different detailed simulations
21 
22  disp('detailed simulation:');
23  descr = thermalblock_model(params);
24  dmodel = gen_detailed_model(descr);
25  % dmodel = thermalblock_model;
26 
27  model_data = gen_model_data(dmodel);
28  dmodel = set_mu(dmodel,[1,1,1,1,1,1,1,1,1]);
29 
30  sim_data = detailed_simulation(dmodel,model_data);
31  plot_params.title = ['detailed solution , \mu_i=1, output s=',...
32  num2str(sim_data.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]);
35 
36  sim_data = detailed_simulation(dmodel,model_data);
37  plot_params.title = ['detailed solution , \mu_i=0.1/1, output s=',...
38  num2str(sim_data.s)];
39  figure, plot_sim_data(dmodel,model_data,sim_data,plot_params);
40 
41  case 2 % rb treatment and comparison to detailed
42 
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.
46 
47  disp('detailed simulation:');
48  descr = thermalblock_model(params);
49  [dmodel, rmodel] = gen_models(descr);
50 % dmodel = thermalblock_model;
51 % rmodel = dmodel;
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);
68 % model.N = 1;
69 % reduced_data = model.reduced_data_subset(model,reduced_data);
70 
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)]);
78 
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)]);
88 
89  % save detailed_data for demo_rb_gui:
90  save(fullfile(rbmatlabresult,'thermal_block_offline_data.mat'),...
91  'dmodel','detailed_data');
92 
93  case 3 % demo_rb_gui
94 
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')
98  thermalblock(2);
99  end;
100 
101  disp('demo_rb_gui:')
102  descr = thermalblock_model;
103  [dmodel, rmodel] = gen_models(descr);
104  load(fn);
105  %dmodel = thermalblock_model;
106  %rmodel = dmodel;
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;
112  demo_rb_gui(rmodel,detailed_data,[],plot_params);
113 
114  case 3.5
115  fn=fullfile(rbmatlabresult, 'thermal_block_offline_data.mat');
116  tmp = load(fn);
117  detailed_data = tmp.detailed_data;
118  descr = thermalblock_model(params);
119  [dmodel, rmodel] = gen_models(descr);
120  reduced_data = gen_reduced_data(rmodel, detailed_data);
121  pr = ParameterSampling.Random(100);
122  pr.init_sample(rmodel);
123  M = pr.sample;
124 
125  nmus = size(M, 1);
126  rtimes = zeros(nmus, 1);
127  dtimes = zeros(nmus, 1);
128  errs = zeros(nmus, 1);
129  for i = 1:size(M,1)
130  rmodel = set_mu(rmodel, M(i,:));
131  rntelapsed = 0;
132  dntelapsed = 0;
133  for j = 1:10
134  tic;
135  rbs = rb_simulation(rmodel, reduced_data);
136  ttemp = toc;
137  rrbs = rb_reconstruction(rmodel, detailed_data, rbs);
138  rntelapsed = rntelapsed + ttemp;
139  tic;
140  dbs = detailed_simulation(dmodel, detailed_data.model_data);
141  ttemp = toc;
142  dntelapsed = dntelapsed + ttemp;
143  end
144  rtimes(i) = rntelapsed/10;
145  dtimes(i) = dntelapsed/10;
146  errs(i) = fem_l2_norm(rrbs.uh - dbs.uh);
147  end
148 
149  save(fullfile(rbmatlabresult, 'thermalblock_timings'), 'errs', 'rtimes', 'dtimes');
150 
151 
152  case 4 % compare with old version
153 
154  disp(['This step will only work after providing nice descr for' ...
155  'thermalblock!']);
156  return
157 
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);
162  pr = ParameterSampling.Random(100);
163  pr.init_sample(omodel);
164  M = pr.sample;
165 
166  otelapsed = 0;
167  for i = 1:size(M,2)
168  omodel = omodel.set_mu(omodel, M(i,:));
169  for j = 1:10
170  tic;
171  rb_simulation(omodel, oreduced_data);
172  ttemp = toc;
173  otelapsed = otelapsed + ttemp;
174  end
175  end
176  disp(num2str(otelapsed));
177 
178  descr = thermalblock_model;
179  [dmodel,rmodel] = gen_models(descr);
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);
183 
184  ntelapsed = 0;
185  for i = 1:size(M,2)
186  descr = omodel.set_mu(descr, M(i,:));
187  rmodel = set_mu(rmodel, M(i,:));
188  for j = 1:10
189  tic;
190  rb_simulation(rmodel, reduced_data);
191  ttemp = toc;
192  ntelapsed = ntelapsed + ttemp;
193  end
194  end
195  disp(num2str(ntelapsed));
196 
197  ntelapsed2 = 0;
198  for i = 1:size(M,2)
199  descr = omodel.set_mu(descr, M(i,:));
200  rmodel = set_mu(rmodel, M(i,:));
201  descr = rmodel.descr;
202  for j = 1:10
203  tic;
204  omodel.rb_simulation(descr, reduced_data);
205  ttemp = toc;
206  ntelapsed2 = ntelapsed2 + ttemp;
207  end
208  end
209  disp(num2str(ntelapsed2));
210  ntelapsed3 = 0;
211  rd = struct(reduced_data);
212  for i = 1:size(M,2)
213  descr = omodel.set_mu(descr, M(i,:));
214  rmodel = set_mu(rmodel, M(i,:));
215  for j = 1:10
216  tic;
217  rb_simulation(rmodel, rd);
218  ttemp = toc;
219  ntelapsed3 = ntelapsed3 + ttemp;
220  end
221  end
222  disp(num2str(ntelapsed3));
223 
224 case 5
225 
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')
230  thermalblock(2);
231  end;
232 
233  descr = thermalblock_model;
234  [dmodel, rmodel] = gen_models(descr);
235  model_data = gen_model_data(dmodel);
236  load(fn);
237 
238  reduced_data = gen_reduced_data(rmodel, detailed_data);
239 
240  nr = 1000;
241 
242  ps=ParameterSampling.Random(nr);
243 
244  ps.init_sample(rmodel);
245 
246  deltas = zeros(1, nr);
247  residuals = zeros(1, nr);
248  errs = zeros(1, nr);
249  h=waitbar(0,'Please wait..');
250  for i=1:nr
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);
256 
257  waitbar(i/nr);
258 
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);
263  end
264  close(h);
265 
266  save(fullfile(rbmatlabresult, 'deltas_and_errs'), 'deltas', 'errs', 'residuals');
267 
268 case 6
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')
273  thermalblock(5);
274  end
275 
276  descr = thermalblock_model;
277  [dmodel, rmodel] = gen_models(descr);
278  model_data = gen_model_data(dmodel);
279  tmp = load(dfn);
280  detailed_data = tmp.detailed_data;
281 
282  tmp = load(fn);
283  deltas = tmp.deltas;
284  errs = tmp.errs;
285  residuals = tmp.residuals;
286 
287  figure
288  % plot estimates and residuals against "true" errors
289  subplot(2,1,1);
290  plot(deltas, errs, 'g*', residuals, errs, 'b+'); set(gca, 'XScale', 'log');
291  legend('estimates', 'residuals');
292  ylabel('error');
293  subplot(2,1,2);
294  plot3(deltas, residuals, errs, 'g*');
295  xlabel('estimates'); ylabel('residuals'); zlabel('errors');
296 
297  % register UQ classes
298  cd ~/workspace/uq_rb_test/
299  startup
300 
301  gFunc = @log10;
302  gInvFunc = @(X) 10.^X;
303  hFunc = @(X) log10(X);
304  hInvFunc = @(X) 10.^(X);
305  gPlotFunc = gInvFunc;
306  hPlotFunc = hInvFunc;
307 
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;
313  M = 2;
314  deltaXns = cell(2,1);
315  resXns = cell(2,1);
316 % dbasis = cell(2,1);
317 % rbasis = cell(2,1);
318  dpb = cell(2,1);
319  rpb = cell(2,1);
320  cpb = cell(2,1);
321  for i=1:2
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);
330  end
331 % keyboard
332  GPs = {};
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)} ];
346 
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'};
352 
353  counter = 1;
354 % alpGp = cell(2,4);
355 % s2s = cell(2,4);
356 
357  for i=1:length(GPs)
358  gp = GPs{i};
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';
366  end
367 
368  stemFn = fullfile(rbmatlabresult, ['plots_', num2str(i)]);
369  trainingFn = [stemFn, '_training.dat'];
370 
371  gp.hyperMu.sigma2 = 1e-1;
372  % disp(num2str(gp.basis.M));
373  n = N(1 + (i>2));
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);
379  NSP = [200, 20];
380  vxns = gp.model.uniformSamplePoints(NSP(dim));
381 % vxns = ((gp.model.domain(1) + 0.5):dwidth/200:(gp.model.domain(2) - 0.5))';
382  xns = xns(1:n,:);
383  % vns = tns(n+1:1000);
384  vns = zeros(size(vxns,1),1);
385  % vns = vns(si);
386  tns = tns(1:n);
387 
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;
392 % keyboard
393  print_datatable(trainingFn, trainingTitle, trainingData);
394 
395  validationData = zeros(5, size(vxns,1));
396 
397  validationFn = [stemFn, '_rvm_validation.dat'];
398 
399  % keyboard
400 
401  Mstart = 0;
402  if isa(gp, 'uqrb.rvm.Inference')
403  Mstart = gp.basis.M;
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;
408  end
409  end
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));
414  end
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;
419  end
420  %gp.alpha = alpGp{i,j};
421 
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;
427  % end
428  p1 = gp.inferPosterior(xns, tns);
429  g1 = p1.predict(vxns);
430 
431  % muCurve = gp.predictForW(vxns,mu);
432  % bfs = gp.regressionBasis.baseFunctionEval(vxns);
433  figure
434  subplot(2,1,1);
435  [confX, confY, stddev] = g1.confidencePatch(0.95);
436  g2=g1.copy();
437  g2.Sigma = g1.Sigma + gp.hyperMu.sigma2*speye(size(g2.Sigma));
438  [confX2, confY2, stddev2] = g2.confidencePatch(0.95);
439 
440  methodname = class(gp);
441  if isa(gp, 'uqrb.rvm.Inference')
442  methodname = class(gp.basis);
443  end
444  if dim == 1
445  plot(xns, tns, ['*', 'g']);
446  hold on
447  plot(vxns, g1.mu, ['-', 'k']);
448  hold on
449  h=patch(vxns(confX2), confY2, ...
450  [0.9,0.9,0.9], 'EdgeColor', [0.7,0.7,0.7]);
451  alpha(h,0.25);
452 
453  h=patch(vxns(confX), confY, ...
454  [0.7,0.7,0.7], 'EdgeColor', [0.6,0.6,0.6]);
455  alpha(h,0.25);
456 % set(gca, 'XScale', 'log');
457  title(['k = ', num2str(n), ' abscissa: ', abscissa, ' bf: ', methodname]);
458  subplot(2,1,2);
459  % [stdCurve1, stdCurve2] = gp.plot(xns, tns, vxns, [], p1.mu, p1.Sigma, gPlotFunc, hPlotFunc);
460  plot(gPlotFunc(xns), hPlotFunc(tns), ['*', 'g']);
461  hold on
462  plot(gPlotFunc(vxns), hPlotFunc(g1.mu), ['-', 'k']);
463  hold on
464  h=patch(gPlotFunc(vxns(confX2)), hPlotFunc(confY2), ...
465  [0.9,0.9,0.9], 'EdgeColor', [0.7,0.7,0.7]);
466  alpha(h,0.25);
467  h=patch(gPlotFunc(vxns(confX)), hPlotFunc(confY), ...
468  [0.7,0.7,0.7], 'EdgeColor', [0.6,0.6,0.6]);
469  alpha(h,0.25);
470 % set(gca, 'YScale', 'log');
471 % set(gca, 'XScale', 'log');
472  title(['k = ', num2str(n), ' abscissa: ', abscissa, ' bf: ', methodname]);
473  end
474 
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;
480 
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;
487 
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;
497  % keyboard;
498 
499  end
500  print_datatable(overviewFn, overviewTitle, overviewData);
501 
502 end
503 
504 end
505 
function [ dmodel , rmodel ] = gen_models(ModelDescr descr,BasisGenDescr bg_descr)
generates an IDetailedModel and an IReducedModel instance from description structures.
Definition: gen_models.m:17
function demo_rb_gui(varargin)
reduced basis demo with sliders
Definition: demo_rb_gui.m:17
Parameter sampling sets.
Definition: Interface.m:1
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.
Definition: plot_sim_data.m:17
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
Definition: thermalblock.m:17