Salome HOME
93657cabf1d3cdd763a2bf8f00e6226669c07e7e
[tools/medcoupling.git] / src / MEDPartitioner_Swig / MEDPartitionerTest.py
1 # Copyright (C) 2012-2016  CEA/DEN, EDF R&D
2 #
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 # Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19
20 from MEDPartitioner import *
21 from MEDLoader import *
22 import unittest
23 from MEDLoaderDataForTest import MEDLoaderDataForTest
24
25 class MEDPartitionerTest(unittest.TestCase):
26     def testPartition(self):
27         fname="PyPartitionTest.med"
28         data=MEDLoaderDataForTest.buildACompleteMEDDataStructureWithFieldsOnCells_1()
29         data.write(fname,2)
30         part_file=MEDPartitioner(fname,2)
31         part_data=MEDPartitioner(data,2)
32         part_file.write("splitted_PyPartitionTest_1")
33         part_data.write("splitted_PyPartitionTest_2")
34         part_file_xml=MEDPartitioner("splitted_PyPartitionTest_1.xml")
35         part_data_xml=MEDPartitioner("splitted_PyPartitionTest_2.xml")
36         data1=part_file_xml.getMEDFileData()
37         data2=part_data_xml.getMEDFileData()
38         m1d=data1.getMeshes().getMeshAtPos(0)
39         m2d=data2.getMeshes().getMeshAtPos(0)
40         self.assertTrue(m1d.isEqual(m2d,1e-12))
41     pass
42     def testPartitionGraph(self):
43         data=MEDLoaderDataForTest.buildACompleteMEDDataStructureWithFieldsOnCells_1()
44         m=data.getMeshes().getMeshAtPos(0)
45         graph=MEDPartitioner.Graph(m.getLevel0Mesh().generateGraph())
46         graph.partGraph(2)
47         tool=MEDPartitioner(data,graph)
48         data2=tool.getMEDFileData()
49         self.assertEqual( 2, data2.getMeshes().getNumberOfMeshes() )
50     pass
51     def testPartitionWithJoints(self):
52         # cartesian mesh 4x4
53         arr=DataArrayDouble(5) ; arr.iota()
54         c=MEDCouplingCMesh() ; c.setCoords(arr,arr)
55         m=c.buildUnstructured()
56         m.setName("mesh")
57         mm=MEDFileUMesh()
58         mm.setMeshAtLevel(0,m)
59         ms=MEDFileMeshes() ; ms.pushMesh(mm)
60         data=MEDFileData()
61         data.setMeshes(ms)
62         part_file=MEDPartitioner(data,4,"metis",True,True,True)
63         data_file=part_file.getMEDFileData()
64         meshes=data_file.getMeshes()
65         self.assertEqual( 4, meshes.getNumberOfMeshes())
66         self.assertEqual( 3, meshes.getMeshAtPos(0).getJoints().getNumberOfJoints())
67         self.assertEqual( 3, meshes.getMeshAtPos(1).getJoints().getNumberOfJoints())
68         self.assertEqual( 3, meshes.getMeshAtPos(2).getJoints().getNumberOfJoints())
69         self.assertEqual( 3, meshes.getMeshAtPos(3).getJoints().getNumberOfJoints())
70         joints=meshes.getMeshAtPos(0).getJoints()
71         self.assertEqual( 1, joints.getJointAtPos(0).getDomainNumber(), 1)
72         #VSR (10/05/2016): changed to work with metis 5.1... to be confirmed!
73         #self.assertEqual( 2, joints.getJointAtPos(1).getDomainNumber(), 2)
74         #self.assertEqual( 3, joints.getJointAtPos(2).getDomainNumber(), 3)
75         self.assertEqual( 3, joints.getJointAtPos(1).getDomainNumber(), 3)
76         self.assertEqual( 2, joints.getJointAtPos(2).getDomainNumber(), 2)
77         self.assertEqual( 2, joints.getJointAtPos(0).getStepAtPos(0).getNumberOfCorrespondences())
78         self.assertEqual( 2, joints.getJointAtPos(1).getStepAtPos(0).getNumberOfCorrespondences())
79         self.assertEqual( 1, joints.getJointAtPos(2).getStepAtPos(0).getNumberOfCorrespondences())
80         found=0
81         for ii in range(joints.getJointAtPos(0).getStepAtPos(0).getNumberOfCorrespondences()):
82             correspond=joints.getJointAtPos(0).getStepAtPos(0).getCorrespondenceAtPos(ii)
83             #VSR (10/05/2016): changed to work with metis 5.1... to be confirmed!
84             #if correspond.getCorrespondence().isEqual(DataArrayInt([1,3,2,4])):
85             if correspond.getCorrespondence().isEqual(DataArrayInt([3,1,4,2])):
86                 found+=1
87                 self.assertEqual(NORM_QUAD4, correspond.getLocalGeometryType())
88                 self.assertEqual(NORM_QUAD4, correspond.getRemoteGeometryType())
89             pass
90         pass
91         self.assertEqual(1,found)
92     pass
93     def testPartitionPartGraph(self):
94         arr=DataArrayDouble(5) ; arr.iota()
95         c=MEDCouplingCMesh() ; c.setCoords(arr,arr)
96         m=c.buildUnstructured()
97         part=MEDPartitioner.Graph(m.generateGraph())
98         part.partGraph(2)
99         a=part.getGraph()
100         p=part.getPartition()
101         self.assertTrue(isinstance(a,MEDCouplingSkyLineArray))
102         self.assertTrue(isinstance(p,MEDCouplingSkyLineArray))
103         self.assertTrue(part.nbVertices() > 0 )
104     pass
105
106 if __name__ == "__main__":
107   unittest.main()
108