Actual source code: ex6.c

petsc-3.10.5 2019-03-28
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 PetscBinaryWrite() 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>

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

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

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

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

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

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

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

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

 50:   /* ---------------------------------------------------------------------- */
 51:   /*          PART 2: Read data from file and form a vector                 */
 52:   /* ---------------------------------------------------------------------- */

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

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

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

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

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

 75:   /* Free data structures */
 76:   VecDestroy(&vec);
 77:   PetscViewerDestroy(&view_in);
 78:   PetscFinalize();
 79:   return ierr;
 80: }

 82: /*TEST

 84:      test:

 86: TEST*/