2 import FiniteVolumes2DPoisson_SQUARE
3 import matplotlib.pyplot as plt
5 from math import log10, sqrt
8 convergence_synthesis=dict(FiniteVolumes2DPoisson_SQUARE.test_desc)
10 def test_validation2DVF_right_triangles():
12 ### 2D FV right triangles mesh
13 meshList=[10,20,50,100,150,200]
14 #meshList=['squareWithRightTriangles_0','squareWithRightTriangles_1','squareWithRightTriangles_2','squareWithRightTriangles_3','squareWithRightTriangles_4','squareWithTriangles_5']
15 mesh_path='../../../ressources/2DRightTriangles/'
16 meshType="Regular_right_triangles"
18 nbMeshes=len(meshList)
19 error_tab=[0]*nbMeshes
20 mesh_size_tab=[0]*nbMeshes
21 mesh_name='squareWithRightTriangles'
22 diag_data=[0]*nbMeshes
25 curv_abs=np.linspace(0,sqrt(2),resolution+1)
29 # Storing of numerical errors, mesh sizes and diagonal values
30 #for filename in meshList:
32 my_mesh=cdmath.Mesh(0,1,nx,0,1,nx,1)
33 my_mesh.setGroupAtPlan(1,0,eps,"Bord1")
34 my_mesh.setGroupAtPlan(0,0,eps,"Bord2")
35 my_mesh.setGroupAtPlan(1,1,eps,"Bord3")
36 my_mesh.setGroupAtPlan(0,1,eps,"Bord4")
37 error_tab[i], mesh_size_tab[i], diag_data[i], min_sol_num, max_sol_num, time_tab[i] =FiniteVolumes2DPoisson_SQUARE.solve(my_mesh,str(nx)+'x'+str(nx),resolution,meshType,testColor)
38 # error_tab[i], mesh_size_tab[i], diag_data[i], min_sol_num, max_sol_num, time_tab[i] =FiniteVolumes2DPoisson_SQUARE.solve_file(mesh_path+filename,resolution,meshType,testColor)
39 assert min_sol_num>-0.01
40 assert max_sol_num<1.4
41 plt.plot(curv_abs, diag_data[i], label= str(mesh_size_tab[i]) + ' cells')
42 error_tab[i]=log10(error_tab[i])
43 time_tab[i]=log10(time_tab[i])
44 mesh_size_tab[i] = 0.5*log10(mesh_size_tab[i])
49 # Plot over diagonal line
51 plt.xlabel('Position on diagonal line')
52 plt.ylabel('Value on diagonal line')
53 plt.title('Plot over diagonal line for finite volumes \n for Laplace operator on 2D right triangles meshes')
54 plt.savefig(mesh_name+"_2DPoissonFV_PlotOverDiagonalLine.png")
56 # Least square linear regression
57 # Find the best a,b such that f(x)=ax+b best approximates the convergence curve
58 # The vector X=(a,b) solves a symmetric linear system AX=B with A=(a1,a2\\a2,a3), B=(b1,b2)
59 a1=np.dot(mesh_size_tab,mesh_size_tab)
60 a2=np.sum(mesh_size_tab)
62 b1=np.dot(error_tab,mesh_size_tab)
66 assert det!=0, 'test_validation2DVF_right_triangles() : Make sure you use distinct meshes and at least two meshes'
70 print( "FV on 2D right triangles mesh : scheme order is ", -a)
71 assert abs(a+0.04)<0.01
73 # Plot of convergence curve
75 plt.plot(mesh_size_tab, error_tab, label='log(|numerical-exact|)')
76 plt.plot(mesh_size_tab, a*np.array(mesh_size_tab)+b,label='least square slope : '+'%.3f' % a)
78 plt.plot(mesh_size_tab, error_tab)
79 plt.xlabel('log(sqrt(number of cells))')
80 plt.ylabel('log(error)')
81 plt.title('Convergence of finite volumes for \n Laplace operator on 2D right triangles meshes')
82 plt.savefig(mesh_name+"_2DPoissonFV_ConvergenceCurve.png")
84 # Plot of computational time
86 plt.plot(mesh_size_tab, time_tab, label='log(cpu time)')
88 plt.xlabel('log(sqrt(number of cells))')
89 plt.ylabel('log(cpu time)')
90 plt.title('Computational time of finite volumes \n for Laplace operator on 2D right triangles meshes')
91 plt.savefig(mesh_name+"_2DPoissonFV_ComputationalTime.png")
95 convergence_synthesis["Mesh_names"]=meshList
96 convergence_synthesis["Mesh_type"]=meshType
97 convergence_synthesis["Mesh_path"]=mesh_path
98 convergence_synthesis["Mesh_description"]=mesh_name
99 convergence_synthesis["Mesh_sizes"]=[10**x for x in mesh_size_tab]
100 convergence_synthesis["Space_dimension"]=2
101 convergence_synthesis["Mesh_dimension"]=2
102 convergence_synthesis["Mesh_cell_type"]="Triangles"
103 convergence_synthesis["Errors"]=[10**x for x in error_tab]
104 convergence_synthesis["Scheme_order"]=-a
105 convergence_synthesis["Test_color"]=testColor
106 convergence_synthesis["Computational_time"]=end-start
108 with open('Convergence_Poisson_2DVF_'+mesh_name+'.json', 'w') as outfile:
109 json.dump(convergence_synthesis, outfile)
111 if __name__ == """__main__""":
112 test_validation2DVF_right_triangles()