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: */


 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

 20: #define ERROR(a) {fprintf(stdout,"ReadInVecs %s \n",a); return -1;}
 21: /*-----------------------------------------------------------------*/
 22: /*
 23:        Reads in a single vector
 24: */
 27: int ReadInVecs(Matrix *plhs[],int t,int dim,int *dims)
 28: {
 29:   int    cookie = 0,M,compx = 0,i;
 30: 
 31:   /* get size of matrix */
 32:   if (PetscBinaryRead(t,&cookie,1,PETSC_INT))   return -1;  /* finished reading file */
 33:   if (cookie != VEC_FILE_COOKIE) ERROR("could not read vector cookie");
 34:   if (PetscBinaryRead(t,&M,1,PETSC_INT))        ERROR("reading number rows");
 35: 
 36:   if (dim == 1) {
 37:     plhs[0]  = mxCreateFull(M,1,compx);
 38:   } else if (dim == 2) {
 39:     if (dims[0]*dims[1] != M) {
 40:       printf("ERROR: m %d * n %d != M %d\n",dims[0],dims[1],M);
 41:       return -1;
 42:     }
 43:     plhs[0]  = mxCreateFull(dims[0],dims[1],compx);
 44:   } else {
 45:     plhs[0] = mxCreateNumericArray(dim,dims,mxDOUBLE_CLASS,mxREAL);
 46:   }

 48:   /* read in matrix */
 49:   if (!compx) {
 50:     if (PetscBinaryRead(t,mxGetPr(plhs[0]),M,PETSC_DOUBLE)) ERROR("read dense matrix");
 51:   } else {
 52:     for (i=0; i<M; i++) {
 53:       if (PetscBinaryRead(t,mxGetPr(plhs[0])+i,1,PETSC_DOUBLE)) ERROR("read dense matrix");
 54:       if (PetscBinaryRead(t,mxGetPi(plhs[0])+i,1,PETSC_DOUBLE)) ERROR("read dense matrix");
 55:     }
 56:   }
 57:   return 0;
 58: }

 60: #undef ERROR
 61: #define ERROR(a) {fprintf(stdout,"ReadInVecs %s \n",a); return;}
 62: /*-----------------------------------------------------------------*/

 66: void mexFunction(int nlhs,Matrix *plhs[],int nrhs,Matrix *prhs[])
 67: {
 68:   static int fd = -1,dims[4],dim = 1,dof;
 69:   char       filename[256],buffer[1024];
 70:   int        err,d2,d3,d4;
 71:   FILE       *file;

 73:   /* check output parameters */
 74:   if (nlhs != 1) ERROR("Receive requires one output argument.");
 75:   if (fd == -1) {
 76:     if (!mxIsString(prhs[0])) ERROR("First arg must be string.");
 77: 
 78:     /* open the file */
 79:     mxGetString(prhs[0],filename,256);
 80:     fd = open(filename,O_RDONLY,0);

 82:     strcat(filename,".info");
 83:     file = fopen(filename,"r");
 84:     if (file) {
 85:       fgets(buffer,1024,file);
 86:       if (!strncmp(buffer,"-daload_info",12)) {
 87:         sscanf(buffer,"-daload_info %d,%d,%d,%d,%d,%d,%d,%d\n",&dim,&dims[0],&dims[1],&dims[2],&dof,&d2,&d3,&d4);
 88:         if (dof > 1) {
 89:           dim++;
 90:           dims[3] = dims[2];
 91:           dims[2] = dims[1];
 92:           dims[1] = dims[0];
 93:           dims[0] = dof;
 94:         }
 95:       }
 96:       fclose(file);
 97:     }
 98:   }

100:   /* read in the next vector */
101:   err = ReadInVecs(plhs,fd,dim,dims);

103:   if (err) {  /* file is finished so close and allow a restart */
104:     close(fd);
105:     fd = -1;
106:   }
107:   return;
108: }


111: