4 #===============================================================================================================================
5 # Name : Tests of using a subcommnicator for sending and receiving a 3D MEDCoupling field on cells (P0) lying on the same mesh between two processors
6 # Author : Michaël Ndjinga
7 # Copyright : CEA Saclay 2021
8 # Description : Use of the parallel Data Exchange Channel StructuredCoincidentDEC of MEDCoupling
9 #================================================================================================================================
11 from mpi4py import MPI
12 import medcoupling as mc
15 size = comm.Get_size()
16 rank = comm.Get_rank()
19 raise ValueError("Processor ", rank, " : aborting.\n Simulation should done on three processors.\n", size, " processors given")
21 print("My rank is ", rank, " among ", size, "processors")
27 interface = mc.CommInterface()
28 source_group = mc.MPIProcessorGroup(interface, procs_source)
29 target_group = mc.MPIProcessorGroup(interface, procs_target)
30 dec = mc.StructuredCoincidentDEC(source_group, target_group)
32 # Create a MEDCouplingUMesh from a 3D cartesian mesh
33 xarr=mc.DataArrayDouble.New(11,1)
35 cmesh=mc.MEDCouplingCMesh.New()
36 cmesh.setCoords(xarr,xarr,xarr)
37 mesh=cmesh.buildUnstructured()
38 mesh.setName("RegularSquare")
40 #Create a field by application of an analytic function
41 if source_group.containsMyRank():
42 field=mesh.fillFromAnalytic(mc.ON_CELLS,1,"(x-5.)*(x-5.)+(y-5.)*(y-5.)+(z-5.)*(z-5.)")
43 field.setName("SourceField")
44 mc.WriteField("source_field.med", field, True)
45 print("Processor ", rank, " has created and saved the source field")
47 field=mesh.fillFromAnalytic(mc.ON_CELLS,1,"0")
48 field.setName("TargetField")
49 print("Processor ", rank, " has created the target field")
51 dec.attachLocalField(field)
54 if source_group.containsMyRank():
56 print("Processor ", rank, " has sent the source field")
57 elif target_group.containsMyRank():
59 print("Processor ", rank, " has received the source field on the target mesh")
60 exact_field=mesh.fillFromAnalytic(mc.ON_CELLS,1,"(x-5.)*(x-5.)+(y-5.)*(y-5.)+(z-5.)*(z-5.)")
61 exact_field.setName("ExactField")
62 error=(field-exact_field).normL2()[0]
63 print("Processor ", rank, " received source field differs from theoretical value by ", error, " (L2 norm on cells)" )
64 assert abs(error)<1.e-6
65 mc.WriteField("target_field.med", field, True)
66 mc.WriteField("exact_field.med", exact_field, True)
68 print("Processor ", rank, " did nothing" )