Actual source code: binaryMatlab.c

petsc-3.3-p7 2013-05-11
  1: /* ----------------------------------------------------------------------
  2:  * Ethan Coon <ecoon@ldeo.columbia.edu> and Richard Katz <richard.katz@earth.ox.ac.uk>
  3:  *
  4:  *        This is a library of functions to write .info files with matlab code
  5:  *      for interpreting PETSc binary files.
  6:  *
  7:  *        Note all "name" and "DMDAFieldName" 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 <petscdmda.h>

 16: /*@C
 17:   PetscViewerBinaryMatlabOpen - Open a binary viewer and write matlab info file initialization.
 18:   This class of viewer writes matlab code to the .info file associated with the binary output file.
 19:   Executing the matlab code with bin/matlab/PetscReadBinaryMatlab.m loads the output into a
 20:   matlab data structure.

 22:   Collective on MPI_Comm

 24:   Input Parameters:
 25: + comm - The communicator
 26: - fname - The binary output filename

 28:   Output Parameter:
 29: . viewer - The viewer object

 31:   Level: beginner

 33:    Question: Why do the following methods exist? Why can you not just do VecView() and PetscBagView() with this viewer
 34:    (that is, why is polymorphism used to implement these things).

 36: .seealso: PetscViewerBinaryMatlabDestroy(), PetscViewerBinaryMatlabOutputVec(),
 37:           PetscViewerBinaryMatlabOutputVecDA(), PetscViewerBinaryMatlabOutputBag(), PetscViewerBinaryOpen()
 38: @*/
 39: PetscErrorCode PetscViewerBinaryMatlabOpen(MPI_Comm comm, const char fname[], PetscViewer *viewer)
 40: {
 41:   FILE          *info;

 45:   PetscViewerBinaryOpen(comm,fname,FILE_MODE_WRITE,viewer);
 46:   PetscViewerBinaryGetInfoPointer(*viewer,&info);
 47:   PetscFPrintf(comm,info,"%%--- begin code written by PetscViewerBinaryMatlabOpen ---%\n");
 48:   PetscFPrintf(comm,info,"%%$$ Set.filename = '%s';\n",fname);
 49:   PetscFPrintf(comm,info,"%%$$ fd = PetscOpenFile(Set.filename);\n");
 50:   PetscFPrintf(comm,info,"%%--- end code written by PetscViewerBinaryMatlabOpen ---%\n\n");
 51:   return(0);
 52: }

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

 57:   Not Collective

 59:   Input Parameter:
 60: . viewer - The viewer object

 62:   Level: beginner

 64: .seealso PetscViewerBinaryMatlabOpen(), PetscViewerBinaryMatlabOutputVec(),
 65:          PetscViewerBinaryMatlabOutputVecDA(), PetscViewerBinaryMatlabOutputBag()
 66: @*/
 69: PetscErrorCode PetscViewerBinaryMatlabDestroy(PetscViewer *viewer)
 70: {
 71:   FILE          *info;
 72:   MPI_Comm       comm;

 76:   PetscObjectGetComm((PetscObject)*viewer,&comm);
 77:   PetscViewerBinaryGetInfoPointer(*viewer,&info);
 78:   PetscFPrintf(comm,info,"%%--- begin code written by PetscViewerBinaryMatlabDestroy ---%\n");
 79:   PetscFPrintf(comm,info,"%%$$ close(fd);\n");
 80:   PetscFPrintf(comm,info,"%%--- end code written by PetscViewerBinaryMatlabDestroy ---%\n\n");
 81:   PetscViewerFlush(*viewer);
 82:   PetscViewerDestroy(viewer);
 83:   return(0);
 84: }

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

 90:   Input Parameters:
 91: + viewer - The viewer object
 92: . name - The bag name
 93: - bag - The bag object containing data to output

 95:   Level: intermediate

 97: .seealso: PetscViewerBinaryMatlabOpen(), PetscViewerBinaryMatlabOutputVec(), PetscViewerBinaryMatlabOutputVecDA()
 98: @*/
101: PetscErrorCode PetscViewerBinaryMatlabOutputBag(PetscViewer viewer, const char name[], PetscBag bag)
102: {
103:   FILE          *info;
104:   MPI_Comm       comm;

108:   PetscObjectGetComm((PetscObject)viewer,&comm);
109:   PetscViewerBinaryGetInfoPointer(viewer,&info);
110:   PetscBagView(bag,viewer);
111:   PetscFPrintf(comm,info,"%%--- begin code written by PetscViewerBinaryMatlabOutputBag ---%\n");
112:   PetscFPrintf(comm,info,"%%$$ Set.%s = PetscBinaryRead(fd);\n",name);
113:   PetscFPrintf(comm,info,"%%--- end code written by PetscViewerBinaryMatlabOutputBag ---%\n\n");
114:   return(0);
115: }
116: 
117: /*@C
118:   PetscViewerBinaryMatlabOutputVec - Output a Vec object to the viewer and write matlab code to
119:   the info file to read a (non-DA) Vec from binary.

121:   Input Parameters:
122: + viewer - The viewer object
123: . name - The name of the field variable to be written
124: - vec -The Vec containing the field data

126:   Level: intermediate

128: .seealso: PetscViewerBinaryMatlabOpen(), PetscViewerBinaryMatlabOutputBag(), PetscViewerBinaryMatlabOutputVecDA()
129: @*/
132: PetscErrorCode PetscViewerBinaryMatlabOutputVec(PetscViewer viewer, const char name[], Vec vec)
133: {
134:   FILE          *info;
135:   MPI_Comm       comm;

139:   PetscObjectGetComm((PetscObject)viewer,&comm);
140:   PetscViewerBinaryGetInfoPointer(viewer,&info);
141:   VecView(vec,viewer);
142:   PetscFPrintf(comm,info,"%%--- begin code written by PetscViewerBinaryMatlabOutputVec ---%\n");
143:   PetscFPrintf(comm,info,"%%$$ Set.%s = PetscBinaryRead(fd);\n",name);
144:   PetscFPrintf(comm,info,"%%--- end code written by PetscViewerBinaryMatlabOutputVec ---%\n\n");
145:   return(0);
146: }

148: /*@C
149:   PetscViewerBinaryMatlabOutputVecDA - Output a Vec object associtated with a DMDA to the viewer and write matlab code
150:   to the info file to read a DMDA's Vec from binary.

152:   Input Parameters:
153: + viewer - The viewer object
154: . name - The name of the field variable to be written
155: . vec - The Vec containing the field data to output
156: - da - The DMDA governing layout of Vec

158:   Level: intermediate

160:   Note: This method requires dof names to have been set using DMDASetFieldName().

162: .seealso: PetscViewerBinaryMatlabOpen(), PetscViewerBinaryMatlabOutputBag(), PetscViewerBinaryMatlabOutputVec(), DMDASetFieldName()
163: @*/
166: PetscErrorCode PetscViewerBinaryMatlabOutputVecDA(PetscViewer viewer, const char name[], Vec vec, DM da)
167: {
168:   MPI_Comm       comm;
169:   FILE          *info;
170:   const char    *fieldname;
171:   PetscInt       dim,ni,nj,nk,pi,pj,pk,dof,n;
172:   PetscBool      flg;

176:   PetscObjectGetComm((PetscObject)viewer,&comm);
177:   PetscViewerBinaryGetInfoPointer(viewer,&info);
178:   DMDAGetInfo(da,&dim,&ni,&nj,&nk,&pi,&pj,&pk,&dof,0,0,0,0,0);
179:   VecView(vec,viewer);
180:   PetscFPrintf(comm,info,"%%--- begin code written by PetscViewerBinaryMatlabOutputVecDA ---%\n");
181:   PetscFPrintf(comm,info,"%%$$ tmp = PetscBinaryRead(fd); \n");
182:   if (dim == 1) { PetscFPrintf(comm,info,"%%$$ tmp = reshape(tmp,%d,%d);\n",dof,ni); }
183:   if (dim == 2) { PetscFPrintf(comm,info,"%%$$ tmp = reshape(tmp,%d,%d,%d);\n",dof,ni,nj); }
184:   if (dim == 3) { PetscFPrintf(comm,info,"%%$$ tmp = reshape(tmp,%d,%d,%d,%d);\n",dof,ni,nj,nk); }

186:   for(n=0; n<dof; n++) {
187:     DMDAGetFieldName(da,n,&fieldname);
188:     PetscStrcmp(fieldname,"",&flg);
189:     if (!flg) {
190:       if (dim == 1) { PetscFPrintf(comm,info,"%%$$ Set.%s.%s = squeeze(tmp(%d,:))';\n",name,fieldname,n+1); }
191:       if (dim == 2) { PetscFPrintf(comm,info,"%%$$ Set.%s.%s = squeeze(tmp(%d,:,:))';\n",name,fieldname,n+1); }
192:       if (dim == 3) { PetscFPrintf(comm,info,"%%$$ Set.%s.%s = permute(squeeze(tmp(%d,:,:,:)),[2 1 3]);\n",name,fieldname,n+1);}
193:     }
194:   }
195:   PetscFPrintf(comm,info,"%%$$ clear tmp; \n");
196:   PetscFPrintf(comm,info,"%%--- end code written by PetscViewerBinaryMatlabOutputVecDA ---%\n\n");
197:   return(0);
198: }