Actual source code: vmatlab.c

 2:  #include src/sys/src/viewer/viewerimpl.h
  3: #include "mat.h"

  5: /*MC
  6:    PETSC_VIEWER_MATLAB - 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 PETSC_VIEWER_BINARY and 
 12:              ${PETSC_DIR}/bin/matlab/PetscReadBinary.m to read matrices into matlab).

 14:              For parallel vectors obtained with DACreateGlobalVector() or DAGetGlobalVector() the vectors are saved to
 15:              the .mat file in natural ordering. You can use DAView() to save the DA 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 DA 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: $                DAView(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(), DAView(), PetscViewerMatlabPutArray(), PETSC_VIEWER_BINARY,
 36:            PETSC_ASCII_VIEWER, DAView(), PetscViewerSetFilename(), PetscViewerSetFileType()

 38: M*/

 40: typedef struct {
 41:   MATFile             *ep;
 42:   PetscMPIInt         rank;
 43:   PetscViewerFileType 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,PetscScalar *array,char *name)
 65: {
 66:   PetscErrorCode     ierr;
 67:   PetscViewer_Matlab *ml = (PetscViewer_Matlab*)mfile->data;
 68:   mxArray            *mat;
 69: 
 71:   if (!ml->rank) {
 72:     PetscLogInfo(0,"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:     PetscLogInfo(0,"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,char *name)
116: {
117:   PetscErrorCode     ierr;
118:   PetscViewer_Matlab *ml = (PetscViewer_Matlab*)mfile->data;
119:   mxArray            *mat;
120: 
122:   if (!ml->rank) {
123:     PetscLogInfo(0,"Getting Matlab array %s\n",name);
124:     mat  = matGetVariable(ml->ep,name);
125:     if (!mat) SETERRQ1(PETSC_ERR_LIB,"Unable to get array %s from matlab",name);
126:     PetscMemcpy(array,mxGetPr(mat),m*n*sizeof(PetscScalar));
127:     PetscLogInfo(0,"Got Matlab array %s\n",name);
128:   }
129:   return(0);
130: }

135: PetscErrorCode PetscViewerSetFileType_Matlab(PetscViewer viewer,PetscViewerFileType 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 PetscViewerSetFilename_Matlab(PetscViewer viewer,const char name[])
152: {
153:   PetscViewer_Matlab  *vmatlab = (PetscViewer_Matlab*)viewer->data;
154:   PetscViewerFileType type = vmatlab->btype;

157:   if (type == (PetscViewerFileType) -1) {
158:     SETERRQ(PETSC_ERR_ORDER,"Must call PetscViewerSetFileType() before PetscViewerSetFilename()");
159:   }

161:   /* only first processor opens file */
162:   if (!vmatlab->rank){
163:     if (type == PETSC_FILE_RDONLY){
164:       vmatlab->ep = matOpen(name,"r");
165:     } else if (type == PETSC_FILE_CREATE || type == PETSC_FILE_WRONLY) {
166:       vmatlab->ep = matOpen(name,"w");
167:     } else {
168:       SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Unknown file type");
169:     }
170:   }
171:   return(0);
172: }

177: PetscErrorCode PetscViewerDestroy_Matlab(PetscViewer v)
178: {
179:   PetscErrorCode     ierr;
180:   PetscViewer_Matlab *vf = (PetscViewer_Matlab*)v->data;

183:   if (vf->ep) matClose(vf->ep);
184:   PetscFree(vf);
185:   return(0);
186: }

191: PetscErrorCode PetscViewerCreate_Matlab(PetscViewer viewer)
192: {
193:   PetscErrorCode     ierr;
194:   PetscViewer_Matlab *e;

197:   PetscNew(PetscViewer_Matlab,&e);
198:   MPI_Comm_rank(viewer->comm,&e->rank);
199:   e->btype     = (PetscViewerFileType)-1;
200:   viewer->data = (void*) e;
201:   PetscObjectComposeFunctionDynamic((PetscObject)viewer,"PetscViewerSetFilename_C","PetscViewerSetFilename_Matlab",
202:                                      PetscViewerSetFilename_Matlab);
203:   PetscObjectComposeFunctionDynamic((PetscObject)viewer,"PetscViewerSetFileType_C","PetscViewerSetFileType_Matlab",
204:                                      PetscViewerSetFileType_Matlab);
205:   viewer->ops->destroy = PetscViewerDestroy_Matlab;
206:   return(0);
207: }

212: /*@C
213:    PetscViewerMatlabOpen - Opens a Matlab .mat file for input or output.

215:    Collective on MPI_Comm

217:    Input Parameters:
218: +  comm - MPI communicator
219: .  name - name of file 
220: -  type - type of file
221: $    PETSC_FILE_CREATE - create new file for Matlab output
222: $    PETSC_FILE_RDONLY - open existing file for Matlab input
223: $    PETSC_FILE_WRONLY - open existing file for Matlab output

225:    Output Parameter:
226: .  binv - PetscViewer for Matlab input/output to use with the specified file

228:    Level: beginner

230:    Note:
231:    This PetscViewer should be destroyed with PetscViewerDestroy().

233:     For writing files it only opens the file on processor 0 in the communicator.
234:     For readable files it opens the file on all nodes that have the file. If 
235:     node 0 does not have the file it generates an error even if other nodes
236:     do have the file.

238:    Concepts: Matlab .mat files
239:    Concepts: PetscViewerMatlab^creating

241: .seealso: PetscViewerASCIIOpen(), PetscViewerSetFormat(), PetscViewerDestroy(),
242:           VecView(), MatView(), VecLoad(), MatLoad()
243: @*/
244: PetscErrorCode PetscViewerMatlabOpen(MPI_Comm comm,const char name[],PetscViewerFileType type,PetscViewer *binv)
245: {
247: 
249:   PetscViewerCreate(comm,binv);
250:   PetscViewerSetType(*binv,PETSC_VIEWER_MATLAB);
251:   PetscViewerSetFileType(*binv,type);
252:   PetscViewerSetFilename(*binv,name);
253:   return(0);
254: }

256: static PetscMPIInt Petsc_Viewer_Matlab_keyval = MPI_KEYVAL_INVALID;

260: /*@C
261:      PETSC_VIEWER_MATLAB_ - Creates a Matlab PetscViewer shared by all processors 
262:                      in a communicator.

264:      Collective on MPI_Comm

266:      Input Parameter:
267: .    comm - the MPI communicator to share the Matlab PetscViewer
268:     
269:      Level: intermediate

271:    Options Database Keys:
272: $    -viewer_matlab_filename <name>

274:    Environmental variables:
275: -   PETSC_VIEWER_MATLAB_FILENAME

277:      Notes:
278:      Unlike almost all other PETSc routines, PETSC_VIEWER_MATLAB_ does not return 
279:      an error code.  The matlab PetscViewer is usually used in the form
280: $       XXXView(XXX object,PETSC_VIEWER_MATLAB_(comm));

282: .seealso: PETSC_VIEWER_MATLAB_WORLD, PETSC_VIEWER_MATLAB_SELF, PetscViewerMatlabOpen(), PetscViewerCreate(),
283:           PetscViewerDestroy()
284: @*/
285: PetscViewer PETSC_VIEWER_MATLAB_(MPI_Comm comm)
286: {
288:   PetscTruth     flg;
289:   PetscViewer    viewer;
290:   char           fname[PETSC_MAX_PATH_LEN];

293:   if (Petsc_Viewer_Matlab_keyval == MPI_KEYVAL_INVALID) {
294:     MPI_Keyval_create(MPI_NULL_COPY_FN,MPI_NULL_DELETE_FN,&Petsc_Viewer_Matlab_keyval,0);
295:     if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_MATLAB_",__FILE__,__SDIR__,1,1," "); viewer = 0;}
296:   }
297:   MPI_Attr_get(comm,Petsc_Viewer_Matlab_keyval,(void **)&viewer,(int*)&flg);
298:   if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_MATLAB_",__FILE__,__SDIR__,1,1," "); viewer = 0;}
299:   if (!flg) { /* PetscViewer not yet created */
300:     PetscOptionsGetenv(comm,"PETSC_VIEWER_MATLAB_FILENAME",fname,PETSC_MAX_PATH_LEN,&flg);
301:     if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_MATLAB_",__FILE__,__SDIR__,1,1," "); viewer = 0;}
302:     if (!flg) {
303:       PetscStrcpy(fname,"matlaboutput.mat");
304:       if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_MATLAB_",__FILE__,__SDIR__,1,1," "); viewer = 0;}
305:     }
306:     PetscViewerMatlabOpen(comm,fname,PETSC_FILE_CREATE,&viewer);
307:     if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_MATLAB_",__FILE__,__SDIR__,1,1," "); viewer = 0;}
308:     PetscObjectRegisterDestroy((PetscObject)viewer);
309:     if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_MATLAB_",__FILE__,__SDIR__,1,1," "); viewer = 0;}
310:     MPI_Attr_put(comm,Petsc_Viewer_Matlab_keyval,(void*)viewer);
311:     if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_MATLAB_",__FILE__,__SDIR__,1,1," "); viewer = 0;}
312:   }
313:   PetscFunctionReturn(viewer);
314: }