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):
17 # Prepare for the mesh
18 print("Processor ", rank, " : Building mesh " );
25 M=solverlab.Mesh(xinf,xsup,nx,yinf,ysup,ny,split_direction)#Regular triangular mesh
26 # set the limit field for each boundary
28 M.setGroupAtPlan(xsup,0,eps,"Bord1")
29 M.setGroupAtPlan(xinf,0,eps,"Bord2")
30 M.setGroupAtPlan(ysup,1,eps,"Bord3")
31 M.setGroupAtPlan(yinf,1,eps,"Bord4")
33 print("Processor ", rank, " : Built a regular triangular 2D mesh from a square mesh with ", nx,"x" ,ny, " cells.")
34 print("Processor ", rank, " : Each square was split in two in direction ",split_direction)
37 Lambda=1.#Thermal conductivity
41 sub_comm = comm.Split(color)
43 myProblem = solverlab.StationaryDiffusionEquation(spaceDim,FEComputation, Lambda, sub_comm);
46 # set the limit value for each boundary
52 myProblem.setDirichletBoundaryCondition("Bord1",T1)
53 myProblem.setDirichletBoundaryCondition("Bord2",T2)
54 myProblem.setDirichletBoundaryCondition("Bord3",T3)
55 myProblem.setDirichletBoundaryCondition("Bord4",T4)
57 #Set the right hand side function
58 my_RHSfield = solverlab.Field("RHS_field", solverlab.NODES, M, 1)
59 for i in range(M.getNumberOfNodes()):
64 my_RHSfield[i]=2*pi*pi*sin(pi*x)*sin(pi*y)#mettre la fonction definie au second membre de l'edp
66 myProblem.setHeatPowerField(my_RHSfield)
69 fileName = "StationnaryDiffusion_2DEF_StructuredTriangles"+str(rank);
71 # computation parameters
72 myProblem.setFileName(fileName);
75 myProblem.initialize();
76 print("Processor ", rank, " : Running python "+ fileName );
78 ok = myProblem.solveStationaryProblem();
80 print( "Python simulation of " + fileName + " failed ! " );
83 ####################### Postprocessing #########################
84 my_ResultField = myProblem.getOutputTemperatureField()
85 #The following formulas use the fact that the exact solution is equal the right hand side divided by 2*pi*pi
86 max_abs_sol_exacte=max(my_RHSfield.max(),-my_RHSfield.min())/(2*pi*pi)
87 max_sol_num=my_ResultField.max()
88 min_sol_num=my_ResultField.min()
90 for i in range(M.getNumberOfNodes()) :
91 if erreur_abs < abs(my_RHSfield[i]/(2*pi*pi) - my_ResultField[i]) :
92 erreur_abs = abs(my_RHSfield[i]/(2*pi*pi) - my_ResultField[i])
94 print("Processor ", rank, " : Absolute error = max(| exact solution - numerical solution |) = ",erreur_abs )
95 print("Processor ", rank, " : Relative error = max(| exact solution - numerical solution |)/max(| exact solution |) = ",erreur_abs/max_abs_sol_exacte)
96 print("Processor ", rank, " : Maximum numerical solution = ", max_sol_num, " Minimum numerical solution = ", min_sol_num)
98 assert erreur_abs/max_abs_sol_exacte <1.
101 print("Processor ", rank, " : ------------ !!! End of calculation !!! -----------" );
103 myProblem.terminate();
104 return erreur_abs/max_abs_sol_exacte
106 if __name__ == """__main__""":
107 comm = MPI.COMM_WORLD
108 size = comm.Get_size()
109 rank = comm.Get_rank()
112 raise ValueError("Processor ", rank, " : aborting.\n Simulation should done on two processors.\n", size, " processors given")
114 print("My rank is ", rank, " among ", size, "processors ")
116 my_relative_error=StationaryDiffusionEquation_2DEF_StructuredTriangles_par(rank, rank)
119 comm.send(my_relative_error, dest=1, tag=11)
120 other_relative_error = comm.recv(source=1, tag=17)
122 other_relative_error = comm.recv(source=0, tag=11)
123 comm.send(my_relative_error, dest=0, tag=17)
125 print("Processor ", rank, " : Difference between the two processor relative errors is ", abs(my_relative_error-other_relative_error) )
126 #print("Processor ", rank, " : Difference between the two processors is ", (my_ResultField-other_ResultField).normMax()[0] )