+ def myTester1(self,fs2,mmq):
+ dataExp=DataArrayDouble([0.,0.,0.,1.,0.,0.,0.,1.,0.,1.,1.,0.,0.,0.,1.,1.,0.,1.,0.,1.,1.,1.,1.,1.,2.,0.,0.,3.,0.,0.,2.,1.,0.,3.,1.,0.,2.,0.,1.,3.,0.,1.,2.,1.,1.,3.,1.,1.,0.5, 0.,0.,0.,0.5, 0.,0.5, 1.,0.,1.,0.5, 0.,0.5, 0.,1.,0.,0.5, 1.,0.5, 1.,1.,1.,0.5, 1.,1.,0.,0.5, 0.,0.,0.5, 0.,1.,0.5, 1.,1.,0.5, 2.5, 0.,0.,2.,0.5, 0.,2.5, 1.,0.,3.,0.5, 0.,2.5, 0.,1.,2.,0.5, 1.,2.5, 1.,1.,3.,0.5, 1.,3.,0.,0.5, 2.,0.,0.5, 2.,1.,0.5, 3.,1.,0.5],40,3)
+ dataExp1=DataArrayInt([1,0,2,3,5,4,6,7,16,17,18,19,20,21,22,23,24,25,26,27,9,8,10,11,13,12,14,15,28,29,30,31,32,33,34,35,36,37,38,39])
+ dataExp2=DataArrayDouble([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0.5,1,2.5,2,4.5,5,6.5,6,3,2,4,5,8.5,9,10.5,10,12.5,13,14.5,14,11,10,12,13])
+ fToTest=fs2[0][0].field(mmq)
+ self.assertEqual(fToTest.getTime(),[3.,1,2])
+ mTest=MEDCoupling1SGTUMesh(fToTest.getMesh())
+ self.assertTrue(mTest.getNodalConnectivity().isEqual(dataExp1))
+ self.assertTrue(mTest.getCoords().isEqual(dataExp,1e-12))
+ self.assertTrue(fToTest.getArray().isEqual(dataExp2,1e-12))
+ # testing 2nd timestep
+ fToTest=fs2[0][1].field(mmq)
+ self.assertEqual(fToTest.getTime(),[5.,3,4])
+ mTest=MEDCoupling1SGTUMesh(fToTest.getMesh())
+ self.assertTrue(mTest.getNodalConnectivity().isEqual(dataExp1))
+ self.assertTrue(mTest.getCoords().isEqual(dataExp,1e-12))
+ self.assertTrue(fToTest.getArray().isEqual(2*dataExp2,1e-12))
+ pass
+
+ def testFieldsLinearToQuadratic2(self):
+ """Same than testFieldsLinearToQuadratic but with profile on NODES"""
+ fname="Pyfile118.med"
+ arr=DataArrayDouble([0,1])
+ m=MEDCouplingCMesh();
+ m.setCoords(arr,arr,arr)
+ m=m.buildUnstructured()
+ m2=m.deepCopy()
+ m2.translate([2,0,0])
+ m3=MEDCouplingUMesh.MergeUMeshes([m,m2])
+ m3.setName("mesh")
+ # add a point for fun
+ m3.setCoords(DataArrayDouble.Aggregate(m3.getCoords(),DataArrayDouble([1.5,1.5,1.5],1,3)))
+ #
+ mm=MEDFileUMesh()
+ mm[0]=m3
+ mmq=mm.linearToQuadratic(0)
+ mms=MEDFileMeshes() ; mms.pushMesh(mm)
+ mmsq=MEDFileMeshes() ; mmsq.pushMesh(mmq)
+ #
+ f=MEDCouplingFieldDouble(ON_NODES)
+ f.setName("field")
+ f.setMesh(m3)
+ f.setTime(3.,1,2)
+ arr=DataArrayDouble(8) ; arr.iota()
+ arr.iota()
+ f.setArray(arr)
+ f1ts=MEDFileField1TS()
+ pfl=DataArrayInt([8,9,10,11,12,13,14,15]) ; pfl.setName("pfl")
+ f1ts.setFieldProfile(f,mm,0,pfl) # f lying on 8 nodes of cell #1
+ f1ts_2=f1ts.deepCopy()
+ f1ts_2.setTime(3,4,5.)
+ f1ts_2.getUndergroundDataArray()[:]*=4.
+ fmts=MEDFileFieldMultiTS()
+ fmts.pushBackTimeStep(f1ts)
+ fmts.pushBackTimeStep(f1ts_2)
+ fs=MEDFileFields()
+ fs.pushField(fmts)
+ fs2=fs.linearToQuadratic(mms,mmsq)
+ mms.write(fname,2) ; fs.write(fname,0)
+ #
+ self.myTester2(fs2,mmq)
+ # Read/write
+ mms=MEDFileMeshes(fname) ; fs=MEDFileFields(fname)
+ mmq=mms[0].linearToQuadratic(0) ; mmqs=MEDFileMeshes() ; mmqs.pushMesh(mmq)
+ fs2=fs.linearToQuadratic(mms,mmqs)
+ self.myTester2(fs2,mmq)
+ ## More vicious add single node 16
+ mm=MEDFileUMesh()
+ mm[0]=m3
+ mmq=mm.linearToQuadratic(0)
+ mms=MEDFileMeshes() ; mms.pushMesh(mm)
+ mmsq=MEDFileMeshes() ; mmsq.pushMesh(mmq)
+ #
+ f=MEDCouplingFieldDouble(ON_NODES)
+ f.setName("field")
+ f.setMesh(m3)
+ f.setTime(3.,1,2)
+ arr=DataArrayDouble(9) ; arr.iota()
+ arr.iota()
+ f.setArray(arr)
+ f1ts=MEDFileField1TS()
+ pfl=DataArrayInt([8,9,10,11,12,13,14,15,16]) ; pfl.setName("pfl")
+ f1ts.setFieldProfile(f,mm,0,pfl) # f lying on 9 nodes of cell #1 + orphan node
+ fmts=MEDFileFieldMultiTS()
+ fmts.pushBackTimeStep(f1ts)
+ fs=MEDFileFields()
+ fs.pushField(fmts)
+ fs2=fs.linearToQuadratic(mms,mmsq)
+ #
+ pflExpected=DataArrayInt([8,9,10,11,12,13,14,15,16,29,30,31,32,33,34,35,36,37,38,39,40]) ; pflExpected.setName("pfl_NODE")
+ f1tsToTest=fs2[0][0]
+ exp1=DataArrayDouble([0,1,2,3,4,5,6,7,8,0.5,1,2.5,2,4.5,5,6.5,6,3,2,4,5])
+ assert(f1tsToTest.getProfile("pfl_NODE").isEqual(pflExpected))
+ assert(f1tsToTest.getUndergroundDataArray().isEqual(exp1,1e-12))
+ assert(f1tsToTest.getFieldSplitedByType()==[(40,[(1,(0,21),'pfl_NODE','')])])
+ pass
+
+ def myTester2(self,fs2,mmq):
+ pflExpected=DataArrayInt([8,9,10,11,12,13,14,15,29,30,31,32,33,34,35,36,37,38,39,40]) ; pflExpected.setName("pfl_NODE")
+ f1tsToTest=fs2[0][0]
+ exp1=DataArrayDouble([0,1,2,3,4,5,6,7,0.5,1,2.5,2,4.5,5,6.5,6,3,2,4,5])
+ self.assertTrue(f1tsToTest.getProfile("pfl_NODE").isEqual(pflExpected))
+ self.assertTrue(f1tsToTest.getUndergroundDataArray().isEqual(exp1,1e-12))
+ self.assertEqual(f1tsToTest.getFieldSplitedByType(),[(NORM_ERROR,[(1,(0,20),'pfl_NODE','')])])
+ fToTest=fs2[0][0].field(mmq)
+ self.assertEqual(fToTest.getTime(),[3.,1,2])
+ mTest=MEDCoupling1SGTUMesh(fToTest.getMesh())
+ self.assertTrue(mTest.getNodalConnectivity().isEqual(DataArrayInt([1,0,2,3,5,4,6,7,8,9,10,11,12,13,14,15,16,17,18,19])))
+ self.assertTrue(mTest.getCoords().isEqual(DataArrayDouble([(2,0,0),(3,0,0),(2,1,0),(3,1,0),(2,0,1),(3,0,1),(2,1,1),(3,1,1),(2.5,0,0),(2,0.5,0),(2.5,1,0),(3,0.5,0),(2.5,0,1),(2,0.5,1),(2.5,1,1),(3,0.5,1),(3,0,0.5),(2,0,0.5),(2,1,0.5),(3,1,0.5)],20,3),1e-12))
+ self.assertTrue(fToTest.getArray().isEqual(exp1,1e-12))
+ # 2nd Time step
+ f1tsToTest=fs2[0][1]
+ self.assertTrue(f1tsToTest.getProfile("pfl_NODE").isEqual(pflExpected))
+ self.assertTrue(f1tsToTest.getUndergroundDataArray().isEqual(4*exp1,1e-12))
+ self.assertEqual(f1tsToTest.getFieldSplitedByType(),[(NORM_ERROR,[(1,(0,20),'pfl_NODE','')])])
+ fToTest=fs2[0][1].field(mmq)
+ self.assertEqual(fToTest.getTime(),[5.,3,4])
+ mTest=MEDCoupling1SGTUMesh(fToTest.getMesh())
+ self.assertTrue(mTest.getNodalConnectivity().isEqual(DataArrayInt([1,0,2,3,5,4,6,7,8,9,10,11,12,13,14,15,16,17,18,19])))
+ self.assertTrue(mTest.getCoords().isEqual(DataArrayDouble([(2,0,0),(3,0,0),(2,1,0),(3,1,0),(2,0,1),(3,0,1),(2,1,1),(3,1,1),(2.5,0,0),(2,0.5,0),(2.5,1,0),(3,0.5,0),(2.5,0,1),(2,0.5,1),(2.5,1,1),(3,0.5,1),(3,0,0.5),(2,0,0.5),(2,1,0.5),(3,1,0.5)],20,3),1e-12))
+ self.assertTrue(fToTest.getArray().isEqual(4*exp1,1e-12))
+
+ pass
+
+ def testSetFieldProfileFlatly1(self):
+ """ Sometimes for downstream code fan of profiles, profile are requested unconditionally. setFieldProfile try to reduce at most profile usage. So setFieldProfileFlatly has been added to always create
+ a profile."""
+ arr=DataArrayDouble(10) ; arr.iota()
+ m=MEDCouplingCMesh() ; m.setCoords(arr,arr)
+ m=m.buildUnstructured()
+ m2=m.deepCopy()
+ m2.simplexize(0)
+ m=MEDCouplingUMesh.MergeUMeshes(m2,m)
+ m.setName("mesh")
+ mm=MEDFileUMesh()
+ mm[0]=m
+ f=MEDCouplingFieldDouble(ON_CELLS)
+ f.setMesh(m)
+ arr=DataArrayDouble(m.getNumberOfCells())
+ arr.iota()
+ f.setArray(arr)
+ f.setName("field")
+ pfl=DataArrayInt(m.getNumberOfCells()) ; pfl.iota() ; pfl.setName("pfl")
+ #
+ refSp=[(3,[(0,(0,162),'','')]),(4,[(0,(162,243),'','')])]
+ refSp1=[(3,[(0,(0,162),'pfl_NORM_TRI3','')]),(4,[(0,(162,243),'pfl_NORM_QUAD4','')])]
+ #
+ f1ts=MEDFileField1TS()
+ f1ts.setFieldProfile(f,mm,0,pfl)
+ self.assertEqual(f1ts.getPfls(),()) # here setFieldProfile has detected useless pfl -> no pfl
+ self.assertEqual(f1ts.getFieldSplitedByType(),refSp)
+ self.assertTrue(f1ts.field(mm).isEqual(f,1e-12,1e-12)) # the essential
+ #
+ f1ts=MEDFileField1TS()
+ f1ts.setFieldProfileFlatly(f,mm,0,pfl) # no optimization attempt. Create pfl unconditionally
+ self.assertEqual(f1ts.getPfls(),("%s_NORM_TRI3"%pfl.getName(),"%s_NORM_QUAD4"%pfl.getName()))
+ self.assertEqual(f1ts.getFieldSplitedByType(),refSp1)
+ self.assertTrue(f1ts.field(mm).isEqual(f,1e-12,1e-12)) # the essential
+ self.assertTrue(f1ts.getProfile("pfl_NORM_TRI3").isIota(162))
+ self.assertTrue(f1ts.getProfile("pfl_NORM_QUAD4").isIota(81))
+ pass
+
+ def testRmGroupAtSpeLevelAndMultiLevGrpCreation(self):
+ """ Here multi level groups are created"""
+ arr=DataArrayDouble(11) ; arr.iota()
+ m=MEDCouplingCMesh() ; m.setCoords(arr,arr)
+ m=m.buildUnstructured()
+ m.setName("mesh")
+ m1=m.buildDescendingConnectivity()[0]
+ mm=MEDFileUMesh()
+ mm[0]=m ; mm[-1]=m1
+ ################
+ grpName="grp0"
+ grp0_0=DataArrayInt([0,1,2,6]) ; grp0_0.setName(grpName)
+ grp0_1=DataArrayInt([0,1,2,7]) ; grp0_1.setName(grpName)
+ grp1=DataArrayInt([1,2,3,5,6]) ; grp1.setName("grp1")
+ grp2=DataArrayInt([2,3,5,8]) ; grp2.setName("grp2")
+ ################ ajouter un groupe sur plusieurs niveau
+ mm.addGroup(0,grp1)
+ mm.addGroup(-1,grp2)
+ mm.addGroup(0,grp0_0)
+ mm.addGroup(-1,grp0_1)
+ self.assertEqual(mm.getGrpNonEmptyLevels(grpName),(0,-1))
+ self.assertTrue(mm.getGroupArr(0,grpName).isEqual(grp0_0))
+ self.assertTrue(mm.getGroupArr(-1,grpName).isEqual(grp0_1))
+ self.assertTrue(mm.getGroupArr(0,"grp1").isEqual(grp1))
+ self.assertTrue(mm.getGroupArr(-1,"grp2").isEqual(grp2))
+ self.assertRaises(InterpKernelException,mm.addGroup,-1,grp0_1) # raise
+ self.assertTrue(mm.getGroupArr(0,grpName).isEqual(grp0_0))
+ self.assertTrue(mm.getGroupArr(-1,grpName).isEqual(grp0_1))
+ self.assertTrue(mm.getGroupArr(0,"grp1").isEqual(grp1))
+ self.assertTrue(mm.getGroupArr(-1,"grp2").isEqual(grp2))
+ mm.removeGroupAtLevel(0,grpName)
+ self.assertEqual(mm.getGrpNonEmptyLevels(grpName),(-1,))
+ self.assertTrue(mm.getGroupArr(-1,grpName).isEqual(grp0_1))
+ self.assertTrue(mm.getGroupArr(0,"grp1").isEqual(grp1))
+ self.assertTrue(mm.getGroupArr(-1,"grp2").isEqual(grp2))
+ mm.removeGroupAtLevel(-1,grpName)
+ self.assertEqual(mm.getGrpNonEmptyLevels(grpName),())
+ self.assertRaises(InterpKernelException,mm.removeGroupAtLevel,-2,grpName)
+ mm.addGroup(-1,grp0_1)
+ mm.addGroup(0,grp0_0)
+ self.assertEqual(mm.getGrpNonEmptyLevels(grpName),(0,-1))
+ self.assertTrue(mm.getGroupArr(0,grpName).isEqual(grp0_0))
+ self.assertTrue(mm.getGroupArr(-1,grpName).isEqual(grp0_1))
+ self.assertTrue(mm.getGroupArr(0,"grp1").isEqual(grp1))
+ self.assertTrue(mm.getGroupArr(-1,"grp2").isEqual(grp2))
+ pass
+
+ def testYutaka(self):
+ """ Thank you to Yutaka Nishizawa for having report this bug. At level -1, adding a first group on all entities leads to a group lying on family 0...
+ Then rearrange method removes unused entites by putting 0 on them -> Previously group has been modified by rearrange. Should not !"""
+ mn="mesh"
+ m=MEDCouplingCMesh()
+ arr=DataArrayDouble(4) ; arr.iota()
+ m.setCoords(arr,arr,arr)
+ m=m.buildUnstructured()
+ m.setName(mn)
+ #
+ m=m.buildUnstructured()
+ m1=m.buildDescendingConnectivity()[0]
+ #
+ mm=MEDFileUMesh()
+ mm[0]=m
+ mm[-1]=m1
+ #
+ grp0=DataArrayInt([0,1,2]) ; grp0.setName("grp0")
+ mm.addGroup(0,grp0)
+ grp1=DataArrayInt([3,4,5,6]) ; grp1.setName("grp1")
+ mm.addGroup(0,grp1)
+ grp2=DataArrayInt([7,8,9]) ; grp2.setName("grp2")
+ mm.addGroup(0,grp2)
+ grp3=DataArrayInt.Range(0,m1.getNumberOfCells(),1) ; grp3.setName("grp3")
+ mm.addGroup(-1,grp3)
+ self.assertNotIn(0,mm.getFamiliesIdsOnGroup("grp3")) # bug was here !
+ grp4=DataArrayInt([3,5,8,10]) ; grp4.setName("grp4")
+ mm.addNodeGroup(grp4)
+ mm.rearrangeFamilies()
+ self.assertEqual(mm.getGrpNonEmptyLevelsExt("grp0"),(0,))
+ self.assertEqual(mm.getGrpNonEmptyLevelsExt("grp1"),(0,))
+ self.assertEqual(mm.getGrpNonEmptyLevelsExt("grp2"),(0,))
+ self.assertEqual(mm.getGrpNonEmptyLevelsExt("grp3"),(-1,))
+ self.assertEqual(mm.getGrpNonEmptyLevelsExt("grp4"),(1,))
+
+ for grp in [grp0,grp1,grp2,grp3,grp4]:
+ self.assertTrue(mm.getGroupArr(mm.getGrpNonEmptyLevelsExt(grp.getName())[0],grp.getName()).isEqual(grp))
+ pass
+ pass
+
+ def tessContxtMger1TS(self):
+ fname="Pyfile119.med"
+ coo=DataArrayDouble(1000) ; coo.iota()
+ m=MEDCouplingUMesh.Build0DMeshFromCoords(coo)
+ m.setName("mesh")
+ WriteMesh(fname,m,True)
+ f=MEDCouplingFieldDouble(ON_CELLS)
+ f.setMesh(m)
+ f.setName("Field")
+ arr=DataArrayDouble(m.getNumberOfCells())
+ f.setArray(arr)
+ f.checkConsistencyLight()
+ for i in range(10):
+ arr[:]=float(i+1)
+ f.setTime(float(i),i,0)
+ WriteFieldUsingAlreadyWrittenMesh(fname,f)
+ pass
+ #
+ mm=MEDFileMesh.New(fname)
+ fmts=MEDFileFieldMultiTS(fname,False)
+ refSize=fmts.getHeapMemorySize()
+ for f1ts in fmts:
+ with f1ts:
+ f=f1ts.field(mm)
+ pass
+ pass
+ self.assertIn(fmts.getHeapMemorySize(),range(refSize,refSize+refSize//10))
+ pass
+