OptFEM2D  0.1
Matlab optimized FEM2D
 All Files Functions Groups Pages
validMassWP1.m
Go to the documentation of this file.
1 function validMassWP1()
2 % function validMassWP1()
3 % Validation function for MassWAssembling P1 functions.
4 %
5 % The Mass Weight Matrix, `\MasseF{w}`, is given by
6 % ``\MasseF{w}_{i,j}=\int_\DOMH w(\q)\FoncBase_i(\q) \FoncBase_j(\q) d\q,\ \forall (i,j)\in\ENS{1}{\nq}^2``
7 % where `\FoncBase_i` are `P_1`-Lagrange basis functions.
8 % This Matrix is computed by functions MassWAssemblingP1{Version} where {Version} is one of
9 % 'base', 'OptV0', 'OptV1' and 'OptV2'.
10 % - Test 1: compute MassW Matrix using previous functions and give errors and cputimes
11 % - Test 2: compute ``\int_\DOM w(x,y) u(x,y) v(x,y) dxdy \approx \DOT{\MasseF{w} \vecb{U}}{\vecb{V}}``
12 % where `\vecb{U}_i=u(\q^i)` and `\vecb{V}_i=v(\q^i)`. Use fonctions `u`, `v` and `w` defined in #valid_FEMmatrices.
13 % - Test 3: retrieve order 2 of `P_1`-Lagrange integration ``|\int_\DOM uvw -\Pi_h(u)\Pi_h(v)\Pi_h(w)d\DOM| \leq C h^2``
14 %
15 % See also:
16 % #MassWAssemblingP1base, #MassWAssemblingP1OptV0, #MassWAssemblingP1OptV1, #MassWAssemblingP1OptV2
17 %
18 % @author Francois Cuvelier @date 2012-11-26
19 
20  disp('*******************************************')
21  disp('* MassW Assembling P1 validations *')
22  disp('*******************************************')
23 
24  Th=SquareMesh(50);
25 
26 
27 % TEST 1
28  disp('-----------------------------------------')
29  disp(' Test 1: Matrices errors and CPU times ')
30  disp('-----------------------------------------')
31  w=@(x,y) cos(x+y);
32  Tw=w(Th.q(1,:),Th.q(2,:));
33  tic();
34  Mbase=MassWAssemblingP1base(Th.nq,Th.nme,Th.me,Th.areas,Tw);
35  T(1)=toc();
36  tic();
37  MOptV0=MassWAssemblingP1OptV0(Th.nq,Th.nme,Th.me,Th.areas,Tw);
38  T(2)=toc();
39  Test1.error(1)=norm(Mbase-MOptV0,Inf);
40  Test1.name{1}='MassWAssemblingP1OptV0';
41  fprintf(' Error P1base vs OptV0 : %e\n',Test1.error(1))
42  tic();
43  MOptV1=MassWAssemblingP1OptV1(Th.nq,Th.nme,Th.me,Th.areas,Tw);
44  T(3)=toc();
45  Test1.error(2)=norm(Mbase-MOptV1,Inf);
46  Test1.name{2}='MassWAssemblingP1OptV1';
47  fprintf(' Error P1base vs OptV1 : %e\n',Test1.error(2))
48  tic();
49  MOptV2=MassWAssemblingP1OptV2(Th.nq,Th.nme,Th.me,Th.areas,Tw);
50  T(4)=toc();
51  Test1.error(3)=norm(Mbase-MOptV2,Inf);
52  Test1.name{3}='MassWAssemblingP1OptV2';
53  fprintf(' Error P1base vs OptV2 : %e\n',Test1.error(3))
54 
55  fprintf(' CPU times base (ref) : %3.4f (s)\n',T(1))
56  fprintf(' CPU times OptV0 : %3.4f (s) - Speed Up X%3.3f\n',T(2),T(1)/T(2))
57  fprintf(' CPU times OptV1 : %3.4f (s) - Speed Up X%3.3f\n',T(3),T(1)/T(3))
58  fprintf(' CPU times OptV2 : %3.4f (s) - Speed Up X%3.3f\n',T(4),T(1)/T(4))
59  checkTest1(Test1)
60 
61 % TEST 2
62  disp('-----------------------------------------------------')
63  disp(' Test 2: Validations by integration on [0,1]x[0,1] ')
64  disp('-----------------------------------------------------')
65  Test=valid_FEMmatrices();
66  for kk=1:length(Test)
67  U=Test(kk).u(Th.q(1,:),Th.q(2,:));
68  V=Test(kk).v(Th.q(1,:),Th.q(2,:));
69  Tw=Test(kk).w(Th.q(1,:),Th.q(2,:));
70  M=MassWAssemblingP1OptV2(Th.nq,Th.nme,Th.me,Th.areas,Tw);
71  Test(kk).error=abs(Test(kk).MassW-U*M*V');
72  fprintf(' function %d : u(x,y)=%s, v(x,y)=%s, w(x,y)=%s\n -> MassW error=%e\n', ...
73  kk,Test(kk).cu,Test(kk).cv,Test(kk).cw,Test(kk).error);
74  end
75  checkTest2(Test)
76 
77 % TEST 3
78  disp('--------------------------------')
79  disp(' Test 3: Validations by order ')
80  disp('--------------------------------')
81  n=length(Test);
82  u=Test(n).u;
83  v=Test(n).v;
84  w=Test(n).w;
85  ExSol=Test(n).MassW;
86 
87  for k=1:10
88  Th=SquareMesh(50*k+50);
89  Tw=w(Th.q(1,:),Th.q(2,:));
90  fprintf(' Matrix size : %d\n',Th.nq);
91  h(k)=GetMaxLengthEdges(Th.q,Th.me);
92  tic();
93  M=MassWAssemblingP1OptV2(Th.nq,Th.nme,Th.me,Th.areas,Tw);
94  TT(k)=toc();
95  U=u(Th.q(1,:),Th.q(2,:));
96  V=v(Th.q(1,:),Th.q(2,:));
97  Error(k)=abs(ExSol-U*M*V');
98  fprintf(' MassWAssemblingP1OptV2 CPU times : %3.3f(s)\n',TT(k));
99  fprintf(' Error : %e\n',Error(k));
100  end
101 
102  loglog(h,Error,'+-k',h,h*1.1*Error(1)/h(1),'-.k',h,1.1*Error(1)*(h/h(1)).^2,'k:')
103  legend('Error','O(h)','O(h^2)')
104  xlabel('h')
105  title('Test 3 : MassW Matrix')
106  checkTest3(h,Error)
107 end
108 
109 function checkTest1(Test)
110  I=find(Test.error>1e-14);
111  if isempty(I)
112  disp('------------------------')
113  disp(' Test 1 (results): OK')
114  disp('------------------------')
115  else
116  disp('----------------------------')
117  disp(' Test 1 (results): FAILED')
118  disp('----------------------------')
119  end
120 end
121 
122 function checkTest2(Test)
123  N=length(Test);
124  cntFalse=0;
125  for k=1:N
126  if (Test(k).degree<=1)
127  if (Test(k).error>1e-14)
128  cntFalse=cntFalse+1;
129  end
130  end
131  end
132  if (cntFalse==0)
133  disp('------------------------')
134  disp(' Test 2 (results): OK')
135  disp('------------------------')
136  else
137  disp('----------------------------')
138  disp(' Test 2 (results): FAILED')
139  disp('----------------------------')
140  end
141 end
142 
143 function checkTest3(h,error)
144  % order 2
145  P=polyfit(log(h),log(error),1);
146  if abs(P(1)-2)<1e-3
147  disp('------------------------')
148  disp(' Test 3 (results): OK')
149  fprintf(' -> found numerical order %f. Must be 2\n',P(1))
150  disp('------------------------')
151  else
152  disp('----------------------------')
153  disp(' Test 3 (results): FAILED')
154  fprintf(' -> found numerical order %f. Must be 2\n',P(1))
155  disp('----------------------------')
156  end
157 end