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.
30: Collective on MPI_Comm
32: Input Parameters:
33: + comm - The communicator
34: - fname - The output filename
36: Output Parameter:
37: . viewer - The viewer object
39: Level: beginner
41: .seealso: PetscViewerBinaryMatlabDestroy()
42: @*/
43: PetscErrorCode PetscViewerBinaryMatlabOpen(MPI_Comm comm, const char fname[], PetscViewer *viewer)
44: {
45: FILE *info;
49: PetscViewerBinaryOpen(comm,fname,FILE_MODE_WRITE,viewer);
50: PetscViewerBinaryGetInfoPointer(*viewer,&info);
51: PetscFPrintf(comm,info,"%%--- begin code written by PetscWriteOutputInitialize ---%\n");
52: PetscFPrintf(comm,info,"%%$$ Set.filename = '%s';\n",fname);
53: PetscFPrintf(comm,info,"%%$$ fd = fopen(Set.filename, 'r', 'ieee-be');\n");
54: PetscFPrintf(comm,info,"%%$$ if (fd < 0) error('Cannot open %s, check for existence of file'); end\n",fname);
55: PetscFPrintf(comm,info,"%%--- end code written by PetscWriteOutputInitialize ---%\n\n");
56: return(0);
57: }
59: /*@C
60: PetscViewerBinaryMatlabDestroy - Write matlab info file finalization and destroy viewer.
62: Not Collective
64: Input Parameter:
65: . viewer - The viewer object
67: Level: beginner
69: .seealso PetscViewerBinaryMatlabOpen(), PetscViewerBinaryMatlabOutputVec(),
70: PetscViewerBinaryMatlabOutputVecDA(), PetscViewerBinaryMatlabOutputBag()
71: @*/
74: PetscErrorCode PetscViewerBinaryMatlabDestroy(PetscViewer viewer)
75: {
76: FILE *info;
77: MPI_Comm comm;
81: PetscObjectGetComm((PetscObject)viewer,&comm);
82: PetscViewerBinaryGetInfoPointer(viewer,&info);
83: PetscFPrintf(comm,info,"%%--- begin code written by PetscWriteOutputFinalize ---%\n");
84: PetscFPrintf(comm,info,"%%$$ fclose(fd);\n");
85: PetscFPrintf(comm,info,"%%--- end code written by PetscWriteOutputFinalize ---%\n\n");
86: PetscViewerFlush(viewer);
87: PetscViewerDestroy(viewer);
88: return(0);
89: }
91: /*@C
92: PetscViewerBinaryMatlabOutputBag - Write matlab code to info file to read a PetscBag from binary.
94: Input Parameters:
95: + viewer - The viewer object
96: . name - The bag name
97: - bag - The bag object containing data to output
99: Level: intermediate
101: .seealso: PetscViewerBinaryMatlabOpen(), PetscViewerBinaryMatlabOutputVec(), PetscViewerBinaryMatlabOutputVecDA()
102: @*/
105: PetscErrorCode PetscViewerBinaryMatlabOutputBag(PetscViewer viewer, const char name[], PetscBag bag)
106: {
107: FILE *info;
108: MPI_Comm comm;
112: PetscObjectGetComm((PetscObject)viewer,&comm);
113: PetscViewerBinaryGetInfoPointer(viewer,&info);
114: PetscBagView(bag,viewer);
115: PetscFPrintf(comm,info,"%%--- begin code written by PetscWriteOutputBag ---%\n");
116: PetscFPrintf(comm,info,"%%$$ Set.%s = PetscBinaryRead(fd);\n",name);
117: PetscFPrintf(comm,info,"%%--- end code written by PetscWriteOutputBag ---%\n\n");
118: return(0);
119: }
120:
121: /*@C
122: PetscViewerBinaryMatlabOutputVec - Write matlab code to info file to read a (non DA) Vec from binary.
124: Input Parameters:
125: + viewer - The viewer object
126: . name - The name of the field variable to be written
127: - vec -The Vec containing the field data
129: Level: intermediate
131: .seealso: PetscViewerBinaryMatlabOpen(), PetscViewerBinaryMatlabOutputBag(), PetscViewerBinaryMatlabOutputVecDA()
132: @*/
135: PetscErrorCode PetscViewerBinaryMatlabOutputVec(PetscViewer viewer, const char name[], Vec vec)
136: {
137: FILE *info;
138: MPI_Comm comm;
142: PetscObjectGetComm((PetscObject)viewer,&comm);
143: PetscViewerBinaryGetInfoPointer(viewer,&info);
144: VecView(vec,viewer);
145: PetscFPrintf(comm,info,"%%--- begin code written by PetscWriteOutputVec ---%\n");
146: PetscFPrintf(comm,info,"%%$$ Set.%s = PetscBinaryRead(fd);\n",name);
147: PetscFPrintf(comm,info,"%%--- end code written by PetscWriteOutputVec ---%\n\n");
148: return(0);
149: }
151: /*@C
152: PetscViewerBinaryMatlabOutputVecDA - Write matlab code to info file to read a DA's Vec from binary.
154: Input Parameters:
155: + viewer - The viewer object
156: . name - The name of the field variable to be written
157: . vec - The Vec containing the field data to output
158: - da - The DA governing layout of Vec
160: Level: intermediate
162: Note: This method requires dof names have been set using DASetFieldName().
164: .seealso: PetscViewerBinaryMatlabOpen(), PetscViewerBinaryMatlabOutputBag(), PetscViewerBinaryMatlabOutputVec(), DASetFieldName()
165: @*/
168: PetscErrorCode PetscViewerBinaryMatlabOutputVecDA(PetscViewer viewer, const char name[], Vec vec, DA da)
169: {
170: MPI_Comm comm;
171: FILE *info;
172: char *fieldname;
173: PetscInt dim,ni,nj,nk,pi,pj,pk,dof,n;
174: PetscTruth flg;
178: PetscObjectGetComm((PetscObject)viewer,&comm);
179: PetscViewerBinaryGetInfoPointer(viewer,&info);
180: DAGetInfo(da,&dim,&ni,&nj,&nk,&pi,&pj,&pk,&dof,PETSC_NULL,PETSC_NULL,PETSC_NULL);
181: VecView(vec,viewer);
182: PetscFPrintf(comm,info,"%%--- begin code written by PetscWriteOutputVecDA ---%\n");
183: PetscFPrintf(comm,info,"%%$$ tmp = PetscBinaryRead(fd); \n");
184: if (dim == 1) { PetscFPrintf(comm,info,"%%$$ tmp = reshape(tmp,%d,%d);\n",dof,ni); }
185: if (dim == 2) { PetscFPrintf(comm,info,"%%$$ tmp = reshape(tmp,%d,%d,%d);\n",dof,ni,nj); }
186: if (dim == 3) { PetscFPrintf(comm,info,"%%$$ tmp = reshape(tmp,%d,%d,%d,%d);\n",dof,ni,nj,nk); }
188: for(n=0; n<dof; n++) {
189: DAGetFieldName(da,n,&fieldname);
190: PetscStrcmp(fieldname,"",&flg);
191: if (!flg) {
192: if (dim == 1) { PetscFPrintf(comm,info,"%%$$ Set.%s.%s = squeeze(tmp(%d,:))';\n",name,fieldname,n+1); }
193: if (dim == 2) { PetscFPrintf(comm,info,"%%$$ Set.%s.%s = squeeze(tmp(%d,:,:))';\n",name,fieldname,n+1); }
194: if (dim == 3) { PetscFPrintf(comm,info,"%%$$ Set.%s.%s = permute(squeeze(tmp(%d,:,:,:)),[2 1 3]);\n",name,fieldname,n+1);}
195: }
196: }
197: PetscFPrintf(comm,info,"%%$$ clear tmp; \n");
198: PetscFPrintf(comm,info,"%%--- end code written by PetscWriteOutputVecDA ---%\n\n");
199: return(0);
200: }