| OptFEM2DP1 Toolbox V1.2b2 Matlab/Octave Optimized P1-Lagrange Finite Element Method in 2D

- Main Page
- Related Pages
- Files

- File List
- File Members

All Files Functions Pages
validStiffElasP1.m
Go to the documentation of this file.
1 function validStiffElasP1(Num)
2 % function validStiffElasP1(Num)
3 % Validation function for the Assembly of the Stiffness Elasticity
4 % Matrix for `P_1`-Lagrange finite elements.
5 %
6 % The Stiffness Elasticity Matrix is given by
7 % ``\StiffElas_{m,l}=\int_{\DOMH} \Odv^t(\BasisFuncTwoD_m) \Ocv(\BasisFuncTwoD_l)dT, \ \forall (m,l)\in\ENS{1}{2\,\nq}^2,``
8 % where `\BasisFuncTwoD_m` are `P_1`-Lagrange vector basis functions.
9 % Here `\Ocv=(\Occ_{xx},\Occ_{yy},\Occ_{xy})^t` and `\Odv=(\Odc_{xx},\Odc_{yy},2\Odc_{xy})^t`
10 % are the elastic stress and strain tensors respectively.
11 %
12 % This Matrix is computed by functions StiffElasAssemblingP1{Version} where {Version} is one of
13 % 'base', 'OptV0', 'OptV1' and 'OptV2'.
14 % - Test 1: computation of the Elastic Stiffness Matrix using previous functions giving errors and computation times
15 % - Test 2: compute ``\int_\DOM \Odv^t(\vecb{u}) \Ocv(\vecb{v})d\q \approx \DOT{\StiffElas \vecb{U}}{\vecb{V}}``
16 % with `\foncdefsmall{\vecb{u}=(u_1,u_2)}{\DOM}{\R^2}` and `\foncdefsmall{\vecb{v}=(v_1,v_2)}{\DOM}{\R^2}`.
17 % We have `\vecb{U}_{2i-1}=u_1(\q^i),` `\vecb{U}_{2i}=u_2(\q^i),` `\vecb{V}_{2i-1}=v_1(\q^i),` `\vecb{V}_{2i}=v_2(\q^i)`
18 % if Num in {0,2} and `\vecb{U}_{i}=u_1(\q^i),` `\vecb{U}_{i+\nq}=u_2(\q^i),` `\vecb{V}_{i}=v_1(\q^i),` `\vecb{V}_{i+\nq}=v_2(\q^i)`
19 % if Num in {1,3}. Use functions `\vecb{u}` and `\vecb{u}` defined in #valid_StiffElas.
20 % - Test 3: retrieve order 2 of `P_1`-Lagrange integration
21 % ``|\int_\DOM \Odv^t(\vecb{u}) \Ocv(\vecb{v})d\q - \DOT{\StiffElas \vecb{U}}{\vecb{V}}| \leq C h^2``
22 %
23 % Num :
24 % - 0 global alternate numbering with local alternate numbering (classical method),
25 % - 1 global block numbering with local alternate numbering,
26 % - 2 global alternate numbering with local block numbering,
27 % - 3 global block numbering with local block numbering.
28 % See also:
29 % #StiffElasAssemblingP1base, #StiffElasAssemblingP1OptV0,
30 % #StiffElasAssemblingP1OptV1, #StiffElasAssemblingP1OptV2, #SquareMesh
31 %
32 % @author François Cuvelier @date 2012-11-26
33 % Copyright:
34 % See \ref license
35
36 disp('********************************************')
37 disp('* Stiff Elas Assembling P1 validations *')
38 disp('********************************************')
39 switch Num
40 case 0
41 s=sprintf('Global alternate numbering / local alternate numbering');
42 case 1
43 s=sprintf('Global block numbering / local alternate numbering');
44 case 2
45 s=sprintf('Global alternate numbering / local block numbering');
46 case 3
47 s=sprintf('Global block numbering / local block numbering');
48 otherwise
49 error('invalid Num value')
50 end
51 fprintf(' Numbering Choice : %s\n',s);
52
53
54
55 Th=SquareMesh(20);
56
57 % TEST 1
58 disp('-----------------------------------------')
59 disp(' Test 1: Matrices errors and CPU times ')
60 disp('-----------------------------------------')
61 tic();
62 Mbase=StiffElasAssemblingP1base(Th.nq,Th.nme,Th.q,Th.me,Th.areas,1,1,Num);
63 T(1)=toc();
64 tic();
65 MOptV0=StiffElasAssemblingP1OptV0(Th.nq,Th.nme,Th.q,Th.me,Th.areas,1,1,Num);
66 T(2)=toc();
67 Test1.error(1)=norm(Mbase-MOptV0,Inf);
68 Test1.name{1}='StiffElasAssemblingP1OptV0';
69 fprintf(' Error P1base vs OptV0 : %e\n',Test1.error(1))
70 tic();
71 MOptV1=StiffElasAssemblingP1OptV1(Th.nq,Th.nme,Th.q,Th.me,Th.areas,1,1,Num);
72 T(3)=toc();
73 Test1.error(2)=norm(Mbase-MOptV1,Inf);
74 Test1.name{2}='StiffElasAssemblingP1OptV1';
75 fprintf(' Error P1base vs OptV1 : %e\n',Test1.error(2))
76 tic();
77 MOptV2=StiffElasAssemblingP1OptV2(Th.nq,Th.nme,Th.q,Th.me,Th.areas,1,1,Num);
78 T(4)=toc();
79 Test1.error(3)=norm(Mbase-MOptV2,Inf);
80 Test1.name{3}='StiffElasAssemblingP1OptV2';
81 fprintf(' Error P1base vs OptV2 : %e\n',Test1.error(3))
82
83 fprintf(' CPU times base (ref) : %3.4f (s)\n',T(1))
84 fprintf(' CPU times OptV0 : %3.4f (s) - Speed Up X%3.3f\n',T(2),T(1)/T(2))
85 fprintf(' CPU times OptV1 : %3.4f (s) - Speed Up X%3.3f\n',T(3),T(1)/T(3))
86 fprintf(' CPU times OptV2 : %3.4f (s) - Speed Up X%3.3f\n',T(4),T(1)/T(4))
87 checkTest1(Test1)
88
89
90
91
92 % TEST 2
93 disp('-----------------------------------------------------')
94 disp(' Test 2: Validations by integration on [0,1]x[0,1] ')
95 disp('-----------------------------------------------------')
96 Test=valid_StiffElas();
97 qf=Th.q;
98 for kk=1:length(Test)
99 KK=StiffElasAssemblingP1OptV2(Th.nq,Th.nme,Th.q,Th.me,Th.areas,Test(kk).lambda,Test(kk).mu,Num);
100 u=Test(kk).u;
101 v=Test(kk).v;
102 switch Num
103 case {0,2}
104 U=[u{1}(qf(1,:),qf(2,:));u{2}(qf(1,:),qf(2,:))];
105 U=U(:);
106 V=[v{1}(qf(1,:),qf(2,:));v{2}(qf(1,:),qf(2,:))];
107 V=V(:);
108 case {1,3}
109 U=[u{1}(qf(1,:),qf(2,:)) u{2}(qf(1,:),qf(2,:))]';
110 V=[v{1}(qf(1,:),qf(2,:)) v{2}(qf(1,:),qf(2,:))]';
111 end
112 %whos
113 Test(kk).error=abs(Test(kk).Stiff-U'*KK*V);
114 fprintf(' functions %d : u(x,y)=(%s,%s), v(x,y)=(%s,%s),\n -> Stiff error=%e\n', ...
115 kk,Test(kk).cu{1},Test(kk).cu{2},Test(kk).cv{1},Test(kk).cv{2},Test(kk).error);
116 end
117 checkTest2(Test)
118
119 % TEST 3
120 disp('--------------------------------')
121 disp(' Test 3: Validations by order ')
122 disp('--------------------------------')
123 n=length(Test);
124 u=Test(n).u;
125 v=Test(n).v;
126 lambda=Test(n).lambda;
127 mu=Test(n).mu;
128 ExSol=Test(n).Stiff;
129
130 for k=1:10
131 Th=SquareMesh(20*k+50);
132 qf=Th.q;
133 fprintf(' Matrix size : %d\n',Th.nq);
134 h(k)=GetMaxLengthEdges(Th.q,Th.me);
135 tic();
136 M=StiffElasAssemblingP1OptV2(Th.nq,Th.nme,Th.q,Th.me,Th.areas,lambda,mu,Num);
137 TT(k)=toc();
138 switch Num
139 case {0,2}
140 U=[u{1}(qf(1,:),qf(2,:));u{2}(qf(1,:),qf(2,:))];
141 U=U(:);
142 V=[v{1}(qf(1,:),qf(2,:));v{2}(qf(1,:),qf(2,:))];
143 V=V(:);
144 case {1,3}
145 U=[u{1}(qf(1,:),qf(2,:)) u{2}(qf(1,:),qf(2,:))]';
146 V=[v{1}(qf(1,:),qf(2,:)) v{2}(qf(1,:),qf(2,:))]';
147 end
148 Error(k)=abs(ExSol-U'*M*V);
149 fprintf(' StiffElasAssemblingP1OptV2 CPU times : %3.3f(s)\n',TT(k));
150 fprintf(' Error : %e\n',Error(k));
151 end
152
153 loglog(h,Error,'+-r',h,h*1.1*Error(1)/h(1),'-sm',h,1.1*Error(1)*(h/h(1)).^2,'-db')
154 legend('Error','O(h)','O(h^2)')
155 xlabel('h')
156 title(sprintf('Test 3 : Stiff Elas. Matrix (Num=%d)',Num))
157 checkTest3(h,Error)
158
159 %figure(3)
160 %spy(M)
161 %title(sprintf('Matrix sparsity for %s numbering',s))
162 end
163
164 function checkTest1(Test)
165 I=find(Test.error>1e-14);
166 if isempty(I)
167 disp('------------------------')
168 disp(' Test 1 (results): OK')
169 disp('------------------------')
170 else
171 disp('----------------------------')
172 disp(' Test 1 (results): FAILED')
173 disp('----------------------------')
174 end
175 end
176
177 function checkTest2(Test)
178 N=length(Test);
179 cntFalse=0;
180 for k=1:N
181 if (Test(k).degree<=1)
182 if (Test(k).error>1e-12)
183 cntFalse=cntFalse+1;
184 end
185 end
186 end
187 if (cntFalse==0)
188 disp('------------------------')
189 disp(' Test 2 (results): OK')
190 disp('------------------------')
191 else
192 disp('----------------------------')
193 disp(' Test 2 (results): FAILED')
194 disp('----------------------------')
195 end
196 end
197
198 function checkTest3(h,error)
199 % order 2
200 P=polyfit(log(h),log(error),1);
201 if abs(P(1)-2)<1e-3
202 disp('------------------------')
203 disp(' Test 3 (results): OK')
204 fprintf(' -> found numerical order %f. Must be 2\n',P(1))
205 disp('------------------------')
206 else
207 disp('----------------------------')
208 disp(' Test 3 (results): FAILED')
209 fprintf(' -> found numerical order %f. Must be 2\n',P(1))
210 disp('----------------------------')
211 end
212 end