]> SALOME platform Git repositories - tools/solverlab.git/blob
Salome HOME
2ed0bbd34c165bc99ce9079a10cdc5a029f0276f
[tools/solverlab.git] /
1 import FiniteVolumes2DDiffusion_SQUARE
2 import matplotlib.pyplot as plt
3 import numpy as np
4 from math import log10, sqrt
5 import time, json
6
7 convergence_synthesis=dict(FiniteVolumes2DDiffusion_SQUARE.test_desc)
8
9 def test_validation2DVF_squares():
10     start = time.time()
11     ### 2D FV rectangular mesh
12 #    meshList=[11,51,151,201]
13     meshList=['squareWithSquares_1','squareWithSquares_2','squareWithSquares_3','squareWithSquares_4','squareWithSquares_5']
14     mesh_path='../../../ressources/2DCartesien/'
15     meshType="Regular_squares"
16     testColor="Green"
17     nbMeshes=len(meshList)
18     error_tab=[0]*nbMeshes
19     mesh_size_tab=[0]*nbMeshes
20     mesh_name='squareWithSquares'
21     diag_data=[0]*nbMeshes
22     time_tab=[0]*nbMeshes
23     resolution=100
24     curv_abs=np.linspace(0,sqrt(2),resolution+1)
25     plt.close('all')
26     i=0
27     # Storing of numerical errors, mesh sizes and diagonal values
28     for filename in meshList:
29         #import cdmath
30 #    for nx in meshList:
31 #        my_mesh=cdmath.Mesh(0,1,nx,0,1,nx)
32 #        error_tab[i], mesh_size_tab[i], diag_data[i], min_sol_num, max_sol_num, time_tab[i] =FiniteVolumes2DDiffusion_SQUARE.solve(my_mesh,str(nx)+'x'+str(nx),resolution,meshType,testColor)
33         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)
34         assert min_sol_num>-0.01 
35         assert max_sol_num<1.2
36         plt.plot(curv_abs, diag_data[i], label= str(mesh_size_tab[i]) + ' cells')
37         error_tab[i]=log10(error_tab[i])
38         time_tab[i]=log10(time_tab[i])
39         mesh_size_tab[i] = 0.5*log10(mesh_size_tab[i])
40         i=i+1
41         
42     end = time.time()
43
44     # Plot over diagonal line
45     plt.legend()
46     plt.xlabel('Position on diagonal line')
47     plt.ylabel('Value on diagonal line')
48     plt.title('Plot over diagonal line for finite volumes \n for the diffusion equation on 2D rectangular meshes')
49     plt.savefig(mesh_name+"_2DDiffusionFV_PlotOverDiagonalLine.png", dpi='figure')
50
51     # Least square linear regression
52     # Find the best a,b such that f(x)=ax+b best approximates the convergence curve
53     # The vector X=(a,b) solves a symmetric linear system AX=B with A=(a1,a2\\a2,a3), B=(b1,b2)
54     a1=np.dot(mesh_size_tab,mesh_size_tab)
55     a2=np.sum(mesh_size_tab)
56     a3=nbMeshes
57     b1=np.dot(error_tab,mesh_size_tab)   
58     b2=np.sum(error_tab)
59     
60     det=a1*a3-a2*a2
61     assert det!=0, 'test_validation2DVF_squares() : Make sure you use distinct meshes and at least two meshes'
62     a=( a3*b1-a2*b2)/det
63     b=(-a2*b1+a1*b2)/det
64     
65     print( "FV for diffusion on 2D rectangular meshes : scheme order is ", -a)
66     assert abs(a+2)<0.1
67     
68     # Plot of convergence curve
69     plt.close()
70     plt.plot(mesh_size_tab, error_tab, label='log(|numerical-exact|)')
71     plt.plot(mesh_size_tab, a*np.array(mesh_size_tab)+b,label='least square slope : '+'%.3f' % a)
72     plt.legend()
73     plt.plot(mesh_size_tab, error_tab)
74     plt.xlabel('log(sqrt(number of cells))')
75     plt.ylabel('log(error)')
76     plt.title('Convergence of finite volumes for \n the diffusion equation on 2D rectangular meshes')
77     plt.savefig(mesh_name+"_2DDiffusionFV_ConvergenceCurve.png", dpi='figure')
78
79     # Plot of computational time
80     plt.close()
81     plt.plot(mesh_size_tab, time_tab, label='log(cpu time)')
82     plt.legend()
83     plt.xlabel('log(sqrt(number of cells))')
84     plt.ylabel('log(cpu time)')
85     plt.title('Computational time of finite volumes \n for the diffusion equation on 2D rectangular meshes')
86     plt.savefig(mesh_name+"_2DDiffusionFV_ComputationalTime.png", dpi='figure')
87     
88     plt.close('all')
89
90     convergence_synthesis["Mesh_names"]=meshList
91     convergence_synthesis["Mesh_type"]=meshType
92     convergence_synthesis["Mesh_path"]=mesh_path
93     convergence_synthesis["Mesh_description"]=mesh_name
94     convergence_synthesis["Mesh_sizes"]=[10**x for x in mesh_size_tab]
95     convergence_synthesis["Space_dimension"]=2
96     convergence_synthesis["Mesh_dimension"]=2
97     convergence_synthesis["Mesh_cell_type"]="Squares"
98     convergence_synthesis["Errors"]=[10**x for x in error_tab]
99     convergence_synthesis["Scheme_order"]=-a
100     convergence_synthesis["Test_color"]=testColor
101     convergence_synthesis["Computational_time"]=end-start
102
103     with open('Convergence_Diffusion_2DVF_'+mesh_name+'.json', 'w') as outfile:  
104         json.dump(convergence_synthesis, outfile)
105
106 if __name__ == """__main__""":
107     test_validation2DVF_squares()