rbmatlab  1.16.09
 All Classes Namespaces Files Functions Variables Modules Pages
ellipt_compliant_detailed_simulation.m
1 function sim_data = ellipt_compliant_detailed_simulation(model,model_data)
2 %function sim_data = ellipt_compliant_detailed_simulation(model,model_data)
3 %
4 % computation of FEM solution for elliptic problem
5 % input: model_data, parameter independent contains grid, p,e,t,
6 % model_data.dl
7 % model.B1/B2
8 % model.gammatop/bottom
9 %
10 % output:
11 % sim_data.bm: boundary condition matrix, dependent of mu
12 % sim_data.u : vector of nodal values of solution in gridpoints
13 % sim_data.s : real value, functional output of problem
14 % sim_data.plot_title: title of the plot
15 %
16 % following data are representation of the elliptic pde
17 % -grad(c*grad u)+a*u=f
18 % sim_data.a: 'reaction' term.
19 % sim_data.f: 'source' term.
20 % sim_data.c: 'diffusivity' term.
21 % sim_data.A_matlab: left hand side, computed with assempde
22 % sim_data.F_matlab: right hand side, computed with assempde
23 % sim_data.M_matlab: Stiffness matrix
24 % sim_data.K_matlab: Stiffness matrix
25 % sim_data.F_matlab: Stiffness matrix
26 % sim_data.Q_matlab: boundary condition (Neumann)
27 % sim_data.G_matlab: boundary condition (Neumann)
28 % sim_data.H_matlab: boundary condition (Dirichlet)
29 % sim_data.B_matlab: boundary condition (Dirichlet)
30 
31 
32 % B. Haasdonk 21.7.2009
33 % S. Langhof
34 
35 
36 
37 sim_data.plot_title='Plot of a detailed simulation';
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
39 %%%%%create boundary matrix
40 
41 %set boundary condition
42 %bottom+top: from left to right
43 %left+right: from bottom to top
44 
45 dl=model_data.dl;
46 
47 noc_bm=length(dl); %number of columns
48 
49 check_left_region = dl(6,:); %number of block left hand side
50 check_right_region = dl(7,:);
51 check_region = dl(6:7,:);
52 
53 zeros_left=find(check_left_region == 0); %find 0's. this edge is an outer
54 zeros_right=find(check_right_region == 0); %boundary
55 
56 nonzeros_left=find(check_left_region);
57 nonzeros_right=find(check_right_region);
58 
59 inner_boundary_index=intersect(nonzeros_left, nonzeros_right); %index of non zero columns in dl
60 
61 zero_index=find(check_region==0);
62 outer_boundary_index=ceil(zero_index/2);
63 outer_boundary_index=outer_boundary_index';
64 
65 check_max_length = zeros(4,max(model.B1,model.B2));
66 for i=1:model.B1
67  check_max_length(1,i)=length(model.gamma_bottom{i}{1})+length(model.gamma_bottom{i}{2})+4; %bottom
68  check_max_length(2,i)=length(model.gamma_top{i}{3})+length(model.gamma_top{i}{4})+8; %top
69 end
70 for l=1:model.B2
71  check_max_length(3,l)=length(model.gamma_left{l}{1})+length(model.gamma_left{l}{2})+4; %left
72  check_max_length(4,l)=length(model.gamma_right{l}{1})+length(model.gamma_right{l}{2})+4; %right
73 end
74 
75 nor_bm=max(max(check_max_length)); %number of rows BM
76 
77 
78 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
79 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80 %%%Boundary matrix
81 BM=zeros(nor_bm, noc_bm); %zeros BM Matrix
82 
83 
84 %%%%% x1/x2, y1/y2 values
85 check_y1=dl(4,:);
86 check_y2=dl(5,:);
87 
88 check_x1=dl(2,:);
89 check_x2=dl(3,:);
90 
91 %find indices of gamma top
92 one_y1_index=find(check_y1 == 1);
93 one_y2_index=find(check_y1 == 1);
94 
95 one_y_index = intersect(one_y1_index, one_y2_index);
96 
97 check_x1_one = check_x1(one_y_index);
98 
99 top_tmp=0;
100 [tmp, top_tmp]=sort(check_x1_one);
101 
102 k=0;
103 top_index=0; %
104 for l=top_tmp
105  k=k+1;
106  top_index(k)=one_y_index(l); %top_index: top indices from left to right
107 end
108 
109 %find indices of gamma bottom
110 
111 zero_y1_index=find(check_y1 == 0);
112 zero_y2_index=find(check_y2 == 0);
113 
114 zero_y_index= intersect(zero_y1_index, zero_y2_index);
115 
116 check_x1_zero=check_x1(zero_y_index);
117 
118 bottom_tmp=0;
119 [tmp, bottom_tmp]=sort(check_x1_zero);
120 k=0;
121 bottom_index=0; %
122 for l=bottom_tmp
123  k=k+1;
124  bottom_index(k)=zero_y_index(l); %bottom index: bottom indices from left to right
125 end
126 
127 %find left index
128 
129 zero_x1_index=find(check_x1 == 0);
130 zero_x2_index=find(check_x2 == 0);
131 
132 zero_x_index=intersect(zero_x1_index, zero_x2_index);
133 
134 check_y1_zero=check_y1(zero_x_index); %
135 
136 left_tmp=0;
137 [tmp, left_tmp]=sort(check_y1_zero);
138 k=0;
139 left_index=0; %
140 for l=left_tmp
141  k=k+1;
142  left_index(k)=zero_x_index(l); %left index: left indices from left to right
143 end
144 
145 %find right index
146 
147 one_x1_index=find(check_x1 == 1);
148 one_x2_index=find(check_x2 == 1);
149 
150 one_x_index=intersect(one_x1_index, one_x2_index);
151 
152 check_y1_one=check_y1(one_x_index); %
153 
154 right_tmp=0;
155 [tmp, right_tmp]=sort(check_y1_one);
156 k=0;
157 right_index=0; %
158 for l=right_tmp
159  k=k+1;
160  right_index(k)=one_x_index(l); %right index: right indices from left to right
161 end
162 
163 
164 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
165 %%%%%%final creation of boundaries
166 
167 %outer bound indices
168 outer_bound=[top_index, bottom_index, right_index, left_index];
169 outer_bound=sort(outer_bound);
170 
171 %inner bound indices
172 tmp_noc_vec=1:noc_bm;
173 for i=outer_bound
174  tmp_noc_vec(i)=-1;
175 end
176 inner_bound=find(tmp_noc_vec >=0);
177 
178 
179 
180 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%55
181 %%%inner boundaries
182 std_inner_boundary_char=['0','0','1','0']; %double(...) -> 48 48 49 48
183 std_inner_boundary_num=[0,repmat(1,1,5)];
184 
185 std_inner_boundary=[std_inner_boundary_num,double(std_inner_boundary_char)];
186 
187 if length(std_inner_boundary) < nor_bm %fill up with zeros
188  std_inner_boundary(nor_bm);
189 end
190 
191 %positioning of inner boundary condition
192 for i=inner_bound
193  BM(:,i)=std_inner_boundary(:);
194 end
195 
196 
197 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
198 %%%outer boundaries
199 
200 %top boundary
201 
202 %max length of vec
203 %l_top_bound=max(check_max_length(2,:));
204 
205 %TOP=zeros(l_top_bound, model.B1);
206 clear fronttail
207 fronttail=repmat(1,1,4); %definition of dim, diri, lq, lg
208 
209 for i=1:model.B1
210  lh=length(model.gamma_top{i}{3}); %number of chars for h
211  lr=length(model.gamma_top{i}{4}); %number of chars for r
212  outer_topboundary_num(i,:)=[fronttail,lh, lr, 48, 48];
213 end
214 
215 outer_topboundary_num=outer_topboundary_num';
216 
217 tmp_char={};
218 for i=1:model.B1
219  tmp_char3=model.gamma_top{i}{3};
220  tmp_char4=model.gamma_top{i}{4};
221 
222  tmp_char{i}=[tmp_char3,tmp_char4]; %
223 end
224 
225 for i=1:model.B1
226  outer_topboundary(:,i)=[outer_topboundary_num(:,i);double(tmp_char{i})'];
227  if length(outer_topboundary(:,i)) < nor_bm
228  outer_topboundary(nor_bm,i) = 0;
229  end
230 end
231 
232 
233 %positioning of top boundary conditions
234 outer_topbound_loop=outer_topboundary;
235 for i=top_index
236  BM(:,i)=outer_topbound_loop(:,1);
237  outer_topbound_loop(:,1)=[];
238 end
239 
240 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
241 %%%bottom boundaries
242 
243 clear fronttail
244 fronttail=[1,0]; %definition of dim, diri, lq, lg
245 
246 for i=1:model.B1
247  lq=length(model.gamma_bottom{i}{1}); %number of chars for
248  lg=length(model.gamma_bottom{i}{2}); %number of chars for
249  outer_bottomboundary_num(i,:)=[fronttail,lq, lg];
250 end
251 
252 outer_bottomboundary_num=outer_bottomboundary_num';
253 
254 tmp_char={};
255 for i=1:model.B1
256  tmp_char1=model.gamma_bottom{i}{1};
257  tmp_char2=model.gamma_bottom{i}{2};
258 
259  tmp_char{i}=[tmp_char1,tmp_char2]; %
260 end
261 
262 a=[];
263 for i=1:model.B1
264  a=[];
265  a=[outer_bottomboundary_num(:,i);double(tmp_char{i})'];
266  if length(a) < nor_bm
267  a(nor_bm);
268  end
269  outer_bottomboundary(:,i)=a;
270 
271 % outer_bottomboundary(:,i)=[outer_bottomboundary_num(:,i);double(tmp_char{i})'];
272 % if length(outer_bottomboundary(:,i)) < nor_bm
273 % outer_bottomboundary(nor_bm,i) = 0;
274 % end
275 end
276 
277 %positioning of bottom boundary conditions
278 outer_bottombound_loop=outer_bottomboundary;
279 for i=bottom_index
280  BM(:,i)=outer_bottombound_loop(:,1);
281  outer_bottombound_loop(:,1)=[];
282 end
283 
284 
285 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
286 %%%%%%left boundaries
287 clear fronttail
288 fronttail=[1,0]; %definition of dim, diri, lq, lg
289 
290 for i=1:model.B2
291  lq=length(model.gamma_left{i}{1}); %number of chars for
292  lg=length(model.gamma_left{i}{2}); %number of chars for
293  outer_leftboundary_num(i,:)=[fronttail,lq, lg];
294 end
295 
296 outer_leftboundary_num=outer_leftboundary_num';
297 
298 tmp_char={};
299 for i=1:model.B2
300  tmp_char1=model.gamma_left{i}{1};
301  tmp_char2=model.gamma_left{i}{2};
302 
303  tmp_char{i}=[tmp_char1,tmp_char2]; %
304 end
305 
306 a=[];
307 for i=1:model.B2
308  a=[];
309  a=[outer_leftboundary_num(:,i);double(tmp_char{i})'];
310  if length(a) < nor_bm
311  a(nor_bm);
312  end
313  outer_leftboundary(:,i)=a;
314 
315 % outer_bottomboundary(:,i)=[outer_bottomboundary_num(:,i);double(tmp_char{i})'];
316 % if length(outer_bottomboundary(:,i)) < nor_bm
317 % outer_bottomboundary(nor_bm,i) = 0;
318 % end
319 end
320 
321 %positioning of left boundary conditions
322 outer_leftbound_loop=outer_leftboundary;
323 for i=left_index
324  BM(:,i)=outer_leftbound_loop(:,1);
325  outer_leftbound_loop(:,1)=[];
326 end
327 
328 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
329 %%%%%%right boundaries
330 clear fronttail
331 fronttail=[1,0]; %definition of dim, diri, lq, lg
332 
333 for i=1:model.B2
334  lq=length(model.gamma_right{i}{1}); %number of chars for
335  lg=length(model.gamma_right{i}{2}); %number of chars for
336  outer_rightboundary_num(i,:)=[fronttail,lq, lg];
337 end
338 
339 outer_rightboundary_num=outer_rightboundary_num';
340 
341 tmp_char={};
342 for i=1:model.B2
343  tmp_char1=model.gamma_right{i}{1};
344  tmp_char2=model.gamma_right{i}{2};
345 
346  tmp_char{i}=[tmp_char1,tmp_char2]; %
347 end
348 
349 a=[];
350 for i=1:model.B2
351  a=[];
352  a=[outer_rightboundary_num(:,i);double(tmp_char{i})'];
353  if length(a) < nor_bm
354  a(nor_bm);
355  end
356  outer_rightboundary(:,i)=a;
357 
358 % outer_bottomboundary(:,i)=[outer_bottomboundary_num(:,i);double(tmp_char{i})'];
359 % if length(outer_bottomboundary(:,i)) < nor_bm
360 % outer_bottomboundary(nor_bm,i) = 0;
361 % end
362 end
363 
364 %positioning of left boundary conditions
365 outer_rightbound_loop=outer_rightboundary;
366 for i=right_index
367  BM(:,i)=outer_rightbound_loop(:,1);
368  outer_rightbound_loop(:,1)=[];
369 end
370 
371 %
372 sim_data.bm=BM;
373 
374 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
375 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
376 
377 % falls stueckweise konstante Diffusivitaet:
378 % modulo sortierung:
379 %
380 % c = ''
381 % for p=1:params.B1*params.B2
382 % mu_p = getfield(model,['mu',num2str(p)])
383 % c = [c,'!',numstr(mu_p)]
384 
385 
386 % allgemeimer:
387 % c = richtige_kombination_und_parameter von model.diffusitivy_ptr(...)
388 
389 
390 %%%%COM Berechnung
391 %for i=1:length(t)
392 % tmp_t=t(1:3,i);
393 % tmp_p=p(:,tmp_t);
394 % tmp_sum=sum(tmp_p,2);
395 % com_t{i}=tmp_sum/3; %CELL ARRAY!!
396 %end
397 
398 % glob = aus com_t konstruieren;
399 
400 
401 %%%%%simplify p,e,t call
402 
403 p=model_data.grid.p;
404 e=model_data.grid.e;
405 t=model_data.grid.t;
406 % Mittelwert von allen Dreiecken:
407 ntria = size(t,2);
408 tp = t(1:3,:);
409 tp(:);
410 px= p(1,tp);
411 px = reshape(px,3,ntria);
412 cx = mean(px)';
413 % selbes spielchen fuer py
414 py=p(2,tp);
415 py = reshape(py,3,ntria);
416 cy = mean(py)';
417 glob = [cx,cy];
418 % Diffusiviy:
419 c = model.diffusivity(glob,model);
420 sim_data.c=c';
421 
422 
423 
424 % Source-Term: f = model.source(glob,model);
425 
426 % ....
427 
428 f=zeros(1,length(c));
429 sim_data.f=f;
430 
431 % Reaction: a = model.reaction(glob,model);
432 
433 a=zeros(1,length(c));
434 sim_data.a=a;
435 
436 % possible to use dir_vals = model.dirichlet_values(dglob,model)
437 
438 % .........
439 % dglob = ...
440 % dir_vals = model.dirichlet_values(dglob,model)
441 % BM = dir_vals ..;
442 
443 % possible to use model.neumann_values(nglob,model) % g_N(x)
444 % .........
445 % nglob = ...
446 % neu_vals = model.neumann_values(bglob,model)
447 % BM = neu_vals ..;
448 
449 % assempde and solve
450 
451 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
452 %%%%%ASSEMBLING+solving%%%%%%%%%%%%%%
453 % assemblieren:
454 A=[];
455 F=[];
456 p=model_data.grid.p;
457 e=model_data.grid.e;
458 t=model_data.grid.t;
459 a=sim_data.a;
460 c=sim_data.c;
461 f=sim_data.f;
462 BM=sim_data.bm;
463 
464 %%assembling system with 'stiff springs' (dirichlet conditions)
465 % [A,F]=assempde(BM,p,e,t,c,a,f); %A stiffness matrix, F right-hand side
466 % u=A\F;
467 % sim_data.u=u;
468 %
469 % sim_data.A_matlab_assempde=A;
470 % sim_data.F_matlab_assempde=F;
471 %%%assembling system, first eliminating diri condi
472 B=[];
473 ud=[];
474 u_tmp=[];
475 [A,F,B,ud]=assempde(BM,p,e,t,c,a,f);
476 u_tmp=A\F;
477 u=B*u_tmp+ud;
478 
479 Afull = speye(size(p,2));
480 non_diri_values=find(p(2,:)~=1);
481 Afull(non_diri_values,non_diri_values) = A;
482 
483 Bfull = ud;
484 Bfull(non_diri_values)= F;
485 ufull = Afull\Bfull;
486 
487 %Afull = A * B';
488 %Bfull = F + A * B' * ud;
489 
490 
491 % A2 = B * A
492 % B
493 
494 %keyboard;
495 
496 sim_data.u=ufull;
497 sim_data.B = Bfull;
498 sim_data.A = Afull;
499 % eventuell ueberfluessig:
500 sim_data.ud=ud;
501 sim_data.u_tmp=u_tmp;
502 sim_data.A_matlab=A;
503 sim_data.F_matlab=F;
504 sim_data.B_matlab=B;
505 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
506 % %split into assembly of area int contribution + boundary condi contribution
507 K=[]; %stiffness matrix
508 M=[]; %mass matrix
509 F=[]; %right-hand side
510 [K,M,F]=assema(p,t,c,a,f);
511 %
512 Q=[];
513 G=[];
514 H=[];
515 B=[];
516 [Q,G,H,B]=assemb(BM,p,e);
517 
518 sim_data.K_matlab = K;
519 sim_data.M_matlab = M;
520 sim_data.F_matlab = F;
521 
522 sim_data.Q_matlab = Q;
523 sim_data.G_matlab = G;
524 sim_data.H_matlab = H;
525 sim_data.B_matlab = B;
526 % %
527 % %%%%%%% 3 different types
528 % %u=assempde(K,M,F,Q,G,H,B);
529 % %sim_data.u=u;
530 % %%%%%%%%%%%%%%%%%%%%%%
531 % K1=[];
532 % F1=[];
533 % [K1,F1]=assempde(K,M,F,Q,G,H,B);
534 % u_split_matlab=K1\F1;
535 % sim_data.A_split_assempde = K1;
536 % sim_data.F_split_assempde = F1;
537 % sim_data.u_split_matlab=u_split_matlab;
538 % %%%%%%%%%%%%%%%%%%%%%%%
539 % K2=[];
540 % F2=[];
541 % B=[];
542 % ud=[];
543 % u_tmp=[];
544 % [K2,F2,B,ud]=assempde(K,M,F,Q,G,H,B);
545 % u_tmp=K2\F2;
546 % u=B*u_tmp+ud;
547 % sim_data.K2_no_stiff = K2;
548 % sim_data.F2_no_stiff = F2;
549 % sim_data.B_no_stiff = B;
550 % sim_data.ud_no_stiff = ud;
551 % sim_data.u_no_stiff_springs=u;
552 
553 
554 
555 %old
556 %% A = ...;
557 %% f = ...;
558 %u=assempde(BM,p,e,t,c,a,f);
559 %% u = A\f;
560 %sim_data.u=u;
561 
562 
563 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5%
564 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
565 % output computation: Integral over boundary segment
566 
567 %sim_data.s=thermalblock_output_function(model.plot_output,model_data)
568 
569 %sim_data.s=thermalblock_output_function(glob,model)
570 
571 %%shortcuts
572 p=model_data.grid.p;
573 e=model_data.grid.e;
574 t=model_data.grid.t;
575 
576 % ausarbeiten des folgenden:
577 
578 calc_mode = model.output_calc_mode;
579 
580 %calc_mode = 2; % 2 only lower edge, 1 all edges
581 
582 if calc_mode == 1
583 ei6 = find(e(6,:)==0); %indices of outer boundary
584 ei7 = find(e(7,:)==0); %edges
585 ei = [ei6,ei7];
586 integral = 0;
587 % glob = Mittelpunkt von Kante / midpoint of edge
588 
589 oe=e(:,ei); %outer boundary edges
590 oestart=oe(1,:); %index of starting point
591 oeend=oe(2,:); %index of ending point
592 
593 poestart=p(:,oestart); %x,y coordinates of correspondign points
594 poeend=p(:,oeend); % '' ''
595 
596 glob=0.5.*(poestart+poeend)';
597 
598 weight = model.output_weight_function(glob,model);
599 % %% eventuell indexmenge von nichtnull-weights bestimmen
600 u_start = sim_data.u(oestart);
601 u_end = sim_data.u(oeend);
602 u_values_in_midpoints = 0.5*(u_start + u_end); %column
603 edge_lengths = sqrt(sum((poeend-poestart).^2)); %row
604 edge_lengths=edge_lengths(:); %column
605 integral = edge_lengths.*u_values_in_midpoints.*weight;
606 
607 sim_data.s=sum(integral);
608 
609 else % calc_mode ==2 %only lower edge
610 
611 zero_y_ind=find(p(2,:)==0); %find indices with y=0
612 
613 x_values=p(1,:);
614 x_value=x_values(zero_y_ind); %corresponding x values with y=0
615 
616 [x_value_sort,i]=sort(x_value); %sort vector
617 glob = [x_value_sort(:),zeros(length(x_value),1)];
618 
619 
620 u_zero_y=u(zero_y_ind); %u values at y=0
621 u_zero_y_sort=u_zero_y(i); %corresponding u values sorted
622 
623 %the following section tests, if the vector x_value contains 0,1, because
624 %we want to compute the integral in the intervall [0,1].
625 if min(x_value) ~= 0
626  warning('x=0 not found!!! extrapolating values!!')
627  x_value=[0,x_value];
628  u_zero=interp1(x_value_sort, u_zero_y_sort,0, 'cubic');
629  u_zero_y_sort=[u_zero,u_zero_y_sort];
630 end
631 
632 if max(x_value) ~= 1
633  warning('x=1 not found!!! extrapolating values!!')
634  x_value=[x_value ,1];
635  u_one = interp1(x_value_sort,u_zero_y_sort,1,'cubic');
636  u_zero_y_sort=[u_zero_y_sort,u_one];
637 end
638 
639 weight_values = model.output_weight_function(glob,model);
640 integrand_values = u_zero_y_sort.* weight_values;
641 sim_data.s = trapz(x_value_sort, integrand_values);
642 
643 end;
644 
645 %if model.plot_output ==1
646 % figure;
647 % plot(x_value_sort, u_zero_y_sort)
648 %end
649 
650 
651 
652 
653 
654 
655