Salome HOME
numpy bindings for dense matrix
[tools/medcoupling.git] / src / MEDCoupling_Swig / MEDCouplingCommon.i
index 3efdfc62fb8839024bbb315b68658f6aec0fcfba..98c05d12b9ec93cd914922378243916abdbb03d4 100644 (file)
@@ -40,6 +40,8 @@
 #include "MEDCouplingFieldOverTime.hxx"
 #include "MEDCouplingDefinitionTime.hxx"
 #include "MEDCouplingFieldDiscretization.hxx"
+#include "MEDCouplingCartesianAMRMesh.hxx"
+#include "MEDCouplingMatrix.hxx"
 #include "MEDCouplingTypemaps.i"
 
 #include "InterpKernelAutoPtr.hxx"
@@ -310,6 +312,23 @@ using namespace INTERP_KERNEL;
 %newobject ParaMEDMEM::MEDCouplingMultiFields::New;
 %newobject ParaMEDMEM::MEDCouplingMultiFields::deepCpy;
 %newobject ParaMEDMEM::MEDCouplingFieldOverTime::New;
+%newobject ParaMEDMEM::MEDCouplingCartesianAMRPatch::getMesh;
+%newobject ParaMEDMEM::MEDCouplingCartesianAMRPatch::__getitem__;
+%newobject ParaMEDMEM::MEDCouplingCartesianAMRMesh::New;
+%newobject ParaMEDMEM::MEDCouplingCartesianAMRMesh::buildUnstructured;
+%newobject ParaMEDMEM::MEDCouplingCartesianAMRMesh::getGodFather;
+%newobject ParaMEDMEM::MEDCouplingCartesianAMRMesh::getFather;
+%newobject ParaMEDMEM::MEDCouplingCartesianAMRMesh::getPatch;
+%newobject ParaMEDMEM::MEDCouplingCartesianAMRMesh::__getitem__;
+%newobject ParaMEDMEM::DenseMatrix::New;
+%newobject ParaMEDMEM::DenseMatrix::deepCpy;
+%newobject ParaMEDMEM::DenseMatrix::shallowCpy;
+%newobject ParaMEDMEM::DenseMatrix::getData;
+%newobject ParaMEDMEM::DenseMatrix::matVecMult;
+%newobject ParaMEDMEM::DenseMatrix::MatVecMult;
+%newobject ParaMEDMEM::DenseMatrix::__add__;
+%newobject ParaMEDMEM::DenseMatrix::__sub__;
+%newobject ParaMEDMEM::DenseMatrix::__mul__;
 
 %feature("unref") MEDCouplingPointSet "$this->decrRef();"
 %feature("unref") MEDCouplingMesh "$this->decrRef();"
@@ -319,6 +338,8 @@ using namespace INTERP_KERNEL;
 %feature("unref") MEDCoupling1DGTUMesh "$this->decrRef();"
 %feature("unref") MEDCouplingExtrudedMesh "$this->decrRef();"
 %feature("unref") MEDCouplingCMesh "$this->decrRef();"
+%feature("unref") MEDCouplingIMesh "$this->decrRef();"
+%feature("unref") MEDCouplingCurveLinearMesh "$this->decrRef();"
 %feature("unref") MEDCouplingField "$this->decrRef();"
 %feature("unref") MEDCouplingFieldDiscretizationP0 "$this->decrRef();"
 %feature("unref") MEDCouplingFieldDiscretizationP1 "$this->decrRef();"
@@ -329,6 +350,9 @@ using namespace INTERP_KERNEL;
 %feature("unref") MEDCouplingMultiFields "$this->decrRef();"
 %feature("unref") MEDCouplingFieldTemplate "$this->decrRef();"
 %feature("unref") MEDCouplingMultiFields "$this->decrRef();"
+%feature("unref") MEDCouplingCartesianAMRMesh "$this->decrRef();"
+%feature("unref") MEDCouplingCartesianAMRPatch "$this->decrRef();"
+%feature("unref") DenseMatrix "$this->decrRef();"
 
 %rename(assign) *::operator=;
 %ignore ParaMEDMEM::MEDCouplingGaussLocalization::pushTinySerializationIntInfo;
@@ -369,7 +393,8 @@ namespace ParaMEDMEM
       EXTRUDED = 8,
       CURVE_LINEAR = 9,
       SINGLE_STATIC_GEO_TYPE_UNSTRUCTURED = 10,
-      SINGLE_DYNAMIC_GEO_TYPE_UNSTRUCTURED = 11
+      SINGLE_DYNAMIC_GEO_TYPE_UNSTRUCTURED = 11,
+      IMAGE_GRID = 12
     } MEDCouplingMeshType;
 
   class DataArrayInt;
@@ -2634,6 +2659,17 @@ namespace ParaMEDMEM
         return oss.str();
       }
 
+      PyObject *structurizeMe(double eps=1e-12) const throw(INTERP_KERNEL::Exception)
+      {
+        DataArrayInt *cellPerm(0),*nodePerm(0);
+        MEDCouplingCMesh *retCpp(self->structurizeMe(cellPerm,nodePerm,eps));
+        PyObject *ret(PyTuple_New(3));
+        PyTuple_SetItem(ret,0,SWIG_NewPointerObj(SWIG_as_voidptr(retCpp),SWIGTYPE_p_ParaMEDMEM__MEDCouplingCMesh, SWIG_POINTER_OWN | 0 ));
+        PyTuple_SetItem(ret,1,SWIG_NewPointerObj(SWIG_as_voidptr(cellPerm),SWIGTYPE_p_ParaMEDMEM__DataArrayInt, SWIG_POINTER_OWN | 0 ));
+        PyTuple_SetItem(ret,2,SWIG_NewPointerObj(SWIG_as_voidptr(nodePerm),SWIGTYPE_p_ParaMEDMEM__DataArrayInt, SWIG_POINTER_OWN | 0 ));
+        return ret;
+      }
+
       static MEDCoupling1SGTUMesh *Merge1SGTUMeshes(PyObject *li) throw(INTERP_KERNEL::Exception)
       {
         std::vector<const ParaMEDMEM::MEDCoupling1SGTUMesh *> tmp;
@@ -2754,6 +2790,8 @@ namespace ParaMEDMEM
     MEDCoupling1SGTUMesh *build1SGTUnstructured() const throw(INTERP_KERNEL::Exception);
     static INTERP_KERNEL::NormalizedCellType GetGeoTypeGivenMeshDimension(int meshDim) throw(INTERP_KERNEL::Exception);
     MEDCoupling1SGTUMesh *build1SGTSubLevelMesh() const throw(INTERP_KERNEL::Exception);
+    static int DeduceNumberOfGivenStructure(const std::vector<int>& st) throw(INTERP_KERNEL::Exception);
+    static std::vector<int> GetSplitVectFromStruct(const std::vector<int>& strct) throw(INTERP_KERNEL::Exception);
     %extend
     {
       virtual MEDCouplingStructuredMesh *buildStructuredSubPart(PyObject *cellPart) const throw(INTERP_KERNEL::Exception)
@@ -2782,25 +2820,8 @@ namespace ParaMEDMEM
 
       static DataArrayInt *BuildExplicitIdsFrom(PyObject *st, PyObject *part) throw(INTERP_KERNEL::Exception)
       {
-        int tmpp1=-1,tmpp2=-1;
-        std::vector<int> tmp=fillArrayWithPyListInt2(part,tmpp1,tmpp2);
         std::vector< std::pair<int,int> > inp;
-        if(tmpp2==2)
-          {
-            inp.resize(tmpp1);
-            for(int i=0;i<tmpp1;i++)
-              { inp[i].first=tmp[2*i]; inp[i].second=tmp[2*i+1]; }
-          }
-        else if(tmpp2==1)
-          {
-            if(tmpp1%2!=0)
-              throw INTERP_KERNEL::Exception("Wrap of MEDCouplingStructuredMesh.BuildExplicitIdsFrom : invalid input size ! Must be even size !");
-            inp.resize(tmpp1/2);
-            for(int i=0;i<tmpp1/2;i++)
-              { inp[i].first=tmp[2*i]; inp[i].second=tmp[2*i+1]; }
-          }
-        else
-          throw INTERP_KERNEL::Exception("Wrap of MEDCouplingStructuredMesh.BuildExplicitIdsFrom : invalid input size !");
+        convertPyToVectorPairInt(part,inp);
         //
         int szArr,sw,iTypppArr;
         std::vector<int> stdvecTyyppArr;
@@ -2810,6 +2831,13 @@ namespace ParaMEDMEM
         return MEDCouplingStructuredMesh::BuildExplicitIdsFrom(tmp5,inp);
       }
 
+      static int DeduceNumberOfGivenRangeInCompactFrmt(PyObject *part) throw(INTERP_KERNEL::Exception)
+      {
+        std::vector< std::pair<int,int> > inp;
+        convertPyToVectorPairInt(part,inp);
+        return MEDCouplingStructuredMesh::DeduceNumberOfGivenRangeInCompactFrmt(inp);
+      }
+
       static DataArrayInt *Build1GTNodalConnectivity(PyObject *li) throw(INTERP_KERNEL::Exception)
       {
         int szArr,sw,iTypppArr;
@@ -2826,6 +2854,13 @@ namespace ParaMEDMEM
         return MEDCouplingStructuredMesh::Build1GTNodalConnectivityOfSubLevelMesh(tmp,tmp+szArr);
       }
 
+      static std::vector<int> GetDimensionsFromCompactFrmt(PyObject *partCompactFormat) throw(INTERP_KERNEL::Exception)
+      {
+        std::vector< std::pair<int,int> > inp;
+        convertPyToVectorPairInt(partCompactFormat,inp);
+        return MEDCouplingStructuredMesh::GetDimensionsFromCompactFrmt(inp);
+      }
+
       static PyObject *IsPartStructured(PyObject *li, PyObject *st) throw(INTERP_KERNEL::Exception)
       {
         int szArr,sw,iTypppArr;
@@ -2959,6 +2994,7 @@ namespace ParaMEDMEM
     std::string getAxisUnit() const throw(INTERP_KERNEL::Exception);
     double getMeasureOfAnyCell() const throw(INTERP_KERNEL::Exception);
     MEDCouplingCMesh *convertToCartesian() const throw(INTERP_KERNEL::Exception);
+    void refineWithFactor(int factor) throw(INTERP_KERNEL::Exception);
     %extend
     {
       MEDCouplingIMesh()
@@ -3021,6 +3057,13 @@ namespace ParaMEDMEM
         self->setDXYZ(originPtr,originPtr+nbTuples);
       }
 
+      static void CondenseFineToCoarse(DataArrayDouble *coarseDA, const std::vector<int>& coarseSt, const DataArrayDouble *fineDA, PyObject *fineLocInCoarse) throw(INTERP_KERNEL::Exception)
+      {
+        std::vector< std::pair<int,int> > inp;
+        convertPyToVectorPairInt(fineLocInCoarse,inp);
+        MEDCouplingIMesh::CondenseFineToCoarse(coarseDA,coarseSt,fineDA,inp);
+      }
+
       std::string __str__() const throw(INTERP_KERNEL::Exception)
       {
         return self->simpleRepr();
@@ -3259,6 +3302,7 @@ namespace ParaMEDMEM
     void setStartTime(double val, int iteration, int order) throw(INTERP_KERNEL::Exception);
     void setEndTime(double val, int iteration, int order) throw(INTERP_KERNEL::Exception);
     void applyLin(double a, double b, int compoId) throw(INTERP_KERNEL::Exception);
+    void applyLin(double a, double b) throw(INTERP_KERNEL::Exception);
     int getNumberOfComponents() const throw(INTERP_KERNEL::Exception);
     int getNumberOfTuples() const throw(INTERP_KERNEL::Exception);
     int getNumberOfValues() const throw(INTERP_KERNEL::Exception);
@@ -4555,6 +4599,265 @@ namespace ParaMEDMEM
          }
       }
   };
+
+  class MEDCouplingCartesianAMRMesh;
+
+  class MEDCouplingCartesianAMRPatch : public RefCountObject
+  {
+  public:
+    int getNumberOfCellsRecursiveWithOverlap() const throw(INTERP_KERNEL::Exception);
+    int getNumberOfCellsRecursiveWithoutOverlap() const throw(INTERP_KERNEL::Exception);
+    int getMaxNumberOfLevelsRelativeToThis() const throw(INTERP_KERNEL::Exception);
+    int getNumberOfOverlapedCellsForFather() const throw(INTERP_KERNEL::Exception);
+    %extend
+    {
+      PyObject *getBLTRRange() const throw(INTERP_KERNEL::Exception)
+      {
+        const std::vector< std::pair<int,int> >& ret(self->getBLTRRange());
+        return convertFromVectorPairInt(ret);
+      }
+
+      MEDCouplingCartesianAMRMesh *getMesh() const throw(INTERP_KERNEL::Exception)
+      {
+        MEDCouplingCartesianAMRMesh *ret(const_cast<MEDCouplingCartesianAMRMesh *>(self->getMesh()));
+        if(ret)
+          ret->incrRef();
+        return ret;
+      }
+
+      void addPatch(PyObject *bottomLeftTopRight, int factor) throw(INTERP_KERNEL::Exception)
+      {
+        std::vector< std::pair<int,int> > inp;
+        convertPyToVectorPairInt(bottomLeftTopRight,inp);
+        self->addPatch(inp,factor);
+      }
+
+      MEDCouplingCartesianAMRPatch *__getitem__(int patchId) const throw(INTERP_KERNEL::Exception)
+      {
+        const MEDCouplingCartesianAMRMesh *mesh(self->getMesh());
+        if(!mesh)
+          throw INTERP_KERNEL::Exception("wrap MEDCouplingCartesianAMRPatch.__getitem__ : no underlying mesh !");
+        if(patchId==mesh->getNumberOfPatches())
+          {
+            std::ostringstream oss;
+            oss << "Requesting for patchId " << patchId << " having only " << mesh->getNumberOfPatches() << " patches !";
+            PyErr_SetString(PyExc_StopIteration,oss.str().c_str());
+            return 0;
+          }
+        MEDCouplingCartesianAMRPatch *ret(const_cast<MEDCouplingCartesianAMRPatch *>(mesh->getPatch(patchId)));
+        if(ret)
+          ret->incrRef();
+        return ret;
+      }
+
+      void __delitem__(int patchId) throw(INTERP_KERNEL::Exception)
+      {
+        MEDCouplingCartesianAMRMesh *mesh(const_cast<MEDCouplingCartesianAMRMesh *>(self->getMesh()));
+        if(!mesh)
+          throw INTERP_KERNEL::Exception("wrap MEDCouplingCartesianAMRPatch.__delitem__ : no underlying mesh !");
+        mesh->removePatch(patchId);
+      }
+
+      int __len__() const throw(INTERP_KERNEL::Exception)
+      {
+        const MEDCouplingCartesianAMRMesh *mesh(self->getMesh());
+        if(!mesh)
+          throw INTERP_KERNEL::Exception("wrap MEDCouplingCartesianAMRPatch.__len__ : no underlying mesh !");
+        return mesh->getNumberOfPatches();
+      }
+    }
+  };
+  
+  class MEDCouplingCartesianAMRMesh : public RefCountObject, public TimeLabel
+  {
+  public:
+    
+    int getSpaceDimension() const throw(INTERP_KERNEL::Exception);
+    int getMaxNumberOfLevelsRelativeToThis() const throw(INTERP_KERNEL::Exception);
+    int getNumberOfCellsAtCurrentLevel() const throw(INTERP_KERNEL::Exception);
+    int getNumberOfCellsRecursiveWithOverlap() const throw(INTERP_KERNEL::Exception);
+    int getNumberOfCellsRecursiveWithoutOverlap() const throw(INTERP_KERNEL::Exception);
+    //
+    int getNumberOfPatches() const throw(INTERP_KERNEL::Exception);    
+    MEDCouplingUMesh *buildUnstructured() const throw(INTERP_KERNEL::Exception);
+    void removePatch(int patchId) throw(INTERP_KERNEL::Exception);
+    void detachFromFather() throw(INTERP_KERNEL::Exception);
+    %extend
+    {
+      static MEDCouplingCartesianAMRMesh *New(const std::string& meshName, int spaceDim, PyObject *nodeStrct, PyObject *origin, PyObject *dxyz) throw(INTERP_KERNEL::Exception)
+      {
+        static const char msg0[]="MEDCouplingCartesianAMRMesh::New : error on 'origin' parameter !";
+        static const char msg1[]="MEDCouplingCartesianAMRMesh::New : error on 'dxyz' parameter !";
+        const int *nodeStrctPtr(0);
+        const double *originPtr(0),*dxyzPtr(0);
+        int sw,sz,val0;
+        std::vector<int> bb0;
+        nodeStrctPtr=convertObjToPossibleCpp1_Safe(nodeStrct,sw,sz,val0,bb0);
+        //
+        double val,val2;
+        std::vector<double> bb,bb2;
+        int sz1,sz2;
+        originPtr=convertObjToPossibleCpp5_SingleCompo(origin,sw,val,bb,msg0,false,sz1);
+        dxyzPtr=convertObjToPossibleCpp5_SingleCompo(dxyz,sw,val2,bb2,msg1,false,sz2);
+        //
+        return MEDCouplingCartesianAMRMesh::New(meshName,spaceDim,nodeStrctPtr,nodeStrctPtr+sz,originPtr,originPtr+sz1,dxyzPtr,dxyzPtr+sz2);
+      }
+
+      MEDCouplingCartesianAMRMesh(const std::string& meshName, int spaceDim, PyObject *nodeStrct, PyObject *origin, PyObject *dxyz) throw(INTERP_KERNEL::Exception)
+      {
+        return ParaMEDMEM_MEDCouplingCartesianAMRMesh_New(meshName,spaceDim,nodeStrct,origin,dxyz);
+      }
+
+      void addPatch(PyObject *bottomLeftTopRight, int factor) throw(INTERP_KERNEL::Exception)
+      {
+        std::vector< std::pair<int,int> > inp;
+        convertPyToVectorPairInt(bottomLeftTopRight,inp);
+        self->addPatch(inp,factor);
+      }
+
+      MEDCouplingCartesianAMRMesh *getFather() const throw(INTERP_KERNEL::Exception)
+      {
+        MEDCouplingCartesianAMRMesh *ret(const_cast<MEDCouplingCartesianAMRMesh *>(self->getFather()));
+        if(ret)
+          ret->incrRef();
+        return ret;
+      }
+      
+      MEDCouplingCartesianAMRMesh *getGodFather() const throw(INTERP_KERNEL::Exception)
+      {
+        MEDCouplingCartesianAMRMesh *ret(const_cast<MEDCouplingCartesianAMRMesh *>(self->getGodFather()));
+        if(ret)
+          ret->incrRef();
+        return ret;
+      }
+
+      MEDCouplingCartesianAMRPatch *getPatch(int patchId) const throw(INTERP_KERNEL::Exception)
+      {
+        MEDCouplingCartesianAMRPatch *ret(const_cast<MEDCouplingCartesianAMRPatch *>(self->getPatch(patchId)));
+        if(ret)
+          ret->incrRef();
+        return ret;
+      }
+
+      MEDCouplingCartesianAMRPatch *__getitem__(int patchId) const throw(INTERP_KERNEL::Exception)
+      {
+        if(patchId==self->getNumberOfPatches())
+          {
+            std::ostringstream oss;
+            oss << "Requesting for patchId " << patchId << " having only " << self->getNumberOfPatches() << " patches !";
+            PyErr_SetString(PyExc_StopIteration,oss.str().c_str());
+            return 0;
+          }
+        MEDCouplingCartesianAMRPatch *ret(const_cast<MEDCouplingCartesianAMRPatch *>(self->getPatch(patchId)));
+        if(ret)
+          ret->incrRef();
+        return ret;
+      }
+
+      void __delitem__(int patchId) throw(INTERP_KERNEL::Exception)
+      {
+        self->removePatch(patchId);
+      }
+
+      int __len__() const throw(INTERP_KERNEL::Exception)
+      {
+        return self->getNumberOfPatches();
+      }
+    }
+  };
+
+  class DenseMatrix : public RefCountObject, public TimeLabel
+  {
+  public:
+    static DenseMatrix *New(int nbRows, int nbCols) throw(INTERP_KERNEL::Exception);
+    static DenseMatrix *New(DataArrayDouble *array, int nbRows, int nbCols) throw(INTERP_KERNEL::Exception);
+    DenseMatrix *deepCpy() const throw(INTERP_KERNEL::Exception);
+    DenseMatrix *shallowCpy() const throw(INTERP_KERNEL::Exception);
+    //
+    int getNumberOfRows() const throw(INTERP_KERNEL::Exception);
+    int getNumberOfCols() const throw(INTERP_KERNEL::Exception);
+    int getNbOfElems() const throw(INTERP_KERNEL::Exception);
+    void reBuild(DataArrayDouble *array, int nbRows=-1, int nbCols=-1) throw(INTERP_KERNEL::Exception);
+    void reShape(int nbRows, int nbCols) throw(INTERP_KERNEL::Exception);
+    void transpose() throw(INTERP_KERNEL::Exception);
+    //
+    bool isEqual(const DenseMatrix& other, double eps) const throw(INTERP_KERNEL::Exception);
+    DataArrayDouble *matVecMult(const DataArrayDouble *vec) const throw(INTERP_KERNEL::Exception);
+    static DataArrayDouble *MatVecMult(const DenseMatrix *mat, const DataArrayDouble *vec) throw(INTERP_KERNEL::Exception);
+    %extend
+    {
+      DenseMatrix(int nbRows, int nbCols) throw(INTERP_KERNEL::Exception)
+      {
+        return DenseMatrix::New(nbRows,nbCols);
+      }
+
+      DenseMatrix(DataArrayDouble *array, int nbRows, int nbCols) throw(INTERP_KERNEL::Exception)
+      {
+        return DenseMatrix::New(array,nbRows,nbCols);
+      }
+
+      PyObject *isEqualIfNotWhy(const DenseMatrix& other, double eps) const throw(INTERP_KERNEL::Exception)
+      {
+        std::string ret1;
+        bool ret0=self->isEqualIfNotWhy(other,eps,ret1);
+        PyObject *ret=PyTuple_New(2);
+        PyObject *ret0Py=ret0?Py_True:Py_False;
+        Py_XINCREF(ret0Py);
+        PyTuple_SetItem(ret,0,ret0Py);
+        PyTuple_SetItem(ret,1,PyString_FromString(ret1.c_str()));
+        return ret;
+      }
+
+      DataArrayDouble *getData() throw(INTERP_KERNEL::Exception)
+      {
+        DataArrayDouble *ret(self->getData());
+        if(ret)
+          ret->incrRef();
+        return ret;
+      }
+
+      DenseMatrix *__add__(const DenseMatrix *other) throw(INTERP_KERNEL::Exception)
+      {
+        return ParaMEDMEM::DenseMatrix::Add(self,other);
+      }
+
+      DenseMatrix *__sub__(const DenseMatrix *other) throw(INTERP_KERNEL::Exception)
+      {
+        return ParaMEDMEM::DenseMatrix::Substract(self,other);
+      }
+
+      DenseMatrix *__mul__(const DenseMatrix *other) throw(INTERP_KERNEL::Exception)
+      {
+        return ParaMEDMEM::DenseMatrix::Multiply(self,other);
+      }
+
+      DenseMatrix *__mul__(const DataArrayDouble *other) throw(INTERP_KERNEL::Exception)
+      {
+        return ParaMEDMEM::DenseMatrix::Multiply(self,other);
+      }
+
+      PyObject *___iadd___(PyObject *trueSelf, const DenseMatrix *other) throw(INTERP_KERNEL::Exception)
+      {
+        self->addEqual(other);
+        Py_XINCREF(trueSelf);
+        return trueSelf;
+      }
+
+      PyObject *___isub___(PyObject *trueSelf, const DenseMatrix *other) throw(INTERP_KERNEL::Exception)
+      {
+        self->substractEqual(other);
+        Py_XINCREF(trueSelf);
+        return trueSelf;
+      }
+#ifdef WITH_NUMPY
+      PyObject *toNumPyMatrix() throw(INTERP_KERNEL::Exception) // not const. It is not a bug !
+      {
+        PyObject *obj(ToNumPyArrayUnderground<DataArrayDouble,double>(self->getData(),NPY_DOUBLE,"DataArrayDouble",self->getNumberOfRows(),self->getNumberOfCols()));
+        return obj;
+      }
+#endif
+    }
+  };
 }
 
 %pythoncode %{