From: ageay Date: Wed, 9 Jan 2013 09:14:41 +0000 (+0000) Subject: MEDFileUMesh::addGroup implementation X-Git-Tag: V6_main_FINAL~423 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=cd29c0fa4ff038a87d94ea38b1f1f58d7450b26f;p=tools%2Fmedcoupling.git MEDFileUMesh::addGroup implementation --- diff --git a/src/MEDLoader/MEDFileMesh.cxx b/src/MEDLoader/MEDFileMesh.cxx index e6f2d5318..a94699ce7 100644 --- a/src/MEDLoader/MEDFileMesh.cxx +++ b/src/MEDLoader/MEDFileMesh.cxx @@ -2356,32 +2356,57 @@ DataArrayInt *MEDFileUMesh::zipCoords() throw(INTERP_KERNEL::Exception) * This method is here only to add a group on node. * MEDFileUMesh::setGroupsAtLevel with 1 in the first parameter. * - * \param [in] ids node ids of and group name of the new group to add. The ids should be sorted and different each other (MED file norm). + * \param [in] ids node ids and group name of the new group to add. The ids should be sorted and different each other (MED file norm). */ void MEDFileUMesh::addNodeGroup(const DataArrayInt *ids) throw(INTERP_KERNEL::Exception) -{ +{ + const DataArrayDouble *coords=_coords; + if(!coords) + throw INTERP_KERNEL::Exception("MEDFileUMesh::addNodeGroup : no coords set !"); + int nbOfNodes=coords->getNumberOfTuples(); + if(!((DataArrayInt *)_fam_coords)) + { _fam_coords=DataArrayInt::New(); _fam_coords->alloc(nbOfNodes,1); _fam_coords->fillWithZero(); } + // + addGroupUnderground(ids,_fam_coords); +} + +void MEDFileUMesh::addGroup(int meshDimRelToMaxExt, const DataArrayInt *ids) throw(INTERP_KERNEL::Exception) +{ + std::vector levs=getNonEmptyLevelsExt(); + if(std::find(levs.begin(),levs.end(),meshDimRelToMaxExt)==levs.end()) + { + std::ostringstream oss; oss << "MEDFileUMesh::addGroup : level " << meshDimRelToMaxExt << " not available ! Should be in "; + std::copy(levs.begin(),levs.end(),std::ostream_iterator(oss," ")); oss << " !"; throw INTERP_KERNEL::Exception(oss.str().c_str()); + } + if(meshDimRelToMaxExt==1) + { addNodeGroup(ids); return ; } + MEDFileUMeshSplitL1 *lev=getMeshAtLevSafe(meshDimRelToMaxExt); + DataArrayInt *fam=lev->getOrCreateAndGetFamilyField(); + addGroupUnderground(ids,fam); +} + +/*! + * \param [in] ids ids and group name of the new group to add. The ids should be sorted and different each other (MED file norm). + * \parma [in,out] famArr family array on level of interest to be renumbered. The input pointer should be not NULL (no check of that will be performed) + */ +void MEDFileUMesh::addGroupUnderground(const DataArrayInt *ids, DataArrayInt *famArr) throw(INTERP_KERNEL::Exception) +{ if(!ids) - throw INTERP_KERNEL::Exception("MEDFileUMesh::addNodeGroup : NULL pointer in input !"); + throw INTERP_KERNEL::Exception("MEDFileUMesh::addGroup : NULL pointer in input !"); std::string grpName(ids->getName()); if(grpName.empty()) - throw INTERP_KERNEL::Exception("MEDFileUMesh::addNodeGroup : empty group name ! MED file format do not accept empty group name !"); + throw INTERP_KERNEL::Exception("MEDFileUMesh::addGroup : empty group name ! MED file format do not accept empty group name !"); ids->checkStrictlyMonotonic(true); + famArr->incrRef(); MEDCouplingAutoRefCountObjectPtr famArrTmp(famArr); std::vector grpsNames=getGroupsNames(); if(std::find(grpsNames.begin(),grpsNames.end(),grpName)!=grpsNames.end()) { - std::ostringstream oss; oss << "MEDFileUMesh::addNodeGroup : Group with name \"" << grpName << "\" already exists ! Destroy it before calling this method !"; + std::ostringstream oss; oss << "MEDFileUMesh::addGroup : Group with name \"" << grpName << "\" already exists ! Destroy it before calling this method !"; throw INTERP_KERNEL::Exception(oss.str().c_str()); } - const DataArrayDouble *coords=_coords; - if(!coords) - throw INTERP_KERNEL::Exception("MEDFileUMesh::addNodeGroup : no coords set !"); - int nbOfNodes=coords->getNumberOfTuples(); - if(!((DataArrayInt *)_fam_coords)) - { _fam_coords=DataArrayInt::New(); _fam_coords->alloc(nbOfNodes,1); _fam_coords->fillWithZero(); } std::list< MEDCouplingAutoRefCountObjectPtr > allFamIds=getAllNonNullFamilyIds(); - allFamIds.erase(std::find(allFamIds.begin(),allFamIds.end(),_fam_coords)); - // - MEDCouplingAutoRefCountObjectPtr famIds=_fam_coords->selectByTupleIdSafe(ids->begin(),ids->end()); + allFamIds.erase(std::find(allFamIds.begin(),allFamIds.end(),famArrTmp)); + MEDCouplingAutoRefCountObjectPtr famIds=famArr->selectByTupleIdSafe(ids->begin(),ids->end()); std::set diffFamIds=famIds->getDifferentValues(); std::vector familyIds; std::vector< MEDCouplingAutoRefCountObjectPtr > idsPerfamiliyIds; @@ -2394,7 +2419,7 @@ void MEDFileUMesh::addNodeGroup(const DataArrayInt *ids) throw(INTERP_KERNEL::Ex { MEDCouplingAutoRefCountObjectPtr ids2Tmp=famIds->getIdsEqual(*famId); MEDCouplingAutoRefCountObjectPtr ids2=ids->selectByTupleId(ids2Tmp->begin(),ids2Tmp->end()); - MEDCouplingAutoRefCountObjectPtr ids1=_fam_coords->getIdsEqual(*famId); + MEDCouplingAutoRefCountObjectPtr ids1=famArr->getIdsEqual(*famId); DataArrayInt *ret0=0,*ret1=0; ids1->splitInTwoPartsWith(ids2,ret0,ret1); MEDCouplingAutoRefCountObjectPtr ret00(ret0),ret11(ret1); @@ -2433,7 +2458,7 @@ void MEDFileUMesh::addNodeGroup(const DataArrayInt *ids) throw(INTERP_KERNEL::Ex for(std::size_t i=0;isetPartOfValuesSimple3(familyIds[i],da->begin(),da->end(),0,1,1); + famArr->setPartOfValuesSimple3(familyIds[i],da->begin(),da->end(),0,1,1); } _families=families; _groups=groups; diff --git a/src/MEDLoader/MEDFileMesh.hxx b/src/MEDLoader/MEDFileMesh.hxx index e643b70a7..51363e3a5 100644 --- a/src/MEDLoader/MEDFileMesh.hxx +++ b/src/MEDLoader/MEDFileMesh.hxx @@ -213,6 +213,7 @@ namespace ParaMEDMEM void setFamilyFieldArr(int meshDimRelToMaxExt, DataArrayInt *famArr) throw(INTERP_KERNEL::Exception); void setRenumFieldArr(int meshDimRelToMaxExt, DataArrayInt *renumArr) throw(INTERP_KERNEL::Exception); void addNodeGroup(const DataArrayInt *ids) throw(INTERP_KERNEL::Exception); + void addGroup(int meshDimRelToMaxExt, const DataArrayInt *ids) throw(INTERP_KERNEL::Exception); void removeMeshAtLevel(int meshDimRelToMax) throw(INTERP_KERNEL::Exception); void setMeshAtLevel(int meshDimRelToMax, MEDCouplingUMesh *m, bool newOrOld=false) throw(INTERP_KERNEL::Exception); void setMeshAtLevelGen(int meshDimRelToMax, MEDCouplingUMesh *m, bool newOrOld) throw(INTERP_KERNEL::Exception); @@ -236,6 +237,7 @@ namespace ParaMEDMEM void synchronizeTinyInfoOnLeaves() const; void changeFamilyIdArr(int oldId, int newId) throw(INTERP_KERNEL::Exception); std::list< MEDCouplingAutoRefCountObjectPtr > getAllNonNullFamilyIds() const; + void addGroupUnderground(const DataArrayInt *ids, DataArrayInt *famArr) throw(INTERP_KERNEL::Exception); private: std::vector< MEDCouplingAutoRefCountObjectPtr > _ms; MEDCouplingAutoRefCountObjectPtr _coords; diff --git a/src/MEDLoader/MEDFileMeshLL.cxx b/src/MEDLoader/MEDFileMeshLL.cxx index 79b166b90..48e6ebbdd 100644 --- a/src/MEDLoader/MEDFileMeshLL.cxx +++ b/src/MEDLoader/MEDFileMeshLL.cxx @@ -635,6 +635,18 @@ MEDCouplingUMesh *MEDFileUMeshSplitL1::getWholeMesh(bool renum) const return tmp.retn(); } +DataArrayInt *MEDFileUMeshSplitL1::getOrCreateAndGetFamilyField() throw(INTERP_KERNEL::Exception) +{ + if((DataArrayInt *)_fam) + return _fam; + MEDCouplingUMesh *m(_m_by_types); + if(!m) + throw INTERP_KERNEL::Exception("MEDFileUMeshSplitL1::getOrCreateAndGetFamilyField : impossible to create a family field array because no mesh specified on this level !"); + int nbOfTuples=m->getNumberOfCells(); + _fam=DataArrayInt::New(); _fam->alloc(nbOfTuples,1); _fam->fillWithZero(); + return _fam; +} + const DataArrayInt *MEDFileUMeshSplitL1::getFamilyField() const { return _fam; diff --git a/src/MEDLoader/MEDFileMeshLL.hxx b/src/MEDLoader/MEDFileMeshLL.hxx index 10b4964c8..910469a25 100644 --- a/src/MEDLoader/MEDFileMeshLL.hxx +++ b/src/MEDLoader/MEDFileMeshLL.hxx @@ -133,6 +133,7 @@ namespace ParaMEDMEM MEDCouplingUMesh *getFamilyPart(const int *idsBg, const int *idsEnd, bool renum) const; DataArrayInt *getFamilyPartArr(const int *idsBg, const int *idsEnd, bool renum) const; MEDCouplingUMesh *getWholeMesh(bool renum) const; + DataArrayInt *getOrCreateAndGetFamilyField() throw(INTERP_KERNEL::Exception); const DataArrayInt *getFamilyField() const; const DataArrayInt *getNumberField() const; const DataArrayInt *getRevNumberField() const; diff --git a/src/MEDLoader/Swig/MEDLoaderCommon.i b/src/MEDLoader/Swig/MEDLoaderCommon.i index 08588c8c2..516422f0b 100644 --- a/src/MEDLoader/Swig/MEDLoaderCommon.i +++ b/src/MEDLoader/Swig/MEDLoaderCommon.i @@ -583,6 +583,7 @@ namespace ParaMEDMEM void setCoords(DataArrayDouble *coords) throw(INTERP_KERNEL::Exception); void eraseGroupsAtLevel(int meshDimRelToMaxExt) throw(INTERP_KERNEL::Exception); void addNodeGroup(const DataArrayInt *ids) throw(INTERP_KERNEL::Exception); + void addGroup(int meshDimRelToMaxExt, const DataArrayInt *ids) throw(INTERP_KERNEL::Exception); void removeMeshAtLevel(int meshDimRelToMax) throw(INTERP_KERNEL::Exception); void setMeshAtLevel(int meshDimRelToMax, MEDCouplingUMesh *m, bool newOrOld=false) throw(INTERP_KERNEL::Exception); void setMeshAtLevelGen(int meshDimRelToMax, MEDCouplingUMesh *m, bool newOrOld) throw(INTERP_KERNEL::Exception); diff --git a/src/MEDLoader/Swig/MEDLoaderTest3.py b/src/MEDLoader/Swig/MEDLoaderTest3.py index 4d9f2964c..75266516c 100644 --- a/src/MEDLoader/Swig/MEDLoaderTest3.py +++ b/src/MEDLoader/Swig/MEDLoaderTest3.py @@ -1858,6 +1858,77 @@ class MEDLoaderTest(unittest.TestCase): da.setValues([1]) self.assertTrue(mm.getGroupArr(-1,"grp0").isEqual(da)) pass + + def testMEDUMeshAddGroup1(self): + fname="Pyfile54.med" + m=MEDFileUMesh() + coo=DataArrayDouble(9) ; coo.iota(1.) ; coo.rearrange(3) ; coo.setInfoOnComponents(["aaa [b]","cc [dd]", "e [fff]"]) + m0=MEDCouplingUMesh("toto",2) ; m0.allocateCells(0) + for i in xrange(7): + m0.insertNextCell(NORM_TRI3,[1,2,1]) + pass + for i in xrange(4): + m0.insertNextCell(NORM_QUAD4,[1,1,2,0]) + pass + for i in xrange(2): + m0.insertNextCell(NORM_POLYGON,[0,0,1,1,2,2]) + pass + m1=MEDCouplingUMesh("toto",1) ; m1.allocateCells(0) ; m1.insertNextCell(NORM_SEG2,[1,6]) ; m1.insertNextCell(NORM_SEG2,[7,3]) + m2=MEDCouplingUMesh("toto",0) ; m2.allocateCells(0) ; m2.insertNextCell(NORM_POINT1,[2]) ; m2.insertNextCell(NORM_POINT1,[6]) ; m2.insertNextCell(NORM_POINT1,[8]) + m0.setCoords(coo) ; m.setMeshAtLevel(0,m0) + m1.setCoords(coo) ; m.setMeshAtLevel(-1,m1) + m2.setCoords(coo) ; m.setMeshAtLevel(-2,m2) + # + mm=m.deepCpy() + famCoo=DataArrayInt([0,2,0,3,2,0,-1,0,0,0,0,-1,3]) ; mm.setFamilyFieldArr(0,famCoo) + da0=DataArrayInt([0,0,0]) ; mm.setFamilyFieldArr(1,da0) + da1=DataArrayInt([0,3]) ; mm.setFamilyFieldArr(-1,da1) + da2=DataArrayInt([0,0,0]) ; mm.setFamilyFieldArr(-2,da2) + mm.setFamilyId("MyFam",2) + mm.setFamilyId("MyOtherFam",3) + mm.setFamilyId("MyOther-1",-1) + mm.setFamiliesOnGroup("grp0",["MyOtherFam"]) + mm.setFamiliesOnGroup("grpA",["MyOther-1"]) + # + daTest=DataArrayInt([1,3,4,6,9,10,12]) ; daTest.setName("grp1") + mm.addGroup(0,daTest) + self.assertTrue(mm.getGroupArr(0,daTest.getName()).isEqual(daTest)) + self.assertTrue(mm.getFamilyFieldAtLevel(0).isEqual(DataArrayInt([6,2,6,8,2,6,5,6,6,7,7,4,8]))) + for lev,arr in [(1,da0),(-1,da1),(-2,da2)]: + self.assertTrue(mm.getFamilyFieldAtLevel(lev).isEqual(arr)) + pass + self.assertEqual(mm.getFamiliesNames(),('Family_4','Family_5','Family_7','Family_8','MyFam','MyOther-1','MyOtherFam')) + self.assertEqual(mm.getGroupsNames(),('grp0','grp1','grpA')) + self.assertEqual(mm.getFamilyNameGivenId(3),'MyOtherFam') + self.assertEqual(mm.getFamilyNameGivenId(2),'MyFam') + for famName,famId in [('Family_4',4),('Family_5',5),('Family_7',7),('Family_8',8)]: + self.assertEqual(mm.getFamilyNameGivenId(famId),famName) + pass + self.assertEqual(mm.getFamiliesOnGroup("grp0"),('MyOtherFam','Family_8')) + da=DataArrayInt([3,12]) ; da.setName("grp0") + self.assertTrue(mm.getGroupArr(0,"grp0").isEqual(da)) + da.setValues([1]) + self.assertTrue(mm.getGroupArr(-1,"grp0").isEqual(da)) + mm.write(fname,2) + mm=MEDFileMesh.New(fname) + self.assertTrue(mm.getGroupArr(0,daTest.getName()).isEqual(daTest)) + self.assertTrue(mm.getFamilyFieldAtLevel(0).isEqual(DataArrayInt([6,2,6,8,2,6,5,6,6,7,7,4,8]))) + for lev,arr in [(1,da0),(-1,da1),(-2,da2)]: + self.assertTrue(mm.getFamilyFieldAtLevel(lev).isEqual(arr)) + pass + self.assertEqual(mm.getFamiliesNames(),('FAMILLE_ZERO','Family_4','Family_5','Family_7','Family_8','MyFam','MyOther-1','MyOtherFam')) + self.assertEqual(mm.getGroupsNames(),('grp0','grp1','grpA')) + self.assertEqual(mm.getFamilyNameGivenId(3),'MyOtherFam') + self.assertEqual(mm.getFamilyNameGivenId(2),'MyFam') + for famName,famId in [('Family_4',4),('Family_5',5),('Family_7',7),('Family_8',8)]: + self.assertEqual(mm.getFamilyNameGivenId(famId),famName) + pass + self.assertEqual(mm.getFamiliesOnGroup("grp0"),('Family_8','MyOtherFam')) + da=DataArrayInt([3,12]) ; da.setName("grp0") + self.assertTrue(mm.getGroupArr(0,"grp0").isEqual(da)) + da.setValues([1]) + self.assertTrue(mm.getGroupArr(-1,"grp0").isEqual(da)) + pass pass unittest.main()