xref: /petsc/src/sys/classes/viewer/impls/matlab/vmatlab.c (revision 8cc058d9cd56c1ccb3be12a47760ddfc446aaffc)
15c6c1daeSBarry Smith 
25c6c1daeSBarry Smith #include <petsc-private/viewerimpl.h>
35c6c1daeSBarry Smith #include <mat.h>
45c6c1daeSBarry Smith 
55c6c1daeSBarry Smith /*MC
65c6c1daeSBarry Smith    PETSCVIEWERMATLAB - A viewer that saves the variables into a MATLAB .mat file that may be read into MATLAB
75c6c1daeSBarry Smith        with load('filename').
85c6c1daeSBarry Smith 
95c6c1daeSBarry Smith    Level: intermediate
105c6c1daeSBarry Smith 
115c6c1daeSBarry Smith        Note: Currently can only save PETSc vectors to .mat files, not matrices (use the PETSCVIEWERBINARY and
125c6c1daeSBarry Smith              ${PETSC_DIR}/bin/matlab/PetscBinaryRead.m to read matrices into matlab).
135c6c1daeSBarry Smith 
145c6c1daeSBarry Smith              For parallel vectors obtained with DMCreateGlobalVector() or DMGetGlobalVector() the vectors are saved to
155c6c1daeSBarry Smith              the .mat file in natural ordering. You can use DMView() to save the DMDA information to the .mat file
165c6c1daeSBarry Smith              the fields in the MATLAB loaded da variable give the array dimensions so you can reshape the MATLAB
175c6c1daeSBarry Smith              vector to the same multidimensional shape as it had in PETSc for plotting etc. For example,
185c6c1daeSBarry Smith 
195c6c1daeSBarry Smith $             In your PETSc C/C++ code (assuming a two dimensional DMDA with one degree of freedom per node)
205c6c1daeSBarry Smith $                PetscObjectSetName((PetscObject)x,"x");
215c6c1daeSBarry Smith $                VecView(x,PETSC_VIEWER_MATLAB_WORLD);
225c6c1daeSBarry Smith $                PetscObjectSetName((PetscObject)da,"da");
235c6c1daeSBarry Smith $                DMView(x,PETSC_VIEWER_MATLAB_WORLD);
245c6c1daeSBarry Smith $             Then from MATLAB
255c6c1daeSBarry Smith $                load('matlaboutput.mat')   % matlaboutput.mat is the default filename
265c6c1daeSBarry Smith $                xnew = zeros(da.n,da.m);
275c6c1daeSBarry Smith $                xnew(:) = x;    % reshape one dimensional vector back to two dimensions
285c6c1daeSBarry Smith 
295c6c1daeSBarry Smith               If you wish to put the same variable into the .mat file several times you need to give it a new
305c6c1daeSBarry Smith               name before each call to view.
315c6c1daeSBarry Smith 
325c6c1daeSBarry Smith               Use PetscViewerMatlabPutArray() to just put an array of doubles into the .mat file
335c6c1daeSBarry Smith 
345c6c1daeSBarry Smith .seealso:  PETSC_VIEWER_MATLAB_(),PETSC_VIEWER_MATLAB_SELF(), PETSC_VIEWER_MATLAB_WORLD(),PetscViewerCreate(),
355c6c1daeSBarry Smith            PetscViewerMatlabOpen(), VecView(), DMView(), PetscViewerMatlabPutArray(), PETSCVIEWERBINARY,
365c6c1daeSBarry Smith            PETSC_ASCII_VIEWER, PetscViewerFileSetName(), PetscViewerFileSetMode()
375c6c1daeSBarry Smith 
385c6c1daeSBarry Smith M*/
395c6c1daeSBarry Smith 
405c6c1daeSBarry Smith typedef struct {
415c6c1daeSBarry Smith   MATFile       *ep;
425c6c1daeSBarry Smith   PetscMPIInt   rank;
435c6c1daeSBarry Smith   PetscFileMode btype;
445c6c1daeSBarry Smith } PetscViewer_Matlab;
455c6c1daeSBarry Smith 
465c6c1daeSBarry Smith #undef __FUNCT__
475c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerMatlabPutArray"
485c6c1daeSBarry Smith /*@C
495c6c1daeSBarry Smith     PetscViewerMatlabPutArray - Puts an array into the MATLAB viewer.
505c6c1daeSBarry Smith 
515c6c1daeSBarry Smith       Not collective: only processor zero saves the array
525c6c1daeSBarry Smith 
535c6c1daeSBarry Smith     Input Parameters:
545c6c1daeSBarry Smith +    mfile - the viewer
555c6c1daeSBarry Smith .    m,n - the dimensions of the array
565c6c1daeSBarry Smith .    array - the array (represented in one dimension)
575c6c1daeSBarry Smith -    name - the name of the array
585c6c1daeSBarry Smith 
595c6c1daeSBarry Smith    Level: advanced
605c6c1daeSBarry Smith 
615c6c1daeSBarry Smith      Notes: Only writes array values on processor 0.
625c6c1daeSBarry Smith 
635c6c1daeSBarry Smith @*/
645c6c1daeSBarry Smith PetscErrorCode  PetscViewerMatlabPutArray(PetscViewer mfile,int m,int n,const PetscScalar *array,const char *name)
655c6c1daeSBarry Smith {
665c6c1daeSBarry Smith   PetscErrorCode     ierr;
675c6c1daeSBarry Smith   PetscViewer_Matlab *ml = (PetscViewer_Matlab*)mfile->data;
685c6c1daeSBarry Smith   mxArray            *mat;
695c6c1daeSBarry Smith 
705c6c1daeSBarry Smith   PetscFunctionBegin;
715c6c1daeSBarry Smith   if (!ml->rank) {
725c6c1daeSBarry Smith     ierr = PetscInfo1(mfile,"Putting MATLAB array %s\n",name);CHKERRQ(ierr);
735c6c1daeSBarry Smith #if !defined(PETSC_USE_COMPLEX)
745c6c1daeSBarry Smith     mat  = mxCreateDoubleMatrix(m,n,mxREAL);
755c6c1daeSBarry Smith #else
765c6c1daeSBarry Smith     mat  = mxCreateDoubleMatrix(m,n,mxCOMPLEX);
775c6c1daeSBarry Smith #endif
785c6c1daeSBarry Smith     ierr = PetscMemcpy(mxGetPr(mat),array,m*n*sizeof(PetscScalar));CHKERRQ(ierr);
795c6c1daeSBarry Smith     matPutVariable(ml->ep,name,mat);
805c6c1daeSBarry Smith 
815c6c1daeSBarry Smith     ierr = PetscInfo1(mfile,"Put MATLAB array %s\n",name);CHKERRQ(ierr);
825c6c1daeSBarry Smith   }
835c6c1daeSBarry Smith   PetscFunctionReturn(0);
845c6c1daeSBarry Smith }
855c6c1daeSBarry Smith 
865c6c1daeSBarry Smith #undef __FUNCT__
875c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerMatlabPutVariable"
885c6c1daeSBarry Smith PetscErrorCode  PetscViewerMatlabPutVariable(PetscViewer viewer,const char *name,void *mat)
895c6c1daeSBarry Smith {
90a297a907SKarl Rupp   PetscViewer_Matlab *ml = (PetscViewer_Matlab*)viewer->data;
915c6c1daeSBarry Smith 
925c6c1daeSBarry Smith   PetscFunctionBegin;
935c6c1daeSBarry Smith   matPutVariable(ml->ep,name,(mxArray*)mat);
945c6c1daeSBarry Smith   PetscFunctionReturn(0);
955c6c1daeSBarry Smith }
965c6c1daeSBarry Smith 
975c6c1daeSBarry Smith #undef __FUNCT__
985c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerMatlabGetArray"
995c6c1daeSBarry Smith /*@C
1005c6c1daeSBarry Smith     PetscViewerMatlabGetArray - Gets a variable from a MATLAB viewer into an array
1015c6c1daeSBarry Smith 
1025c6c1daeSBarry Smith     Not Collective; only processor zero reads in the array
1035c6c1daeSBarry Smith 
1045c6c1daeSBarry Smith     Input Parameters:
1055c6c1daeSBarry Smith +    mfile - the MATLAB file viewer
1065c6c1daeSBarry Smith .    m,n - the dimensions of the array
1075c6c1daeSBarry Smith .    array - the array (represented in one dimension)
1085c6c1daeSBarry Smith -    name - the name of the array
1095c6c1daeSBarry Smith 
1105c6c1daeSBarry Smith    Level: advanced
1115c6c1daeSBarry Smith 
1125c6c1daeSBarry Smith      Notes: Only reads in array values on processor 0.
1135c6c1daeSBarry Smith 
1145c6c1daeSBarry Smith @*/
1155c6c1daeSBarry Smith PetscErrorCode  PetscViewerMatlabGetArray(PetscViewer mfile,int m,int n,PetscScalar *array,const char *name)
1165c6c1daeSBarry Smith {
1175c6c1daeSBarry Smith   PetscErrorCode     ierr;
1185c6c1daeSBarry Smith   PetscViewer_Matlab *ml = (PetscViewer_Matlab*)mfile->data;
1195c6c1daeSBarry Smith   mxArray            *mat;
1205c6c1daeSBarry Smith 
1215c6c1daeSBarry Smith   PetscFunctionBegin;
1225c6c1daeSBarry Smith   if (!ml->rank) {
1235c6c1daeSBarry Smith     ierr = PetscInfo1(mfile,"Getting MATLAB array %s\n",name);CHKERRQ(ierr);
1245c6c1daeSBarry Smith     mat  = matGetVariable(ml->ep,name);
1255c6c1daeSBarry Smith     if (!mat) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Unable to get array %s from matlab",name);
1265c6c1daeSBarry Smith     ierr = PetscMemcpy(array,mxGetPr(mat),m*n*sizeof(PetscScalar));CHKERRQ(ierr);
1275c6c1daeSBarry Smith     ierr = PetscInfo1(mfile,"Got MATLAB array %s\n",name);CHKERRQ(ierr);
1285c6c1daeSBarry Smith   }
1295c6c1daeSBarry Smith   PetscFunctionReturn(0);
1305c6c1daeSBarry Smith }
1315c6c1daeSBarry Smith 
1325c6c1daeSBarry Smith #undef __FUNCT__
1335c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileSetMode_Matlab"
1345c6c1daeSBarry Smith PetscErrorCode  PetscViewerFileSetMode_Matlab(PetscViewer viewer,PetscFileMode type)
1355c6c1daeSBarry Smith {
1365c6c1daeSBarry Smith   PetscViewer_Matlab *vmatlab = (PetscViewer_Matlab*)viewer->data;
1375c6c1daeSBarry Smith 
1385c6c1daeSBarry Smith   PetscFunctionBegin;
1395c6c1daeSBarry Smith   vmatlab->btype = type;
1405c6c1daeSBarry Smith   PetscFunctionReturn(0);
1415c6c1daeSBarry Smith }
1425c6c1daeSBarry Smith 
1435c6c1daeSBarry Smith /*
1445c6c1daeSBarry Smith         Actually opens the file
1455c6c1daeSBarry Smith */
1465c6c1daeSBarry Smith #undef __FUNCT__
1475c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileSetName_Matlab"
1485c6c1daeSBarry Smith PetscErrorCode  PetscViewerFileSetName_Matlab(PetscViewer viewer,const char name[])
1495c6c1daeSBarry Smith {
1505c6c1daeSBarry Smith   PetscViewer_Matlab *vmatlab = (PetscViewer_Matlab*)viewer->data;
1515c6c1daeSBarry Smith   PetscFileMode      type     = vmatlab->btype;
1525c6c1daeSBarry Smith 
1535c6c1daeSBarry Smith   PetscFunctionBegin;
1545c6c1daeSBarry Smith   if (type == (PetscFileMode) -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call PetscViewerFileSetMode() before PetscViewerFileSetName()");
1555c6c1daeSBarry Smith   if (vmatlab->ep) matClose(vmatlab->ep);
1565c6c1daeSBarry Smith 
1575c6c1daeSBarry Smith   /* only first processor opens file */
1585c6c1daeSBarry Smith   if (!vmatlab->rank) {
159a297a907SKarl Rupp     if (type == FILE_MODE_READ) vmatlab->ep = matOpen(name,"r");
160a297a907SKarl Rupp     else if (type == FILE_MODE_WRITE || type == FILE_MODE_WRITE) vmatlab->ep = matOpen(name,"w");
161a297a907SKarl Rupp     else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Unknown file type");
1625c6c1daeSBarry Smith   }
1635c6c1daeSBarry Smith   PetscFunctionReturn(0);
1645c6c1daeSBarry Smith }
1655c6c1daeSBarry Smith 
1665c6c1daeSBarry Smith #undef __FUNCT__
1675c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerDestroy_Matlab"
1685c6c1daeSBarry Smith PetscErrorCode PetscViewerDestroy_Matlab(PetscViewer v)
1695c6c1daeSBarry Smith {
1705c6c1daeSBarry Smith   PetscErrorCode     ierr;
1715c6c1daeSBarry Smith   PetscViewer_Matlab *vf = (PetscViewer_Matlab*)v->data;
1725c6c1daeSBarry Smith 
1735c6c1daeSBarry Smith   PetscFunctionBegin;
1745c6c1daeSBarry Smith   if (vf->ep) matClose(vf->ep);
1755c6c1daeSBarry Smith   ierr = PetscFree(vf);CHKERRQ(ierr);
1765c6c1daeSBarry Smith   PetscFunctionReturn(0);
1775c6c1daeSBarry Smith }
1785c6c1daeSBarry Smith 
1795c6c1daeSBarry Smith #undef __FUNCT__
1805c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerCreate_Matlab"
181*8cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PetscViewerCreate_Matlab(PetscViewer viewer)
1825c6c1daeSBarry Smith {
1835c6c1daeSBarry Smith   PetscErrorCode     ierr;
1845c6c1daeSBarry Smith   PetscViewer_Matlab *e;
1855c6c1daeSBarry Smith 
1865c6c1daeSBarry Smith   PetscFunctionBegin;
1875c6c1daeSBarry Smith   ierr         = PetscNewLog(viewer,PetscViewer_Matlab,&e);CHKERRQ(ierr);
188ce94432eSBarry Smith   ierr         = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&e->rank);CHKERRQ(ierr);
1895c6c1daeSBarry Smith   e->btype     = (PetscFileMode)-1;
1905c6c1daeSBarry Smith   viewer->data = (void*) e;
191a297a907SKarl Rupp 
19200de8ff0SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileSetName_C","PetscViewerFileSetName_Matlab",PetscViewerFileSetName_Matlab);CHKERRQ(ierr);
19300de8ff0SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileSetMode_C","PetscViewerFileSetMode_Matlab",PetscViewerFileSetMode_Matlab);CHKERRQ(ierr);
194a297a907SKarl Rupp 
1955c6c1daeSBarry Smith   viewer->ops->destroy = PetscViewerDestroy_Matlab;
1965c6c1daeSBarry Smith   PetscFunctionReturn(0);
1975c6c1daeSBarry Smith }
1985c6c1daeSBarry Smith 
1995c6c1daeSBarry Smith #undef __FUNCT__
2005c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerMatlabOpen"
2015c6c1daeSBarry Smith /*@C
2025c6c1daeSBarry Smith    PetscViewerMatlabOpen - Opens a Matlab .mat file for output
2035c6c1daeSBarry Smith 
2045c6c1daeSBarry Smith    Collective on MPI_Comm
2055c6c1daeSBarry Smith 
2065c6c1daeSBarry Smith    Input Parameters:
2075c6c1daeSBarry Smith +  comm - MPI communicator
2085c6c1daeSBarry Smith .  name - name of file
2095c6c1daeSBarry Smith -  type - type of file
2105c6c1daeSBarry Smith $    FILE_MODE_WRITE - create new file for MATLAB output
2115c6c1daeSBarry Smith $    FILE_MODE_READ - open existing file for MATLAB input
2125c6c1daeSBarry Smith $    FILE_MODE_WRITE - open existing file for MATLAB output
2135c6c1daeSBarry Smith 
2145c6c1daeSBarry Smith    Output Parameter:
2155c6c1daeSBarry Smith .  binv - PetscViewer for MATLAB output to use with the specified file
2165c6c1daeSBarry Smith 
2175c6c1daeSBarry Smith    Level: beginner
2185c6c1daeSBarry Smith 
2195c6c1daeSBarry Smith    Note: This PetscViewer should be destroyed with PetscViewerDestroy().
2205c6c1daeSBarry Smith 
2215c6c1daeSBarry Smith     For writing files it only opens the file on processor 0 in the communicator.
2225c6c1daeSBarry Smith 
2235c6c1daeSBarry Smith      This only saves Vecs it cannot be used to save Mats. We recommend using the PETSCVIEWERBINARY to save objects to be loaded into MATLAB
2245c6c1daeSBarry Smith      instead of this routine.
2255c6c1daeSBarry Smith 
2265c6c1daeSBarry Smith    Concepts: MATLAB .mat files
2275c6c1daeSBarry Smith    Concepts: PetscViewerMatlab^creating
2285c6c1daeSBarry Smith 
2295c6c1daeSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerSetFormat(), PetscViewerDestroy(), PETSCVIEWERBINARY, PetscViewerBinaryOpen()
2305c6c1daeSBarry Smith           VecView(), MatView(), VecLoad(), MatLoad()
2315c6c1daeSBarry Smith @*/
2325c6c1daeSBarry Smith PetscErrorCode  PetscViewerMatlabOpen(MPI_Comm comm,const char name[],PetscFileMode type,PetscViewer *binv)
2335c6c1daeSBarry Smith {
2345c6c1daeSBarry Smith   PetscErrorCode ierr;
2355c6c1daeSBarry Smith 
2365c6c1daeSBarry Smith   PetscFunctionBegin;
2375c6c1daeSBarry Smith   ierr = PetscViewerCreate(comm,binv);CHKERRQ(ierr);
2385c6c1daeSBarry Smith   ierr = PetscViewerSetType(*binv,PETSCVIEWERMATLAB);CHKERRQ(ierr);
2395c6c1daeSBarry Smith   ierr = PetscViewerFileSetMode(*binv,type);CHKERRQ(ierr);
2405c6c1daeSBarry Smith   ierr = PetscViewerFileSetName(*binv,name);CHKERRQ(ierr);
2415c6c1daeSBarry Smith   PetscFunctionReturn(0);
2425c6c1daeSBarry Smith }
2435c6c1daeSBarry Smith 
2445c6c1daeSBarry Smith static PetscMPIInt Petsc_Viewer_Matlab_keyval = MPI_KEYVAL_INVALID;
2455c6c1daeSBarry Smith 
2465c6c1daeSBarry Smith #undef __FUNCT__
2475c6c1daeSBarry Smith #define __FUNCT__ "PETSC_VIEWER_MATLAB_"
2485c6c1daeSBarry Smith /*@C
2495c6c1daeSBarry Smith      PETSC_VIEWER_MATLAB_ - Creates a Matlab PetscViewer shared by all processors
2505c6c1daeSBarry Smith                      in a communicator.
2515c6c1daeSBarry Smith 
2525c6c1daeSBarry Smith      Collective on MPI_Comm
2535c6c1daeSBarry Smith 
2545c6c1daeSBarry Smith      Input Parameter:
2555c6c1daeSBarry Smith .    comm - the MPI communicator to share the Matlab PetscViewer
2565c6c1daeSBarry Smith 
2575c6c1daeSBarry Smith      Level: intermediate
2585c6c1daeSBarry Smith 
2595c6c1daeSBarry Smith    Options Database Keys:
2605c6c1daeSBarry Smith $    -viewer_matlab_filename <name>
2615c6c1daeSBarry Smith 
2625c6c1daeSBarry Smith    Environmental variables:
2635c6c1daeSBarry Smith -   PETSC_VIEWER_MATLAB_FILENAME
2645c6c1daeSBarry Smith 
2655c6c1daeSBarry Smith      Notes:
2665c6c1daeSBarry Smith      Unlike almost all other PETSc routines, PETSC_VIEWER_MATLAB_ does not return
2675c6c1daeSBarry Smith      an error code.  The matlab PetscViewer is usually used in the form
2685c6c1daeSBarry Smith $       XXXView(XXX object,PETSC_VIEWER_MATLAB_(comm));
2695c6c1daeSBarry Smith 
2705c6c1daeSBarry Smith      Use PETSC_VIEWER_SOCKET_() or PetscViewerSocketOpen() to communicator with an interactive MATLAB session.
2715c6c1daeSBarry Smith 
2725c6c1daeSBarry Smith .seealso: PETSC_VIEWER_MATLAB_WORLD, PETSC_VIEWER_MATLAB_SELF, PetscViewerMatlabOpen(), PetscViewerCreate(),
2735c6c1daeSBarry Smith           PetscViewerDestroy()
2745c6c1daeSBarry Smith @*/
2755c6c1daeSBarry Smith PetscViewer  PETSC_VIEWER_MATLAB_(MPI_Comm comm)
2765c6c1daeSBarry Smith {
2775c6c1daeSBarry Smith   PetscErrorCode ierr;
2785c6c1daeSBarry Smith   PetscBool      flg;
2795c6c1daeSBarry Smith   PetscViewer    viewer;
2805c6c1daeSBarry Smith   char           fname[PETSC_MAX_PATH_LEN];
2815c6c1daeSBarry Smith   MPI_Comm       ncomm;
2825c6c1daeSBarry Smith 
2835c6c1daeSBarry Smith   PetscFunctionBegin;
2840298fd71SBarry Smith   ierr = PetscCommDuplicate(comm,&ncomm,NULL);if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_MATLAB_",__FILE__,__SDIR__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
2855c6c1daeSBarry Smith   if (Petsc_Viewer_Matlab_keyval == MPI_KEYVAL_INVALID) {
2865c6c1daeSBarry Smith     ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,MPI_NULL_DELETE_FN,&Petsc_Viewer_Matlab_keyval,0);
2875c6c1daeSBarry Smith     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_MATLAB_",__FILE__,__SDIR__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
2885c6c1daeSBarry Smith   }
2895c6c1daeSBarry Smith   ierr = MPI_Attr_get(ncomm,Petsc_Viewer_Matlab_keyval,(void**)&viewer,(int*)&flg);
2905c6c1daeSBarry Smith   if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_MATLAB_",__FILE__,__SDIR__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
2915c6c1daeSBarry Smith   if (!flg) { /* PetscViewer not yet created */
2925c6c1daeSBarry Smith     ierr = PetscOptionsGetenv(ncomm,"PETSC_VIEWER_MATLAB_FILENAME",fname,PETSC_MAX_PATH_LEN,&flg);
2935c6c1daeSBarry Smith     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_MATLAB_",__FILE__,__SDIR__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
2945c6c1daeSBarry Smith     if (!flg) {
2955c6c1daeSBarry Smith       ierr = PetscStrcpy(fname,"matlaboutput.mat");
2965c6c1daeSBarry Smith       if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_MATLAB_",__FILE__,__SDIR__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
2975c6c1daeSBarry Smith     }
2985c6c1daeSBarry Smith     ierr = PetscViewerMatlabOpen(ncomm,fname,FILE_MODE_WRITE,&viewer);
2995c6c1daeSBarry Smith     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_MATLAB_",__FILE__,__SDIR__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
3005c6c1daeSBarry Smith     ierr = PetscObjectRegisterDestroy((PetscObject)viewer);
3015c6c1daeSBarry Smith     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_MATLAB_",__FILE__,__SDIR__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
3025c6c1daeSBarry Smith     ierr = MPI_Attr_put(ncomm,Petsc_Viewer_Matlab_keyval,(void*)viewer);
3035c6c1daeSBarry Smith     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_MATLAB_",__FILE__,__SDIR__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
3045c6c1daeSBarry Smith   }
3055c6c1daeSBarry Smith   ierr = PetscCommDestroy(&ncomm);
3065c6c1daeSBarry Smith   if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_MATLAB_",__FILE__,__SDIR__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
3075c6c1daeSBarry Smith   PetscFunctionReturn(viewer);
3085c6c1daeSBarry Smith }
3095c6c1daeSBarry Smith 
3105c6c1daeSBarry Smith 
3115c6c1daeSBarry Smith 
3125c6c1daeSBarry Smith 
3135c6c1daeSBarry Smith 
314