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>
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 = fopen(Set.filename, 'r', 'ieee-be');\n");
50: PetscFPrintf(comm,info,"%%$$ if (fd < 0) error('Cannot open %s, check for existence of file'); end\n",fname);
51: PetscFPrintf(comm,info,"%%--- end code written by PetscViewerBinaryMatlabOpen ---%\n\n");
52: return(0);
53: }
55: /*@C
56: PetscViewerBinaryMatlabDestroy - Write matlab info file finalization and destroy viewer.
58: Not Collective
60: Input Parameter:
61: . viewer - The viewer object
63: Level: beginner
65: .seealso PetscViewerBinaryMatlabOpen(), PetscViewerBinaryMatlabOutputVec(),
66: PetscViewerBinaryMatlabOutputVecDA(), PetscViewerBinaryMatlabOutputBag()
67: @*/
70: PetscErrorCode PetscViewerBinaryMatlabDestroy(PetscViewer viewer)
71: {
72: FILE *info;
73: MPI_Comm comm;
77: PetscObjectGetComm((PetscObject)viewer,&comm);
78: PetscViewerBinaryGetInfoPointer(viewer,&info);
79: PetscFPrintf(comm,info,"%%--- begin code written by PetscViewerBinaryMatlabDestroy ---%\n");
80: PetscFPrintf(comm,info,"%%$$ fclose(fd);\n");
81: PetscFPrintf(comm,info,"%%--- end code written by PetscViewerBinaryMatlabDestroy ---%\n\n");
82: PetscViewerFlush(viewer);
83: PetscViewerDestroy(viewer);
84: return(0);
85: }
87: /*@C
88: PetscViewerBinaryMatlabOutputBag - Output a bag object to the viewer and write matlab code to the
89: info file to read a PetscBag from binary.
91: Input Parameters:
92: + viewer - The viewer object
93: . name - The bag name
94: - bag - The bag object containing data to output
96: Level: intermediate
98: .seealso: PetscViewerBinaryMatlabOpen(), PetscViewerBinaryMatlabOutputVec(), PetscViewerBinaryMatlabOutputVecDA()
99: @*/
102: PetscErrorCode PetscViewerBinaryMatlabOutputBag(PetscViewer viewer, const char name[], PetscBag bag)
103: {
104: FILE *info;
105: MPI_Comm comm;
109: PetscObjectGetComm((PetscObject)viewer,&comm);
110: PetscViewerBinaryGetInfoPointer(viewer,&info);
111: PetscBagView(bag,viewer);
112: PetscFPrintf(comm,info,"%%--- begin code written by PetscViewerBinaryMatlabOutputBag ---%\n");
113: PetscFPrintf(comm,info,"%%$$ Set.%s = PetscBinaryRead(fd);\n",name);
114: PetscFPrintf(comm,info,"%%--- end code written by PetscViewerBinaryMatlabOutputBag ---%\n\n");
115: return(0);
116: }
117:
118: /*@C
119: PetscViewerBinaryMatlabOutputVec - Output a Vec object to the viewer and write matlab code to
120: the info file to read a (non-DA) Vec from binary.
122: Input Parameters:
123: + viewer - The viewer object
124: . name - The name of the field variable to be written
125: - vec -The Vec containing the field data
127: Level: intermediate
129: .seealso: PetscViewerBinaryMatlabOpen(), PetscViewerBinaryMatlabOutputBag(), PetscViewerBinaryMatlabOutputVecDA()
130: @*/
133: PetscErrorCode PetscViewerBinaryMatlabOutputVec(PetscViewer viewer, const char name[], Vec vec)
134: {
135: FILE *info;
136: MPI_Comm comm;
140: PetscObjectGetComm((PetscObject)viewer,&comm);
141: PetscViewerBinaryGetInfoPointer(viewer,&info);
142: VecView(vec,viewer);
143: PetscFPrintf(comm,info,"%%--- begin code written by PetscViewerBinaryMatlabOutputVec ---%\n");
144: PetscFPrintf(comm,info,"%%$$ Set.%s = PetscBinaryRead(fd);\n",name);
145: PetscFPrintf(comm,info,"%%--- end code written by PetscViewerBinaryMatlabOutputVec ---%\n\n");
146: return(0);
147: }
149: /*@C
150: PetscViewerBinaryMatlabOutputVecDA - Output a Vec object associtated with a DA to the viewer and write matlab code
151: to the info file to read a DA's Vec from binary.
153: Input Parameters:
154: + viewer - The viewer object
155: . name - The name of the field variable to be written
156: . vec - The Vec containing the field data to output
157: - da - The DA governing layout of Vec
159: Level: intermediate
161: Note: This method requires dof names to have been set using DASetFieldName().
163: .seealso: PetscViewerBinaryMatlabOpen(), PetscViewerBinaryMatlabOutputBag(), PetscViewerBinaryMatlabOutputVec(), DASetFieldName()
164: @*/
167: PetscErrorCode PetscViewerBinaryMatlabOutputVecDA(PetscViewer viewer, const char name[], Vec vec, DA da)
168: {
169: MPI_Comm comm;
170: FILE *info;
171: char *fieldname;
172: PetscInt dim,ni,nj,nk,pi,pj,pk,dof,n;
173: PetscTruth flg;
177: PetscObjectGetComm((PetscObject)viewer,&comm);
178: PetscViewerBinaryGetInfoPointer(viewer,&info);
179: DAGetInfo(da,&dim,&ni,&nj,&nk,&pi,&pj,&pk,&dof,PETSC_NULL,PETSC_NULL,PETSC_NULL);
180: VecView(vec,viewer);
181: PetscFPrintf(comm,info,"%%--- begin code written by PetscViewerBinaryMatlabOutputVecDA ---%\n");
182: PetscFPrintf(comm,info,"%%$$ tmp = PetscBinaryRead(fd); \n");
183: if (dim == 1) { PetscFPrintf(comm,info,"%%$$ tmp = reshape(tmp,%d,%d);\n",dof,ni); }
184: if (dim == 2) { PetscFPrintf(comm,info,"%%$$ tmp = reshape(tmp,%d,%d,%d);\n",dof,ni,nj); }
185: if (dim == 3) { PetscFPrintf(comm,info,"%%$$ tmp = reshape(tmp,%d,%d,%d,%d);\n",dof,ni,nj,nk); }
187: for(n=0; n<dof; n++) {
188: DAGetFieldName(da,n,&fieldname);
189: PetscStrcmp(fieldname,"",&flg);
190: if (!flg) {
191: if (dim == 1) { PetscFPrintf(comm,info,"%%$$ Set.%s.%s = squeeze(tmp(%d,:))';\n",name,fieldname,n+1); }
192: if (dim == 2) { PetscFPrintf(comm,info,"%%$$ Set.%s.%s = squeeze(tmp(%d,:,:))';\n",name,fieldname,n+1); }
193: if (dim == 3) { PetscFPrintf(comm,info,"%%$$ Set.%s.%s = permute(squeeze(tmp(%d,:,:,:)),[2 1 3]);\n",name,fieldname,n+1);}
194: }
195: }
196: PetscFPrintf(comm,info,"%%$$ clear tmp; \n");
197: PetscFPrintf(comm,info,"%%--- end code written by PetscViewerBinaryMatlabOutputVecDA ---%\n\n");
198: return(0);
199: }