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