+ 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 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)
+