Salome HOME
bos #26461 [EDF] (2021) MedCoupling: debug tetra/tetra 3d intersector algorithm
[tools/medcoupling.git] / src / MEDCoupling_Swig / MEDCouplingRemapperTest.py
index 58054d5192bf4d97490c27f447c95b37b7042a08..144118b08bee8109b398a76074d527ebe3bf60d2 100644 (file)
@@ -1,5 +1,5 @@
 #  -*- coding: iso-8859-1 -*-
-# Copyright (C) 2007-2019  CEA/DEN, EDF R&D
+# Copyright (C) 2007-2021  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
@@ -955,7 +955,7 @@ class MEDCouplingBasicsTest(unittest.TestCase):
         m=rem.getCrudeCSRMatrix()
         row=array([1,1,2,2,3,3])
         col=array([0,1,1,2,5,6])
-        data=array([0.9,0.1,0.3,0.7,0.5,0.5])
+        data=array([1.8,0.2,0.6,1.4,1.0,1.0])
         mExp2=csr_matrix((data,(row,col)),shape=(5,11))
         diff=abs(m-mExp2)
         self.assertAlmostEqual(diff.sum(),0.,14)
@@ -1084,6 +1084,52 @@ class MEDCouplingBasicsTest(unittest.TestCase):
         self.assertTrue(ftrg.isEqual(ftrg2,1e-12,1e-12))
         pass
 
+    def testPointLocator2D2DNonConvexPolygons(self):
+        """ PointLocator remapper now correclty support non-convex polygons
+        """
+        src = MEDCouplingUMesh('src', 2)
+        coo = DataArrayDouble([(6,1),(6,2),(4,2),(4,3),(3,3),(3,4),(2,4),(2,6),(1,6),(1,8),(2,8),(2,9),(3,9),(3,8),(4,8),(4,9),(5,9),(5,8),
+          (6,8),(6,9),(7,9),(7,8),(8,8),(8,9),(9,9),(9,8),(10,8),(10,9),(11,9),(11,8),(12,8),(12,9),(13,9),(13,8),
+          (14,8),(14,9),(15,9),(15,8),(16,8),(16,6),(15,6),(15,4),(14,4),(14,3),(13,3),(13,2),(11,2),(11,1),(16,11),
+          (15,11),(15,13),(14,13),(14,14),(13,14),(13,15),(11,15),(11,16),(6,16),(6,15),(4,15),(4,14),(3,14),(3,13),(2,13),
+          (2,11),(1,11)])
+        src.setCoords(coo)
+        c = DataArrayInt([5, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
+         31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 5, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
+         21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
+         60, 61, 62, 63, 64, 65])
+        cI = DataArrayInt([0, 49, 98])
+        src.setConnectivity(c, cI)
+        src.checkConsistency()
+        tgt = MEDCouplingCMesh('tgt')
+        da = DataArrayDouble(18, 1); da.iota();
+        tgt.setCoords(da, da)
+        tgt = tgt.buildUnstructured()
+        srcF = MEDCouplingFieldDouble(ON_CELLS, ONE_TIME)
+        srcF.setArray(DataArrayDouble([25.,50.]))
+        srcF.setMesh(src)
+        srcF.setNature(IntensiveConservation)
+        remap = MEDCouplingRemapper()
+        remap.setIntersectionType(PointLocator)
+        remap.prepare(src, tgt, "P0P0")
+        tgtF = remap.transferField(srcF, 0.0)
+        ids1 = [137, 139, 141, 143, 145, 147, 149, 151, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 171, 172,
+        173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200,
+        201, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 242,
+        243, 244, 245, 246, 247, 248, 249, 250, 261, 262, 263, 264, 265]
+        ids2 = [23, 24, 25, 26, 27, 38, 39, 40, 41, 42, 43, 44, 45, 46, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 70, 71, 72, 73, 74, 75, 76,
+           77, 78, 79, 80, 81, 82, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113,
+           114, 115, 116, 117, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 138, 140, 142, 144, 146, 148, 150]
+        ids3 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 47, 48,
+                49, 50, 51, 52, 53, 65, 66, 67, 68, 69, 83, 84, 85, 86, 100, 101, 102, 118, 119, 135, 136, 152, 153, 169, 170, 186, 187, 188, 202, 203,
+                204, 205, 219, 220, 221, 222, 223, 235, 236, 237, 238, 239, 240, 241, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 266, 267, 268,
+                269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288]
+        a = tgtF.getArray()
+        self.assertTrue(a[ids1].isUniform(50.0, 1e-12))
+        self.assertTrue(a[ids2].isUniform(25.0, 1e-12))
+        self.assertTrue(a[ids3].isUniform(0.0, 1e-12))
+        pass
+
     def testExtrudedOnDiffZLev1(self):
         """Non regression bug : This test is base on P0P0 ExtrudedExtruded. This test checks that if the input meshes are not based on a same plane // OXY the interpolation works"""
         arrX=DataArrayDouble([0,1]) ; arrY=DataArrayDouble([0,1]) ; arrZ=DataArrayDouble([0,1,2])
@@ -1302,13 +1348,255 @@ class MEDCouplingBasicsTest(unittest.TestCase):
         self.assertTrue(abs(res-ref)/ref<1e-12)
         pass
 
+    def test3D0DPointLocator(self):
+        """
+        For pointlocator fans, Remapper support following intersection
+        IntersectionType == PointLocator
+        - source == 3D
+        - target == 0D
+        """
+        src = MEDCouplingUMesh("src",3)
+        src.allocateCells()
+        src.setCoords( DataArrayDouble([(0,0,0),(1,0,0),(0,1,0),(0,0,1)]) )
+        src.insertNextCell(NORM_TETRA4,[0,1,2,3])
+        trg = MEDCouplingUMesh.Build0DMeshFromCoords( DataArrayDouble([(0.4,0.3,0.07)]) )
+        # P1P1
+        rem=MEDCouplingRemapper()
+        rem.setIntersectionType(PointLocator)
+        rem.prepare(src,trg,"P1P1")
+        self.checkMatrix(rem.getCrudeMatrix(),[{0:0.23,1:0.4,2:0.3,3:0.07}],src.getNumberOfNodes(),1e-12)
+        # P1P0
+        rem=MEDCouplingRemapper()
+        rem.setIntersectionType(PointLocator)
+        rem.prepare(src,trg,"P1P0")
+        self.checkMatrix(rem.getCrudeMatrix(),[{0:0.23,1:0.4,2:0.3,3:0.07}],src.getNumberOfNodes(),1e-12)
+        # P0P1
+        rem=MEDCouplingRemapper()
+        rem.setIntersectionType(PointLocator)
+        rem.prepare(src,trg,"P0P1")
+        self.checkMatrix(rem.getCrudeMatrix(),[{0:1.0}],src.getNumberOfCells(),1e-12)
+        # P0P0
+        rem=MEDCouplingRemapper()
+        rem.setIntersectionType(PointLocator)
+        rem.prepare(src,trg,"P0P0")
+        self.checkMatrix(rem.getCrudeMatrix(),[{0:1.0}],src.getNumberOfCells(),1e-12)
+        pass
+
+    def test2D0DPointLocator(self):
+        """
+        For pointlocator fans, Remapper support following intersection
+        IntersectionType == PointLocator
+        - source == 2D
+        - target == 0D
+        """
+        src = MEDCouplingUMesh("src",2)
+        src.allocateCells()
+        src.setCoords( DataArrayDouble([(0,0),(1,0),(0,1)]) )
+        src.insertNextCell(NORM_TRI3,[0,1,2])
+        trg = MEDCouplingUMesh.Build0DMeshFromCoords( DataArrayDouble([(0.4,0.3)]) )
+        # P1P1
+        rem=MEDCouplingRemapper()
+        rem.setIntersectionType(PointLocator)
+        rem.prepare(src,trg,"P1P1")
+        self.checkMatrix(rem.getCrudeMatrix(),[{0:0.3,1:0.4,2:0.3}],src.getNumberOfNodes(),1e-12)
+        # P1P0
+        rem=MEDCouplingRemapper()
+        rem.setIntersectionType(PointLocator)
+        rem.prepare(src,trg,"P1P0")
+        self.checkMatrix(rem.getCrudeMatrix(),[{0:0.3,1:0.4,2:0.3}],src.getNumberOfNodes(),1e-12)
+        # P0P1
+        rem=MEDCouplingRemapper()
+        rem.setIntersectionType(PointLocator)
+        rem.prepare(src,trg,"P0P1")
+        self.checkMatrix(rem.getCrudeMatrix(),[{0:1.0}],src.getNumberOfNodes(),1e-12)
+        # P0P0
+        rem=MEDCouplingRemapper()
+        rem.setIntersectionType(PointLocator)
+        rem.prepare(src,trg,"P0P0")
+        self.checkMatrix(rem.getCrudeMatrix(),[{0:1.0}],src.getNumberOfNodes(),1e-12)
+        pass
+
+    def test1D0DPointLocator(self):
+        """
+        For pointlocator fans, Remapper support following intersection
+        IntersectionType == PointLocator
+        - source == 1D
+        - target == 0D
+        """
+        # P1P1 - 0
+        src = MEDCouplingUMesh("src",1)
+        src.allocateCells()
+        src.setCoords( DataArrayDouble([0,1]) )
+        src.insertNextCell(NORM_SEG2,[0,1])
+        trg = MEDCouplingUMesh.Build0DMeshFromCoords( DataArrayDouble([0.4]) )
+        rem=MEDCouplingRemapper()
+        rem.setIntersectionType(PointLocator)
+        rem.prepare(src,trg,"P1P1")
+        self.checkMatrix(rem.getCrudeMatrix(),[{0:0.6,1:0.4}],src.getNumberOfNodes(),1e-12)
+        # P1P1 - 1
+        src = MEDCouplingUMesh("src",1)
+        src.allocateCells()
+        src.setCoords( DataArrayDouble([0,1]) )
+        src.insertNextCell(NORM_SEG2,[1,0]) # permutation
+        trg = MEDCouplingUMesh.Build0DMeshFromCoords( DataArrayDouble([0.4]) )
+        rem=MEDCouplingRemapper()
+        rem.setIntersectionType(PointLocator)
+        rem.prepare(src,trg,"P1P1")
+        self.checkMatrix(rem.getCrudeMatrix(),[{0:0.6,1:0.4}],src.getNumberOfNodes(),1e-12)
+        # P1P1 - 2
+        src = MEDCouplingUMesh("src",1)
+        src.allocateCells()
+        src.setCoords( DataArrayDouble([1,0]) )
+        src.insertNextCell(NORM_SEG2,[0,1])
+        trg = MEDCouplingUMesh.Build0DMeshFromCoords( DataArrayDouble([0.4]) )
+        rem=MEDCouplingRemapper()
+        rem.setIntersectionType(PointLocator)
+        rem.prepare(src,trg,"P1P1")
+        self.checkMatrix(rem.getCrudeMatrix(),[{0:0.4,1:0.6}],src.getNumberOfNodes(),1e-12)
+        # P1P1 - 3 - 2DCurve
+        src = MEDCouplingUMesh("src",1)
+        src.allocateCells()
+        src.setCoords( DataArrayDouble([0,1]) )
+        src.insertNextCell(NORM_SEG2,[0,1])
+        trg = MEDCouplingUMesh.Build0DMeshFromCoords( DataArrayDouble([0.4]) )
+        src.changeSpaceDimension(2) ; trg.changeSpaceDimension(2)
+        src.rotate([-1.,-1.],1.2)
+        trg.rotate([-1.,-1.],1.2)
+        rem=MEDCouplingRemapper()
+        rem.setIntersectionType(PointLocator)
+        rem.prepare(src,trg,"P1P1")
+        self.checkMatrix(rem.getCrudeMatrix(),[{0:0.6,1:0.4}],src.getNumberOfNodes(),1e-12)
+        # P1P1 - 4
+        src = MEDCouplingUMesh("src",1)
+        src.allocateCells()
+        src.setCoords( DataArrayDouble([1.1,7.6,2.3,5.4]) )
+        src.insertNextCell(NORM_SEG2,[0,2])
+        src.insertNextCell(NORM_SEG2,[2,3])
+        src.insertNextCell(NORM_SEG2,[3,1])
+        for eps in [0,1e-13,-1e-13]:
+            trg = MEDCouplingUMesh.Build0DMeshFromCoords( DataArrayDouble([0.4,2.3+eps,4.,7.]) )
+            rem=MEDCouplingRemapper()
+            rem.setIntersectionType(PointLocator)
+            rem.prepare(src,trg,"P1P1")
+            rem.nullifiedTinyCoeffInCrudeMatrixAbs(1e-12)
+            self.checkMatrix(rem.getCrudeMatrix(),[{}, {2: 2.0}, {2: 0.4516129032258065, 3: 0.5483870967741935}, {1: 0.7272727272727273, 3: 0.27272727272727265}],src.getNumberOfNodes(),1e-12)
+        # P1P1 - 5 - descending order of coords in source mesh
+        src = MEDCouplingUMesh("src",1)
+        src.allocateCells()
+        src.setCoords( DataArrayDouble([3.,1.]) )
+        src.insertNextCell(NORM_SEG2,[0,1])
+        trg = MEDCouplingUMesh.Build0DMeshFromCoords( DataArrayDouble([2.3]) )
+        rem=MEDCouplingRemapper()
+        rem.setIntersectionType(PointLocator)
+        rem.prepare(src,trg,"P1P1")
+        self.checkMatrix(rem.getCrudeMatrix(),[{0:0.65,1:0.35}],src.getNumberOfNodes(),1e-12)
+        pass
+
+    @unittest.skipUnless(MEDCouplingHasNumPyBindings() and MEDCouplingHasSciPyBindings(),"requires numpy AND scipy")
+    def testRemToCSRMatrix(self):
+        import scipy
+        mPy = [{0:1.0,1:3.0,3:7.0,6:10.},{1:12.0,2:23.0}]
+        m = MEDCouplingRemapper.ToCSRMatrix(mPy,8)
+        self.assertTrue(isinstance(m,scipy.sparse.csr.csr_matrix))
+        self.assertEqual(m.getnnz(),6)
+        self.assertAlmostEqual(m[0,0],1.0,12)
+        self.assertAlmostEqual(m[0,1],3.0,12)
+        self.assertAlmostEqual(m[0,3],7.0,12)
+        self.assertAlmostEqual(m[0,6],10.0,12)
+        self.assertAlmostEqual(m[1,1],12.0,12)
+        self.assertAlmostEqual(m[1,2],23.0,12)
+        self.assertEqual(m.shape,(2,8))
+
+    def test_Interpolation2D3D_bbox_adjustment_1(self):
+        """ Interpolation 2D <-> 3D was not using bounding box adjustment.
+        In case of a 2D mesh perfectly aligned with the axis, the bounding box intersection was not working properly (flat bounding box).
+        """
+        ## Source
+        meshS = MEDCouplingUMesh('SupportOf_TEMPERATURE_OUT', 2)
+        coo = cooS = DataArrayDouble([(-0.00074999999999877595,0.00000000000000000000,0.00032540000000000005),
+                (-0.00049999999999755579,0.00025000000000140708,0.00032540000000000005),(-0.00049999999999755600,0.00000000000000000000,0.00032540000000000005),
+                (-0.00100000000000000002,0.00000000000000000000,0.00032540000000000005),(-0.00100000000000000002,0.00025000000000000543,0.00032540000000000005),
+                (-0.00075651925565617829,0.00034416831541328637,0.00032540000000000005)])  # the extra 5e-20 on Z is the true culprit :-)
+        meshS.setCoords(coo)
+        c = DataArrayInt([3, 0, 1, 2, 3, 0, 3, 4, 3, 5, 0, 4, 3, 5, 1, 0])
+        cI = DataArrayInt([0, 4, 8, 12, 16])
+        meshS.setConnectivity(c, cI)
+        meshS.checkConsistency()
+        ## Target
+        meshT = MEDCouplingUMesh('IJK_mesh', 3)
+        coo = DataArrayDouble([(-0.001,0,0.000303602),(-0.0009,0,0.000303602),(-0.0008,0,0.000303602),(-0.0007,0,0.000303602),(-0.0006,0,0.000303602),
+            (-0.0005,0,0.000303602),(-0.001,0.0005,0.000303602),(-0.0009,0.0005,0.000303602),(-0.0008,0.0005,0.000303602),(-0.0007,0.0005,0.000303602),
+            (-0.0006,0.0005,0.000303602),(-0.0005,0.0005,0.000303602),(-0.001,0,0.0003254),(-0.0009,0,0.0003254),(-0.0008,0,0.0003254),(-0.0007,0,0.0003254),
+            (-0.0006,0,0.0003254),(-0.0005,0,0.0003254),(-0.001,0.0005,0.0003254),(-0.0009,0.0005,0.0003254),(-0.0008,0.0005,0.0003254),(-0.0007,0.0005,0.0003254),
+            (-0.0006,0.0005,0.0003254),(-0.0005,0.0005,0.0003254)])
+        meshT.setCoords(coo)
+        c = DataArrayInt([18, 1, 0, 6, 7, 13, 12, 18, 19, 18, 2, 1, 7, 8, 14, 13, 19, 20, 18, 3, 2, 8, 9, 15, 14, 20, 21, 18, 4, 3, 9, 10, 16, 15, 21, 22,
+                             18, 5, 4, 10, 11, 17, 16, 22, 23])
+        cI = DataArrayInt([0, 9, 18, 27, 36, 45])
+        meshT.setConnectivity(c, cI)
+        meshT.checkConsistency()
+        ## Dummy field
+        fldSrc = MEDCouplingFieldDouble(ON_CELLS, ONE_TIME)
+        fldSrc.setMesh(meshS)
+        da = DataArrayDouble(meshS.getNumberOfCells())
+        da[:] = 50.0
+        fldSrc.setArray(da)
+        remap = MEDCouplingRemapper()
+        # remap.setBoundingBoxAdjustmentAbs(1.0e-5)  # was not taken into account for 2D/3D - but we don't even need it! Default value is OK.
+        remap.prepare(meshS, meshT, "P0P0")
+        fldSrc.setNature(IntensiveMaximum)
+        fldTgt = remap.transferField(fldSrc, -1.0)
+        self.assertTrue(fldTgt.getArray().isUniform(50.0, 1e-12))
+
+    def testGrandyBug1(self):
+        """
+        Non regression test relative to test tuleap26461
+        """
+        rem = MEDCouplingRemapper()
+        src_final = MEDCouplingUMesh("src_final",3)
+        src_final.setCoords( DataArrayDouble([0.74763179385813627,2.0528797000000716,0.42830000000000013,0.77426950622837643,2.0528797000000001,0.40000000000000036,0.77426950622837643,2.0528797000000001,0.42830000000000013,0.77426950622830537,2.0262419876297604,0.42830000000000013],4,3) )
+        src_final.allocateCells()
+        src_final.insertNextCell(NORM_TETRA4,[0,3,2,1])
+        trg_final = MEDCouplingUMesh("trg_final",3)
+        trg_final.setCoords( DataArrayDouble([0.81034725000000007,1.9988565499999984,0.40000000000000002,0.75632410000000005,2.0528796999999983,0.40000000000000002,0.75632410000000005,1.9988565499999984,0.41800000000000004,0.81034725000000007,2.0528796999999983,0.41800000000000004],4,3) )
+        trg_final.allocateCells()
+        trg_final.insertNextCell(NORM_TETRA4,[0,2,1,3])
+
+        ref_values = [# ref values coming from geom2medcoupling.py
+            (0.0,   1.671615506097834e-08),
+            (1e-12, 1.671615506712106e-08),
+            (1e-11, 1.671615512239666e-08),
+            (1e-10, 1.6716155675164925e-08),
+            (1e-9,  1.671616120285316e-08),
+            (1e-8,  1.6716216479802182e-08),
+            (1e-7,  1.671676925650806e-08),
+            (1e-6,  1.672014459316238e-08),
+            (1e-5,  1.6805275475457618e-08),
+            (1e-4,  1.7608769838220544e-08),
+            (1e-3,  2.5791583779126835e-08)
+        ]
+
+        for ty,ref_value in ref_values:
+            trg_final2 = trg_final.deepCopy()
+            trg_final2.translate([0,ty,0])
+            rem.setPrecision(1e-12)
+            rem.prepare(src_final,trg_final2,"P0P0")
+            mat_mc = rem.getCrudeMatrix()
+            csr_new = MEDCouplingRemapper.ToCSRMatrix(mat_mc,src_final.getNumberOfCells())
+            delta = abs(csr_new[0,0]-ref_value)/ref_value
+            self.assertTrue(delta < 1e-3)
+
     def checkMatrix(self,mat1,mat2,nbCols,eps):
         self.assertEqual(len(mat1),len(mat2))
         for i in range(len(mat1)):
-            self.assertTrue(max(mat2[i].keys())<nbCols)
-            self.assertTrue(max(mat1[i].keys())<nbCols)
-            self.assertTrue(min(mat2[i].keys())>=0)
-            self.assertTrue(min(mat1[i].keys())>=0)
+            if len(mat2[i].keys())>0:
+                self.assertTrue(max(mat2[i].keys())<nbCols)
+            if len(mat1[i].keys())>0:
+                self.assertTrue(max(mat1[i].keys())<nbCols)
+            if len(mat2[i].keys())>0:
+                self.assertTrue(min(mat2[i].keys())>=0)
+            if len(mat1[i].keys())>0:
+                self.assertTrue(min(mat1[i].keys())>=0)
             s1=set(mat1[i].keys()) ; s2=set(mat2[i].keys())
             for elt in s1.intersection(s2):
                 self.assertTrue(abs(mat1[i][elt]-mat2[i][elt])<eps)