1 import TransportEquation1DCenteredImplicit
2 import matplotlib.pyplot as plt
4 from math import log10, sqrt
9 def test_validation1DTransportEquationCenteredImplicit(cfl,isSmooth):
12 meshList=[50,100,200,400,800]
13 meshType="1D regular grid"
15 nbMeshes=len(meshList)
16 mesh_size_tab=meshList
17 mesh_name='RegularGrid'
20 error_u_tab=[0]*nbMeshes
22 total_var_u=[0]*nbMeshes
30 # Storing of numerical errors, mesh sizes and solution
32 min_u[i], max_u[i], sol_u[i], total_var_u[i], error_u_tab[i], time_tab[i] = TransportEquation1DCenteredImplicit.solve(nx,cfl,a,b,isSmooth)
33 assert max_u[i]>-1e-5 and max_u[i]<1+1e-5
34 error_u_tab[i]=log10(error_u_tab[i])
35 time_tab[i]=log10(time_tab[i])
42 for i in range(nbMeshes):
43 plt.plot(np.linspace(a,b,meshList[i]), sol_u[i], label= str(mesh_size_tab[i]) + ' cells')
47 plt.title('Plot of the numerical solution of the transport equation \n with implicit centered scheme on a 1D regular grid')
49 plt.savefig(mesh_name+"_1DTransportEquationCenteredImplicit_CFL"+str(cfl)+"_Smooth_PlotOfSolution.png")
51 plt.savefig(mesh_name+"_1DTransportEquationCenteredImplicit_CFL"+str(cfl)+"_Stiff_PlotOfSolution.png")
54 # Plot of maximal value
56 plt.plot(mesh_size_tab, max_u, label='Maximum value')
58 plt.xlabel('Number of cells')
60 plt.title('Maximum velocity norm of the transport equation \n with implicit centered scheme on a 1D regular grid')
62 plt.savefig(mesh_name+"_1DTransportEquationCenteredImplicit_CFL"+str(cfl)+"_Smooth_MaxSolution.png")
64 plt.savefig(mesh_name+"_1DTransportEquationCenteredImplicit_CFL"+str(cfl)+"_Stiff_MaxSolution.png")
66 # Plot of total variation
68 plt.plot(mesh_size_tab, total_var_u, label='Total variation')
70 plt.xlabel('Number of cells')
72 plt.title('Total variation for the transport equation \n with implicit centered scheme on a 1D regular grid')
74 plt.savefig(mesh_name+"_1DTransportEquationCenteredImplicit_CFL"+str(cfl)+"_Smooth_TotalVariation.png")
76 plt.savefig(mesh_name+"_1DTransportEquationCenteredImplicit_CFL"+str(cfl)+"_Stiff_TotalVariation.png")
78 for i in range(nbMeshes):
79 mesh_size_tab[i]=log10(mesh_size_tab[i])
81 # Least square linear regression
82 # Find the best a,b such that f(x)=ax+b best approximates the convergence curve
83 # The vector X=(a,b) solves a symmetric linear system AX=B with A=(a1,a2\\a2,a3), B=(b1,b2)
84 a1=np.dot(mesh_size_tab,mesh_size_tab)
85 a2=np.sum(mesh_size_tab)
89 assert det!=0, 'test_validation1DTransportEquationCenteredImplicit() : Make sure you use distinct meshes and at least two meshes'
91 b1u=np.dot(error_u_tab,mesh_size_tab)
92 b2u=np.sum(error_u_tab)
93 au=( a3*b1u-a2*b2u)/det
94 bu=(-a2*b1u+a1*b2u)/det
96 print("Implicit Centered scheme for Transport Equation on 1D regular grid : scheme order is ", -au)
98 # Plot of convergence curve
100 plt.plot(mesh_size_tab, error_u_tab, label='log(|error|)')
101 plt.plot(mesh_size_tab, a*np.array(mesh_size_tab)+b,label='least square slope : '+'%.3f' % a)
103 plt.xlabel('log(Number of cells)')
104 plt.ylabel('log(|error u|)')
105 plt.title('Convergence of finite volumes for the transport equation \n with implicit centered scheme on a 1D regular grid')
107 plt.savefig(mesh_name+"1DTransportEquationCenteredImplicit_CFL"+str(cfl)+"_Smooth_ConvergenceCurve.png")
109 plt.savefig(mesh_name+"1DTransportEquationCenteredImplicit_CFL"+str(cfl)+"_Stiff_ConvergenceCurve.png")
111 # Plot of computational time
113 plt.plot(mesh_size_tab, time_tab, label='log(cpu time)')
115 plt.xlabel('log(Number of cells)')
116 plt.ylabel('log(cpu time)')
117 plt.title('Computational time of finite volumes for the transport equation \n with implicit centered scheme on a 1D regular grid')
119 plt.savefig(mesh_name+"1DTransportEquationCenteredImplicit_CFL"+str(cfl)+"_Smooth_ComputationalTime.png")
121 plt.savefig(mesh_name+"1DTransportEquationCenteredImplicit_CFL"+str(cfl)+"_Stiff_ComputationalTime.png")
125 convergence_synthesis={}
127 convergence_synthesis["PDE_model"]="Transport_Equation"
128 convergence_synthesis["PDE_is_stationary"]=False
129 convergence_synthesis["PDE_search_for_stationary_solution"]=True
130 convergence_synthesis["Numerical_method_name"]="Centered scheme"
131 convergence_synthesis["Numerical_method_space_discretization"]="Finite volumes"
132 convergence_synthesis["Numerical_method_time_discretization"]="Implicit"
133 convergence_synthesis["Initial_data"]="sine"
134 convergence_synthesis["Boundary_conditions"]="Periodic"
135 convergence_synthesis["Numerical_parameter_cfl"]=cfl
136 convergence_synthesis["Space_dimension"]=2
137 convergence_synthesis["Mesh_dimension"]=2
138 convergence_synthesis["Mesh_names"]=meshList
139 convergence_synthesis["Mesh_type"]=meshType
140 convergence_synthesis["Mesh_description"]=mesh_name
141 convergence_synthesis["Mesh_sizes"]=mesh_size_tab
142 convergence_synthesis["Mesh_cell_type"]="1D regular grid"
143 convergence_synthesis["Numerical_ersolution"]=max_u
144 convergence_synthesis["Scheme_order"]=-au
145 convergence_synthesis["Test_color"]=testColor
146 convergence_synthesis["Computational_time"]=end-start
148 with open('Convergence_1DTransportEquationCenteredImplicit_'+mesh_name+'.json', 'w') as outfile:
149 json.dump(convergence_synthesis, outfile)
151 if __name__ == """__main__""":
152 if len(sys.argv) >2 :
153 cfl = float(sys.argv[1])
154 isSmooth = bool(int(sys.argv[2]))
155 test_validation1DTransportEquationCenteredImplicit(cfl,isSmooth)
157 test_validation1DTransportEquationCenteredImplicit(0.99,True)