xref: /petsc/src/sys/classes/viewer/impls/matlab/vmatlab.c (revision 9371c9d470a9602b6d10a8bf50c9b2280a79e45a)
15c6c1daeSBarry Smith 
2af0996ceSBarry Smith #include <petsc/private/viewerimpl.h>
35c6c1daeSBarry Smith #include <mat.h>
45c6c1daeSBarry Smith 
55c6c1daeSBarry Smith typedef struct {
65c6c1daeSBarry Smith   MATFile      *ep;
75c6c1daeSBarry Smith   PetscMPIInt   rank;
85c6c1daeSBarry Smith   PetscFileMode btype;
95c6c1daeSBarry Smith } PetscViewer_Matlab;
105c6c1daeSBarry Smith 
115c6c1daeSBarry Smith /*@C
125c6c1daeSBarry Smith     PetscViewerMatlabPutArray - Puts an array into the MATLAB viewer.
135c6c1daeSBarry Smith 
145c6c1daeSBarry Smith       Not collective: only processor zero saves the array
155c6c1daeSBarry Smith 
165c6c1daeSBarry Smith     Input Parameters:
175c6c1daeSBarry Smith +    mfile - the viewer
185c6c1daeSBarry Smith .    m,n - the dimensions of the array
195c6c1daeSBarry Smith .    array - the array (represented in one dimension)
205c6c1daeSBarry Smith -    name - the name of the array
215c6c1daeSBarry Smith 
225c6c1daeSBarry Smith    Level: advanced
235c6c1daeSBarry Smith 
2495452b02SPatrick Sanan      Notes:
2595452b02SPatrick Sanan     Only writes array values on processor 0.
265c6c1daeSBarry Smith 
275c6c1daeSBarry Smith @*/
28*9371c9d4SSatish Balay PetscErrorCode PetscViewerMatlabPutArray(PetscViewer mfile, int m, int n, const PetscScalar *array, const char *name) {
292cb5e1ccSBarry Smith   PetscViewer_Matlab *ml;
305c6c1daeSBarry Smith   mxArray            *mat;
315c6c1daeSBarry Smith 
325c6c1daeSBarry Smith   PetscFunctionBegin;
3328b400f6SJacob Faibussowitsch   PetscCheck(mfile, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL, "Null argument: probably PETSC_VIEWER_MATLAB_() failed");
342cb5e1ccSBarry Smith   ml = (PetscViewer_Matlab *)mfile->data;
355c6c1daeSBarry Smith   if (!ml->rank) {
369566063dSJacob Faibussowitsch     PetscCall(PetscInfo(mfile, "Putting MATLAB array %s\n", name));
375c6c1daeSBarry Smith #if !defined(PETSC_USE_COMPLEX)
385c6c1daeSBarry Smith     mat = mxCreateDoubleMatrix(m, n, mxREAL);
395c6c1daeSBarry Smith #else
405c6c1daeSBarry Smith     mat = mxCreateDoubleMatrix(m, n, mxCOMPLEX);
415c6c1daeSBarry Smith #endif
429566063dSJacob Faibussowitsch     PetscCall(PetscArraycpy(mxGetPr(mat), array, m * n));
435c6c1daeSBarry Smith     matPutVariable(ml->ep, name, mat);
445c6c1daeSBarry Smith 
459566063dSJacob Faibussowitsch     PetscCall(PetscInfo(mfile, "Put MATLAB array %s\n", name));
465c6c1daeSBarry Smith   }
475c6c1daeSBarry Smith   PetscFunctionReturn(0);
485c6c1daeSBarry Smith }
495c6c1daeSBarry Smith 
50*9371c9d4SSatish Balay PetscErrorCode PetscViewerMatlabPutVariable(PetscViewer viewer, const char *name, void *mat) {
51a297a907SKarl Rupp   PetscViewer_Matlab *ml = (PetscViewer_Matlab *)viewer->data;
525c6c1daeSBarry Smith 
535c6c1daeSBarry Smith   PetscFunctionBegin;
545c6c1daeSBarry Smith   matPutVariable(ml->ep, name, (mxArray *)mat);
555c6c1daeSBarry Smith   PetscFunctionReturn(0);
565c6c1daeSBarry Smith }
575c6c1daeSBarry Smith 
585c6c1daeSBarry Smith /*@C
595c6c1daeSBarry Smith     PetscViewerMatlabGetArray - Gets a variable from a MATLAB viewer into an array
605c6c1daeSBarry Smith 
615c6c1daeSBarry Smith     Not Collective; only processor zero reads in the array
625c6c1daeSBarry Smith 
635c6c1daeSBarry Smith     Input Parameters:
645c6c1daeSBarry Smith +    mfile - the MATLAB file viewer
655c6c1daeSBarry Smith .    m,n - the dimensions of the array
665c6c1daeSBarry Smith .    array - the array (represented in one dimension)
675c6c1daeSBarry Smith -    name - the name of the array
685c6c1daeSBarry Smith 
695c6c1daeSBarry Smith    Level: advanced
705c6c1daeSBarry Smith 
7195452b02SPatrick Sanan      Notes:
7295452b02SPatrick Sanan     Only reads in array values on processor 0.
735c6c1daeSBarry Smith 
745c6c1daeSBarry Smith @*/
75*9371c9d4SSatish Balay PetscErrorCode PetscViewerMatlabGetArray(PetscViewer mfile, int m, int n, PetscScalar *array, const char *name) {
762cb5e1ccSBarry Smith   PetscViewer_Matlab *ml;
775c6c1daeSBarry Smith   mxArray            *mat;
785c6c1daeSBarry Smith 
795c6c1daeSBarry Smith   PetscFunctionBegin;
8028b400f6SJacob Faibussowitsch   PetscCheck(mfile, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL, "Null argument: probably PETSC_VIEWER_MATLAB_() failed");
812cb5e1ccSBarry Smith   ml = (PetscViewer_Matlab *)mfile->data;
825c6c1daeSBarry Smith   if (!ml->rank) {
839566063dSJacob Faibussowitsch     PetscCall(PetscInfo(mfile, "Getting MATLAB array %s\n", name));
845c6c1daeSBarry Smith     mat = matGetVariable(ml->ep, name);
8528b400f6SJacob Faibussowitsch     PetscCheck(mat, PETSC_COMM_SELF, PETSC_ERR_LIB, "Unable to get array %s from matlab", name);
869566063dSJacob Faibussowitsch     PetscCall(PetscArraycpy(array, mxGetPr(mat), m * n));
879566063dSJacob Faibussowitsch     PetscCall(PetscInfo(mfile, "Got MATLAB array %s\n", name));
885c6c1daeSBarry Smith   }
895c6c1daeSBarry Smith   PetscFunctionReturn(0);
905c6c1daeSBarry Smith }
915c6c1daeSBarry Smith 
92*9371c9d4SSatish Balay PetscErrorCode PetscViewerFileSetMode_Matlab(PetscViewer viewer, PetscFileMode type) {
935c6c1daeSBarry Smith   PetscViewer_Matlab *vmatlab = (PetscViewer_Matlab *)viewer->data;
945c6c1daeSBarry Smith 
955c6c1daeSBarry Smith   PetscFunctionBegin;
965c6c1daeSBarry Smith   vmatlab->btype = type;
975c6c1daeSBarry Smith   PetscFunctionReturn(0);
985c6c1daeSBarry Smith }
995c6c1daeSBarry Smith 
1005c6c1daeSBarry Smith /*
1015c6c1daeSBarry Smith         Actually opens the file
1025c6c1daeSBarry Smith */
103*9371c9d4SSatish Balay PetscErrorCode PetscViewerFileSetName_Matlab(PetscViewer viewer, const char name[]) {
1045c6c1daeSBarry Smith   PetscViewer_Matlab *vmatlab = (PetscViewer_Matlab *)viewer->data;
1055c6c1daeSBarry Smith   PetscFileMode       type    = vmatlab->btype;
1065c6c1daeSBarry Smith 
1075c6c1daeSBarry Smith   PetscFunctionBegin;
108cc73adaaSBarry Smith   PetscCheck(type != (PetscFileMode)-1, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Must call PetscViewerFileSetMode() before PetscViewerFileSetName()");
1095c6c1daeSBarry Smith   if (vmatlab->ep) matClose(vmatlab->ep);
1105c6c1daeSBarry Smith 
1115c6c1daeSBarry Smith   /* only first processor opens file */
1125c6c1daeSBarry Smith   if (!vmatlab->rank) {
113a297a907SKarl Rupp     if (type == FILE_MODE_READ) vmatlab->ep = matOpen(name, "r");
1147e4fd573SVaclav Hapla     else if (type == FILE_MODE_WRITE) vmatlab->ep = matOpen(name, "w");
115f7d195e4SLawrence Mitchell     else {
116f7d195e4SLawrence Mitchell       PetscCheck(type != FILE_MODE_UNDEFINED, PetscObjectComm((PetscObject)viewer), PETSC_ERR_ORDER, "Must call PetscViewerFileSetMode() before PetscViewerFileSetName()");
117f7d195e4SLawrence Mitchell       SETERRQ(PetscObjectComm((PetscObject)viewer), PETSC_ERR_SUP, "Unsupported file mode %s", PetscFileModes[type]);
118f7d195e4SLawrence Mitchell     }
1195c6c1daeSBarry Smith   }
1205c6c1daeSBarry Smith   PetscFunctionReturn(0);
1215c6c1daeSBarry Smith }
1225c6c1daeSBarry Smith 
123*9371c9d4SSatish Balay PetscErrorCode PetscViewerDestroy_Matlab(PetscViewer v) {
1245c6c1daeSBarry Smith   PetscViewer_Matlab *vf = (PetscViewer_Matlab *)v->data;
1255c6c1daeSBarry Smith 
1265c6c1daeSBarry Smith   PetscFunctionBegin;
1275c6c1daeSBarry Smith   if (vf->ep) matClose(vf->ep);
1289566063dSJacob Faibussowitsch   PetscCall(PetscFree(vf));
1292e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileSetName_C", NULL));
1302e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileSetMode_C", NULL));
1315c6c1daeSBarry Smith   PetscFunctionReturn(0);
1325c6c1daeSBarry Smith }
1335c6c1daeSBarry Smith 
1348556b5ebSBarry Smith /*MC
1358556b5ebSBarry Smith    PETSCVIEWERMATLAB - A viewer that saves the variables into a MATLAB .mat file that may be read into MATLAB
1368556b5ebSBarry Smith        with load('filename').
1378556b5ebSBarry Smith 
1388556b5ebSBarry Smith    Level: intermediate
1398556b5ebSBarry Smith 
1408556b5ebSBarry Smith        Note: Currently can only save PETSc vectors to .mat files, not matrices (use the PETSCVIEWERBINARY and
1418556b5ebSBarry Smith              ${PETSC_DIR}/share/petsc/matlab/PetscBinaryRead.m to read matrices into MATLAB).
1428556b5ebSBarry Smith 
1438556b5ebSBarry Smith              For parallel vectors obtained with DMCreateGlobalVector() or DMGetGlobalVector() the vectors are saved to
1448556b5ebSBarry Smith              the .mat file in natural ordering. You can use DMView() to save the DMDA information to the .mat file
1458556b5ebSBarry Smith              the fields in the MATLAB loaded da variable give the array dimensions so you can reshape the MATLAB
1468556b5ebSBarry Smith              vector to the same multidimensional shape as it had in PETSc for plotting etc. For example,
1478556b5ebSBarry Smith 
1488556b5ebSBarry Smith $             In your PETSc C/C++ code (assuming a two dimensional DMDA with one degree of freedom per node)
1498556b5ebSBarry Smith $                PetscObjectSetName((PetscObject)x,"x");
1508556b5ebSBarry Smith $                VecView(x,PETSC_VIEWER_MATLAB_WORLD);
1518556b5ebSBarry Smith $                PetscObjectSetName((PetscObject)da,"da");
1528556b5ebSBarry Smith $                DMView(x,PETSC_VIEWER_MATLAB_WORLD);
1538556b5ebSBarry Smith $             Then from MATLAB
1548556b5ebSBarry Smith $                load('matlaboutput.mat')   % matlaboutput.mat is the default filename
1558556b5ebSBarry Smith $                xnew = zeros(da.n,da.m);
1568556b5ebSBarry Smith $                xnew(:) = x;    % reshape one dimensional vector back to two dimensions
1578556b5ebSBarry Smith 
1588556b5ebSBarry Smith               If you wish to put the same variable into the .mat file several times you need to give it a new
1598556b5ebSBarry Smith               name before each call to view.
1608556b5ebSBarry Smith 
1618556b5ebSBarry Smith               Use PetscViewerMatlabPutArray() to just put an array of doubles into the .mat file
1628556b5ebSBarry Smith 
163c2e3fba1SPatrick Sanan .seealso: `PETSC_VIEWER_MATLAB_()`, `PETSC_VIEWER_MATLAB_SELF`, `PETSC_VIEWER_MATLAB_WORLD`, `PetscViewerCreate()`,
164db781477SPatrick Sanan           `PetscViewerMatlabOpen()`, `VecView()`, `DMView()`, `PetscViewerMatlabPutArray()`, `PETSCVIEWERBINARY`, `PETSCVIEWERASCII`, `PETSCVIEWERDRAW`,
165db781477SPatrick Sanan           `PETSC_VIEWER_STDOUT_()`, `PetscViewerFileSetName()`, `PetscViewerFileSetMode()`, `PetscViewerFormat`
1668556b5ebSBarry Smith 
1678556b5ebSBarry Smith M*/
168*9371c9d4SSatish Balay PETSC_EXTERN PetscErrorCode PetscViewerCreate_Matlab(PetscViewer viewer) {
1695c6c1daeSBarry Smith   PetscViewer_Matlab *e;
1705c6c1daeSBarry Smith 
1715c6c1daeSBarry Smith   PetscFunctionBegin;
1729566063dSJacob Faibussowitsch   PetscCall(PetscNewLog(viewer, &e));
1739566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)viewer), &e->rank));
1747e4fd573SVaclav Hapla   e->btype     = FILE_MODE_UNDEFINED;
1755c6c1daeSBarry Smith   viewer->data = (void *)e;
176a297a907SKarl Rupp 
1779566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileSetName_C", PetscViewerFileSetName_Matlab));
1789566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileSetMode_C", PetscViewerFileSetMode_Matlab));
179a297a907SKarl Rupp 
1805c6c1daeSBarry Smith   viewer->ops->destroy = PetscViewerDestroy_Matlab;
1815c6c1daeSBarry Smith   PetscFunctionReturn(0);
1825c6c1daeSBarry Smith }
1835c6c1daeSBarry Smith 
1845c6c1daeSBarry Smith /*@C
1855c6c1daeSBarry Smith    PetscViewerMatlabOpen - Opens a Matlab .mat file for output
1865c6c1daeSBarry Smith 
187d083f849SBarry Smith    Collective
1885c6c1daeSBarry Smith 
1895c6c1daeSBarry Smith    Input Parameters:
1905c6c1daeSBarry Smith +  comm - MPI communicator
1915c6c1daeSBarry Smith .  name - name of file
1925c6c1daeSBarry Smith -  type - type of file
1935c6c1daeSBarry Smith $    FILE_MODE_WRITE - create new file for MATLAB output
1945c6c1daeSBarry Smith $    FILE_MODE_READ - open existing file for MATLAB input
1955c6c1daeSBarry Smith $    FILE_MODE_WRITE - open existing file for MATLAB output
1965c6c1daeSBarry Smith 
1975c6c1daeSBarry Smith    Output Parameter:
1985c6c1daeSBarry Smith .  binv - PetscViewer for MATLAB output to use with the specified file
1995c6c1daeSBarry Smith 
2005c6c1daeSBarry Smith    Level: beginner
2015c6c1daeSBarry Smith 
2025c6c1daeSBarry Smith    Note: This PetscViewer should be destroyed with PetscViewerDestroy().
2035c6c1daeSBarry Smith 
2045c6c1daeSBarry Smith     For writing files it only opens the file on processor 0 in the communicator.
2055c6c1daeSBarry Smith 
2065c6c1daeSBarry 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
2075c6c1daeSBarry Smith      instead of this routine.
2085c6c1daeSBarry Smith 
209db781477SPatrick Sanan .seealso: `PetscViewerASCIIOpen()`, `PetscViewerPushFormat()`, `PetscViewerDestroy()`, `PETSCVIEWERBINARY`, `PetscViewerBinaryOpen()`
210db781477SPatrick Sanan           `VecView()`, `MatView()`, `VecLoad()`, `MatLoad()`
2115c6c1daeSBarry Smith @*/
212*9371c9d4SSatish Balay PetscErrorCode PetscViewerMatlabOpen(MPI_Comm comm, const char name[], PetscFileMode type, PetscViewer *binv) {
2135c6c1daeSBarry Smith   PetscFunctionBegin;
2149566063dSJacob Faibussowitsch   PetscCall(PetscViewerCreate(comm, binv));
2159566063dSJacob Faibussowitsch   PetscCall(PetscViewerSetType(*binv, PETSCVIEWERMATLAB));
2169566063dSJacob Faibussowitsch   PetscCall(PetscViewerFileSetMode(*binv, type));
2179566063dSJacob Faibussowitsch   PetscCall(PetscViewerFileSetName(*binv, name));
2185c6c1daeSBarry Smith   PetscFunctionReturn(0);
2195c6c1daeSBarry Smith }
2205c6c1daeSBarry Smith 
2215c6c1daeSBarry Smith static PetscMPIInt Petsc_Viewer_Matlab_keyval = MPI_KEYVAL_INVALID;
2225c6c1daeSBarry Smith 
2235c6c1daeSBarry Smith /*@C
2245c6c1daeSBarry Smith      PETSC_VIEWER_MATLAB_ - Creates a Matlab PetscViewer shared by all processors
2255c6c1daeSBarry Smith                      in a communicator.
2265c6c1daeSBarry Smith 
227d083f849SBarry Smith      Collective
2285c6c1daeSBarry Smith 
2295c6c1daeSBarry Smith      Input Parameter:
2305c6c1daeSBarry Smith .    comm - the MPI communicator to share the Matlab PetscViewer
2315c6c1daeSBarry Smith 
2325c6c1daeSBarry Smith      Level: intermediate
2335c6c1daeSBarry Smith 
2345c6c1daeSBarry Smith    Options Database Keys:
23510699b91SBarry Smith .    -viewer_matlab_filename <name> - name of the Matlab file
2365c6c1daeSBarry Smith 
2375c6c1daeSBarry Smith    Environmental variables:
23810699b91SBarry Smith .   PETSC_VIEWER_MATLAB_FILENAME - name of the Matlab file
2395c6c1daeSBarry Smith 
2405c6c1daeSBarry Smith      Notes:
2415c6c1daeSBarry Smith      Unlike almost all other PETSc routines, PETSC_VIEWER_MATLAB_ does not return
2425c6c1daeSBarry Smith      an error code.  The matlab PetscViewer is usually used in the form
2435c6c1daeSBarry Smith $       XXXView(XXX object,PETSC_VIEWER_MATLAB_(comm));
2445c6c1daeSBarry Smith 
2455c6c1daeSBarry Smith      Use PETSC_VIEWER_SOCKET_() or PetscViewerSocketOpen() to communicator with an interactive MATLAB session.
2465c6c1daeSBarry Smith 
247db781477SPatrick Sanan .seealso: `PETSC_VIEWER_MATLAB_WORLD`, `PETSC_VIEWER_MATLAB_SELF`, `PetscViewerMatlabOpen()`, `PetscViewerCreate()`,
248db781477SPatrick Sanan           `PetscViewerDestroy()`
2495c6c1daeSBarry Smith @*/
250*9371c9d4SSatish Balay PetscViewer PETSC_VIEWER_MATLAB_(MPI_Comm comm) {
2515c6c1daeSBarry Smith   PetscErrorCode ierr;
2525c6c1daeSBarry Smith   PetscBool      flg;
2535c6c1daeSBarry Smith   PetscViewer    viewer;
2545c6c1daeSBarry Smith   char           fname[PETSC_MAX_PATH_LEN];
2555c6c1daeSBarry Smith   MPI_Comm       ncomm;
2565c6c1daeSBarry Smith 
2575c6c1daeSBarry Smith   PetscFunctionBegin;
258*9371c9d4SSatish Balay   ierr = PetscCommDuplicate(comm, &ncomm, NULL);
259*9371c9d4SSatish Balay   if (ierr) {
260*9371c9d4SSatish Balay     PetscError(PETSC_COMM_SELF, __LINE__, "PETSC_VIEWER_MATLAB_", __FILE__, PETSC_ERR_PLIB, PETSC_ERROR_INITIAL, " ");
261*9371c9d4SSatish Balay     PetscFunctionReturn(0);
262*9371c9d4SSatish Balay   }
2635c6c1daeSBarry Smith   if (Petsc_Viewer_Matlab_keyval == MPI_KEYVAL_INVALID) {
26412801b39SBarry Smith     ierr = MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN, MPI_COMM_NULL_DELETE_FN, &Petsc_Viewer_Matlab_keyval, 0);
265*9371c9d4SSatish Balay     if (ierr) {
266*9371c9d4SSatish Balay       PetscError(PETSC_COMM_SELF, __LINE__, "PETSC_VIEWER_MATLAB_", __FILE__, PETSC_ERR_PLIB, PETSC_ERROR_INITIAL, " ");
267*9371c9d4SSatish Balay       PetscFunctionReturn(NULL);
268*9371c9d4SSatish Balay     }
2695c6c1daeSBarry Smith   }
27047435625SJed Brown   ierr = MPI_Comm_get_attr(ncomm, Petsc_Viewer_Matlab_keyval, (void **)&viewer, (int *)&flg);
271*9371c9d4SSatish Balay   if (ierr) {
272*9371c9d4SSatish Balay     PetscError(PETSC_COMM_SELF, __LINE__, "PETSC_VIEWER_MATLAB_", __FILE__, PETSC_ERR_PLIB, PETSC_ERROR_INITIAL, " ");
273*9371c9d4SSatish Balay     PetscFunctionReturn(NULL);
274*9371c9d4SSatish Balay   }
2755c6c1daeSBarry Smith   if (!flg) { /* PetscViewer not yet created */
2765c6c1daeSBarry Smith     ierr = PetscOptionsGetenv(ncomm, "PETSC_VIEWER_MATLAB_FILENAME", fname, PETSC_MAX_PATH_LEN, &flg);
277*9371c9d4SSatish Balay     if (ierr) {
278*9371c9d4SSatish Balay       PetscError(PETSC_COMM_SELF, __LINE__, "PETSC_VIEWER_MATLAB_", __FILE__, PETSC_ERR_PLIB, PETSC_ERROR_REPEAT, " ");
279*9371c9d4SSatish Balay       PetscFunctionReturn(NULL);
280*9371c9d4SSatish Balay     }
2815c6c1daeSBarry Smith     if (!flg) {
2825c6c1daeSBarry Smith       ierr = PetscStrcpy(fname, "matlaboutput.mat");
283*9371c9d4SSatish Balay       if (ierr) {
284*9371c9d4SSatish Balay         PetscError(PETSC_COMM_SELF, __LINE__, "PETSC_VIEWER_MATLAB_", __FILE__, PETSC_ERR_PLIB, PETSC_ERROR_REPEAT, " ");
285*9371c9d4SSatish Balay         PetscFunctionReturn(NULL);
286*9371c9d4SSatish Balay       }
2875c6c1daeSBarry Smith     }
2885c6c1daeSBarry Smith     ierr = PetscViewerMatlabOpen(ncomm, fname, FILE_MODE_WRITE, &viewer);
289*9371c9d4SSatish Balay     if (ierr) {
290*9371c9d4SSatish Balay       PetscError(PETSC_COMM_SELF, __LINE__, "PETSC_VIEWER_MATLAB_", __FILE__, PETSC_ERR_PLIB, PETSC_ERROR_REPEAT, " ");
291*9371c9d4SSatish Balay       PetscFunctionReturn(NULL);
292*9371c9d4SSatish Balay     }
2935c6c1daeSBarry Smith     ierr = PetscObjectRegisterDestroy((PetscObject)viewer);
294*9371c9d4SSatish Balay     if (ierr) {
295*9371c9d4SSatish Balay       PetscError(PETSC_COMM_SELF, __LINE__, "PETSC_VIEWER_MATLAB_", __FILE__, PETSC_ERR_PLIB, PETSC_ERROR_REPEAT, " ");
296*9371c9d4SSatish Balay       PetscFunctionReturn(NULL);
297*9371c9d4SSatish Balay     }
29847435625SJed Brown     ierr = MPI_Comm_set_attr(ncomm, Petsc_Viewer_Matlab_keyval, (void *)viewer);
299*9371c9d4SSatish Balay     if (ierr) {
300*9371c9d4SSatish Balay       PetscError(PETSC_COMM_SELF, __LINE__, "PETSC_VIEWER_MATLAB_", __FILE__, PETSC_ERR_PLIB, PETSC_ERROR_INITIAL, " ");
301*9371c9d4SSatish Balay       PetscFunctionReturn(NULL);
302*9371c9d4SSatish Balay     }
3035c6c1daeSBarry Smith   }
3045c6c1daeSBarry Smith   ierr = PetscCommDestroy(&ncomm);
305*9371c9d4SSatish Balay   if (ierr) {
306*9371c9d4SSatish Balay     PetscError(PETSC_COMM_SELF, __LINE__, "PETSC_VIEWER_MATLAB_", __FILE__, PETSC_ERR_PLIB, PETSC_ERROR_REPEAT, " ");
307*9371c9d4SSatish Balay     PetscFunctionReturn(NULL);
308*9371c9d4SSatish Balay   }
3095c6c1daeSBarry Smith   PetscFunctionReturn(viewer);
3105c6c1daeSBarry Smith }
311