1 import TransportEquation1DUpwindExplicit
2 import matplotlib.pyplot as plt
4 from math import log10, sqrt
9 def test_validation1DTransportEquationUpwindExplicit(cfl,isSmooth):
12 meshList=[10,20,50,100,200, 400]
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] = TransportEquation1DUpwindExplicit.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')
46 plt.title('Plot of the numerical solution of the transport equation \n with explicit upwind scheme on a 1D regular grid')
47 plt.ylim( - 0.1, 1.1 )
50 plt.savefig(mesh_name+"_1DTransportEquation_UpwindExplicit_CFL"+str(cfl)+"_Smooth_PlotOfSolution.png")
52 plt.savefig(mesh_name+"_1DTransportEquation_UpwindExplicit_CFL"+str(cfl)+"_Stiff_PlotOfSolution.png")
55 # Plot of maximal value
57 plt.plot(mesh_size_tab, max_u, label='Maximum value')
59 plt.xlabel('Number of cells')
61 plt.title('Maximum solution norm of the transport equation \n with explicit upwind scheme on a 1D regular grid')
63 plt.savefig(mesh_name+"_1DTransportEquationUpwindExplicit_CFL"+str(cfl)+"_Smooth_MaxSolution.png")
65 plt.savefig(mesh_name+"_1DTransportEquationUpwindExplicit_CFL"+str(cfl)+"_Stiff_MaxSolution.png")
67 # Plot of total variation
69 plt.plot(mesh_size_tab, total_var_u, label='Total variation')
71 plt.xlabel('Number of cells')
73 plt.title('Total variation for the transport equation \n with explicit upwind scheme on a 1D regular grid')
75 plt.savefig(mesh_name+"_1DTransportEquationUpwindExplicit_CFL"+str(cfl)+"_Smooth_TotalVariation.png")
77 plt.savefig(mesh_name+"_1DTransportEquationUpwindExplicit_CFL"+str(cfl)+"_Stiff_TotalVariation.png")
79 for i in range(nbMeshes):
80 mesh_size_tab[i]=log10(mesh_size_tab[i])
82 # Least square linear regression
83 # Find the best a,b such that f(x)=ax+b best approximates the convergence curve
84 # The vector X=(a,b) solves a symmetric linear system AX=B with A=(a1,a2\\a2,a3), B=(b1,b2)
85 a1=np.dot(mesh_size_tab,mesh_size_tab)
86 a2=np.sum(mesh_size_tab)
90 assert det!=0, 'test_validation1DTransportEquationUpwindExplicit() : Make sure you use distinct meshes and at least two meshes'
92 b1u=np.dot(error_u_tab,mesh_size_tab)
93 b2u=np.sum(error_u_tab)
94 a=( a3*b1u-a2*b2u)/det
95 b=(-a2*b1u+a1*b2u)/det
97 print("Explicit Upwind scheme for Transport Equation on 1D regular grid : scheme order is ", -a)
99 assert -a>0.48 and -a<1.02
101 # Plot of convergence curve
103 plt.plot(mesh_size_tab, error_u_tab, label='log(|error|)')
104 plt.plot(mesh_size_tab, a*np.array(mesh_size_tab)+b,label='straight line with slope : '+'%.3f' % a)
106 plt.xlabel('log(Number of cells)')
107 plt.ylabel('log(|error u|)')
108 plt.title('Convergence of finite volumes for the transport equation \n with explicit upwind scheme on a 1D regular grid')
110 plt.savefig(mesh_name+"1DTransportEquationUpwindExplicit_CFL"+str(cfl)+"_Smooth_ConvergenceCurve.png")
112 plt.savefig(mesh_name+"1DTransportEquationUpwindExplicit_CFL"+str(cfl)+"_Stiff_ConvergenceCurve.png")
114 # Plot of computational time
116 plt.plot(mesh_size_tab, time_tab, label='log(cpu time)')
118 plt.xlabel('log(Number of cells)')
119 plt.ylabel('log(cpu time)')
120 plt.title('Computational time of finite volumes for the transport equation \n with explicit upwind scheme on a 1D regular grid')
122 plt.savefig(mesh_name+"1DTransportEquationUpwindExplicit_CFL"+str(cfl)+"_Smooth_ComputationalTime.png")
124 plt.savefig(mesh_name+"1DTransportEquationUpwindExplicit_CFL"+str(cfl)+"_Stiff_ComputationalTime.png")
128 convergence_synthesis={}
130 convergence_synthesis["PDE_model"]="Transport_Equation"
131 convergence_synthesis["PDE_is_stationary"]=False
132 convergence_synthesis["PDE_search_for_stationary_solution"]=True
133 convergence_synthesis["Numerical_method_name"]="Upwind scheme"
134 convergence_synthesis["Numerical_method_space_discretization"]="Finite volumes"
135 convergence_synthesis["Numerical_method_time_discretization"]="Explicit"
136 convergence_synthesis["Initial_data"]="sine"
137 convergence_synthesis["Boundary_conditions"]="Periodic"
138 convergence_synthesis["Numerical_parameter_cfl"]=cfl
139 convergence_synthesis["Space_dimension"]=2
140 convergence_synthesis["Mesh_dimension"]=2
141 convergence_synthesis["Mesh_names"]=meshList
142 convergence_synthesis["Mesh_type"]=meshType
143 convergence_synthesis["Mesh_description"]=mesh_name
144 convergence_synthesis["Mesh_sizes"]=mesh_size_tab
145 convergence_synthesis["Mesh_cell_type"]="1D regular grid"
146 convergence_synthesis["Numerical_ersolution"]=max_u
147 convergence_synthesis["Scheme_order"]=-a
148 convergence_synthesis["Test_color"]=testColor
149 convergence_synthesis["Computational_time"]=end-start
151 with open('Convergence_1DTransportEquationUpwindExplicit_'+mesh_name+'.json', 'w') as outfile:
152 json.dump(convergence_synthesis, outfile)
154 if __name__ == """__main__""":
155 if len(sys.argv) >2 :
156 cfl = float(sys.argv[1])
157 isSmooth = bool(int(sys.argv[2]))
158 test_validation1DTransportEquationUpwindExplicit(cfl,isSmooth)
160 test_validation1DTransportEquationUpwindExplicit(0.99,True)