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)
4 % Function performing a detailed simulation and calculating the derivative
5 % solution with respect to all parameters.
6 % required fields of model:
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
23 % compute_derivative_info: flag
if derivative information shall be
26 %
return fields of sim_data:
27 % U : sequence of DOF vectors
28 % y : sequence of output functional values (
if activated)
30 % Markus Dihlmann 15.10.10
32 disp(
'OLD VERSION of derivative detailed simulation !!!!!!!!!!!!!!!!!!!!!!!')
35 disp('entered detailed simulation ');
38 if isempty(model_data)
39 model_data = gen_model_data(model);
42 if ~isfield(model,'compute_derivative_info')
43 model.compute_derivative_info = 0;
47 model.decomp_mode = 1; % == components;
51 if ~isfield(model,'optimization')
52 disp('Model has no field optimization.')
53 model.optimization =[];
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
60 indx_mu_i = 1:length(get_mu(model));
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
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;
72 save_time_index = ones(1,model.nt+1);
73 save_time_indices = 1:(model.nt+1);
76 % initial values by midpoint evaluation
77 U0_comp = model.init_values_algorithm(model,model_data);
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);
85 %assembling initial values true solution
86 Ut = lincomb_sequence(U0_comp,U0_coeff);
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?!
94 %%%%%%%%%%%%%%%%%%%%%% HIER SPARSE MATRITZEN VERWENDEN!!!???
96 U_der = zeros(length(Ut), length(save_time_indices),nr_indx_mu_i);
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));
103 %%%%%%%%%%%%%%%%%%%%%% end: HIER SPARSE MATRITZEN VERWENDEN!!!
108 %%%%% DEBUG D, 06.12.10
110 U = zeros(length(Ut),length(save_time_indices));
112 %U = sparse(length(Ut),length(save_time_indices));
113 %%%%% END DEBUG D, 06.12.10
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;
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;
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
136 if save_time_index(t_ind+1)
138 if model.compute_derivative_info
140 %%%%%%% DEBUG E, 06.12.10
142 U_der(:,t_column,i)=Ut_der(:,i);
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
148 time_indices(t_column) = t_ind;
150 t_column = t_column + 1;
153 if ~isfield(model,
'compute_output_functional')
154 model.compute_output_functional = 0;
157 %model.plot(U0,model_data.grid,params);
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);
165 for t_ind = 1:model.nt
166 t = (t_ind-1)*model.dt;
168 disp(['entered time-loop step ',num2str(t_ind)]);
173 % get matrices and bias-vector
175 if (t_ind==1)||(~model.data_const_in_time)%operators_required
179 % new assembly in every iteration
180 if ~model.affinely_decomposed
181 [L_I, L_E, b] = model.operators_ptr(model, model_data);
183 % or simple assembly based on affine parameter decomposition
184 % => turns out to be slower for few components
186 % test affine decomposition
188 disp('test affine decomposition of operators')
189 test_affine_decomp(model.operators_ptr,3,1,model,model_data);
192 model.decomp_mode = 2;
193 [L_I_coeff, L_E_coeff, b_coeff] = model.operators_ptr(model, ...
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);
201 %if model.data_const_in_time
202 % operators_required = 0;
205 if model.compute_derivative_info
206 % Get Operators for derivative information:
207 [sLi,sLe,sb] = model.rb_derivative_operators_coefficients_ptr(model);
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
213 L_I_derived=zeros(size(L_I_comp{1},1),size(L_I_comp{1},2),nr_indx_mu_i);
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));
218 %%%%%%%%%%%%%%%%%%%%%%% END: HIER AUCH NOCH ÄNDERN FALLS ES
219 %%%%%%%%%%%%%%%%%%%%%%% LÄUFT!!!!!!!!!! 07.12.10
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);
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);
237 if ~isequal(sLi,[]) %only
do the linear combination
if the coefficients sLi are not empty
238 %%%%%%%%%%% DEBUG G, 07.12.10
240 L_I_derived(:,:,i) = lincomb_sequence(L_I_comp,sLi(indx_mu_i(i),:));
242 %L_I_derived{i} = lincomb_sequence(L_I_comp,sLi(indx_mu_i(i)));
243 %%%%%%%%%%% End: DEBUG G, 07.12.10
245 %%%%%%% DEBUG H, 07.12.10
247 L_I_derived(1,1,i); % ?????????????? ÄNDERN IN L_I_derived{1}=0 ??
250 %%%%%%% END DEBUG H, 07.12.10
252 %%%%%% DEBUG A, 06.12.10
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),:));
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
266 rhs = (speye(size(L_E)) + model.dt*L_E) * Ut + model.dt*b;
269 if isempty(find(L_I, 1))%isequal(L_I, speye(size(L_I)))
272 % solve linear system
273 % disp(
'check symmetry and choose solver accordingly!');
275 % nonsymmetric solvers:
276 % [U(:,t+1), flag] = bicgstab(L_I,rhs,[],1000);
277 % [U(:,t+1), flag] = cgs(L_I,rhs,[],1000);
279 % symmetric solver, non pd:
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);
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);
293 % disp([
'error in system solution, solver return flag = ', ...
301 if save_time_index(t_ind)
303 time_indices(t_column) = t_ind;
305 t_column = t_column + 1;
308 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
309 %calculation derivative solution
310 if model.compute_derivative_info
312 %%%% DEBUG C, 06.12.10
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;
319 Ut_der(:,i) = (speye(size(L_I))- model.dt*L_I)\rhs_der;
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;
327 % Ut_der(:,i) = (speye(size(L_I))- model.dt*L_I)\rhs_der;
329 %%%% end DEBUG C, 06.12.10
332 %%%%% DEBUG F, 06.12.10
334 if save_time_index(t_ind)
335 U_der(:,t_column-1,i) = Ut_der(:,i);
338 %if save_time_index(t_ind)
339 % U_der{i}(:,t_column-1) = Ut_der(:,i);
342 %%%%% END DEBUG F, 06.12.10
350 if model.verbose == 9
356 if model.compute_derivative_info
357 sim_data.U_der = U_der;
360 sim_data.time = time;
361 sim_data.time_indices = time_indices;
363 if model.compute_output_functional
366 v = model.operators_output(model,model_data);
367 sim_data.y = (v{1}
') * U;
369 if model.compute_derivative_info
370 for i = 1:nr_indx_mu_i
371 %%%%% DEBUG G, 06.12.10
373 sim_data.y_der(:,i) = -(v{1}')*U_der(:,:,i);
375 %sim_data.y_der(:,i) = (-1).*(v{1}
')*U_der{i};
376 %%%%% END DEBUG G, 06.12.10
function r = verbose(level, message, messageId)
This function displays messages depending on a message-id and/or a level. Aditionally you can set/res...