Salome HOME
Indices are stored as mcIdType type instead of int to support switch to 64bits indexing
[tools/medcoupling.git] / src / MEDCoupling_Swig / MEDCouplingRemapperTest.py
index dc0d1ea794ee38d03b7ea268294baa69236060d7..58054d5192bf4d97490c27f447c95b37b7042a08 100644 (file)
@@ -1,5 +1,5 @@
 #  -*- coding: iso-8859-1 -*-
-# Copyright (C) 2007-2016  CEA/DEN, EDF R&D
+# Copyright (C) 2007-2019  CEA/DEN, EDF R&D
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -806,8 +806,8 @@ class MEDCouplingBasicsTest(unittest.TestCase):
         vals*=1e-5
         eps0=DataArrayDouble(m0.data)-vals ; eps0.abs()
         self.assertTrue(eps0.findIdsInRange(1e-17,1e300).empty())
-        self.assertTrue(DataArrayInt(m0.indices).isEqual(DataArrayInt([0,1,3,1,4,5,4,6,7,6,8,9,8,10,11,10,12,13,12,14,15,14,16,17,16,18,19,18,20,21,20,22,23,22,24,25,24,26,27,0,2,3,1,3,5,4,5,7,6,7,9,8,9,11,10,11,13,12,13,15,14,15,17,16,17,19,18,19,21,20,21,23,22,23,25,24,25,27,0,2,3,1,3,5,4,5,7,6,7,9,8,9,11,10,11,13,12,13,15,14,15,17,16,17,19,18,19,21,20,21,23,22,23,25,24,25,27,0,2,3,1,3,5,4,5,7,6,7,9,8,9,11,10,11,13,12,13,15,14,15,17,16,17,19,18,19,21,20,21,23,22,23,25,24,25,27,0,2,3,1,3,5,4,5,7,6,7,9,8,9,11,10,11,13,12,13,15,14,15,17,16,17,19,18,19,21,20,21,23,22,23,25,24,25,27,0,1,3,1,4,5,4,6,7,6,8,9,8,10,11,10,12,13,12,14,15,14,16,17,16,18,19,18,20,21,20,22,23,22,24,25,24,26,27,0,1,3,1,4,5,4,6,7,6,8,9,8,10,11,10,12,13,12,14,15,14,16,17,16,18,19,18,20,21,20,22,23,22,24,25,24,26,27,0,1,3,1,4,5,4,6,7,6,8,9,8,10,11,10,12,13,12,14,15,14,16,17,16,18,19,18,20,21,20,22,23,22,24,25,24,26,27])))
-        self.assertTrue(DataArrayInt(m0.indptr).isEqual(DataArrayInt([0,3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60,63,66,69,72,75,78,81,84,87,90,93,96,99,102,105,108,111,114,117,120,123,126,129,132,135,138,141,144,147,150,153,156,159,162,165,168,171,174,177,180,183,186,189,192,195,198,201,204,207,210,213,216,219,222,225,228,231,234,237,240,243,246,249,252,255,258,261,264,267,270,273,276,279,282,285,288,291,294,297,300,303,306,309,312])))
+        self.assertTrue(DataArrayInt32(m0.indices).isEqual(DataArrayInt32([0,1,3,1,4,5,4,6,7,6,8,9,8,10,11,10,12,13,12,14,15,14,16,17,16,18,19,18,20,21,20,22,23,22,24,25,24,26,27,0,2,3,1,3,5,4,5,7,6,7,9,8,9,11,10,11,13,12,13,15,14,15,17,16,17,19,18,19,21,20,21,23,22,23,25,24,25,27,0,2,3,1,3,5,4,5,7,6,7,9,8,9,11,10,11,13,12,13,15,14,15,17,16,17,19,18,19,21,20,21,23,22,23,25,24,25,27,0,2,3,1,3,5,4,5,7,6,7,9,8,9,11,10,11,13,12,13,15,14,15,17,16,17,19,18,19,21,20,21,23,22,23,25,24,25,27,0,2,3,1,3,5,4,5,7,6,7,9,8,9,11,10,11,13,12,13,15,14,15,17,16,17,19,18,19,21,20,21,23,22,23,25,24,25,27,0,1,3,1,4,5,4,6,7,6,8,9,8,10,11,10,12,13,12,14,15,14,16,17,16,18,19,18,20,21,20,22,23,22,24,25,24,26,27,0,1,3,1,4,5,4,6,7,6,8,9,8,10,11,10,12,13,12,14,15,14,16,17,16,18,19,18,20,21,20,22,23,22,24,25,24,26,27,0,1,3,1,4,5,4,6,7,6,8,9,8,10,11,10,12,13,12,14,15,14,16,17,16,18,19,18,20,21,20,22,23,22,24,25,24,26,27])))
+        self.assertTrue(DataArrayInt32(m0.indptr).isEqual(DataArrayInt32([0,3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60,63,66,69,72,75,78,81,84,87,90,93,96,99,102,105,108,111,114,117,120,123,126,129,132,135,138,141,144,147,150,153,156,159,162,165,168,171,174,177,180,183,186,189,192,195,198,201,204,207,210,213,216,219,222,225,228,231,234,237,240,243,246,249,252,255,258,261,264,267,270,273,276,279,282,285,288,291,294,297,300,303,306,309,312])))
         #
         rem2=MEDCouplingRemapper() ; rem2.setIntersectionType(Barycentric)
         rem2.prepare(b,a,"P0P1")
@@ -839,7 +839,7 @@ class MEDCouplingBasicsTest(unittest.TestCase):
         rem.setMinDotBtwPlane3DSurfIntersect(0.99)# this line is important it is to tell to remapper to select only cells with very close orientation
         rem.prepare(skinAndNonConformCells,skinAndNonConformCells,"P0P0")
         mat=rem.getCrudeCSRMatrix()
-        indptr=DataArrayInt(mat.indptr)
+        indptr=DataArrayInt32(mat.indptr) #not depend on MEDCouplingUse64BitIDs()
         indptr2=indptr.deltaShiftIndex()
         cellIdsOfNonConformCells=indptr2.findIdsNotEqual(1)
         cellIdsOfSkin=indptr2.findIdsEqual(1)
@@ -998,8 +998,10 @@ class MEDCouplingBasicsTest(unittest.TestCase):
         self.assertEqual(rem.getCrudeMatrix(),[{0: 1.0}, {1: 1.0}])
         rem2=MEDCouplingRemapper()
         rem2.setIntersectionType(PointLocator)
-        rem2.prepare(mt,ms,"P0P0") # reverse mt<->ms
-        self.assertEqual(rem2.getCrudeMatrix(),[{0: 1.0}, {1: 1.0}])
+        ##
+        # 2D to 3D with point locator does not make sense:
+        ##
+        self.assertRaises(InterpKernelException, rem2.prepare,mt,ms,"P0P0")
         pass
 
     def test2D1Dand1D2DPointLocator1(self):
@@ -1155,7 +1157,7 @@ class MEDCouplingBasicsTest(unittest.TestCase):
         self.assertEqual(1, len(rmp.getCrudeMatrix()[0]))
         pass
 
-    @unittest.skipUnless(MEDCouplingHasNumPyBindings() and MEDCouplingHasSciPyBindings() and IsCXX11Compiled(),"requires numpy AND scipy AND C++11")
+    @unittest.skipUnless(MEDCouplingHasNumPyBindings() and MEDCouplingHasSciPyBindings(),"requires numpy AND scipy AND C++11")
     def testP1P1PL3DSpaceFrom1DTo0D(self):
         from scipy.sparse import csr_matrix
         from numpy import array
@@ -1192,7 +1194,7 @@ class MEDCouplingBasicsTest(unittest.TestCase):
         rem.setIntersectionType(PointLocator)
         self.assertEqual(rem.prepare(src,trg,"P1P1"),1)
         mat=rem.getCrudeCSRMatrix()
-        row=array([2,2, 3,3, 0,0, 1,1]) # here ref to target point 3 
+        row=array([2,2, 3,3, 0,0, 1,1]) # here ref to target point 3
         col=array([1,2, 1,2, 1,2, 1,2])
         data=array([0.1,0.9, 0.3,0.7, 0.5,0.5, 0.8,0.2])
         mExp2=csr_matrix((data,(row,col)),shape=(4,3))
@@ -1200,6 +1202,106 @@ class MEDCouplingBasicsTest(unittest.TestCase):
         self.assertAlmostEqual(delta2.sum(),0.,14)
         pass
 
+    def testSetMatrix1(self):
+        """ Remapper has now setCrudeMatrix method to reload matrix to skip prepare phase """
+        cooS=DataArrayDouble([1,1, 7,1, 7,2, 1,2],4,2)
+        cooT=DataArrayDouble([0,0, 3,0, 3,3, 0,3, 6,0, 12,0, 12,3, 6,3],8,2)
+        ms=MEDCouplingUMesh("source",2) ; ms.allocateCells(1) ; ms.insertNextCell(NORM_QUAD4,[0,1,2,3]) ; ms.setCoords(cooS)
+        mt=MEDCouplingUMesh("target",2) ; mt.allocateCells(2) ; mt.insertNextCell(NORM_QUAD4,[0,1,2,3]) ; mt.insertNextCell(NORM_QUAD4,[4,5,6,7]) ; mt.setCoords(cooT)
+        rem=MEDCouplingRemapper()
+        self.assertEqual(rem.prepare(ms,mt,"P0P0"),1) # [{0: 2.0}, {0: 1.0}]
+        fs=MEDCouplingFieldDouble(ON_CELLS)
+        fs.setMesh(ms)
+        fs.setArray(DataArrayDouble([10]))
+        fs.checkConsistencyLight()
+        #
+        fs.setNature(ExtensiveConservation)
+        self.assertTrue(rem.transferField(fs,1e300).getArray().isEqual(DataArrayDouble([20./3,10./3.]),1e-12))# sum is equal to 10. First value is twice than second value
+        #
+        fs.setNature(ExtensiveMaximum)
+        self.assertTrue(rem.transferField(fs,1e300).getArray().isEqual(DataArrayDouble([20./6.,10./6.]),1e-12))#sum is equal to 5 (10/2. because only half part on input cell is intercepted by the target cells). First value is twice than second value
+        #
+        fs.setNature(IntensiveConservation)
+        self.assertTrue(rem.transferField(fs,1e300).getArray().isEqual(DataArrayDouble([2./9.*10.,1./18.*10.]),1e-12))#
+        #
+        fs.setNature(IntensiveMaximum)
+        self.assertTrue(rem.transferField(fs,1e300).getArray().isEqual(DataArrayDouble([10.,10.]),1e-12))#
+        ####
+        rem2=MEDCouplingRemapper()
+        rem2.setCrudeMatrix(ms,mt,"P0P0",rem.getCrudeMatrix())
+        fs.setNature(ExtensiveConservation)
+        self.assertTrue(rem2.transferField(fs,1e300).getArray().isEqual(DataArrayDouble([20./3,10./3.]),1e-12))
+        #
+        fs.setNature(ExtensiveMaximum)
+        self.assertTrue(rem2.transferField(fs,1e300).getArray().isEqual(DataArrayDouble([20./6.,10./6.]),1e-12))
+        #
+        fs.setNature(IntensiveConservation)
+        self.assertTrue(rem2.transferField(fs,1e300).getArray().isEqual(DataArrayDouble([2./9.*10.,1./18.*10.]),1e-12))
+        #
+        fs.setNature(IntensiveMaximum)
+        self.assertTrue(rem2.transferField(fs,1e300).getArray().isEqual(DataArrayDouble([10.,10.]),1e-12))
+        #
+        srcFt=MEDCouplingFieldTemplate.New(ON_CELLS);
+        trgFt=MEDCouplingFieldTemplate.New(ON_CELLS);
+        srcFt.setMesh(ms);
+        trgFt.setMesh(mt);
+        rem3=MEDCouplingRemapper()
+        rem3.setCrudeMatrixEx(srcFt,trgFt,rem.getCrudeMatrix())
+        fs.setNature(ExtensiveConservation)
+        self.assertTrue(rem3.transferField(fs,1e300).getArray().isEqual(DataArrayDouble([20./3,10./3.]),1e-12))
+        pass
+
+    @unittest.skipUnless(MEDCouplingHasNumPyBindings() and MEDCouplingHasSciPyBindings(),"requires numpy AND scipy")
+    def testSetMatrix2(self):
+        """ Remapper has now setCrudeMatrix method to reload matrix to skip prepare phase. Same as testSetMatrix1 but with CSR scipy matrix """
+        arrx_s=DataArrayDouble(6) ; arrx_s.iota()
+        arry_s=DataArrayDouble(6) ; arry_s.iota()
+        ms=MEDCouplingCMesh() ; ms.setCoords(arrx_s,arry_s)
+        ms=ms.buildUnstructured()
+        #
+        arrx_t=DataArrayDouble([2.5,4.5,5.5])
+        arry_t=DataArrayDouble([2.5,3.5,5.5])
+        mt=MEDCouplingCMesh() ; mt.setCoords(arrx_t,arry_t)
+        mt=mt.buildUnstructured()
+        #
+        rem=MEDCouplingRemapper()
+        self.assertEqual(rem.prepare(ms,mt,"P0P0"),1)
+        #
+        fs=MEDCouplingFieldDouble(ON_CELLS)
+        fs.setMesh(ms)
+        arr=DataArrayDouble(25) ; arr.iota()
+        fs.setArray(arr)
+        fs.checkConsistencyLight()
+        #
+        fs.setNature(ExtensiveConservation)
+        self.assertTrue(rem.transferField(fs,1e300).getArray().isEqual(DataArrayDouble([54.25,11.75,79.25,16.75]),1e-12))
+        mat=rem.getCrudeCSRMatrix()
+        rem2=MEDCouplingRemapper()
+        rem2.setCrudeMatrix(ms,mt,"P0P0",mat)
+        self.assertTrue(rem2.transferField(fs,1e300).getArray().isEqual(DataArrayDouble([54.25,11.75,79.25,16.75]),1e-12))
+        pass
+
+    def testSmallTetraCell(self):
+        """This test is a non regression test. When using tetra/tetra P0P0 interpolation on very small cells the
+        3x3 matrix in the TetraAffine contains very small values and so the determinant is small (cubic).
+        So the tetra was detected as flat. Now the infinite norm of matrix is considered to establish if matrix is inversible or not."""
+        coords = [(-0.019866666666666668, 0.02, 0.002), (-0.020000073463967143, 0.019999926535763005, 0.0018666666666666673), (-0.020000073463967143, 0.019999926535763005, 0.002), (-0.020000072974206463, 0.019866593202430387, 0.002)]
+        m=MEDCouplingUMesh("mesh",3)
+        m.allocateCells()
+        m.insertNextCell(NORM_TETRA4,[0,1,2,3])
+        m.setCoords(DataArrayDouble(coords))
+        rem=MEDCouplingRemapper()
+        rem.setPrecision(1e-12)
+        rem.prepare(m,m,"P0P0")
+        mat=rem.getCrudeMatrix()
+        self.assertTrue(len(mat)==1)
+        self.assertTrue(len(mat[0])==1)
+        self.assertTrue(list(mat[0].keys())==[0])
+        res=list(mat[0].values())[0]
+        ref=float(m.getMeasureField(True).getArray())
+        self.assertTrue(abs(res-ref)/ref<1e-12)
+        pass
+
     def checkMatrix(self,mat1,mat2,nbCols,eps):
         self.assertEqual(len(mat1),len(mat2))
         for i in range(len(mat1)):