1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2017-2022 CEA/DEN, EDF R&D
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.
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.
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
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 # Author : Anthony Geay (EDF R&D)
22 from medcoupling import *
26 def WriteInTmpDir(func):
27 def decoratedFunc(*args,**kwargs):
30 with tempfile.TemporaryDirectory() as tmpdirname:
32 ret = func(*args,**kwargs)
37 class FileCreator(object):
38 def __init__(self,tester,fname):
48 if os.path.exists(self._fname):
49 os.remove(self._fname)
53 def __exit__(self, type, value, traceback):
55 if not os.path.exists(self._fname):
56 self._tester.assertTrue(False)
59 os.remove(self._fname)
62 class medcouplingTest(unittest.TestCase):
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")
73 @unittest.skipUnless(HasMEDFileExt(),"Requires link to MED file")
78 arr=DataArrayDouble(10) ; arr.iota()
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())
97 @unittest.skipUnless(HasRenumberExt(),"Requires Boost or Metis to activate Renumberer")
99 arr=DataArrayDouble(10) ; arr.iota()
100 m=MEDCouplingCMesh() ; m.setCoords(arr,arr)
101 m=m.buildUnstructured() ; m.setName("mesh")
103 renf=RenumberingFactory("Boost")
104 neigh,neighi=m.computeNeighborsOfCells()
105 n2o,o2n=renf.renumber(neigh,neighi)
109 @unittest.skipUnless(HasPartitionerExt(),"Requires Partitioner activation")
111 for alg in MEDPartitioner.AvailableAlgorithms():
112 st="Graph.%s"%alg.upper()
114 self.partitionerTesterHelper(eval(st))
118 @unittest.skipUnless(HasParallelInterpolatorExt(),"Requires // interpolator activated")
120 interface=CommInterface()
123 @unittest.skipUnless(HasMEDFileExt(),"Requires link to MED file")
125 f=MEDCouplingFieldDouble(ON_NODES)
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)
133 self.assertEqual(b,10) ; self.assertEqual(c,13) ; self.assertAlmostEqual(a,10.75,14);
134 f2=MEDCouplingFieldInt(ON_NODES)
135 f2.copyTimeInfoFrom(f1ts)
137 self.assertEqual(b,10) ; self.assertEqual(c,13) ; self.assertAlmostEqual(a,10.75,14);
138 f3=MEDCouplingFieldFloat(ON_NODES)
139 f3.copyTimeInfoFrom(f1ts)
141 self.assertEqual(b,10) ; self.assertEqual(c,13) ; self.assertAlmostEqual(a,10.75,14);
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)
153 procIdOnCells=g.getPartition().getValuesArray()
154 m0=m[procIdOnCells.findIdsEqual(0)] ; m0.setName("m0")
159 if __name__ == "__main__":
160 if HasParallelInterpolatorExt():
162 from mpi4py import MPI # if not imported test3 may failed due to MPI call of partitioner algorithms.