OptFEM2DP1 Toolbox  V1.2b3
Matlab/Octave Optimized P1-Lagrange Finite Element Method in 2D
 All Files Functions Pages
validStiffP1.m
Go to the documentation of this file.
1 function validStiffP1()
2 % function validStiffP1()
3 % Validation function for the assembly of the stiffness matrix for
4 % `P_1`-Lagrange finite element method
5 %
6 % The Stiffness Matrix `\Stiff` is given by
7 % ``\Stiff_{i,j}=\int_\DOMH \DOT{\GRAD\FoncBase_i(\q)}{\GRAD\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 StiffAssemblingP1{Version} where {Version} is one of
10 % 'base', 'OptV0', 'OptV1' and 'OptV2'.
11 % - Test 1: Computation of the Stiffness Matrix using all the versions giving errors and computation times
12 % - Test 2: Computation of the integral ``\int_\DOM \DOT{\GRAD u(\q)}{\GRAD v(\q)}d\q \approx \DOT{\Stiff \vecb{U}}{\vecb{V}}``
13 % where `\vecb{U}_i=u(\q^i)` and `\vecb{V}_i=v(\q^i)`.
14 % Functions `u` and `v` are those defined in #valid_FEMmatrices.
15 % - Test 3: One retrieves the order 2 of `P_1`-Lagrange integration
16 % ``|\int_\DOM \DOT{\GRAD u}{\GRAD v} -\DOT{\GRAD \Pi_h(u)}{\GRAD \Pi_h(v)}d\DOM| \leq C h^2``
17 %
18 % See also:
22 %
23 % Results:
24 % \image html images/validStiffP1.png "figure : validStiffP1 Test 3 result"
25 %
26 % @author François Cuvelier @date 2012-11-26
27 % Copyright:
28 % See \ref license
29 
30  disp('*******************************************')
31  disp('* Stiff Assembling P1 validations *')
32  disp('*******************************************')
33 
34  Th=SquareMesh(50);
35 
36 % TEST 1
37  disp('-----------------------------------------')
38  disp(' Test 1: Matrices errors and CPU times ')
39  disp('-----------------------------------------')
40  tic();
41  Mbase=StiffAssemblingP1base(Th.nq,Th.nme,Th.q,Th.me,Th.areas);
42  T(1)=toc();
43  tic();
44  MOptV0=StiffAssemblingP1OptV0(Th.nq,Th.nme,Th.q,Th.me,Th.areas);
45  T(2)=toc();
46  Test1.error(1)=norm(Mbase-MOptV0,Inf);
47  Test1.name{1}='StiffAssemblingP1OptV0';
48  fprintf(' Error P1base vs OptV0 : %e\n',Test1.error(1))
49  tic();
50  MOptV1=StiffAssemblingP1OptV1(Th.nq,Th.nme,Th.q,Th.me,Th.areas);
51  T(3)=toc();
52  Test1.error(2)=norm(Mbase-MOptV1,Inf);
53  Test1.name{2}='StiffAssemblingP1OptV1';
54  fprintf(' Error P1base vs OptV1 : %e\n',Test1.error(2))
55  tic();
56  MOptV2=StiffAssemblingP1OptV2(Th.nq,Th.nme,Th.q,Th.me,Th.areas);
57  T(4)=toc();
58  Test1.error(3)=norm(Mbase-MOptV2,Inf);
59  Test1.name{3}='StiffAssemblingP1OptV2';
60  fprintf(' Error P1base vs OptV2 : %e\n',Test1.error(3))
61 
62  fprintf(' CPU times base (ref) : %3.4f (s)\n',T(1))
63  fprintf(' CPU times OptV0 : %3.4f (s) - Speed Up X%3.3f\n',T(2),T(1)/T(2))
64  fprintf(' CPU times OptV1 : %3.4f (s) - Speed Up X%3.3f\n',T(3),T(1)/T(3))
65  fprintf(' CPU times OptV2 : %3.4f (s) - Speed Up X%3.3f\n',T(4),T(1)/T(4))
66  checkTest1(Test1)
67 
68  M=Mbase;
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  Test(kk).error=abs(Test(kk).Stiff-U*M*V');
79  fprintf(' functions %d : u(x,y)=%s, v(x,y)=%s,\n -> Stiff error=%e\n',kk,Test(kk).cu,Test(kk).cv,Test(kk).error);
80  end
81  checkTest2(Test)
82 
83 % TEST 3
84  disp('--------------------------------')
85  disp(' Test 3: Validations by order ')
86  disp('--------------------------------')
87  n=length(Test);
88  u=Test(n).u;
89  v=Test(n).v;
90  ExSol=Test(n).Stiff;
91 
92  for k=1:10
93  Th=SquareMesh(50*k+50);
94  fprintf(' Matrix size : %d\n',Th.nq);
95  h(k)=GetMaxLengthEdges(Th.q,Th.me);
96  tic();
97  M=StiffAssemblingP1OptV2(Th.nq,Th.nme,Th.q,Th.me,Th.areas);
98  TT(k)=toc();
99  U=u(Th.q(1,:),Th.q(2,:));
100  V=v(Th.q(1,:),Th.q(2,:));
101  Error(k)=abs(ExSol-U*M*V');
102  fprintf(' StiffAssemblingP1OptV2 CPU times : %3.3f(s)\n',TT(k));
103  fprintf(' Error : %e\n',Error(k));
104  end
105 
106  loglog(h,Error,'+-r',h,h*1.1*Error(1)/h(1),'-sm',h,1.1*Error(1)*(h/h(1)).^2,'-db')
107  legend('Error','O(h)','O(h^2)')
108  xlabel('h')
109  title('Test 3 : Stiffness Matrix')
110  checkTest3(h,Error)
111 end
112 
113 function checkTest1(Test)
114  I=find(Test.error>1e-14);
115  if isempty(I)
116  disp('------------------------')
117  disp(' Test 1 (results): OK')
118  disp('------------------------')
119  else
120  disp('----------------------------')
121  disp(' Test 1 (results): FAILED')
122  disp('----------------------------')
123  end
124 end
125 
126 function checkTest2(Test)
127  N=length(Test);
128  cntFalse=0;
129  for k=1:N
130  if ( ismember(Test(k).degree,[0 1]) )
131  if (Test(k).error>1e-12)
132  cntFalse=cntFalse+1;
133  end
134  end
135  end
136  if (cntFalse==0)
137  disp('------------------------')
138  disp(' Test 2 (results): OK')
139  disp('------------------------')
140  else
141  disp('----------------------------')
142  disp(' Test 2 (results): FAILED')
143  disp('----------------------------')
144  end
145 end
146 
147 function checkTest3(h,error)
148  % order 2
149  P=polyfit(log(h),log(error),1);
150  if abs(P(1)-2)<1e-2
151  disp('------------------------')
152  disp(' Test 3 (results): OK')
153  fprintf(' -> found numerical order %f. Must be 2\n',P(1))
154  disp('------------------------')
155  else
156  disp('----------------------------')
157  disp(' Test 3 (results): FAILED')
158  fprintf(' -> found numerical order %f. Must be 2\n',P(1))
159  disp('----------------------------')
160  end
161 end