Actual source code: ex171.c

petsc-3.6.1 2015-08-06
Report Typos and Errors
  2: static char help[] = "Tests MatDiagonalSet() on MatLoad() matrix \n\n";

  4: #include <petscmat.h>

  8: int main(int argc,char **args)
  9: {
 10:   Mat            A;
 11:   Vec            x;
 13:   PetscViewer    fd;              /* viewer */
 14:   char           file[PETSC_MAX_PATH_LEN]; /* input file name */
 15:   PetscReal      norm;
 16:   PetscBool      flg;

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

 20:   /* Determine file from which we read the matrix A */
 21:   PetscOptionsGetString(NULL,"-f",file,PETSC_MAX_PATH_LEN,&flg);
 22:   if (!flg) SETERRQ(PETSC_COMM_WORLD,1,"Must indicate binary file with the -f option");

 24:   /* Load matrix A */
 25:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,file,FILE_MODE_READ,&fd);
 26:   MatCreate(PETSC_COMM_WORLD,&A);
 27:   MatLoad(A,fd);
 28:   PetscViewerDestroy(&fd);
 29:   MatCreateVecs(A,&x,NULL);
 30:   MatGetDiagonal(A,x);
 31:   VecScale(x,-1.0);
 32:   MatDiagonalSet(A,x,ADD_VALUES);
 33:   MatGetDiagonal(A,x);
 34:   VecNorm(x,NORM_2,&norm);
 35:   PetscPrintf(PETSC_COMM_WORLD,"Norm %g\n",(double)norm);

 37:   /* Free data structures */
 38:   MatDestroy(&A);
 39:   VecDestroy(&x);
 40:   PetscFinalize();
 41:   return 0;
 42: }