OptFEM2D Toolbox for Matlab  V1.2b1
Matlab/Octave Optimized P1-Lagrange Finite Element Method in 2D
 All Files Functions Pages
validStiffP1.m
Go to the documentation of this file.
1 function validStiffP1()
2 % function validStiffP1()
3 % Validation function for the assembly of the stiffness matrix for
4 % `P_1`-Lagrange finite element method
5 %
6 % The Stiffness Matrix `\Stiff` is given by
7 % ``\Stiff_{i,j}=\int_\DOMH \DOT{\GRAD\FoncBase_i(\q)}{\GRAD\FoncBase_j(\q)}d\q,\ \forall (i,j)\in\ENS{1}{\nq}^2``
8 % where `\FoncBase_i` are `P_1`-Lagrange basis functions.
9 % This Matrix is computed by functions StiffAssemblingP1{Version} where {Version} is one of
10 % 'base', 'OptV0', 'OptV1' and 'OptV2'.
11 % - Test 1: Computation of the Stiffness Matrix using all the versions giving errors and cputimes
12 % - Test 2: Computation of the integral ``\int_\DOM \DOT{\GRAD u(\q)}{\GRAD v(\q)}d\q \approx \DOT{\Stiff \vecb{U}}{\vecb{V}}``
13 % where `\vecb{U}_i=u(\q^i)` and `\vecb{V}_i=v(\q^i)`.
14 % Functions `u` and `v` are those defined in #valid_FEMmatrices.
15 % - Test 3: One retrieves the order 2 of `P_1`-Lagrange integration
16 % ``|\int_\DOM \DOT{\GRAD u}{\GRAD v} -\DOT{\GRAD \Pi_h(u)}{\GRAD \Pi_h(v)}d\DOM| \leq C h^2``
17 %
18 % See also:
22 %
23 % Results:
24 % \image html images/validStiffP1.png "figure : validStiffP1 Test 3 result"
25 %
26 % @author François Cuvelier @date 2012-11-26
27 %
28 % OptFEM2DP1 [V1.2b1] - Copyright (C) 2013 CJS (LAGA)
29 %
30 % This file is part of OptFEM2DP1.
31 % OptFEM2DP1 is free software: you can redistribute it and/or modify
32 % it under the terms of the GNU General Public License as published by
33 % the Free Software Foundation, either version 3 of the License, or
34 % (at your option) any later version.
35 %
36 % OptFEM2DP1 is distributed in the hope that it will be useful,
37 % but WITHOUT ANY WARRANTY; without even the implied warranty of
38 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39 % GNU General Public License for more details.
40 %
41 % You should have received a copy of the GNU General Public License
42 % along with this program. If not, see <http://www.gnu.org/licenses/>.
43 
44  disp('*******************************************')
45  disp('* Stiff Assembling P1 validations *')
46  disp('*******************************************')
47 
48  Th=SquareMesh(50);
49 
50 % TEST 1
51  disp('-----------------------------------------')
52  disp(' Test 1: Matrices errors and CPU times ')
53  disp('-----------------------------------------')
54  tic();
55  Mbase=StiffAssemblingP1base(Th.nq,Th.nme,Th.q,Th.me,Th.areas);
56  T(1)=toc();
57  tic();
58  MOptV0=StiffAssemblingP1OptV0(Th.nq,Th.nme,Th.q,Th.me,Th.areas);
59  T(2)=toc();
60  Test1.error(1)=norm(Mbase-MOptV0,Inf);
61  Test1.name{1}='StiffAssemblingP1OptV0';
62  fprintf(' Error P1base vs OptV0 : %e\n',Test1.error(1))
63  tic();
64  MOptV1=StiffAssemblingP1OptV1(Th.nq,Th.nme,Th.q,Th.me,Th.areas);
65  T(3)=toc();
66  Test1.error(2)=norm(Mbase-MOptV1,Inf);
67  Test1.name{2}='StiffAssemblingP1OptV1';
68  fprintf(' Error P1base vs OptV1 : %e\n',Test1.error(2))
69  tic();
70  MOptV2=StiffAssemblingP1OptV2(Th.nq,Th.nme,Th.q,Th.me,Th.areas);
71  T(4)=toc();
72  Test1.error(3)=norm(Mbase-MOptV2,Inf);
73  Test1.name{3}='StiffAssemblingP1OptV2';
74  fprintf(' Error P1base vs OptV2 : %e\n',Test1.error(3))
75 
76  fprintf(' CPU times base (ref) : %3.4f (s)\n',T(1))
77  fprintf(' CPU times OptV0 : %3.4f (s) - Speed Up X%3.3f\n',T(2),T(1)/T(2))
78  fprintf(' CPU times OptV1 : %3.4f (s) - Speed Up X%3.3f\n',T(3),T(1)/T(3))
79  fprintf(' CPU times OptV2 : %3.4f (s) - Speed Up X%3.3f\n',T(4),T(1)/T(4))
80  checkTest1(Test1)
81 
82  M=Mbase;
83 
84 % TEST 2
85  disp('-----------------------------------------------------')
86  disp(' Test 2: Validations by integration on [0,1]x[0,1] ')
87  disp('-----------------------------------------------------')
88  Test=valid_FEMmatrices();
89  for kk=1:length(Test)
90  U=Test(kk).u(Th.q(1,:),Th.q(2,:));
91  V=Test(kk).v(Th.q(1,:),Th.q(2,:));
92  Test(kk).error=abs(Test(kk).Stiff-U*M*V');
93  fprintf(' functions %d : u(x,y)=%s, v(x,y)=%s,\n -> Stiff error=%e\n',kk,Test(kk).cu,Test(kk).cv,Test(kk).error);
94  end
95  checkTest2(Test)
96 
97 % TEST 3
98  disp('--------------------------------')
99  disp(' Test 3: Validations by order ')
100  disp('--------------------------------')
101  n=length(Test);
102  u=Test(n).u;
103  v=Test(n).v;
104  ExSol=Test(n).Stiff;
105 
106  for k=1:10
107  Th=SquareMesh(50*k+50);
108  fprintf(' Matrix size : %d\n',Th.nq);
109  h(k)=GetMaxLengthEdges(Th.q,Th.me);
110  tic();
111  M=StiffAssemblingP1OptV2(Th.nq,Th.nme,Th.q,Th.me,Th.areas);
112  TT(k)=toc();
113  U=u(Th.q(1,:),Th.q(2,:));
114  V=v(Th.q(1,:),Th.q(2,:));
115  Error(k)=abs(ExSol-U*M*V');
116  fprintf(' StiffAssemblingP1OptV2 CPU times : %3.3f(s)\n',TT(k));
117  fprintf(' Error : %e\n',Error(k));
118  end
119 
120  loglog(h,Error,'+-r',h,h*1.1*Error(1)/h(1),'-sm',h,1.1*Error(1)*(h/h(1)).^2,'-db')
121  legend('Error','O(h)','O(h^2)')
122  xlabel('h')
123  title('Test 3 : Stiffness Matrix')
124  checkTest3(h,Error)
125 end
126 
127 function checkTest1(Test)
128  I=find(Test.error>1e-14);
129  if isempty(I)
130  disp('------------------------')
131  disp(' Test 1 (results): OK')
132  disp('------------------------')
133  else
134  disp('----------------------------')
135  disp(' Test 1 (results): FAILED')
136  disp('----------------------------')
137  end
138 end
139 
140 function checkTest2(Test)
141  N=length(Test);
142  cntFalse=0;
143  for k=1:N
144  if (Test(k).degree<=1)
145  if (Test(k).error>1e-12)
146  cntFalse=cntFalse+1;
147  end
148  end
149  end
150  if (cntFalse==0)
151  disp('------------------------')
152  disp(' Test 2 (results): OK')
153  disp('------------------------')
154  else
155  disp('----------------------------')
156  disp(' Test 2 (results): FAILED')
157  disp('----------------------------')
158  end
159 end
160 
161 function checkTest3(h,error)
162  % order 2
163  P=polyfit(log(h),log(error),1);
164  if abs(P(1)-2)<1e-2
165  disp('------------------------')
166  disp(' Test 3 (results): OK')
167  fprintf(' -> found numerical order %f. Must be 2\n',P(1))
168  disp('------------------------')
169  else
170  disp('----------------------------')
171  disp(' Test 3 (results): FAILED')
172  fprintf(' -> found numerical order %f. Must be 2\n',P(1))
173  disp('----------------------------')
174  end
175 end