]> SALOME platform Git repositories - tools/medcoupling.git/commitdiff
Salome HOME
New SauvWriter option for copy group lying on a single family
authorageay <ageay>
Tue, 3 Dec 2013 09:23:25 +0000 (09:23 +0000)
committerageay <ageay>
Tue, 3 Dec 2013 09:23:25 +0000 (09:23 +0000)
src/MEDLoader/SauvWriter.cxx
src/MEDLoader/SauvWriter.hxx
src/MEDLoader/Swig/MEDLoaderCommon.i
src/MEDLoader/Swig/MEDLoaderDataForTest.py
src/MEDLoader/Swig/MEDLoaderTest.py
src/MEDLoader/Swig/SauvLoaderTest.py

index 697847bb6ac05c182b0ec5e941e455053ee61676..6478c2bbe9774dddb490819c64854b29e482a3e2 100644 (file)
@@ -212,11 +212,9 @@ namespace
   }
 }
 
-//================================================================================
-/*!
- * \brief Creates SauvWriter
- */
-//================================================================================
+SauvWriter::SauvWriter():_cpy_grp_if_on_single_family(false)
+{
+}
 
 SauvWriter* SauvWriter::New()
 {
@@ -233,6 +231,16 @@ std::vector<const BigMemoryObject *> SauvWriter::getDirectChildren() const
   return std::vector<const BigMemoryObject *>();
 }
 
+void SauvWriter::setCpyGrpIfOnASingleFamilyStatus(bool status)
+{
+  _cpy_grp_if_on_single_family=status;
+}
+
+bool SauvWriter::getCpyGrpIfOnASingleFamilyStatus() const
+{
+  return _cpy_grp_if_on_single_family;
+}
+
 //================================================================================
 /*!
  * \brief Fills own DS by MEDFileData
@@ -447,7 +455,23 @@ void SauvWriter::fillGroupSubMeshes()
       if (k != famNames.size())
           famSubMeshes.resize(k);
       SubMesh* grpSubMesh = addSubMesh( groupName, famSubMeshes[0]->_dimRelExt );
-      grpSubMesh->_subs.swap( famSubMeshes );
+      if(!_cpy_grp_if_on_single_family)
+        grpSubMesh->_subs.swap( famSubMeshes );
+      else
+        {
+          /* If a group sub mesh consists of only one family, the group is written as 
+           * a copy of this family. 
+           * A mesh composed of only one submesh may cause an issue with some Gibi operators.*/
+          if (famSubMeshes.size() == 1)
+            {
+              for(int i = 0; i < famSubMeshes[0]->cellIDsByTypeSize() ; i++)
+                {
+                  grpSubMesh->_cellIDsByType[i] = famSubMeshes[0]->_cellIDsByType[i];
+                }
+            }
+          else
+            grpSubMesh->_subs.swap( famSubMeshes );
+        }
     }
 }
 
index d6e732c1c17b2402b15008926933fa16a1948816..bbbdce98d277bb849f18239abfb5c08dc672bf18 100644 (file)
@@ -46,11 +46,13 @@ namespace ParaMEDMEM
   class SauvWriter : public ParaMEDMEM::RefCountObject
   {
   public:
-    MEDLOADER_EXPORT static SauvWriter * New();
+    MEDLOADER_EXPORT static SauvWriter *New();
     MEDLOADER_EXPORT void setMEDFileDS(const MEDFileData* medData, unsigned meshIndex = 0);
     MEDLOADER_EXPORT void write(const char* fileName);
-
+    MEDLOADER_EXPORT void setCpyGrpIfOnASingleFamilyStatus(bool status);
+    MEDLOADER_EXPORT bool getCpyGrpIfOnASingleFamilyStatus() const;
   private:
+    SauvWriter();
     std::size_t getHeapMemorySizeWithoutChildren() const;
     std::vector<const BigMemoryObject *> getDirectChildren() const;
     /*!
@@ -109,6 +111,7 @@ namespace ParaMEDMEM
     std::vector<SauvUtilities::nameGIBItoMED> _longNames[ LN_NB ];
 
     std::fstream*                             _sauvFile;
+    bool _cpy_grp_if_on_single_family;
   };
 }
 
index 750847a5562a441ae1d7b9a0cc14394bcbc9843d..cfac2c6e0dc95d257488effa12c3397daf3398b6 100644 (file)
@@ -2822,6 +2822,8 @@ namespace ParaMEDMEM
     static SauvWriter * New();
     void setMEDFileDS(const MEDFileData* medData, unsigned meshIndex = 0) throw(INTERP_KERNEL::Exception);
     void write(const char* fileName) throw(INTERP_KERNEL::Exception);
+    void setCpyGrpIfOnASingleFamilyStatus(bool status) throw(INTERP_KERNEL::Exception);
+    bool getCpyGrpIfOnASingleFamilyStatus() const throw(INTERP_KERNEL::Exception);
   };
   
   ///////////////
index 22e8cfc28b27994d7c0e7ca897a78ae02e6a4a8c..8a0aee21c8f4c31238d96967e01e6d3152e06309 100644 (file)
@@ -657,6 +657,55 @@ class MEDLoaderDataForTest:
         #
         data=MEDFileData() ; data.setMeshes(ms) ; data.setFields(fs)
         return data
+
+    def buildAMEDFileDataWithGroupOnOneFamilyForSauv(self):
+        # Coordinates
+        coords = [0.,0., 0.,1., 1.,1., 1.,0.]
+        # lvl 0 connectivity
+        conn2D   = [1,2,3,4]
+        # lvl -1 connectivity
+        conn1D   = [0,1, 1,2, 2,3, 4,1]
+        # lvl 0 mesh
+        mesh2D=MEDCouplingUMesh.New()
+        mesh2D.setMeshDimension(2)
+        mesh2D.allocateCells(1)
+        mesh2D.insertNextCell(NORM_QUAD4,4,conn2D)
+        mesh2D.finishInsertingCells()
+        # lvl -1 mesh
+        mesh1D=MEDCouplingUMesh.New()
+        mesh1D.setMeshDimension(1)
+        mesh1D.allocateCells(4)
+        mesh1D.insertNextCell(NORM_SEG2,2,conn1D[0:2])
+        mesh1D.insertNextCell(NORM_SEG2,2,conn1D[2:4])
+        mesh1D.insertNextCell(NORM_SEG2,2,conn1D[4:6])
+        mesh1D.insertNextCell(NORM_SEG2,2,conn1D[6:8])
+        mesh1D.finishInsertingCells()
+        # assigning coordinates
+        meshCoords=DataArrayDouble.New()
+        meshCoords.setValues(coords, 4, 2)
+        mesh2D.setCoords(meshCoords)
+        mesh1D.setCoords(meshCoords)
+        # Creating a multi level mesh
+        mm = MEDFileUMesh.New()
+        mm.setMeshAtLevel(0, mesh2D)
+        mm.setMeshAtLevel(-1, mesh1D)
+        mm.setName("carre")
+        # Creating groups
+        # Creating a group with an element on level -1
+        grp0_LM1 = DataArrayInt.New([0])
+        grp0_LM1.setName("grp0_LM1")
+        # Creating a group with all elements on level -1
+        grp1_LM1 = DataArrayInt.New([0,1,2,3])
+        grp1_LM1.setName("grp1_LM1")
+        #
+        mm.setGroupsAtLevel(-1,[grp0_LM1,grp1_LM1])
+        #
+        ms=MEDFileMeshes.New()
+        ms.setMeshAtPos(0,mm)
+        mfd=MEDFileData.New()
+        mfd.setMeshes(ms)
+        #
+        return mfd
     
     build1DMesh_1=classmethod(build1DMesh_1)
     build2DCurveMesh_1=classmethod(build2DCurveMesh_1)
@@ -675,4 +724,5 @@ class MEDLoaderDataForTest:
     buildVecFieldOnGauss_2_Simpler=classmethod(buildVecFieldOnGauss_2_Simpler)
     buildVecFieldOnGaussNE_1=classmethod(buildVecFieldOnGaussNE_1)
     buildACompleteMEDDataStructureWithFieldsOnCells_1=classmethod(buildACompleteMEDDataStructureWithFieldsOnCells_1)
+    buildAMEDFileDataWithGroupOnOneFamilyForSauv=classmethod(buildAMEDFileDataWithGroupOnOneFamilyForSauv)
     pass
index 3440231357fa0f289a016c8d4b9f443ee1ffea89..f04d53d953a55c7e4108d5bed1e057f428fd65de 100644 (file)
@@ -658,6 +658,15 @@ class MEDLoaderTest(unittest.TestCase):
         self.assertTrue(m0.isEqual(mRead,1e-12))
         pass
 
+    def testChangeGroupName(self):
+        """ This test is a non regression test on MEDFileUMesh.changeGroupName thanks to Alliance.
+        """
+        mfd=MEDLoaderDataForTest.buildAMEDFileDataWithGroupOnOneFamilyForSauv()
+        mesh = mfd.getMeshes().getMeshAtPos(0)
+        mesh.changeGroupName("grp0_LM1", "xonall1")
+        self.assertTrue("xonall1" in mesh.getGroupsNames())
+        pass
+
     pass
 
 unittest.main()
index 0da6104cdc1b71ccc92654f375fd5aa196709db4..8cd667ffd10372a7f4649efc0887711b1334eff2 100644 (file)
@@ -321,9 +321,70 @@ class SauvLoaderTest(unittest.TestCase):
         self.assertTrue( f2.isEqual( f, 1e-12, 1e-12 ))
         del sr
         os.remove( sauvFile )
+        pass
 
+    def testSauvWriterGroupWithOneFamily(self):
+        """
+        This test checks an option for sauv writing. It is requested here to copy a group from a family if a group is lying on a single family.
+        """
+        import re
+        mfd=MEDLoaderDataForTest.buildAMEDFileDataWithGroupOnOneFamilyForSauv()
+        sauvFile = "mesh.sauv"
+        sw=SauvWriter.New()
+        sw.setMEDFileDS(mfd)
+        self.assertTrue(not sw.getCpyGrpIfOnASingleFamilyStatus())
+        sw.setCpyGrpIfOnASingleFamilyStatus(True)
+        self.assertTrue(sw.getCpyGrpIfOnASingleFamilyStatus())
+        sw.write(sauvFile)
+        
+        f = open(sauvFile)
+        # String pattern for the header of the sub meshes record ("PILE" number, number of named objects, number of objects)
+        pattern_pile= re.compile(r'\sPILE\sNUMERO\s+(?P<number>[0-9]+)NBRE\sOBJETS\sNOMMES\s+(?P<nbnamed>[0-9]+)NBRE\sOBJETS\s+(?P<nbobjects>[0-9]+)')
+        # String pattern for a sub mesh header (cell type, number of components and three numbers)
+        pattern_header=re.compile(r'\s+(?P<type>[0-9]+)\s+(?P<nbsubs>[0-9]+)\s+[0-9]+\s+[0-9]+\s+[0-9]+')
+        
+        nbobjects=0
+        line = f.readline()
+        while(line):
+            match_pile = pattern_pile.match(line)
+            if match_pile:
+                number=int(match_pile.group("number"))
+                if number == 1:
+                    nbnamed=int(match_pile.group("nbnamed"))
+                    nbobjects=int(match_pile.group("nbobjects"))
+                    break
+                pass
+            line=f.readline()
+            pass
+        
+        # Skipping the objects names
+        f.readline()
+        # Skipping the objects ids
+        f.readline()
 
+        # Looking for each sub-mesh header 
+        line = f.readline()
+        cur_object=0
+        while(line and cur_object < nbobjects):
+            match_header=pattern_header.match(line)
+            if match_header:
+                cell_type=int(match_header.group("type"))
+                nb_subs=int(match_header.group("nbsubs"))
+                # Looking for a compound object
+                if cell_type == 0:
+                    # Testing if there is only one component
+                    self.assertTrue(nb_subs > 1)
+                else:
+                    f.readline()
+                    f.readline()
+                    cur_object = cur_object + 1
+                    pass
+                pass
+            line=f.readline()
+            pass
+        os.remove(sauvFile)
         pass
+
     pass
 
 unittest.main()