+
+ def testMEDFileFieldsLoadSpecificEntities1(self):
+ nbNodes=11
+ fieldName="myField"
+ fileName="Pyfile87.med"
+ nbPdt=10
+ meshName="Mesh"
+ #
+ m=MEDCouplingCMesh()
+ arr=DataArrayDouble(nbNodes) ; arr.iota()
+ m.setCoords(arr)
+ m=m.buildUnstructured()
+ m.setName(meshName)
+ #
+ fmts=MEDFileFieldMultiTS()
+ for i in xrange(nbPdt):
+ f=MEDCouplingFieldDouble(ON_NODES)
+ f.setMesh(m)
+ arr=DataArrayDouble(nbNodes) ; arr.iota() ; arr*=i
+ f.setArray(arr)
+ f.setName(fieldName)
+ f.setTime(float(i),i,0)
+ fmts.appendFieldNoProfileSBT(f)
+ pass
+ #
+ mm=MEDFileUMesh() ; mm[0]=m
+ fmts.write(fileName,2)
+ mm.write(fileName,0)
+ #
+ fs=MEDFileFields(fileName,False)
+ fs2=MEDFileFields.LoadSpecificEntities(fileName,[(ON_NODES,NORM_ERROR)],False)
+ fs.loadArraysIfNecessary()
+ fs2.loadArraysIfNecessary()
+ for i in xrange(nbPdt):
+ self.assertTrue(fs[fieldName][i].getUndergroundDataArray().isEqual(fs2[fieldName][i].getUndergroundDataArray(),1e-12))
+ pass
+ m1=MEDCouplingCMesh() ; m1.setCoords(DataArrayDouble([0,1,2,3]),DataArrayDouble([0,1])) ; m1=m1.buildUnstructured() ; m1.simplexize(0)
+ m2=MEDCouplingCMesh() ; m2.setCoords(DataArrayDouble([3,4,5]),DataArrayDouble([0,1])) ; m2=m2.buildUnstructured()
+ m3=MEDCouplingUMesh.MergeUMeshes(m1,m2) ; m3.setName(meshName)
+ fmts=MEDFileFieldMultiTS()
+ for i in xrange(nbPdt):
+ f=MEDCouplingFieldDouble(ON_CELLS)
+ f.setMesh(m3)
+ arr=DataArrayDouble(8) ; arr.iota() ; arr*=i
+ f.setArray(arr)
+ f.setName(fieldName)
+ f.setTime(float(i),i,0)
+ fmts.appendFieldNoProfileSBT(f)
+ pass
+ mm=MEDFileUMesh() ; mm[0]=m3
+ del mm[0]
+ self.assertEqual(mm.getNonEmptyLevels(),())
+ mm[0]=m3
+ self.assertEqual(mm.getNonEmptyLevels(),(0,))
+ fmts.write(fileName,2)
+ fs=MEDFileFields(fileName,False)
+ fs2=MEDFileFields.LoadSpecificEntities(fileName,[(ON_CELLS,NORM_TRI3)],False)
+ fs3=MEDFileFieldMultiTS.LoadSpecificEntities(fileName,fieldName,[(ON_CELLS,NORM_QUAD4)],False)
+ fs4=MEDFileFields.LoadSpecificEntities(fileName,[(ON_CELLS,NORM_TRI3),(ON_CELLS,NORM_QUAD4)],False)
+ fs.loadArraysIfNecessary()
+ fs2.loadArraysIfNecessary()
+ fs3.loadArraysIfNecessary()
+ fs4.loadArraysIfNecessary()
+ for i in xrange(nbPdt):
+ self.assertTrue(fs[fieldName][i].getUndergroundDataArray()[:6].isEqual(fs2[fieldName][i].getUndergroundDataArray(),1e-12))
+ self.assertTrue(fs[fieldName][i].getUndergroundDataArray()[6:8].isEqual(fs3[i].getUndergroundDataArray(),1e-12))
+ self.assertTrue(fs[fieldName][i].getUndergroundDataArray().isEqual(fs4[fieldName][i].getUndergroundDataArray(),1e-12))
+ pass
+ pass
+
+ def testMEDFileLotsOfTSRW1(self):
+ nbNodes=11
+ fieldName="myField"
+ fileName="Pyfile88.med"
+ nbPdt=300 # <- perftest = 30000
+ meshName="Mesh"
+ #
+ maxPdt=100 # <- optimum = 500
+ m=MEDCouplingCMesh()
+ arr=DataArrayDouble(nbNodes) ; arr.iota()
+ m.setCoords(arr)
+ m=m.buildUnstructured()
+ m.setName(meshName)
+ #
+ nbOfField=nbPdt/maxPdt
+ fs=MEDFileFields()
+ for j in xrange(nbOfField):
+ fmts=MEDFileFieldMultiTS()
+ s=DataArray.GetSlice(slice(0,nbPdt,1),j,nbOfField)
+ for i in xrange(s.start,s.stop,s.step):
+ f=MEDCouplingFieldDouble(ON_NODES)
+ f.setMesh(m)
+ arr=DataArrayDouble(nbNodes) ; arr.iota() ; arr*=i
+ f.setArray(arr)
+ f.setName("%s_%d"%(fieldName,j))
+ f.setTime(float(i),i,0)
+ fmts.appendFieldNoProfileSBT(f)
+ pass
+ fs.pushField(fmts)
+ pass
+ #
+ mm=MEDFileUMesh() ; mm[0]=m
+ fs.write(fileName,2)
+ mm.write(fileName,0)
+ ############
+ def appendInDict(d,key,val):
+ if key in d:
+ d[key].append(val)
+ else:
+ d[key]=[val]
+ pass
+ import re
+ allFields=MEDLoader.GetAllFieldNames(fileName)
+ allFieldsDict={}
+ pat=re.compile("([\d]+)([\s\S]+)$")
+ for st in allFields:
+ stRev=st[::-1]
+ m=pat.match(stRev)
+ if m:
+ appendInDict(allFieldsDict,m.group(2)[::-1],m.group(1)[::-1])
+ pass
+ else:
+ appendInDict(allFieldsDict,st,'')
+ pass
+ pass
+ fs2=MEDFileFields()
+ for k in allFieldsDict:
+ if allFieldsDict[k]!=['']:
+ allFieldsDict[k]=sorted(allFieldsDict[k],key=lambda x: int(x))
+ pass
+ fmts2=[]
+ for it in allFieldsDict[k]:
+ fmts2.append(MEDFileFieldMultiTS.LoadSpecificEntities(fileName,k+it,[(ON_NODES,NORM_ERROR)]))
+ pass
+ fmts2.reverse()
+ zeResu=fmts2.pop()
+ nbIter=len(fmts2)
+ for ii in xrange(nbIter):
+ zeResu.pushBackTimeSteps(fmts2.pop())
+ pass
+ zeResu.setName(k)
+ fs2.pushField(zeResu)
+ pass
+ self.assertEqual(fs2[0].getTimeSteps(),[(i,0,float(i)) for i in xrange(nbPdt)])
+ pass
+
+ def testMEDFileMeshRearrangeFamIds1(self):
+ """ Test for bug EDF10720. The aim of this test is the call of MEDFileMesh.rearrangeFamilies."""
+ fileName="Pyfile89.med"
+ meshName='Maillage_2'
+ mm=MEDFileUMesh()
+ coords=DataArrayDouble([(0.,0.,0.),(0.,0.,200.),(0.,200.,200.),(0.,200.,0.),(200.,0.,0.),(200.,0.,200.),(200.,200.,200.),(200.,200.,0.),(0.,0.,100.),(0.,100.,200.),(0.,200.,100.),(0.,100.,0.),(200.,0.,100.),(200.,100.,200.),(200.,200.,100.),(200.,100.,0.),(100.,0.,0.),(100.,0.,200.),(100.,200.,0.),(100.,200.,200.),(0.,116.87743909766768,83.12256090233232),(200.,116.87743909766768,83.12256090233232),(116.87743909766769,0.,116.87743909766769),(116.87743909766769,200.,116.87743909766769),(116.87743909766769,116.87743909766769,0.),(116.87743909766769,116.87743909766769,200.),(63.3851584383713,56.1391811199829,119.728314479261),(138.008709441123,116.039297556044,119.903790959468)])
+ #
+ c0=DataArrayInt([14,1,26,9,8,14,17,26,1,8,14,27,26,17,22,14,26,16,20,8,14,8,0,16,11,14,16,20,11,24,14,25,20,26,27,14,22,26,24,27,14,26,16,22,24,14,8,26,22,17,14,20,9,25,26,14,19,20,25,23,14,23,6,27,25,14,19,23,10,20,14,27,22,21,24,14,27,21,14,18,14,26,9,25,17,14,13,27,25,17,14,27,18,24,21,14,22,21,15,12,14,27,20,24,18,14,23,25,27,20,14,13,27,6,25,14,23,27,6,14,14,15,16,22,12,14,27,17,13,22,14,22,27,21,13,14,24,16,22,15,14,24,18,7,21,14,12,4,15,16,14,22,12,5,13,14,8,26,16,22,14,13,27,21,14,14,20,18,10,3,14,14,27,18,23,14,14,27,6,13,14,21,22,13,12,14,25,26,17,27,14,19,9,25,20,14,26,24,20,16,14,22,24,15,21,14,9,26,1,17,14,23,27,18,20,14,20,11,18,3,14,14,18,21,7,14,19,2,9,10,14,19,23,25,6,14,18,23,20,10,14,20,26,8,9,14,22,13,5,17,14,24,11,18,20,14,21,15,7,24,14,19,20,10,9,14,20,26,27,24,14,16,8,11,20])
+ c0i=DataArrayInt([0,5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100,105,110,115,120,125,130,135,140,145,150,155,160,165,170,175,180,185,190,195,200,205,210,215,220,225,230,235,240,245,250,255,260,265,270,275])
+ m0=MEDCouplingUMesh(meshName,3) ; m0.setCoords(coords)
+ m0.setConnectivity(c0,c0i)
+ mm[0]=m0
+ #
+ c1=DataArrayInt([3,8,20,11,3,8,9,20,3,9,2,10,3,20,9,10,3,0,8,11,3,9,8,1,3,20,10,3,3,11,20,3,3,15,21,12,3,5,12,13,3,21,13,12,3,15,12,4,3,14,6,13,3,14,13,21,3,7,14,21,3,7,21,15,3,5,22,12,3,4,12,16,3,17,1,8,3,16,8,0,3,5,17,22,3,12,22,16,3,22,17,8,3,16,22,8,3,10,2,19,3,7,18,14,3,14,23,6,3,3,10,18,3,23,19,6,3,18,23,14,3,10,19,23,3,10,23,18,3,3,18,11,3,7,24,18,3,15,4,16,3,11,16,0,3,7,15,24,3,18,24,11,3,24,15,16,3,11,24,16,3,9,19,2,3,19,25,6,3,17,5,13,3,1,17,9,3,25,13,6,3,9,25,19,3,17,13,25,3,17,25,9])
+ c1i=DataArrayInt([0,4,8,12,16,20,24,28,32,36,40,44,48,52,56,60,64,68,72,76,80,84,88,92,96,100,104,108,112,116,120,124,128,132,136,140,144,148,152,156,160,164,168,172,176,180,184,188,192])
+ m1=MEDCouplingUMesh(meshName,2) ; m1.setCoords(coords)
+ m1.setConnectivity(c1,c1i)
+ mm[-1]=m1
+ #
+ c2=DataArrayInt([0,8,8,1,1,9,9,2,3,10,10,2,0,11,11,3,4,12,12,5,5,13,13,6,7,14,14,6,4,15,15,7,0,16,16,4,1,17,17,5,3,18,18,7,2,19,19,6])
+ m2=MEDCoupling1SGTUMesh(meshName,NORM_SEG2)
+ m2.setNodalConnectivity(c2) ; m2.setCoords(coords)
+ mm[-2]=m2.buildUnstructured()
+ #
+ ref0=DataArrayInt(55) ; ref0[:]=0
+ mm.setFamilyFieldArr(0,ref0)
+ mm.setFamilyFieldArr(1,DataArrayInt([0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]))
+ ref1=DataArrayInt([0,0,0,0,0,0,0,0,-6,-6,-6,-6,-6,-6,-6,-6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0])
+ mm.setFamilyFieldArr(-1,ref1)
+ ref2=DataArrayInt([0,0,-7,-7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0])
+ mm.setFamilyFieldArr(-2,ref2)
+ #
+ for f,fid in (('FAMILLE_ZERO',0),('FAM_-6_Groupe_1',-6),('FAM_-7_Groupe_2',-7),('FAM_2_Groupe_3',2)):
+ mm.setFamilyId(f,fid)
+ for grp,fams in [('Groupe_1',('FAM_-6_Groupe_1',)),('Groupe_2',('FAM_-7_Groupe_2',)),('Groupe_3',('FAM_2_Groupe_3',))]:
+ mm.setFamiliesOnGroup(grp,fams)
+ mm.write(fileName,2)
+ #
+ mm=MEDFileMesh.New(fileName)
+ grp=mm.getGroup(-1,"Groupe_1")
+ dai=grp.computeFetchedNodeIds()
+ dai.setName("TOTO")
+ mm.addGroup(1,dai)
+ mm.rearrangeFamilies() # <- the aim of the test
+ self.assertTrue(dai.isEqual(mm.getGroupArr(1,"TOTO")))
+ self.assertTrue(mm.getFamilyFieldAtLevel(0).isEqual(ref0))
+ self.assertTrue(mm.getFamilyFieldAtLevel(-1).isEqual(ref1))
+ self.assertTrue(mm.getFamilyFieldAtLevel(-2).isEqual(ref2))
+ self.assertTrue(mm.getFamilyFieldAtLevel(1).isEqual(DataArrayInt([0,0,2,0,9,9,9,9,0,0,0,0,9,9,9,9,0,0,0,0,0,9,0,0,0,0,0,0])))
+ allGrps=[('Groupe_1',('FAM_-6_Groupe_1',)),('Groupe_2',('FAM_-7_Groupe_2',)),('Groupe_3',('FAM_2_Groupe_3',)),('TOTO',('Family_9',))]
+ allFams=[('FAMILLE_ZERO',0),('FAM_-6_Groupe_1',-6),('FAM_-7_Groupe_2',-7),('FAM_2_Groupe_3',2),('Family_9',9)]
+ self.assertEqual(list(mm.getGroupsNames()),[elt[0] for elt in allGrps])
+ for elt,fams in allGrps:
+ self.assertEqual(mm.getFamiliesOnGroup(elt),fams)
+ self.assertEqual(list(mm.getFamiliesNames()),[elt[0] for elt in allFams])
+ for elt,eltId in allFams:
+ self.assertEqual(mm.getFamilyId(elt),eltId)
+ pass
+
+ def testNonRegrCMeshSetFieldPfl1(self):
+ """ Non regression test. For structured mesh, push a false partial field in MEDFileField1TS using setFieldProfile."""
+ ff=MEDFileField1TS()
+ meshName="mesh"
+ mm=MEDFileCMesh()
+ m=MEDCouplingCMesh() ; arr=DataArrayDouble(5) ; arr.iota()
+ m.setCoords(arr)
+ m.setName(meshName)
+ mm.setMesh(m)
+ field=MEDCouplingFieldDouble(ON_CELLS)
+ field.setMesh(m)
+ field.setArray(DataArrayDouble([1.2,2.3,3.4,4.5]))
+ field.setName("Field")
+ field.checkCoherency()
+ pfl=DataArrayInt([0,1,2,3]) ; pfl.setName("TUTU") #<- false profile because defined on all cells !
+ ff.setFieldProfile(field,mm,0,pfl) # <- bug was revealed here !
+ self.assertEqual(ff.getPfls(),())
+ field2=ff.getFieldOnMeshAtLevel(ON_CELLS,0,mm)
+ self.assertTrue(field.isEqual(field2,1e-12,1e-12))
+ del ff,mm,field,field2,pfl
+ # same with unstructured mesh
+ ff=MEDFileField1TS()
+ meshName="mesh"
+ mm=MEDFileUMesh()
+ m=MEDCouplingCMesh() ; arr=DataArrayDouble(5) ; arr.iota()
+ m.setCoords(arr)
+ m.setName(meshName)
+ m=m.buildUnstructured()
+ mm[0]=m
+ field=MEDCouplingFieldDouble(ON_CELLS)
+ field.setMesh(m)
+ field.setArray(DataArrayDouble([1.2,2.3,3.4,4.5]))
+ field.setName("Field")
+ field.checkCoherency()
+ pfl=DataArrayInt([0,1,2,3]) ; pfl.setName("TUTU")
+ ff.setFieldProfile(field,mm,0,pfl)
+ self.assertEqual(ff.getPfls(),())
+ field2=ff.getFieldOnMeshAtLevel(ON_CELLS,0,mm)
+ self.assertTrue(field.isEqual(field2,1e-12,1e-12))
+ pass
+
+ def testMEDFileUMeshLinearToQuadraticAndRev1(self):
+ meshName="mesh"
+ fileName="Pyfile90.med"
+ fileName2="Pyfile91.med"
+ arr=DataArrayDouble(5) ; arr.iota()
+ m=MEDCouplingCMesh() ; m.setCoords(arr,arr)
+ m=m.buildUnstructured()
+ d=DataArrayInt([3,7,11,15])
+ m1=m[d]
+ m1.simplexize(0)
+ m2=m[d.buildComplement(m.getNumberOfCells())]
+ m=MEDCouplingUMesh.MergeUMeshesOnSameCoords(m1,m2)
+ m.changeSpaceDimension(3,0.)
+ arr=DataArrayDouble(3) ; arr.iota()
+ m1D=MEDCouplingCMesh() ; m1D.setCoords(arr) ; m1D=m1D.buildUnstructured() ; m1D.changeSpaceDimension(3,0.)
+ m1D.setCoords(m1D.getCoords()[:,[1,2,0]])
+ delta=m.getNumberOfNodes()*(m1D.getNumberOfNodes()-1)
+ m3D=m.buildExtrudedMesh(m1D,0)
+ m3D.sortCellsInMEDFileFrmt()
+ m3D.setName(meshName)
+ m2D=m ; m2D.setCoords(m3D.getCoords()) ; m2D.shiftNodeNumbersInConn(delta) ; m2D.setName(meshName) ; m2D.checkCoherency2()
+ m1D=m2D.computeSkin() ; m1D.setName(meshName)
+ #
+ mm=MEDFileUMesh()
+ mm[0]=m3D ; mm[-1]=m2D ; mm[-2]=m1D
+ grpEdge0=DataArrayInt([1,2,3,5]) ; grpEdge0.setName("East")
+ grpEdge1=DataArrayInt([0,1]) ; grpEdge1.setName("Corner1")
+ grpFaceSouth=DataArrayInt([0,1,8,9,10]) ; grpFaceSouth.setName("SouthFace")
+ grpFaceNorth=DataArrayInt([6,7,17,18,19]) ; grpFaceNorth.setName("NorthFace")
+ diagFace=DataArrayInt([0,1,13,15,17]) ; diagFace.setName("DiagFace")
+ vol1=DataArrayInt([20,21,23,24]) ; vol1.setName("vol1")
+ vol2=DataArrayInt([2,3,4,5,21,24]) ; vol2.setName("vol2")
+ mm.setGroupsAtLevel(0,[vol1,vol2])
+ mm.setGroupsAtLevel(-1,[grpFaceSouth,grpFaceNorth,diagFace])
+ mm.setGroupsAtLevel(-2,[grpEdge0,grpEdge1])
+ #
+ mmOut1=mm.linearToQuadratic(0,0.)
+ mmOut1.write(fileName2,2)
+ mmOut2=mmOut1.quadraticToLinear(0.)
+ self.assertTrue(mm.isEqual(mmOut2,1e-12)[0])
+ pass
+
+ def testMEDFileMeshAddGroup1(self):
+ m=MEDCouplingCMesh()
+ arrX=DataArrayDouble(9) ; arrX.iota()
+ arrY=DataArrayDouble(4) ; arrY.iota()
+ m.setCoords(arrX,arrY)
+ m.setName("mesh")
+ mm=MEDFileCMesh()
+ mm.setMesh(m)
+ grp0=DataArrayInt([3,5,6,21,22]) ; grp0.setName("grp0")
+ mm.addGroup(0,grp0)
+ grp1=DataArrayInt([3,4,5,8,18,19,22]) ; grp1.setName("grp1")
+ mm.addGroup(0,grp1)
+ grp2=DataArrayInt([0,1,2,10,11]) ; grp2.setName("grp2")
+ mm.addGroup(0,grp2)
+ grp3=DataArrayInt([23]) ; grp3.setName("grp3")
+ mm.addGroup(0,grp3)
+ for grp in [grp0,grp1,grp2,grp3]:
+ self.assertTrue(mm.getGroupArr(0,grp.getName()).isEqual(grp))
+ self.assertEqual(mm.getGroupsNames(),('grp0','grp1','grp2','grp3'))
+ delta=12
+ for grp in [grp0,grp1,grp2,grp3]:
+ grpNode=grp.deepCpy() ; grpNode+=delta ; grpNode.setName("%s_node"%grp.getName())
+ mm.addGroup(1,grpNode)
+ self.assertEqual(mm.getGroupsNames(),('grp0','grp0_node','grp1','grp1_node','grp2','grp2_node','grp3','grp3_node'))
+ for grp in [grp0,grp1,grp2,grp3]:
+ self.assertTrue(mm.getGroupArr(0,grp.getName()).isEqual(grp))
+ for grp in [grp0,grp1,grp2,grp3]:
+ grpExp=grp+delta ; grpExp.setName("%s_node"%grp.getName())
+ self.assertTrue(mm.getGroupArr(1,"%s_node"%grp.getName()).isEqual(grpExp))
+ mm.normalizeFamIdsMEDFile()
+ for grp in [grp0,grp1,grp2,grp3]:
+ self.assertTrue(mm.getGroupArr(0,grp.getName()).isEqual(grp))
+ for grp in [grp0,grp1,grp2,grp3]:
+ grpExp=grp+delta ; grpExp.setName("%s_node"%grp.getName())
+ self.assertTrue(mm.getGroupArr(1,"%s_node"%grp.getName()).isEqual(grpExp))
+ pass
+