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