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_checkerboard():
11 ##### 2D FV checkerboard mesh
12 meshList=['checkerboard_4x4','checkerboard_8x8','checkerboard_16x16','checkerboard_32x32','checkerboard_64x64','checkerboard_128x128']
13 meshType="Non_conforming_checkerboard"
15 nbMeshes=len(meshList)
16 error_tab=[0]*nbMeshes
17 mesh_size_tab=[0]*nbMeshes
18 mesh_path='../../../ressources/2DCheckerboard/'
19 mesh_name='squareWithCheckerboard'
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)
29 assert min_sol_num>-0.01
30 assert max_sol_num<1.02
31 plt.plot(curv_abs, diag_data[i], label= str(mesh_size_tab[i]) + ' cells')
32 error_tab[i]=log10(error_tab[i])
33 time_tab[i]=log10(time_tab[i])
34 mesh_size_tab[i] = 0.5*log10(mesh_size_tab[i])
39 # Plot over diagonal line
41 plt.xlabel('Position on diagonal line')
42 plt.ylabel('Value on diagonal line')
43 plt.title('Plot over diagonal line for finite volumes for \n the diffusion equation on 2D checkerboard meshes')
44 plt.savefig(mesh_name+"_2DDiffusionVF_PlotOverDiagonalLine.png", dpi='figure')
46 # Least square linear regression
47 # Find the best a,b such that f(x)=ax+b best approximates the convergence curve
48 # The vector X=(a,b) solves a symmetric linear system AX=B with A=(a1,a2\\a2,a3), B=(b1,b2)
49 a1=np.dot(mesh_size_tab,mesh_size_tab)
50 a2=np.sum(mesh_size_tab)
52 b1=np.dot(error_tab,mesh_size_tab)
56 assert det!=0, 'test_validation2DVF_checkerboard() : Make sure you use distinct meshes and at least two meshes'
60 print "FV for diffusion on 2D checkerboard meshes : scheme order is ", -a
61 assert abs(a+0.5475)<0.001 #The scheme is not converging
63 # Plot of convergence curve
65 plt.plot(mesh_size_tab, error_tab, label='log(|numerical-exact|)')
66 plt.plot(mesh_size_tab, a*np.array(mesh_size_tab)+b,label='least square slope : '+'%.3f' % a)
68 plt.xlabel('log(sqrt(number of cells))')
69 plt.ylabel('log(error)')
70 plt.title('Convergence of finite volumes for \n the diffusion equation on 2D checkerboard meshes')
71 plt.savefig(mesh_name+"_2DDiffusionVF_ConvergenceCurve.png", dpi='figure')
73 # Plot of computational time
75 plt.plot(mesh_size_tab, time_tab, label='log(cpu time)')
77 plt.xlabel('log(sqrt(number of cells))')
78 plt.ylabel('log(cpu time)')
79 plt.title('Computational time of finite volumes for \n the diffusion equation on 2D checkerboard meshes')
80 plt.savefig(mesh_name+"_2DDiffusionVF_ComputationalTime.png", dpi='figure')
84 convergence_synthesis["Mesh_names"]=meshList
85 convergence_synthesis["Mesh_type"]=meshType
86 convergence_synthesis["Mesh_path"]=mesh_path
87 convergence_synthesis["Mesh_description"]=mesh_name
88 convergence_synthesis["Mesh_sizes"]=[10**x for x in mesh_size_tab]
89 convergence_synthesis["Space_dimension"]=2
90 convergence_synthesis["Mesh_dimension"]=2
91 convergence_synthesis["Mesh_cell_type"]="Squares"
92 convergence_synthesis["Errors"]=[10**x for x in error_tab]
93 convergence_synthesis["Scheme_order"]=-a
94 convergence_synthesis["Test_color"]=testColor
95 convergence_synthesis["Computational_time"]=end-start
97 with open('Convergence_Diffusion_2DVF_'+mesh_name+'.json', 'w') as outfile:
98 json.dump(convergence_synthesis, outfile)
100 if __name__ == """__main__""":
101 test_validation2DVF_checkerboard()