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