4 #===============================================================================================================================
5 # Name : Tests of launching two independent simulations in parallel
6 # Author : Michaël Ndjinga
7 # Copyright : CEA Saclay 2021
9 #================================================================================================================================
11 from mpi4py import MPI
14 from math import sin, pi
16 def StationaryDiffusionEquation_2DEF_StructuredTriangles_par(split_direction, rank):
18 # Prepare for the mesh
19 print("Processor ", rank, " : Building mesh " );
26 M=solverlab.Mesh(xinf,xsup,nx,yinf,ysup,ny,split_direction)#Regular triangular 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("Processor ", rank, " : Built a regular triangular 2D mesh from a square mesh with ", nx,"x" ,ny, " cells.")
35 print("Processor ", rank, " : Each square was split in two in direction ",split_direction)
37 myProblem = solverlab.StationaryDiffusionEquation(spaceDim,FEComputation);
40 # set the limit value for each boundary
46 myProblem.setDirichletBoundaryCondition("Bord1",T1)
47 myProblem.setDirichletBoundaryCondition("Bord2",T2)
48 myProblem.setDirichletBoundaryCondition("Bord3",T3)
49 myProblem.setDirichletBoundaryCondition("Bord4",T4)
51 #Set the right hand side function
52 my_RHSfield = solverlab.Field("RHS_field", solverlab.NODES, M, 1)
53 for i in range(M.getNumberOfNodes()):
58 my_RHSfield[i]=2*pi*pi*sin(pi*x)*sin(pi*y)#mettre la fonction definie au second membre de l'edp
60 myProblem.setHeatPowerField(my_RHSfield)
61 myProblem.setLinearSolver(solverlab.GMRES,solverlab.ILU);
64 fileName = "StationnaryDiffusion_2DEF_StructuredTriangles"+str(rank);
66 # computation parameters
67 myProblem.setFileName(fileName);
70 myProblem.initialize();
71 print("Processor ", rank, " : Running python "+ fileName );
73 ok = myProblem.solveStationaryProblem();
75 print( "Python simulation of " + fileName + " failed ! " );
78 ####################### Postprocessing #########################
79 my_ResultField = myProblem.getOutputTemperatureField()
80 #The following formulas use the fact that the exact solution is equal the right hand side divided by 2*pi*pi
81 max_abs_sol_exacte=max(my_RHSfield.max(),-my_RHSfield.min())/(2*pi*pi)
82 max_sol_num=my_ResultField.max()
83 min_sol_num=my_ResultField.min()
85 for i in range(M.getNumberOfNodes()) :
86 if erreur_abs < abs(my_RHSfield[i]/(2*pi*pi) - my_ResultField[i]) :
87 erreur_abs = abs(my_RHSfield[i]/(2*pi*pi) - my_ResultField[i])
89 print("Processor ", rank, " : Absolute error = max(| exact solution - numerical solution |) = ",erreur_abs )
90 print("Processor ", rank, " : Relative error = max(| exact solution - numerical solution |)/max(| exact solution |) = ",erreur_abs/max_abs_sol_exacte)
91 print("Processor ", rank, " : Maximum numerical solution = ", max_sol_num, " Minimum numerical solution = ", min_sol_num)
93 assert erreur_abs/max_abs_sol_exacte <1.
96 print("Processor ", rank, " : ------------ !!! End of calculation !!! -----------" );
98 myProblem.terminate();
99 return erreur_abs/max_abs_sol_exacte
101 if __name__ == """__main__""":
102 comm = MPI.COMM_WORLD
103 size = comm.Get_size()
104 rank = comm.Get_rank()
107 raise ValueError("Processor ", rank, " : aborting.\n Simulation should done on two processors.\n", size, " processors given")
109 print("My rank is ", rank, " among ", size, "processors ")
111 my_relative_error=StationaryDiffusionEquation_2DEF_StructuredTriangles_par(rank, rank)
114 comm.send(my_relative_error, dest=1, tag=11)
115 other_relative_error = comm.recv(source=1, tag=17)
117 other_relative_error = comm.recv(source=0, tag=11)
118 comm.send(my_relative_error, dest=0, tag=17)
120 print("Processor ", rank, " : Difference between the two processor relative errors is ", abs(my_relative_error-other_relative_error) )
121 #print("Processor ", rank, " : Difference between the two processors is ", (my_ResultField-other_ResultField).normMax()[0] )