4 import validationStationaryDiffusionEquation
6 import matplotlib.pyplot as plt
8 from math import log10, sqrt
11 convergence_synthesis=dict(validationStationaryDiffusionEquation.test_desc)
13 def convergence_StationaryDiffusion_2DFE_Neumann_RightTriangles():
15 ### 2D FE right triangles mesh
18 meshList=[5,20,50,100, 200,400]
19 nbMeshes=len(meshList)
20 error_tab=[0]*nbMeshes
21 mesh_size_tab=[0]*nbMeshes
22 mesh_name='squareWithTriangles'
23 meshType="Regular_RightTriangles"
24 diag_data=[0]*nbMeshes
27 curv_abs=np.linspace(0,sqrt(2),resolution+1)
30 testColor="Orange (not order 2), singular matrix"
31 # Storing of numerical errors, mesh sizes and diagonal values
33 my_mesh=cm.Mesh(0,1,nx,0,1,nx,1)
34 error_tab[i], mesh_size_tab[i], diag_data[i], min_sol_num, max_sol_num, time_tab[i] =validationStationaryDiffusionEquation.SolveStationaryDiffusionEquation(my_mesh,resolution,meshType,method,BC)
36 assert min_sol_num>-1.22
38 plt.plot(curv_abs, diag_data[i], label= str(mesh_size_tab[i]) + ' cells')
39 error_tab[i]=log10(error_tab[i])
40 time_tab[i]=log10(time_tab[i])
41 mesh_size_tab[i] = 0.5*log10(mesh_size_tab[i])
47 # Plot over diagonal line
49 plt.xlabel('Position on diagonal line')
50 plt.ylabel('Value on diagonal line')
51 plt.title('Plot over diagonal line for finite elements for the Poisson problem \n on 2D right triangles meshes with with Neumann BC')
52 plt.savefig(mesh_name+"_2DFE_StatDiffusion_Neumann_PlotOverDiagonalLine.png")
54 # Least square linear regression
55 # Find the best a,b such that f(x)=ax+b best approximates the convergence curve
56 # The vector X=(a,b) solves a symmetric linear system AX=B with A=(a1,a2\\a2,a3), B=(b1,b2)
57 a1=np.dot(mesh_size_tab,mesh_size_tab)
58 a2=np.sum(mesh_size_tab)
60 b1=np.dot(error_tab,mesh_size_tab)
64 assert det!=0, 'convergence_StationaryDiffusion_2DFE_Neumann_RightTriangles() : Make sure you use distinct meshes and at least two meshes'
68 print "FE for diffusion on 2D right triangle meshes: scheme order is ", -a
69 assert abs(a+0.91)<0.01
71 # Plot of convergence curve
73 plt.plot(mesh_size_tab, error_tab, label='log(|numerical-exact|)')
74 plt.plot(mesh_size_tab, a*np.array(mesh_size_tab)+b,label='least square slope : '+'%.3f' % a)
76 plt.plot(mesh_size_tab, error_tab)
77 plt.xlabel('log(sqrt(number of cells))')
78 plt.ylabel('log(error)')
79 plt.title('Convergence of finite elements for the Poisson problem \n on 2D right triangles meshes with with Neumann BC')
80 plt.savefig(mesh_name+"_2DFE_StatDiffusion_Neumann_ConvergenceCurve.png")
82 # Plot of computational time
84 plt.plot(mesh_size_tab, time_tab, label='log(cpu time)')
86 plt.xlabel('log(sqrt(number of cells))')
87 plt.ylabel('log(cpu time)')
88 plt.title('Computational time of finite elements for the Poisson problem \n on 2D right triangles meshes with with Neumann BC')
89 plt.savefig(mesh_name+"_2DFE_StatDiffusion_Neumann_ComputationalTime.png")
93 convergence_synthesis["Mesh_names"]=meshList
94 convergence_synthesis["Mesh_type"]=meshType
95 #convergence_synthesis["Mesh_path"]=mesh_path
96 convergence_synthesis["Mesh_description"]=mesh_name
97 convergence_synthesis["Mesh_sizes"]=[10**x for x in mesh_size_tab]
98 convergence_synthesis["Space_dim"]=2
99 convergence_synthesis["Mesh_dim"]=2
100 convergence_synthesis["Mesh_cell_type"]="Triangles"
101 convergence_synthesis["Errors"]=[10**x for x in error_tab]
102 convergence_synthesis["Scheme_order"]=round(-a,4)
103 convergence_synthesis["Test_color"]=testColor
104 convergence_synthesis["PDE_model"]='Poisson'
105 convergence_synthesis["Bound_cond"]=BC
106 convergence_synthesis["Num_method"]=method
107 convergence_synthesis["Comput_time"]=round(end-start,3)
109 with open('Convergence_Poisson_2DFE_'+mesh_name+'.json', 'w') as outfile:
110 json.dump(convergence_synthesis, outfile)
113 if __name__ == """__main__""":
114 convergence_StationaryDiffusion_2DFE_Neumann_RightTriangles()