Actual source code: readinvecs.c

  1: /*$Id: readinvecs.c,v 1.8 2001/03/23 23:19:53 balay Exp $*/

  3: /*    Reads in PETSc vectors from a PETSc binary file into matlab

  5:   Since this is called from Matlab it cannot be compiled with C++.
  6:   Modified Sept 28, 2003 RFK: updated obsolete mx functions.
  7: */


 10:  #include petscsys.h
 11:  #include petscvec.h
 12: #include "mex.h"
 13: #include <fcntl.h>
 14: #if defined(PETSC_HAVE_UNISTD_H)
 15: #include <unistd.h>
 16: #endif
 17: #if defined (PETSC_HAVE_IO_H)
 18: #include <io.h>
 19: #endif
 20: #if defined(PETSC_HAVE_STRINGS_H)
 21: #include <strings.h>
 22: #endif
 23: #if defined(PETSC_HAVE_STROPTS_H)
 24: #include <stropts.h>
 25: #endif

 27: #define ERROR(a) {fprintf(stdout,"ReadInVecs %s \n",a); return -1;}
 28: /*-----------------------------------------------------------------*/
 29: /*
 30:        Reads in a single vector
 31: */
 34: int ReadInVecs(mxArray *plhs[],int t,int dim,int *dims)
 35: {
 36:   int    cookie = 0,M,compx = 0,i;
 37: 
 38:   /* get size of matrix */
 39:   if (PetscBinaryRead(t,&cookie,1,PETSC_INT))   return -1;  /* finished reading file */
 40:   if (cookie != VEC_FILE_COOKIE) ERROR("could not read vector cookie");
 41:   if (PetscBinaryRead(t,&M,1,PETSC_INT))        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("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) { /* real */
 57:     if (PetscBinaryRead(t,mxGetPr(plhs[0]),M,PETSC_DOUBLE)) 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)) ERROR("read dense matrix");
 61:       if (PetscBinaryRead(t,mxGetPi(plhs[0])+i,1,PETSC_DOUBLE)) ERROR("read dense matrix");
 62:     }
 63:   }
 64:   return 0;
 65: }

 67: #undef ERROR
 68: #define 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[256],buffer[1024];
 77:   int        err,d2,d3,d4;
 78:   FILE       *file;

 80:   /* check output parameters */
 81:   if (nlhs != 1) ERROR("Receive requires one output argument.");
 82:   if (fd == -1) {
 83:     if (!mxIsChar(prhs[0])) 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: