Salome HOME
Introduced check functions in MEDFileUMesh:
authorabn <adrien.bruneton@cea.fr>
Wed, 3 Feb 2016 09:45:51 +0000 (10:45 +0100)
committerAnthony Geay <anthony.geay@edf.fr>
Fri, 19 Feb 2016 07:19:16 +0000 (08:19 +0100)
       + checkCoherency(),
       + checkSMESHCoherency()
       + and clearNodeAndCellNumbers()

Also added hasUniqueValues() in DataArrayInt

src/MEDCoupling/MEDCouplingMemArray.cxx
src/MEDCoupling/MEDCouplingMemArray.hxx
src/MEDCoupling_Swig/MEDCouplingBasicsTest2.py
src/MEDCoupling_Swig/MEDCouplingMemArray.i
src/MEDLoader/MEDFileMesh.cxx
src/MEDLoader/MEDFileMesh.hxx
src/MEDLoader/MEDFileMeshLL.cxx
src/MEDLoader/MEDFileMeshLL.hxx
src/MEDLoader/Swig/MEDLoaderCommon.i
src/MEDLoader/Swig/MEDLoaderTest3.py

index a53430517ce3dcda26b4b9a1ac3a7050e1de6f98..bca5865f2ae049782d2807bfb3e2c968d029e543 100644 (file)
@@ -7820,6 +7820,24 @@ bool DataArrayInt::isUniform(int val) const
   return true;
 }
 
+/*!
+ * Checks if all values in \a this array are unique.
+ *  \return bool - \a true if condition above is true
+ *  \throw If \a this is not allocated.
+ *  \throw If \a this->getNumberOfComponents() != 1
+ */
+bool DataArrayInt::hasUniqueValues() const
+{
+  checkAllocated();
+  if(getNumberOfComponents()!=1)
+    throw INTERP_KERNEL::Exception("DataArrayInt::hasOnlyUniqueValues: must be applied on DataArrayInt with only one component, you can call 'rearrange' method before !");
+  int nbOfTuples(getNumberOfTuples());
+  std::set<int> s(begin(),end());  // in C++11, should use unordered_set (O(1) complexity)
+  if (s.size() != nbOfTuples)
+    return false;
+  return true;
+}
+
 /*!
  * Creates a new DataArrayDouble and assigns all (textual and numerical) data of \a this
  * array to the new one.
index 51723e5f47cc53f43cf563a8ad882e6d856f2c51..432610845e82dc12e711c959e12b5c87e8e7934f 100644 (file)
@@ -522,6 +522,7 @@ namespace MEDCoupling
     MEDCOUPLING_EXPORT DataArrayInt *buildPermArrPerLevel() const;
     MEDCOUPLING_EXPORT bool isIota(int sizeExpected) const;
     MEDCOUPLING_EXPORT bool isUniform(int val) const;
+    MEDCOUPLING_EXPORT bool hasUniqueValues() const;
     MEDCOUPLING_EXPORT DataArrayInt *subArray(int tupleIdBg, int tupleIdEnd=-1) const;
     MEDCOUPLING_EXPORT void rearrange(int newNbOfCompo);
     MEDCOUPLING_EXPORT void transpose();
index fabd7936e15cc221def888d8886bf45918d8db11..7584085b51697aee0474f3b9b22fdf595202da2c 100644 (file)
@@ -1870,6 +1870,19 @@ class MEDCouplingBasicsTest2(unittest.TestCase):
         da2.setIJ(1,0,1.+1.e-11);
         self.assertTrue(not da2.isUniform(1.,1.e-12));
         pass
+
+    def testDAHasUniqueValues1(self):
+        da=DataArrayInt([1,2,3,4,5])
+        self.assertTrue(da.hasUniqueValues())
+        da[1,0] = 5
+        self.assertFalse(da.hasUniqueValues())
+        da=DataArrayInt([])
+        self.assertTrue(da.hasUniqueValues())
+        da=DataArrayInt([(1,2), (2,3)]) # wrong num of compo
+        self.assertRaises(InterpKernelException, da.hasUniqueValues)
+        da=DataArrayInt()  # non allocated array
+        self.assertRaises(InterpKernelException, da.hasUniqueValues)
+        pass
     
     def testDADFromPolarToCart1(self):
         tab1=[2.,0.2,2.5,0.7]
index 455b6bd3cc3ec09a08cff482ccbb547f13b33b71..1ab220f00da5b345231410798177552b513f9be7 100644 (file)
@@ -2622,6 +2622,7 @@ namespace MEDCoupling
     DataArrayInt *buildPermArrPerLevel() const throw(INTERP_KERNEL::Exception);
     bool isIota(int sizeExpected) const throw(INTERP_KERNEL::Exception);
     bool isUniform(int val) const throw(INTERP_KERNEL::Exception);
+    bool hasUniqueValues() const throw(INTERP_KERNEL::Exception);
     DataArrayInt *subArray(int tupleIdBg, int tupleIdEnd=-1) const throw(INTERP_KERNEL::Exception);
     void transpose() throw(INTERP_KERNEL::Exception);
     DataArrayInt *changeNbOfComponents(int newNbOfComp, int dftValue) const throw(INTERP_KERNEL::Exception);
index 23e75204031888ca2ac6870ea08797e93720b2f6..0c6c6e2878f09429b3377d14fa9f91e9ecde7338 100644 (file)
@@ -2488,6 +2488,97 @@ bool MEDFileUMesh::isEqual(const MEDFileMesh *other, double eps, std::string& wh
   return pd0->isEqual(pd1,what);
 }
 
+/*!
+ * Check that the current object MEDFileUMesh is consistent. This does not check the optional renumbering of
+ * nodes and cells. This last item is important for SMESH, see checkSMESHConsistency().
+ * \throw if any internal part (i.e. mesh sub-levels and single geometric-type meshes) are inconsistent
+ * \throw if internal family array is inconsistent
+ * \sa checkSMESHConsistency()
+ */
+void MEDFileUMesh::checkConsistency() const
+{
+  if(!_coords || !_coords->isAllocated())
+    {
+      if(!_ms.size())
+        throw INTERP_KERNEL::Exception("MEDFileUMesh::checkConsistency(): coords are null but some mesh parts are present!");
+      if (!_fam_coords)
+        throw INTERP_KERNEL::Exception("MEDFileUMesh::checkConsistency(): coords are null but not the internal node family array!");
+      if (!_num_coords || !_rev_num_coords)
+        throw INTERP_KERNEL::Exception("MEDFileUMesh::checkConsistency(): coords are null but not the internal node numbering array!");
+    }
+  else
+    {
+      int nbCoo = _coords->getNumberOfTuples();
+      if (_fam_coords)
+        _fam_coords->checkNbOfTuplesAndComp(nbCoo,1,"MEDFileUMesh::checkConsistency(): inconsistent internal node family array!");
+      if (_num_coords)
+        {
+          _num_coords->checkNbOfTuplesAndComp(nbCoo,1,"MEDFileUMesh::checkConsistency(): inconsistent internal node numbering array!");
+          int pos;
+          int maxValue=_num_coords->getMaxValue(pos);
+          if (!_rev_num_coords || _rev_num_coords->getNumberOfTuples() != (maxValue+1))
+            throw INTERP_KERNEL::Exception("MEDFileUMesh::checkConsistency(): inconsistent internal revert node numbering array!");
+        }
+      if ((_num_coords && !_rev_num_coords) || (!_num_coords && _rev_num_coords))
+        throw INTERP_KERNEL::Exception("MEDFileUMesh::checkConsistency(): inconsistent internal numbering arrays (one is null)!");
+      if (_num_coords && !_num_coords->hasUniqueValues())
+        throw INTERP_KERNEL::Exception("MEDFileUMesh::checkConsistency(): inconsistent internal node numbering array: duplicates found!");
+      if (_name_coords)
+        _name_coords->checkNbOfTuplesAndComp(nbCoo,MED_SNAME_SIZE,"MEDFileUMesh::checkConsistency(): inconsistent internal coord name array!");
+      // Now sub part check:
+      for (std::vector< MCAuto<MEDFileUMeshSplitL1> >::const_iterator it=_ms.begin();
+          it != _ms.end(); it++)
+        (*it)->checkConsistency();
+    }
+}
+
+/**
+ * Same as checkConsistency() but also checks that optional entities (edges, faces, volumes) numbers are
+ * consistent, i.e. the numbering is either set to null for all sub-levels (thus letting SMESH numbers the
+ * entities as it likes), or non overlapping between all sub-levels.
+ * \throw if the condition above is not respected
+ */
+void MEDFileUMesh::checkSMESHConsistency() const
+{
+  checkConsistency();
+  // For all sub-levels, numbering is either always null or with void intersection:
+  if (_ms.size())
+    {
+      std::vector< MCAuto<MEDFileUMeshSplitL1> >::const_iterator it=_ms.begin();
+      std::vector< const DataArrayInt * > v;
+      bool voidOrNot = ((*it)->_num == 0);
+      for (it++; it != _ms.end(); it++)
+        if( ((*it)->_num == 0) != voidOrNot )
+          throw INTERP_KERNEL::Exception("MEDFileUMesh::checkConsistency(): inconsistent numbering between mesh sub-levels!");
+        else if (!voidOrNot)
+          v.push_back((*it)->_num);
+      if (!voidOrNot)
+        {
+          // don't forget the 1st one:
+          v.push_back(_ms[0]->_num);
+          MCAuto<DataArrayInt> inter = DataArrayInt::BuildIntersection(v);
+          if (inter->getNumberOfTuples())
+            throw INTERP_KERNEL::Exception("MEDFileUMesh::checkConsistency(): overlapping entity numbering between mesh sub-levels!");
+        }
+    }
+}
+
+/**
+ * Reset optional node and cell numbering for all sub levels in this. This particularly useful to make
+ * sure SMESH will handle the mesh correctly, as it tries to use those numbers if given.
+ */
+void MEDFileUMesh::clearNodeAndCellNumbers()
+{
+  _num_coords = 0;
+  _rev_num_coords = 0;
+  for (std::vector< MCAuto<MEDFileUMeshSplitL1> >::iterator it=_ms.begin();
+      it != _ms.end(); it++)
+    {
+      (*it)->_num = 0;
+      (*it)->_rev_num = 0;
+    }
+}
+
 /*!
  * Clears redundant attributes of incorporated data arrays.
  */
@@ -3694,12 +3785,15 @@ void MEDFileUMesh::optimizeFamilies()
  *  other side of the group is no more a neighbor)
  *   - finally, the connectivity of (part of) the top level-cells bordering the group is also modified so that some cells
  *  bordering the newly created boundary use the newly computed nodes.
+ *  Finally note that optional cell numbers are also affected by this method and might become invalid for SMESH.
+ *  Use clearNodeAndCellNumbers() afterwards to ensure a proper SMESH loading.
  *
  *  \param[in] grpNameM1 name of the (-1)-level group defining the boundary
  *  \param[out] nodesDuplicated ids of the initial nodes which have been duplicated (and whose copy is put at the end of
  *  the coord array)
  *  \param[out] cellsModified ids of the cells whose connectivity has been modified (to use the newly created nodes)
  *  \param[out] cellsNotModified ids of the rest of cells bordering the new boundary whose connectivity remains unchanged.
+ *  \sa clearNodeAndCellNumbers()
  */
 void MEDFileUMesh::buildInnerBoundaryAlongM1Group(const std::string& grpNameM1, DataArrayInt *&nodesDuplicated,
                                            DataArrayInt *&cellsModified, DataArrayInt *&cellsNotModified)
@@ -3793,6 +3887,7 @@ void MEDFileUMesh::buildInnerBoundaryAlongM1Group(const std::string& grpNameM1,
       newFam->setPartOfValuesSimple1(0,nbNodes,newNbOfNodes,1,0,1,1);
       _fam_coords=newFam;
     }
+
   nodesDuplicated=nodeIdsToDuplicate.retn();
   cellsModified=cellsToModifyConn0.retn();
   cellsNotModified=cellsToModifyConn1.retn();
index f4676c218a23c8afe32a3b4aaf0d1efae2bee100..ef17b2e47aab23b3606303a8a387a103a922c5cc 100644 (file)
@@ -255,6 +255,9 @@ namespace MEDCoupling
     MEDLOADER_EXPORT MEDFileMesh *deepCopy() const;
     MEDLOADER_EXPORT MEDFileMesh *shallowCpy() const;
     MEDLOADER_EXPORT bool isEqual(const MEDFileMesh *other, double eps, std::string& what) const;
+    MEDLOADER_EXPORT void checkConsistency() const;
+    MEDLOADER_EXPORT void checkSMESHConsistency() const;
+    MEDLOADER_EXPORT void clearNodeAndCellNumbers();
     MEDLOADER_EXPORT void clearNonDiscrAttributes() const;
     MEDLOADER_EXPORT void setName(const std::string& name);
     //
index f46f9b0c77a5c573595a5323796d2da65e92d007..2f6e05dff5ee6213696a4a258568635a522591f7 100644 (file)
@@ -947,6 +947,29 @@ MEDFileUMeshSplitL1 *MEDFileUMeshSplitL1::deepCopy(DataArrayDouble *coords) cons
   return ret.retn();
 }
 
+void MEDFileUMeshSplitL1::checkConsistency() const
+{
+  if (!_fam || _fam->getNumberOfTuples() != getSize())
+    throw INTERP_KERNEL::Exception("MEDFileUMeshSplitL1::checkConsistency(): internal family array has an invalid size!");
+  int nbCells = getSize();
+  if (_num)
+    {
+      _num->checkNbOfTuplesAndComp(nbCells,1,"MEDFileUMeshSplitL1::checkConsistency(): inconsistent internal node numbering array!");
+      int pos;
+      int maxValue=_num->getMaxValue(pos);
+      if (!_rev_num || _rev_num->getNumberOfTuples() != (maxValue+1))
+        throw INTERP_KERNEL::Exception("MEDFileUMeshSplitL1::checkConsistency(): inconsistent internal revert node numbering array!");
+    }
+  if ((_num && !_rev_num) || (!_num && _rev_num))
+    throw INTERP_KERNEL::Exception("MEDFileUMeshSplitL1::checkConsistency(): inconsistent internal numbering arrays (one is null)!");
+  if (_num && !_num->hasUniqueValues())
+    throw INTERP_KERNEL::Exception("MEDFileUMeshSplitL1::checkConsistency(): inconsistent internal node numbering array: duplicates found!");
+  if (_names)
+    _names->checkNbOfTuplesAndComp(nbCells,1,"MEDFileUMeshSplitL1::checkConsistency(): internal cell naming array has an invalid size!");
+
+  _m_by_types.checkConsistency();
+}
+
 bool MEDFileUMeshSplitL1::isEqual(const MEDFileUMeshSplitL1 *other, double eps, std::string& what) const
 {
   if(!_m_by_types.isEqual(other->_m_by_types,eps,what))
@@ -1836,6 +1859,16 @@ bool MEDFileUMeshAggregateCompute::isEqual(const MEDFileUMeshAggregateCompute& o
   return true;
 }
 
+void MEDFileUMeshAggregateCompute::checkConsistency() const
+{
+  if(_mp_time >= _m_time)
+    for(std::vector< MCAuto<MEDCoupling1GTUMesh> >::const_iterator it=_m_parts.begin();
+        it!=_m_parts.end(); it++)
+      (*it)->checkConsistency();
+  else
+    _m->checkConsistency();
+}
+
 void MEDFileUMeshAggregateCompute::clearNonDiscrAttributes() const
 {
   for(std::vector< MCAuto<MEDCoupling1GTUMesh> >::const_iterator it=_m_parts.begin();it!=_m_parts.end();it++)
index e5e29cf68e785b97878f5f64e31f14d030503a73..565c44318c57206cdda16c8aebd707743625c950 100644 (file)
@@ -186,6 +186,7 @@ namespace MEDCoupling
     MEDFileUMeshAggregateCompute deepCopy(DataArrayDouble *coords) const;
     void shallowCpyMeshes();
     bool isEqual(const MEDFileUMeshAggregateCompute& other, double eps, std::string& what) const;
+    void checkConsistency() const;
     void clearNonDiscrAttributes() const;
     void synchronizeTinyInfo(const MEDFileMesh& master) const;
     bool empty() const;
@@ -211,6 +212,7 @@ namespace MEDCoupling
   class MEDFileUMeshSplitL1 : public RefCountObject
   {
     friend class MEDFileUMeshPermCompute;
+    friend class MEDFileUMesh;
   public:
     MEDFileUMeshSplitL1(const MEDFileUMeshSplitL1& other);
     MEDFileUMeshSplitL1(const MEDFileUMeshL2& l2, const std::string& mName, int id);
@@ -222,6 +224,7 @@ namespace MEDCoupling
     std::vector<const BigMemoryObject *> getDirectChildrenWithNull() const;
     MEDFileUMeshSplitL1 *shallowCpyUsingCoords(DataArrayDouble *coords) const;
     MEDFileUMeshSplitL1 *deepCopy(DataArrayDouble *coords) const;
+    void checkConsistency() const;
     void setCoords(DataArrayDouble *coords);
     bool isEqual(const MEDFileUMeshSplitL1 *other, double eps, std::string& what) const;
     void clearNonDiscrAttributes() const;
index bb4a5e6587d7ebb276f528cd5b786616fb187635..21dc6559c74f1faef0e8c5a450a4eee503f8bde4 100644 (file)
@@ -1205,6 +1205,9 @@ namespace MEDCoupling
     ~MEDFileUMesh();
     int getSpaceDimension() const throw(INTERP_KERNEL::Exception);
     int getRelativeLevOnGeoType(INTERP_KERNEL::NormalizedCellType gt) const throw(INTERP_KERNEL::Exception);
+    void checkConsistency() const throw(INTERP_KERNEL::Exception);
+    void checkSMESHConsistency() const throw(INTERP_KERNEL::Exception);
+    void clearNodeAndCellNumbers();
     //
     MEDCouplingUMesh *getGroup(int meshDimRelToMaxExt, const std::string& grp, bool renum=false) const throw(INTERP_KERNEL::Exception);
     MEDCouplingUMesh *getGroups(int meshDimRelToMaxExt, const std::vector<std::string>& grps, bool renum=false) const throw(INTERP_KERNEL::Exception);
index a058b967fe66867b54d4d0e9bc2010bc554624d3..0f93d488d328f65b94c13c2e2fd486586773c9e9 100644 (file)
@@ -4852,6 +4852,103 @@ class MEDLoaderTest3(unittest.TestCase):
       self.assertTrue(mm.getHiddenCppPointer()==mm2.getHiddenCppPointer()) # optimization
       pass
 
+    def testCheckCoherency(self):
+      m2 = MEDCouplingUMesh("2d", 2)
+      m2.setCoords(DataArrayDouble([(0.0, 1.0)] * 4, 4,2))  # whatever
+      m2.setConnectivity(DataArrayInt([NORM_TRI3, 0,1,2,NORM_TRI3, 1,2,3]), DataArrayInt(([0,4,8])))
+      m1 , _, _ , _, _ = m2.buildDescendingConnectivity()
+      mum = MEDFileUMesh()
+      mum.setMeshAtLevel(0, m2)
+      mum.setMeshAtLevel(-1, m1)
+      mum.checkConsistency()
+      mum2 = mum.deepCopy()
+
+      # Nodes
+      arr = DataArrayInt([2]*4)
+      mum.setFamilyFieldArr(1, arr); arr.reAlloc(35);
+      self.assertRaises(InterpKernelException, mum.checkConsistency)
+      mum=mum2; mum2=mum.deepCopy();
+      arr = DataArrayInt([2]*4)
+      mum.setRenumFieldArr(1, arr); arr.reAlloc(35);
+      self.assertRaises(InterpKernelException, mum.checkConsistency)
+      mum=mum2; mum2=mum.deepCopy();
+      mum.setRenumFieldArr(1, DataArrayInt([2]*4))
+      self.assertRaises(InterpKernelException, mum.checkConsistency)
+      mum=mum2; mum2=mum.deepCopy();
+      arr = DataArrayAsciiChar(['tutu           x']*4)
+      mum.setNameFieldAtLevel(1, arr); arr.reAlloc(35);
+      self.assertRaises(InterpKernelException, mum.checkConsistency)
+
+      # 2D
+      mum=mum2; mum2=mum.deepCopy();
+      arr = DataArrayInt([2]*2)
+      mum.setFamilyFieldArr(0, arr); arr.reAlloc(35);
+      self.assertRaises(InterpKernelException, mum.checkConsistency)
+      mum=mum2; mum2=mum.deepCopy();
+      arr = DataArrayInt([2]*2)
+      mum.setRenumFieldArr(0, arr); arr.reAlloc(35);
+      self.assertRaises(InterpKernelException, mum.checkConsistency)
+      mum=mum2; mum2=mum.deepCopy();
+      mum.setRenumFieldArr(0, DataArrayInt([2]*2))
+      self.assertRaises(InterpKernelException, mum.checkConsistency)
+      mum=mum2; mum2=mum.deepCopy();
+      arr = DataArrayAsciiChar(['tutu           x']*2)
+      mum.setNameFieldAtLevel(0, arr); arr.reAlloc(35);
+      self.assertRaises(InterpKernelException, mum.checkConsistency)
+
+      # 1D
+      mum=mum2; mum2=mum.deepCopy();
+      arr = DataArrayInt([2]*5)
+      mum.setFamilyFieldArr(-1, arr); arr.reAlloc(35);
+      self.assertRaises(InterpKernelException, mum.checkConsistency)
+      mum=mum2; mum2=mum.deepCopy();
+      arr = DataArrayInt([2]*5)
+      mum.setRenumFieldArr(-1, arr); arr.reAlloc(35);
+      self.assertRaises(InterpKernelException, mum.checkConsistency)
+      mum=mum2; mum2=mum.deepCopy();
+      mum.setRenumFieldArr(-1, DataArrayInt([2]*5))
+      self.assertRaises(InterpKernelException, mum.checkConsistency)
+      mum=mum2; mum2=mum.deepCopy();
+      arr = DataArrayAsciiChar(['tutu           x']*5)
+      mum.setNameFieldAtLevel(-1, arr); arr.reAlloc(35);
+      self.assertRaises(InterpKernelException, mum.checkConsistency)
+
+    def testCheckSMESHConsistency(self):
+      m2 = MEDCouplingUMesh("2d", 2)
+      m2.setCoords(DataArrayDouble([(0.0, 1.0)] * 4, 4,2))  # whatever
+      m2.setConnectivity(DataArrayInt([NORM_TRI3, 0,1,2,NORM_TRI3, 1,2,3]), DataArrayInt(([0,4,8])))
+      m1 , _, _ , _, _ = m2.buildDescendingConnectivity()
+      mum = MEDFileUMesh()
+      mum.setMeshAtLevel(0, m2)
+      mum.setMeshAtLevel(-1, m1)
+      mum.checkConsistency()
+      mum.checkSMESHConsistency()
+      n2 = DataArrayInt(m2.getNumberOfCells(), 1); n2.iota(1)
+      n1 = DataArrayInt(m1.getNumberOfCells(), 1); n1.iota(1)
+      mum.setRenumFieldArr(0, n2)
+      mum.setRenumFieldArr(-1, n1)
+      self.assertRaises(InterpKernelException, mum.checkSMESHConsistency)
+      mum.setRenumFieldArr(-1, n1+100)
+      mum.checkSMESHConsistency()
+      pass
+
+    def testClearNodeAndCellNumbers(self):
+      m2 = MEDCouplingUMesh("2d", 2)
+      m2.setCoords(DataArrayDouble([(0.0, 1.0)] * 4, 4,2))  # whatever
+      m2.setConnectivity(DataArrayInt([NORM_TRI3, 0,1,2,NORM_TRI3, 1,2,3]), DataArrayInt(([0,4,8])))
+      m1 , _, _ , _, _ = m2.buildDescendingConnectivity()
+      mum = MEDFileUMesh()
+      mum.setMeshAtLevel(0, m2)
+      mum.setMeshAtLevel(-1, m1)
+      mum.checkConsistency()
+      n2 = DataArrayInt(m2.getNumberOfCells(), 1); n2.iota(1)
+      n1 = DataArrayInt(m1.getNumberOfCells(), 1); n1.iota(1)
+      mum.setRenumFieldArr(0, n2)
+      mum.setRenumFieldArr(-1, n1)
+      mum.clearNodeAndCellNumbers()
+      mum.checkSMESHConsistency()
+      pass
+
     pass
 
 if __name__ == "__main__":