Salome HOME
Corrected indentation, expected values and missing parenthesis
[tools/solverlab.git] / CoreFlows / examples / Python / Convergence / StationaryDiffusion / 3DFE_Dirichlet_DelaunayTetrahedra / convergence_StationaryDiffusion_3DFE_Dirichlet_DelaunayTetrahedra.py
1 #!/usr/bin/env python
2 # -*-coding:utf-8 -*
3
4 import validationStationaryDiffusionEquation
5 import cdmath as cm
6 import matplotlib.pyplot as plt
7 import numpy as np
8 from math import log10, sqrt
9 import time, json, os
10
11 convergence_synthesis=dict(validationStationaryDiffusionEquation.test_desc)
12
13 def convergence_StationaryDiffusion_3DFE_Dirichlet_DelaunayTetrahedra():
14     start = time.time() 
15     ### 3D FE Delaunay Tetrahedra meshes
16     method = 'FE'
17     BC = 'Dirichlet'
18     meshList=['meshCubeTetrahedra_0','meshCubeTetrahedra_1','meshCubeTetrahedra_2','meshCubeTetrahedra_3','meshCubeTetrahedra_4','meshCubeTetrahedra_5']
19     mesh_path=os.environ['CDMATH_INSTALL']+'/share/meshes/3DTetrahedra/'
20     mesh_name='cubeWithDelaunayTetrahedra'
21     meshType="Unstructured_Tetrahedra"
22     nbMeshes=len(meshList)
23     error_tab=[0]*nbMeshes
24     mesh_size_tab=[0]*nbMeshes
25     diag_data=[0]*nbMeshes
26     time_tab=[0]*nbMeshes
27     resolution=100
28     curv_abs=np.linspace(0,sqrt(2),resolution+1)
29     plt.close('all')
30     i=0
31     testColor="Orange (not order 2)"
32     # Storing of numerical errors, mesh sizes and diagonal values
33     for filename in meshList:
34         my_mesh=cm.Mesh(mesh_path+filename+".med")
35         error_tab[i], mesh_size_tab[i], diag_data[i], min_sol_num, max_sol_num, time_tab[i] =validationStationaryDiffusionEquation.SolveStationaryDiffusionEquation(my_mesh,resolution,meshType,method,BC)
36
37         assert min_sol_num>=0 
38         assert max_sol_num<1
39         plt.plot(curv_abs, diag_data[i], label= str(mesh_size_tab[i]) + ' cells')
40         error_tab[i]=log10(error_tab[i])
41         time_tab[i]=log10(time_tab[i])
42         mesh_size_tab[i] = 0.5*log10(mesh_size_tab[i])
43         i=i+1
44     end = time.time()
45         
46    
47     # Plot over diagonal line
48     plt.legend()
49     plt.xlabel('Position on diagonal line')
50     plt.ylabel('Value on diagonal line')
51     plt.title('Plot over diagonal line for finite volumes for Poisson problem \n on 3D Delaunay tetrahedra with Dirichlet BC')
52     plt.savefig(mesh_name+"_3DFE_StatDiffusion_Dirichlet_PlotOverDiagonalLine.png")
53
54     # Least square linear regression
55     # Find the best a,b such that f(x)=ax+b best approximates the convergence curve
56     # The vector X=(a,b) solves a symmetric linear system AX=B with A=(a1,a2\\a2,a3), B=(b1,b2)
57     a1=np.dot(mesh_size_tab,mesh_size_tab)
58     a2=np.sum(mesh_size_tab)
59     a3=nbMeshes
60     b1=np.dot(error_tab,mesh_size_tab)   
61     b2=np.sum(error_tab)
62     
63     det=a1*a3-a2*a2
64     assert det!=0, 'convergence_StationaryDiffusion_3DFE_Dirichlet_DelaunayTetrahedra() : Make sure you use distinct meshes and at least two meshes'
65     a=( a3*b1-a2*b2)/det
66     b=(-a2*b1+a1*b2)/det
67     
68     print( "FE for diffusion on 3D Delaunay tetrahedron meshes: scheme order is ", -a)
69     assert abs(a+0.627)<0.01
70     
71     # Plot of convergence curve
72     plt.close()
73     plt.plot(mesh_size_tab, error_tab, label='log(|numerical-exact|)')
74     plt.plot(mesh_size_tab, a*np.array(mesh_size_tab)+b,label='least square slope : '+'%.3f' % a)
75     plt.legend()
76     plt.plot(mesh_size_tab, error_tab)
77     plt.xlabel('log(sqrt(number of cells))')
78     plt.ylabel('log(error)')
79     plt.title('Convergence of finite volumes for Poisson problem \n on 3D Delaunay tetrahedra meshes with Dirichlet BC')
80     plt.savefig(mesh_name+"_3DFE_StatDiffusion_Dirichlet_ConvergenceCurve.png")
81
82     # Plot of computational time
83     plt.close()
84     plt.plot(mesh_size_tab, time_tab, label='log(cpu time)')
85     plt.legend()
86     plt.xlabel('log(sqrt(number of cells))')
87     plt.ylabel('log(cpu time)')
88     plt.title('Computational time of finite volumes for Poisson problem \n on 3D Delaunay tetrahedra meshes with Dirichlet BC')
89     plt.savefig(mesh_name+"_3DFE_StatDiffusion_Dirichlet_ComputationalTime.png")
90     
91     plt.close('all')
92
93     convergence_synthesis["Mesh_names"]=meshList
94     convergence_synthesis["Mesh_type"]=meshType
95     #convergence_synthesis["Mesh_path"]=mesh_path
96     convergence_synthesis["Mesh_description"]=mesh_name
97     convergence_synthesis["Mesh_sizes"]=[10**x for x in mesh_size_tab]
98     convergence_synthesis["Space_dim"]=3
99     convergence_synthesis["Mesh_dim"]=3
100     convergence_synthesis["Mesh_cell_type"]="Tetrahedron"
101     convergence_synthesis["Errors"]=[10**x for x in error_tab]
102     convergence_synthesis["Scheme_order"]=round(-a,4)
103     convergence_synthesis["Test_color"]=testColor
104     convergence_synthesis["PDE_model"]='Poisson'
105     convergence_synthesis["Bound_cond"]=BC
106     convergence_synthesis["Num_method"]=method
107     convergence_synthesis["Comput_time"]=round(end-start,3)
108
109     with open('Convergence_Poisson_3DFE_'+mesh_name+'.json', 'w') as outfile:  
110         json.dump(convergence_synthesis, outfile)
111
112    
113 if __name__ == """__main__""":
114     convergence_StationaryDiffusion_3DFE_Dirichlet_DelaunayTetrahedra()