rbmatlab  1.13.10
 All Classes Namespaces Files Functions Variables Groups Pages
demo_lin_ds.m
1 function demo_lin_ds
2 %function demo_lin_ds
3 %
4 % function demonstrating RB approach for linear dynamical systems
5 % according to MATHMOD 2009 paper
6 %
7 % Method also demonstrates new call of all reduced schemes:
8 %
9 % model_data = gen_model_data(model);
10 %
11 % sim_data = detailed_simulation(model,model_data);
12 % produces some structure with suitable fields, e.g DOF vector
13 % output sequence, reference to output files, simulation
14 % information, etc. Last parameter is model-struct, possibly
15 % precedent parameters, e.g. grid, etc.
16 % so not only DOF-vector is the return argument, but a structure
17 % detailed_data = gen_detailed_data(model,model_data);
18 % produce high-dimensional data, e.g. reduced basis, etc.
19 % mu is not known.
20 % reduced_data = gen_reduced_data(model,detailed_data);
21 % produce low-dimensional data, e.g. gram matrices between
22 % reduced basis vectors, subgrid extraction, etc.
23 % mu is not known (so is the previous offline-data or
24 % combination of offline and online data)
25 % rb_sim_data = rb_simulation(model,reduced_data)
26 % perform reduced simulation, i.e. mu known
27 % produces some structure with suitable fields, e.g DOF vector,
28 % error estimation
29 %
30 % Further routines "extending" datasets, e.g.:
31 % rb_sim_data = rb_reconstruction(rb_sim_data,model)
32 % reduced_data = extract_reduced_data_subset(reduced_data,model)
33 % detailed_sim_data = rb_basis_generation(detailed_data,model)
34 
35 % Bernard Haasdonk 2.4.2009
36 
37 %step = 1 % simulation and visualization of dynamical system
38 %step = 2 % reduced simulation and visualization of approximation
39 step =3 % detailed and reduced simulation and visualization of
40  % error and estimator
41 
42 
43 model = lin_ds_model_default;
44 
45 model.data_const_in_time = 1;
46 model.T = 5*pi;
47 model.nt = 10000;
48 model.affinely_decomposed = 1;
49 model.A_function_ptr = @A_func;
50 % ratio of first to second ellipses axes
51 %model.A_axes_ratio = 2;
52 model.A_axes_ratio = 2;
53 model.B_function_ptr = @B_func;
54 model.C_function_ptr = @C_func;
55 model.D_function_ptr = @D_func;
56 model.x0_function_ptr = @x0_func;
57 % shift of x0 in third coordinate
58 model.x0_shift=1;
59 model.u_function_ptr = @(model) 0.1; % define u constant
60 %model.u_function_ptr = @(model) sin(4*model.t);
61 %model.u_function_ptr = @(model) 0; % define u constant 0
62 %model.u_function_ptr = @(model) model.t; % define u = 1 * t
63 model.G_matrix_function_ptr = @(model,model_data) eye(3,3);
64 % the following lead to both larger C1 and C2!!!
65 %model.G_matrix_function_ptr = @(model) diag([2,0.5,1]);
66 %model.G_matrix_function_ptr = @(model) diag([0.5,2,1]);
67 
68 % dummy parametrization:, parameter is not used...
69 model.mu_names = {'A_axes_ratio','x0_shift'};
70 %model.mu_ranges = {[0.5,2],[0,1]};
71 model.mu_ranges = {[0.5,0.5],[1,1]};
72 model.affinely_decomposed = 1;
73 
74 % set function pointers to main model methods
75 %model.gen_model_data = @lin_ds_gen_model_data;
76 %model.detailed_simulation = @lin_ds_detailed_simulation;
77 %model.gen_detailed_data = @lin_ds_gen_detailed_data;
78 %model.gen_reduced_data = @lin_ds_gen_reduced_data;
79 %model.rb_simulation = @lin_ds_rb_simulation;
80 %model.plot_sim_data = @lin_ds_plot_sim_data;
81 %model.rb_reconstruction = @lin_ds_rb_reconstruction;
82 % determin model data, i.e. matrix components, matrix G
83 
84 model_data = gen_model_data(model); % matrix components
85 
86 % be sure to determine correct constants once, if anything in the
87 % problem setting has changed!!
88 estimate_bounds = 1;
89 % the following only needs to be performed, if the model are changed.
90 if estimate_bounds
91  disp('Estimating continuity bound constants')
92  model.estimate_lin_ds_nmu = 10;
93  model.estimate_lin_ds_nX = 100;
94  model.estimate_lin_ds_nt = 100;
95  format long;
96  [C1,C2] = lin_ds_estimate_bound_constants(model,model_data)
97  model.state_bound_constant_C1 = C1;
98  model.output_bound_constant_C2 = C2;
99 else
100  model.state_bound_constant_C1 = 2;
101  model.output_bound_constant_C2 = sqrt(3);
102 end;
103 model.error_estimation = 1;
104 model_data.RB = eye(3,2);
105 model.RB_generation_mode = 'from_detailed_data';
106 
107 switch step
108  case 1 % simulation and visualization
109  % later: sim_data = detailed_simulation(model);
110 
111  sim_data = detailed_simulation(model,model_data);
112 
113  p = plot_sim_data(model,model_data,sim_data,[]);
114 
115  case 2 % reduced simulation
116  %define reduced basis of first two coordinates (i.e. error only
117  %by u!)
118 
119  model.debug = 1;
120 
121  % produce high-dimensional data, e.g. reduced basis, etc.
122  % mu is not known
123  detailed_data = gen_detailed_data(model,model_data);
124 
125  % produce low-dimensional data, e.g. gram matrices between
126  % reduced basis vectors, subgrid extraction, etc.
127  % mu is not known (should comprise previous offline and online steps)
128 
129  reduced_data = gen_reduced_data(model,detailed_data);
130  model.N = reduced_data.N;
131 
132  % rb_sim_data = rb_simulation(reduced_data,model)
133  % perform reduced simulation, i.e. mu known
134  % produces some structure with suitable fields, e.g DOF vector
135 
136  rb_sim_data = rb_simulation(model,reduced_data);
137 
138  rb_sim_data = rb_reconstruction(model,detailed_data,rb_sim_data);
139 
140  model.plot_title = 'reduced trajectory';
141  %model.plot_indices = 1:3; => default
142  p = plot_sim_data(model,model_data,rb_sim_data,[]);
143 % keyboard;
144 
145  case 3 % detailed and reduced simulation
146  model.x0_shift=0;
147  %model.u_function_ptr = @(model) 0; % define u constant 0
148  % => leads to error 0 :-) if x0_shift = 0
149 
150  %model.u_function_ptr = @(model) 0.1; % define u constant
151  %=> error linearly increasing
152  model.u_function_ptr = @(model) 0.3*sin(2*model.t)+0.002*model.t;
153 
154 
155  sim_data = detailed_simulation(model,model_data);
156  detailed_data = gen_detailed_data(model,model_data);
157  model.enable_error_estimator=1;
158  reduced_data = gen_reduced_data(model,detailed_data);
159  model.N = reduced_data.N;
160  rb_sim_data = rb_simulation(model,reduced_data);
161  rb_sim_data = rb_reconstruction(model,detailed_data, ...
162  rb_sim_data);
163 
164  figure, plot3(sim_data.X(1,:),...
165  sim_data.X(2,:),...
166  sim_data.X(3,:), ...
167  'color','b' ...
168  );
169  hold on
170  plot3(rb_sim_data.X(1,:),...
171  rb_sim_data.X(2,:),...
172  rb_sim_data.X(3,:),...
173  'color','r');
174  axis equal;
175  title('exact and reduced trajectories');
176  legend({'exact','reduced'});
177 
178  error = sim_data;
179  error.X = sim_data.X-rb_sim_data.X;
180  error.Y = sim_data.Y-rb_sim_data.Y;
181 
182  err = sqrt(sum((detailed_data.G * error.X).*error.X,1));
183  figure, plot([rb_sim_data.DeltaX(:),err(:)]),
184  Ylim = get(gca,'Ylim');
185  set(gca,'Ylim',[0,max(Ylim)]);
186  title('rb state error and estimator');
187  legend({'estimator','error'});
188 
189  figure, plot([rb_sim_data.DeltaY(:),abs(error.Y(:))]);
190  title('rb output error and estimator');
191  Ylim = get(gca,'Ylim');
192  set(gca,'Ylim',[0,max(Ylim)]);
193  legend({'estimator','error'});
194  disp('end');
195  %keyboard;
196 
197  otherwise
198  error('step unknown');
199 end;
200 
201 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
202 % functions defining the dynamical system and input/initial data
203 function A = A_func(model,model_data);
204 A = eval_affine_decomp(@A_components,@A_coefficients,...
205  model,model_data);
206 
207 % A_parameter: ratio of first to second axis of
208 function Acomp = A_components(model,model_data)
209 %Acomp = {[0 -0.25 0; 4 0 0 ; 0 0 0 ]};
210 %Acomp = {[0 -1 0; 1 0 0 ; 0 0 0 ]};
211 %Acomp = {[-0.01 0 0; 0 -0.9 0 ; 0 0 -0.001 ]};
212 Acomp = {[0 1 0; 0 0 0 ; 0 0 0 ],...
213  [0 0 0; 1 0 0 ; 0 0 0 ] ...
214  };
215 
216 function Acoeff = A_coefficients(model)
217 Acoeff = [-model.A_axes_ratio, 1/model.A_axes_ratio];
218 
219 function B = B_func(model,model_data)
220 B = eval_affine_decomp(@B_components,@B_coefficients,model,model_data);
221 
222 function Bcomp = B_components(model,model_data)
223 Bcomp = {[0;0;1]};
224 
225 function Bcoeff = B_coefficients(model)
226 Bcoeff = 1;
227 
228 function C = C_func(model,model_data)
229 C = eval_affine_decomp(@C_components,@C_coefficients,model,model_data);
230 
231 function Ccomp = C_components(model,model_data)
232 Ccomp = {[1,1,1]};
233 %Ccomp = {[2,0,1],[0,2,1]};
234 
235 function Ccoeff = C_coefficients(model)
236 Ccoeff = 1;
237 %Ccoeff = [model.C_weight,1-model.C_weight];
238 
239 function D = D_func(model,model_data)
240 D = 0; % no decomposition required for scheme
241 
242 function x0 = x0_func(model,model_data)
243 x0 = eval_affine_decomp(@x0_components,@x0_coefficients,model,model_data);
244 
245 function x0comp = x0_components(model,model_data)
246 x0comp = {[1;0;0],[1;0;1]};
247 
248 function x0coeff = x0_coefficients(model)
249 x0coeff = [1-model.x0_shift,model.x0_shift];
250 
251 
252 
253 
254 %| \docupdate