Actual source code: vmatlab.c
2: #include <petsc/private/viewerimpl.h>
3: #include <mat.h>
5: typedef struct {
6: MATFile *ep;
7: PetscMPIInt rank;
8: PetscFileMode btype;
9: } PetscViewer_Matlab;
11: /*@C
12: PetscViewerMatlabPutArray - Puts an array into the MATLAB viewer.
14: Not collective: only processor zero saves the array
16: Input Parameters:
17: + mfile - the viewer
18: . m,n - the dimensions of the array
19: . array - the array (represented in one dimension)
20: - name - the name of the array
22: Level: advanced
24: Notes:
25: Only writes array values on processor 0.
27: @*/
28: PetscErrorCode PetscViewerMatlabPutArray(PetscViewer mfile,int m,int n,const PetscScalar *array,const char *name)
29: {
30: PetscViewer_Matlab *ml;
31: mxArray *mat;
34: ml = (PetscViewer_Matlab*)mfile->data;
35: if (!ml->rank) {
36: PetscInfo(mfile,"Putting MATLAB array %s\n",name);
37: #if !defined(PETSC_USE_COMPLEX)
38: mat = mxCreateDoubleMatrix(m,n,mxREAL);
39: #else
40: mat = mxCreateDoubleMatrix(m,n,mxCOMPLEX);
41: #endif
42: PetscArraycpy(mxGetPr(mat),array,m*n);
43: matPutVariable(ml->ep,name,mat);
45: PetscInfo(mfile,"Put MATLAB array %s\n",name);
46: }
47: return 0;
48: }
50: PetscErrorCode PetscViewerMatlabPutVariable(PetscViewer viewer,const char *name,void *mat)
51: {
52: PetscViewer_Matlab *ml = (PetscViewer_Matlab*)viewer->data;
54: matPutVariable(ml->ep,name,(mxArray*)mat);
55: return 0;
56: }
58: /*@C
59: PetscViewerMatlabGetArray - Gets a variable from a MATLAB viewer into an array
61: Not Collective; only processor zero reads in the array
63: Input Parameters:
64: + mfile - the MATLAB file viewer
65: . m,n - the dimensions of the array
66: . array - the array (represented in one dimension)
67: - name - the name of the array
69: Level: advanced
71: Notes:
72: Only reads in array values on processor 0.
74: @*/
75: PetscErrorCode PetscViewerMatlabGetArray(PetscViewer mfile,int m,int n,PetscScalar *array,const char *name)
76: {
77: PetscViewer_Matlab *ml;
78: mxArray *mat;
81: ml = (PetscViewer_Matlab*)mfile->data;
82: if (!ml->rank) {
83: PetscInfo(mfile,"Getting MATLAB array %s\n",name);
84: mat = matGetVariable(ml->ep,name);
86: PetscArraycpy(array,mxGetPr(mat),m*n);
87: PetscInfo(mfile,"Got MATLAB array %s\n",name);
88: }
89: return 0;
90: }
92: PetscErrorCode PetscViewerFileSetMode_Matlab(PetscViewer viewer,PetscFileMode type)
93: {
94: PetscViewer_Matlab *vmatlab = (PetscViewer_Matlab*)viewer->data;
96: vmatlab->btype = type;
97: return 0;
98: }
100: /*
101: Actually opens the file
102: */
103: PetscErrorCode PetscViewerFileSetName_Matlab(PetscViewer viewer,const char name[])
104: {
105: PetscViewer_Matlab *vmatlab = (PetscViewer_Matlab*)viewer->data;
106: PetscFileMode type = vmatlab->btype;
109: if (vmatlab->ep) matClose(vmatlab->ep);
111: /* only first processor opens file */
112: if (!vmatlab->rank) {
113: if (type == FILE_MODE_READ) vmatlab->ep = matOpen(name,"r");
114: else if (type == FILE_MODE_WRITE) vmatlab->ep = matOpen(name,"w");
116: else SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP, "Unsupported file mode %s",PetscFileModes[type]);
117: }
118: return 0;
119: }
121: PetscErrorCode PetscViewerDestroy_Matlab(PetscViewer v)
122: {
123: PetscViewer_Matlab *vf = (PetscViewer_Matlab*)v->data;
125: if (vf->ep) matClose(vf->ep);
126: PetscFree(vf);
127: return 0;
128: }
130: /*MC
131: PETSCVIEWERMATLAB - A viewer that saves the variables into a MATLAB .mat file that may be read into MATLAB
132: with load('filename').
134: Level: intermediate
136: Note: Currently can only save PETSc vectors to .mat files, not matrices (use the PETSCVIEWERBINARY and
137: ${PETSC_DIR}/share/petsc/matlab/PetscBinaryRead.m to read matrices into MATLAB).
139: For parallel vectors obtained with DMCreateGlobalVector() or DMGetGlobalVector() the vectors are saved to
140: the .mat file in natural ordering. You can use DMView() to save the DMDA information to the .mat file
141: the fields in the MATLAB loaded da variable give the array dimensions so you can reshape the MATLAB
142: vector to the same multidimensional shape as it had in PETSc for plotting etc. For example,
144: $ In your PETSc C/C++ code (assuming a two dimensional DMDA with one degree of freedom per node)
145: $ PetscObjectSetName((PetscObject)x,"x");
146: $ VecView(x,PETSC_VIEWER_MATLAB_WORLD);
147: $ PetscObjectSetName((PetscObject)da,"da");
148: $ DMView(x,PETSC_VIEWER_MATLAB_WORLD);
149: $ Then from MATLAB
150: $ load('matlaboutput.mat') % matlaboutput.mat is the default filename
151: $ xnew = zeros(da.n,da.m);
152: $ xnew(:) = x; % reshape one dimensional vector back to two dimensions
154: If you wish to put the same variable into the .mat file several times you need to give it a new
155: name before each call to view.
157: Use PetscViewerMatlabPutArray() to just put an array of doubles into the .mat file
159: .seealso: PETSC_VIEWER_MATLAB_(),PETSC_VIEWER_MATLAB_SELF, PETSC_VIEWER_MATLAB_WORLD,PetscViewerCreate(),
160: PetscViewerMatlabOpen(), VecView(), DMView(), PetscViewerMatlabPutArray(), PETSCVIEWERBINARY, PETSCVIEWERASCII, PETSCVIEWERDRAW,
161: PETSC_VIEWER_STDOUT_(), PetscViewerFileSetName(), PetscViewerFileSetMode(), PetscViewerFormat
163: M*/
164: PETSC_EXTERN PetscErrorCode PetscViewerCreate_Matlab(PetscViewer viewer)
165: {
166: PetscViewer_Matlab *e;
168: PetscNewLog(viewer,&e);
169: MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&e->rank);
170: e->btype = FILE_MODE_UNDEFINED;
171: viewer->data = (void*) e;
173: PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileSetName_C",PetscViewerFileSetName_Matlab);
174: PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileSetMode_C",PetscViewerFileSetMode_Matlab);
176: viewer->ops->destroy = PetscViewerDestroy_Matlab;
177: return 0;
178: }
180: /*@C
181: PetscViewerMatlabOpen - Opens a Matlab .mat file for output
183: Collective
185: Input Parameters:
186: + comm - MPI communicator
187: . name - name of file
188: - type - type of file
189: $ FILE_MODE_WRITE - create new file for MATLAB output
190: $ FILE_MODE_READ - open existing file for MATLAB input
191: $ FILE_MODE_WRITE - open existing file for MATLAB output
193: Output Parameter:
194: . binv - PetscViewer for MATLAB output to use with the specified file
196: Level: beginner
198: Note: This PetscViewer should be destroyed with PetscViewerDestroy().
200: For writing files it only opens the file on processor 0 in the communicator.
202: This only saves Vecs it cannot be used to save Mats. We recommend using the PETSCVIEWERBINARY to save objects to be loaded into MATLAB
203: instead of this routine.
205: .seealso: PetscViewerASCIIOpen(), PetscViewerPushFormat(), PetscViewerDestroy(), PETSCVIEWERBINARY, PetscViewerBinaryOpen()
206: VecView(), MatView(), VecLoad(), MatLoad()
207: @*/
208: PetscErrorCode PetscViewerMatlabOpen(MPI_Comm comm,const char name[],PetscFileMode type,PetscViewer *binv)
209: {
210: PetscViewerCreate(comm,binv);
211: PetscViewerSetType(*binv,PETSCVIEWERMATLAB);
212: PetscViewerFileSetMode(*binv,type);
213: PetscViewerFileSetName(*binv,name);
214: return 0;
215: }
217: static PetscMPIInt Petsc_Viewer_Matlab_keyval = MPI_KEYVAL_INVALID;
219: /*@C
220: PETSC_VIEWER_MATLAB_ - Creates a Matlab PetscViewer shared by all processors
221: in a communicator.
223: Collective
225: Input Parameter:
226: . comm - the MPI communicator to share the Matlab PetscViewer
228: Level: intermediate
230: Options Database Keys:
231: . -viewer_matlab_filename <name> - name of the Matlab file
233: Environmental variables:
234: . PETSC_VIEWER_MATLAB_FILENAME - name of the Matlab file
236: Notes:
237: Unlike almost all other PETSc routines, PETSC_VIEWER_MATLAB_ does not return
238: an error code. The matlab PetscViewer is usually used in the form
239: $ XXXView(XXX object,PETSC_VIEWER_MATLAB_(comm));
241: Use PETSC_VIEWER_SOCKET_() or PetscViewerSocketOpen() to communicator with an interactive MATLAB session.
243: .seealso: PETSC_VIEWER_MATLAB_WORLD, PETSC_VIEWER_MATLAB_SELF, PetscViewerMatlabOpen(), PetscViewerCreate(),
244: PetscViewerDestroy()
245: @*/
246: PetscViewer PETSC_VIEWER_MATLAB_(MPI_Comm comm)
247: {
249: PetscBool flg;
250: PetscViewer viewer;
251: char fname[PETSC_MAX_PATH_LEN];
252: MPI_Comm ncomm;
254: PetscCommDuplicate(comm,&ncomm,NULL);if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_MATLAB_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");return 0;}
255: if (Petsc_Viewer_Matlab_keyval == MPI_KEYVAL_INVALID) {
256: MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN,MPI_COMM_NULL_DELETE_FN,&Petsc_Viewer_Matlab_keyval,0);
257: if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_MATLAB_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");return NULL;}
258: }
259: MPI_Comm_get_attr(ncomm,Petsc_Viewer_Matlab_keyval,(void**)&viewer,(int*)&flg);
260: if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_MATLAB_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");return NULL;}
261: if (!flg) { /* PetscViewer not yet created */
262: PetscOptionsGetenv(ncomm,"PETSC_VIEWER_MATLAB_FILENAME",fname,PETSC_MAX_PATH_LEN,&flg);
263: if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_MATLAB_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_REPEAT," ");return NULL;}
264: if (!flg) {
265: PetscStrcpy(fname,"matlaboutput.mat");
266: if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_MATLAB_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_REPEAT," ");return NULL;}
267: }
268: PetscViewerMatlabOpen(ncomm,fname,FILE_MODE_WRITE,&viewer);
269: if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_MATLAB_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_REPEAT," ");return NULL;}
270: PetscObjectRegisterDestroy((PetscObject)viewer);
271: if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_MATLAB_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_REPEAT," ");return NULL;}
272: MPI_Comm_set_attr(ncomm,Petsc_Viewer_Matlab_keyval,(void*)viewer);
273: if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_MATLAB_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");return NULL;}
274: }
275: PetscCommDestroy(&ncomm);
276: if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_MATLAB_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_REPEAT," ");return NULL;}
277: return viewer;
278: }