]> SALOME platform Git repositories - tools/solverlab.git/blob
Salome HOME
4028b07e37d84f66a4ed7a35bc4ceba39e3094e6
[tools/solverlab.git] /
1 import cdmath
2 import WaveSystemUpwind
3 import matplotlib
4 matplotlib.use("Agg")
5 import matplotlib.pyplot as plt
6 import numpy as np
7 from math import log10, sqrt
8 import sys
9 import time, json
10
11     
12 def test_validation2DWaveSystemUpwindSquares(bctype,scaling):
13     start = time.time()
14     #### 2D square mesh
15     meshList=[7,15,31,51,81]#,101
16     #meshList=['squareWithSquares_1','squareWithSquares_2','squareWithSquares_3','squareWithSquares_4','squareWithSquares_5']
17     mesh_path='../../../ressources/2DCartesien/'
18     meshType="Regular squares"
19     testColor="Green"
20     nbMeshes=len(meshList)
21     error_p_tab=[0]*nbMeshes
22     error_u_tab=[0]*nbMeshes
23     mesh_size_tab=[0]*nbMeshes
24     mesh_name='squareWithSquares'
25     diag_data_press=[0]*nbMeshes
26     diag_data_vel=[0]*nbMeshes
27     time_tab=[0]*nbMeshes
28     t_final=[0]*nbMeshes
29     ndt_final=[0]*nbMeshes
30     max_vel=[0]*nbMeshes
31     resolution=100
32     curv_abs=np.linspace(0,sqrt(2),resolution+1)
33     
34     plt.close('all')
35     i=0
36     cfl=0.5
37     # Storing of numerical errors, mesh sizes and diagonal values
38     #for filename in meshList:
39     for nx in meshList:
40         my_mesh=cdmath.Mesh(0,1,nx,0,1,nx)
41         error_p_tab[i], error_u_tab[i], mesh_size_tab[i], t_final[i], ndt_final[i], max_vel[i], diag_data_press[i], diag_data_vel[i], time_tab[i] =WaveSystemUpwind.solve(my_mesh,"square"+str(nx)+'x'+str(nx), resolution,scaling,meshType,testColor,cfl,bctype)
42         #error_p_tab[i], error_u_tab[i], mesh_size_tab[i], t_final[i], ndt_final[i], max_vel[i], diag_data_press[i], diag_data_vel[i], time_tab[i] =WaveSystemUpwind.solve_file(mesh_path+filename, mesh_name, resolution,scaling,meshType,testColor,cfl,bctype)
43         assert max_vel[i]>0.0001 and max_vel[i]<1.
44         print( "error_p_tab[i]=", error_p_tab[i], " zero leads to singularity in plotting of convergence curve")
45         #error_p_tab[i]=log10(error_p_tab[i])
46         error_u_tab[i]=log10(error_u_tab[i])
47         time_tab[i]=log10(time_tab[i])
48         i=i+1
49     
50     end = time.time()
51
52     # Plot over diagonal line
53     for i in range(nbMeshes):
54         plt.plot(curv_abs, diag_data_press[i], label= str(mesh_size_tab[i]) + ' cells')
55     plt.legend()
56     plt.xlabel('Position on diagonal line')
57     plt.ylabel('Pressure on diagonal line')
58     plt.title('Plot over diagonal line for stationary wave system \n with upwind scheme on 2D square meshes')
59     plt.savefig(mesh_name+'_Pressure_2DWaveSystemUpwind_'+"PlotOverDiagonalLine.png")
60     plt.close()
61
62     plt.clf()
63     for i in range(nbMeshes):
64         plt.plot(curv_abs, diag_data_vel[i],   label= str(mesh_size_tab[i]) + ' cells')
65     plt.legend()
66     plt.xlabel('Position on diagonal line')
67     plt.ylabel('Velocity on diagonal line')
68     plt.title('Plot over diagonal line for the stationary wave system \n with upwind scheme on 2D square meshes')
69     plt.savefig(mesh_name+"_Velocity_2DWaveSystemUpwind_"+"PlotOverDiagonalLine.png")    
70     plt.close()
71
72     # Plot of number of time steps
73     plt.close()
74     plt.plot(mesh_size_tab, ndt_final, label='Number of time step to reach stationary regime')
75     plt.legend()
76     plt.xlabel('number of cells')
77     plt.ylabel('Max time steps for stationary regime')
78     plt.title('Number of times steps required for the stationary Wave System \n with upwind scheme on 2D square meshes')
79     plt.savefig(mesh_name+"_2DWaveSystemUpwind_"+"TimeSteps.png")
80     
81     # Plot of stationary time
82     plt.close()
83     plt.plot(mesh_size_tab, t_final, label='Time where stationary regime is reached')
84     plt.legend()
85     plt.xlabel('number of cells')
86     plt.ylabel('Max time for stationary regime')
87     plt.title('Simulated time  for the stationary Wave System \n with upwind scheme on 2D square meshes')
88     plt.savefig(mesh_name+"_2DWaveSystemUpwind_"+"TimeFinal.png")
89     
90     # Plot of number of maximal velocity norm
91     plt.close()
92     plt.plot(mesh_size_tab, max_vel, label='Maximum velocity norm')
93     plt.legend()
94     plt.xlabel('number of cells')
95     plt.ylabel('Max velocity norm')
96     plt.title('Maximum velocity norm  for the stationary Wave System \n with upwind scheme on 2D square meshes')
97     plt.savefig(mesh_name+"_2DWaveSystemUpwind_"+"MaxVelNorm.png")
98     
99     for i in range(nbMeshes):
100         mesh_size_tab[i]=0.5*log10(mesh_size_tab[i])
101         
102     # Least square linear regression
103     # Find the best a,b such that f(x)=ax+b best approximates the convergence curve
104     # The vector X=(a,b) solves a symmetric linear system AX=B with A=(a1,a2\\a2,a3), B=(b1,b2)
105     a1=np.dot(mesh_size_tab,mesh_size_tab)
106     a2=np.sum(mesh_size_tab)
107     a3=nbMeshes
108     
109     det=a1*a3-a2*a2
110     assert det!=0, 'test_validation2DWaveSystemUpwind_squares() : Make sure you use distinct meshes and at least two meshes'
111
112     #zero pressure leads to singularity
113     # b1p=np.dot(error_p_tab,mesh_size_tab)   
114     # b2p=np.sum(error_p_tab)
115     # ap=( a3*b1p-a2*b2p)/det
116     # bp=(-a2*b1p+a1*b2p)/det
117     
118     # print("FV upwind on 2D square meshes : scheme order for pressure is ", -ap)
119
120     b1u=np.dot(error_u_tab,mesh_size_tab)   
121     b2u=np.sum(error_u_tab)
122     au=( a3*b1u-a2*b2u)/det
123     bu=(-a2*b1u+a1*b2u)/det
124     
125     print("FV upwind on 2D square meshes : scheme order for velocity is ", -au)
126     
127     # Plot of convergence curves
128     # plt.close()
129     # plt.plot(mesh_size_tab, error_p_tab, label='|error on stationary pressure|')
130     # plt.legend()
131     # plt.xlabel('1/2 log(number of cells)')
132     # plt.ylabel('|error p|')
133     # plt.title('Convergence of finite volumes for the stationary Wave System \n with upwind scheme on 2D square meshes')
134     # plt.savefig(mesh_name+"_Pressure_2DWaveSystemUpwind_"+"ConvergenceCurve.png")
135     
136     plt.close()
137     plt.plot(mesh_size_tab, error_u_tab, label='log(|error on stationary velocity|)')
138     plt.legend()
139     plt.xlabel('1/2 log(number of cells)')
140     plt.ylabel('log(|error u|)')
141     plt.title('Convergence of finite volumes for the stationary Wave System\n with upwind scheme  on 2D square meshes')
142     plt.savefig(mesh_name+"_Velocity_2DWaveSystemUpwind_"+"ConvergenceCurve.png")
143     
144     # Plot of computational time
145     plt.close()
146     plt.plot(mesh_size_tab, time_tab, label='log(cpu time)')
147     plt.legend()
148     plt.xlabel('1/2 log(number of cells)')
149     plt.ylabel('log(cpu time)')
150     plt.title('Computational time of finite volumes for the stationary Wave System \n with upwind scheme on 2D square meshes')
151     plt.savefig(mesh_name+"_2DWaveSystemUpwind_ComputationalTimeSquares.png")
152
153     plt.close('all')
154
155     convergence_synthesis={}
156
157     convergence_synthesis["PDE_model"]="Wave system"
158     convergence_synthesis["PDE_is_stationary"]=False
159     convergence_synthesis["PDE_search_for_stationary_solution"]=True
160     convergence_synthesis["Numerical_method_name"]="Upwind"
161     convergence_synthesis["Numerical_method_space_discretization"]="Finite volumes"
162     convergence_synthesis["Numerical_method_time_discretization"]="Implicit"
163     convergence_synthesis["Initial_data"]="Constant pressure, divergence free velocity"
164     convergence_synthesis["Boundary_conditions"]="Periodic"
165     convergence_synthesis["Numerical_parameter_cfl"]=cfl
166     convergence_synthesis["Space_dimension"]=2
167     convergence_synthesis["Mesh_dimension"]=2
168     convergence_synthesis["Mesh_names"]=meshList
169     convergence_synthesis["Mesh_type"]=meshType
170     convergence_synthesis["Mesh_path"]=mesh_path
171     convergence_synthesis["Mesh_description"]=mesh_name
172     convergence_synthesis["Mesh_sizes"]=mesh_size_tab
173     convergence_synthesis["Mesh_cell_type"]="Squares"
174     convergence_synthesis["Numerical_error_velocity"]=error_u_tab
175     convergence_synthesis["Numerical_error_pressure"]=error_p_tab
176     convergence_synthesis["Max_vel_norm"]=max_vel
177     convergence_synthesis["Final_time"]=t_final  
178     convergence_synthesis["Final_time_step"]=ndt_final  
179     convergence_synthesis["Scheme_order"]=-au#min(-au,-ap)
180     convergence_synthesis["Scheme_order_vel"]=-au
181     #convergence_synthesis["Scheme_order_press"]=-ap
182     convergence_synthesis["Scaling_preconditioner"]="None"
183     convergence_synthesis["Test_color"]=testColor
184     convergence_synthesis["Computational_time"]=end-start
185
186     with open('Convergence_WaveSystem_2DFV_Upwind_'+mesh_name+'.json', 'w') as outfile:  
187         json.dump(convergence_synthesis, outfile)
188
189 if __name__ == """__main__""":
190     if len(sys.argv) >2 :
191         bctype = sys.argv[1]
192         scaling = int(sys.argv[2])
193         test_validation2DWaveSystemUpwindSquares(bctype,scaling)
194     else :
195         raise ValueError("test_validation2DWaveSystemUpwindSquares.py expects a mesh file name and a scaling parameter")