3 #===============================================================================================================================
4 # Name : Résolution VF de l'équation de Poisson 2D -\triangle T = f sur un carré avec conditions aux limites de Neumann T=0
5 # Author : Michaël Ndjinga
6 # Copyright : CEA Saclay 2019
7 # Description : Utilisation de la méthode des volumes finis avec champs T et f discrétisés aux cellules d'un maillage triangulaire
8 # Création et sauvegarde du champ résultant ainsi que du champ second membre en utilisant CDMATH
9 # Comparaison de la solution numérique avec la solution exacte T=cos(pi*x)*cos(pi*y)
10 #================================================================================================================================
12 import CoreFlows as cf
14 from math import cos, pi
16 def StationaryDiffusionEquation_2DFV_StructuredSquares_Neumann():
18 # Prepare for the mesh
19 print("Building mesh " );
26 M=cm.Mesh(xinf,xsup,nx,yinf,ysup,ny)#Regular square mesh
27 # set the limit field for each boundary
29 M.setGroupAtPlan(xsup,0,eps,"Bord1")
30 M.setGroupAtPlan(xinf,0,eps,"Bord2")
31 M.setGroupAtPlan(ysup,1,eps,"Bord3")
32 M.setGroupAtPlan(yinf,1,eps,"Bord4")
34 print "Built a regular 2D square mesh with ", nx,"x" ,ny, " cells"
37 myProblem = cf.StationaryDiffusionEquation(spaceDim,FEComputation);
40 # set the limit value for each boundary
46 myProblem.setNeumannBoundaryCondition("Bord1")
47 myProblem.setNeumannBoundaryCondition("Bord2")
48 myProblem.setNeumannBoundaryCondition("Bord3")
49 myProblem.setNeumannBoundaryCondition("Bord4")
51 #Set the right hand side function
52 my_RHSfield = cm.Field("RHS_field", cm.CELLS, M, 1)
53 for i in range(M.getNumberOfCells()):
58 my_RHSfield[i]=2*pi*pi*cos(pi*x)*cos(pi*y)#mettre la fonction definie au second membre de l'edp
60 myProblem.setHeatPowerField(my_RHSfield)
61 myProblem.setLinearSolver(cf.GMRES,cf.ILU);
64 fileName = "StationnaryDiffusion_2DFV_StructuredSquares_Neumann";
66 # computation parameters
67 myProblem.setFileName(fileName);
70 myProblem.initialize();
71 print("Running python "+ fileName );
73 ok = myProblem.solveStationaryProblem();
75 print( "Python simulation of " + fileName + " failed ! " );
78 print( "Python simulation of " + fileName + " is successful !" );
79 ####################### Postprocessing #########################
80 my_ResultField = myProblem.getOutputTemperatureField()
81 #The following formulas use the fact that the exact solution is equal the right hand side divided by 2*pi*pi
82 max_abs_sol_exacte=max(my_RHSfield.max(),-my_RHSfield.min())/(2*pi*pi)
83 max_sol_num=my_ResultField.max()
84 min_sol_num=my_ResultField.min()
86 for i in range(M.getNumberOfCells()) :
87 if erreur_abs < abs(my_RHSfield[i]/(2*pi*pi) - my_ResultField[i]) :
88 erreur_abs = abs(my_RHSfield[i]/(2*pi*pi) - my_ResultField[i])
90 print("Absolute error = max(| exact solution - numerical solution |) = ",erreur_abs )
91 print("Relative error = max(| exact solution - numerical solution |)/max(| exact solution |) = ",erreur_abs/max_abs_sol_exacte)
92 print ("Maximum numerical solution = ", max_sol_num, " Minimum numerical solution = ", min_sol_num)
94 assert erreur_abs/max_abs_sol_exacte <1.
97 print( "------------ !!! End of calculation !!! -----------" );
99 myProblem.terminate();
102 if __name__ == """__main__""":
103 StationaryDiffusionEquation_2DFV_StructuredSquares_Neumann()