Actual source code: binaryMatlab.c

  1: /* ----------------------------------------------------------------------
  2:  * Ethan Coon <ecoon@ldeo.columbia.edu> and Richard Katz <katz@ldeo.columbia.edu>
  3:  *
  4:  *        This is a library of functions to write .info files with matlab code
  5:  *      for interpreting various PETSc binary files.
  6:  *
  7:  *        Note all "name" and "DAFieldName" variables must be Matlab-Kosher
  8:  *        i.e. no whitespace or illegal characters such as grouping 
  9:  *        operators, quotations, math/boolean operators, etc. 
 10:  * ----------------------------------------------------------------------*/
 11: #include <petscviewer.h>
 12: #include <petscda.h>

 14: /* ---------------------------------------------------------------------
 15:  *  PetscViewerBinaryMatlabOpen
 16:  *        
 17:  *                Input
 18:  *                ------------------------------
 19:  *                comm        | mpi communicator
 20:  *                fname        | filename
 21:  *                viewer        | petsc viewer object
 22:  *                
 23:  * ----------------------------------------------------------------------*/

 27: /*@C
 28:   PetscViewerBinaryMatlabOpen - Open a binary viewer and write matlab info file initialization.
 29:   This class of viewer writes matlab code to the .info file associated with the binary output file.
 30:   Executing the matlab code with bin/matlab/PetscReadBinaryMatlab.m loads the output into a
 31:   matlab data structure.

 33:   Collective on MPI_Comm

 35:   Input Parameters:
 36: + comm - The communicator
 37: - fname - The binary output filename

 39:   Output Parameter:
 40: . viewer - The viewer object

 42:   Level: beginner

 44: .seealso: PetscViewerBinaryMatlabDestroy(), PetscViewerBinaryMatlabOutputVec(),
 45:           PetscViewerBinaryMatlabOutputVecDA(), PetscViewerBinaryMatlabOutputBag()
 46: @*/
 47: PetscErrorCode PetscViewerBinaryMatlabOpen(MPI_Comm comm, const char fname[], PetscViewer *viewer)
 48: {
 49:   FILE          *info;

 53:   PetscViewerBinaryOpen(comm,fname,FILE_MODE_WRITE,viewer);
 54:   PetscViewerBinaryGetInfoPointer(*viewer,&info);
 55:   PetscFPrintf(comm,info,"%%--- begin code written by PetscViewerBinaryMatlabOpen ---%\n");
 56:   PetscFPrintf(comm,info,"%%$$ Set.filename = '%s';\n",fname);
 57:   PetscFPrintf(comm,info,"%%$$ fd = fopen(Set.filename, 'r', 'ieee-be');\n");
 58:   PetscFPrintf(comm,info,"%%$$ if (fd < 0) error('Cannot open %s, check for existence of file'); end\n",fname);
 59:   PetscFPrintf(comm,info,"%%--- end code written by PetscViewerBinaryMatlabOpen ---%\n\n");
 60:   return(0);
 61: }

 63: /*@C
 64:   PetscViewerBinaryMatlabDestroy - Write matlab info file finalization and destroy viewer.

 66:   Not Collective

 68:   Input Parameter:
 69: . viewer - The viewer object

 71:   Level: beginner

 73: .seealso PetscViewerBinaryMatlabOpen(), PetscViewerBinaryMatlabOutputVec(),
 74:          PetscViewerBinaryMatlabOutputVecDA(), PetscViewerBinaryMatlabOutputBag()
 75: @*/
 78: PetscErrorCode PetscViewerBinaryMatlabDestroy(PetscViewer viewer)
 79: {
 80:   FILE          *info;
 81:   MPI_Comm       comm;

 85:   PetscObjectGetComm((PetscObject)viewer,&comm);
 86:   PetscViewerBinaryGetInfoPointer(viewer,&info);
 87:   PetscFPrintf(comm,info,"%%--- begin code written by PetscViewerBinaryMatlabDestroy ---%\n");
 88:   PetscFPrintf(comm,info,"%%$$ fclose(fd);\n");
 89:   PetscFPrintf(comm,info,"%%--- end code written by PetscViewerBinaryMatlabDestroy ---%\n\n");
 90:   PetscViewerFlush(viewer);
 91:   PetscViewerDestroy(viewer);
 92:   return(0);
 93: }

 95: /*@C
 96:   PetscViewerBinaryMatlabOutputBag - Output a bag object to the viewer and write matlab code to the
 97:   info file to read a PetscBag from binary.

 99:   Input Parameters:
100: + viewer - The viewer object
101: . name - The bag name
102: - bag - The bag object containing data to output

104:   Level: intermediate

106: .seealso: PetscViewerBinaryMatlabOpen(), PetscViewerBinaryMatlabOutputVec(), PetscViewerBinaryMatlabOutputVecDA()
107: @*/
110: PetscErrorCode PetscViewerBinaryMatlabOutputBag(PetscViewer viewer, const char name[], PetscBag bag)
111: {
112:   FILE          *info;
113:   MPI_Comm       comm;

117:   PetscObjectGetComm((PetscObject)viewer,&comm);
118:   PetscViewerBinaryGetInfoPointer(viewer,&info);
119:   PetscBagView(bag,viewer);
120:   PetscFPrintf(comm,info,"%%--- begin code written by PetscViewerBinaryMatlabOutputBag ---%\n");
121:   PetscFPrintf(comm,info,"%%$$ Set.%s = PetscBinaryRead(fd);\n",name);
122:   PetscFPrintf(comm,info,"%%--- end code written by PetscViewerBinaryMatlabOutputBag ---%\n\n");
123:   return(0);
124: }
125: 
126: /*@C
127:   PetscViewerBinaryMatlabOutputVec - Output a Vec object to the viewer and write matlab code to
128:   the info file to read a (non-DA) Vec from binary.

130:   Input Parameters:
131: + viewer - The viewer object
132: . name - The name of the field variable to be written
133: - vec -The Vec containing the field data

135:   Level: intermediate

137: .seealso: PetscViewerBinaryMatlabOpen(), PetscViewerBinaryMatlabOutputBag(), PetscViewerBinaryMatlabOutputVecDA()
138: @*/
141: PetscErrorCode PetscViewerBinaryMatlabOutputVec(PetscViewer viewer, const char name[], Vec vec)
142: {
143:   FILE          *info;
144:   MPI_Comm       comm;

148:   PetscObjectGetComm((PetscObject)viewer,&comm);
149:   PetscViewerBinaryGetInfoPointer(viewer,&info);
150:   VecView(vec,viewer);
151:   PetscFPrintf(comm,info,"%%--- begin code written by PetscViewerBinaryMatlabOutputVec ---%\n");
152:   PetscFPrintf(comm,info,"%%$$ Set.%s = PetscBinaryRead(fd);\n",name);
153:   PetscFPrintf(comm,info,"%%--- end code written by PetscViewerBinaryMatlabOutputVec ---%\n\n");
154:   return(0);
155: }

157: /*@C
158:   PetscViewerBinaryMatlabOutputVecDA - Output a Vec object associtated with a DA to the viewer and write matlab code
159:   to the info file to read a DA's Vec from binary.

161:   Input Parameters:
162: + viewer - The viewer object
163: . name - The name of the field variable to be written
164: . vec - The Vec containing the field data to output
165: - da - The DA governing layout of Vec

167:   Level: intermediate

169:   Note: This method requires dof names to have been set using DASetFieldName().

171: .seealso: PetscViewerBinaryMatlabOpen(), PetscViewerBinaryMatlabOutputBag(), PetscViewerBinaryMatlabOutputVec(), DASetFieldName()
172: @*/
175: PetscErrorCode PetscViewerBinaryMatlabOutputVecDA(PetscViewer viewer, const char name[], Vec vec, DA da)
176: {
177:   MPI_Comm       comm;
178:   FILE          *info;
179:   char          *fieldname;
180:   PetscInt       dim,ni,nj,nk,pi,pj,pk,dof,n;
181:   PetscTruth     flg;

185:   PetscObjectGetComm((PetscObject)viewer,&comm);
186:   PetscViewerBinaryGetInfoPointer(viewer,&info);
187:   DAGetInfo(da,&dim,&ni,&nj,&nk,&pi,&pj,&pk,&dof,PETSC_NULL,PETSC_NULL,PETSC_NULL);
188:   VecView(vec,viewer);
189:   PetscFPrintf(comm,info,"%%--- begin code written by PetscViewerBinaryMatlabOutputVecDA ---%\n");
190:   PetscFPrintf(comm,info,"%%$$ tmp = PetscBinaryRead(fd); \n");
191:   if (dim == 1) { PetscFPrintf(comm,info,"%%$$ tmp = reshape(tmp,%d,%d);\n",dof,ni); }
192:   if (dim == 2) { PetscFPrintf(comm,info,"%%$$ tmp = reshape(tmp,%d,%d,%d);\n",dof,ni,nj); }
193:   if (dim == 3) { PetscFPrintf(comm,info,"%%$$ tmp = reshape(tmp,%d,%d,%d,%d);\n",dof,ni,nj,nk); }

195:   for(n=0; n<dof; n++) {
196:     DAGetFieldName(da,n,&fieldname);
197:     PetscStrcmp(fieldname,"",&flg);
198:     if (!flg) {
199:       if (dim == 1) { PetscFPrintf(comm,info,"%%$$ Set.%s.%s = squeeze(tmp(%d,:))';\n",name,fieldname,n+1); }
200:       if (dim == 2) { PetscFPrintf(comm,info,"%%$$ Set.%s.%s = squeeze(tmp(%d,:,:))';\n",name,fieldname,n+1); }
201:       if (dim == 3) { PetscFPrintf(comm,info,"%%$$ Set.%s.%s = permute(squeeze(tmp(%d,:,:,:)),[2 1 3]);\n",name,fieldname,n+1);}
202:     }
203:   }
204:   PetscFPrintf(comm,info,"%%$$ clear tmp; \n");
205:   PetscFPrintf(comm,info,"%%--- end code written by PetscViewerBinaryMatlabOutputVecDA ---%\n\n");
206:   return(0);
207: }