Salome HOME
[Intersect2D] Keep flexibility on orientation of mesh2
[tools/medcoupling.git] / src / PyWrapping / TestPyWrapGathered_medcoupling.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2017-2021  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
24 import unittest
25
26 def WriteInTmpDir(func):
27     def decoratedFunc(*args,**kwargs):
28         import tempfile,os
29         ret = None
30         with tempfile.TemporaryDirectory() as tmpdirname:
31             os.chdir(tmpdirname)
32             ret = func(*args,**kwargs)
33             pass
34         return ret
35     return decoratedFunc
36
37 class FileCreator(object):
38     def __init__(self,tester,fname):
39         self._tester=tester
40         self._fname=fname
41         pass
42         
43     def fileName(self):
44         return self._fname
45     
46     def __enter__(self):
47         import os
48         if os.path.exists(self._fname):
49             os.remove(self._fname)
50             pass
51         return self
52     
53     def __exit__(self, type, value, traceback):
54         import os
55         if not os.path.exists(self._fname):
56             self._tester.assertTrue(False)
57             pass
58         else:
59             os.remove(self._fname)
60         pass
61         
62 class medcouplingTest(unittest.TestCase):
63
64     def test0(self):
65         """ Unconditional test : medcoupling "kernel" classes """
66         f=MEDCouplingFieldDouble(ON_CELLS)
67         g=DataArrayDouble(10,2)
68         h=MEDCouplingUMesh("mesh",3)
69         hh=MEDCouplingRemapper()
70         ee=InterpKernelException("ee")
71         pass
72     
73     @unittest.skipUnless(HasMEDFileExt(),"Requires link to MED file")
74     @WriteInTmpDir
75     def test1(self):
76         import sys
77         fname="mctest1.med"
78         arr=DataArrayDouble(10) ; arr.iota()
79         m=MEDCouplingCMesh()
80         m.setCoords(arr,arr)
81         m.setName("mesh")
82         with FileCreator(self,fname) as fc:
83             m.write(fc.fileName())
84         m=m.buildUnstructured()
85         with FileCreator(self,fname) as fc:
86             m.write(fc.fileName())
87         f=MEDCouplingFieldDouble(ON_NODES) ; f.setMesh(m) ; f.setArray(m.getCoords()) ; f.setName("field")
88         with FileCreator(self,fname) as fc:
89             f.write(fc.fileName())
90         f=MEDCouplingFieldFloat(ON_NODES) ; f.setMesh(m)
91         d=DataArrayFloat(m.getNumberOfNodes()) ; d.iota()
92         f.setArray(d) ; f.setName("field1")
93         with FileCreator(self,fname) as fc:
94             f.write(fc.fileName())
95         pass
96
97     @unittest.skipUnless(HasRenumberExt(),"Requires Boost or Metis to activate Renumberer")
98     def test2(self):
99         arr=DataArrayDouble(10) ; arr.iota()
100         m=MEDCouplingCMesh() ; m.setCoords(arr,arr)
101         m=m.buildUnstructured() ; m.setName("mesh")
102         #
103         renf=RenumberingFactory("Boost")
104         neigh,neighi=m.computeNeighborsOfCells()
105         n2o,o2n=renf.renumber(neigh,neighi)
106         mRenum=m[n2o]
107         pass
108
109     @unittest.skipUnless(HasPartitionerExt(),"Requires Partitioner activation")
110     def test3(self):
111         for alg in MEDPartitioner.AvailableAlgorithms():
112             st="Graph.%s"%alg.upper()
113             print(st)
114             self.partitionerTesterHelper(eval(st))
115             pass
116         pass
117     
118     @unittest.skipUnless(HasParallelInterpolatorExt(),"Requires // interpolator activated")
119     def test4(self):
120         interface=CommInterface()
121         pass
122
123     @unittest.skipUnless(HasMEDFileExt(),"Requires link to MED file")
124     def test5(self):
125         f=MEDCouplingFieldDouble(ON_NODES)
126         f.setTime(1.25,3,6)
127         a,b,c=f.getTime()
128         self.assertEqual(b,3) ; self.assertEqual(c,6) ; self.assertAlmostEqual(a,1.25,14);
129         f1ts=MEDFileField1TS()
130         f1ts.setTime(10,13,10.75)
131         f.copyTimeInfoFrom(f1ts)
132         a,b,c=f.getTime()
133         self.assertEqual(b,10) ; self.assertEqual(c,13) ; self.assertAlmostEqual(a,10.75,14);
134         f2=MEDCouplingFieldInt(ON_NODES)
135         f2.copyTimeInfoFrom(f1ts)
136         a,b,c=f2.getTime()
137         self.assertEqual(b,10) ; self.assertEqual(c,13) ; self.assertAlmostEqual(a,10.75,14);
138         f3=MEDCouplingFieldFloat(ON_NODES)
139         f3.copyTimeInfoFrom(f1ts)
140         a,b,c=f3.getTime()
141         self.assertEqual(b,10) ; self.assertEqual(c,13) ; self.assertAlmostEqual(a,10.75,14);
142         pass
143         
144
145     def partitionerTesterHelper(self,algoSelected):
146         arr=DataArrayDouble(10) ; arr.iota()
147         m=MEDCouplingCMesh() ; m.setCoords(arr,arr)
148         m=m.buildUnstructured() ; m.setName("mesh")
149         a,b=m.computeNeighborsOfCells()
150         sk=MEDCouplingSkyLineArray(b,a)
151         g=MEDPartitioner.Graph(sk,algoSelected)
152         g.partGraph(4)
153         procIdOnCells=g.getPartition().getValuesArray()
154         m0=m[procIdOnCells.findIdsEqual(0)] ; m0.setName("m0")
155         pass
156     
157     pass
158
159 if __name__ == "__main__":
160     if HasParallelInterpolatorExt():
161         try:
162             from mpi4py import MPI # if not imported test3 may failed due to MPI call of partitioner algorithms.
163         except:
164             pass
165         pass
166     unittest.main()
167     pass
168