Changeset 22674


Ignore:
Timestamp:
04/04/18 11:46:26 (7 years ago)
Author:
Mathieu Morlighem
Message:

CHG: working on making Chaco and MeshPartition python friendly

Location:
issm/trunk-jpl/src
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/src/m/modules/MeshPartition.m

    r22069 r22674  
    33%
    44%          Usage:
    5 %                       [element_partitioning,node_partitioning]=MeshPartition(md.mesh,numpartitions)");
     5%                       [element_partitioning,node_partitioning]=MeshPartition(md,numpartitions)");
    66%
    77%          element_partitioning: Vector of partitioning area numbers, for every element.
     
    1414end
    1515
     16%Get mesh info from md.mesh
     17numberofvertices = md.mesh.numberofvertices;
     18numberofelements = md.mesh.numberofelements;
     19elements         = md.mesh.elements;
     20numberofelements2d = 0;
     21numberofvertices2d = 0;
     22numberoflayers     = 1;
     23elements2d         = [];
     24if isa(md.mesh,'mesh3dprisms')
     25        elementtype = 'Penta';
     26        numberofelements2d = md.mesh.numberofelements2d;
     27        numberofvertices2d = md.mesh.numberofvertices2d;
     28        numberoflayers     = md.mesh.numberoflayers;
     29        elements2d         = md.mesh.elements2d;
     30elseif isa(md.mesh,'mesh2d')
     31        elementtype = 'Tria';
     32elseif isa(md.mesh,'mesh2dvertical')
     33        elementtype = 'Tria';
     34end
     35
    1636% Call mex module
    17 [element_partitioning, node_partitioning] = MeshPartition_matlab(md.mesh,numpartitions);
     37[element_partitioning, node_partitioning] = MeshPartition_matlab(numberofvertices,elements,numberofvertices2d,elements2d,numberoflayers,meshelementtype,numpartitions);
  • issm/trunk-jpl/src/wrappers/MeshPartition/MeshPartition.cpp

    r22069 r22674  
    1515WRAPPER(MeshPartition_python){
    1616
    17         /*Indexing: */
    18         int i,j;
    19 
    2017        /* required input: */
    21         int  meshelementtype;
    2218        int  numberofelements;
    2319        int  numberofvertices;
    24         int *elements         = NULL;
     20        int *elements = NULL;
    2521        int  elements_width;
    26 
    2722        int numberofelements2d=0;
    2823        int numberofvertices2d=0;
    2924        int* elements2d=NULL;
    30 
    3125        int numberoflayers;
    3226        int numareas=1;
     
    4236
    4337        /*checks on arguments on the matlab side: */
    44         CheckNumMatlabArguments(nlhs,NLHS,nrhs,NRHS,__FUNCT__,&MeshPartitionUsage);
     38        CHECKARGUMENTS(NLHS,NRHS,&MeshPartitionUsage);
    4539
    4640        /*Fetch data: */
    47         FetchData(&numberofelements,mxGetAssignedField(MESH,0,"numberofelements"));
    48         FetchData(&numberofvertices,mxGetAssignedField(MESH,0,"numberofvertices"));
    49         FetchData(&elements,NULL,&elements_width,mxGetAssignedField(MESH,0,"elements"));
     41        FetchData(&numberofvertices,NUMBEROFVERTICES);
     42        FetchData(&elements,&numberofelements,&elements_width,ELEMENTS);
     43        FetchData(&numberofvertices2d,NUMBEROFVERTICES2D);
     44        FetchData(&elements2d,&numberofelements2d,NULL,ELEMENTS2D);
     45        FetchData(&numberoflayers,NUMBEROFLAYERS);
     46        FetchData(&numareas,NUMAREAS);
    5047
    51         if(strcmp(mxGetClassName(MESH),"mesh3dprisms")==0){
    52                 meshelementtype = PentaEnum;
    53                 FetchData(&numberofelements2d,mxGetAssignedField(MESH,0,"numberofelements2d"));
    54                 FetchData(&numberofvertices2d,mxGetAssignedField(MESH,0,"numberofvertices2d"));
    55                 FetchData(&elements2d,NULL,NULL,mxGetAssignedField(MESH,0,"elements2d"));
    56                 FetchData(&numberoflayers,mxGetAssignedField(MESH,0,"numberoflayers"));
    57         }
    58         else if(strcmp(mxGetClassName(MESH),"mesh2d")==0){
    59                 meshelementtype = TriaEnum;
    60                 numberoflayers=1;
    61         }
    62         else if(strcmp(mxGetClassName(MESH),"mesh2dvertical")==0){
    63                 meshelementtype = TriaEnum;
    64                 numberoflayers=1;
    65         }
    66         else{
    67                 _error_("Mesh type "<<mxGetClassName(MESH)<<" not supported yet");
    68         }
    69         FetchData(&numareas,NUMAREAS);
     48        /*Get mesh element type and convert to Enum*/
     49        char* meshtype_str = NULL;
     50        FetchData(&meshtype_str,MESHELEMENTTYPE);
     51        int meshelementtype = StringToEnumx(meshtype_str);
     52        xDelete<char>(meshtype_str);
    7053
    7154        /*Run partitioning algorithm based on a "clever" use of the Metis partitioner: */
     
    7558        /*Post process node_partitioning and element_partitioning to be in double format. Metis needed them in int* format: */
    7659        element_partitioning=xNew<double>(numberofelements);
    77         for (i=0;i<numberofelements;i++){
     60        for(int i=0;i<numberofelements;i++){
    7861                element_partitioning[i]=(double)int_element_partitioning[i]+1; //Metis indexing from 0, matlab from 1.
    7962        }
    8063
    8164        node_partitioning=xNew<double>(numberofvertices);
    82         for (i=0;i<numberofvertices;i++){
     65        for(int i=0;i<numberofvertices;i++){
    8366                node_partitioning[i]=(double)int_node_partitioning[i]+1; //Metis indexing from 0, matlab from 1.
    8467        }
     
    9073        /*Free ressources:*/
    9174        xDelete<int>(elements);
    92         xDelete<int>( elements2d);
     75        xDelete<int>(elements2d);
    9376        xDelete<int>(int_element_partitioning);
    9477        xDelete<int>(int_node_partitioning);
  • issm/trunk-jpl/src/wrappers/MeshPartition/MeshPartition.h

    r16329 r22674  
    2727#ifdef _HAVE_MATLAB_MODULES_
    2828/* serial input macros: */
    29 #define MESH    prhs[0]
    30 #define NUMAREAS prhs[1]
     29#define NUMBEROFVERTICES   prhs[0]
     30#define ELEMENTS           prhs[1]
     31#define NUMBEROFVERTICES2D prhs[2]
     32#define ELEMENTS2D         prhs[3]
     33#define NUMBEROFLAYERS     prhs[4]
     34#define MESHELEMENTTYPE    prhs[5]
     35#define NUMAREAS           prhs[6]
    3136/* serial output macros: */
    3237#define ELEMENTPARTITIONING (mxArray**)&plhs[0]
     
    3641#ifdef _HAVE_PYTHON_MODULES_
    3742/* serial input macros: */
    38 #define MESH     PyTuple_GetItem(args,0)
    39 #define NUMAREAS PyTuple_GetItem(args,1)
     43#define NUMBEROFVERTICES   PyTuple_GetItem(args,0)
     44#define ELEMENTS           PyTuple_GetItem(args,1)
     45#define NUMBEROFVERTICES2D PyTuple_GetItem(args,2)
     46#define ELEMENTS2D         PyTuple_GetItem(args,3)
     47#define NUMBEROFLAYERS     PyTuple_GetItem(args,4)
     48#define MESHELEMENTTYPE    PyTuple_GetItem(args,5)
     49#define NUMAREAS           PyTuple_GetItem(args,6)
    4050/* serial output macros: */
    4151#define ELEMENTPARTITIONING output,0
     
    4757#define NLHS  2
    4858#undef NRHS
    49 #define NRHS  2
     59#define NRHS  7
    5060
    5161#endif  /* _MESHPARTITION_H */
  • issm/trunk-jpl/src/wrappers/python/Makefile.am

    r22668 r22674  
    3939                                                InterpFromMeshToGrid_python.la\
    4040                                                IssmConfig_python.la\
     41                                                MeshPartition_python.la\
    4142                                                MeshProfileIntersection_python.la\
    4243                                                NodeConnectivity_python.la\
    4344                                                Triangle_python.la\
    4445                                                ProcessRifts_python.la
     46
     47if CHACO
     48lib_LTLIBRARIES += Chaco_python.la
     49endif
    4550endif
    4651#}}}
     
    101106BamgTriangulate_python_la_LIBADD = ${deps} $(MPILIB) $(PETSCLIB) $(GSLLIB) $(PROJ4LIB)
    102107
     108if CHACO
     109Chaco_python_la_SOURCES = ../Chaco/Chaco.cpp
     110Chaco_python_la_CXXFLAGS = ${AM_CXXFLAGS}
     111Chaco_python_la_LIBADD = ${deps} $(MPILIB)  $(CHACOLIB) $(PETSCLIB) $(GSLLIB) $(PROJ4LIB)
     112endif
     113
    103114ContourToMesh_python_la_SOURCES = ../ContourToMesh/ContourToMesh.cpp
    104115ContourToMesh_python_la_CXXFLAGS = ${AM_CXXFLAGS}
     
    133144IssmConfig_python_la_LIBADD = ${deps} $(MPILIB) $(PETSCLIB)
    134145
     146MeshPartition_python_la_SOURCES = ../MeshPartition/MeshPartition.cpp
     147MeshPartition_python_la_CXXFLAGS = ${AM_CXXFLAGS}
     148MeshPartition_python_la_LIBADD = ${deps} $(MPILIB) $(PETSCLIB) $(GSLLIB) $(PROJ4LIB)
     149
    135150MeshProfileIntersection_python_la_SOURCES = ../MeshProfileIntersection/MeshProfileIntersection.cpp
    136151MeshProfileIntersection_python_la_CXXFLAGS = ${AM_CXXFLAGS}
  • issm/trunk-jpl/src/wrappers/python/io/FetchPythonData.cpp

    r21864 r22674  
    4444        /*output: */
    4545        *pscalar=dscalar;
     46}
     47/*}}}*/
     48/*FUNCTION FetchData(float* pscalar,PyObject* py_float){{{*/
     49void FetchData(float* pscalar,PyObject* py_float){
     50
     51        float fscalar;
     52
     53        /*return internal value: */
     54        if  (PyFloat_Check(py_float))
     55         fscalar=PyFloat_AsDouble(py_float);
     56        else if (PyLong_Check(py_float)){
     57                #if _PYTHON_MAJOR_ == 3
     58                fscalar=(float)PyLong_AsLong(py_float);
     59                #else
     60                fscalar=(float)PyLong_AsDouble(py_float);
     61                #endif
     62        }
     63        else if (PyInt_Check(py_float))
     64         fscalar=(float)PyInt_AsLong(py_float);
     65        else if (PyBool_Check(py_float))
     66         fscalar=(float)PyLong_AsLong(py_float);
     67        else if (PyTuple_Check(py_float) && (int)PyTuple_Size(py_float)==1)
     68         FetchData(&fscalar,PyTuple_GetItem(py_float,(Py_ssize_t)0));
     69        else if (PyList_Check(py_float) && (int)PyList_Size(py_float)==1)
     70         FetchData(&fscalar,PyList_GetItem(py_float,(Py_ssize_t)0));
     71        else
     72         _error_("unrecognized float type in input!");
     73
     74        /*output: */
     75        *pscalar=fscalar;
    4676}
    4777/*}}}*/
     
    464494}
    465495/*}}}*/
     496/*FUNCTION FetchData(float** pvector,int* pM, PyObject* py_vector){{{*/
     497void FetchData(float** pvector,int* pM,PyObject* py_vector){
     498
     499        /*output: */
     500        float* vector=NULL;
     501        int M;
     502        int ndim;
     503        npy_intp*  dims=NULL;
     504
     505        /*intermediary:*/
     506        long*   lvector=NULL;
     507        bool*   bvector=NULL;
     508        double* dvector=NULL;
     509        int i;
     510        PyObject* py_vector2=NULL;
     511
     512        if     (PyArray_Check((PyArrayObject*)py_vector)) {
     513                /*retrieve dimensions: */
     514                ndim=PyArray_NDIM((const PyArrayObject*)py_vector);
     515                if      (ndim==1) {
     516                        dims=PyArray_DIMS((PyArrayObject*)py_vector);
     517                        M=dims[0];
     518                }
     519                else if (ndim==2) {
     520                        dims=PyArray_DIMS((PyArrayObject*)py_vector);
     521                        if (dims[1]==1)
     522                         M=dims[0];
     523                        else
     524                         _error_("expecting an Mx1 matrix or M vector in input!");
     525                }
     526                else
     527                 _error_("expecting an Mx1 matrix or M vector in input!");
     528
     529                if (M) {
     530                        if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_vector)) {
     531                                py_vector2=PyArray_ContiguousFromAny(py_vector,NPY_LONG,ndim,ndim);
     532                                py_vector=py_vector2;
     533                        }
     534
     535                        if      (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_DOUBLE) {
     536                                /*retrieve internal value: */
     537                                dvector=(double*)PyArray_DATA((PyArrayObject*)py_vector);
     538
     539                                /*transform into int vector: */
     540                                vector=xNew<float>(M);
     541                                for(i=0;i<M;i++)vector[i]=(float)lvector[i];
     542                        }
     543
     544                        else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_LONG) {
     545                                /*retrieve internal value: */
     546                                lvector=(long*)PyArray_DATA((PyArrayObject*)py_vector);
     547
     548                                /*transform into int vector: */
     549                                vector=xNew<float>(M);
     550                                for(i=0;i<M;i++)vector[i]=(float)lvector[i];
     551                        }
     552
     553                        else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_BOOL) {
     554                                /*retrieve internal value: */
     555                                bvector=(bool*)PyArray_DATA((PyArrayObject*)py_vector);
     556
     557                                /*transform into int vector: */
     558                                vector=xNew<float>(M);
     559                                for(i=0;i<M;i++)vector[i]=(float)bvector[i];
     560                        }
     561
     562                        else
     563                         _error_("unrecognized int pyarray type in input!");
     564
     565                        if(py_vector2) delete(py_vector2);
     566                }
     567                else
     568                 vector=NULL;
     569        }
     570        else{
     571                M=1;
     572                vector=xNew<float>(M);
     573                FetchData(&(vector[0]),py_vector);
     574        }
     575
     576        /*output: */
     577        if(pM)*pM=M;
     578        if(pvector)*pvector=vector;
     579}
     580/*}}}*/
    466581/*FUNCTION FetchData(int** pvector,int* pM, PyObject* py_vector){{{*/
    467582void FetchData(int** pvector,int* pM,PyObject* py_vector){
     
    809924}
    810925/*}}}*/
     926void FetchChacoData(int* pnvtxs,int** padjacency,int** pstart,float** pewgts,PyObject* A_IN,PyObject* EWGTS_IN){
     927        _error_("Nathan... I need your help here");
     928}
    811929
    812930/*Python version dependent: */
  • issm/trunk-jpl/src/wrappers/python/io/WritePythonData.cpp

    r21624 r22674  
    9393        dim=(npy_intp)M;
    9494        array=PyArray_SimpleNewFromData(1,&dim,NPY_DOUBLE,vector_python);
     95
     96        PyTuple_SetItem(py_tuple, index, array);
     97
     98}/*}}}*/
     99/*FUNCTION WriteData(PyObject* py_tuple,int index, short* vector, int M){{{*/
     100void WriteData(PyObject* py_tuple, int index,short* vector, int M){
     101
     102        long* lvector=NULL;
     103        npy_intp dim=10;
     104        PyObject* array=NULL;
     105
     106        /*transform into long matrix: */
     107        lvector=xNew<long>(M);
     108        for(int i=0;i<M;i++)lvector[i]=(long)vector[i];
     109
     110        dim=(npy_intp)M;
     111        array=PyArray_SimpleNewFromData(1,&dim,NPY_INT64,lvector);
    95112
    96113        PyTuple_SetItem(py_tuple, index, array);
  • issm/trunk-jpl/src/wrappers/python/io/pythonio.h

    r15335 r22674  
    2222void WriteData(PyObject* py_tuple,int index, int integer);
    2323void WriteData(PyObject* py_tuple,int index, double* vector, int M);
     24void WriteData(PyObject* py_tuple,int index, short* vector, int M);
    2425void WriteData(PyObject* py_tuple,int index, int* vector, int M);
    2526void WriteData(PyObject* py_tuple,int index, char* string);
     
    3536void FetchData(bool** pmatrix,int* pM,int *pN,PyObject* py_matrix);
    3637void FetchData(double** pvector,int* pM,PyObject* py_ref);
     38void FetchData(float** pvector,int* pM,PyObject* dataref);
    3739void FetchData(int** pvector,int* pM,PyObject* py_ref);
    3840void FetchData(bool** pvector,int* pM,PyObject* py_ref);
    3941void FetchData(char** pstring,PyObject* py_unicode);
    4042void FetchData(double* pscalar,PyObject* py_float);
     43void FetchData(short* pscalar,PyObject* py_float);
    4144void FetchData(int* pscalar,PyObject* py_long);
    4245void FetchData(bool* pbool,PyObject* py_boolean);
     
    4649void FetchData(Options** poptions,int istart, int nrhs,PyObject* py_tuple);
    4750void FetchData(Contours** pcontours,PyObject* py_list);
     51void FetchChacoData(int* pnvtxs,int** padjacency,int** pstart,float** pewgts,PyObject* A_IN,PyObject* EWGTS_IN);
    4852
    4953int CheckNumPythonArguments(PyObject* inputs,int NRHS, void (*function)( void ));
Note: See TracChangeset for help on using the changeset viewer.