rbmatlab  1.16.09
 All Classes Namespaces Files Functions Variables Modules Pages
scm_coercive_greedy.m
Go to the documentation of this file.
1 function SCM_data=scm_coercive_greedy(model, params, operator,innerproduct_matrix)
2 %model=scm_greedy(model, model_data)
3 %
4 % SCM greedy function
5 % computes the offline data for the calculation of the coercive constant
6 % requires model and model data structure
7 %
8 %
9 % SCM Properties:
10 %
11 %
12 % SCM_data.Ma : number of parameters with an already
13 % computed constant near to mu
14 % SCM_data.Mp : number of parameters without computed
15 % constant near to mu
16 % SCM_data.NumberOfParameters : size of the test set used in the SCM
17 % Greedy procedure
18 % SCM_data.eps : error boundary of the Greedy procedure
19 % SCM_data.max_iterations : maximal number of Greedy iterations
20 % SCM_data.parallel : = 0 non parallel version
21 % = 1 parallel (pool stays active after
22 % computaton)
23 % = 2 parallel (closing pool afterwards)
24 % David Kreplin 05.01.2015
25 
26 if isempty(params)
27  warning('No SCM properties are set -> using standart values')
28  %SCM standart values
29  params.Ma = inf;
30  params.Mp = 0;
31  params.NumberOfParameters = 100;
32  params.eps=0.1;
33  params.max_iterations=inf;
34  params.parallel=0;
35 end
36 
37 
38 if ~isfield(params, 'Ma')
39  params.Ma = inf;
40 end
41 
42 if ~isfield(params, 'Mp')
43  params.Mp = 0;
44 end
45 
46 if ~isfield(params, 'NumberOfParameters')
47  warning('SCM: No Number of Parameters is set -> setting NumberOfParameters = 100');
48  params.NumberOfParameters = 100;
49 end
50 
51 if ~isfield(params, 'eps')
52  params.eps = 0.1;
53 end
54 
55 if ~isfield(params, 'max_iterations')
56  params.max_iterations = inf;
57 end
58 
59 if ~isfield(params, 'parallel')
60  params.parallel = 0;
61 end
62 
63 if ~isfield(params, 'para_rand_seed')
64  params.para_rand_seed = 1234;
65 end
66 
67 %% Parameter set
68 if ~isfield(params, 'ParameterSet')
69  rand('state',params.para_rand_seed);
70  params.ParameterSet = rand_uniform(params.NumberOfParameters,model.mu_ranges);
71 end
72 NumberOfParameters = length(params.ParameterSet(1,:));
73 
74 SCM_data.params = params;
75 
76 %% Boundary BOX for YLB
77 model.decomp_mode=1;
78 [A,~] = operator(model);
79 A=remove_dirichlet(A,1,params.dirichlet_gids);
80 
81 SCM_data.params.Q=length(A);
82 SCM_data.lower_bound_coercivity = zeros(SCM_data.params.Q,1);
83 SCM_data.upper_bound_coercivity = zeros(SCM_data.params.Q,1);
84 
85 %innerproduct_matrix = model.get_inner_product_matrix(model_data);
86 innerproduct_matrix(SCM_data.params.dirichlet_gids,:)=[];
87 innerproduct_matrix(:,SCM_data.params.dirichlet_gids)=[];
88 
89 disp('-- SCM: computing boundary box');
90 for i = 1:SCM_data.params.Q
91 
92  [msgstr, msgid] = lastwarn;
93  lastwarn('');
94  EV= eigs(0.5*(A{1,i}+transpose(A{1,i})),innerproduct_matrix,2,'be');
95  [~, warnid] = lastwarn;
96  if strcmp(warnid,'MATLAB:eigs:NotAllEigsConverged')
97  warning('Eigenvalues did not converged -> using the eig function (more expensive)');
98  EV= eig(full(A{1,i}),full(innerproduct_matrix));
99  end
100 
101  lastwarn(msgstr, msgid);
102 
103  SCM_data.lower_bound_coercivity(i) = min(EV);
104  SCM_data.upper_bound_coercivity(i) = max(EV);
105 end
106 
107 
108 
109 %% Initalize C
110 SCM_data.C=[];
111 SCM_data.C=AddC(params.ParameterSet(:,1),model,operator,innerproduct_matrix,SCM_data);
112 SCM_data.params.ParameterSet(:,1)=[];
113 NumberOfParameters = NumberOfParameters-1;
114 %% SCM Greedy
115 n=0;
116 eta=params.eps+1;
117 %% Greedy Parallel
118 if params.parallel >= 1
119  % s = matlabpool('size'); %OLDER MATLAB VERSION
120  % if s == 0
121  % matlabpool('open');
122  % end;
123  if isempty(gcp('nocreate'))
124  parpool;
125  end;
126  %reduce parallel data amount
127  ParameterSet = SCM_data.params.ParameterSet;
128  while params.eps <= eta
129  n=n+1;
130  eta_temp=zeros(NumberOfParameters,1);
131  parfor i = 1:NumberOfParameters
132  alpha_UB = scm_ub(ParameterSet(:,i),model,operator,SCM_data);
133  alpha_LB = scm_coercive_lb(ParameterSet(:,i),model,opereator,SCM_data);
134  eta_temp(i) = (alpha_UB-alpha_LB)/(alpha_UB);
135  end
136 
137  [eta,index]=max(eta_temp);
138 
139  SCM_data.C=AddC(ParameterSet(:,index),model,operator,innerproduct_matrix,SCM_data);
140  ParameterSet(:,index) = [];
141  NumberOfParameters = NumberOfParameters-1;
142 
143  disp(['-- SCM: step ' num2str(n) ' - error:' num2str(eta)]);
144  if n >= params.max_iterations
145  break
146  end;
147  end
148 
149  SCM_data.params.ParameterSet=ParameterSet;
150  if params.parallel == 2
151  % matlabpool('close'); %OLDER MATLAB VERSION
152  delete(gcp('nocreate'))
153  end
154 else
155 
156  %% non-parallel version
157  while params.eps <= eta
158  n=n+1;
159  eta_temp=zeros(NumberOfParameters,1);
160  for i = 1:NumberOfParameters
161  alpha_UB = scm_ub(SCM_data.params.ParameterSet(:,i),model,operator,SCM_data);
162  alpha_LB = scm_coercive_lb(SCM_data.params.ParameterSet(:,i),model,operator,SCM_data);
163  eta_temp(i) = (alpha_UB-alpha_LB)/(alpha_UB);
164  end
165 
166  [eta,index]=max(eta_temp);
167 
168  SCM_data.C=AddC(SCM_data.params.ParameterSet(:,index),model,operator,innerproduct_matrix,SCM_data);
169 
170  SCM_data.params.ParameterSet(:,index) = [];
171  NumberOfParameters = NumberOfParameters-1;
172 
173  disp(['-- SCM: step ' num2str(n) ' - error:' num2str(eta)]);
174  if n >= SCM_data.params.max_iterations
175  break
176  end;
177  end
178 
179 end
180 
181 
182 
183 end
184 
185 
186 function C=AddC(mu,model,operator,innerproduct_matrix,SCM_data)
187 
188 if isempty(SCM_data.C)
189  C.Set = mu;
190  model = set_mu(model,mu);
191  model.decomp_mode=0;
192  [A,~] = operator(model);
193  A=remove_dirichlet(A,0,SCM_data.params.dirichlet_gids);
194  A=0.5*(A+transpose(A));
195  % [V,D] = eig(full(A),full(innerproduct_matrix));
196  try
197  [v,d] = fast_eigs( A,innerproduct_matrix,'sm' );
198  catch err
199  try
200  [v,d] = eigs( A,innerproduct_matrix,1,'sm' );
201  catch err2
202  warning('full spectrum needed, since eigs did not converged');
203  [v,d] = eig( full(A),full(innerproduct_matrix) );
204  [d,i]=min(max(d));
205  v=v(:,i);
206  end
207  end
208  C.coercivity = d;% min(max(D));
209 
210  model.decomp_mode=2;
211  [A_para,~] = operator(model);
212  C.ParameterVector = A_para';
213  %v=V(:,end);
214  model.decomp_mode=1;
215  [A_Matrix,~] = operator(model);
216  A_Matrix=remove_dirichlet(A_Matrix,1,SCM_data.params.dirichlet_gids);
217  y_UB=zeros(SCM_data.params.Q,1);
218  norm_value = v'*innerproduct_matrix*v;
219  for i = 1:SCM_data.params.Q
220  y_UB(i) = v'*0.5*(A_Matrix{1,i}+transpose(A_Matrix{1,i}))*v/norm_value;
221  end
222  C.UBVector = y_UB;
223 else
224  C.Set = [SCM_data.C.Set mu];
225  model = set_mu(model,mu);
226  model.decomp_mode=0;
227  [A,~] = operator(model);
228  A=remove_dirichlet(A,0,SCM_data.params.dirichlet_gids);
229  A=0.5*(A+transpose(A));
230  %[V,D] = eig(full(A),full(innerproduct_matrix));
231  try
232  [v,d] = fast_eigs( A,innerproduct_matrix,'sm' );
233  catch err
234  try
235  [v,d] = eigs( A,innerproduct_matrix,1,'sm' );
236  catch err2
237  warning('full spectrum needed, since eigs did not converged');
238  [v,d] = eig( full(A),full(innerproduct_matrix) );
239  [d,i]=min(max(d));
240  v=v(:,i);
241  end
242  end
243  C.coercivity = [SCM_data.C.coercivity;d]; %min(max(D))];
244 
245  model.decomp_mode=2;
246  [A,~] = operator(model);
247  C.ParameterVector = [SCM_data.C.ParameterVector;A(1:SCM_data.params.Q)'];
248  %v=V(:,end);
249  model.decomp_mode=1;
250  [A_Matrix,~] = operator(model);
251  A_Matrix=remove_dirichlet(A_Matrix,1,SCM_data.params.dirichlet_gids);
252  y_UB=zeros(SCM_data.params.Q,1);
253  norm_value = v'*innerproduct_matrix*v;
254  for i = 1:SCM_data.params.Q
255  y_UB(i) = v'*0.5*(A_Matrix{1,i}+transpose(A_Matrix{1,i}))*v/norm_value;
256  end
257  C.UBVector = [SCM_data.C.UBVector y_UB];
258 
259 end
260 
261 end
262 
263 %% Upperbound computation
264 
265 function alpha_UB = scm_ub(mu,model,operator,SCM_data)
266 
267 model = set_mu(model,mu);
268 model.decomp_mode=2;
269 [theta_q,~] = operator(model);
270 alpha_UB=min(theta_q'*SCM_data.C.UBVector);
271 
272 end
273 
274 
275 %%
276 
277 function A=remove_dirichlet(A,mode,I)
278 if mode == 0
279  A(I,:) = [];
280  A(:,I)= [];
281 else
282  for i = 1:length(A)
283  A{1,i}(I,:) = [];
284  A{1,i}(:,I) = [];
285  end
286 end
287 end
function alpha_LB = scm_coercive_lb(mu, model, operator, SCM_data)
alpha_LB = scm_lb(mu,model,model_data) computation of the lowerbound of the coercive constant equal t...
function SCM_data = scm_coercive_greedy(model, params, operator, innerproduct_matrix)
model=scm_greedy(model, model_data)
Customizable implementation of an abstract greedy algorithm.
Definition: DuneRBLeafNode.m:1