Salome HOME
Update copyrights
[tools/medcoupling.git] / src / PyWrapping / TestPyWrapGathered_medcoupling.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2017-2019  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     def test0(self):
53         """ Unconditional test : medcoupling "kernel" classes """
54         f=MEDCouplingFieldDouble(ON_CELLS)
55         g=DataArrayDouble(10,2)
56         h=MEDCouplingUMesh("mesh",3)
57         hh=MEDCouplingRemapper()
58         ee=InterpKernelException("ee")
59         pass
60     
61     @unittest.skipUnless(HasMEDFileExt(),"Requires link to MED file")
62     def test1(self):
63         import sys
64         fname="mctest1.med"
65         arr=DataArrayDouble(10) ; arr.iota()
66         m=MEDCouplingCMesh()
67         m.setCoords(arr,arr)
68         m.setName("mesh")
69         with FileCreator(self,fname) as fc:
70             m.write(fc.fileName())
71         m=m.buildUnstructured()
72         with FileCreator(self,fname) as fc:
73             m.write(fc.fileName())
74         f=MEDCouplingFieldDouble(ON_NODES) ; f.setMesh(m) ; f.setArray(m.getCoords()) ; f.setName("field")
75         with FileCreator(self,fname) as fc:
76             f.write(fc.fileName())
77         f=MEDCouplingFieldFloat(ON_NODES) ; f.setMesh(m)
78         d=DataArrayFloat(m.getNumberOfNodes()) ; d.iota()
79         f.setArray(d) ; f.setName("field1")
80         with FileCreator(self,fname) as fc:
81             f.write(fc.fileName())
82         pass
83
84     @unittest.skipUnless(HasRenumberExt(),"Requires Boost or Metis to activate Renumberer")
85     def test2(self):
86         arr=DataArrayDouble(10) ; arr.iota()
87         m=MEDCouplingCMesh() ; m.setCoords(arr,arr)
88         m=m.buildUnstructured() ; m.setName("mesh")
89         #
90         renf=RenumberingFactory("Boost")
91         neigh,neighi=m.computeNeighborsOfCells()
92         n2o,o2n=renf.renumber(neigh,neighi)
93         mRenum=m[n2o]
94         pass
95
96     @unittest.skipUnless(HasPartitionerExt(),"Requires Partitioner activation")
97     def test3(self):
98         for alg in MEDPartitioner.AvailableAlgorithms():
99             st="Graph.%s"%alg.upper()
100             print(st)
101             self.partitionerTesterHelper(eval(st))
102             pass
103         pass
104     
105     @unittest.skipUnless(HasParallelInterpolatorExt(),"Requires // interpolator activated")
106     def test4(self):
107         interface=CommInterface()
108         pass
109
110     @unittest.skipUnless(HasMEDFileExt(),"Requires link to MED file")
111     def test5(self):
112         f=MEDCouplingFieldDouble(ON_NODES)
113         f.setTime(1.25,3,6)
114         a,b,c=f.getTime()
115         self.assertEqual(b,3) ; self.assertEqual(c,6) ; self.assertAlmostEqual(a,1.25,14);
116         f1ts=MEDFileField1TS()
117         f1ts.setTime(10,13,10.75)
118         f.copyTimeInfoFrom(f1ts)
119         a,b,c=f.getTime()
120         self.assertEqual(b,10) ; self.assertEqual(c,13) ; self.assertAlmostEqual(a,10.75,14);
121         f2=MEDCouplingFieldInt(ON_NODES)
122         f2.copyTimeInfoFrom(f1ts)
123         a,b,c=f2.getTime()
124         self.assertEqual(b,10) ; self.assertEqual(c,13) ; self.assertAlmostEqual(a,10.75,14);
125         f3=MEDCouplingFieldFloat(ON_NODES)
126         f3.copyTimeInfoFrom(f1ts)
127         a,b,c=f3.getTime()
128         self.assertEqual(b,10) ; self.assertEqual(c,13) ; self.assertAlmostEqual(a,10.75,14);
129         pass
130         
131
132     def partitionerTesterHelper(self,algoSelected):
133         arr=DataArrayDouble(10) ; arr.iota()
134         m=MEDCouplingCMesh() ; m.setCoords(arr,arr)
135         m=m.buildUnstructured() ; m.setName("mesh")
136         a,b=m.computeNeighborsOfCells()
137         sk=MEDCouplingSkyLineArray(b,a)
138         g=MEDPartitioner.Graph(sk,algoSelected)
139         g.partGraph(4)
140         procIdOnCells=g.getPartition().getValuesArray()
141         m0=m[procIdOnCells.findIdsEqual(0)] ; m0.setName("m0")
142         pass
143     
144     pass
145
146 if __name__ == "__main__":
147     if HasParallelInterpolatorExt():
148         try:
149             from mpi4py import MPI # if not imported test3 may failed due to MPI call of partitioner algorithms.
150         except:
151             pass
152         pass
153     unittest.main()
154     pass
155