Actual source code: ex34.c

petsc-3.8.4 2018-03-24
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>

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

 17:   PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
 18:   /* Read matrix and RHS */
 19:   PetscOptionsGetString(NULL,NULL,"-fin",file,256,&flg);
 20:   if (!flg) SETERRQ(PETSC_COMM_WORLD,1,help);
 21:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,file,FILE_MODE_READ,&fd);
 22:   MatCreate(PETSC_COMM_WORLD,&A);
 23:   MatSetType(A,MATSEQAIJ);
 24:   MatLoad(A,fd);
 25:   VecCreate(PETSC_COMM_WORLD,&x);
 26:   VecLoad(x,fd);
 27:   PetscViewerDestroy(&fd);

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

 36:   /* Free data structures */
 37:   MatDestroy(&A);
 38:   VecDestroy(&x);
 39:   PetscViewerDestroy(&fd);

 41:   PetscFinalize();
 42:   return ierr;
 43: }