Actual source code: readinvecs.c
2: /* Reads in PETSc vectors from a PETSc binary file into matlab
4: Since this is called from Matlab it cannot be compiled with C++.
5: Modified Sept 28, 2003 RFK: updated obsolete mx functions.
6: */
9: #include petscsys.h
10: #include petscvec.h
11: #include "mex.h"
12: #include <fcntl.h>
13: #if defined(PETSC_HAVE_UNISTD_H)
14: #include <unistd.h>
15: #endif
16: #if defined (PETSC_HAVE_IO_H)
17: #include <io.h>
18: #endif
19: #if defined(PETSC_HAVE_STRINGS_H)
20: #include <strings.h>
21: #endif
22: #if defined(PETSC_HAVE_STRING_H)
23: #include <string.h>
24: #endif
26: #define PETSC_MEX_ERROR(a) {fprintf(stdout,"ReadInVecs %s \n",a); return -1;}
27: /*-----------------------------------------------------------------*/
28: /*
29: Reads in a single vector
30: */
33: PetscErrorCode ReadInVecs(mxArray *plhs[],int t,int dim,int *dims)
34: {
35: int cookie = 0,M,i;
36: mxComplexity compx = mxREAL;
38: /* get size of matrix */
39: if (PetscBinaryRead(t,&cookie,1,PETSC_INT)) return -1; /* finished reading file */
40: if (cookie != VEC_FILE_COOKIE) PETSC_MEX_ERROR("could not read vector cookie");
41: if (PetscBinaryRead(t,&M,1,PETSC_INT)) PETSC_MEX_ERROR("reading number rows");
42:
43: if (dim == 1) {
44: plhs[0] = mxCreateDoubleMatrix(M,1,mxREAL);
45: } else if (dim == 2) {
46: if (dims[0]*dims[1] != M) {
47: printf("PETSC_MEX_ERROR: m %d * n %d != M %d\n",dims[0],dims[1],M);
48: return -1;
49: }
50: plhs[0] = mxCreateDoubleMatrix(dims[0],dims[1],mxREAL);
51: } else {
52: plhs[0] = mxCreateNumericArray(dim,dims,mxDOUBLE_CLASS,mxREAL);
53: }
55: /* read in matrix */
56: if (compx == mxREAL) { /* real */
57: if (PetscBinaryRead(t,mxGetPr(plhs[0]),M,PETSC_DOUBLE)) PETSC_MEX_ERROR("read dense matrix");
58: } else { /* complex, currently not used */
59: for (i=0; i<M; i++) {
60: if (PetscBinaryRead(t,mxGetPr(plhs[0])+i,1,PETSC_DOUBLE)) PETSC_MEX_ERROR("read dense matrix");
61: if (PetscBinaryRead(t,mxGetPi(plhs[0])+i,1,PETSC_DOUBLE)) PETSC_MEX_ERROR("read dense matrix");
62: }
63: }
64: return 0;
65: }
67: #undef PETSC_MEX_ERROR
68: #define PETSC_MEX_ERROR(a) {fprintf(stdout,"ReadInVecs %s \n",a); return;}
69: /*-----------------------------------------------------------------*/
73: void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
74: {
75: static int fd = -1,dims[4],dim = 1,dof;
76: char filename[1024],buffer[1024];
77: int err,d2,d3,d4;
78: FILE *file;
80: /* check output parameters */
81: if (nlhs != 1) PETSC_MEX_ERROR("Receive requires one output argument.");
82: if (fd == -1) {
83: if (!mxIsChar(prhs[0])) PETSC_MEX_ERROR("First arg must be string.");
84:
85: /* open the file */
86: mxGetString(prhs[0],filename,256);
87: fd = open(filename,O_RDONLY,0);
89: strcat(filename,".info");
90: file = fopen(filename,"r");
91: if (file) {
92: fgets(buffer,1024,file);
93: if (!strncmp(buffer,"-daload_info",12)) {
94: sscanf(buffer,"-daload_info %d,%d,%d,%d,%d,%d,%d,%d\n",&dim,&dims[0],&dims[1],&dims[2],&dof,&d2,&d3,&d4);
95: if (dof > 1) {
96: dim++;
97: dims[3] = dims[2];
98: dims[2] = dims[1];
99: dims[1] = dims[0];
100: dims[0] = dof;
101: }
102: }
103: fclose(file);
104: }
105: }
107: /* read in the next vector */
108: err = ReadInVecs(plhs,fd,dim,dims);
110: if (err) { /* file is finished so close and allow a restart */
111: close(fd);
112: fd = -1;
113: }
114: return;
115: }
118: