OptFEM2DP1 Toolbox  V1.2b3
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 computation times
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 % Copyright:
27 % See \ref license
28 
29  disp('*******************************************')
30  disp('* MassW Assembling P1 validations *')
31  disp('*******************************************')
32 
33  Th=SquareMesh(50);
34 
35 
36 % TEST 1
37  disp('-----------------------------------------')
38  disp(' Test 1: Matrices errors and CPU times ')
39  disp('-----------------------------------------')
40  w=@(x,y) cos(x+y);
41  Tw=w(Th.q(1,:),Th.q(2,:));
42  tic();
43  Mbase=MassWAssemblingP1base(Th.nq,Th.nme,Th.me,Th.areas,Tw);
44  T(1)=toc();
45  tic();
46  MOptV0=MassWAssemblingP1OptV0(Th.nq,Th.nme,Th.me,Th.areas,Tw);
47  T(2)=toc();
48  Test1.error(1)=norm(Mbase-MOptV0,Inf);
49  Test1.name{1}='MassWAssemblingP1OptV0';
50  fprintf(' Error P1base vs OptV0 : %e\n',Test1.error(1))
51  tic();
52  MOptV1=MassWAssemblingP1OptV1(Th.nq,Th.nme,Th.me,Th.areas,Tw);
53  T(3)=toc();
54  Test1.error(2)=norm(Mbase-MOptV1,Inf);
55  Test1.name{2}='MassWAssemblingP1OptV1';
56  fprintf(' Error P1base vs OptV1 : %e\n',Test1.error(2))
57  tic();
58  MOptV2=MassWAssemblingP1OptV2(Th.nq,Th.nme,Th.me,Th.areas,Tw);
59  T(4)=toc();
60  Test1.error(3)=norm(Mbase-MOptV2,Inf);
61  Test1.name{3}='MassWAssemblingP1OptV2';
62  fprintf(' Error P1base vs OptV2 : %e\n',Test1.error(3))
63 
64  fprintf(' CPU times base (ref) : %3.4f (s)\n',T(1))
65  fprintf(' CPU times OptV0 : %3.4f (s) - Speed Up X%3.3f\n',T(2),T(1)/T(2))
66  fprintf(' CPU times OptV1 : %3.4f (s) - Speed Up X%3.3f\n',T(3),T(1)/T(3))
67  fprintf(' CPU times OptV2 : %3.4f (s) - Speed Up X%3.3f\n',T(4),T(1)/T(4))
68  checkTest1(Test1)
69 
70 % TEST 2
71  disp('-----------------------------------------------------')
72  disp(' Test 2: Validations by integration on [0,1]x[0,1] ')
73  disp('-----------------------------------------------------')
74  Test=valid_FEMmatrices();
75  for kk=1:length(Test)
76  U=Test(kk).u(Th.q(1,:),Th.q(2,:));
77  V=Test(kk).v(Th.q(1,:),Th.q(2,:));
78  Tw=Test(kk).w(Th.q(1,:),Th.q(2,:));
79  M=MassWAssemblingP1OptV2(Th.nq,Th.nme,Th.me,Th.areas,Tw);
80  Test(kk).error=abs(Test(kk).MassW-U*M*V');
81  fprintf(' function %d : u(x,y)=%s, v(x,y)=%s, w(x,y)=%s\n -> MassW error=%e\n', ...
82  kk,Test(kk).cu,Test(kk).cv,Test(kk).cw,Test(kk).error);
83  end
84  checkTest2(Test)
85 
86 % TEST 3
87  disp('--------------------------------')
88  disp(' Test 3: Validations by order ')
89  disp('--------------------------------')
90  n=length(Test);
91  u=Test(n).u;
92  v=Test(n).v;
93  w=Test(n).w;
94  ExSol=Test(n).MassW;
95 
96  for k=1:10
97  Th=SquareMesh(50*k+50);
98  Tw=w(Th.q(1,:),Th.q(2,:));
99  fprintf(' Matrix size : %d\n',Th.nq);
100  h(k)=GetMaxLengthEdges(Th.q,Th.me);
101  tic();
102  M=MassWAssemblingP1OptV2(Th.nq,Th.nme,Th.me,Th.areas,Tw);
103  TT(k)=toc();
104  U=u(Th.q(1,:),Th.q(2,:));
105  V=v(Th.q(1,:),Th.q(2,:));
106  Error(k)=abs(ExSol-U*M*V');
107  fprintf(' MassWAssemblingP1OptV2 CPU times : %3.3f(s)\n',TT(k));
108  fprintf(' Error : %e\n',Error(k));
109  end
110 
111  loglog(h,Error,'+-r',h,h*1.1*Error(1)/h(1),'-sm',h,1.1*Error(1)*(h/h(1)).^2,'-db')
112  legend('Error','O(h)','O(h^2)')
113  xlabel('h')
114  title('Test 3 : MassW Matrix')
115  checkTest3(h,Error)
116 end
117 
118 function checkTest1(Test)
119  I=find(Test.error>1e-14);
120  if isempty(I)
121  disp('------------------------')
122  disp(' Test 1 (results): OK')
123  disp('------------------------')
124  else
125  disp('----------------------------')
126  disp(' Test 1 (results): FAILED')
127  disp('----------------------------')
128  end
129 end
130 
131 function checkTest2(Test)
132  N=length(Test);
133  cntFalse=0;
134  for k=1:N
135  if ( ismember(Test(k).degree,[0 1]) )
136  if (Test(k).error>1e-14)
137  cntFalse=cntFalse+1;
138  end
139  end
140  end
141  if (cntFalse==0)
142  disp('------------------------')
143  disp(' Test 2 (results): OK')
144  disp('------------------------')
145  else
146  disp('----------------------------')
147  disp(' Test 2 (results): FAILED')
148  disp('----------------------------')
149  end
150 end
151 
152 function checkTest3(h,error)
153  % order 2
154  P=polyfit(log(h),log(error),1);
155  if ( abs(P(1)-2)<5e-2 )
156  disp('------------------------')
157  disp(' Test 3 (results): OK')
158  fprintf(' -> found numerical order %f. Must be 2\n',P(1))
159  disp('------------------------')
160  else
161  disp('----------------------------')
162  disp(' Test 3 (results): FAILED')
163  fprintf(' -> found numerical order %f. Must be 2\n',P(1))
164  disp('----------------------------')
165  end
166 end