rbmatlab  1.16.09
 All Classes Namespaces Files Functions Variables Modules Pages
lin_evol_opt_detailed_der_simulation_old.m
1 function sim_data = lin_evol_opt_detailed_der_simulation_old(model,model_data)
2 %function sim_data = lin_evol_opt_detailed_der_simulation(model,model_data)
3 %
4 % Function performing a detailed simulation and calculating the derivative
5 % solution with respect to all parameters.
6 % required fields of model:
7 % T : final time
8 % nt : number of time-intervals until T, i.e. nt+1
9 % solution slices are computed
10 % init_values_algorithm: name of function for computing the
11 % initvalues-DOF with arguments (grid, params)
12 % example: init_values_cog
13 % operators_algorithm: name of function for computing the
14 % L_E,L_I,b-operators with arguments (grid, params)
15 % example operators_conv_diff
16 % data_const_in_time : if this optional field is 1, the time
17 % evolution is performed with constant operators,
18 % i.e. only the initial-time-operators are computed
19 % and used throughout the time simulation.
20 % compute_output_functional: flag indicating, whether output
21 % functional is to be computed
22 %
23 % compute_derivative_info: flag if derivative information shall be
24 % calculated or not
25 %
26 % return fields of sim_data:
27 % U : sequence of DOF vectors
28 % y : sequence of output functional values (if activated)
29 %
30 % Markus Dihlmann 15.10.10
31 
32 disp('OLD VERSION of derivative detailed simulation !!!!!!!!!!!!!!!!!!!!!!!')
33 
34 if model.verbose >= 9
35  disp('entered detailed simulation ');
36 end;
37 
38 if isempty(model_data)
39  model_data = gen_model_data(model);
40 end;
41 
42 if ~isfield(model,'compute_derivative_info')
43  model.compute_derivative_info = 0;
44 end
45 
46 
47 model.decomp_mode = 1; % == components;
48 model.t = 0;
49 
50 
51 if ~isfield(model,'optimization')
52  disp('Model has no field optimization.')
53  model.optimization =[];
54 end
55 
56 
57 if isfield(model.optimization,'params_to_optimize')
58  indx_mu_i = find(model.optimization.params_to_optimize); %indexes i of \mu_i for which derivative information is needed
59 else
60  indx_mu_i = 1:length(get_mu(model));
61 end
62 nr_indx_mu_i= length(indx_mu_i); %nr of parameters to be optimized
63 %nr_derivatives = size(model.optimization.params_to_optimize,2); %total nr of parameters in the model
64 
65 
66 
67 if isfield(model,'save_time_indices')
68  save_time_index = zeros(1,model.nt+1);
69  save_time_index(model.save_time_indices(:)+1) = 1;
70  save_time_indices = find(save_time_index==1)-1;
71 else
72  save_time_index = ones(1,model.nt+1);
73  save_time_indices = 1:(model.nt+1);
74 end;
75 
76 % initial values by midpoint evaluation
77 U0_comp = model.init_values_algorithm(model,model_data);
78 
79 model.decomp_mode=2; %==> coefficients
80 U0_coeff = model.init_values_algorithm(model, model_data);
81 if model.compute_derivative_info
82  U0_der_coeff = model.rb_derivative_init_values_coefficients(model);
83 end
84 
85 %assembling initial values true solution
86 Ut = lincomb_sequence(U0_comp,U0_coeff);
87 
88 %assembling initial values derivative solution
89 if model.compute_derivative_info
90 Ut_der = zeros(length(Ut),nr_indx_mu_i);
91 for i=1:size(U0_der_coeff,1)
92  Ut_der(:,i) = lincomb_sequence(U0_comp, U0_der_coeff(i,:)'); %%% FRAGE: PASST DAS MIT Ut_der? Hat 3 dim statt 2?!
93 end
94 %%%%%%%%%%%%%%%%%%%%%% HIER SPARSE MATRITZEN VERWENDEN!!!???
95 % original:
96 U_der = zeros(length(Ut), length(save_time_indices),nr_indx_mu_i);
97 
98 %new:
99 %U_der=cell(nr_indx_mu_i,1);
100 %for i=1:nr_indx_mu_i
101 % U_der{i} = sparse(length(Ut), length(save_time_indices));
102 %end
103 %%%%%%%%%%%%%%%%%%%%%% end: HIER SPARSE MATRITZEN VERWENDEN!!!
104 
105 end
106 
107 % preallocate U
108 %%%%% DEBUG D, 06.12.10
109 %original:
110 U = zeros(length(Ut),length(save_time_indices));
111 %new:
112 %U = sparse(length(Ut),length(save_time_indices));
113 %%%%% END DEBUG D, 06.12.10
114 
115 time_indices = zeros(1,length(save_time_indices));
116 time = zeros(1,length(save_time_indices));
117 model.dt = model.T/model.nt;
118 %operators_required = 1;
119 if ~isfield(model,'data_const_in_time')
120  model.data_const_in_time = 0;
121 end,
122 
123 % get operator components
124 if model.affinely_decomposed
125  old_decomp_mode = model.decomp_mode;
126  model.decomp_mode = 1;
127  [L_I_comp, L_E_comp, b_comp] = model.operators_ptr(model, model_data);
128  model.decomp_mode = old_decomp_mode;
129 end;
130 
131 t_column = 1; % next column index to be filled in output
132 t_ind = 0; % t_ind between 0 and nt
133 t = 0; % absolute time between 0 and T
134 
135 % store init data
136 if save_time_index(t_ind+1)
137  U(:,t_column) = Ut;
138  if model.compute_derivative_info
139  for i=1:nr_indx_mu_i
140  %%%%%%% DEBUG E, 06.12.10
141  %original:
142  U_der(:,t_column,i)=Ut_der(:,i);
143  %new:
144  %U_der{i}(:,t_column) = Ut_der(:,i); %%% FRAGE: PASST DAS MIT Ut_der? Hat 3 dim statt 2?!
145  %%%%%%% END DEBUG E, 06.12.10
146  end
147  end
148  time_indices(t_column) = t_ind;
149  time(t_column) = t;
150  t_column = t_column + 1;
151 end;
152 
153 if ~isfield(model,'compute_output_functional')
154  model.compute_output_functional = 0;
155 end,
156 
157 %model.plot(U0,model_data.grid,params);
158 
159 %allocate memory for derivation matrices:
160 %L_E_derived = cell(nr_indx_mu_i,1);
161 %L_I_derived = cell(nr_indx_mu_i,1);
162 %b_derived = cell(nr_indx_mu_i,1);
163 
164 % loop over fv-steps
165 for t_ind = 1:model.nt
166  t = (t_ind-1)*model.dt;
167  if model.verbose >= 10
168  disp(['entered time-loop step ',num2str(t_ind)]);
169  end;
170  if model.verbose == 9
171  fprintf('.');
172  end;
173  % get matrices and bias-vector
174 
175  if (t_ind==1)||(~model.data_const_in_time)%operators_required
176 
177  model.t = t;
178 
179  % new assembly in every iteration
180  if ~model.affinely_decomposed
181  [L_I, L_E, b] = model.operators_ptr(model, model_data);
182  else
183  % or simple assembly based on affine parameter decomposition
184  % => turns out to be slower for few components
185 
186  % test affine decomposition
187  if model.debug
188  disp('test affine decomposition of operators')
189  test_affine_decomp(model.operators_ptr,3,1,model,model_data);
190  disp('OK')
191  end;
192  model.decomp_mode = 2;
193  [L_I_coeff, L_E_coeff, b_coeff] = model.operators_ptr(model, ...
194  model_data);
195  model.decomp_mode = old_decomp_mode;
196  L_I = lincomb_sequence(L_I_comp, L_I_coeff);
197  L_E = lincomb_sequence(L_E_comp, L_E_coeff);
198  b = lincomb_sequence(b_comp, b_coeff);
199  end;
200 
201  %if model.data_const_in_time
202  % operators_required = 0;
203  %end;
204 
205  if model.compute_derivative_info
206  % Get Operators for derivative information:
207  [sLi,sLe,sb] = model.rb_derivative_operators_coefficients_ptr(model);
208  %debug 28.10.10
209  if ~isempty(L_I_comp) %only do the linear combination if the coefficients sLi are not empty
210  %%%%%%%%%%%%%%%%%%%%%%% HIER AUCH NOCH ÄNDERN FALLS ES
211  %%%%%%%%%%%%%%%%%%%%%%% LÄUFT!!!!!!!!!! 07.12.10
212  %old:
213  L_I_derived=zeros(size(L_I_comp{1},1),size(L_I_comp{1},2),nr_indx_mu_i);
214  %new:
215  %for i=1:nr_indx_mu_i
216  % L_I_derived{i}=sparse(size(L_I_comp{1},1),size(L_I_comp{1},2));
217  %end
218  %%%%%%%%%%%%%%%%%%%%%%% END: HIER AUCH NOCH ÄNDERN FALLS ES
219  %%%%%%%%%%%%%%%%%%%%%%% LÄUFT!!!!!!!!!! 07.12.10
220  end
221 
222  % DEBUG 06.12.10
223  %%% original:
224  L_E_derived=zeros(size(L_E_comp{1},1),size(L_E_comp{1},2),nr_indx_mu_i);
225  b_derived=zeros(length(b_comp{1}),nr_indx_mu_i);
226  %%% end original
227  %%%%%%new try:
228  %for i=1:nr_indx_mu_i
229  % L_E_derived{i} = sparse(size(L_E_comp{1},1),size(L_E_comp{1},2));
230  % b_derived{i} = sparse(length(b_comp{1}),1);
231  %end
232 
233  %%%%%
234  %End DEBUG 06.12.10
235 
236  for i=1:nr_indx_mu_i
237  if ~isequal(sLi,[]) %only do the linear combination if the coefficients sLi are not empty
238  %%%%%%%%%%% DEBUG G, 07.12.10
239  %old:
240  L_I_derived(:,:,i) = lincomb_sequence(L_I_comp,sLi(indx_mu_i(i),:));
241  %new:
242  %L_I_derived{i} = lincomb_sequence(L_I_comp,sLi(indx_mu_i(i)));
243  %%%%%%%%%%% End: DEBUG G, 07.12.10
244  else
245  %%%%%%% DEBUG H, 07.12.10
246  %old:
247  L_I_derived(1,1,i); % ?????????????? ÄNDERN IN L_I_derived{1}=0 ??
248  %new:
249  %L_I_derived{i}=0;
250  %%%%%%% END DEBUG H, 07.12.10
251  end
252 %%%%%% DEBUG A, 06.12.10
253 %%%%%% original:
254  L_E_derived(:,:,i) = lincomb_sequence(L_E_comp,sLe(indx_mu_i(i),:));
255  b_derived(:,i) = lincomb_sequence(b_comp,sb(indx_mu_i(i),:));
256 %%%%%% new
257 % L_E_derived{i} = lincomb_sequence(L_E_comp,sLe(indx_mu_i(i),:));
258 % b_derived{i} = lincomb_sequence(b_comp,sb(indx_mu_i(i),:));
259 %%%%%% END DEBUG A, 06.12.10
260  end
261  end
262 
263  end;
264 
265  %rhs = L_E * Ut + b;
266  rhs = (speye(size(L_E)) + model.dt*L_E) * Ut + model.dt*b;
267  Ut_old=Ut;
268 
269  if isempty(find(L_I, 1))%isequal(L_I, speye(size(L_I)))
270  Ut = rhs;
271  else
272  % solve linear system
273  % disp('check symmetry and choose solver accordingly!');
274  % keyboard;
275  % nonsymmetric solvers:
276  % [U(:,t+1), flag] = bicgstab(L_I,rhs,[],1000);
277  % [U(:,t+1), flag] = cgs(L_I,rhs,[],1000);
278  %
279  % symmetric solver, non pd:
280  % omit symmlq:
281  % see bug_symmlq.mat for a very strange bug: cannot solve identity system!
282  % reported to matlab central, but no solution up to now.
283  % [U(:,t+1), flag] = symmlq(L_I,rhs,[],1000);
284  %
285  % [U(:,t+1), flag] = minres(L_I,rhs,[],1000);
286  % symmetric solver, pd:
287  %[U(:,t+1), flag] = pcg(L_I,rhs,[],1000);
288  % bicgstab works also quite well:
289  %[U(:,t+1), flag] = bicgstab(L_I,rhs,[],1000);
290  Ut = (speye(size(L_I))-model.dt*L_I)\rhs;%Ut + model.dt*(L_I \ rhs);
291 
292  % if flag>0
293  % disp(['error in system solution, solver return flag = ', ...
294  % num2str(flag)]);
295  % keyboard;
296  % end;
297 
298  end;
299 
300 
301  if save_time_index(t_ind)
302  U(:,t_column) = Ut;
303  time_indices(t_column) = t_ind;
304  time(t_column) = t;
305  t_column = t_column + 1;
306  end;
307 
308  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
309  %calculation derivative solution
310  if model.compute_derivative_info
311  for i=1:nr_indx_mu_i
312  %%%% DEBUG C, 06.12.10
313  %%%% original:
314  rhs_der = (speye(size(L_E)) + model.dt*L_E) * Ut_der(:,i) +...
315  model.dt*L_E_derived(:,:,i) * Ut_old+ model.dt*L_I_derived(:,:,i) *Ut + model.dt*b_derived(:,i);
316  if isequal(L_I,zeros(size(L_I)))
317  Ut_der(:,i) = rhs_der;
318  else
319  Ut_der(:,i) = (speye(size(L_I))- model.dt*L_I)\rhs_der;
320  end
321  %%%% new:
322 % rhs_der = (speye(size(L_E)) + model.dt*L_E) * Ut_der(:,i) +...
323 % model.dt*L_E_derived{i} * Ut_old + model.dt*L_I_derived{i}*Ut + model.dt*b_derived{i};
324 % if isequal(L_I,zeros(size(L_I)))
325 % Ut_der(:,i) = rhs_der;
326 % else
327 % Ut_der(:,i) = (speye(size(L_I))- model.dt*L_I)\rhs_der;
328 % end
329  %%%% end DEBUG C, 06.12.10
330 
331 
332  %%%%% DEBUG F, 06.12.10
333  %original:
334  if save_time_index(t_ind)
335  U_der(:,t_column-1,i) = Ut_der(:,i);
336  end
337  %new:
338  %if save_time_index(t_ind)
339  % U_der{i}(:,t_column-1) = Ut_der(:,i);
340  %end;
341 
342  %%%%% END DEBUG F, 06.12.10
343 
344  end
345  end
346 
347 end;
348 
349 
350 if model.verbose == 9
351  fprintf('\n');
352 end;
353 
354 
355 sim_data.U = U;
356 if model.compute_derivative_info
357 sim_data.U_der = U_der;
358 end
359 
360 sim_data.time = time;
361 sim_data.time_indices = time_indices;
362 
363 if model.compute_output_functional
364  % get operators
365  model.decomp_mode=1;
366  v = model.operators_output(model,model_data);
367  sim_data.y = (v{1}') * U;
368  sim_data.y_der=[];
369  if model.compute_derivative_info
370  for i = 1:nr_indx_mu_i
371  %%%%% DEBUG G, 06.12.10
372  % original:
373  sim_data.y_der(:,i) = -(v{1}')*U_der(:,:,i);
374  %new:
375  %sim_data.y_der(:,i) = (-1).*(v{1}')*U_der{i};
376  %%%%% END DEBUG G, 06.12.10
377  end
378  end
379 end;
380 
381 %| \docupdate
function r = verbose(level, message, messageId)
This function displays messages depending on a message-id and/or a level. Aditionally you can set/res...
Definition: verbose.m:17