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