Actual source code: ex34.c

petsc-3.6.1 2015-08-06
Report Typos and Errors
  2: static char help[] = "Reads a matrix and vector from a file and writes to another. Input options:\n\
  3:   -fin <input_file> : file to load.  For example see $PETSC_DIR/share/petsc/datafiles/matrices\n\
  4:   -fout <output_file> : file for saving output matrix and vector\n\n";

  6: #include <petscmat.h>

 10: int main(int argc,char **args)
 11: {
 13:   PetscBool      flg;
 14:   Vec            x;
 15:   Mat            A;
 16:   char           file[256];
 17:   PetscViewer    fd;

 19:   PetscInitialize(&argc,&args,(char*)0,help);

 21:   /* Read matrix and RHS */
 22:   PetscOptionsGetString(NULL,"-fin",file,256,&flg);
 23:   if (!flg) SETERRQ(PETSC_COMM_WORLD,1,help);
 24:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,file,FILE_MODE_READ,&fd);
 25:   MatCreate(PETSC_COMM_WORLD,&A);
 26:   MatSetType(A,MATSEQAIJ);
 27:   MatLoad(A,fd);
 28:   VecCreate(PETSC_COMM_WORLD,&x);
 29:   VecLoad(x,fd);
 30:   PetscViewerDestroy(&fd);

 32:   /* Write matrix and vector */
 33:   PetscOptionsGetString(NULL,"-fout",file,256,&flg);
 34:   if (!flg) SETERRQ(PETSC_COMM_WORLD,1,help);
 35:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,file,FILE_MODE_WRITE,&fd);
 36:   MatView(A,fd);
 37:   VecView(x,fd);

 39:   /* Free data structures */
 40:   MatDestroy(&A);
 41:   VecDestroy(&x);
 42:   PetscViewerDestroy(&fd);

 44:   PetscFinalize();
 45:   return 0;
 46: }