Salome HOME
Copyright update 2021
[tools/medcoupling.git] / src / ParaMEDMEM_Swig / test_BasicOperation.py
1 #!/usr/bin/env python
2 #  -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2020-2021  CEA/DEN, EDF R&D
4 #
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License, or (at your option) any later version.
9 #
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18 #
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #
21
22 __doc__ = """Here a mesh is initially loaded into two parts in the 2 procs along Y axis ( low part and upper part ).
23 Then a redistribution of cells is performed along X axis ( left part and right part )
24 """
25
26 import medcoupling as mc
27
28 from mpi4py import MPI
29
30 def GenerateGlobalMesh():
31     mesh = mc.MEDCouplingCMesh()
32     arr = mc.DataArrayDouble(11) ; arr.iota()
33     mesh.setCoords(arr,arr)
34     mesh = mesh.buildUnstructured()
35     return mesh
36
37 def MeshPart0():
38     mesh = GenerateGlobalMesh()
39     p0 = mesh[list(range(50))]
40     nodeIds = p0.computeFetchedNodeIds()
41     p0.zipCoords()
42     return mc.ParaUMesh(p0,mc.DataArrayInt(list(range(50))),nodeIds)
43     
44 def MeshPart1():
45     mesh = GenerateGlobalMesh()
46     cellIds = mc.DataArrayInt(list(range(50,100,1)))
47     p1 = mesh[cellIds]
48     nodeIds = p1.computeFetchedNodeIds()
49     p1.zipCoords()
50     return mc.ParaUMesh(p1,cellIds,nodeIds)
51
52 workPerProc = {0 : MeshPart0 , 1 : MeshPart1}
53 distribPerProc = { 0 : [0, 1, 2, 3, 4, 5, 11, 12, 13, 14, 15, 16, 22, 23, 24, 25, 26, 27, 33, 34, 35, 36, 37, 38, 44, 45, 46, 47, 48, 49, 55, 56, 57, 58, 59, 60, 66, 67, 68, 69, 70, 71, 77, 78, 79, 80, 81, 82, 88, 89, 90, 91, 92, 93, 99, 100, 101, 102, 103, 104, 110, 111, 112, 113, 114, 115],
54 1 : [6, 7, 8, 9, 10, 17, 18, 19, 20, 21, 28, 29, 30, 31, 32, 39, 40, 41, 42, 43, 50, 51, 52, 53, 54, 61, 62, 63, 64, 65, 72, 73, 74, 75, 76, 83, 84, 85, 86, 87, 94, 95, 96, 97, 98, 105, 106, 107, 108, 109, 116, 117, 118, 119, 120]}
55
56 cellsExpected = {0 : [0, 1, 2, 3, 4, 10, 11, 12, 13, 14, 20, 21, 22, 23, 24, 30, 31, 32, 33, 34, 40, 41, 42, 43, 44, 50, 51, 52, 53, 54, 60, 61, 62, 63, 64, 70, 71, 72, 73, 74, 80, 81, 82, 83, 84, 90, 91, 92, 93, 94],
57 1 : [6, 7, 8, 9, 16, 17, 18, 19, 26, 27, 28, 29, 36, 37, 38, 39, 46, 47, 48, 49, 56, 57, 58, 59, 66, 67, 68, 69, 76, 77, 78, 79, 86, 87, 88, 89, 96, 97, 98, 99]}
58
59 if MPI.COMM_WORLD.size != 2 :
60     raise RuntimeError("Expected to be lanched with 2 procs !")
61
62 def test():
63     pmesh = workPerProc[MPI.COMM_WORLD.rank]()
64     pnodeids = mc.DataArrayInt(distribPerProc[MPI.COMM_WORLD.rank])
65     cells = pmesh.getCellIdsLyingOnNodes(pnodeids,True)
66     assert(cells.isEqual( mc.DataArrayInt(cellsExpected[MPI.COMM_WORLD.rank]) ))
67     pmesh_red = pmesh.redistributeCells(cells)
68     expected_mesh = GenerateGlobalMesh()[cells] ; expected_mesh.zipCoords()
69     assert( expected_mesh.isEqual(pmesh_red.getMesh(),1e-12) )
70
71 if __name__ == "__main__":
72     test()