Changeset 15520


Ignore:
Timestamp:
07/19/13 09:22:18 (12 years ago)
Author:
Mathieu Morlighem
Message:

CHG: removed lots of uneeded matlab object conversion and fixed Chaco's blas conflict with petsc's

Location:
issm/trunk-jpl/src/wrappers/matlab
Files:
12 deleted
3 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/src/wrappers/matlab/Makefile.am

    r15440 r15520  
    1313io_sources= ./include/matlabincludes.h\
    1414                                ./io/matlabio.h\
    15                                 ./io/MatlabNArrayToNArray.cpp\
    1615                                ./io/CheckNumMatlabArguments.cpp\
    17                                 ./io/mxGetAssignedField.cpp\
    1816                                ./io/WriteMatlabData.cpp\
    19                                 ./io/FetchMatlabData.cpp\
    20                                 ./io/OptionParse.cpp\
    21                                 ./io/MatlabMatrixToMatrix.cpp\
    22                                 ./io/MatlabVectorToVector.cpp\
    23                                 ./io/MatlabVectorToDoubleVector.cpp\
    24                                 ./io/MatlabMatrixToDoubleMatrix.cpp\
    25                                 ./io/MatlabMatrixToIssmMat.cpp\
    26                                 ./io/MatlabVectorToIssmVec.cpp
    27 
    28                                
    29 if PETSC
    30 io_sources += ./io/MatlabMatrixToPetscMat.cpp\
    31                                 ./io/MatlabVectorToPetscVec.cpp
    32 endif
     17                                ./io/FetchMatlabData.cpp
    3318
    3419ALLCXXFLAGS= -fPIC -D_GNU_SOURCE -fno-omit-frame-pointer -pthread -D_CPP_ -D_WRAPPERS_ $(CXXFLAGS) $(CXXOPTFLAGS)
     
    160145Chaco_la_SOURCES = ../Chaco/Chaco.cpp\
    161146                                                 ../Chaco/Chaco.h
    162 Chaco_la_LIBADD = ${deps} $(MPILIB) $(PETSCLIB) $(CHACOLIB) $(GSLLIB)
     147Chaco_la_LIBADD = ${deps} $(MPILIB) $(CHACOLIB) $(GSLLIB) $(PETSCLIB)
    163148
    164149ContourToMesh_la_SOURCES = ../ContourToMesh/ContourToMesh.cpp\
  • issm/trunk-jpl/src/wrappers/matlab/io/FetchMatlabData.cpp

    r15516 r15520  
    1010
    1111#include "./matlabio.h"
     12#include <cstring>
    1213
    1314/*Primitive data types*/
     
    484485
    485486/*ISSM objects*/
    486 /*FUNCTION FetchData(Matrix<double>** pmatrix,const mxArray* dataref){{{*/
    487 void FetchData(Matrix<double>** pmatrix,const mxArray* dataref){
    488 
    489         Matrix<double>* outmatrix=NULL;
    490         int dummy=0;
    491 
    492         if (mxIsClass(dataref,"double") ){
    493 
    494                 /*Convert matlab matrix to matrix: */
    495                 outmatrix=MatlabMatrixToMatrix(dataref);
    496 
    497         }
    498         else{
    499                 /*This is an error: we don't have the correct input!: */
    500                 _error_("Input parameter of class " << mxGetClassName(dataref) << " not supported yet");
    501         }
    502 
    503         /*Assign output pointers:*/
    504         *pmatrix=outmatrix;
    505 }
    506 /*}}}*/
    507 /*FUNCTION FetchData(Vector<double>** pvector,const mxArray* dataref){{{*/
    508 void FetchData(Vector<double>** pvector,const mxArray* dataref){
    509 
    510         Vector<double>* vector=NULL;
    511         int dummy;
    512 
    513         if(mxIsEmpty(dataref)){
    514                 /*Nothing to pick up. Just initialize matrix pointer to NULL: */
    515                 vector=new Vector<double>(0);
    516         }
    517         else if (mxIsClass(dataref,"double") ){
    518 
    519                 /*Convert matlab vector to petsc vector: */
    520                 vector=MatlabVectorToVector(dataref);
    521         }
    522         else{
    523                 /*This is an error: we don't have the correct input!: */
    524                 _error_("Input parameter of class " << mxGetClassName(dataref) << " not supported yet");
    525         }
    526 
    527         /*Assign output pointers:*/
    528         *pvector=vector;
    529 }
    530 /*}}}*/
    531487/*FUNCTION FetchData(BamgGeom** pbamggeom,const mxArray* dataref){{{*/
    532488void FetchData(BamgGeom** pbamggeom,const mxArray* dataref){
     
    684640}
    685641/*}}}*/
     642
     643/*Toolkit*/
     644/*FUNCTION MatlabMatrixToDoubleMatrix {{{*/
     645int MatlabMatrixToDoubleMatrix(double** pmatrix,int* pmatrix_rows,int* pmatrix_cols,const mxArray* mxmatrix){
     646
     647        int        i,j,count,rows,cols;
     648
     649        /*output: */
     650        double* matrix=NULL;
     651
     652        /*matlab indices: */
     653        mwIndex*    ir=NULL;
     654        mwIndex*    jc=NULL;
     655
     656        /*Ok, first check if we are dealing with a sparse or full matrix: */
     657        if (mxIsSparse(mxmatrix)){
     658
     659                /*Dealing with sparse matrix: recover size first: */
     660                double* pmxmatrix=(double*)mxGetPr(mxmatrix);
     661                rows=mxGetM(mxmatrix);
     662                cols=mxGetN(mxmatrix);
     663
     664                if(rows*cols){
     665                        matrix=xNewZeroInit<double>(rows*cols);
     666
     667                        /*Now, get ir,jc and pr: */
     668                        ir=mxGetIr(mxmatrix);
     669                        jc=mxGetJc(mxmatrix);
     670
     671                        /*Now, start inserting data into double* matrix: */
     672                        count=0;
     673                        for(i=0;i<cols;i++){
     674                                for(j=0;j<(jc[i+1]-jc[i]);j++){
     675                                        matrix[rows*ir[count]+i]=pmxmatrix[count];
     676                                        count++;
     677                                }
     678                        }
     679                }
     680
     681        }
     682        else if(mxIsClass(mxmatrix,"double")){
     683                /*Dealing with dense matrix: recover pointer and size: */
     684                double* pmxmatrix=(double*)mxGetPr(mxmatrix);
     685                rows=mxGetM(mxmatrix);
     686                cols=mxGetN(mxmatrix);
     687
     688                /*Create serial matrix: */
     689                if(rows*cols){
     690                        matrix=xNewZeroInit<double>(rows*cols);
     691
     692                        for(i=0;i<rows;i++){
     693                                for(j=0;j<cols;j++){
     694                                        matrix[cols*i+j]=(double)pmxmatrix[rows*j+i];
     695                                }
     696                        }
     697                }
     698        }
     699        else if(mxIsClass(mxmatrix,"single")){
     700                /*Dealing with dense matrix: recover pointer and size: */
     701                float *pmxmatrix=(float*)mxGetPr(mxmatrix);
     702                rows=mxGetM(mxmatrix);
     703                cols=mxGetN(mxmatrix);
     704
     705                /*Create serial matrix: */
     706                if(rows*cols){
     707                        matrix=xNewZeroInit<double>(rows*cols);
     708
     709                        for(i=0;i<rows;i++){
     710                                for(j=0;j<cols;j++){
     711                                        matrix[cols*i+j]=(double)pmxmatrix[rows*j+i];
     712                                }
     713                        }
     714                }
     715        }
     716        else if(mxIsClass(mxmatrix,"int16")){
     717                /*Dealing with dense matrix: recover pointer and size: */
     718                short int *pmxmatrix=(short*)mxGetPr(mxmatrix);
     719                rows=mxGetM(mxmatrix);
     720                cols=mxGetN(mxmatrix);
     721
     722                /*Create serial matrix: */
     723                if(rows*cols){
     724                        matrix=xNewZeroInit<double>(rows*cols);
     725
     726                        for(i=0;i<rows;i++){
     727                                for(j=0;j<cols;j++){
     728                                        matrix[cols*i+j]=(double)pmxmatrix[rows*j+i];
     729                                }
     730                        }
     731                }
     732        }
     733        else if(mxIsClass(mxmatrix,"uint8")){
     734                /*Dealing with dense matrix: recover pointer and size: */
     735                char *pmxmatrix=(char*)mxGetPr(mxmatrix);
     736                rows=mxGetM(mxmatrix);
     737                cols=mxGetN(mxmatrix);
     738
     739                /*Create serial matrix: */
     740                if(rows*cols){
     741                        matrix=xNewZeroInit<double>(rows*cols);
     742
     743                        for(i=0;i<rows;i++){
     744                                for(j=0;j<cols;j++){
     745                                        matrix[cols*i+j]=(double)pmxmatrix[rows*j+i];
     746                                }
     747                        }
     748                }
     749        }
     750        else{
     751                _error_("Matlab matrix type Not implemented yet");
     752        }
     753
     754        /*Assign output pointer: */
     755        *pmatrix=matrix;
     756        *pmatrix_rows=rows;
     757        *pmatrix_cols=cols;
     758
     759        return 1;
     760}/*}}}*/
     761/*FUNCTION MatlabNArrayToNArray(double** pmatrix,int* pmatrix_numel,int* pmatrix_ndims,int** pmatrix_size,const mxArray* mxmatrix){{{*/
     762int MatlabNArrayToNArray(double** pmatrix,int* pmatrix_numel,int* pmatrix_ndims,int** pmatrix_size,const mxArray* mxmatrix){
     763
     764        int  i,j,rows,cols;
     765        int  numel,ndims;
     766        int *size,*dims;
     767        double* mxmatrix_ptr=NULL;
     768        const mwSize* ipt=NULL;
     769
     770        /*output: */
     771        double* matrix=NULL;
     772
     773        /*matlab indices: */
     774        mwIndex *ir    = NULL;
     775        mwIndex *jc    = NULL;
     776        double  *pr    = NULL;
     777        int      count;
     778
     779        /*get Matlab matrix information: */
     780        numel=mxGetNumberOfElements(mxmatrix);
     781        ndims=mxGetNumberOfDimensions(mxmatrix);
     782        ipt  =mxGetDimensions(mxmatrix);
     783        size =xNew<int>(ndims);
     784        for (i=0;i<ndims;i++) size[i]=(int)ipt[i];
     785
     786        /*Ok, first check if we are dealing with a sparse or full matrix: */
     787        if (mxIsSparse(mxmatrix)){
     788
     789                /*Dealing with sparse matrix: recover size first: */
     790                rows = mxGetM(mxmatrix);
     791                cols = mxGetN(mxmatrix);
     792
     793                matrix=xNewZeroInit<double>(rows*cols);
     794
     795                /*Now, get ir,jc and pr: */
     796                ir = mxGetIr(mxmatrix);
     797                jc = mxGetJc(mxmatrix);
     798                pr = mxGetPr(mxmatrix);
     799
     800                /*Now, start inserting data into double* matrix: */
     801                count=0;
     802                for(i=0;i<cols;i++){
     803                        for(j=0;j<(jc[i+1]-jc[i]);j++){
     804                                *(matrix+rows*ir[count]+i)=pr[count];
     805                                count++;
     806                        }
     807                }
     808
     809        }
     810        else{
     811
     812                /*Dealing with dense matrix: recover pointer and size: */
     813                mxmatrix_ptr=(double*)mxGetPr(mxmatrix);
     814
     815                /*Create serial matrix: */
     816                matrix=xNewZeroInit<double>(numel);
     817
     818                dims=xNew<int>(ndims);
     819                for(i=0;i<numel;i++){
     820                        ColumnWiseDimsFromIndex(dims,i,size,ndims);
     821                        j = IndexFromRowWiseDims(dims,size,ndims);
     822                        matrix[j]=(double)mxmatrix_ptr[i];
     823                }
     824                xDelete<int>(dims);
     825        }
     826
     827        /*Assign output pointer: */
     828        *pmatrix       = matrix;
     829        *pmatrix_numel = numel;
     830        *pmatrix_ndims = ndims;
     831        *pmatrix_size  = size;
     832
     833        return 1;
     834}
     835/*}}}*/
     836/*FUNCTION MatlabNArrayToNArray(bool** pmatrix,int* pmatrix_numel,int* pmatrix_ndims,int** pmatrix_size,const mxArray* mxmatrix){{{*/
     837int MatlabNArrayToNArray(bool** pmatrix,int* pmatrix_numel,int* pmatrix_ndims,int** pmatrix_size,const mxArray* mxmatrix){
     838
     839        int  i,j,rows,cols;
     840        int  numel,ndims;
     841        int *size,*dims;
     842        bool* mxmatrix_ptr=NULL;
     843        const mwSize* ipt=NULL;
     844
     845        /*output: */
     846        bool* matrix=NULL;
     847
     848        /*matlab indices: */
     849        mwIndex *ir    = NULL;
     850        mwIndex *jc    = NULL;
     851        bool    *pm    = NULL;
     852        int      count;
     853
     854        /*get Matlab matrix information: */
     855        numel = mxGetNumberOfElements(mxmatrix);
     856        ndims = mxGetNumberOfDimensions(mxmatrix);
     857        ipt   = mxGetDimensions(mxmatrix);
     858        size  = xNew<int>(ndims);
     859        for (i=0;i<ndims;i++) size[i]=(int)ipt[i];
     860
     861        /*Ok, first check if we are dealing with a sparse or full matrix: */
     862        if (mxIsSparse(mxmatrix)){
     863
     864                /*Dealing with sparse matrix: recover size first: */
     865                rows=mxGetM(mxmatrix);
     866                cols=mxGetN(mxmatrix);
     867                matrix=xNewZeroInit<bool>(rows*cols);
     868
     869                /*Now, get ir,jc and pm: */
     870                ir=mxGetIr(mxmatrix);
     871                jc=mxGetJc(mxmatrix);
     872                pm=(bool*)mxGetData(mxmatrix);
     873
     874                /*Now, start inserting data into bool* matrix: */
     875                count=0;
     876                for(i=0;i<cols;i++){
     877                        for(j=0;j<(jc[i+1]-jc[i]);j++){
     878                                matrix[rows*ir[count]+i]=pm[count];
     879                                count++;
     880                        }
     881                }
     882        }
     883        else{
     884
     885                /*Dealing with dense matrix: recover pointer and size: */
     886                mxmatrix_ptr=(bool*)mxGetData(mxmatrix);
     887
     888                /*Create serial matrix: */
     889                matrix=xNew<bool>(numel);
     890                dims=xNew<int>(ndims);
     891                for(i=0;i<numel;i++){
     892                        ColumnWiseDimsFromIndex(dims,i,size,ndims);
     893                        j=IndexFromRowWiseDims(dims,size,ndims);
     894                        matrix[j]=(bool)mxmatrix_ptr[i];
     895                }
     896                xDelete<int>(dims);
     897        }
     898
     899        /*Assign output pointer: */
     900        *pmatrix       = matrix;
     901        *pmatrix_numel = numel;
     902        *pmatrix_ndims = ndims;
     903        *pmatrix_size  = size;
     904
     905        return 1;
     906}
     907/*}}}*/
     908/*FUNCTION MatlabNArrayToNArray(char** pmatrix,int* pmatrix_numel,int* pmatrix_ndims,int** pmatrix_size,const mxArray* mxmatrix){{{*/
     909int MatlabNArrayToNArray(char** pmatrix,int* pmatrix_numel,int* pmatrix_ndims,int** pmatrix_size,const mxArray* mxmatrix){
     910
     911        int           i,j,rows,cols;
     912        int           numel,ndims;
     913        int          *size , *dims;
     914        mxChar       *mxmatrix_ptr = NULL;
     915        const mwSize *ipt          = NULL;
     916
     917        /*output: */
     918        char* matrix=NULL;
     919
     920        /*matlab indices: */
     921        mwIndex *ir    = NULL;
     922        mwIndex *jc    = NULL;
     923        char    *pm    = NULL;
     924        int      count;
     925
     926        /*get Matlab matrix information: */
     927        numel = mxGetNumberOfElements(mxmatrix);
     928        ndims = mxGetNumberOfDimensions(mxmatrix);
     929        ipt   = mxGetDimensions(mxmatrix);
     930        size  = xNew<int>(ndims);
     931        for (i=0;i<ndims;i++) size[i]=(int)ipt[i];
     932
     933        /*Ok, first check if we are dealing with a sparse or full matrix: */
     934        if (mxIsSparse(mxmatrix)){
     935
     936                /*Dealing with sparse matrix: recover size first: */
     937                rows = mxGetM(mxmatrix);
     938                cols = mxGetN(mxmatrix);
     939                matrix=xNew<char>(rows*cols);
     940
     941                /*Now, get ir,jc and pm: */
     942                ir = mxGetIr(mxmatrix);
     943                jc = mxGetJc(mxmatrix);
     944                pm = (char*)mxGetData(mxmatrix);
     945
     946                /*Now, start inserting data into char* matrix: */
     947                count=0;
     948                for(i=0;i<cols;i++){
     949                        for(j=0;j<(jc[i+1]-jc[i]);j++){
     950                                matrix[rows*ir[count]+i]=(char)pm[count];
     951                                count++;
     952                        }
     953                }
     954        }
     955        else{
     956                /*Dealing with dense matrix: recover pointer and size: */
     957                mxmatrix_ptr=mxGetChars(mxmatrix);
     958
     959                /*Create serial matrix: */
     960                matrix=xNew<char>(numel+1);
     961                matrix[numel]='\0';
     962
     963                /*looping code adapted from Matlab example explore.c: */
     964                int elements_per_page = size[0] * size[1];
     965                /* total_number_of_pages = size[2] x size[3] x ... x size[N-1] */
     966                int total_number_of_pages = 1;
     967                for (i=2; i<ndims; i++) {
     968                        total_number_of_pages *= size[i];
     969                }
     970
     971                i=0;
     972                for (int page=0; page < total_number_of_pages; page++) {
     973                        int row;
     974                        /* On each page, walk through each row. */
     975                        for (row=0; row<size[0]; row++)  {
     976                                int column;
     977                                j = (page * elements_per_page) + row;
     978
     979                                /* Walk along each column in the current row. */
     980                                for (column=0; column<size[1]; column++) {
     981                                        *(matrix+i++)=(char)*(mxmatrix_ptr+j);
     982                                        j += size[0];
     983                                }
     984                        }
     985                }
     986        }
     987
     988        /*Assign output pointer: */
     989        *pmatrix       = matrix;
     990        *pmatrix_numel = numel;
     991        *pmatrix_ndims = ndims;
     992        *pmatrix_size  = size;
     993
     994        return 1;
     995}
     996/*}}}*/
     997/*FUNCTION mxGetAssignedField{{{*/
     998mxArray* mxGetAssignedField(const mxArray* pmxa_array,int number,const char* field){
     999
     1000        //output
     1001        mxArray* mxfield=NULL;
     1002
     1003        //input
     1004        mxArray    *inputs[2];
     1005        mxArray    *pindex      = NULL;
     1006        const char *fnames[2];
     1007        mwSize      ndim        = 2;
     1008        mwSize      onebyone[2] = {1,1};
     1009
     1010        //We want to call the subsasgn method, and get the returned array.This ensures that if we are running
     1011        //large sized problems, the data is truly loaded from disk by the model subsasgn class method.
     1012        inputs[0]=(mxArray*)pmxa_array; //this is the model
     1013
     1014        //create index structure used in the assignment (index.type='.' and index.subs='x' for field x for ex)
     1015        fnames[0] = "type";
     1016        fnames[1] = "subs";
     1017        pindex=mxCreateStructArray( ndim,onebyone,2,fnames);
     1018        mxSetField( pindex, 0, "type",mxCreateString("."));
     1019        mxSetField( pindex, 0, "subs",mxCreateString(field));
     1020        inputs[1]=pindex;
     1021
     1022        mexCallMATLAB( 1, &mxfield, 2, (mxArray**)inputs, "subsref");
     1023
     1024        return mxfield;
     1025}/*}}}*/
     1026
     1027GenericOption<double>* OptionDoubleParse( char* name, const mxArray* prhs[]){ /*{{{*/
     1028
     1029        GenericOption<double> *odouble = NULL;
     1030
     1031        /*check and parse the name  */
     1032        odouble=new GenericOption<double>();
     1033        odouble->name =xNew<char>(strlen(name)+1);
     1034        memcpy(odouble->name,name,(strlen(name)+1)*sizeof(char));
     1035        FetchData(&odouble->value,prhs[0]);
     1036        odouble->numel=1;
     1037        odouble->ndims=1;
     1038        odouble->size=NULL;
     1039
     1040        return(odouble);
     1041}/*}}}*/
     1042GenericOption<double*>* OptionDoubleArrayParse( char* name, const mxArray* prhs[]){ /*{{{*/
     1043
     1044        GenericOption<double*> *odouble = NULL;
     1045
     1046        /*check and parse the name  */
     1047        odouble=new GenericOption<double*>();
     1048        odouble->name =xNew<char>(strlen(name)+1);
     1049        memcpy(odouble->name,name,(strlen(name)+1)*sizeof(char));
     1050
     1051        /*check and parse the value  */
     1052        if (!mxIsClass(prhs[0],"double")){
     1053                _error_("Value of option \"" << odouble->name  << "\" must be class \"double\", not class \"" << mxGetClassName(prhs[0]) <<"\".");
     1054        }
     1055        FetchData(&odouble->value,&odouble->numel,&odouble->ndims,&odouble->size,prhs[0]);
     1056
     1057        return(odouble);
     1058}/*}}}*/
     1059GenericOption<bool*>* OptionLogicalParse( char* name, const mxArray* prhs[]){ /*{{{*/
     1060
     1061        GenericOption<bool*> *ological = NULL;
     1062
     1063        /*check and parse the name  */
     1064        ological=new GenericOption<bool*>();
     1065        ological->name =xNew<char>(strlen(name)+1);
     1066        memcpy(ological->name,name,(strlen(name)+1)*sizeof(char));
     1067
     1068        /*check and parse the value  */
     1069        if (!mxIsClass(prhs[0],"logical")){
     1070                _error_("Value of option \"" << ological->name  << "\" must be class \"logical\", not class \"" << mxGetClassName(prhs[0]) <<"\".");
     1071        }
     1072        FetchData(&ological->value,&ological->numel,&ological->ndims,&ological->size,prhs[0]);
     1073
     1074        return(ological);
     1075}/*}}}*/
     1076GenericOption<char*>* OptionCharParse( char* name, const mxArray* prhs[]){ /*{{{*/
     1077
     1078        GenericOption<char*>  *ochar = NULL;
     1079
     1080        /*check and parse the name  */
     1081        ochar=new GenericOption<char*>();
     1082        ochar->name =xNew<char>(strlen(name)+1);
     1083        memcpy(ochar->name,name,(strlen(name)+1)*sizeof(char));
     1084
     1085        /*check and parse the value  */
     1086        if (!mxIsClass(prhs[0],"char")){
     1087                _error_("Value of option \"" << ochar->name  << "\" must be class \"char\", not class \"" << mxGetClassName(prhs[0]) <<"\".");
     1088        }
     1089        FetchData(&ochar->value,&ochar->numel,&ochar->ndims,&ochar->size,prhs[0]);
     1090
     1091        return(ochar);
     1092}/*}}}*/
     1093GenericOption<Options**>* OptionStructParse( char* name, const mxArray* prhs[]){ /*{{{*/
     1094
     1095        int            i;
     1096        char           namei[161];
     1097        Option*                   option      = NULL;
     1098        GenericOption<Options**>  *ostruct    = NULL;
     1099        const mwSize  *ipt        = NULL;
     1100        const mxArray *structi;
     1101        mwIndex        sindex;
     1102
     1103        /*check and parse the name  */
     1104        ostruct=new GenericOption<Options**>();
     1105        ostruct->name =xNew<char>(strlen(name)+1);
     1106        memcpy(ostruct->name,name,(strlen(name)+1)*sizeof(char));
     1107
     1108        /*check and parse the value  */
     1109        if (!mxIsClass(prhs[0],"struct")){
     1110                _error_("Value of option \"" << ostruct->name  << "\" must be class \"struct\", not class \"" << mxGetClassName(prhs[0]) <<"\".");
     1111        }
     1112        ostruct->numel=mxGetNumberOfElements(prhs[0]);
     1113        ostruct->ndims=mxGetNumberOfDimensions(prhs[0]);
     1114        ipt           =mxGetDimensions(prhs[0]);
     1115        ostruct->size =xNew<int>(ostruct->ndims);
     1116        for (i=0; i<ostruct->ndims; i++) ostruct->size[i]=(int)ipt[i];
     1117        if (ostruct->numel) ostruct->value=xNew<Options*>(ostruct->numel);
     1118
     1119        /*loop through and process each element of the struct array  */
     1120        for (sindex=0; sindex<ostruct->numel; sindex++) {
     1121                ostruct->value[sindex]=new Options;
     1122
     1123                /*loop through and process each field for the element  */
     1124                for (i=0; i<mxGetNumberOfFields(prhs[0]); i++) {
     1125                        sprintf(namei,"%s.%s",name,mxGetFieldNameByNumber(prhs[0],i));
     1126                        structi=mxGetFieldByNumber(prhs[0],sindex,i);
     1127
     1128                        option=(Option*)OptionParse(namei,&structi);
     1129                        ostruct->value[sindex]->AddObject((Object*)option);
     1130                        option=NULL;
     1131                }
     1132        }
     1133
     1134        return(ostruct);
     1135}/*}}}*/
     1136GenericOption<Options*>* OptionCellParse( char* name, const mxArray* prhs[]){ /*{{{*/
     1137
     1138        int            i;
     1139        int           *dims;
     1140        char           namei[161];
     1141        char           cstr[81];
     1142        GenericOption<Options*> *ocell      = NULL;
     1143        Option        *option     = NULL;
     1144        const mwSize  *ipt        = NULL;
     1145        const mxArray *celli;
     1146        mwIndex        cindex;
     1147
     1148        /*check and parse the name  */
     1149        ocell=new GenericOption<Options*>();
     1150        ocell->name =xNew<char>(strlen(name)+1);
     1151        memcpy(ocell->name,name,(strlen(name)+1)*sizeof(char));
     1152
     1153        /*check and parse the value  */
     1154        if (!mxIsClass(prhs[0],"cell")){
     1155                _error_("Value of option \"" << ocell->name  << "\" must be class \"cell\", not class \"" << mxGetClassName(prhs[0]) <<"\".");
     1156        }
     1157
     1158        ocell->numel=mxGetNumberOfElements(prhs[0]);
     1159        ocell->ndims=mxGetNumberOfDimensions(prhs[0]);
     1160        ipt         =mxGetDimensions(prhs[0]);
     1161        ocell->size =xNew<int>(ocell->ndims);
     1162        for (i=0; i<ocell->ndims; i++) ocell->size[i]=(int)ipt[i];
     1163        ocell->value=new Options;
     1164
     1165        /*loop through and process each element of the cell array  */
     1166        dims=xNew<int>(ocell->ndims);
     1167        for (cindex=0; cindex<ocell->numel; cindex++) {
     1168                ColumnWiseDimsFromIndex(dims,(int)cindex,ocell->size,ocell->ndims);
     1169                StringFromDims(cstr,dims,ocell->ndims);
     1170                #ifdef _INTEL_WIN_
     1171                        _snprintf(namei,161,"%s%s",name,cstr);
     1172                #else
     1173                        snprintf(namei,161,"%s%s",name,cstr);
     1174                #endif
     1175                celli=mxGetCell(prhs[0],cindex);
     1176
     1177                option=(Option*)OptionParse(namei,&celli);
     1178                ocell->value->AddObject((Object*)option);
     1179                option=NULL;
     1180        }
     1181        xDelete<int>(dims);
     1182
     1183        return(ocell);
     1184}/*}}}*/
     1185Option* OptionParse(char* name, const mxArray* prhs[]){ /*{{{*/
     1186
     1187        Option  *option = NULL;
     1188        mxArray *lhs[1];
     1189
     1190        /*parse the value according to the matlab data type  */
     1191        if     (mxIsClass(prhs[0],"double")  && (mxGetNumberOfElements(prhs[0])==1))
     1192         option=(Option*)OptionDoubleParse(name,prhs);
     1193        else if(mxIsClass(prhs[0],"double")  && (mxGetNumberOfElements(prhs[0])!=1))
     1194         option=(Option*)OptionDoubleArrayParse(name,prhs);
     1195        else if(mxIsClass(prhs[0],"logical"))
     1196         option=(Option*)OptionLogicalParse(name,prhs);
     1197        else if(mxIsClass(prhs[0],"char"))
     1198         option=(Option*)OptionCharParse(name,prhs);
     1199        else if(mxIsClass(prhs[0],"struct"))
     1200         option=(Option*)OptionStructParse(name,prhs);
     1201        else if(mxIsClass(prhs[0],"cell"))
     1202         option=(Option*)OptionCellParse(name,prhs);
     1203        else {
     1204                _printf0_("  Converting value of option \"" << name << "\" from unrecognized class \"" << mxGetClassName(prhs[0]) << "\" to class \"" << "struct" << "\".\n");
     1205                if (!mexCallMATLAB(1,lhs,1,(mxArray**)prhs,"struct")) {
     1206                        option=(Option*)OptionStructParse(name,(const mxArray**)lhs);
     1207                        mxDestroyArray(lhs[0]);
     1208                }
     1209                else _error_("Second argument value of option \""<< name <<"\" is of unrecognized class \""<< mxGetClassName(prhs[0]) <<"\".");
     1210        }
     1211
     1212        return(option);
     1213}/*}}}*/
  • issm/trunk-jpl/src/wrappers/matlab/io/matlabio.h

    r15335 r15520  
    3939void FetchData(bool** pmatrix,int* pM,int *pN,const mxArray* dataref);
    4040void FetchData(bool** pmatrix,int* pnumel,int* pndims,int** psize,const mxArray* dataref);
    41 void FetchData(Matrix<double>** pmatrix,const mxArray* dataref);
    4241void FetchData(int** pvector,int* pM,const mxArray* dataref);
    4342void FetchData(float** pvector,int* pM,const mxArray* dataref);
    4443void FetchData(double** pvector,int* pM,const mxArray* dataref);
    4544void FetchData(bool** pvector,int* pM,const mxArray* dataref);
    46 void FetchData(Vector<double>** pvector,const mxArray* dataref);
    4745void FetchData(char** pstring,const mxArray* dataref);
    4846void FetchData(char** pmatrix,int* pnumel,int* pndims,int** psize,const mxArray* dataref);
     
    7371int CheckNumMatlabArguments(int nlhs,int NLHS, int nrhs,int NRHS, const char* THISFUNCTION, void (*function)( void ));
    7472
    75 /*Matlab to Matrix routines: */
    76 Matrix<double>* MatlabMatrixToMatrix(const mxArray* mxmatrix);
    77 Vector<double>* MatlabVectorToVector(const mxArray* mxvector);
    78 
    7973/*Matlab to double* routines: */
    80 int MatlabVectorToDoubleVector(double** pvector,int* pvector_rows,const mxArray* mxvector);
    8174int MatlabMatrixToDoubleMatrix(double** pmatrix,int* pmatrix_rows,int* pmatrix_cols,const mxArray* mxmatrix);
    8275int MatlabNArrayToNArray(double** pmatrix,int* pmatrix_numel,int* pmatrix_ndims,int** pmatrix_size,const mxArray* mxmatrix);
     
    8477int MatlabNArrayToNArray(char** pmatrix,int* pmatrix_numel,int* pmatrix_ndims,int** pmatrix_size,const mxArray* mxmatrix);
    8578
    86 /*Matlab to IssmDenseMat routines: */
    87 IssmMat<double>* MatlabMatrixToIssmMat(const mxArray* dataref);
    88 IssmVec<double>* MatlabVectorToIssmVec(const mxArray* dataref);
    89 
    90 /*Matlab to Petsc routines: */
    91 #ifdef _HAVE_PETSC_
    92 int MatlabMatrixToPetscMat(Mat* matrix,int* prows,int* pcols, const mxArray* mxmatrix);
    93 PetscMat* MatlabMatrixToPetscMat(const mxArray* mxmatrix);
    94 int MatlabVectorToPetscVec(Vec* pvector,int* pvector_rows,const mxArray* mxvector);
    95 PetscVec* MatlabVectorToPetscVec(const mxArray* mxvector);
    96 #endif
    97 
    9879/*Print*/
    9980void ApiPrintf(const char* string);
Note: See TracChangeset for help on using the changeset viewer.