xref: /petsc/src/sys/classes/viewer/impls/matlab/vmatlab.c (revision 20f4b53cbb5e9bd9ef12b76a8697d60d197cda17)
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
12811af0c4SBarry Smith     PetscViewerMatlabPutArray - Puts an array into the `PETSCVIEWERMATLAB` viewer.
135c6c1daeSBarry Smith 
14*20f4b53cSBarry Smith       Not Collective: only processor zero saves `array`
155c6c1daeSBarry Smith 
165c6c1daeSBarry Smith     Input Parameters:
175c6c1daeSBarry Smith +    mfile - the viewer
18*20f4b53cSBarry Smith .    m - the first dimensions of `array`
19*20f4b53cSBarry Smith .    n - the second dimensions of `array`
205c6c1daeSBarry Smith .    array - the array (represented in one dimension)
213f423023SBarry Smith -    name - the MATLAB name of `array`
225c6c1daeSBarry Smith 
235c6c1daeSBarry Smith    Level: advanced
245c6c1daeSBarry Smith 
25811af0c4SBarry Smith     Note:
263f423023SBarry Smith     Only writes `array` values on processor 0.
275c6c1daeSBarry Smith 
28811af0c4SBarry Smith .seealso: `PETSCVIEWERMATLAB`, `PetscViewerMatlabGetArray()`
295c6c1daeSBarry Smith @*/
30d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerMatlabPutArray(PetscViewer mfile, int m, int n, const PetscScalar *array, const char *name)
31d71ae5a4SJacob Faibussowitsch {
322cb5e1ccSBarry Smith   PetscViewer_Matlab *ml;
335c6c1daeSBarry Smith   mxArray            *mat;
345c6c1daeSBarry Smith 
355c6c1daeSBarry Smith   PetscFunctionBegin;
3628b400f6SJacob Faibussowitsch   PetscCheck(mfile, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL, "Null argument: probably PETSC_VIEWER_MATLAB_() failed");
372cb5e1ccSBarry Smith   ml = (PetscViewer_Matlab *)mfile->data;
385c6c1daeSBarry Smith   if (!ml->rank) {
399566063dSJacob Faibussowitsch     PetscCall(PetscInfo(mfile, "Putting MATLAB array %s\n", name));
405c6c1daeSBarry Smith #if !defined(PETSC_USE_COMPLEX)
415c6c1daeSBarry Smith     mat = mxCreateDoubleMatrix(m, n, mxREAL);
425c6c1daeSBarry Smith #else
435c6c1daeSBarry Smith     mat = mxCreateDoubleMatrix(m, n, mxCOMPLEX);
445c6c1daeSBarry Smith #endif
459566063dSJacob Faibussowitsch     PetscCall(PetscArraycpy(mxGetPr(mat), array, m * n));
465c6c1daeSBarry Smith     matPutVariable(ml->ep, name, mat);
475c6c1daeSBarry Smith 
489566063dSJacob Faibussowitsch     PetscCall(PetscInfo(mfile, "Put MATLAB array %s\n", name));
495c6c1daeSBarry Smith   }
503ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
515c6c1daeSBarry Smith }
525c6c1daeSBarry Smith 
53d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerMatlabPutVariable(PetscViewer viewer, const char *name, void *mat)
54d71ae5a4SJacob Faibussowitsch {
55a297a907SKarl Rupp   PetscViewer_Matlab *ml = (PetscViewer_Matlab *)viewer->data;
565c6c1daeSBarry Smith 
575c6c1daeSBarry Smith   PetscFunctionBegin;
585c6c1daeSBarry Smith   matPutVariable(ml->ep, name, (mxArray *)mat);
593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
605c6c1daeSBarry Smith }
615c6c1daeSBarry Smith 
625c6c1daeSBarry Smith /*@C
63811af0c4SBarry Smith     PetscViewerMatlabGetArray - Gets a variable from a `PETSCVIEWERMATLAB` viewer into an array
645c6c1daeSBarry Smith 
655c6c1daeSBarry Smith     Not Collective; only processor zero reads in the array
665c6c1daeSBarry Smith 
675c6c1daeSBarry Smith     Input Parameters:
685c6c1daeSBarry Smith +    mfile - the MATLAB file viewer
69*20f4b53cSBarry Smith .    m - the first dimensions of `array`
70*20f4b53cSBarry Smith .    n - the second dimensions of `array`
715c6c1daeSBarry Smith .    array - the array (represented in one dimension)
723f423023SBarry Smith -    name - the MATLAB name of `array`
735c6c1daeSBarry Smith 
745c6c1daeSBarry Smith    Level: advanced
755c6c1daeSBarry Smith 
76811af0c4SBarry Smith     Note:
773f423023SBarry Smith     Only reads in `array` values on processor 0.
785c6c1daeSBarry Smith 
79811af0c4SBarry Smith .seealso: `PETSCVIEWERMATLAB`, `PetscViewerMatlabPutArray()`
805c6c1daeSBarry Smith @*/
81d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerMatlabGetArray(PetscViewer mfile, int m, int n, PetscScalar *array, const char *name)
82d71ae5a4SJacob Faibussowitsch {
832cb5e1ccSBarry Smith   PetscViewer_Matlab *ml;
845c6c1daeSBarry Smith   mxArray            *mat;
855c6c1daeSBarry Smith 
865c6c1daeSBarry Smith   PetscFunctionBegin;
8728b400f6SJacob Faibussowitsch   PetscCheck(mfile, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL, "Null argument: probably PETSC_VIEWER_MATLAB_() failed");
882cb5e1ccSBarry Smith   ml = (PetscViewer_Matlab *)mfile->data;
895c6c1daeSBarry Smith   if (!ml->rank) {
909566063dSJacob Faibussowitsch     PetscCall(PetscInfo(mfile, "Getting MATLAB array %s\n", name));
915c6c1daeSBarry Smith     mat = matGetVariable(ml->ep, name);
9228b400f6SJacob Faibussowitsch     PetscCheck(mat, PETSC_COMM_SELF, PETSC_ERR_LIB, "Unable to get array %s from matlab", name);
939566063dSJacob Faibussowitsch     PetscCall(PetscArraycpy(array, mxGetPr(mat), m * n));
949566063dSJacob Faibussowitsch     PetscCall(PetscInfo(mfile, "Got MATLAB array %s\n", name));
955c6c1daeSBarry Smith   }
963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
975c6c1daeSBarry Smith }
985c6c1daeSBarry Smith 
99d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerFileSetMode_Matlab(PetscViewer viewer, PetscFileMode type)
100d71ae5a4SJacob Faibussowitsch {
1015c6c1daeSBarry Smith   PetscViewer_Matlab *vmatlab = (PetscViewer_Matlab *)viewer->data;
1025c6c1daeSBarry Smith 
1035c6c1daeSBarry Smith   PetscFunctionBegin;
1045c6c1daeSBarry Smith   vmatlab->btype = type;
1053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1065c6c1daeSBarry Smith }
1075c6c1daeSBarry Smith 
1085c6c1daeSBarry Smith /*
1095c6c1daeSBarry Smith         Actually opens the file
1105c6c1daeSBarry Smith */
111d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerFileSetName_Matlab(PetscViewer viewer, const char name[])
112d71ae5a4SJacob Faibussowitsch {
1135c6c1daeSBarry Smith   PetscViewer_Matlab *vmatlab = (PetscViewer_Matlab *)viewer->data;
1145c6c1daeSBarry Smith   PetscFileMode       type    = vmatlab->btype;
1155c6c1daeSBarry Smith 
1165c6c1daeSBarry Smith   PetscFunctionBegin;
117cc73adaaSBarry Smith   PetscCheck(type != (PetscFileMode)-1, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Must call PetscViewerFileSetMode() before PetscViewerFileSetName()");
1185c6c1daeSBarry Smith   if (vmatlab->ep) matClose(vmatlab->ep);
1195c6c1daeSBarry Smith 
1205c6c1daeSBarry Smith   /* only first processor opens file */
1215c6c1daeSBarry Smith   if (!vmatlab->rank) {
122a297a907SKarl Rupp     if (type == FILE_MODE_READ) vmatlab->ep = matOpen(name, "r");
1237e4fd573SVaclav Hapla     else if (type == FILE_MODE_WRITE) vmatlab->ep = matOpen(name, "w");
124f7d195e4SLawrence Mitchell     else {
125f7d195e4SLawrence Mitchell       PetscCheck(type != FILE_MODE_UNDEFINED, PetscObjectComm((PetscObject)viewer), PETSC_ERR_ORDER, "Must call PetscViewerFileSetMode() before PetscViewerFileSetName()");
126f7d195e4SLawrence Mitchell       SETERRQ(PetscObjectComm((PetscObject)viewer), PETSC_ERR_SUP, "Unsupported file mode %s", PetscFileModes[type]);
127f7d195e4SLawrence Mitchell     }
1285c6c1daeSBarry Smith   }
1293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1305c6c1daeSBarry Smith }
1315c6c1daeSBarry Smith 
132d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerDestroy_Matlab(PetscViewer v)
133d71ae5a4SJacob Faibussowitsch {
1345c6c1daeSBarry Smith   PetscViewer_Matlab *vf = (PetscViewer_Matlab *)v->data;
1355c6c1daeSBarry Smith 
1365c6c1daeSBarry Smith   PetscFunctionBegin;
1375c6c1daeSBarry Smith   if (vf->ep) matClose(vf->ep);
1389566063dSJacob Faibussowitsch   PetscCall(PetscFree(vf));
1392e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileSetName_C", NULL));
1402e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileSetMode_C", NULL));
1413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1425c6c1daeSBarry Smith }
1435c6c1daeSBarry Smith 
1448556b5ebSBarry Smith /*MC
1458556b5ebSBarry Smith    PETSCVIEWERMATLAB - A viewer that saves the variables into a MATLAB .mat file that may be read into MATLAB
1468556b5ebSBarry Smith        with load('filename').
1478556b5ebSBarry Smith 
1488556b5ebSBarry Smith    Level: intermediate
1498556b5ebSBarry Smith 
150b0f901b8SJose E. Roman        Notes:
151b0f901b8SJose E. Roman              Currently can only save PETSc vectors to .mat files, not matrices (use the `PETSCVIEWERBINARY` and
1528556b5ebSBarry Smith              ${PETSC_DIR}/share/petsc/matlab/PetscBinaryRead.m to read matrices into MATLAB).
1538556b5ebSBarry Smith 
154811af0c4SBarry Smith              For parallel vectors obtained with `DMCreateGlobalVector()` or `DMGetGlobalVector()` the vectors are saved to
155811af0c4SBarry Smith              the .mat file in natural ordering. You can use DMView() to save the `DMDA` information to the .mat file
156b0f901b8SJose E. Roman              the fields in the MATLAB loaded da variable give the array dimensions so you can reshape the MATLAB
1578556b5ebSBarry Smith              vector to the same multidimensional shape as it had in PETSc for plotting etc. For example,
1588556b5ebSBarry Smith 
159b0f901b8SJose E. Roman              In your PETSc C/C++ code (assuming a two dimensional `DMDA` with one degree of freedom per node)
160b0f901b8SJose E. Roman .vb
161b0f901b8SJose E. Roman                 PetscObjectSetName((PetscObject)x,"x");
162b0f901b8SJose E. Roman                 VecView(x,PETSC_VIEWER_MATLAB_WORLD);
163b0f901b8SJose E. Roman                 PetscObjectSetName((PetscObject)da,"da");
164b0f901b8SJose E. Roman                 DMView(x,PETSC_VIEWER_MATLAB_WORLD);
165b0f901b8SJose E. Roman .ve
166b0f901b8SJose E. Roman              Then from MATLAB
167b0f901b8SJose E. Roman .vb
168b0f901b8SJose E. Roman                 load('matlaboutput.mat')   % matlaboutput.mat is the default filename
169b0f901b8SJose E. Roman                 xnew = zeros(da.n,da.m);
170b0f901b8SJose E. Roman                 xnew(:) = x;    % reshape one dimensional vector back to two dimensions
171b0f901b8SJose E. Roman .ve
1728556b5ebSBarry Smith 
1738556b5ebSBarry Smith               If you wish to put the same variable into the .mat file several times you need to give it a new
1748556b5ebSBarry Smith               name before each call to view.
1758556b5ebSBarry Smith 
176811af0c4SBarry Smith               Use `PetscViewerMatlabPutArray()` to just put an array of doubles into the .mat file
1778556b5ebSBarry Smith 
178c2e3fba1SPatrick Sanan .seealso: `PETSC_VIEWER_MATLAB_()`, `PETSC_VIEWER_MATLAB_SELF`, `PETSC_VIEWER_MATLAB_WORLD`, `PetscViewerCreate()`,
179db781477SPatrick Sanan           `PetscViewerMatlabOpen()`, `VecView()`, `DMView()`, `PetscViewerMatlabPutArray()`, `PETSCVIEWERBINARY`, `PETSCVIEWERASCII`, `PETSCVIEWERDRAW`,
180811af0c4SBarry Smith           `PETSC_VIEWER_STDOUT_()`, `PetscViewerFileSetName()`, `PetscViewerFileSetMode()`, `PetscViewerFormat`, `PetscMatlabEngine`
1818556b5ebSBarry Smith M*/
182d71ae5a4SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscViewerCreate_Matlab(PetscViewer viewer)
183d71ae5a4SJacob Faibussowitsch {
1845c6c1daeSBarry Smith   PetscViewer_Matlab *e;
1855c6c1daeSBarry Smith 
1865c6c1daeSBarry Smith   PetscFunctionBegin;
1874dfa11a4SJacob Faibussowitsch   PetscCall(PetscNew(&e));
1889566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)viewer), &e->rank));
1897e4fd573SVaclav Hapla   e->btype     = FILE_MODE_UNDEFINED;
1905c6c1daeSBarry Smith   viewer->data = (void *)e;
191a297a907SKarl Rupp 
1929566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileSetName_C", PetscViewerFileSetName_Matlab));
1939566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileSetMode_C", PetscViewerFileSetMode_Matlab));
194a297a907SKarl Rupp 
1955c6c1daeSBarry Smith   viewer->ops->destroy = PetscViewerDestroy_Matlab;
1963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1975c6c1daeSBarry Smith }
1985c6c1daeSBarry Smith 
1995c6c1daeSBarry Smith /*@C
2005c6c1daeSBarry Smith    PetscViewerMatlabOpen - Opens a Matlab .mat file for output
2015c6c1daeSBarry Smith 
202d083f849SBarry Smith    Collective
2035c6c1daeSBarry Smith 
2045c6c1daeSBarry Smith    Input Parameters:
2055c6c1daeSBarry Smith +  comm - MPI communicator
2065c6c1daeSBarry Smith .  name - name of file
2075c6c1daeSBarry Smith -  type - type of file
2083f423023SBarry Smith .vb
2093f423023SBarry Smith     FILE_MODE_WRITE - create new file for MATLAB output
2103f423023SBarry Smith     FILE_MODE_READ - open existing file for MATLAB input
2113f423023SBarry Smith     FILE_MODE_WRITE - open existing file for MATLAB output
2123f423023SBarry Smith .ve
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 
219811af0c4SBarry Smith    Notes:
220811af0c4SBarry Smith    This `PetscViewer` should be destroyed with `PetscViewerDestroy()`.
2215c6c1daeSBarry Smith 
2225c6c1daeSBarry Smith    For writing files it only opens the file on processor 0 in the communicator.
2235c6c1daeSBarry Smith 
224811af0c4SBarry Smith    This only saves `Vec`s it cannot be used to save `Mat`s. We recommend using the `PETSCVIEWERBINARY` to save objects to be loaded into MATLAB
2255c6c1daeSBarry Smith    instead of this routine.
2265c6c1daeSBarry Smith 
2273f423023SBarry Smith    PETSc must be configured with the option `--with-matlab` for this functionality
228750b007cSBarry Smith 
229811af0c4SBarry Smith .seealso: `PETSCVIEWERMATLAB`, `PetscViewerASCIIOpen()`, `PetscViewerPushFormat()`, `PetscViewerDestroy()`, `PETSCVIEWERBINARY`, `PetscViewerBinaryOpen()`
230db781477SPatrick Sanan           `VecView()`, `MatView()`, `VecLoad()`, `MatLoad()`
2315c6c1daeSBarry Smith @*/
232d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerMatlabOpen(MPI_Comm comm, const char name[], PetscFileMode type, PetscViewer *binv)
233d71ae5a4SJacob Faibussowitsch {
2345c6c1daeSBarry Smith   PetscFunctionBegin;
2359566063dSJacob Faibussowitsch   PetscCall(PetscViewerCreate(comm, binv));
2369566063dSJacob Faibussowitsch   PetscCall(PetscViewerSetType(*binv, PETSCVIEWERMATLAB));
2379566063dSJacob Faibussowitsch   PetscCall(PetscViewerFileSetMode(*binv, type));
2389566063dSJacob Faibussowitsch   PetscCall(PetscViewerFileSetName(*binv, name));
2393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2405c6c1daeSBarry Smith }
2415c6c1daeSBarry Smith 
2425c6c1daeSBarry Smith static PetscMPIInt Petsc_Viewer_Matlab_keyval = MPI_KEYVAL_INVALID;
2435c6c1daeSBarry Smith 
2445c6c1daeSBarry Smith /*@C
245811af0c4SBarry Smith      PETSC_VIEWER_MATLAB_ - Creates a `PETSCVIEWERMATLAB` `PetscViewer` shared by all processors
2465c6c1daeSBarry Smith                      in a communicator.
2475c6c1daeSBarry Smith 
248d083f849SBarry Smith      Collective
2495c6c1daeSBarry Smith 
2505c6c1daeSBarry Smith      Input Parameter:
251811af0c4SBarry Smith .    comm - the MPI communicator to share the Matlab `PetscViewer`
252811af0c4SBarry Smith 
253811af0c4SBarry Smith    Options Database Key:
254811af0c4SBarry Smith .    -viewer_matlab_filename <name> - name of the Matlab file
255811af0c4SBarry Smith 
256811af0c4SBarry Smith    Environmental variable:
257811af0c4SBarry Smith .   `PETSC_VIEWER_MATLAB_FILENAME` - name of the Matlab file
2585c6c1daeSBarry Smith 
2595c6c1daeSBarry Smith      Level: intermediate
2605c6c1daeSBarry Smith 
261811af0c4SBarry Smith      Note:
262811af0c4SBarry Smith      Unlike almost all other PETSc routines, `PETSC_VIEWER_MATLAB_()` does not return
2635c6c1daeSBarry Smith      an error code.  The matlab PetscViewer is usually used in the form
2645c6c1daeSBarry Smith $       XXXView(XXX object,PETSC_VIEWER_MATLAB_(comm));
2655c6c1daeSBarry Smith 
266811af0c4SBarry Smith      Use `PETSC_VIEWER_SOCKET_()` or `PetscViewerSocketOpen()` to communicator with an interactive MATLAB session.
2675c6c1daeSBarry Smith 
268db781477SPatrick Sanan .seealso: `PETSC_VIEWER_MATLAB_WORLD`, `PETSC_VIEWER_MATLAB_SELF`, `PetscViewerMatlabOpen()`, `PetscViewerCreate()`,
269db781477SPatrick Sanan           `PetscViewerDestroy()`
2705c6c1daeSBarry Smith @*/
271d71ae5a4SJacob Faibussowitsch PetscViewer PETSC_VIEWER_MATLAB_(MPI_Comm comm)
272d71ae5a4SJacob Faibussowitsch {
2735c6c1daeSBarry Smith   PetscErrorCode ierr;
2745c6c1daeSBarry Smith   PetscBool      flg;
2755c6c1daeSBarry Smith   PetscViewer    viewer;
2765c6c1daeSBarry Smith   char           fname[PETSC_MAX_PATH_LEN];
2775c6c1daeSBarry Smith   MPI_Comm       ncomm;
2785c6c1daeSBarry Smith 
2795c6c1daeSBarry Smith   PetscFunctionBegin;
2809371c9d4SSatish Balay   ierr = PetscCommDuplicate(comm, &ncomm, NULL);
2819371c9d4SSatish Balay   if (ierr) {
2829371c9d4SSatish Balay     PetscError(PETSC_COMM_SELF, __LINE__, "PETSC_VIEWER_MATLAB_", __FILE__, PETSC_ERR_PLIB, PETSC_ERROR_INITIAL, " ");
283ac016777SBarry Smith     PetscFunctionReturn(NULL);
2849371c9d4SSatish Balay   }
2855c6c1daeSBarry Smith   if (Petsc_Viewer_Matlab_keyval == MPI_KEYVAL_INVALID) {
28612801b39SBarry Smith     ierr = MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN, MPI_COMM_NULL_DELETE_FN, &Petsc_Viewer_Matlab_keyval, 0);
2879371c9d4SSatish Balay     if (ierr) {
2889371c9d4SSatish Balay       PetscError(PETSC_COMM_SELF, __LINE__, "PETSC_VIEWER_MATLAB_", __FILE__, PETSC_ERR_PLIB, PETSC_ERROR_INITIAL, " ");
2899371c9d4SSatish Balay       PetscFunctionReturn(NULL);
2909371c9d4SSatish Balay     }
2915c6c1daeSBarry Smith   }
29247435625SJed Brown   ierr = MPI_Comm_get_attr(ncomm, Petsc_Viewer_Matlab_keyval, (void **)&viewer, (int *)&flg);
2939371c9d4SSatish Balay   if (ierr) {
2949371c9d4SSatish Balay     PetscError(PETSC_COMM_SELF, __LINE__, "PETSC_VIEWER_MATLAB_", __FILE__, PETSC_ERR_PLIB, PETSC_ERROR_INITIAL, " ");
2959371c9d4SSatish Balay     PetscFunctionReturn(NULL);
2969371c9d4SSatish Balay   }
2975c6c1daeSBarry Smith   if (!flg) { /* PetscViewer not yet created */
2985c6c1daeSBarry Smith     ierr = PetscOptionsGetenv(ncomm, "PETSC_VIEWER_MATLAB_FILENAME", fname, PETSC_MAX_PATH_LEN, &flg);
2999371c9d4SSatish Balay     if (ierr) {
3009371c9d4SSatish Balay       PetscError(PETSC_COMM_SELF, __LINE__, "PETSC_VIEWER_MATLAB_", __FILE__, PETSC_ERR_PLIB, PETSC_ERROR_REPEAT, " ");
3019371c9d4SSatish Balay       PetscFunctionReturn(NULL);
3029371c9d4SSatish Balay     }
3035c6c1daeSBarry Smith     if (!flg) {
304c6a7a370SJeremy L Thompson       ierr = PetscStrncpy(fname, "matlaboutput.mat", sizeof(fname));
3059371c9d4SSatish Balay       if (ierr) {
3069371c9d4SSatish Balay         PetscError(PETSC_COMM_SELF, __LINE__, "PETSC_VIEWER_MATLAB_", __FILE__, PETSC_ERR_PLIB, PETSC_ERROR_REPEAT, " ");
3079371c9d4SSatish Balay         PetscFunctionReturn(NULL);
3089371c9d4SSatish Balay       }
3095c6c1daeSBarry Smith     }
3105c6c1daeSBarry Smith     ierr = PetscViewerMatlabOpen(ncomm, fname, FILE_MODE_WRITE, &viewer);
3119371c9d4SSatish Balay     if (ierr) {
3129371c9d4SSatish Balay       PetscError(PETSC_COMM_SELF, __LINE__, "PETSC_VIEWER_MATLAB_", __FILE__, PETSC_ERR_PLIB, PETSC_ERROR_REPEAT, " ");
3139371c9d4SSatish Balay       PetscFunctionReturn(NULL);
3149371c9d4SSatish Balay     }
3155c6c1daeSBarry Smith     ierr = PetscObjectRegisterDestroy((PetscObject)viewer);
3169371c9d4SSatish Balay     if (ierr) {
3179371c9d4SSatish Balay       PetscError(PETSC_COMM_SELF, __LINE__, "PETSC_VIEWER_MATLAB_", __FILE__, PETSC_ERR_PLIB, PETSC_ERROR_REPEAT, " ");
3189371c9d4SSatish Balay       PetscFunctionReturn(NULL);
3199371c9d4SSatish Balay     }
32047435625SJed Brown     ierr = MPI_Comm_set_attr(ncomm, Petsc_Viewer_Matlab_keyval, (void *)viewer);
3219371c9d4SSatish Balay     if (ierr) {
3229371c9d4SSatish Balay       PetscError(PETSC_COMM_SELF, __LINE__, "PETSC_VIEWER_MATLAB_", __FILE__, PETSC_ERR_PLIB, PETSC_ERROR_INITIAL, " ");
3239371c9d4SSatish Balay       PetscFunctionReturn(NULL);
3249371c9d4SSatish Balay     }
3255c6c1daeSBarry Smith   }
3265c6c1daeSBarry Smith   ierr = PetscCommDestroy(&ncomm);
3279371c9d4SSatish Balay   if (ierr) {
3289371c9d4SSatish Balay     PetscError(PETSC_COMM_SELF, __LINE__, "PETSC_VIEWER_MATLAB_", __FILE__, PETSC_ERR_PLIB, PETSC_ERROR_REPEAT, " ");
3299371c9d4SSatish Balay     PetscFunctionReturn(NULL);
3309371c9d4SSatish Balay   }
3315c6c1daeSBarry Smith   PetscFunctionReturn(viewer);
3325c6c1daeSBarry Smith }
333