xref: /petsc/src/mat/tests/ex171.c (revision ebead697dbf761eb322f829370bbe90b3bd93fa3)
1 
2 static char help[] = "Tests MatDiagonalSet() on MatLoad() matrix \n\n";
3 
4 #include <petscmat.h>
5 
6 int main(int argc,char **args)
7 {
8   Mat            A;
9   Vec            x;
10   PetscViewer    fd;              /* viewer */
11   char           file[PETSC_MAX_PATH_LEN]; /* input file name */
12   PetscReal      norm;
13   PetscBool      flg;
14 
15   PetscFunctionBeginUser;
16   PetscCall(PetscInitialize(&argc,&args,(char*)0,help));
17   /* Determine file from which we read the matrix A */
18   PetscCall(PetscOptionsGetString(NULL,NULL,"-f",file,sizeof(file),&flg));
19   PetscCheck(flg,PETSC_COMM_WORLD,PETSC_ERR_USER,"Must indicate binary file with the -f option");
20 
21   /* Load matrix A */
22   PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD,file,FILE_MODE_READ,&fd));
23   PetscCall(MatCreate(PETSC_COMM_WORLD,&A));
24   PetscCall(MatLoad(A,fd));
25   PetscCall(PetscViewerDestroy(&fd));
26   PetscCall(MatCreateVecs(A,&x,NULL));
27   PetscCall(MatGetDiagonal(A,x));
28   PetscCall(VecScale(x,-1.0));
29   PetscCall(MatDiagonalSet(A,x,ADD_VALUES));
30   PetscCall(MatGetDiagonal(A,x));
31   PetscCall(VecNorm(x,NORM_2,&norm));
32   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Norm %g\n",(double)norm));
33 
34   /* Free data structures */
35   PetscCall(MatDestroy(&A));
36   PetscCall(VecDestroy(&x));
37   PetscCall(PetscFinalize());
38   return 0;
39 }
40 
41 /*TEST
42 
43    test:
44       nsize: 4
45       requires: datafilespath !complex double !defined(PETSC_USE_64BIT_INDICES)
46       args: -f ${wPETSC_DIR}/share/petsc/datafiles/matrices/ns-real-int32-float64 -malloc_dump
47 
48 TEST*/
49