]> SALOME platform Git repositories - tools/medcoupling.git/blob - src/PyWrapping/TestPyWrapGathered_medcoupling.py
Salome HOME
New useful DataArrayInt32::findIdForEach
[tools/medcoupling.git] / src / PyWrapping / TestPyWrapGathered_medcoupling.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2017  CEA/DEN, EDF R&D
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20 # Author : Anthony Geay (EDF R&D)
21
22 from medcoupling import *
23 import unittest
24
25 class FileCreator(object):
26     def __init__(self,tester,fname):
27         self._tester=tester
28         self._fname=fname
29         pass
30         
31     def fileName(self):
32         return self._fname
33     
34     def __enter__(self):
35         import os
36         if os.path.exists(self._fname):
37             os.remove(self._fname)
38             pass
39         return self
40     
41     def __exit__(self, type, value, traceback):
42         import os
43         if not os.path.exists(self._fname):
44             self._tester.assertTrue(False)
45             pass
46         else:
47             os.remove(self._fname)
48         pass
49         
50 class medcouplingTest(unittest.TestCase):
51     
52     @unittest.skipUnless(HasMEDFileExt(),"Requires link to MED file")
53     def test1(self):
54         import sys
55         fname="mctest1.med"
56         arr=DataArrayDouble(10) ; arr.iota()
57         m=MEDCouplingCMesh()
58         m.setCoords(arr,arr)
59         m.setName("mesh")
60         with FileCreator(self,fname) as fc:
61             m.write(fc.fileName())
62         m=m.buildUnstructured()
63         with FileCreator(self,fname) as fc:
64             m.write(fc.fileName())
65         f=MEDCouplingFieldDouble(ON_NODES) ; f.setMesh(m) ; f.setArray(m.getCoords()) ; f.setName("field")
66         with FileCreator(self,fname) as fc:
67             f.write(fc.fileName())
68         f=MEDCouplingFieldFloat(ON_NODES) ; f.setMesh(m)
69         d=DataArrayFloat(m.getNumberOfNodes()) ; d.iota()
70         f.setArray(d) ; f.setName("field1")
71         with FileCreator(self,fname) as fc:
72             f.write(fc.fileName())
73         pass
74
75     @unittest.skipUnless(HasRenumberExt(),"Requires Boost or Metis to activate Renumberer")
76     def test2(self):
77         arr=DataArrayDouble(10) ; arr.iota()
78         m=MEDCouplingCMesh() ; m.setCoords(arr,arr)
79         m=m.buildUnstructured() ; m.setName("mesh")
80         #
81         renf=RenumberingFactory("Boost")
82         neigh,neighi=m.computeNeighborsOfCells()
83         n2o,o2n=renf.renumber(neigh,neighi)
84         mRenum=m[n2o]
85         pass
86
87     @unittest.skipUnless(HasPartitionerExt(),"Requires Partitioner activation")
88     def test3(self):
89         for alg in MEDPartitioner.AvailableAlgorithms():
90             st="Graph.%s"%alg.upper()
91             print(st)
92             self.partitionerTesterHelper(eval(st))
93             pass
94         pass
95     
96     @unittest.skipUnless(HasParallelInterpolatorExt(),"Requires // interpolator activated")
97     def test4(self):
98         interface=CommInterface()
99         pass
100
101     @unittest.skipUnless(HasMEDFileExt(),"Requires link to MED file")
102     def test5(self):
103         f=MEDCouplingFieldDouble(ON_NODES)
104         f.setTime(1.25,3,6)
105         a,b,c=f.getTime()
106         self.assertEqual(b,3) ; self.assertEqual(c,6) ; self.assertAlmostEqual(a,1.25,14);
107         f1ts=MEDFileField1TS()
108         f1ts.setTime(10,13,10.75)
109         f.copyTimeInfoFrom(f1ts)
110         a,b,c=f.getTime()
111         self.assertEqual(b,10) ; self.assertEqual(c,13) ; self.assertAlmostEqual(a,10.75,14);
112         f2=MEDCouplingFieldInt(ON_NODES)
113         f2.copyTimeInfoFrom(f1ts)
114         a,b,c=f2.getTime()
115         self.assertEqual(b,10) ; self.assertEqual(c,13) ; self.assertAlmostEqual(a,10.75,14);
116         f3=MEDCouplingFieldFloat(ON_NODES)
117         f3.copyTimeInfoFrom(f1ts)
118         a,b,c=f3.getTime()
119         self.assertEqual(b,10) ; self.assertEqual(c,13) ; self.assertAlmostEqual(a,10.75,14);
120         pass
121         
122
123     def partitionerTesterHelper(self,algoSelected):
124         arr=DataArrayDouble(10) ; arr.iota()
125         m=MEDCouplingCMesh() ; m.setCoords(arr,arr)
126         m=m.buildUnstructured() ; m.setName("mesh")
127         a,b=m.computeNeighborsOfCells()
128         sk=MEDCouplingSkyLineArray(b,a)
129         g=MEDPartitioner.Graph(sk,algoSelected)
130         g.partGraph(4)
131         procIdOnCells=g.getPartition().getValuesArray()
132         m0=m[procIdOnCells.findIdsEqual(0)] ; m0.setName("m0")
133         pass
134     
135     pass
136
137 if __name__ == "__main__":
138     if HasParallelInterpolatorExt():
139         try:
140             from mpi4py import MPI # if not imported test3 may failed due to MPI call of partitioner algorithms.
141         except:
142             pass
143         pass
144     unittest.main()
145     pass
146