1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2017-2023 CEA, EDF
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):
28 import tempfile,os, sys
30 with tempfile.TemporaryDirectory() as tmpdirname:
32 ret = func(*args,**kwargs)
33 os.chdir(os.path.dirname(tmpdirname))
38 class FileCreator(object):
39 def __init__(self,tester,fname):
49 if os.path.exists(self._fname):
50 os.remove(self._fname)
54 def __exit__(self, type, value, traceback):
56 if not os.path.exists(self._fname):
57 self._tester.assertTrue(False)
60 os.remove(self._fname)
63 class medcouplingTest(unittest.TestCase):
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")
74 @unittest.skipUnless(HasMEDFileExt(),"Requires link to MED file")
79 arr=DataArrayDouble(10) ; arr.iota()
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())
98 @unittest.skipUnless(HasRenumberExt(),"Requires Boost or Metis to activate Renumberer")
100 arr=DataArrayDouble(10) ; arr.iota()
101 m=MEDCouplingCMesh() ; m.setCoords(arr,arr)
102 m=m.buildUnstructured() ; m.setName("mesh")
104 renf=RenumberingFactory("Boost")
105 neigh,neighi=m.computeNeighborsOfCells()
106 n2o,o2n=renf.renumber(neigh,neighi)
110 @unittest.skipUnless(HasPartitionerExt(),"Requires Partitioner activation")
112 for alg in MEDPartitioner.AvailableAlgorithms():
113 st="Graph.%s"%alg.upper()
115 self.partitionerTesterHelper(eval(st))
119 @unittest.skipUnless(HasParallelInterpolatorExt(),"Requires // interpolator activated")
121 interface=CommInterface()
124 @unittest.skipUnless(HasMEDFileExt(),"Requires link to MED file")
126 f=MEDCouplingFieldDouble(ON_NODES)
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)
134 self.assertEqual(b,10) ; self.assertEqual(c,13) ; self.assertAlmostEqual(a,10.75,14);
135 f2=MEDCouplingFieldInt(ON_NODES)
136 f2.copyTimeInfoFrom(f1ts)
138 self.assertEqual(b,10) ; self.assertEqual(c,13) ; self.assertAlmostEqual(a,10.75,14);
139 f3=MEDCouplingFieldFloat(ON_NODES)
140 f3.copyTimeInfoFrom(f1ts)
142 self.assertEqual(b,10) ; self.assertEqual(c,13) ; self.assertAlmostEqual(a,10.75,14);
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)
154 procIdOnCells=g.getPartition().getValuesArray()
155 m0=m[procIdOnCells.findIdsEqual(0)] ; m0.setName("m0")
160 if __name__ == "__main__":
161 if HasParallelInterpolatorExt():
163 from mpi4py import MPI # if not imported test3 may failed due to MPI call of partitioner algorithms.