rbmatlab  1.13.10
 All Classes Namespaces Files Functions Variables Groups Pages
follicle_experiments.m
Go to the documentation of this file.
1 function res = follicle_experiments(step,model,model_data,detailed_data)
2 %function follicle_experiments(step[,model,model_data,detailed_data])
3 % experiments with the model of the human follicle growth
4 %
5 % model range is n=200, noutput = 50, nt = 500, time = 19.02 years
6 % implicit Euler time-discretization, accept relatively large error at
7 % initial time, for large overall time interval and few timesteps
8 % and "interactivity"
9 %
10 % (step = 1 simulation and visualization of detailed dynamical
11 % system, using filecaching => do not call directly)
12 % step = 1.1 simulation and visualization of detailed dynamical
13 % system => n=20,n_output=20 seems too small
14 % step = 1.2 simulation and visualization of detailed dynamical
15 % system => n=50, n_output=20 seems better
16 % step = 1.3 simulation and visualization of detailed dynamical
17 % system => n=50, n_output=50 seems better, no big
18 % difference to 1.2
19 % step = 1.4 simulation and visualization of detailed dynamical
20 % system => n=70, n_output=50 no big difference to
21 % 1.2, 1.3
22 % step = 1.5 simulation and visualization of detailed dynamical
23 % system => n=200, n_output=50 seems better
24 % takes about 2 minutes. Results saved for step 1.6
25 % parameter variation explicit by setting mu.
26 % step 1.6: check influence of different nt on accuracy
27 % => relative error to nt=1000 pointwise <= 4% so OK for me
28 % (step = 1.7
29 % load precomputed example for range = 200
30 % show second heap around 70x70 by suitable color
31 % scaling. Generate Movie of data => Does not yet work!!!)
32 % NEW: step = 1.8, n=200, n_output = 50, initial data distributed
33 % over larger domain init_range = 20
34 % and different output functional 'expectation_M'
35 % (step = 2 reduced simulation and visualization of approximation
36 % => do not call directly)
37 % step = 2.1 n=20, n_output = 20, full reduced basis. should be
38 % identical to 1.1
39 % (step = 3 detailed and reduced simulation and visualization of
40 % error and estimator
41 % => do not call directly)
42 % step = 3.1 n=20, n_output = 20, full reduced basis. error should
43 % be almost 0, but therefore large overestimation (but
44 % still small estimator)
45 % step = 3.2 n=20, n_output = 20, partial reduced basis (stupid basis)
46 % (first 136 states). error should
47 % be larger than 0, but overestimation reduced only
48 % factor 3
49 % step = 3.3 n=20, n_output = 20, partial reduced basis
50 % (lower 136 triangular states). error should
51 % be larger than 0, but overestimation reduced only
52 % factor 3. Error & estimator less than 3.2, so more
53 % clever basis
54 % step = 3.4, n=200, n_output = 50, generate POD basis of
55 % single trajectory
56 % basis is saved for use in further steps
57 % and plotted
58 % step = 3.41, n=200, n_output = 50, generate POD basis of
59 % 9 trajectories
60 % basis is saved for use in further steps
61 % and plotted
62 % step = 3.42, n=200, n_output = 50, generate POD basis of
63 % 25 trajectories
64 % basis is saved for use in further steps
65 % and plotted
66 % step = 3.5 experiments with a part of POD-basis from 3.4
67 % step = 3.6 n=200, n_output = 50, generate POD-Greedy basis with
68 % true error as indicator, 5x5 p-points logarithmically
69 % equidistant distributed
70 % NEW: case 3.62 % search for several mu largest required regularization parameter
71 % step = 3.7 test POD-greedy-basis from step 3.6
72 
73 % step = 3.8 generate system matrices and export to file for
74 % remote BT generation. System A = 20301x20301
75 % => ss = sys(A,B,C,D) => 3 GB system ss!!!!!
76 % balreal takes about ...
77 % step 4.0: demo_rb_gui of POD basis from 3.4
78 % step 5.0: Optimization with RB model with POD basis from 3.4
79 %
80 % some steps explicitly require a model and model_data
81 %
82 % Done:
83 % check different dynamics in parameter range => 1.5 with differing mu
84 % check correct nt size: double and error determination
85 % visualization of basis
86 % generate multiple trajectory POD basis
87 % distributed initial data -> 1.8
88 % choice of output functions: 'average','Ex','Ey' -> select in model
89 %
90 % Done 7.9.2011:
91 % (again) generate single trajectory POD basis => 3.4 (15 minutes)
92 % generate POD-Greedy, true-error basis overnight, epsilon 0
93 % => 3.6 running => after 2 hours early stop:
94 % did not find PCA extension!
95 % generate POD-Greedy, projection-error basis overnight 9x9, epsilon 0
96 % => 3.63 remote on am100, 21 Basis vectors, good basis!
97 % opitmization with basis and "expectation N"
98 % => 4.0. landscape: perfect: error 0.268 very localized
99 % optimum value: instead of mu= (0.01,0.01)
100 % J(0.01009 0.010123).16455
101 % sehr schoen
102 % make demo_rb_gui work
103 % solve optimization problem with reduced model
104 % profiler matrix assembly, double assembly problem
105 % => OK, solved, model initialization now without model_data.
106 % correct output functional constant for separatrix :
107 %
108 % Todo:
109 % generate POD multiple trajectory basis => 3.41, now 25 trajectories
110 % => hangup... => am100 started 8.9.2011, 17:10h
111 % test different bases
112 % check stability issue => generate case, that demonstrates problem
113 % compute output functional constant for expectations
114 
115 % select best basis for optimization
116 % check error estimator in first step: is implicit computation of
117 % error estimator OK? Why not 0? terrible overestimation
118 % check possible size for BT from control toolbox: adjust n small enough!!!!!
119 % => N= 70 works, but strange results
120 % generate BT basis
121 % compare quality of BT to POD on single trajectory
122 % compare quality of BT to POD on parameter corners
123 %
124 % movie of default trajectory => moviegen does not work
125 % step = 3.9 compute BT-basis (compute remotely on am100)
126 % step = 3.9 test BT-basis (computed remotely on am100)
127 % - POD-Greedy basis using error estimator
128 % - POD-Greedy basis by adaptive grid
129 
130 res = [];
131 
132 % Bernard Haasdonk 21.4.2010
133 
134 if (nargin<1) || (isempty(step))
135  step = 1.1 % simulation and visualization of detailed dynamical system
136  %step = 2 % reduced simulation and visualization of approximation
137  %step =3 % detailed and reduced simulation and visualization of
138  % error and estimator
139 end;
140 
141 if (nargin < 2)
142  model = [];
143 end;
144 
145 if (nargin < 3)
146  model_data = [];
147 % model = follicle_model([]);
148 % model_data = gen_model_data(model);
149 % model = follicle_model([]);
150 % model_data = gen_model_data(model);
151 % model_data.RB = eye(model.dim_x,231); % i.e. first 231 states
152 % model.RB_generation_mode = 'from_detailed_data';
153 end;
154 
155 if (nargin < 4)
156  detailed_data = [];
157 end;
158 
159 %switch basis
160 % case 1
161 % model_data.RB = eye(model.dim_x,231); % i.e. first 231 states
162 % model.RB_generation_mode = 'from_detailed_data';
163 % case 2
164 % model_data.RB = eye(model.dim_x,200); % i.e. first 200 states
165 % model.RB_generation_mode = 'from_detailed_data';
166 % case 3
167 % model_data.RB = eye(model.dim_x,20); % i.e. first 200 states
168 % model.RB_generation_mode = 'from_detailed_data';
169 % case 4
170 % otherwise
171 % error('basis not implemented!');
172 %end;
173 %model_data.RB = eye(model.dim_x,model.dim_x); % i.e. non-reduced
174 %model_data.RB = eye(model.dim_x,200); % i.e. first 200 states
175 
176 switch step
177  case 1 % simulation and visualization
178  tic;
179  if isempty(model_data)
180  model_data = gen_model_data(model);
181  end;
182  sim_data = filecache_function(@detailed_simulation,model,model_data);
183 % sim_data = detailed_simulation(model,model_data);
184  t = toc;
185  mu = model.get_mu(model);
186  model.plot_title = ['state probabilities for mu=(',...
187  num2str(mu(1)),',',num2str(mu(2)),')'];
188  p = plot_sim_data(model,model_data,sim_data,model);
189 % model.plot_sim_data_state(model,model_data,sim_data,model);
190  %compute sum for plausibility check of time range/dimension
191 % l1norm = sum(sim_data.X);
192 % figure, plot(l1norm); xlabel('time'); title('l1norm of X over time');
193  fprintf('\n')
194  disp(['time for detailed_simulation :',num2str(t)]);
195  if model.debug
196  disp(['finished, please interact with data']);
197  keyboard;
198  end;
199  res = sim_data;
200 
201  case 1.1 % call step 1 with changed parameters
202  params.range = 20;
203  params.output_range = 20;
204 % params.output_plot_indices = [1];
205  model = follicle_model(params);
206  model_data = gen_model_data(model);
207  follicle_experiments(1,model,model_data);
208 
209  case 1.2 % call step 1 with changed parameters
210  params.range = 50;
211  params.output_range = 20;
212 % params.output_plot_indices = [1,2];
213  model = follicle_model(params);
214  model_data = gen_model_data(model);
215  follicle_experiments(1,model,model_data);
216 
217  case 1.3 % call step 1 with changed parameters
218  params.range = 50;
219  params.output_range = 50;
220 % params.output_plot_indices = [1,2];
221  model = follicle_model(params);
222  model_data = gen_model_data(model);
223  follicle_experiments(1,model,model_data);
224 
225  case 1.4 % call step 1 with changed parameters
226  params.range = 70;
227  params.output_range = 50;
228  % params.output_plot_indices = [1,2];
229  model = follicle_model(params);
230  model_data = gen_model_data(model);
231  follicle_experiments(1,model,model_data);
232 
233  case 1.5 % call step 1 with changed parameters
234  tic;
235  params.range = 200;
236  params.output_range = 50;
237  % params.output_plot_indices = [1,2];
238  model = follicle_model(params);
239  model.clim = [0,0.00005];
240  % model_data = gen_model_data(model);
241  % mu = (u1, u2), mu_ranges : {[0.005,0.02],[0.005,0.02]};
242 % mu = [0.005,0.005]; % => blob to 3e-6, yout to 0.3
243 % mu = [0.01,0.01]; % => blob to 1e-6, yout to 0.1
244 % mu = [0.02,0.02]; % => blob to 0, yout to almost 0, left corner visible
245 % mu = [0.005,0.02]; % => blob to 0, yout to almost 0, left corner invisible
246  mu = [0.02,0.005]; % => blob non existing, yout 0.9, linear decay
247 
248  model = model.set_mu(model,mu);
249  % model.save_time_indices = 0:(model.nt/100):model.nt;
250  %model.save_time_indices = 1:100:1001;
251  %model.nt = 1000;
252  %model.T = 1000*model.nt;
253 % sim_data = follicle_experiments(1,model,model_data);
254  sim_data = follicle_experiments(1,model);
255  save(fullfile(rbmatlabtemp,'follicle_n200_example.mat'),...
256  'sim_data','model','model_data');
257  t = toc;
258  disp(['computation time t = ',num2str(t),' sec.']);
259  keyboard;
260 
261  case 1.6
262  % test nt behaviour
263 
264  params.range = 200;
265  params.output_range = 50;
266  % params.output_plot_indices = [1,2];
267  model = follicle_model(params);
268  if isempty(model_data)
269  model_data = gen_model_data(model);
270  end;
271  model.clim = [0,0.00005];
272 
273  % nt = 2000, return every 4th slice
274  % nt = 1000, return every second slice
275  % nt = 500
276 
277  disp('nt = 500');
278  mu = model.get_mu(model);
279  sim_data500 = filecache_function(@detailed_simulation,model, ...
280  model_data);
281 % model.plot_title = ['state probabilities for nt = 500'];
282 % plot_state_probabilities(model,model_data,sim_data500,model);
283 
284  disp('nt = 1000');
285  model.nt = 1000;
286  model.save_time_indices = 0:2:1000;
287  sim_data1000 = filecache_function(@detailed_simulation,model, ...
288  model_data);
289 % model.plot_title = ['state probabilities for nt = 1000'];
290 % plot_state_probabilities(model,model_data,sim_data1000,model);
291 
292  sim_data_err = sim_data500;
293  sim_data_err.X = abs(sim_data_err.X- sim_data1000.X);
294  model.plot_title = ['absolute value of error'];
295 % plot_state_probabilities(model,model_data,sim_data_err,model);
296 
297  nz = find(abs(sim_data1000.X)>0);
298  z = find(sim_data1000.X==0);
299  sim_data_err_rel = sim_data500;
300  sim_data_err_rel.X(nz)= sim_data_err.X(nz)./abs(sim_data1000.X(nz));
301  sim_data_err_rel.X(z) = sim_data_err.X(z) * 1e10;
302  model.plot_title = ['relative error'];
303  model.clim =[0,0.1];
304 
305  model.plot_sim_data_state(model,model_data,sim_data_err_rel,model);
306 
307  keyboard;
308 
309  case 1.7
310  %generate movie from 1.6
311 
312  % currently problem in movie generation: mpgwrite produces
313  % non-readable file, explorer crash, matlab-dll warning, etc.
314  % perhaps use Markus method, see basisgen_animation
315 
316  load(fullfile(rbmatlabtemp,'follicle_n200_example.mat'));
317  model.clim = [0,0.00005];
318  outputfn = fullfile(rbmatlabtemp,'follicle_trajectory_small2.mpg');
319  if exist(outputfn)
320  error(['file ',outputfn,' existing!']);
321  end;
322  fps = 15;
323  %params.show_colorbar = 1
324  params.xnumintervals = model.range+1;
325  params.ynumintervals = model.range+1;
326  params.xrange = [-0.5,model.range+0.5];
327  params.yrange = [-0.5,model.range+0.5];
328  params.verbose = 0;
329  grid = rectgrid(params);
330  model.no_lines = 1;
331  %grid = Grids.rectgrid(params);
332  plot_state_probability_slice(model,model_data,grid,sim_data.X(:,1),model);
333 % set(gcf,'Position',[10,10,300,200]);
334  set(gcf,'Position',[10,10,800,800]);
335  for j = 1:10
336 % for j = 1:size(sim_data.X,2)
337  cla;
338  plot_state_probability_slice(model,model_data,grid,sim_data.X(:,j),model);
339 % text(0.1,0.9,text_sequence{i},'FontSize',30,'Color',[0,0,1])
340  F(j) = getframe(gca);
341  end;
342 
343  C = colormap;
344  keyboard;
345  % play movie
346  movie(F);
347  %mov = close(mov);
348  mpgwrite(F,C,outputfn,...
349  [1, 0, 1, 0, 10, 1, 1, 1]);
350 
351  keyboard;
352 
353  case 1.8
354  tic;
355  params.range = 200;
356  params.output_range = 50;
357  % params.output_plot_indices = [1,2];
358  params.init_range = 20;
359 % params.output_type = 'separatrix';
360  params.output_type = 'expectation_N';
361 % params.reg_epsilon = 0.00009;
362  params.reg_epsilon = 0.00000;
363  model = follicle_model(params);
364  model.clim = [0,0.00005];
365  % model_data = gen_model_data(model);
366  % mu = (u1, u2), mu_ranges : {[0.005,0.02],[0.005,0.02]};
367 % mu = [0.005,0.005]; % => blob to 5e-5, yout to 3.7
368 % mu = [0.01,0.01]; % => blob to 5e-5, yout to 3.8
369 % mu = [0.02,0.02]; % => blob to 0, yout to almost 3.7,
370 % mu = [0.005,0.02]; % => blob to 0, yout to almost 9.2
371  mu = [0.02,0.005]; % => blob non existing, yout 0.3
372 
373  model = model.set_mu(model,mu);
374  % model.save_time_indices = 0:(model.nt/100):model.nt;
375  %model.save_time_indices = 1:100:1001;
376  %model.nt = 1000;
377  %model.T = 1000*model.nt;
378 % sim_data = follicle_experiments(1,model,model_data);
379  sim_data = follicle_experiments(1,model);
380  save(fullfile(rbmatlabtemp,'follicle_n200_example.mat'),...
381  'sim_data','model','model_data');
382  t = toc;
383  disp(['computation time t = ',num2str(t),' sec.']);
384  keyboard;
385 
386  case 2 % reduced simulation
387  %define reduced basis of first two coordinates (i.e. error only
388  %by u!)
389 
390  model.debug = 1;
391 
392  % produce high-dimensional data, e.g. reduced basis, etc.
393  % mu is not known
394  detailed_data = gen_detailed_data(model,model_data);
395  disp('gen_detailed_data successful');
396 
397  % produce low-dimensional data, e.g. gram matrices between
398  % reduced basis vectors, subgrid extraction, etc.
399  % mu is not known (should comprise previous offline and online steps)
400 
401  reduced_data = gen_reduced_data(model,detailed_data);
402  disp('gen_reduced_data successful');
403  model.N = reduced_data.N;
404 
405  % rb_sim_data = rb_simulation(reduced_data,model)
406  % perform reduced simulation, i.e. mu known
407  % produces some structure with suitable fields, e.g DOF vector
408 
409  disp('entering reduced simulation');
410  rb_sim_data = rb_simulation(model,reduced_data);
411  disp('reduced simulation successful');
412 
413  rb_sim_data = rb_reconstruction(model,detailed_data,rb_sim_data);
414 
415  model.plot_title = 'reduced trajectory';
416  %model.plot_indices = 1:3; => default
417  p = plot_sim_data(model,model_data,rb_sim_data,[]);
418 
419  disp(['finished, please interact with data']);
420  keyboard;
421 
422  case 2.1
423  par.range = 20;
424  par.output_range = 20;
425  model = follicle_model(par);
426  model_data = gen_model_data(model);
427  model_data.RB = eye(model.dim_x,231); % i.e. first 231 states
428  model.RB_generation_mode = 'from_detailed_data';
429  follicle_experiments(2,model,model_data);
430 
431  case 3 % detailed and reduced simulation
432 
433  %model.x0_shift=0;
434  %model.u_function_ptr = @(model) 0; % define u constant 0
435  % => leads to error 0 :-) if x0_shift = 0
436 
437  %model.u_function_ptr = @(model) 0.1; % define u constant
438  %=> error linearly increasing
439  %model.u_function_ptr = @(model) 0.3*sin(2*model.t)+0.002*model.t;
440 
441  tic;
442 
443  % remove some fields such that caching works
444  tmp_model_data = model_data;
445  tmp_model = model;
446  if isfield(tmp_model_data,'RB')
447  tmp_model_data = rmfield(tmp_model_data,'RB');
448  end;
449  if isfield(tmp_model_data,'RB_generation_mode')
450  tmp_model = rmfield(tmp_model,'RB_generation_mode');
451  end;
452  sim_data = filecache_function(@detailed_simulation,tmp_model,tmp_model_data);
453  t1 = toc;
454  fprintf('\n');
455  disp(['time for detailed simulation: ',num2str(t1)]);
456 
457 % model.plot_title = 'full simulation';
458 % model.plot_sim_data_state(model,model_data,sim_data,model);
459 
460  if isempty(detailed_data)
461  detailed_data = gen_detailed_data(model,model_data);
462  end;
463 
464  reduced_data = gen_reduced_data(model,detailed_data);
465  model.N = reduced_data.N;
466  tic;
467  rb_sim_data = rb_simulation(model,reduced_data);
468  t2 = toc;
469  disp(['time for reduced simulation: ',num2str(t2)]);
470 
471  rb_sim_data = rb_reconstruction(model,detailed_data, ...
472  rb_sim_data);
473 
474 % model.plot_title = 'reduced simulation';
475 % model.plot_sim_data_state(model,model_data,rb_sim_data,model);
476 
477 % figure, plot3(sim_data.X(1,:),...
478 % sim_data.X(2,:),...
479 % sim_data.X(3,:), ...
480 % 'color','b' ...
481 % );
482 % hold on
483 % plot3(rb_sim_data.X(1,:),...
484 % rb_sim_data.X(2,:),...
485 % rb_sim_data.X(3,:),...
486 % 'color','r');
487 % axis equal;
488 % title('exact and reduced trajectories');
489 % legend({'exact','reduced'});
490 
491  er = sim_data;
492  er.X = sim_data.X-rb_sim_data.X;
493  er.Y = sim_data.Y-rb_sim_data.Y;
494 
495  model.plot_title = 'error';
496  model.plot_sim_data_state(model,model_data,er,model);
497 
498  err = sqrt(sum((detailed_data.G * er.X).*er.X,1));
499  max_t_err = err;
500 
501  % somehow replace by matrix version!!!
502  for i = 2:length(max_t_err)
503  if max_t_err(i)<max_t_err(i-1)
504  max_t_err(i) = max_t_err(i-1);
505  end;
506  end;
507 
508  % figure, plot(sim_data.time,[rb_sim_data.DeltaX(:),max_t_err(:),err(:)]),
509  if model.enable_error_estimator
510  figure, plot([rb_sim_data.DeltaX(:),max_t_err(:),err(:)]),
511  Ylim = get(gca,'Ylim');
512  set(gca,'Ylim',[0,max(Ylim)]);
513  title('rb state error and estimator');
514  legend({'estimator \Delta_x(t)','||e||_{L\infty([0,t],L2)}',...
515  '||e(t)||_{L2}'});
516  else
517  figure, plot([max_t_err(:),err(:)]),
518  Ylim = get(gca,'Ylim');
519  set(gca,'Ylim',[0,max(Ylim)]);
520  title('rb state error');
521  legend({'||e||_{L\infty([0,t],L2)}',...
522  '||e(t)||_{L2}'});
523  end;
524 
525  if model.dim_y > 1
526  er.Ynorm = sum(er.Y.^2)';
527  else
528  er.Ynorm = abs(er.Y(:));
529  end;
530 
531  if model.enable_error_estimator
532  figure, plot([rb_sim_data.DeltaY(:),er.Ynorm]);
533  title('rb output error and estimator');
534  Ylim = get(gca,'Ylim');
535  set(gca,'Ylim',[0,max(Ylim)]);
536  legend({'estimator','error'});
537  figure, plot(rb_sim_data.time_indices,rb_sim_data.Rnorms);
538  title('Residual norms')
539  else
540  figure, plot([er.Ynorm]);
541  title('rb output error');
542  Ylim = get(gca,'Ylim');
543  set(gca,'Ylim',[0,max(Ylim)]);
544  legend({'error'});
545  end;
546 
547 % plot_sim_data(model,model_data,sim_data,model);
548  if model.debug
549  disp(['finished, please interact with data']);
550  keyboard;
551  end;
552 
553  case 3.1
554 
555  model = follicle_model([]);
556  model_data.RB = eye(model.dim_x,231); % i.e. first 231 states
557  model.RB_generation_mode = 'from_detailed_data';
558  follicle_experiments(3,model,model_data);
559 
560  case 3.2
561 
562  model = follicle_model([]);
563  % length i = 136 (range <=15)
564  E = eye(model.dim_x,136);
565  model_data.RB = E;
566  model.RB_generation_mode = 'from_detailed_data';
567  follicle_experiments(3,model,model_data);
568 
569  case 3.3
570 
571  par = [];
572  par.output_type = 'expectation_N';
573  model = follicle_model(par);
574  model = model.set_mu(model,[0.01,0.01]);
575  model_data = gen_model_data(model);
576  % length i = 136 (range <=15)
577  i = find((model_data.Ns+model_data.Ms)<=15); % i.e. lower states
578 
579  E = eye(model.dim_x);
580  model_data.RB = E(:,i);
581  model.RB_generation_mode = 'from_detailed_data';
582  follicle_experiments(3,model,model_data);
583  keyboard;
584 
585  case 3.31
586  fn = fullfile(rbmatlabtemp,'follicle_PODGreedy_detailed_data_proj_err_9x9.mat');
587  if ~exist(fn)
588  error('please run step 3.6 (or any similar) first!!');
589  end;
590  load(fn);
591  % length i = 136 (range <=15)
592  %i = find((model_data.Ns+model_data.Ms)<=15); % i.e. lower states
593 
594  model_data.RB = detailed_data.V;
595  model.RB_generation_mode = 'from_detailed_data';
596  follicle_experiments(3,model,model_data);
597 
598  case 3.4
599  % generate POD basis of single trajectory
600  params.range = 200;
601  params.output_range = 50;
602  % params.output_plot_indices = [1,2];
603  params.init_range = 20;
604 % params.output_type = 'separatrix';
605  params.output_type = 'expectation_N';
606 % params.reg_epsilon = 0.00009;
607  params.reg_epsilon = 0.00000;
608  model = follicle_model(params);
609  % model.RB_generation_mode = 'PCA_trajectory';
610  % model.RB_stop_Nmax = 10;
611  model_data = gen_model_data(model);
612  model.RB_generation_mode = 'from_detailed_data';
613  tmpmodel = model;
614  tmpmodel.save_time_indices = 0:model.nt;
615  disp('computing trajectory');
616  sim_data = filecache_function(@detailed_simulation,tmpmodel,model_data);
617  disp('computing POD');
618  RBinit = orthonormalize_gram_schmidt(sim_data.X(:,1),model_data.G,1e-20);
619  Xorth = sim_data.X(:,2:end)- ...
620  RBinit * ((RBinit' * model_data.G) * sim_data.X(:,2:end));
621  % keyboard;
622  clear('sim_data');
623  [U,S,V,flag] = svds(Xorth,20); % single svds(..20) takes about
624  % 5 min!!
625  i = find(diag(S)>=eps);
626  U = U(:,i);
627  %[U,S] = svds_batch(sim_data.X,5,100);
628  % warning: basis not fully orthogonal!!!
629  % U = orth(U);
630  disp('computing gram-schmidt orthogonalization');
631  model_data.RB = orthonormalize_gram_schmidt([RBinit,U],...
632  model_data.G,eps);
633  % keyboard;
634  detailed_data = gen_detailed_data(model,model_data);
635  save(fullfile(rbmatlabtemp,'follicle_POD_detailed_data.mat'),...
636  'model','model_data','detailed_data');
637 
638  model.clim = [-0.00005,0.00005];
639  model.plot_detailed_data(model,detailed_data,[]);
640 % follicle_experiments(3,model,model_data);
641 
642  case 3.41
643  % generate POD basis of multiple trajectories
644 
645  % model.RB_generation_mode = 'PCA_trajectory';
646  % model.RB_stop_Nmax = 10;
647  par.range = 200;
648  par.output_range = 50;
649  params.init_range = 20;
650 % params.output_type = 'separatrix';
651  params.output_type = 'expectation_N';
652 % params.reg_epsilon = 0.00009;
653  params.reg_epsilon = 0.00000;
654  model = follicle_model(par);
655  model_data = gen_model_data(model);
656  model.RB_generation_mode = 'from_detailed_data';
657 
658  u1s = [0.005,0.007,0.01,0.014,0.02];
659  u2s = [0.005,0.007,0.01,0.014,0.02];
660 % u2s = [0.005,0.01,0.02];
661 
662  Us = [];
663  Ss = [];
664 
665  tic;
666  for u1i = 1:length(u1s);
667  for u2i = 1:length(u2s);
668  tmpmodel = model;
669  tmpmodel.save_time_indices = 0:model.nt;
670  mu = [u1s(u1i),u2s(u2i)];
671  disp(['processing trajectory for mu=',num2str(mu)])
672  tmpmodel = tmpmodel.set_mu(tmpmodel,mu);
673  disp('computing trajectory');
674  sim_data = filecache_function(@detailed_simulation,tmpmodel,model_data);
675  disp('computing POD');
676  RBinit = orthonormalize_gram_schmidt(sim_data.X(:,1),model_data.G,1e-20);
677  Xorth = sim_data.X(:,2:end)- ...
678  RBinit * ((RBinit' * model_data.G) * sim_data.X(:,2:end));
679  % keyboard;
680  clear('sim_data');
681  [U,S,V,flag] = svds(Xorth,15);
682  clear('Xorth');
683  i = find(diag(S)>=eps);
684  U = U(:,i);
685  disp('computing gram-schmidt orthogonalization');
686  U = orthonormalize_gram_schmidt(U,model_data.G,eps);
687  S = diag(S);
688  Ss = [Ss;S(i)];
689  Us = [Us,U];
690  end;
691  end;
692 
693  % save temporary result
694  save(fullfile(rbmatlabtemp,'partial_PODs'));
695 
696  % one final SVD merging the partial bases:
697  Us = Us*diag(Ss);
698  disp('computing final POD');
699  [U,S,V,flag] = svds(Us,min(100,size(Us,2)));
700  i = find(diag(S)>=eps);
701  Utot = Us(:,i);
702  disp('computing final gram-schmidt orthogonalization');
703  model_data.RB = orthonormalize_gram_schmidt([RBinit,Utot],model_data.G,eps);
704 
705  detailed_data = gen_detailed_data(model,model_data);
706  save(fullfile(rbmatlabtemp,'follicle_multi_POD_detailed_data.mat'),...
707  'model','model_data','detailed_data');
708 
709  model.clim = [-0.00005,0.00005];
710  model.plot_detailed_data(model,detailed_data,[]);
711 % follicle_experiments(3,model,model_data);
712  t = toc;
713  disp(['runtime ',num2str(t)]);
714 
715  case 3.5
716 
717  % test POD basis, only part of basis vectors
718 % fn = fullfile(rbmatlabtemp,'follicle_multi_POD_detailed_data.mat');
719  fn = fullfile(rbmatlabtemp, ...
720  'follicle_PODgreedy_detailed_data_non_orth.mat');
721  if ~exist(fn)
722  error('please run step 3.4 first!!');
723  end;
724  load(fn);
725  model.clim = [-0.00005,0.00005];
726  plot_detailed_data(model,detailed_data,[]);
727  return;
728  % model_data.RB = model_data.RB(:,1:2); => Acceleration 1000 !!
729 % RB = orthonormalize_qr(model_data.RB(:,1:20));
730 % RB = orth(model_data.RB(:,1:2));
731  RB = model_data.RB(:,1:4);
732  model_data.RB = RB;
733  model.debug = 1;
734 % model.enable_error_estimator = 1;
735  model.enable_error_estimator = 1;
736  follicle_experiments(3,model,model_data);
737  keyboard;
738 
739  case 3.6
740 
741  % POD-Greedy basis with true error as indicator (default settings
742  % are in model)
743  tic;
744 
745  par.range = 200;
746  par.output_range = 50;
747  par.init_range = 20;
748  model = follicle_model(par);
749  model.RB_generation_mode = 'greedy_log_uniform_fixed';
750  model.RB_detailed_train_savepath = 'follicle_model_detailed_train_eps0';
751 
752  model_data = gen_model_data(model);
753  detailed_data = gen_detailed_data(model,model_data);
754  save(fullfile(rbmatlabtemp,'follicle_PODGreedy_detailed_data_eps0.mat'),...
755  'model','model_data','detailed_data');
756 
757  t = toc;
758  disp('computation time: ')
759  t
760 % model.N = 20;
761 % follicle_experiments(3,model,model_data);
762 
763  case 3.61
764 
765  % POD-Greedy basis with true error as indicator (default settings
766  % are in model) with regularization
767  par.range = 200;
768  par.output_range = 50;
769  par.init_range = 20;
770  model = follicle_model(par);
771  model.RB_generation_mode = 'greedy_log_uniform_fixed';
772  model.RB_detailed_train_savepath = 'follicle_model_detailed_train_eps0.0186714';
773 % model.RB_numintervals = [1,1];
774 % model.RB_stop_Nmax = 20;
775 % model.reg_epsilon = 0.00009;
776  model.reg_epsilon = 0.0186714; % maximum eigenvalue over 25 points
777 % model.debug = 1;
778  model_data = gen_model_data(model);
779  detailed_data = gen_detailed_data(model,model_data);
780  save(fullfile(rbmatlabtemp,...
781  'follicle_PODGreedy_detailed_data_eps0.0186714.mat'),...
782  'model','model_data','detailed_data');
783 
784 % model.N = 20;
785 % follicle_experiments(3,model,model_data);
786 
787  case 3.62 % search for several mu largest required regularization parameter
788  par.range = 200;
789  par.output_range = 50;
790  par.init_range = 20;
791  model = follicle_model(par);
792 
793  par.numintervals = model.RB_numintervals;
794  par.range = model.mu_ranges;
795  for i = 1:length(par.range);
796  par.range{i} = log(par.range{i});
797  end;
798  % enable restart of basis generation!!!
799  % detailed_data.RB_info = [];
800  MMesh0 = cubegrid(par);
801  Mtrain_log = get(MMesh0,'vertex')';
802  mus = exp(Mtrain_log);
803 % mus = rand_uniform(nmus,model.mu_ranges);
804  model.reg_epsilon = 0.0186714; % maximum eigenvalue over 25 points
805  model.decomp_mode = 0;
806  model_data = gen_model_data(model);
807  lmax = -10000;
808 % for i = 1:size(mus,2);
809  for i = 25;
810  mu = mus(:,i)
811  model = model.set_mu(model,mu);
812  A = model.A_function_ptr(model,model_data);
813  As = 0.5*(A+A');
814  l_current = eigs(As-10*speye(size(As)),1,'SM');
815  l_current = 10+l_current;
816  if l_current > lmax
817  lmax = l_current;
818  disp(['detected eigenvalue ',num2str(lmax)]);
819  end;
820  end;
821 
822 
823  case 3.63
824 
825  % POD-Greedy basis with true projection error as indicator (default settings
826  % are in model) 9x9 parameter grid
827  tic;
828 
829  par.range = 200;
830  par.output_range = 50;
831  par.init_range = 20;
832  model = follicle_model(par);
833  model.RB_generation_mode = 'greedy_log_uniform_fixed';
834  model.RB_detailed_train_savepath = 'follicle_model_detailed_train_eps0_9x9';
835  model.RB_error_indicator = 'projection-error';
836  model.RB_stop_epsilon = 1e-16;
837  model.RB_numintervals = [8,8];
838  model_data = gen_model_data(model);
839  detailed_data = gen_detailed_data(model,model_data);
840  save(fullfile(rbmatlabtemp,'follicle_PODGreedy_detailed_data_proj_err_9x9.mat'),...
841  'model','model_data','detailed_data');
842 
843  t = toc;
844  disp('computation time: ')
845  t
846 % model.N = 20;
847 % follicle_experiments(3,model,model_data);
848 
849  case 3.7
850 
851  % test POD-Greedy basis
852 % fn = fullfile(rbmatlabtemp,'follicle_PODGreedy_detailed_data.mat');
853  fn = fullfile(rbmatlabtemp,'follicle_PODGreedy_detailed_data_proj_err_9x9.mat');
854  if ~exist(fn)
855  error('please run step 3.6 (or any similar) first!!');
856  end;
857  load(fn);
858 % model_data.RB = model_data.RB(:,1:2); => Acceleration 1000 !!
859 % model_data.RB = model_data.RB(:,1:20);
860 % model.debug = 1;
861  model.enable_error_estimator = 0;
862  follicle_experiments(3,model,model_data,detailed_data);
863  plot_detailed_data(model,detailed_data,[]);
864  keyboard;
865 
866  case 3.8 % generate BT export matrices
867 
868  params.range = 200;
869 % params.range = 70;
870  params.output_range = 50;
871  % params.output_plot_indices = [1,2];
872  model = follicle_model(params);
873  model.clim = [0,0.00005];
874  model_data = gen_model_data(model);
875 
876  fn = 'follicle_model_system_matrices.mat';
877  fn2 = 'follicle_model_BT.m';
878  fn3 = 'follicle_model_BT_basis.mat';
879  fullfn = fullfile(rbmatlabtemp, ...
880  fn);
881  fullfn2 = fullfile(rbmatlabtemp, ...
882  fn2);
883  if exist(fullfn)
884  delete(fullfn);
885 % error(['file ',fullfn,' existing, please remove.']);
886  end;
887  if exist(fullfn2)
888  delete(fullfn2);
889 % error(['file ',fullfn2,' existing, please remove.']);
890  end;
891 
892  % matrix export for remote BT basis computation
893  model.decomp_mode = 0;
894  A = filecache_function(model.A_function_ptr,model,model_data);
895  B = model.B_function_ptr(model,model_data);
896  C = model.C_function_ptr(model,model_data);
897  D = model.D_function_ptr(model,model_data);
898  save(fullfn,'model','A','B','C','D');
899  disp(['system matrices exported to file ',fullfn]);
900 
901  diary(fullfile(rbmatlabtemp,fn2));
902  disp( [' load(''',fn,'''); '...
903  ' sys = ss(A,B,C,D); ',...
904  ' tic; ', ...
905  ' [sysb,G,T,Tinv] = balreal(sys); ', ...
906  ' time = toc; ', ...
907  ' W = transpose(T(1:500,:)); ',...
908  ' V = Tinv(:,1:500); ',...
909  ' H = G(1:500); ',...
910  ' save([''~/',fn3,'''],''V'',''W'',''H'',''time'');']);
911  diary off;
912  disp(['BT script exported to file ',fullfn2]);
913 
914  disp(['please copy files to remote host into your matlab startup' ...
915  ' directory and execute'])
916  disp([' matlab -nodesktop -nosplash -r ',fn2]);
917 
918  disp(['then copy remote file ~/',fn3,' to local ',rbmatlabtemp,' and press enter'])
919 
920  keyboard;
921 
922  tmp = load(fullfile(rbmatlabtemp,fn3));
923  if isfield(tmp,'V') && isfield(tmp,'W')
924  disp('BT basis OK')
925  else
926  error('error in file generation');
927  end;
928 
929  case 3.9
930  % test BT basis
931 
932  case 4.0 % reduced basis in demo_rb_gui;
933 
934  % test POD basis only part of basis vectors
935 % fn = fullfile(rbmatlabtemp,'follicle_POD_detailed_data.mat');
936 % fn = fullfile(rbmatlabtemp,'follicle_PODgreedy_detailed_data_eps0.0186714.mat');
937 %fn = fullfile(rbmatlabtemp, ...
938 % 'follicle_PODGreedy_detailed_data_eps0.0186714.mat');
939 %fn = fullfile(rbmatlabtemp,'follicle_PODgreedy_detailed_data_eps0.mat');
940 %fn = fullfile(rbmatlabtemp,'follicle_PODgreedy_detailed_data_proj_err.mat');
941 fn = fullfile(rbmatlabtemp,'follicle_PODgreedy_detailed_data_proj_err_9x9.mat');
942 % fn = fullfile(rbmatlabtemp,'follicle_multi_POD_detailed_data.mat');
943 % fn = fullfile(rbmatlabtemp,'follicle_PODGreedy_detailed_data_non_orth.mat');
944  if ~exist(fn)
945  error('please run step 3.4 (or similar) first!!');
946  end;
947  load(fn);
948  model.N = size(detailed_data.V,2);
949  model.clim = [0.00000,0.00005];
950  demo_rb_gui(model,detailed_data);
951  keyboard;
952 
953  case 5.0
954  % solve optimization problem: target output curve for mu = (0.01,0.01)
955 
956  % load target output curve
957  params.range = 200;
958  params.output_range = 50;
959  % params.output_plot_indices = [1,2];
960  params.init_range = 20;
961  params.output_type='expectation_N';
962  model = follicle_model(params);
963  model.clim = [0,0.00005];
964  model_data = gen_model_data(model);
965  mu = [0.01,0.01];
966  model = model.set_mu(model,mu);
967 % mu = model.get_mu(model);
968  sim_data = filecache_function(@detailed_simulation,model,model_data);
969  Ytarget = sim_data.Y;
970 
971  % load reduced model
972 % fn = fullfile(rbmatlabtemp,'follicle_multi_POD_detailed_data.mat');
973 % fn = fullfile(rbmatlabtemp,'follicle_POD_detailed_data.mat');
974 % fn = fullfile(rbmatlabtemp,'follicle_PODGreedy_detailed_data_hand_orth.mat');
975 fn = fullfile(rbmatlabtemp,'follicle_PODgreedy_detailed_data_proj_err_9x9.mat');
976 % fn = fullfile(rbmatlabtemp,'follicle_PODgreedy_detailed_data_eps0.mat');
977 % fn = fullfile(rbmatlabtemp,'follicle_PODGreedy_detailed_data.mat');
978  if ~exist(fn)
979  error('please run basis generation step 3.* first!!');
980  end;
981 % keyboard;
982  model_old = model;
983  model_data_old = model_data;
984  load(fn);
985  model = model_old;
986  model_data.C_components = model_data_old.C_components;
987  detailed_data.C_components = model_data_old.C_components;
988 
989  sim_data_check = filecache_function(@detailed_simulation,model,model_data);
990  Ytarget_check = sim_data_check.Y;
991 
992  if ~isequal(Ytarget,Ytarget_check)
993  error('model and stored data inconsistent!');
994  end;
995  % model.C_function_ptr = model_old.C_function_ptr;
996  % keyboard;
997 % model.N = floor(size(detailed_data.V,2)/2);
998 % model.N = 12;
999  model.N = size(detailed_data.V,2);
1000  reduced_data = gen_reduced_data(model,detailed_data);
1001 
1002  % set up optimization function
1003  fun = @(mu) my_opt_target(mu,Ytarget,model,reduced_data)
1004 
1005  if 1
1006  disp('generating surface');
1007  % plot target surface
1008  logr = log([0.005,0.02]);
1009  logrange = logr(1):(logr(2)-logr(1))/20:logr(2);
1010  range = exp(logrange)
1011  [X,Y] = meshgrid(range,range);
1012  Z = zeros(size(X));
1013  for i = 1:length(X(:))
1014  Z(i) = fun([X(i),Y(i)]);
1015 % if (Z(i))>1e10
1016 % keyboard;
1017 % end;
1018  end;
1019  surf(X,Y,Z);
1020  set(gca,'Zscale','log')
1021  keyboard;
1022  end;
1023 
1024 % mu0 = [0.005,0.005];
1025 % mu0 = [0.02,0.005];
1026  mu0 = [0.02,0.02];
1027  lb = [0.005,0.005];
1028  ub = [0.02,0.02];
1029  [mumin,fval,exitflag,output,lambda,grad] = fmincon(fun,mu0,[],[],[],[],lb,ub);
1030 
1031  disp('The original value is')
1032 
1033  mu;
1034 
1035  disp('And target value J(mu)')
1036  fun(mu);
1037 
1038  disp('optimization finished, please inspect workspace');
1039  keyboard;
1040 
1041  otherwise
1042  error('step unknown');
1043 end;
1044 
1045 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1046 
1047 function J = my_opt_target(mu,Ytarget,model,reduced_data);
1048 % function J = my_opt_target(mu,Ytarget,model,reduced_data);
1049 % my_opt_target
1050 
1051 tmpmodel = model.set_mu(model,mu);
1052 rb_sim_data = rb_simulation(tmpmodel,reduced_data);
1053 J = sum((rb_sim_data.Y-Ytarget).^2);
1054 disp(['J(',num2str(mu(:)'),')=',num2str(J)]);
1055