1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2017-2019 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 *
25 class FileCreator(object):
26 def __init__(self,tester,fname):
36 if os.path.exists(self._fname):
37 os.remove(self._fname)
41 def __exit__(self, type, value, traceback):
43 if not os.path.exists(self._fname):
44 self._tester.assertTrue(False)
47 os.remove(self._fname)
50 class medcouplingTest(unittest.TestCase):
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")
61 @unittest.skipUnless(HasMEDFileExt(),"Requires link to MED file")
65 arr=DataArrayDouble(10) ; arr.iota()
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())
84 @unittest.skipUnless(HasRenumberExt(),"Requires Boost or Metis to activate Renumberer")
86 arr=DataArrayDouble(10) ; arr.iota()
87 m=MEDCouplingCMesh() ; m.setCoords(arr,arr)
88 m=m.buildUnstructured() ; m.setName("mesh")
90 renf=RenumberingFactory("Boost")
91 neigh,neighi=m.computeNeighborsOfCells()
92 n2o,o2n=renf.renumber(neigh,neighi)
96 @unittest.skipUnless(HasPartitionerExt(),"Requires Partitioner activation")
98 for alg in MEDPartitioner.AvailableAlgorithms():
99 st="Graph.%s"%alg.upper()
101 self.partitionerTesterHelper(eval(st))
105 @unittest.skipUnless(HasParallelInterpolatorExt(),"Requires // interpolator activated")
107 interface=CommInterface()
110 @unittest.skipUnless(HasMEDFileExt(),"Requires link to MED file")
112 f=MEDCouplingFieldDouble(ON_NODES)
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)
120 self.assertEqual(b,10) ; self.assertEqual(c,13) ; self.assertAlmostEqual(a,10.75,14);
121 f2=MEDCouplingFieldInt(ON_NODES)
122 f2.copyTimeInfoFrom(f1ts)
124 self.assertEqual(b,10) ; self.assertEqual(c,13) ; self.assertAlmostEqual(a,10.75,14);
125 f3=MEDCouplingFieldFloat(ON_NODES)
126 f3.copyTimeInfoFrom(f1ts)
128 self.assertEqual(b,10) ; self.assertEqual(c,13) ; self.assertAlmostEqual(a,10.75,14);
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)
140 procIdOnCells=g.getPartition().getValuesArray()
141 m0=m[procIdOnCells.findIdsEqual(0)] ; m0.setName("m0")
146 if __name__ == "__main__":
147 if HasParallelInterpolatorExt():
149 from mpi4py import MPI # if not imported test3 may failed due to MPI call of partitioner algorithms.