OptFEM2D Toolbox for Matlab  V1.2b1
Matlab/Octave Optimized P1-Lagrange Finite Element Method in 2D
 All Files Functions Pages
validMassWP1.m
Go to the documentation of this file.
1 function validMassWP1()
2 % function validMassWP1()
3 % Validation function for the assembly of the weighted mass matrix
4 % for `P_1`-Lagrange finite elements
5 %
6 % The Weighted Mass Matrix `\MasseF{w}` is given by
7 % ``\MasseF{w}_{i,j}=\int_\DOMH w(\q)\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 MassWAssemblingP1{Version} where {Version} is one of
10 % 'base', 'OptV0', 'OptV1' and 'OptV2'.
11 % - Test 1: Computation of the MassW Matrix using all the versions giving errors and cputimes
12 % - Test 2: Computation of the integral ``\int_\DOM w(x,y) u(x,y) v(x,y) dxdy \approx \DOT{\MasseF{w} \vecb{U}}{\vecb{V}}``
13 % where `\vecb{U}_i=u(\q^i)` and `\vecb{V}_i=v(\q^i)`.
14 % Functions `u`, `v` and `w` are those defined in #valid_FEMmatrices.
15 % - Test 3: One retrieves the order 2 of `P_1`-Lagrange integration ``|\int_\DOM u\,v\,w -\Pi_h(u)\,\Pi_h(v)\,\Pi_h(w)d\DOM| \leq C h^2``
16 %
17 % See also:
18 % #MassWAssemblingP1base, #MassWAssemblingP1OptV0,
19 % #MassWAssemblingP1OptV1, #MassWAssemblingP1OptV2,
20 % #valid_FEMmatrices, #SquareMesh, #GetMaxLengthEdges
21 %
22 % Results:
23 % \image html images/validMassWP1.png "figure : validMassWP1 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('* MassW Assembling P1 validations *')
45  disp('*******************************************')
46 
47  Th=SquareMesh(50);
48 
49 
50 % TEST 1
51  disp('-----------------------------------------')
52  disp(' Test 1: Matrices errors and CPU times ')
53  disp('-----------------------------------------')
54  w=@(x,y) cos(x+y);
55  Tw=w(Th.q(1,:),Th.q(2,:));
56  tic();
57  Mbase=MassWAssemblingP1base(Th.nq,Th.nme,Th.me,Th.areas,Tw);
58  T(1)=toc();
59  tic();
60  MOptV0=MassWAssemblingP1OptV0(Th.nq,Th.nme,Th.me,Th.areas,Tw);
61  T(2)=toc();
62  Test1.error(1)=norm(Mbase-MOptV0,Inf);
63  Test1.name{1}='MassWAssemblingP1OptV0';
64  fprintf(' Error P1base vs OptV0 : %e\n',Test1.error(1))
65  tic();
66  MOptV1=MassWAssemblingP1OptV1(Th.nq,Th.nme,Th.me,Th.areas,Tw);
67  T(3)=toc();
68  Test1.error(2)=norm(Mbase-MOptV1,Inf);
69  Test1.name{2}='MassWAssemblingP1OptV1';
70  fprintf(' Error P1base vs OptV1 : %e\n',Test1.error(2))
71  tic();
72  MOptV2=MassWAssemblingP1OptV2(Th.nq,Th.nme,Th.me,Th.areas,Tw);
73  T(4)=toc();
74  Test1.error(3)=norm(Mbase-MOptV2,Inf);
75  Test1.name{3}='MassWAssemblingP1OptV2';
76  fprintf(' Error P1base vs OptV2 : %e\n',Test1.error(3))
77 
78  fprintf(' CPU times base (ref) : %3.4f (s)\n',T(1))
79  fprintf(' CPU times OptV0 : %3.4f (s) - Speed Up X%3.3f\n',T(2),T(1)/T(2))
80  fprintf(' CPU times OptV1 : %3.4f (s) - Speed Up X%3.3f\n',T(3),T(1)/T(3))
81  fprintf(' CPU times OptV2 : %3.4f (s) - Speed Up X%3.3f\n',T(4),T(1)/T(4))
82  checkTest1(Test1)
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  Tw=Test(kk).w(Th.q(1,:),Th.q(2,:));
93  M=MassWAssemblingP1OptV2(Th.nq,Th.nme,Th.me,Th.areas,Tw);
94  Test(kk).error=abs(Test(kk).MassW-U*M*V');
95  fprintf(' function %d : u(x,y)=%s, v(x,y)=%s, w(x,y)=%s\n -> MassW error=%e\n', ...
96  kk,Test(kk).cu,Test(kk).cv,Test(kk).cw,Test(kk).error);
97  end
98  checkTest2(Test)
99 
100 % TEST 3
101  disp('--------------------------------')
102  disp(' Test 3: Validations by order ')
103  disp('--------------------------------')
104  n=length(Test);
105  u=Test(n).u;
106  v=Test(n).v;
107  w=Test(n).w;
108  ExSol=Test(n).MassW;
109 
110  for k=1:10
111  Th=SquareMesh(50*k+50);
112  Tw=w(Th.q(1,:),Th.q(2,:));
113  fprintf(' Matrix size : %d\n',Th.nq);
114  h(k)=GetMaxLengthEdges(Th.q,Th.me);
115  tic();
116  M=MassWAssemblingP1OptV2(Th.nq,Th.nme,Th.me,Th.areas,Tw);
117  TT(k)=toc();
118  U=u(Th.q(1,:),Th.q(2,:));
119  V=v(Th.q(1,:),Th.q(2,:));
120  Error(k)=abs(ExSol-U*M*V');
121  fprintf(' MassWAssemblingP1OptV2 CPU times : %3.3f(s)\n',TT(k));
122  fprintf(' Error : %e\n',Error(k));
123  end
124 
125  loglog(h,Error,'+-r',h,h*1.1*Error(1)/h(1),'-sm',h,1.1*Error(1)*(h/h(1)).^2,'-db')
126  legend('Error','O(h)','O(h^2)')
127  xlabel('h')
128  title('Test 3 : MassW Matrix')
129  checkTest3(h,Error)
130 end
131 
132 function checkTest1(Test)
133  I=find(Test.error>1e-14);
134  if isempty(I)
135  disp('------------------------')
136  disp(' Test 1 (results): OK')
137  disp('------------------------')
138  else
139  disp('----------------------------')
140  disp(' Test 1 (results): FAILED')
141  disp('----------------------------')
142  end
143 end
144 
145 function checkTest2(Test)
146  N=length(Test);
147  cntFalse=0;
148  for k=1:N
149  if (Test(k).degree<=1)
150  if (Test(k).error>1e-14)
151  cntFalse=cntFalse+1;
152  end
153  end
154  end
155  if (cntFalse==0)
156  disp('------------------------')
157  disp(' Test 2 (results): OK')
158  disp('------------------------')
159  else
160  disp('----------------------------')
161  disp(' Test 2 (results): FAILED')
162  disp('----------------------------')
163  end
164 end
165 
166 function checkTest3(h,error)
167  % order 2
168  P=polyfit(log(h),log(error),1);
169  if abs(P(1)-2)<1e-2
170  disp('------------------------')
171  disp(' Test 3 (results): OK')
172  fprintf(' -> found numerical order %f. Must be 2\n',P(1))
173  disp('------------------------')
174  else
175  disp('----------------------------')
176  disp(' Test 3 (results): FAILED')
177  fprintf(' -> found numerical order %f. Must be 2\n',P(1))
178  disp('----------------------------')
179  end
180 end