3 % experiments with the model of the human follicle growth
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
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
19 % step = 1.4 simulation and visualization of detailed dynamical
20 % system => n=70, n_output=50 no big difference to
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
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
39 % (step = 3 detailed and reduced simulation and visualization of
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
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
54 % step = 3.4, n=200, n_output = 50, generate POD basis of
56 % basis is saved for use in further steps
58 % step = 3.41, n=200, n_output = 50, generate POD basis of
60 % basis is saved for use in further steps
62 % step = 3.42, n=200, n_output = 50, generate POD basis of
64 % basis is saved for use in further steps
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
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
80 % some steps explicitly require a model and model_data
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
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
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 :
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
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
121 % compare quality of BT to POD on single trajectory
122 % compare quality of BT to POD on parameter corners
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
132 % Bernard Haasdonk 21.4.2010
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
148 % model_data = gen_model_data(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';
161 % model_data.RB = eye(model.dim_x,231); % i.e. first 231 states
162 % model.RB_generation_mode =
'from_detailed_data';
164 % model_data.RB = eye(model.dim_x,200); % i.e. first 200 states
165 % model.RB_generation_mode =
'from_detailed_data';
167 % model_data.RB = eye(model.dim_x,20); % i.e. first 200 states
168 % model.RB_generation_mode =
'from_detailed_data';
171 % error(
'basis not implemented!');
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
177 case 1 % simulation and visualization
179 if isempty(model_data)
180 model_data = gen_model_data(model);
183 % sim_data = detailed_simulation(model,model_data);
185 mu = model.get_mu(model);
186 model.plot_title = ['state probabilities for mu=(',...
187 num2str(mu(1)),',',num2str(mu(2)),')'];
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');
194 disp(['time for detailed_simulation :',num2str(t)]);
196 disp(['finished, please interact with data']);
201 case 1.1 % call step 1 with changed parameters
203 params.output_range = 20;
204 % params.output_plot_indices = [1];
206 model_data = gen_model_data(model);
209 case 1.2 % call step 1 with changed parameters
211 params.output_range = 20;
212 % params.output_plot_indices = [1,2];
214 model_data = gen_model_data(model);
217 case 1.3 % call step 1 with changed parameters
219 params.output_range = 50;
220 % params.output_plot_indices = [1,2];
222 model_data = gen_model_data(model);
225 case 1.4 % call step 1 with changed parameters
227 params.output_range = 50;
228 % params.output_plot_indices = [1,2];
230 model_data = gen_model_data(model);
233 case 1.5 % call step 1 with changed parameters
236 params.output_range = 50;
237 % params.output_plot_indices = [1,2];
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
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;
252 %model.T = 1000*model.nt;
255 save(fullfile(rbmatlabtemp,
'follicle_n200_example.mat'),...
256 'sim_data',
'model',
'model_data');
258 disp([
'computation time t = ',num2str(t),
' sec.']);
265 params.output_range = 50;
266 % params.output_plot_indices = [1,2];
268 if isempty(model_data)
269 model_data = gen_model_data(model);
271 model.clim = [0,0.00005];
273 % nt = 2000, return every 4th slice
274 % nt = 1000, return every second slice
278 mu = model.get_mu(model);
281 % model.plot_title = ['state probabilities for nt = 500'];
282 % plot_state_probabilities(model,model_data,sim_data500,model);
286 model.save_time_indices = 0:2:1000;
289 % model.plot_title = ['state probabilities for nt = 1000'];
290 % plot_state_probabilities(model,model_data,sim_data1000,model);
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);
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'];
305 model.plot_sim_data_state(model,model_data,sim_data_err_rel,model);
310 %generate movie from 1.6
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
316 load(fullfile(rbmatlabtemp,'follicle_n200_example.mat'));
317 model.clim = [0,0.00005];
318 outputfn = fullfile(rbmatlabtemp,'follicle_trajectory_small2.mpg');
320 error(['file ',outputfn,' existing!']);
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];
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]);
336 % for j = 1:size(sim_data.X,2)
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);
348 mpgwrite(F,C,outputfn,...
349 [1, 0, 1, 0, 10, 1, 1, 1]);
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;
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
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;
377 %model.T = 1000*model.nt;
380 save(fullfile(rbmatlabtemp,
'follicle_n200_example.mat'),...
381 'sim_data',
'model',
'model_data');
383 disp([
'computation time t = ',num2str(t),
' sec.']);
386 case 2 % reduced simulation
387 %define reduced basis of first two coordinates (i.e. error only
392 % produce high-dimensional data, e.g. reduced basis, etc.
394 detailed_data = gen_detailed_data(model,model_data);
395 disp('gen_detailed_data successful');
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)
401 reduced_data = gen_reduced_data(model,detailed_data);
402 disp('gen_reduced_data successful');
403 model.N = reduced_data.N;
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
409 disp('entering reduced simulation');
410 rb_sim_data = rb_simulation(model,reduced_data);
411 disp('reduced simulation successful');
413 rb_sim_data = rb_reconstruction(model,detailed_data,rb_sim_data);
415 model.plot_title = 'reduced trajectory';
416 %model.plot_indices = 1:3; => default
419 disp(['finished, please interact with data']);
424 par.output_range = 20;
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';
431 case 3 % detailed and reduced simulation
434 %model.u_function_ptr = @(model) 0; % define u constant 0
435 % => leads to error 0 :-) if x0_shift = 0
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;
443 % remove some fields such that caching works
444 tmp_model_data = model_data;
446 if isfield(tmp_model_data,'RB')
447 tmp_model_data = rmfield(tmp_model_data,'RB');
449 if isfield(tmp_model_data,'RB_generation_mode')
450 tmp_model = rmfield(tmp_model,'RB_generation_mode');
455 disp(['time for detailed simulation: ',num2str(t1)]);
457 % model.plot_title = 'full simulation';
458 % model.plot_sim_data_state(model,model_data,sim_data,model);
460 if isempty(detailed_data)
461 detailed_data = gen_detailed_data(model,model_data);
464 reduced_data = gen_reduced_data(model,detailed_data);
465 model.N = reduced_data.N;
467 rb_sim_data = rb_simulation(model,reduced_data);
469 disp(['time for reduced simulation: ',num2str(t2)]);
471 rb_sim_data = rb_reconstruction(model,detailed_data, ...
474 % model.plot_title = 'reduced simulation';
475 % model.plot_sim_data_state(model,model_data,rb_sim_data,model);
477 % figure, plot3(sim_data.X(1,:),...
478 % sim_data.X(2,:),...
479 % sim_data.X(3,:), ...
483 % plot3(rb_sim_data.X(1,:),...
484 % rb_sim_data.X(2,:),...
485 % rb_sim_data.X(3,:),...
488 % title('exact and reduced trajectories');
489 % legend({
'exact',
'reduced'});
492 er.X = sim_data.X-rb_sim_data.X;
493 er.Y = sim_data.Y-rb_sim_data.Y;
495 model.plot_title =
'error';
496 model.plot_sim_data_state(model,model_data,er,model);
498 err = sqrt(sum((detailed_data.G * er.X).*er.X,1));
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);
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)}',...
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)}',...
526 er.Ynorm = sum(er.Y.^2)
';
528 er.Ynorm = abs(er.Y(:));
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
')
540 figure, plot([er.Ynorm]);
541 title('rb output error
');
542 Ylim = get(gca,'Ylim
');
543 set(gca,'Ylim
',[0,max(Ylim)]);
547 % plot_sim_data(model,model_data,sim_data,model);
549 disp(['finished, please interact with data
']);
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);
562 model = follicle_model([]);
563 % length i = 136 (range <=15)
564 E = eye(model.dim_x,136);
566 model.RB_generation_mode = 'from_detailed_data
';
567 follicle_experiments(3,model,model_data);
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
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);
586 fn = fullfile(rbmatlabtemp,'follicle_PODGreedy_detailed_data_proj_err_9x9.mat
');
588 error('please run step 3.6 (or any similar) first!!
');
591 % length i = 136 (range <=15)
592 %i = find((model_data.Ns+model_data.Ms)<=15); % i.e. lower states
594 model_data.RB = detailed_data.V;
595 model.RB_generation_mode = 'from_detailed_data
';
596 follicle_experiments(3,model,model_data);
599 % generate POD basis of single trajectory
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
';
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));
623 [U,S,V,flag] = svds(Xorth,20); % single svds(..20) takes about
625 i = find(diag(S)>=eps);
627 %[U,S] = svds_batch(sim_data.X,5,100);
628 % warning: basis not fully orthogonal!!!
630 disp('computing gram-schmidt orthogonalization');
631 model_data.RB = orthonormalize_gram_schmidt([RBinit,U],...
634 detailed_data = gen_detailed_data(model,model_data);
635 save(fullfile(rbmatlabtemp,'follicle_POD_detailed_data.mat'),...
636 'model','model_data','detailed_data');
638 model.clim = [-0.00005,0.00005];
639 model.plot_detailed_data(model,detailed_data,[]);
643 % generate POD basis of multiple trajectories
645 % model.RB_generation_mode = 'PCA_trajectory';
646 % model.RB_stop_Nmax = 10;
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;
655 model_data = gen_model_data(model);
656 model.RB_generation_mode = 'from_detailed_data';
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];
666 for u1i = 1:length(u1s);
667 for u2i = 1:length(u2s);
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');
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));
681 [U,S,V,flag] = svds(Xorth,15);
683 i = find(diag(S)>=eps);
685 disp('computing gram-schmidt orthogonalization');
686 U = orthonormalize_gram_schmidt(U,model_data.G,eps);
693 % save temporary result
694 save(fullfile(rbmatlabtemp,'partial_PODs'));
696 % one final SVD merging the partial bases:
698 disp('computing final POD');
699 [U,S,V,flag] = svds(Us,min(100,size(Us,2)));
700 i = find(diag(S)>=eps);
702 disp('computing final gram-schmidt orthogonalization');
703 model_data.RB = orthonormalize_gram_schmidt([RBinit,Utot],model_data.G,eps);
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');
709 model.clim = [-0.00005,0.00005];
710 model.plot_detailed_data(model,detailed_data,[]);
713 disp(['runtime ',num2str(t)]);
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');
722 error('please run step 3.4 first!!');
725 model.clim = [-0.00005,0.00005];
726 plot_detailed_data(model,detailed_data,[]);
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);
734 % model.enable_error_estimator = 1;
735 model.enable_error_estimator = 1;
741 % POD-Greedy basis with true error as indicator (default settings
746 par.output_range = 50;
749 model.RB_generation_mode = 'greedy_log_uniform_fixed';
750 model.RB_detailed_train_savepath = 'follicle_model_detailed_train_eps0';
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');
758 disp('computation time: ')
765 % POD-Greedy basis with true error as indicator (default settings
766 % are in model) with regularization
768 par.output_range = 50;
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
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');
787 case 3.62 % search for several mu largest required regularization parameter
789 par.output_range = 50;
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});
798 % enable restart of basis generation!!!
799 % detailed_data.RB_info = [];
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);
808 % for i = 1:size(mus,2);
811 model = model.set_mu(model,mu);
812 A = model.A_function_ptr(model,model_data);
814 l_current = eigs(As-10*speye(size(As)),1,
'SM');
815 l_current = 10+l_current;
818 disp([
'detected eigenvalue ',num2str(lmax)]);
825 % POD-Greedy basis with true projection error as indicator (default settings
826 % are in model) 9x9 parameter grid
830 par.output_range = 50;
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');
844 disp(
'computation time: ')
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');
855 error('please run step 3.6 (or any similar) first!!');
858 % model_data.RB = model_data.RB(:,1:2); => Acceleration 1000 !!
859 % model_data.RB = model_data.RB(:,1:20);
861 model.enable_error_estimator = 0;
863 plot_detailed_data(model,detailed_data,[]);
866 case 3.8 % generate BT export matrices
870 params.output_range = 50;
871 % params.output_plot_indices = [1,2];
873 model.clim = [0,0.00005];
874 model_data = gen_model_data(model);
876 fn = 'follicle_model_system_matrices.mat';
877 fn2 = 'follicle_model_BT.m';
878 fn3 = 'follicle_model_BT_basis.mat';
879 fullfn = fullfile(rbmatlabtemp, ...
881 fullfn2 = fullfile(rbmatlabtemp, ...
885 % error(['file ',fullfn,' existing, please remove.']);
889 % error(['file ',fullfn2,' existing, please remove.']);
892 % matrix export for remote BT basis computation
893 model.decomp_mode = 0;
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]);
901 diary(fullfile(rbmatlabtemp,fn2));
902 disp( [' load(''',fn,'''); '...
903 ' sys = ss(A,B,C,D); ',...
905 ' [sysb,G,T,Tinv] = balreal(sys); ', ...
907 ' W = transpose(T(1:500,:)); ',...
908 ' V = Tinv(:,1:500); ',...
909 ' H = G(1:500); ',...
910 ' save([''~/',fn3,'''],''V'',''W'',''H'',''time'');']);
912 disp(['BT script exported to file ',fullfn2]);
914 disp(['please copy files to remote host into your matlab startup' ...
915 ' directory and execute'])
916 disp([' matlab -nodesktop -nosplash -r ',fn2]);
918 disp(['then copy remote file ~/',fn3,' to local ',rbmatlabtemp,' and press enter'])
922 tmp = load(fullfile(rbmatlabtemp,fn3));
923 if isfield(tmp,'V') && isfield(tmp,'W')
926 error('error in file generation');
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');
945 error('please run step 3.4 (or similar) first!!');
948 model.N = size(detailed_data.V,2);
949 model.clim = [0.00000,0.00005];
950 demo_rb_gui(model,detailed_data);
954 % solve optimization problem: target output curve for mu = (0.01,0.01)
956 % load target output curve
958 params.output_range = 50;
959 % params.output_plot_indices = [1,2];
960 params.init_range = 20;
961 params.output_type='expectation_N';
963 model.clim = [0,0.00005];
964 model_data = gen_model_data(model);
966 model = model.set_mu(model,mu);
967 % mu = model.get_mu(model);
969 Ytarget = sim_data.Y;
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');
979 error('please run basis generation step 3.* first!!');
983 model_data_old = model_data;
986 model_data.C_components = model_data_old.C_components;
987 detailed_data.C_components = model_data_old.C_components;
990 Ytarget_check = sim_data_check.Y;
992 if ~isequal(Ytarget,Ytarget_check)
993 error('model and stored data inconsistent!');
995 % model.C_function_ptr = model_old.C_function_ptr;
997 % model.N = floor(size(detailed_data.V,2)/2);
999 model.N = size(detailed_data.V,2);
1000 reduced_data = gen_reduced_data(model,detailed_data);
1002 % set up optimization function
1003 fun = @(mu) my_opt_target(mu,Ytarget,model,reduced_data)
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);
1013 for i = 1:length(X(:))
1014 Z(i) = fun([X(i),Y(i)]);
1020 set(gca,'Zscale','log')
1024 % mu0 = [0.005,0.005];
1025 % mu0 = [0.02,0.005];
1029 [mumin,fval,exitflag,output,lambda,grad] = fmincon(fun,mu0,[],[],[],[],lb,ub);
1031 disp('The original value is')
1035 disp('And target value J(mu)')
1038 disp('optimization finished, please inspect workspace');
1042 error('step unknown');
1045 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1047 function J = my_opt_target(mu,Ytarget,model,reduced_data);
1048 % function J = my_opt_target(mu,Ytarget,model,reduced_data);
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)]);