Actual source code: ex6.c

petsc-3.5.0 2014-06-30
Report Typos and Errors
  2: static char help[] = "Writes an array to a file, then reads an array from a file, then forms a vector.\n\n";

  4: /*
  5:     This uses the low level PetscBinarWrite() and PetscBinaryRead() to access a binary file. It will not work in parallel! 

  7:     We HIGHLY recommend using instead VecView() and VecLoad() to read and write Vectors in binary format (which also work in parallel). Then you can use
  8:     share/petsc/matlab/PetscBinaryRead() and share/petsc/matlab/PetscBinaryWrite() to read (or write) the vector into MATLAB.

 10:     Note this also works for matrices with MatView() and MatLoad().
 11: */
 12: #include <petscvec.h>

 16: int main(int argc,char **args)
 17: {
 19:   PetscMPIInt    size;
 20:   int            fd;
 21:   PetscInt       i,m = 10,sz;
 22:   PetscScalar    *avec,*array;
 23:   Vec            vec;
 24:   PetscViewer    view_out,view_in;

 26:   PetscInitialize(&argc,&args,(char*)0,help);
 27:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
 28:   if (size != 1) SETERRQ(PETSC_COMM_SELF,1,"This is a uniprocessor example only!");

 30:   PetscOptionsGetInt(NULL,"-m",&m,NULL);

 32:   /* ---------------------------------------------------------------------- */
 33:   /*          PART 1: Write some data to a file in binary format            */
 34:   /* ---------------------------------------------------------------------- */

 36:   /* Allocate array and set values */
 37:   PetscMalloc1(m,&array);
 38:   for (i=0; i<m; i++) array[i] = i*10.0;

 40:   /* Open viewer for binary output */
 41:   PetscViewerBinaryOpen(PETSC_COMM_SELF,"input.dat",FILE_MODE_WRITE,&view_out);
 42:   PetscViewerBinaryGetDescriptor(view_out,&fd);

 44:   /* Write binary output */
 45:   PetscBinaryWrite(fd,&m,1,PETSC_INT,PETSC_FALSE);
 46:   PetscBinaryWrite(fd,array,m,PETSC_SCALAR,PETSC_FALSE);

 48:   /* Destroy the output viewer and work array */
 49:   PetscViewerDestroy(&view_out);
 50:   PetscFree(array);

 52:   /* ---------------------------------------------------------------------- */
 53:   /*          PART 2: Read data from file and form a vector                 */
 54:   /* ---------------------------------------------------------------------- */

 56:   /* Open input binary viewer */
 57:   PetscViewerBinaryOpen(PETSC_COMM_SELF,"input.dat",FILE_MODE_READ,&view_in);
 58:   PetscViewerBinaryGetDescriptor(view_in,&fd);

 60:   /* Create vector and get pointer to data space */
 61:   VecCreate(PETSC_COMM_SELF,&vec);
 62:   VecSetSizes(vec,PETSC_DECIDE,m);
 63:   VecSetFromOptions(vec);
 64:   VecGetArray(vec,&avec);

 66:   /* Read data into vector */
 67:   PetscBinaryRead(fd,&sz,1,PETSC_INT);
 68:   if (sz <=0) SETERRQ(PETSC_COMM_SELF,1,"Error: Must have array length > 0");

 70:   PetscPrintf(PETSC_COMM_SELF,"reading data in binary from input.dat, sz =%D ...\n",sz);
 71:   PetscBinaryRead(fd,avec,sz,PETSC_SCALAR);

 73:   /* View vector */
 74:   VecRestoreArray(vec,&avec);
 75:   VecView(vec,PETSC_VIEWER_STDOUT_SELF);

 77:   /* Free data structures */
 78:   VecDestroy(&vec);
 79:   PetscViewerDestroy(&view_in);
 80:   PetscFinalize();
 81:   return 0;
 82: }