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