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
25: #if defined(PETSC_HAVE_STROPTS_H)
26: #include <stropts.h>
27: #endif
29: #define PETSC_MEX_ERROR(a) {fprintf(stdout,"ReadInVecs %s \n",a); return -1;}
30: /*-----------------------------------------------------------------*/
31: /*
32: Reads in a single vector
33: */
36: PetscErrorCode ReadInVecs(mxArray *plhs[],int t,int dim,int *dims)
37: {
38: int cookie = 0,M,i;
39: mxComplexity compx = mxREAL;
41: /* get size of matrix */
42: if (PetscBinaryRead(t,&cookie,1,PETSC_INT)) return -1; /* finished reading file */
43: if (cookie != VEC_FILE_COOKIE) PETSC_MEX_ERROR("could not read vector cookie");
44: if (PetscBinaryRead(t,&M,1,PETSC_INT)) PETSC_MEX_ERROR("reading number rows");
45:
46: if (dim == 1) {
47: plhs[0] = mxCreateDoubleMatrix(M,1,mxREAL);
48: } else if (dim == 2) {
49: if (dims[0]*dims[1] != M) {
50: printf("PETSC_MEX_ERROR: m %d * n %d != M %d\n",dims[0],dims[1],M);
51: return -1;
52: }
53: plhs[0] = mxCreateDoubleMatrix(dims[0],dims[1],mxREAL);
54: } else {
55: plhs[0] = mxCreateNumericArray(dim,dims,mxDOUBLE_CLASS,mxREAL);
56: }
58: /* read in matrix */
59: if (compx == mxREAL) { /* real */
60: if (PetscBinaryRead(t,mxGetPr(plhs[0]),M,PETSC_DOUBLE)) PETSC_MEX_ERROR("read dense matrix");
61: } else { /* complex, currently not used */
62: for (i=0; i<M; i++) {
63: if (PetscBinaryRead(t,mxGetPr(plhs[0])+i,1,PETSC_DOUBLE)) PETSC_MEX_ERROR("read dense matrix");
64: if (PetscBinaryRead(t,mxGetPi(plhs[0])+i,1,PETSC_DOUBLE)) PETSC_MEX_ERROR("read dense matrix");
65: }
66: }
67: return 0;
68: }
70: #undef PETSC_MEX_ERROR
71: #define PETSC_MEX_ERROR(a) {fprintf(stdout,"ReadInVecs %s \n",a); return;}
72: /*-----------------------------------------------------------------*/
76: void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
77: {
78: static int fd = -1,dims[4],dim = 1,dof;
79: char filename[1024],buffer[1024];
80: int err,d2,d3,d4;
81: FILE *file;
83: /* check output parameters */
84: if (nlhs != 1) PETSC_MEX_ERROR("Receive requires one output argument.");
85: if (fd == -1) {
86: if (!mxIsChar(prhs[0])) PETSC_MEX_ERROR("First arg must be string.");
87:
88: /* open the file */
89: mxGetString(prhs[0],filename,256);
90: fd = open(filename,O_RDONLY,0);
92: strcat(filename,".info");
93: file = fopen(filename,"r");
94: if (file) {
95: fgets(buffer,1024,file);
96: if (!strncmp(buffer,"-daload_info",12)) {
97: sscanf(buffer,"-daload_info %d,%d,%d,%d,%d,%d,%d,%d\n",&dim,&dims[0],&dims[1],&dims[2],&dof,&d2,&d3,&d4);
98: if (dof > 1) {
99: dim++;
100: dims[3] = dims[2];
101: dims[2] = dims[1];
102: dims[1] = dims[0];
103: dims[0] = dof;
104: }
105: }
106: fclose(file);
107: }
108: }
110: /* read in the next vector */
111: err = ReadInVecs(plhs,fd,dim,dims);
113: if (err) { /* file is finished so close and allow a restart */
114: close(fd);
115: fd = -1;
116: }
117: return;
118: }
121: