rbmatlab  1.13.10
 All Classes Namespaces Files Functions Variables Groups Pages
lin_ds_adjoint_experiments.m
Go to the documentation of this file.
1 % script performing different experiments for linear dynamical
2 % system
3 
4 step = 1; % initialize model, compute some trajectories
5 %step = 2; % generate reduced basis by POD
6 %step = 3; % comparison detailed and reduced simulation
7 
8 params = [];
9 model = [];
10 %params.coarse_factor = 2; % fact 2 => ndofs = 16384, nt = 1024, 15s detailed
11 %params.coarse_factor = 1; % fact 1 => ndofs = 32768, nt = 2048, 60s detailed
12 params.coarse_factor = 1;
13 %verbose(1);
14 
15 % some filenames for storing data
16 trajectory_fnbase = 'advection_fv_output_vconst_trajectory';
17 detailed_data_fname = 'advection_fv_output_vconst_detailed_data';
18 rb_fname = 'advection_fv_output_vconst_rb';
19 
20 switch step
21  case 1
22 
23  model = lin_ds_advection_vconst_fast_model(params);
24 
25  model_data = gen_model_data(model);
26 
27  mus = {[0,0,0],[1,1,1],[1,1,0.5],[0,0.5,1]};
28 
29  for m = 1:length(mus)
30  disp(['computing trajectory number ',num2str(m)]);
31  mu = mus{m};
32  model = model.set_mu(model,mu);
33  ds_sim_data = detailed_simulation(model,model_data);
34  save(fullfile(rbmatlabtemp,[trajectory_fnbase,num2str(m)]),...
35  'model','model_data','ds_sim_data');
36  end;
37 
38  case 2
39 
40  disp('PCA of snapshots takes a while...');
41  load(fullfile(rbmatlabtemp,[trajectory_fnbase,'1']),...
42  'model','model_data','ds_sim_data');
43  % smaller epsilon does result in more than 1 vector for mu=0,0,0
44  epsilon = 1e-4;
45  RB = PCA_fixspace(ds_sim_data.X,[],model_data.G,[],[],epsilon);
46  %RB = orthonormalize_qr(ds_sim_data.X,model_data.G);
47  % select correct orthogonal set
48  K = RB' * model_data.G * RB;
49  i = find(abs(diag(K)-1)>1e-2);
50  i = min(i);
51  disp(['found ',num2str(size(RB,2)),' basis vectors after traj. 1']);
52  if isempty(i)
53  i = size(RB,2)+1;
54  end;
55  % keyboard;
56  RB = RB(:,1:(i-1));
57  disp(['reduced to ',num2str(size(RB,2)),' basis vectors.']);
58  % keyboard;
59 
60  load(fullfile(rbmatlabtemp,[trajectory_fnbase,'2']),...
61  'model','model_data','ds_sim_data');
62  RB2 = PCA_fixspace(ds_sim_data.X,RB,model_data.G,[],[],epsilon);
63  %RB = orthonormalize_qr(ds_sim_data.X,model_data.G);
64  % select correct orthogonal set
65  K = RB2' * model_data.G * RB2;
66  i = find(abs(diag(K)-1)>1e-2);
67  i = min(i);
68  if isempty(i)
69  i = size(RB2,2)+1;
70  end;
71  disp(['found ',num2str(size(RB2,2)),' basis vectors after traj. 2']);
72  % keyboard;
73  RB2 = RB2(:,1:(i-1));
74  disp(['reduced to ',num2str(size(RB2,2)),' basis vectors after traj. 2']);
75  RB = [RB, RB2];
76  % RB = orthonormalize(RB);
77 
78  V = RB;
79  W = model_data.G * V;
80  save(fullfile(rbmatlabtemp,rb_fname),'RB');
81 
82  model.RB_basis_filename = fullfile(rbmatlabtemp,rb_fname);
83  model.RB_generation_mode = 'file_load';
84 
85  detailed_data = gen_detailed_data(model,model_data);
86 
87  model = model;
88  save(fullfile(rbmatlabtemp,detailed_data_fname),...
89  'model','detailed_data')
90 
91  case 3 % compare detailed and reduced simulation
92 
93  load(fullfile(rbmatlabtemp,detailed_data_fname),...
94  'model','detailed_data')
95  load(fullfile(rbmatlabtemp,[trajectory_fnbase,'2']),...
96  'ds_sim_data');
97 
98  model = model;
99 
100  %perform single reduced simulation for given parameter
101  reduced_data = gen_reduced_data(model,detailed_data);
102  model.N = reduced_data.N;
103  tic,
104 % model.nt = model.nt/4;
105 % disp('set nt to quarter!!');
106  rb_sim_data = rb_simulation(model,reduced_data);
107  t_red = toc;
108  rb_sim_data = rb_reconstruction(model,detailed_data,rb_sim_data)
109  disp(['time for reduced simulation: ',num2str(t_red)]);
110 
111  % plot of reduced trajectory
112  lin_evol_model = model.base_model;
113  lin_evol_model_data = gen_model_data(lin_evol_model);
114 
115  params.no_lines = 1;
116  params.show_colorbar = 1;
117  params.axis_equal = 1;
118  params.plot = lin_evol_model.plot;
119  % plot single snapshot
120  %lin_evol_model.plot(rb_sim_data.X(:,1),lin_evol_model_data.grid, ...
121  % params);
122 
123  % plot sequence:
124  params.title = 'reduced solution';
125  plot_sequence(rb_sim_data.X,lin_evol_model_data.grid,params)
126 
127  % plot sequence:
128  params.title = 'error';
129  plot_sequence((rb_sim_data.X-ds_sim_data.X),...
130  lin_evol_model_data.grid,params)
131 
132  % plot of reduced output
133  figure,plot(rb_sim_data.time,[rb_sim_data.Y;ds_sim_data.Y]);
134  legend('reduced output','detailed output');
135 
136  case 4 % einfache Basis durch balanciertes Abschneiden erzeugen
137  % A = ...
138  % B = ...
139  % k = 10;
140 
141  %[V,W] = balanced_truncation(A,B,C,D,k)
142 
143  case 5 % test der Basis anahand von balancierten System
144 
145  otherwise
146  error('step unknown');
147 end;
148 
149 
150 
151