Actual source code: vmatlab.c
2: #include <private/viewerimpl.h>
3: #include <mat.h>
5: /*MC
6: PETSCVIEWERMATLAB - A viewer that saves the variables into a MATLAB .mat file that may be read into MATLAB
7: with load('filename').
9: Level: intermediate
11: Note: Currently can only save PETSc vectors to .mat files, not matrices (use the PETSCVIEWERBINARY and
12: ${PETSC_DIR}/bin/matlab/PetscBinaryRead.m to read matrices into matlab).
14: For parallel vectors obtained with DMCreateGlobalVector() or DMGetGlobalVector() the vectors are saved to
15: the .mat file in natural ordering. You can use DMView() to save the DMDA information to the .mat file
16: the fields in the MATLAB loaded da variable give the array dimensions so you can reshape the MATLAB
17: vector to the same multidimensional shape as it had in PETSc for plotting etc. For example,
19: $ In your PETSc C/C++ code (assuming a two dimensional DMDA with one degree of freedom per node)
20: $ PetscObjectSetName((PetscObject)x,"x");
21: $ VecView(x,PETSC_VIEWER_MATLAB_WORLD);
22: $ PetscObjectSetName((PetscObject)da,"da");
23: $ DMView(x,PETSC_VIEWER_MATLAB_WORLD);
24: $ Then from MATLAB
25: $ load('matlaboutput.mat') % matlaboutput.mat is the default filename
26: $ xnew = zeros(da.n,da.m);
27: $ xnew(:) = x; % reshape one dimensional vector back to two dimensions
29: If you wish to put the same variable into the .mat file several times you need to give it a new
30: name before each call to view.
32: Use PetscViewerMatlabPutArray() to just put an array of doubles into the .mat file
34: .seealso: PETSC_VIEWER_MATLAB_(),PETSC_VIEWER_MATLAB_SELF(), PETSC_VIEWER_MATLAB_WORLD(),PetscViewerCreate(),
35: PetscViewerMatlabOpen(), VecView(), DMView(), PetscViewerMatlabPutArray(), PETSCVIEWERBINARY,
36: PETSC_ASCII_VIEWER, PetscViewerFileSetName(), PetscViewerFileSetMode()
38: M*/
40: typedef struct {
41: MATFile *ep;
42: PetscMPIInt rank;
43: PetscFileMode btype;
44: } PetscViewer_Matlab;
48: /*@C
49: PetscViewerMatlabPutArray - Puts an array into the MATLAB viewer.
51: Not collective: only processor zero saves the array
53: Input Parameters:
54: + mfile - the viewer
55: . m,n - the dimensions of the array
56: . array - the array (represented in one dimension)
57: - name - the name of the array
59: Level: advanced
61: Notes: Only writes array values on processor 0.
63: @*/
64: PetscErrorCode PetscViewerMatlabPutArray(PetscViewer mfile,int m,int n,const PetscScalar *array,const char *name)
65: {
66: PetscErrorCode ierr;
67: PetscViewer_Matlab *ml = (PetscViewer_Matlab*)mfile->data;
68: mxArray *mat;
69:
71: if (!ml->rank) {
72: PetscInfo1(mfile,"Putting MATLAB array %s\n",name);
73: #if !defined(PETSC_USE_COMPLEX)
74: mat = mxCreateDoubleMatrix(m,n,mxREAL);
75: #else
76: mat = mxCreateDoubleMatrix(m,n,mxCOMPLEX);
77: #endif
78: PetscMemcpy(mxGetPr(mat),array,m*n*sizeof(PetscScalar));
79: matPutVariable(ml->ep,name,mat);
81: PetscInfo1(mfile,"Put MATLAB array %s\n",name);
82: }
83: return(0);
84: }
88: PetscErrorCode PetscViewerMatlabPutVariable(PetscViewer viewer,const char* name,void* mat)
89: {
90: PetscViewer_Matlab *ml = (PetscViewer_Matlab*)viewer->data; ;
93: matPutVariable(ml->ep,name,(mxArray*)mat);
94: return(0);
95: }
96:
99: /*@C
100: PetscViewerMatlabGetArray - Gets a variable from a MATLAB viewer into an array
102: Not Collective; only processor zero reads in the array
104: Input Parameters:
105: + mfile - the MATLAB file viewer
106: . m,n - the dimensions of the array
107: . array - the array (represented in one dimension)
108: - name - the name of the array
110: Level: advanced
112: Notes: Only reads in array values on processor 0.
114: @*/
115: PetscErrorCode PetscViewerMatlabGetArray(PetscViewer mfile,int m,int n,PetscScalar *array,const char *name)
116: {
117: PetscErrorCode ierr;
118: PetscViewer_Matlab *ml = (PetscViewer_Matlab*)mfile->data;
119: mxArray *mat;
120:
122: if (!ml->rank) {
123: PetscInfo1(mfile,"Getting MATLAB array %s\n",name);
124: mat = matGetVariable(ml->ep,name);
125: if (!mat) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Unable to get array %s from matlab",name);
126: PetscMemcpy(array,mxGetPr(mat),m*n*sizeof(PetscScalar));
127: PetscInfo1(mfile,"Got MATLAB array %s\n",name);
128: }
129: return(0);
130: }
135: PetscErrorCode PetscViewerFileSetMode_Matlab(PetscViewer viewer,PetscFileMode type)
136: {
137: PetscViewer_Matlab *vmatlab = (PetscViewer_Matlab*)viewer->data;
140: vmatlab->btype = type;
141: return(0);
142: }
145: /*
146: Actually opens the file
147: */
151: PetscErrorCode PetscViewerFileSetName_Matlab(PetscViewer viewer,const char name[])
152: {
153: PetscViewer_Matlab *vmatlab = (PetscViewer_Matlab*)viewer->data;
154: PetscFileMode type = vmatlab->btype;
157: if (type == (PetscFileMode) -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call PetscViewerFileSetMode() before PetscViewerFileSetName()");
159: /* only first processor opens file */
160: if (!vmatlab->rank){
161: if (type == FILE_MODE_READ){
162: vmatlab->ep = matOpen(name,"r");
163: } else if (type == FILE_MODE_WRITE || type == FILE_MODE_WRITE) {
164: vmatlab->ep = matOpen(name,"w");
165: } else {
166: SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Unknown file type");
167: }
168: }
169: return(0);
170: }
175: PetscErrorCode PetscViewerDestroy_Matlab(PetscViewer v)
176: {
177: PetscErrorCode ierr;
178: PetscViewer_Matlab *vf = (PetscViewer_Matlab*)v->data;
181: if (vf->ep) matClose(vf->ep);
182: PetscFree(vf);
183: return(0);
184: }
189: PetscErrorCode PetscViewerCreate_Matlab(PetscViewer viewer)
190: {
191: PetscErrorCode ierr;
192: PetscViewer_Matlab *e;
195: PetscNewLog(viewer,PetscViewer_Matlab,&e);
196: MPI_Comm_rank(((PetscObject)viewer)->comm,&e->rank);
197: e->btype = (PetscFileMode)-1;
198: viewer->data = (void*) e;
199: PetscObjectComposeFunctionDynamic((PetscObject)viewer,"PetscViewerFileSetName_C","PetscViewerFileSetName_Matlab",
200: PetscViewerFileSetName_Matlab);
201: PetscObjectComposeFunctionDynamic((PetscObject)viewer,"PetscViewerFileSetMode_C","PetscViewerFileSetMode_Matlab",
202: PetscViewerFileSetMode_Matlab);
203: viewer->ops->destroy = PetscViewerDestroy_Matlab;
204: return(0);
205: }
210: /*@C
211: PetscViewerMatlabOpen - Opens a Matlab .mat file for output
213: Collective on MPI_Comm
215: Input Parameters:
216: + comm - MPI communicator
217: . name - name of file
218: - type - type of file
219: $ FILE_MODE_WRITE - create new file for MATLAB output
220: $ FILE_MODE_READ - open existing file for MATLAB input
221: $ FILE_MODE_WRITE - open existing file for MATLAB output
223: Output Parameter:
224: . binv - PetscViewer for MATLAB output to use with the specified file
226: Level: beginner
228: Note: This PetscViewer should be destroyed with PetscViewerDestroy().
230: For writing files it only opens the file on processor 0 in the communicator.
232: This only saves Vecs it cannot be used to save Mats. We recommend using the PETSCVIEWERBINARY to save objects to be loaded into MATLAB
233: instead of this routine.
235: Concepts: MATLAB .mat files
236: Concepts: PetscViewerMatlab^creating
238: .seealso: PetscViewerASCIIOpen(), PetscViewerSetFormat(), PetscViewerDestroy(), PETSCVIEWERBINARY, PetscViewerBinaryOpen()
239: VecView(), MatView(), VecLoad(), MatLoad()
240: @*/
241: PetscErrorCode PetscViewerMatlabOpen(MPI_Comm comm,const char name[],PetscFileMode type,PetscViewer *binv)
242: {
244:
246: PetscViewerCreate(comm,binv);
247: PetscViewerSetType(*binv,PETSCVIEWERMATLAB);
248: PetscViewerFileSetMode(*binv,type);
249: PetscViewerFileSetName(*binv,name);
250: return(0);
251: }
253: static PetscMPIInt Petsc_Viewer_Matlab_keyval = MPI_KEYVAL_INVALID;
257: /*@C
258: PETSC_VIEWER_MATLAB_ - Creates a Matlab PetscViewer shared by all processors
259: in a communicator.
261: Collective on MPI_Comm
263: Input Parameter:
264: . comm - the MPI communicator to share the Matlab PetscViewer
265:
266: Level: intermediate
268: Options Database Keys:
269: $ -viewer_matlab_filename <name>
271: Environmental variables:
272: - PETSC_VIEWER_MATLAB_FILENAME
274: Notes:
275: Unlike almost all other PETSc routines, PETSC_VIEWER_MATLAB_ does not return
276: an error code. The matlab PetscViewer is usually used in the form
277: $ XXXView(XXX object,PETSC_VIEWER_MATLAB_(comm));
279: Use PETSC_VIEWER_SOCKET_() or PetscViewerSocketOpen() to communicator with an interactive MATLAB session.
281: .seealso: PETSC_VIEWER_MATLAB_WORLD, PETSC_VIEWER_MATLAB_SELF, PetscViewerMatlabOpen(), PetscViewerCreate(),
282: PetscViewerDestroy()
283: @*/
284: PetscViewer PETSC_VIEWER_MATLAB_(MPI_Comm comm)
285: {
287: PetscBool flg;
288: PetscViewer viewer;
289: char fname[PETSC_MAX_PATH_LEN];
290: MPI_Comm ncomm;
293: PetscCommDuplicate(comm,&ncomm,PETSC_NULL);if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_MATLAB_",__FILE__,__SDIR__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");return(0);}
294: if (Petsc_Viewer_Matlab_keyval == MPI_KEYVAL_INVALID) {
295: MPI_Keyval_create(MPI_NULL_COPY_FN,MPI_NULL_DELETE_FN,&Petsc_Viewer_Matlab_keyval,0);
296: if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_MATLAB_",__FILE__,__SDIR__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");return(0);}
297: }
298: MPI_Attr_get(ncomm,Petsc_Viewer_Matlab_keyval,(void **)&viewer,(int*)&flg);
299: if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_MATLAB_",__FILE__,__SDIR__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");return(0);}
300: if (!flg) { /* PetscViewer not yet created */
301: PetscOptionsGetenv(ncomm,"PETSC_VIEWER_MATLAB_FILENAME",fname,PETSC_MAX_PATH_LEN,&flg);
302: if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_MATLAB_",__FILE__,__SDIR__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");return(0);}
303: if (!flg) {
304: PetscStrcpy(fname,"matlaboutput.mat");
305: if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_MATLAB_",__FILE__,__SDIR__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");return(0);}
306: }
307: PetscViewerMatlabOpen(ncomm,fname,FILE_MODE_WRITE,&viewer);
308: if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_MATLAB_",__FILE__,__SDIR__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");return(0);}
309: PetscObjectRegisterDestroy((PetscObject)viewer);
310: if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_MATLAB_",__FILE__,__SDIR__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");return(0);}
311: MPI_Attr_put(ncomm,Petsc_Viewer_Matlab_keyval,(void*)viewer);
312: if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_MATLAB_",__FILE__,__SDIR__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");return(0);}
313: }
314: PetscCommDestroy(&ncomm);
315: if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_MATLAB_",__FILE__,__SDIR__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");return(0);}
316: PetscFunctionReturn(viewer);
317: }