rbmatlab  1.13.10
 All Classes Namespaces Files Functions Variables Groups Pages
thermal_block.m
Go to the documentation of this file.
1 %step = 1; % single detailed simulation and plot
2 %step = 2; % affine decomposition of FEM-matrix and RHS
3 step = 3; % reduced problem
4 
5 switch step
6 
7  case 1
8  params = [];
9  params.B1 = 4; params.B2 = 3;
10  model = thermalblock_model(params);
11 
12  model = model.set_mu(model,ones(params.B1*params.B2-1,1)); % 1 in each block
13 % model = model.set_mu(model,rand(size(model.get_mu(model)))+0.1); % 1 in each block
14 
15  % the following is parameter-independent
16  model_data = gen_model_data(model);
17 
18  % the following is parameter-dependent
19  %f = figure;
20  sim_data = detailed_simulation(model,model_data);
21 
22  if model.plot_solution == 1
23  f=figure;
24  plot_sim_data(model,model_data,sim_data,[]);
25  end
26 % keyboard;
27 % close(f);
28 
29 % params.B1 = 2; params.B2 = 2;
30 % model = thermalblock_model(params);
31 % model_data = gen_model_data(model);
32 % mu = model.get_mu(model);
33 % mu(:)= 1;
34 % model = model.set_mu(model,mu);
35 % sim_data = detailed_simulation(model,model_data);
36 % plot_sim_data(model,model_data,sim_data,[]);
37 
38  case 2
39 
40  params=[];
41  params.B1 = 4;
42  params.B2 = 3;
43 
44  detailed_data = [];
45  U=[];
46 
47  model=thermalblock_model(params);
48 
49  model.plot_solution = 0;
50 
51  % the following is parameter-independent
52  model_data = gen_model_data(model);
53 
54  %number of simulations (NOS)
55  NOS=5;
56  M = rand_uniform(NOS,model.mu_ranges);
57 % M(size(M,1)+1,:)=1;
58  for k=1:NOS
59 
60  % the following is parameter-dependent
61  %f = figure;
62 
63  % random parameters in model.mu_ranges!!
64  model = model.set_mu(model,M(:,k));
65  %model.mus = model.mus';
66  sim_data = detailed_simulation(model,model_data);
67 
68  if model.plot_solution == 1
69  f=figure;
70  plot_sim_data(model,model_data,sim_data,[]);
71  end
72 
73  %U(:,k)=sim_data.u_no_stiff_springs;
74  U(:,k)=sim_data.u;
75  end
76 
77  % Orthonormierung von U bzgl. H1 Skalarprodukt
78  % .. Skalarprodukt-Matrix: G_ij = int_Omega grad(phi_i(x)) grad(phi_j(x))
79  % Hilfsroutine G = H10_scalar_product_matrix(p,t)
80  % RB = PCA_fixspace(U,[],G);
81 
82  p=model_data.grid.p;
83  e=model_data.grid.e;
84  t=model_data.grid.t;
85 
86  c=sim_data.c;
87  a=sim_data.a;
88  f=sim_data.f;
89  BM=sim_data.bm;
90 
91  Amat = sim_data.A;
92  Bmat = sim_data.B;
93 % Amat = sim_data.A_matlab;
94 % Fmat = sim_data.F_matlab;
95 % Bmat = sim_data.B_matlab;
96 
97  u = sim_data.u;
98  ud = sim_data.ud;
99  u_tmp = sim_data.u_tmp;
100 %
101 % K = sim_data.K_matlab;
102 % M = sim_data.M_matlab;
103 % F = sim_data.F_matlab;
104 %
105 % % Neumann:
106 % Q = sim_data.Q_matlab;
107 % G = sim_data.G_matlab;
108 % % Dirichlet:
109 % H = sim_data.H_matlab;
110 % B = sim_data.B_matlab;
111 %
112  diri_values=find(p(2,:)==1);
113  diri_values(:);
114 
115  ZERO=sparse(length(p), length(p));
116  for i=diri_values
117  ZERO(i,i)=1;
118  end
119  Zero_comp{1,1}=ZERO;
120 
121  G=H10_scalar_product_matrix(p,t,c,a,f);
122 
123  % check: f(x,y) = x*(1-x)*y*(1-y)
124  % H10_norm per hand und DOF-Vektor F durch Punktauswertungen
125  % f = @(x,y) x.*(1-x).*y.*(1-y);
126  % F = f(p(1,:),p(2,:));
127  % H10_norm = F'* G * F;
128  % should be identical to hand-calculation
129 
130  RB = PCA_fixspace(U,[],G);
131 
132 % % % => Ergebnis RB' * G * RB = eye;
133 % % % check result
134 % check=[];
135 % check=RB'*G*RB; %should be eye(NOS);
136 % % trace_check=trace(check); %should be NOS
137 % % matrix_check=sum(sum(check)); %should be NOS
138 % err = max(max(abs(check - eye(NOS))));
139 %
140 % if err < 1e-6
141 % disp('RB passed');
142 % else
143 % disp('RB problem');
144 % end
145 
146  check=[];
147  check=RBcheck(RB,G,NOS);
148  if check ==1
149  disp('RB passed')
150  else
151  disp('RB Prob')
152  end
153 
154  detailed_data.RB = RB;
155 
156  % PARAMETERUNABHAENGIG
157  %
158  % % fem-systemmatrix: komponenten zerlegung, spaerliche Matrizen
159  % % ...
160  %
161  % % glob = dreiecksmittelpunkte
162  glob = get_triangle_midpoints(p,t);
163  % % {c1...c_Qc} = model.diffusivity_components_ptr(glob,model);
164  [C,Cmatrix]=model.diffusivity_components_ptr(glob,model);
165  %
166  % % K1 = assema(c1,...)
167  % % ...
168  % % K_QK = assema(c_QK,...)
169  %
170  % %K_components = { K1, ... KQK};
171 
172 
173 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
174 % %%%adding spring constant
175 % % Some scaling
176 % d1=full(diag(H*H'));
177 % l=find(d1==0);
178 % d1(l)=Inf*ones(size(l));
179 % id1=diag(sparse(1./d1));
180 % % Form normal equations
181 % R=H'*id1*R;
182 % H=H'*id1*H;
183 %
184 % % Incorporate constraints (Dirichlet conditions)
185 % % Find a positive spring constant of suitable size.
186 %
187 % np=size(p,2);
188 % tscale=1/sqrt(np); % Triangle size
189 % kscale=max(max(abs(K)));
190 % mscale=max(max(abs(M)));
191 % qscale=max(max(abs(Q)));
192 % hscale=1;
193 %
194 % % Spring constant
195 % spc=max(max(kscale*tscale^2,mscale),qscale*tscale)/(hscale*tscale^3);
196 %
197 % %M=F+G+spc*R; %R
198 
199 
200 
201 % [K,dummy1,dummy2]=assema(p,t,sim_data.c,a,f);
202 % => K mit loescher der Dirichlet Zeilen und Spalten
203 % identisch zu Amat
204 
205 
206  K_comp=cell(1,size(C,2));
207  for i=1:size(C,2)
208  clear K K1
209  [K,dummy1,dummy2]=assema(p,t,C{1,i}',a,f);
210 % K_comp{1,i}=K;
211 % end
212  %K1=K+M+Q+spc*H; %A
213  K1=K;
214  for j=diri_values
215  K1(j,:);
216  K1(:,j);
217  end
218  K_comp{1,i}=K1; %vorher K
219  end
220  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
221  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
222 
223  %
224  % A_fem_components = {K_compo, M_components, Q_components};
225 
226  % later: M_comp and Q_comp for general elliptic problems
227  %thermalblock case M,Q are zero
228 
229  M_comp = {};
230  Q_comp = {};
231  %A_comp = cell(1,1);
232  A_comp = [Zero_comp,K_comp, Q_comp, M_comp];
233 
234  %
235  % % fem-rechte Seite: komponenten zerlegung
236 
237  %G_i = int_boundary g phi_i
238  %compute with assemb
239  differentneumannvalues=1; %neumann value 1 at the bottom
240  g_comp=cell(1,length(differentneumannvalues));
241  for i=1:differentneumannvalues
242  [dummy1,G,dummy2,dummy3]=assemb(BM,p,e); %BM depends on differentneumannvalues
243 
244  %M1=F+G+spc*R; %R
245  M1=G;
246  g_comp{1,i}=M1; %M1 war vorher G
247  end
248 
249  G_comp=[g_comp];
250  %[G]=model.neumann_components_ptr(glob,model);
251 
252  % G wie vorher, nur in cell array packen:
253  %G = ...
254 
255  %G_comp = {G};
256  %thermalblock f is zero
257  F_comp = {};
258 
259  B_comp = [G_comp, F_comp];
260 
261  % reduced_data darf keine hochdimensionalen Daten enthalten
262  detailed_data.A_components = A_comp;
263  detailed_data.B_components = B_comp;
264 
265  % % check if affine decomposition works:
266 
267  % set mu arbitrary and multiply, different:
268  % model = model.set_mu(...)
269  % sim_data = detailed_simulation(...);
270 
271  % PARAMETERABHAENGIG
272 
273  theta_A = model.diffusivity_coefficients_ptr([],model);
274  theta_A = [theta_A,1];
275 
276  theta_A = [1,theta_A];
277 
278  theta_G = model.neumann_value_coefficients([],model);
279  theta_F = [];
280  theta_B = [theta_G, theta_F];
281 
282  % A = lincomb_sequence(A_comp, theta_A(mu));
283  A = lincomb_sequence(A_comp, theta_A);
284  %A = checkA(A,Amat);
285 
286  B = lincomb_sequence(B_comp, theta_B);
287 
288  u2 = A\B;
289 % u2 = u2+1;
290 
291  pdeplot(p,e,t,'xydata',u,'colormap','jet','contour','on')
292  title('u')
293  figure,pdeplot(p,e,t,'xydata',u2,'colormap','jet','contour','on')
294  title('u2')
295 
296  keyboard;
297 %
298 % % Goal: u2 == sim_data.u;
299 % if u2==sim_data.u
300 % disp('Yipi');
301 % else
302 % error('solutions not equal...');
303 % end;
304 
305 % checkB=abs(R-Fmat);
306 % if find(checkB > 1e-10) %shoud be an empty matrix
307 % disp('Problem with B') %when true, some values are above our limit
308 % else
309 % disp('Eureka! Right hand sides are nearly equal')
310 % end
311 %
312 % checkA=abs(A-Amat);
313 % if find(checkA > 1e-10) %shoud be an empty matrix
314 % disp('Problem with A') %when true, some values are above our limit
315 % else
316 % disp('Eureka! Matrices are nearly equal')
317 % end
318 %
319 % checku=abs(u-u2);
320 % if find(checku > 1e-10) %shoud be an empty matrix
321 % disp('Problem with solution') %when true, some values are above our limit
322 % else
323 % disp('Eureka! Solution are nearly equal')
324 % end
325 
326  case 3
327 
328  % reduzierte Daten erzeugen:
329  for q = 1:length(A_comp)
330  reduced_data.AN_components{q} = RB'*A_comp{q}*RB;
331  end
332  reduced_data.BN_components = {B_comp{1}'*RB};
333 
334 
335  % reduzierte Simulation:
336 % AN = linearcomb(reduced_data.AN_components, theta_A);
337 % BN = linearcomb(reduced_data.BN_components, theta_B);
338 % uN = AN\BN;
339 
340  AN = lincomb_sequence(reduced_data.AN_components, theta_A);
341  BN = lincomb_sequence(reduced_data.BN_components, theta_B);
342  uN = AN\BN(:);
343 
344 
345  u3 = RB * uN;
346 % % reduced output sN = ...
347 %
348 % if (u2==sim_data.u) && (A == sim_data.A) && (B == sim_data.B)
349 % disp('Yipi');
350 % else
351 % error('solutions not equal...');
352 % end;
353 %
354 % % rb_sim_data = rb_simulation(model,reduced_data);
355 % % in elliptic_compliant_rb_simulation
356 % % ... koeffizientenfunktionen auswerten
357 % % ... linearkombinationen bilden
358 %
359 %
360 % %model.RB_generation_mode = 'from_detailed_data';
361 % % detailed_data = gen_detailed_data(model,model_data)
362 %
363 %
364 % % reduzierte basis generieren
365  end;
366 %
367 %
368 % %model = thermalblock_model(params); % struktur, die Einstellungen enthaelt
369 % % oder
370 % %model = thermalblock_model; % struktur, die Einstellungen enthaelt
371 % %model_data = gen_model_data(model); % Gitter anlegen und in model_data speichern
372 % %sim_data = detailed_simulation(model,model_data)
373 %
374 % %pdeplot(model_data.grid.p,model_data.grid.e,model_data.grid.t,'xydata', sim_data.u);
375 
376 
377 
378 
379 
380 
381 
382 
383 
384 
385 
386 
387