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