]> SALOME platform Git repositories - tools/medcoupling.git/commitdiff
Salome HOME
Extract fields and UMeshes is now available and tested agy/br810_2
authorAnthony Geay <anthony.geay@edf.fr>
Fri, 26 Aug 2016 08:27:47 +0000 (10:27 +0200)
committerAnthony Geay <anthony.geay@edf.fr>
Fri, 26 Aug 2016 08:27:47 +0000 (10:27 +0200)
src/MEDLoader/MEDFileField.cxx
src/MEDLoader/MEDFileMesh.cxx
src/MEDLoader/MEDFileMesh.hxx
src/MEDLoader/Swig/MEDLoaderCommon.i
src/MEDLoader/Swig/MEDLoaderTest3.py

index ed34130cd81f2c40d379ee535b7ee370083028a9..612cb82d95fda34b0e60b17b44edc26a49c5f8ab 100644 (file)
@@ -4763,7 +4763,7 @@ bool MEDFileAnyTypeField1TSWithoutSDA::presenceOfMultiDiscPerGeoType() const
 
 MEDCouplingFieldDouble *MEDFileAnyTypeField1TSWithoutSDA::fieldOnMesh(const MEDFileFieldGlobsReal *glob, const MEDFileMesh *mesh, MCAuto<DataArray>& arrOut, const MEDFileFieldNameScope& nasc) const
 {
-  static const char MSG0[]="MEDFileAnyTypeField1TSWithoutSDA::fieldOnMesh : the field is too complex to be able to extract a field ! Call getFieldOnMeshAtLevel method instead to deal with complexity !";
+  static const char MSG0[]="MEDFileAnyTypeField1TSWithoutSDA::fieldOnMesh : the field is too complex to be able to be extracted with  \"field\" method ! Call getFieldOnMeshAtLevel method instead to deal with complexity !";
   if(_field_per_mesh.empty())
     throw INTERP_KERNEL::Exception("MEDFileAnyTypeField1TSWithoutSDA::fieldOnMesh : the field is empty ! Nothing to extract !");
   if(_field_per_mesh.size()>1)
@@ -6424,7 +6424,7 @@ DataArrayDouble *MEDFileField1TS::ReturnSafelyDataArrayDouble(MCAuto<DataArray>&
  * The keys of \a extractDef is level relative to max ext of \a mm mesh.
  *
  * \return A new object that the caller is responsible to deallocate.
- * \sa MEDFileUMesh::deduceNodeSubPartFromCellSubPart
+ * \sa MEDFileUMesh::deduceNodeSubPartFromCellSubPart , MEDFileUMesh::extractPart
  */
 MEDFileField1TS *MEDFileField1TS::extractPart(const std::map<int, MCAuto<DataArrayInt> >& extractDef, MEDFileMesh *mm) const
 {
@@ -6444,6 +6444,8 @@ MEDFileField1TS *MEDFileField1TS::extractPart(const std::map<int, MCAuto<DataArr
               if(it2!=extractDef.end())
                 {
                   MCAuto<DataArrayInt> t((*it2).second);
+                  if(t.isNull())
+                    throw INTERP_KERNEL::Exception("MEDFileField1TS::extractPart : presence of a value with null pointer 1 !");
                   MCAuto<MEDCouplingFieldDouble> f(getFieldOnMeshAtLevel(ON_CELLS,(*lev),mm));
                   MCAuto<MEDCouplingFieldDouble> fOut(f->buildSubPart(t));
                   ret->setFieldNoProfileSBT(fOut);
@@ -6456,6 +6458,8 @@ MEDFileField1TS *MEDFileField1TS::extractPart(const std::map<int, MCAuto<DataArr
           if(it2==extractDef.end())
             throw INTERP_KERNEL::Exception("MEDFileField1TS::extractPart : presence of a NODE field and no extract array available for NODE !");
           MCAuto<DataArrayInt> t((*it2).second);
+          if(t.isNull())
+            throw INTERP_KERNEL::Exception("MEDFileField1TS::extractPart : presence of a value with null pointer 1 !");
           MCAuto<MEDCouplingFieldDouble> f(getFieldOnMeshAtLevel(ON_NODES,0,mm));
           MCAuto<MEDCouplingFieldDouble> fOut(f->deepCopy());
           DataArrayDouble *arr(f->getArray());
index 341f08c5dee3a920818f6db32507228e8e6f90e6..d187a455e0e1e851d19a5a9a542a37b1fd76b94b 100644 (file)
@@ -4068,7 +4068,7 @@ DataArrayInt *MEDFileUMesh::zipCoords()
  * This map tells for each level of cells, the cells kept in the extraction.
  * 
  * \return - a new reference of DataArrayInt that represents sorted node ids, the extraction is lying on.
- * \sa MEDFileField1TS::extractPart
+ * \sa MEDFileField1TS::extractPart, MEDFileUMesh::extractPart
  */
 DataArrayInt *MEDFileUMesh::deduceNodeSubPartFromCellSubPart(const std::map<int, MCAuto<DataArrayInt> >& extractDef) const
 {
@@ -4078,6 +4078,8 @@ DataArrayInt *MEDFileUMesh::deduceNodeSubPartFromCellSubPart(const std::map<int,
     {
       if((*it).first>1)
         throw INTERP_KERNEL::Exception("MEDFileUMesh::deduceNodeSubPartFromCellSubPart : invalid key ! Must be <=1 !");
+      if((*it).second.isNull())
+        throw INTERP_KERNEL::Exception("MEDFileUMesh::deduceNodeSubPartFromCellSubPart : presence of a value with null pointer !");
       if((*it).first==1)
         continue;
       if(std::find(levs.begin(),levs.end(),(*it).first)==levs.end())
@@ -4092,6 +4094,76 @@ DataArrayInt *MEDFileUMesh::deduceNodeSubPartFromCellSubPart(const std::map<int,
   return DataArrayInt::BuildListOfSwitchedOn(fetchedNodes);
 }
 
+/*!
+ * This method returns a new MEDFileUMesh that is the result of the extraction of cells/nodes in \a this.
+ * 
+ * \return - a new reference of MEDFileUMesh
+ * \sa MEDFileUMesh::deduceNodeSubPartFromCellSubPart, MEDFileFields::extractPart
+ */
+MEDFileUMesh *MEDFileUMesh::extractPart(const std::map<int, MCAuto<DataArrayInt> >& extractDef) const
+{
+  MCAuto<MEDFileUMesh> ret(MEDFileUMesh::New()); ret->setName(getName()); ret->copyFamGrpMapsFrom(*this);
+  std::vector<int> levs(getNonEmptyLevels());
+  for(std::map<int, MCAuto<DataArrayInt> >::const_iterator it=extractDef.begin();it!=extractDef.end();it++)
+    {
+      if((*it).first>1)
+        throw INTERP_KERNEL::Exception("MEDFileUMesh::extractPart : invalid key ! Must be <=1 !");
+      if((*it).second.isNull())
+        throw INTERP_KERNEL::Exception("MEDFileUMesh::extractPart : presence of a value with null pointer !");
+      if((*it).first==1)
+        continue;
+      if(std::find(levs.begin(),levs.end(),(*it).first)==levs.end())
+        {
+          std::ostringstream oss; oss << "MEDFileUMesh::extractPart : invalid level " << (*it).first << " ! Not present in this !";
+          throw INTERP_KERNEL::Exception(oss.str());
+        }
+      MCAuto<MEDCouplingUMesh> m(getMeshAtLevel((*it).first));
+      MCAuto<MEDCouplingUMesh> mPart(m->buildPartOfMySelf((*it).second->begin(),(*it).second->end(),true));
+      ret->setMeshAtLevel((*it).first,mPart);
+      const DataArrayInt *fam(getFamilyFieldAtLevel((*it).first)),*num(getNumberFieldAtLevel((*it).first));
+      if(fam)
+        {
+          MCAuto<DataArrayInt> famPart(fam->selectByTupleIdSafe((*it).second->begin(),(*it).second->end()));
+          ret->setFamilyFieldArr((*it).first,famPart);
+        }
+      if(num)
+        {
+          MCAuto<DataArrayInt> numPart(num->selectByTupleIdSafe((*it).second->begin(),(*it).second->end()));
+          ret->setFamilyFieldArr((*it).first,numPart);
+        }
+    }
+  std::map<int, MCAuto<DataArrayInt> >::const_iterator it2(extractDef.find(1));
+  if(it2!=extractDef.end())
+    {
+      const DataArrayDouble *coo(ret->getCoords());
+      if(!coo)
+        throw INTERP_KERNEL::Exception("MEDFileUMesh::extractPart : trying to extract nodes whereas there is no nodes !");
+      MCAuto<DataArrayInt> o2nNodes(((*it2).second)->invertArrayN2O2O2N(coo->getNumberOfTuples()));
+      MCAuto<DataArrayDouble> cooPart(coo->selectByTupleIdSafe((*it2).second->begin(),(*it2).second->end()));
+      ret->setCoords(cooPart);
+      const DataArrayInt *fam(getFamilyFieldAtLevel(1)),*num(getNumberFieldAtLevel(1));
+      if(fam)
+        {
+          MCAuto<DataArrayInt> famPart(fam->selectByTupleIdSafe((*it2).second->begin(),(*it2).second->end()));
+          ret->setFamilyFieldArr(1,famPart);
+        }
+      if(num)
+        {
+          MCAuto<DataArrayInt> numPart(num->selectByTupleIdSafe((*it2).second->begin(),(*it2).second->end()));
+          ret->setFamilyFieldArr(1,numPart);
+        }
+      for(std::map<int, MCAuto<DataArrayInt> >::const_iterator it3=extractDef.begin();it3!=extractDef.end();it3++)
+        {
+          if((*it3).first==1)
+            continue;
+          MCAuto<MEDCouplingUMesh> m(ret->getMeshAtLevel((*it3).first));
+          m->renumberNodesInConn(o2nNodes->begin());
+          ret->setMeshAtLevel((*it3).first,m);
+        }
+    }
+  return ret.retn();
+}
+
 /*!
  * This method performs an extrusion along a path defined by \a m1D.
  * \a this is expected to be a mesh with max mesh dimension equal to 2.
index a5f3ba9819bd7eb57892d22579d015517de7ac5e..9f0231313ad906583b759bdf525a90b537b5f9e8 100644 (file)
@@ -330,6 +330,7 @@ namespace MEDCoupling
     MEDLOADER_EXPORT bool unPolyze(std::vector<int>& oldCode, std::vector<int>& newCode, DataArrayInt *& o2nRenumCell);
     MEDLOADER_EXPORT DataArrayInt *zipCoords();
     MEDLOADER_EXPORT DataArrayInt *deduceNodeSubPartFromCellSubPart(const std::map<int, MCAuto<DataArrayInt> >& extractDef) const;
+    MEDLOADER_EXPORT MEDFileUMesh *extractPart(const std::map<int, MCAuto<DataArrayInt> >& extractDef) const;
     MEDLOADER_EXPORT MEDFileUMesh *buildExtrudedMesh(const MEDCouplingUMesh *m1D, int policy) const;
     MEDLOADER_EXPORT MEDFileUMesh *linearToQuadratic(int conversionType=0, double eps=1e-12) const;
     MEDLOADER_EXPORT MEDFileUMesh *quadraticToLinear(double eps=1e-12) const;
index 61002b51a9e3c5837d685c03fb675d215f5fd31a..818147457532b8f2e4a48e6d2a3596a7f9e34677 100644 (file)
@@ -121,6 +121,7 @@ using namespace MEDCoupling;
 %newobject MEDCoupling::MEDFileUMesh::extractNumberFieldOnGeoType;
 %newobject MEDCoupling::MEDFileUMesh::zipCoords;
 %newobject MEDCoupling::MEDFileUMesh::deduceNodeSubPartFromCellSubPart;
+%newobject MEDCoupling::MEDFileUMesh::extractPart;
 %newobject MEDCoupling::MEDFileUMesh::buildExtrudedMesh;
 %newobject MEDCoupling::MEDFileUMesh::linearToQuadratic;
 %newobject MEDCoupling::MEDFileUMesh::quadraticToLinear;
@@ -1400,6 +1401,13 @@ namespace MEDCoupling
            return self->deduceNodeSubPartFromCellSubPart(extractDefCpp);
          }
 
+         MEDFileUMesh *extractPart(PyObject *extractDef) const throw(INTERP_KERNEL::Exception)
+         {
+           std::map<int, MCAuto<DataArrayInt> > extractDefCpp;
+           convertToMapIntDataArrayInt(extractDef,extractDefCpp);
+           return self->extractPart(extractDefCpp);
+         }
+
          void setMeshes(PyObject *li, bool renum=false) throw(INTERP_KERNEL::Exception)
          {
            std::vector<const MEDCouplingUMesh *> ms;
index 0c6efa4523af782e11d673f0f5ca9863cbb60b8a..9ec3797c1ae5ac888ea0425cb84469e208441d9b 100644 (file)
@@ -5418,6 +5418,115 @@ class MEDLoaderTest3(unittest.TestCase):
         mm=MEDFileMesh.New(fname) ; f1ts=MEDFileField1TS(fname,fieldName,6,7)
         self.assertTrue(f.isEqual(f1ts.field(mm),1e-12,1e-12))
         pass
+
+    def testExtractPart1(self):
+        coo=DataArrayDouble([(0,0),(1,0),(2,0),(3,0),(4,0),(0,1),(1,1),(2,1),(3,1),(4,1),(0,2),(1,2),(2,2),(3,2),(4,2)])
+        meshName="mesh"
+        m0=MEDCouplingUMesh(meshName,2) ; m0.setCoords(coo) ; m0.allocateCells()
+        m0.insertNextCell(NORM_TRI3,[8,4,3])
+        m0.insertNextCell(NORM_TRI3,[8,9,4])
+        m0.insertNextCell(NORM_TRI3,[7,13,8])
+        m0.insertNextCell(NORM_TRI3,[7,12,13])
+        m0.insertNextCell(NORM_TRI3,[0,6,1])
+        m0.insertNextCell(NORM_TRI3,[0,5,6])
+        m0.insertNextCell(NORM_QUAD4,[1,6,7,2])
+        m0.insertNextCell(NORM_QUAD4,[2,7,8,3])
+        m0.insertNextCell(NORM_QUAD4,[8,13,14,9])
+        m0.insertNextCell(NORM_QUAD4,[6,11,12,7])
+        m0.insertNextCell(NORM_QUAD4,[5,10,11,6])
+        #
+        m1=MEDCouplingUMesh(meshName,1) ; m1.setCoords(coo) ; m1.allocateCells()
+        m1.insertNextCell(NORM_SEG2,[10,5])
+        m1.insertNextCell(NORM_SEG2,[5,0])
+        m1.insertNextCell(NORM_SEG2,[0,1])
+        m1.insertNextCell(NORM_SEG2,[1,2])
+        m1.insertNextCell(NORM_SEG2,[2,3])
+        m1.insertNextCell(NORM_SEG2,[3,4])
+        m1.insertNextCell(NORM_SEG2,[4,9])
+        m1.insertNextCell(NORM_SEG2,[9,14])
+        m1.insertNextCell(NORM_SEG2,[14,13])
+        m1.insertNextCell(NORM_SEG2,[13,12])
+        m1.insertNextCell(NORM_SEG2,[12,11])
+        m1.insertNextCell(NORM_SEG2,[11,10])
+        mm=MEDFileUMesh()
+        mm[0]=m0 ; mm[-1]=m1
+        arr0=DataArrayInt([0,1,2,3,4,6,7,8,12,13])
+        tab={} #
+        tab[0]=DataArrayInt([0,2,3,4,6,7])
+        tab[-1]=DataArrayInt([2,3,4,5,9])
+        fs=MEDFileFields()
+        self.assertTrue(mm.deduceNodeSubPartFromCellSubPart(tab).isEqual(arr0))
+        tab[1]=arr0
+        #
+        fname0="Field0"
+        fmts=MEDFileFieldMultiTS() ; fs.pushField(fmts)
+        t0=(16.5,3,4)
+        ic=["toto [m]"]
+        arr0_0=DataArrayDouble([100,101,102,103,104,105,106,107,108,109,110]) ; arr0_0.setInfoOnComponents(ic)
+        f0=MEDCouplingFieldDouble(ON_CELLS) ; f0.setTime(*t0) ; f0.setArray(arr0_0)
+        f0.setMesh(m0) ; f0.setName(fname0)
+        f1=MEDCouplingFieldDouble(ON_CELLS) ; f1.setTime(*t0) ; f1.setArray(DataArrayDouble([200,201,202,203,204,205,206,207,208,209,210,211]))
+        f1.setMesh(m1) ; f1.setName(fname0) ; f1.getArray().setInfoOnComponents(ic)
+        f2=MEDCouplingFieldDouble(ON_NODES) ; f2.setTime(*t0) ; f2.setArray(DataArrayDouble([300,301,302,303,304,305,306,307,308,309,310,311,312,313,314]))
+        f2.setMesh(m0) ; f2.setName(fname0) ; f2.getArray().setInfoOnComponents(ic)
+        f1ts=MEDFileField1TS() ; f1ts.setFieldNoProfileSBT(f0) ; f1ts.setFieldNoProfileSBT(f1) ; f1ts.setFieldNoProfileSBT(f2)
+        fmts.pushBackTimeStep(f1ts)
+        #
+        mmOut=mm.extractPart(tab)
+        #
+        fsPart0=fs.extractPart(tab,mm)
+        self.assertEqual(len(fsPart0),1)
+        fmtsP=fsPart0[0]
+        self.assertEqual(len(fmtsP),1)
+        f1ts=fmtsP[0]
+        self.assertRaises(InterpKernelException,f1ts.field,mmOut)
+        #
+        self.assertTrue(mmOut[0].computeCellCenterOfMass().isEqual(m0[tab[0]].computeCellCenterOfMass(),1e-12))
+        self.assertTrue(mmOut[-1].computeCellCenterOfMass().isEqual(m1[tab[-1]].computeCellCenterOfMass(),1e-12))
+        #
+        m0Part=m0.deepCopy()[tab[0]] ; m0Part.renumberNodes(tab[1].invertArrayN2O2O2N(mm.getNumberOfNodes()),len(tab[1])) ; m0Part.setName(m0.getName())
+        self.assertTrue(mmOut[0].isEqual(m0Part,1e-12))
+        m1Part=m1.deepCopy()[tab[-1]] ; m1Part.renumberNodes(tab[1].invertArrayN2O2O2N(mm.getNumberOfNodes()),len(tab[1])) ; m1Part.setName(m0.getName())
+        self.assertTrue(mmOut[0].isEqual(m0Part,1e-12))
+        self.assertTrue(mmOut[-1].isEqual(m1Part,1e-12))
+        #
+        f0Part=f1ts.getFieldOnMeshAtLevel(ON_CELLS,0,mmOut) ; f0Part.checkConsistencyLight()
+        self.assertEqual(f0Part.getTypeOfField(),ON_CELLS)
+        self.assertTrue(f0Part.getMesh().isEqual(m0Part,1e-12))
+        arr0Exp=DataArrayDouble([100,102,103,104,106,107]) ; arr0Exp.setInfoOnComponents(ic)
+        self.assertTrue(f0Part.getArray().isEqual(arr0Exp,1e-12)) ; self.assertEqual(f0Part.getTime(),list(t0))
+        f1Part=f1ts.getFieldOnMeshAtLevel(ON_CELLS,-1,mmOut) ; f1Part.checkConsistencyLight()
+        self.assertEqual(f1Part.getTypeOfField(),ON_CELLS)
+        self.assertTrue(f1Part.getMesh().isEqual(m1Part,1e-12))
+        arr1Exp=DataArrayDouble([202,203,204,205,209]) ; arr1Exp.setInfoOnComponents(ic)
+        self.assertTrue(f1Part.getArray().isEqual(arr1Exp,1e-12)) ; self.assertEqual(f1Part.getTime(),list(t0))
+        #
+        f2Part=f1ts.getFieldOnMeshAtLevel(ON_NODES,0,mmOut) ; f2Part.checkConsistencyLight()
+        arr2Exp=DataArrayDouble([300,301,302,303,304,306,307,308,312,313]) ; arr2Exp.setInfoOnComponents(ic)
+        self.assertTrue(f2Part.getArray().isEqual(arr2Exp,1e-12)) ; self.assertEqual(f2Part.getTime(),list(t0))
+        # multisteps
+        fs=MEDFileFields() ; fmts=MEDFileFieldMultiTS() ; fs.pushField(fmts)
+        tss=[(16.5,3,4),(17.5,4,5),(18.5,5,6)]
+        for i,tt in enumerate(tss):
+            f0=MEDCouplingFieldDouble(ON_CELLS) ; f0.setTime(*tt)
+            myarr=arr0_0+i*1000.
+            f0.setArray(myarr)
+            f0.setMesh(m0) ; f0.setName(fname0) ; f0.getArray().setInfoOnComponents(ic)
+            f1ts=MEDFileField1TS() ; f1ts.setFieldNoProfileSBT(f0) ; fmts.pushBackTimeStep(f1ts)
+            pass
+        fsPart1=fs.extractPart(tab,mm)
+        self.assertEqual(len(fsPart1),1)
+        fmtsP=fsPart1[0]
+        self.assertEqual(len(fmtsP),len(tss))
+        for i,(f1tsP,tt) in enumerate(zip(fmtsP,tss)):
+            fPart=f1tsP.field(mmOut) ; fPart.checkConsistencyLight()
+            self.assertEqual(fPart.getTypeOfField(),ON_CELLS)
+            arr0Exp=DataArrayDouble([100,102,103,104,106,107]) ; arr0Exp.setInfoOnComponents(ic) ; arr0Exp+=i*1000.
+            self.assertTrue(fPart.getMesh().isEqual(m0Part,1e-12))
+            self.assertTrue(fPart.getArray().isEqual(arr0Exp,1e-12))
+            self.assertEqual(fPart.getTime(),list(tt))
+            pass
+        pass
     
     pass