OptFEM3DP1 Toolbox  V1.0
Matlab/Octave Optimized P1-Lagrange Finite Element Method in 3D
 All Files Functions Variables Pages
validMass3DP1.m
Go to the documentation of this file.
1 function validMass3DP1()
2 % Copyright:
3 % See \ref license
4 
5 Th=CubeMesh(3);
6 %M=MassAssembling3DP1base(Th.nq,Th.nme,Th.me,Th.volumes);
7 
8 
9  disp('-----------------------------------------')
10  disp(' Test 1: Matrices errors and CPU times ')
11  disp('-----------------------------------------')
12  tic();
13  Mbase=MassAssembling3DP1base(Th.nq,Th.nme,Th.me,Th.volumes);
14  T(1)=toc();
15  tic();
16  MOptV0=MassAssembling3DP1OptV0(Th.nq,Th.nme,Th.me,Th.volumes);
17  T(2)=toc();
18  Test1.error(1)=norm(Mbase-MOptV0,Inf);
19  Test1.name{1}='MassAssembling3DP1OptV0';
20  fprintf(' Error P1base vs OptV0 : %e\n',Test1.error(1))
21  tic();
22  MOptV1=MassAssembling3DP1OptV1(Th.nq,Th.nme,Th.me,Th.volumes);
23  T(3)=toc();
24  Test1.error(2)=norm(Mbase-MOptV1,Inf);
25  Test1.name{2}='MassAssembling3DP1OptV1';
26  fprintf(' Error P1base vs OptV1 : %e\n',Test1.error(2))
27  tic();
28  MOptV2=MassAssembling3DP1OptV2(Th.nq,Th.nme,Th.me,Th.volumes);
29  T(4)=toc();
30  Test1.error(3)=norm(Mbase-MOptV2,Inf);
31  Test1.name{3}='MassAssembling3DP1OptV2';
32  fprintf(' Error P1base vs OptV2 : %e\n',Test1.error(3))
33 
34  fprintf(' CPU times base (ref) : %3.4f (s)\n',T(1))
35  fprintf(' CPU times OptV0 : %3.4f (s) - Speed Up X%3.3f\n',T(2),T(1)/T(2))
36  fprintf(' CPU times OptV1 : %3.4f (s) - Speed Up X%3.3f\n',T(3),T(1)/T(3))
37  fprintf(' CPU times OptV2 : %3.4f (s) - Speed Up X%3.3f\n',T(4),T(1)/T(4))
38  checkTest1(Test1)
39 
40 % TEST 2
41  disp('-----------------------------------------------------------')
42  disp(' Test 2: Validations by integration on [0,1]x[0,1]x[0,1] ')
43  disp('-----------------------------------------------------------')
44 Test=valid_FEMmatrices();
45 M=Mbase;
46 for kk=1:length(Test)
47  U=Test(kk).u(Th.q(1,:),Th.q(2,:),Th.q(3,:));
48  V=Test(kk).v(Th.q(1,:),Th.q(2,:),Th.q(3,:));
49  Test(kk).error=abs(Test(kk).Mass-U*M*V');
50  fprintf(' function %d : u(x,y,z)=%s, v(x,y,z)=%s,\n -> Mass error=%e\n',kk,Test(kk).cu,Test(kk).cv,abs(Test(kk).Mass-U*M*V'));
51 end
52  checkTest2(Test)
53 
54 % TEST 3
55  disp('--------------------------------')
56  disp(' Test 3: Validations by order ')
57  disp('--------------------------------')
58  n=length(Test);
59  u=Test(n).u;
60  v=Test(n).v;
61  ExSol=Test(n).Mass;
62  fprintf(' functions %d : u(x,y,z)=%s, v(x,y,z)=%s,\n',n, ...
63  Test(n).cu,Test(n).cv);
64  fprintf(' Integral of u(x,y,z)v(x,y,z) over unit cube : %.16f\n',ExSol);
65  N=10;
66  for k=1:10
67  Th=CubeMesh(5+k*3);
68  fprintf('(%d/%d) Matrix size : %d\n',k,N,Th.nq);
69  h(k)=GetMaxLengthEdges(Th.q,Th.me);
70  tic();
71  M=MassAssembling3DP1OptV2(Th.nq,Th.nme,Th.me,Th.volumes);
72  TT(k)=toc();
73  U=u(Th.q(1,:),Th.q(2,:),Th.q(3,:));
74  V=v(Th.q(1,:),Th.q(2,:),Th.q(3,:));
75  Error(k)=abs(ExSol-U*M*V');
76  fprintf(' MassAssembling3DP1OptV2 CPU times : %3.3f(s)\n',TT(k));
77  fprintf(' Error : %e\n',Error(k));
78  end
79 
80  loglog(h,Error,'+-r',h,h*1.1*Error(1)/h(1),'-sm',h,1.1*Error(1)*(h/h(1)).^2,'-db')
81  legend('Error','O(h)','O(h^2)')
82  xlabel('h')
83  title('Test 3 : Mass Matrix')
84  checkTest3(h,Error)
85 end
86 
87 
88 function checkTest1(Test)
89  I=find(Test.error>1e-14);
90  if isempty(I)
91  disp('------------------------')
92  disp(' Test 1 (results): OK')
93  disp('------------------------')
94  else
95  disp('----------------------------')
96  disp(' Test 1 (results): FAILED')
97  disp('----------------------------')
98  end
99 end
100 
101 function checkTest2(Test)
102  N=length(Test);
103  cntFalse=0;
104  for k=1:N
105  if (ismember(Test(k).degree,[0 1]))
106  if (Test(k).error>1e-14)
107  cntFalse=cntFalse+1;
108  end
109  end
110  end
111  if (cntFalse==0)
112  disp('------------------------')
113  disp(' Test 2 (results): OK')
114  disp('------------------------')
115  else
116  disp('----------------------------')
117  disp(' Test 2 (results): FAILED')
118  disp('----------------------------')
119  end
120 end
121 
122 function checkTest3(h,error)
123  % order 2
124  P=polyfit(log(h),log(error),1);
125  if abs(P(1)-2)< 0.1
126  disp('------------------------')
127  disp(' Test 3 (results): OK')
128  fprintf(' -> found numerical order %f. Must be 2\n',P(1))
129  disp('------------------------')
130  else
131  disp('----------------------------')
132  disp(' Test 3 (results): FAILED')
133  fprintf(' -> found numerical order %f. Must be 2\n',P(1))
134  disp('----------------------------')
135  end
136 end
137