1 import FiniteVolumes2DDiffusion_SQUARE
2 import matplotlib.pyplot as plt
4 from math import log10,sqrt
7 convergence_synthesis=dict(FiniteVolumes2DDiffusion_SQUARE.test_desc)
9 def test_validation2DVF_hexagons():
11 ##### 2D FV hexagonal mesh
12 meshList=['squareWithHexagons_1','squareWithHexagons_2','squareWithHexagons_3','squareWithHexagons_4','squareWithHexagons_5']
13 meshType="Regular_hexagons"
15 nbMeshes=len(meshList)
16 error_tab=[0]*nbMeshes
17 mesh_size_tab=[0]*nbMeshes
18 mesh_path='../../../ressources/2DHexagons/'
19 mesh_name='squareWithHexagons'
20 diag_data=[0]*nbMeshes
23 curv_abs=np.linspace(0,sqrt(2),resolution+1)
26 # Storing of numerical errors, mesh sizes and diagonal values
27 for filename in meshList:
28 error_tab[i], mesh_size_tab[i], diag_data[i], min_sol_num, max_sol_num, time_tab[i] =FiniteVolumes2DDiffusion_SQUARE.solve_file(mesh_path+filename,resolution,meshType,testColor)
30 assert min_sol_num>-0.03
31 assert max_sol_num<1.2
32 plt.plot(curv_abs, diag_data[i], label= str(mesh_size_tab[i]) + ' cells')
33 error_tab[i]=log10(error_tab[i])
34 time_tab[i]=log10(time_tab[i])
35 mesh_size_tab[i] = 0.5*log10(mesh_size_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 volumes \n for the diffusion equation on 2D hexagonal meshes')
45 plt.savefig(mesh_name+"_2DDiffusionVF_PlotOverDiagonalLine.png", dpi='figure')
47 # Least square linear regression
48 # Find the best a,b such that f(x)=ax+b best approximates the convergence curve
49 # The vector X=(a,b) solves a symmetric linear system AX=B with A=(a1,a2\\a2,a3), B=(b1,b2)
50 a1=np.dot(mesh_size_tab,mesh_size_tab)
51 a2=np.sum(mesh_size_tab)
53 b1=np.dot(error_tab,mesh_size_tab)
57 assert det!=0, 'test_validation2DVF_hexagons() : Make sure you use distinct meshes and at least two meshes'
61 print "FV for diffusion on 2D hexagonal meshes : scheme order is ", -a
62 assert abs(a+1.95)<0.01
64 # Plot of convergence curve
66 plt.plot(mesh_size_tab, error_tab, label='log(|numerical-exact|)')
67 plt.plot(mesh_size_tab, a*np.array(mesh_size_tab)+b,label='least square slope : '+'%.3f' % a)
69 plt.xlabel('log(sqrt(number of cells))')
70 plt.ylabel('log(error)')
71 plt.title('Convergence of finite volumes \n for the diffusion equation on a 2D hexagonal meshes')
72 plt.savefig(mesh_name+"_2DDiffusionVF_ConvergenceCurve.png", dpi='figure')
74 # Plot of computational time
76 plt.plot(mesh_size_tab, time_tab, label='log(cpu time)')
78 plt.xlabel('log(sqrt(number of cells))')
79 plt.ylabel('log(cpu time)')
80 plt.title('Computational time of finite volumes \n for the diffusion equation on 2D hexagonal meshes')
81 plt.savefig(mesh_name+"_2DpoissonVF_ComputationalTime.png", dpi='figure')
85 convergence_synthesis["Mesh_names"]=meshList
86 convergence_synthesis["Mesh_type"]=meshType
87 convergence_synthesis["Mesh_path"]=mesh_path
88 convergence_synthesis["Mesh_description"]=mesh_name
89 convergence_synthesis["Mesh_sizes"]=[10**x for x in mesh_size_tab]
90 convergence_synthesis["Space_dimension"]=2
91 convergence_synthesis["Mesh_dimension"]=2
92 convergence_synthesis["Mesh_cell_type"]="Hexagons"
93 convergence_synthesis["Errors"]=[10**x for x in error_tab]
94 convergence_synthesis["Scheme_order"]=-a
95 convergence_synthesis["Test_color"]=testColor
96 convergence_synthesis["Computational_time"]=end-start
98 with open('Convergence_Diffusion_2DVF_'+mesh_name+'.json', 'w') as outfile:
99 json.dump(convergence_synthesis, outfile)
101 if __name__ == """__main__""":
102 test_validation2DVF_hexagons()