1 import FiniteElements2DDiffusion_SQUARE
2 import matplotlib.pyplot as plt
4 from math import log10, sqrt
7 convergence_synthesis=dict(FiniteElements2DDiffusion_SQUARE.test_desc)
9 def test_validation2DEF_Delaunay_triangles():
11 #### 2D FE Delaunay triangle mesh
12 meshList=['squareWithTriangles_1','squareWithTriangles_2','squareWithTriangles_3','squareWithTriangles_4','squareWithTriangles_5']#linear system is hard to solve for 'squareWithTriangles_5'
13 meshType="Unstructured_triangles"
15 nbMeshes=len(meshList)
16 error_tab=[0]*nbMeshes
17 mesh_size_tab=[0]*nbMeshes
18 mesh_path='../../../ressources/2DTriangles/'
19 mesh_name='squareWithDelaunayTriangles'
20 diag_data=[0]*nbMeshes
25 curv_abs=np.linspace(0,sqrt(2),resolution+1)
28 # Storing of numerical errors, mesh sizes and diagonal values
29 for filename in meshList:
30 error_tab[i], mesh_size_tab[i], diag_data[i], min_tab[i], max_tab[i], time_tab[i] =FiniteElements2DDiffusion_SQUARE.solve(mesh_path+filename, resolution, meshType, testColor)
31 assert min_tab[i]>-0.01
33 plt.plot(curv_abs, diag_data[i], label= str(mesh_size_tab[i]) + ' nodes')
34 error_tab[i]=log10(error_tab[i])
35 time_tab[i]=log10(time_tab[i])
40 # Plot over diagonal line
42 plt.xlabel('Position on diagonal line')
43 plt.ylabel('Value on diagonal line')
44 plt.title('Plot over diagonal line for finite elements \n for Laplace operator on 2D Delaunay triangle meshes')
45 plt.savefig(mesh_name+"_2DDiffusionEF_PlotOverDiagonalLine.png")
48 # Plot of min and max curves
50 plt.plot(mesh_size_tab, min_tab, label='Minimum value')
51 plt.plot(mesh_size_tab, max_tab, label='Maximum value')
53 plt.xlabel('Number of nodes')
55 plt.title('Min/Max of Finite elements \n for Laplace operator on 2D Delaunay triangle meshes')
56 plt.savefig(mesh_name+"_2DDiffusionEF_MinMax.png")
58 for i in range(nbMeshes):
59 mesh_size_tab[i] = 0.5*log10(mesh_size_tab[i])
61 # Least square linear regression
62 # Find the best a,b such that f(x)=ax+b best approximates the convergence curve
63 # The vector X=(a,b) solves a symmetric linear system AX=B with A=(a1,a2\\a2,a3), B=(b1,b2)
64 a1=np.dot(mesh_size_tab,mesh_size_tab)
65 a2=np.sum(mesh_size_tab)
67 b1=np.dot(error_tab,mesh_size_tab)
71 assert det!=0, 'test_validation2DEF_Delaunay_triangles() : Make sure you use distinct meshes and at least two meshes'
75 print( "FE on 2D Delaunay triangle mesh : scheme order is ", -a )
76 assert abs(a+2.12)<0.1
78 # Plot of convergence curves
80 plt.plot(mesh_size_tab, error_tab, label='log(|numerical-exact|)')
81 plt.plot(mesh_size_tab, a*np.array(mesh_size_tab)+b,label='least square slope : '+'%.3f' % a)
83 plt.xlabel('log(sqrt(number of nodes))')
84 plt.ylabel('log(error)')
85 plt.title('Convergence of finite elements \n for Laplace operator on 2D Delaunay triangle meshes')
86 plt.savefig(mesh_name+"_2DDiffusionEF_ConvergenceCurve.png")
88 # Plot of computational time
90 plt.plot(mesh_size_tab, time_tab, label='log(cpu time)')
92 plt.xlabel('log(sqrt(number of nodes))')
93 plt.ylabel('log(cpu time)')
94 plt.title('Computational time of finite elements \n for Laplace operator on 2D Delaunay triangle meshes')
95 plt.savefig(mesh_name+"_2DDiffusionEF_ComputationalTime.png")
99 convergence_synthesis["Mesh_names"]=meshList
100 convergence_synthesis["Mesh_type"]=meshType
101 convergence_synthesis["Mesh_path"]=mesh_path
102 convergence_synthesis["Mesh_description"]=mesh_name
103 convergence_synthesis["Mesh_sizes"]=[10**x for x in mesh_size_tab]
104 convergence_synthesis["Space_dimension"]=2
105 convergence_synthesis["Mesh_dimension"]=2
106 convergence_synthesis["Mesh_cell_type"]="Triangles"
107 convergence_synthesis["Errors"]=[10**x for x in error_tab]
108 convergence_synthesis["Scheme_order"]=-a
109 convergence_synthesis["Test_color"]=testColor
110 convergence_synthesis["Computational_time"]=end-start
112 with open('Convergence_Diffusion_2DFE_'+mesh_name+'.json', 'w') as outfile:
113 json.dump(convergence_synthesis, outfile)
115 if __name__ == """__main__""":
116 test_validation2DEF_Delaunay_triangles()